2016-02-17 21:25:20 -04:00
|
|
|
#pragma once
|
2011-02-14 00:27:07 -04:00
|
|
|
|
2015-08-15 19:49:52 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
2016-03-04 13:59:06 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_HAL/I2CDevice.h>
|
2015-08-15 19:49:52 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2011-02-14 00:27:07 -04:00
|
|
|
|
2016-03-10 20:41:18 -04:00
|
|
|
#include "AP_Compass.h"
|
2014-11-15 21:58:23 -04:00
|
|
|
#include "AP_Compass_Backend.h"
|
2011-02-14 00:27:07 -04:00
|
|
|
|
2015-08-10 20:30:32 -03:00
|
|
|
class AuxiliaryBus;
|
|
|
|
class AuxiliaryBusSlave;
|
|
|
|
class AP_InertialSensor;
|
2016-03-04 13:59:06 -04:00
|
|
|
class AP_HMC5843_BusDriver;
|
2015-08-07 19:54:58 -03:00
|
|
|
|
2014-11-15 21:58:23 -04:00
|
|
|
class AP_Compass_HMC5843 : public AP_Compass_Backend
|
2011-02-14 00:27:07 -04:00
|
|
|
{
|
2012-08-17 03:19:22 -03:00
|
|
|
public:
|
2016-03-04 13:59:06 -04:00
|
|
|
static AP_Compass_Backend *probe(Compass &compass,
|
2016-01-22 11:30:30 -04:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,
|
|
|
|
bool force_external = false);
|
2015-08-07 19:54:58 -03:00
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
static AP_Compass_Backend *probe_mpu6000(Compass &compass);
|
2015-08-07 19:54:58 -03:00
|
|
|
|
2016-05-13 13:47:05 -03:00
|
|
|
static constexpr const char *name = "HMC5843";
|
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
virtual ~AP_Compass_HMC5843();
|
2016-02-26 13:42:26 -04:00
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
bool init() override;
|
|
|
|
void read() override;
|
|
|
|
void accumulate() override;
|
2016-02-26 13:42:26 -04:00
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
private:
|
2016-01-22 11:30:30 -04:00
|
|
|
AP_Compass_HMC5843(Compass &compass, AP_HMC5843_BusDriver *bus,
|
|
|
|
bool force_external);
|
2016-02-26 13:42:26 -04:00
|
|
|
|
|
|
|
bool _detect_version();
|
2016-03-04 13:59:06 -04:00
|
|
|
bool _calibrate();
|
2016-03-07 20:10:08 -04:00
|
|
|
bool _setup_sampling_mode();
|
2016-02-26 13:42:26 -04:00
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
/* Read a single sample */
|
|
|
|
bool _read_sample();
|
|
|
|
|
|
|
|
AP_HMC5843_BusDriver *_bus;
|
2016-02-26 13:42:26 -04:00
|
|
|
|
|
|
|
float _scaling[3];
|
2016-03-07 20:10:08 -04:00
|
|
|
float _gain_scale;
|
2016-02-26 13:42:26 -04:00
|
|
|
uint32_t _last_accum_time;
|
|
|
|
|
|
|
|
// when unhealthy the millis() value to retry at
|
|
|
|
uint32_t _retry_time;
|
|
|
|
|
|
|
|
int16_t _mag_x;
|
|
|
|
int16_t _mag_y;
|
|
|
|
int16_t _mag_z;
|
|
|
|
int16_t _mag_x_accum;
|
|
|
|
int16_t _mag_y_accum;
|
|
|
|
int16_t _mag_z_accum;
|
|
|
|
uint8_t _accum_count;
|
|
|
|
|
|
|
|
uint8_t _base_config;
|
|
|
|
uint8_t _compass_instance;
|
2016-03-07 20:10:08 -04:00
|
|
|
uint8_t _gain_config;
|
2016-02-26 13:42:26 -04:00
|
|
|
uint8_t _product_id;
|
|
|
|
|
|
|
|
bool _initialised;
|
2016-01-22 11:30:30 -04:00
|
|
|
bool _force_external;
|
2015-08-07 19:54:58 -03:00
|
|
|
};
|
2011-06-28 13:30:42 -03:00
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
class AP_HMC5843_BusDriver
|
2015-08-07 19:54:58 -03:00
|
|
|
{
|
|
|
|
public:
|
2016-03-04 13:59:06 -04:00
|
|
|
virtual ~AP_HMC5843_BusDriver() { }
|
|
|
|
|
|
|
|
virtual bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) = 0;
|
|
|
|
virtual bool register_read(uint8_t reg, uint8_t *val) = 0;
|
|
|
|
virtual bool register_write(uint8_t reg, uint8_t val) = 0;
|
|
|
|
|
|
|
|
virtual AP_HAL::Semaphore *get_semaphore() = 0;
|
|
|
|
|
2015-08-10 20:30:32 -03:00
|
|
|
virtual bool configure() { return true; }
|
|
|
|
virtual bool start_measurements() { return true; }
|
2015-08-07 19:54:58 -03:00
|
|
|
};
|
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
class AP_HMC5843_BusDriver_HALDevice : public AP_HMC5843_BusDriver
|
2015-08-07 19:54:58 -03:00
|
|
|
{
|
|
|
|
public:
|
2016-03-04 13:59:06 -04:00
|
|
|
AP_HMC5843_BusDriver_HALDevice(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev);
|
|
|
|
|
|
|
|
bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;
|
|
|
|
bool register_read(uint8_t reg, uint8_t *val) override;
|
|
|
|
bool register_write(uint8_t reg, uint8_t val) override;
|
|
|
|
|
|
|
|
AP_HAL::Semaphore *get_semaphore() override;
|
2014-11-15 21:58:23 -04:00
|
|
|
|
2015-08-07 19:54:58 -03:00
|
|
|
private:
|
2016-03-04 13:59:06 -04:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> _dev;
|
2011-02-14 00:27:07 -04:00
|
|
|
};
|
2015-08-07 19:54:58 -03:00
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
class AP_HMC5843_BusDriver_Auxiliary : public AP_HMC5843_BusDriver
|
2015-08-10 20:30:32 -03:00
|
|
|
{
|
|
|
|
public:
|
2016-03-04 13:59:06 -04:00
|
|
|
AP_HMC5843_BusDriver_Auxiliary(AP_InertialSensor &ins, uint8_t backend_id,
|
|
|
|
uint8_t addr);
|
|
|
|
virtual ~AP_HMC5843_BusDriver_Auxiliary();
|
|
|
|
|
|
|
|
bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;
|
|
|
|
bool register_read(uint8_t reg, uint8_t *val) override;
|
|
|
|
bool register_write(uint8_t reg, uint8_t val) override;
|
|
|
|
|
|
|
|
AP_HAL::Semaphore *get_semaphore() override;
|
|
|
|
|
2015-08-10 20:30:32 -03:00
|
|
|
bool configure() override;
|
|
|
|
bool start_measurements() override;
|
|
|
|
|
|
|
|
private:
|
2016-02-26 13:42:26 -04:00
|
|
|
AuxiliaryBus *_bus;
|
|
|
|
AuxiliaryBusSlave *_slave;
|
|
|
|
bool _started;
|
2015-08-10 20:30:32 -03:00
|
|
|
};
|