CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Compass/AP_Compass_Backend.h
Views: 1798
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
};
80
81
#if AP_COMPASS_MSP_ENABLED
82
virtual void handle_msp(const MSP::msp_compass_data_message_t &pkt) {}
83
#endif
84
85
#if AP_COMPASS_EXTERNALAHRS_ENABLED
86
virtual void handle_external(const AP_ExternalAHRS::mag_data_message_t &pkt) {}
87
#endif
88
89
protected:
90
91
/*
92
* A compass measurement is expected to pass through the following functions:
93
* 1. rotate_field - this rotates the measurement in-place from sensor frame
94
* to body frame
95
* 2. publish_raw_field - this provides an uncorrected point-sample for
96
* calibration libraries
97
* 3. correct_field - this corrects the measurement in-place for hard iron,
98
* soft iron, motor interference, and non-orthogonality errors
99
* 4. publish_filtered_field - legacy filtered magnetic field
100
*
101
* All those functions expect the mag field to be in milligauss.
102
*/
103
104
void rotate_field(Vector3f &mag, uint8_t instance);
105
void publish_raw_field(const Vector3f &mag, uint8_t instance);
106
void correct_field(Vector3f &mag, uint8_t i);
107
void publish_filtered_field(const Vector3f &mag, uint8_t instance);
108
void set_last_update_usec(uint32_t last_update, uint8_t instance);
109
110
void accumulate_sample(Vector3f &field, uint8_t instance,
111
uint32_t max_samples = 10);
112
void drain_accumulated_samples(uint8_t instance, const Vector3f *scale = NULL);
113
114
// register a new compass instance with the frontend
115
bool register_compass(int32_t dev_id, uint8_t& instance) const;
116
117
// set dev_id for an instance
118
void set_dev_id(uint8_t instance, uint32_t dev_id);
119
120
// save dev_id, used by SITL
121
void save_dev_id(uint8_t instance);
122
123
// set external state for an instance
124
void set_external(uint8_t instance, bool external);
125
126
// tell if instance is an external compass
127
bool is_external(uint8_t instance);
128
129
// set rotation of an instance
130
void set_rotation(uint8_t instance, enum Rotation rotation);
131
132
// get board orientation (for SITL)
133
enum Rotation get_board_orientation(void) const;
134
135
// access to frontend
136
Compass &_compass;
137
138
// semaphore for access to shared frontend data
139
HAL_Semaphore _sem;
140
141
// Check that the compass field is valid by using a mean filter on the vector length
142
bool field_ok(const Vector3f &field);
143
144
uint32_t get_error_count() const { return _error_count; }
145
private:
146
void apply_corrections(Vector3f &mag, uint8_t i);
147
148
// mean field length for range filter
149
float _mean_field_length;
150
// number of dropped samples. Not used for now, but can be usable to choose more reliable sensor
151
uint32_t _error_count;
152
};
153
154