2016-02-17 21:25:20 -04:00
|
|
|
#pragma once
|
2011-02-14 00:27:07 -04:00
|
|
|
|
2023-01-29 19:33:08 -04:00
|
|
|
#include "AP_Compass_config.h"
|
|
|
|
|
|
|
|
#if AP_COMPASS_HMC5843_ENABLED
|
|
|
|
|
|
|
|
#ifndef HAL_COMPASS_HMC5843_I2C_ADDR
|
|
|
|
#define HAL_COMPASS_HMC5843_I2C_ADDR 0x1E
|
|
|
|
#endif
|
|
|
|
|
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>
|
2016-11-04 03:57:52 -03:00
|
|
|
#include <AP_HAL/Device.h>
|
2011-02-14 00:27:07 -04:00
|
|
|
|
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:
|
2018-08-06 19:21:27 -03:00
|
|
|
static AP_Compass_Backend *probe(AP_HAL::OwnPtr<AP_HAL::Device> dev,
|
2019-09-01 19:17:05 -03:00
|
|
|
bool force_external,
|
|
|
|
enum Rotation rotation);
|
2015-08-07 19:54:58 -03:00
|
|
|
|
2019-09-01 19:17:05 -03:00
|
|
|
static AP_Compass_Backend *probe_mpu6000(enum Rotation rotation);
|
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
|
|
|
void read() override;
|
2016-02-26 13:42:26 -04:00
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
private:
|
2018-08-06 19:21:27 -03:00
|
|
|
AP_Compass_HMC5843(AP_HMC5843_BusDriver *bus,
|
2016-11-06 06:04:48 -04:00
|
|
|
bool force_external, enum Rotation rotation);
|
2016-02-26 13:42:26 -04:00
|
|
|
|
2016-11-09 07:33:38 -04:00
|
|
|
bool init();
|
2016-11-07 04:04:40 -04:00
|
|
|
bool _check_whoami();
|
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
|
|
|
|
2017-01-13 15:26:14 -04:00
|
|
|
void _timer();
|
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
/* Read a single sample */
|
|
|
|
bool _read_sample();
|
|
|
|
|
2016-11-07 04:04:40 -04:00
|
|
|
// ask for a new sample
|
|
|
|
void _take_sample();
|
|
|
|
|
2016-03-04 13:59:06 -04:00
|
|
|
AP_HMC5843_BusDriver *_bus;
|
2016-02-26 13:42:26 -04:00
|
|
|
|
2018-09-28 14:41:48 -03:00
|
|
|
Vector3f _scaling;
|
2016-03-07 20:10:08 -04:00
|
|
|
float _gain_scale;
|
2016-02-26 13:42:26 -04:00
|
|
|
|
|
|
|
int16_t _mag_x;
|
|
|
|
int16_t _mag_y;
|
|
|
|
int16_t _mag_z;
|
|
|
|
|
|
|
|
uint8_t _compass_instance;
|
|
|
|
|
2016-11-06 06:04:48 -04:00
|
|
|
enum Rotation _rotation;
|
|
|
|
|
2016-11-04 06:24:53 -03:00
|
|
|
bool _initialised:1;
|
|
|
|
bool _force_external:1;
|
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; }
|
2016-10-31 22:17:13 -03:00
|
|
|
|
|
|
|
virtual AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t, AP_HAL::Device::PeriodicCb) = 0;
|
2016-11-04 06:24:53 -03:00
|
|
|
|
|
|
|
// set device type within a device class
|
|
|
|
virtual void set_device_type(uint8_t devtype) = 0;
|
|
|
|
|
|
|
|
// return 24 bit bus identifier
|
|
|
|
virtual uint32_t get_bus_id(void) const = 0;
|
2016-12-01 00:00:34 -04:00
|
|
|
|
|
|
|
virtual void set_retries(uint8_t retries) {}
|
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-11-04 03:57:52 -03:00
|
|
|
AP_HMC5843_BusDriver_HALDevice(AP_HAL::OwnPtr<AP_HAL::Device> dev);
|
2016-03-04 13:59:06 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-10-31 22:17:13 -03:00
|
|
|
AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
|
2016-11-04 06:24:53 -03:00
|
|
|
|
|
|
|
// set device type within a device class
|
|
|
|
void set_device_type(uint8_t devtype) override {
|
|
|
|
_dev->set_device_type(devtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return 24 bit bus identifier
|
|
|
|
uint32_t get_bus_id(void) const override {
|
|
|
|
return _dev->get_bus_id();
|
|
|
|
}
|
2016-12-01 00:00:34 -04:00
|
|
|
|
|
|
|
void set_retries(uint8_t retries) override {
|
|
|
|
return _dev->set_retries(retries);
|
|
|
|
}
|
2016-10-31 22:17:13 -03:00
|
|
|
|
2015-08-07 19:54:58 -03:00
|
|
|
private:
|
2016-11-04 03:57:52 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::Device> _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;
|
|
|
|
|
2016-10-31 22:17:13 -03:00
|
|
|
AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
|
2016-11-04 06:24:53 -03:00
|
|
|
|
|
|
|
// set device type within a device class
|
|
|
|
void set_device_type(uint8_t devtype) override;
|
|
|
|
|
|
|
|
// return 24 bit bus identifier
|
|
|
|
uint32_t get_bus_id(void) const override;
|
2016-10-31 22:17:13 -03:00
|
|
|
|
2015-08-10 20:30:32 -03:00
|
|
|
private:
|
2016-02-26 13:42:26 -04:00
|
|
|
AuxiliaryBus *_bus;
|
|
|
|
AuxiliaryBusSlave *_slave;
|
|
|
|
bool _started;
|
2015-08-10 20:30:32 -03:00
|
|
|
};
|
2023-01-29 19:33:08 -04:00
|
|
|
|
|
|
|
#endif // AP_COMPASS_HMC5843_ENABLED
|