2014-11-21 12:26:58 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
#ifndef AP_Compass_AK8963_H
|
|
|
|
#define AP_Compass_AK8963_H
|
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-08-15 19:49:52 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2014-11-21 12:26:58 -04:00
|
|
|
|
|
|
|
#include "Compass.h"
|
2014-11-15 21:58:23 -04:00
|
|
|
#include "AP_Compass_Backend.h"
|
2014-11-21 12:26:58 -04:00
|
|
|
|
2015-07-06 14:50:59 -03:00
|
|
|
class AP_AK8963_SerialBus
|
|
|
|
{
|
|
|
|
public:
|
2015-07-21 03:49:42 -03:00
|
|
|
struct PACKED raw_value {
|
|
|
|
int16_t val[3];
|
|
|
|
uint8_t st2;
|
|
|
|
};
|
|
|
|
|
2015-07-06 14:50:59 -03:00
|
|
|
virtual void register_read(uint8_t address, uint8_t *value, uint8_t count) = 0;
|
|
|
|
uint8_t register_read(uint8_t address) {
|
|
|
|
uint8_t reg;
|
|
|
|
register_read(address, ®, 1);
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
virtual void register_write(uint8_t address, uint8_t value) = 0;
|
|
|
|
virtual AP_HAL::Semaphore* get_semaphore() = 0;
|
|
|
|
virtual bool configure() = 0;
|
2015-07-22 16:11:20 -03:00
|
|
|
virtual bool start_measurements() = 0;
|
2015-07-21 03:49:42 -03:00
|
|
|
virtual void read_raw(struct raw_value *rv) = 0;
|
2015-07-10 01:05:21 -03:00
|
|
|
virtual uint32_t get_dev_id() = 0;
|
2015-07-06 14:50:59 -03:00
|
|
|
};
|
|
|
|
|
2014-11-15 21:58:23 -04:00
|
|
|
class AP_Compass_AK8963 : public AP_Compass_Backend
|
2014-11-21 12:26:58 -04:00
|
|
|
{
|
2015-02-23 19:17:44 -04:00
|
|
|
public:
|
2015-07-06 14:50:59 -03:00
|
|
|
AP_Compass_AK8963(Compass &compass, AP_AK8963_SerialBus *bus);
|
2015-02-23 19:17:44 -04:00
|
|
|
|
2015-07-10 01:05:21 -03:00
|
|
|
static AP_Compass_Backend *detect_mpu9250(Compass &compass);
|
|
|
|
static AP_Compass_Backend *detect_i2c1(Compass &compass);
|
2015-07-02 10:37:37 -03:00
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
bool init(void);
|
|
|
|
void read(void);
|
|
|
|
void accumulate(void);
|
|
|
|
|
2014-11-21 12:26:58 -04:00
|
|
|
private:
|
2015-07-22 08:50:32 -03:00
|
|
|
void _make_factory_sensitivity_adjustment(Vector3f& field) const;
|
|
|
|
Vector3f _get_filtered_field() const;
|
|
|
|
void _reset_filter();
|
|
|
|
|
2015-07-02 10:37:37 -03:00
|
|
|
bool _reset();
|
2015-07-22 16:11:20 -03:00
|
|
|
bool _setup_mode();
|
2015-07-02 10:37:37 -03:00
|
|
|
bool _check_id();
|
|
|
|
bool _calibrate();
|
|
|
|
|
|
|
|
void _update();
|
|
|
|
void _dump_registers();
|
|
|
|
bool _sem_take_blocking();
|
2015-07-06 14:50:59 -03:00
|
|
|
bool _sem_take_nonblocking();
|
2015-07-02 10:37:37 -03:00
|
|
|
bool _sem_give();
|
|
|
|
|
|
|
|
float _magnetometer_ASA[3] {0, 0, 0};
|
|
|
|
uint8_t _compass_instance;
|
2014-11-21 12:26:58 -04:00
|
|
|
|
|
|
|
float _mag_x_accum;
|
|
|
|
float _mag_y_accum;
|
|
|
|
float _mag_z_accum;
|
|
|
|
uint32_t _accum_count;
|
|
|
|
|
2015-07-02 10:37:37 -03:00
|
|
|
bool _initialized;
|
2014-11-21 12:26:58 -04:00
|
|
|
uint32_t _last_update_timestamp;
|
|
|
|
uint32_t _last_accum_time;
|
2015-07-06 14:50:59 -03:00
|
|
|
|
|
|
|
AP_AK8963_SerialBus *_bus;
|
|
|
|
AP_HAL::Semaphore *_bus_sem;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AP_AK8963_SerialBus_MPU9250: public AP_AK8963_SerialBus
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AP_AK8963_SerialBus_MPU9250();
|
|
|
|
void register_read(uint8_t address, uint8_t *value, uint8_t count);
|
|
|
|
void register_write(uint8_t address, uint8_t value);
|
|
|
|
AP_HAL::Semaphore* get_semaphore();
|
|
|
|
bool configure();
|
2015-07-22 16:11:20 -03:00
|
|
|
bool start_measurements();
|
2015-07-21 03:49:42 -03:00
|
|
|
void read_raw(struct raw_value *rv);
|
2015-07-10 01:05:21 -03:00
|
|
|
uint32_t get_dev_id();
|
2015-07-06 14:50:59 -03:00
|
|
|
private:
|
|
|
|
void _read(uint8_t address, uint8_t *value, uint32_t count);
|
|
|
|
void _write(uint8_t address, const uint8_t *value, uint32_t count);
|
|
|
|
void _write(uint8_t address, const uint8_t value) {
|
|
|
|
_write(address, &value, 1);
|
|
|
|
}
|
|
|
|
AP_HAL::SPIDeviceDriver *_spi;
|
|
|
|
AP_HAL::Semaphore *_spi_sem;
|
2014-11-21 12:26:58 -04:00
|
|
|
};
|
|
|
|
|
2015-07-10 01:05:21 -03:00
|
|
|
class AP_AK8963_SerialBus_I2C: public AP_AK8963_SerialBus
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AP_AK8963_SerialBus_I2C(AP_HAL::I2CDriver *i2c, uint8_t addr);
|
|
|
|
void register_read(uint8_t address, uint8_t *value, uint8_t count);
|
|
|
|
void register_write(uint8_t address, uint8_t value);
|
|
|
|
AP_HAL::Semaphore* get_semaphore();
|
2015-07-22 16:11:20 -03:00
|
|
|
bool configure(){ return true; }
|
|
|
|
bool start_measurements() { return true; }
|
2015-07-21 03:49:42 -03:00
|
|
|
void read_raw(struct raw_value *rv);
|
2015-07-10 01:05:21 -03:00
|
|
|
uint32_t get_dev_id();
|
|
|
|
private:
|
|
|
|
void _read(uint8_t address, uint8_t *value, uint32_t count);
|
|
|
|
void _write(uint8_t address, const uint8_t *value, uint32_t count);
|
|
|
|
void _write(uint8_t address, const uint8_t value) {
|
|
|
|
_write(address, &value, 1);
|
|
|
|
}
|
|
|
|
AP_HAL::I2CDriver *_i2c;
|
|
|
|
AP_HAL::Semaphore *_i2c_sem;
|
2015-07-21 11:37:07 -03:00
|
|
|
uint8_t _addr;
|
2015-07-10 01:05:21 -03:00
|
|
|
};
|
2014-11-21 12:26:58 -04:00
|
|
|
#endif
|