APM:Libraries
AP_Compass_HMC5843.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <AP_Common/AP_Common.h>
4 #include <AP_HAL/AP_HAL.h>
5 #include <AP_HAL/Device.h>
6 #include <AP_Math/AP_Math.h>
7 
8 #include "AP_Compass.h"
9 #include "AP_Compass_Backend.h"
10 
11 class AuxiliaryBus;
12 class AuxiliaryBusSlave;
13 class AP_InertialSensor;
15 
17 {
18 public:
21  bool force_external = false,
22  enum Rotation rotation = ROTATION_NONE);
23 
24  static AP_Compass_Backend *probe_mpu6000(Compass &compass,
25  enum Rotation rotation = ROTATION_NONE);
26 
27  static constexpr const char *name = "HMC5843";
28 
29  virtual ~AP_Compass_HMC5843();
30 
31  void read() override;
32 
33 private:
35  bool force_external, enum Rotation rotation);
36 
37  bool init();
38  bool _check_whoami();
39  bool _calibrate();
40  bool _setup_sampling_mode();
41 
42  void _timer();
43 
44  /* Read a single sample */
45  bool _read_sample();
46 
47  // ask for a new sample
48  void _take_sample();
49 
51 
52  float _scaling[3];
53  float _gain_scale;
54 
55  int16_t _mag_x;
56  int16_t _mag_y;
57  int16_t _mag_z;
58  int16_t _mag_x_accum;
59  int16_t _mag_y_accum;
60  int16_t _mag_z_accum;
61  uint8_t _accum_count;
62 
64 
66 
67  bool _initialised:1;
69 };
70 
72 {
73 public:
74  virtual ~AP_HMC5843_BusDriver() { }
75 
76  virtual bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) = 0;
77  virtual bool register_read(uint8_t reg, uint8_t *val) = 0;
78  virtual bool register_write(uint8_t reg, uint8_t val) = 0;
79 
80  virtual AP_HAL::Semaphore *get_semaphore() = 0;
81 
82  virtual bool configure() { return true; }
83  virtual bool start_measurements() { return true; }
84 
85  virtual AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t, AP_HAL::Device::PeriodicCb) = 0;
86 
87  // set device type within a device class
88  virtual void set_device_type(uint8_t devtype) = 0;
89 
90  // return 24 bit bus identifier
91  virtual uint32_t get_bus_id(void) const = 0;
92 
93  virtual void set_retries(uint8_t retries) {}
94 };
95 
97 {
98 public:
100 
101  bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;
102  bool register_read(uint8_t reg, uint8_t *val) override;
103  bool register_write(uint8_t reg, uint8_t val) override;
104 
105  AP_HAL::Semaphore *get_semaphore() override;
106 
107  AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
108 
109  // set device type within a device class
110  void set_device_type(uint8_t devtype) override {
111  _dev->set_device_type(devtype);
112  }
113 
114  // return 24 bit bus identifier
115  uint32_t get_bus_id(void) const override {
116  return _dev->get_bus_id();
117  }
118 
119  void set_retries(uint8_t retries) override {
120  return _dev->set_retries(retries);
121  }
122 
123 private:
125 };
126 
128 {
129 public:
131  uint8_t addr);
133 
134  bool block_read(uint8_t reg, uint8_t *buf, uint32_t size) override;
135  bool register_read(uint8_t reg, uint8_t *val) override;
136  bool register_write(uint8_t reg, uint8_t val) override;
137 
138  AP_HAL::Semaphore *get_semaphore() override;
139 
140  bool configure() override;
141  bool start_measurements() override;
142 
143  AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
144 
145  // set device type within a device class
146  void set_device_type(uint8_t devtype) override;
147 
148  // return 24 bit bus identifier
149  uint32_t get_bus_id(void) const override;
150 
151 private:
154  bool _started;
155 };
void read() override
AP_HMC5843_BusDriver * _bus
AP_Compass_HMC5843(Compass &compass, AP_HMC5843_BusDriver *bus, bool force_external, enum Rotation rotation)
static constexpr const char * name
enum Rotation _rotation
AP_HAL::OwnPtr< AP_HAL::Device > _dev
static AP_Compass_Backend * probe(Compass &compass, AP_HAL::OwnPtr< AP_HAL::Device > dev, bool force_external=false, enum Rotation rotation=ROTATION_NONE)
Rotation
Definition: rotations.h:27
static AP_InertialSensor ins
Definition: AHRS_Test.cpp:18
#define constexpr
Definition: AP_HAL_Macros.h:16
static AP_HAL::OwnPtr< AP_HAL::Device > dev
Definition: ICM20789.cpp:16
void * PeriodicHandle
Definition: Device.h:42
static Compass compass
Definition: AHRS_Test.cpp:20
bool _setup_sampling_mode()
virtual ~AP_Compass_HMC5843()
Common definitions and utility routines for the ArduPilot libraries.
void set_device_type(uint8_t devtype) override
virtual bool configure()
virtual void set_retries(uint8_t retries)
void set_retries(uint8_t retries) override
virtual bool start_measurements()
static AP_Compass_Backend * probe_mpu6000(Compass &compass, enum Rotation rotation=ROTATION_NONE)
uint32_t get_bus_id(void) const override