Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Compass/AP_Compass_Backend.h
9557 views
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
/*
17
Compass driver backend class. Each supported compass sensor type
18
needs to have an object derived from this class.
19
*/
20
#pragma once
21
22
#include "AP_Compass_config.h"
23
24
#if AP_COMPASS_EXTERNALAHRS_ENABLED
25
#include <AP_ExternalAHRS/AP_ExternalAHRS.h>
26
#endif
27
28
#if AP_COMPASS_MSP_ENABLED
29
#include <AP_MSP/msp.h>
30
#endif
31
32
#include <AP_Math/AP_Math.h>
33
34
class Compass; // forward declaration
35
class AP_Compass_Backend
36
{
37
public:
38
AP_Compass_Backend();
39
40
// we declare a virtual destructor so that drivers can
41
// override with a custom destructor if need be.
42
virtual ~AP_Compass_Backend(void) {}
43
44
// read sensor data
45
virtual void read(void) = 0;
46
47
/*
48
device driver IDs. These are used to fill in the devtype field
49
of the device ID, which shows up as COMPASS*ID* parameters to
50
users. The values are chosen for compatibility with existing PX4
51
drivers.
52
If a change is made to a driver that would make existing
53
calibration values invalid then this number must be changed.
54
*/
55
enum DevTypes {
56
DEVTYPE_HMC5883_OLD = 0x01,
57
DEVTYPE_HMC5883 = 0x07,
58
DEVTYPE_LSM303D = 0x02,
59
DEVTYPE_AK8963 = 0x04,
60
DEVTYPE_BMM150 = 0x05,
61
DEVTYPE_LSM9DS1 = 0x06,
62
DEVTYPE_LIS3MDL = 0x08,
63
DEVTYPE_AK09916 = 0x09,
64
DEVTYPE_IST8310 = 0x0A,
65
DEVTYPE_ICM20948 = 0x0B,
66
DEVTYPE_MMC3416 = 0x0C,
67
DEVTYPE_QMC5883L = 0x0D,
68
DEVTYPE_MAG3110 = 0x0E,
69
DEVTYPE_SITL = 0x0F,
70
DEVTYPE_IST8308 = 0x10,
71
DEVTYPE_RM3100 = 0x11,
72
DEVTYPE_RM3100_2 = 0x12, // unused, past mistake
73
DEVTYPE_MMC5983 = 0x13,
74
DEVTYPE_AK09918 = 0x14,
75
DEVTYPE_AK09915 = 0x15,
76
DEVTYPE_QMC5883P = 0x16,
77
DEVTYPE_BMM350 = 0x17,
78
DEVTYPE_IIS2MDC = 0x18,
79
DEVTYPE_LIS2MDL = 0x19,
80
};
81
82
#if AP_COMPASS_MSP_ENABLED
83
virtual void handle_msp(const MSP::msp_compass_data_message_t &pkt) {}
84
#endif
85
86
#if AP_COMPASS_EXTERNALAHRS_ENABLED
87
virtual void handle_external(const AP_ExternalAHRS::mag_data_message_t &pkt) {}
88
#endif
89
90
protected:
91
92
// this backend's instance number
93
uint8_t instance;
94
95
/*
96
* A compass measurement is expected to pass through the following functions:
97
* 1. rotate_field - this rotates the measurement in-place from sensor frame
98
* to body frame
99
* 2. publish_raw_field - this provides an uncorrected point-sample for
100
* calibration libraries
101
* 3. correct_field - this corrects the measurement in-place for hard iron,
102
* soft iron, motor interference, and non-orthogonality errors
103
* 4. publish_filtered_field - legacy filtered magnetic field
104
*
105
* All those functions expect the mag field to be in milligauss.
106
*/
107
108
void rotate_field(Vector3f &mag);
109
void publish_raw_field(const Vector3f &mag);
110
void correct_field(Vector3f &mag);
111
void publish_filtered_field(const Vector3f &mag);
112
void set_last_update_usec(uint32_t last_update);
113
114
void accumulate_sample(Vector3f &field,
115
uint32_t max_samples = 10);
116
void drain_accumulated_samples(const Vector3f *scale = NULL);
117
118
// register compass instance with the frontend
119
bool register_compass(int32_t dev_id) WARN_IF_UNUSED;
120
121
// set dev_id for an instance
122
void set_dev_id(uint32_t dev_id);
123
124
// save dev_id, used by SITL
125
void save_dev_id();
126
127
// set external state for an instance
128
void set_external(bool external);
129
130
// tell if instance is an external compass
131
bool is_external();
132
133
// set rotation of an instance
134
void set_rotation(enum Rotation rotation);
135
136
// get board orientation (for SITL)
137
enum Rotation get_board_orientation(void) const;
138
139
// access to frontend
140
Compass &_compass;
141
142
// semaphore for access to shared frontend data
143
HAL_Semaphore _sem;
144
145
// Check that the compass field is valid by using a mean filter on the vector length
146
bool field_ok(const Vector3f &field);
147
148
uint32_t get_error_count() const { return _error_count; }
149
private:
150
void apply_corrections(Vector3f &mag, uint8_t i);
151
152
// mean field length for range filter
153
float _mean_field_length;
154
// number of dropped samples. Not used for now, but can be usable to choose more reliable sensor
155
uint32_t _error_count;
156
};
157
158