2014-11-15 21:58:23 -04:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compass driver backend class. Each supported compass sensor type
|
|
|
|
needs to have an object derived from this class.
|
|
|
|
*/
|
2016-02-17 21:25:20 -04:00
|
|
|
#pragma once
|
2014-11-15 21:58:23 -04:00
|
|
|
|
2022-10-26 06:21:36 -03:00
|
|
|
#include "AP_Compass_config.h"
|
|
|
|
|
|
|
|
#if AP_COMPASS_EXTERNALAHRS_ENABLED
|
|
|
|
#include <AP_ExternalAHRS/AP_ExternalAHRS.h>
|
2020-09-03 19:11:05 -03:00
|
|
|
#endif
|
|
|
|
|
2022-10-26 06:21:36 -03:00
|
|
|
#if AP_COMPASS_MSP_ENABLED
|
|
|
|
#include <AP_MSP/msp.h>
|
|
|
|
#endif
|
2022-02-25 01:06:27 -04:00
|
|
|
|
2023-01-04 18:47:39 -04:00
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
|
2014-11-15 21:58:23 -04:00
|
|
|
class Compass; // forward declaration
|
|
|
|
class AP_Compass_Backend
|
|
|
|
{
|
|
|
|
public:
|
2018-08-06 19:21:27 -03:00
|
|
|
AP_Compass_Backend();
|
2014-11-15 21:58:23 -04:00
|
|
|
|
|
|
|
// we declare a virtual destructor so that drivers can
|
|
|
|
// override with a custom destructor if need be.
|
|
|
|
virtual ~AP_Compass_Backend(void) {}
|
|
|
|
|
|
|
|
// read sensor data
|
2015-02-23 19:17:44 -04:00
|
|
|
virtual void read(void) = 0;
|
2014-11-15 21:58:23 -04:00
|
|
|
|
2016-11-04 21:24:21 -03:00
|
|
|
/*
|
|
|
|
device driver IDs. These are used to fill in the devtype field
|
|
|
|
of the device ID, which shows up as COMPASS*ID* parameters to
|
|
|
|
users. The values are chosen for compatibility with existing PX4
|
|
|
|
drivers.
|
|
|
|
If a change is made to a driver that would make existing
|
|
|
|
calibration values invalid then this number must be changed.
|
|
|
|
*/
|
|
|
|
enum DevTypes {
|
2016-11-07 17:54:32 -04:00
|
|
|
DEVTYPE_HMC5883_OLD = 0x01,
|
2016-11-07 04:04:40 -04:00
|
|
|
DEVTYPE_HMC5883 = 0x07,
|
2016-11-04 21:24:21 -03:00
|
|
|
DEVTYPE_LSM303D = 0x02,
|
|
|
|
DEVTYPE_AK8963 = 0x04,
|
|
|
|
DEVTYPE_BMM150 = 0x05,
|
|
|
|
DEVTYPE_LSM9DS1 = 0x06,
|
2016-11-24 02:54:44 -04:00
|
|
|
DEVTYPE_LIS3MDL = 0x08,
|
2016-11-24 03:46:47 -04:00
|
|
|
DEVTYPE_AK09916 = 0x09,
|
2016-12-07 11:12:40 -04:00
|
|
|
DEVTYPE_IST8310 = 0x0A,
|
2017-05-24 05:45:19 -03:00
|
|
|
DEVTYPE_ICM20948 = 0x0B,
|
2016-12-19 02:53:48 -04:00
|
|
|
DEVTYPE_MMC3416 = 0x0C,
|
2018-06-27 00:31:10 -03:00
|
|
|
DEVTYPE_QMC5883L = 0x0D,
|
|
|
|
DEVTYPE_MAG3110 = 0x0E,
|
|
|
|
DEVTYPE_SITL = 0x0F,
|
2018-07-31 21:27:40 -03:00
|
|
|
DEVTYPE_IST8308 = 0x10,
|
2020-05-23 01:38:00 -03:00
|
|
|
DEVTYPE_RM3100 = 0x11,
|
2021-03-23 18:06:38 -03:00
|
|
|
DEVTYPE_RM3100_2 = 0x12, // unused, past mistake
|
2021-11-03 17:16:18 -03:00
|
|
|
DEVTYPE_MMC5983 = 0x13,
|
2021-10-05 17:12:27 -03:00
|
|
|
DEVTYPE_AK09918 = 0x14,
|
2021-10-20 00:03:43 -03:00
|
|
|
DEVTYPE_AK09915 = 0x15,
|
2016-11-04 21:24:21 -03:00
|
|
|
};
|
2017-04-02 11:56:15 -03:00
|
|
|
|
2022-10-26 06:21:36 -03:00
|
|
|
#if AP_COMPASS_MSP_ENABLED
|
2020-09-03 19:11:05 -03:00
|
|
|
virtual void handle_msp(const MSP::msp_compass_data_message_t &pkt) {}
|
|
|
|
#endif
|
2017-04-02 11:56:15 -03:00
|
|
|
|
2022-10-26 06:21:36 -03:00
|
|
|
#if AP_COMPASS_EXTERNALAHRS_ENABLED
|
2020-12-28 01:26:18 -04:00
|
|
|
virtual void handle_external(const AP_ExternalAHRS::mag_data_message_t &pkt) {}
|
|
|
|
#endif
|
|
|
|
|
2014-11-15 21:58:23 -04:00
|
|
|
protected:
|
2015-03-18 19:15:22 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A compass measurement is expected to pass through the following functions:
|
|
|
|
* 1. rotate_field - this rotates the measurement in-place from sensor frame
|
|
|
|
* to body frame
|
|
|
|
* 2. publish_raw_field - this provides an uncorrected point-sample for
|
|
|
|
* calibration libraries
|
|
|
|
* 3. correct_field - this corrects the measurement in-place for hard iron,
|
2023-10-11 04:41:52 -03:00
|
|
|
* soft iron, motor interference, and non-orthogonality errors
|
2016-03-10 21:07:52 -04:00
|
|
|
* 4. publish_filtered_field - legacy filtered magnetic field
|
2015-09-29 16:51:21 -03:00
|
|
|
*
|
|
|
|
* All those functions expect the mag field to be in milligauss.
|
2015-03-18 19:15:22 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
void rotate_field(Vector3f &mag, uint8_t instance);
|
2017-09-05 22:58:35 -03:00
|
|
|
void publish_raw_field(const Vector3f &mag, uint8_t instance);
|
2015-03-18 19:15:22 -03:00
|
|
|
void correct_field(Vector3f &mag, uint8_t i);
|
|
|
|
void publish_filtered_field(const Vector3f &mag, uint8_t instance);
|
2016-05-04 04:11:02 -03:00
|
|
|
void set_last_update_usec(uint32_t last_update, uint8_t instance);
|
2015-02-23 19:17:44 -04:00
|
|
|
|
2018-09-28 13:11:53 -03:00
|
|
|
void accumulate_sample(Vector3f &field, uint8_t instance,
|
|
|
|
uint32_t max_samples = 10);
|
|
|
|
void drain_accumulated_samples(uint8_t instance, const Vector3f *scale = NULL);
|
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
// register a new compass instance with the frontend
|
2019-11-20 03:18:10 -04:00
|
|
|
bool register_compass(int32_t dev_id, uint8_t& instance) const;
|
2015-02-23 19:17:44 -04:00
|
|
|
|
|
|
|
// set dev_id for an instance
|
|
|
|
void set_dev_id(uint8_t instance, uint32_t dev_id);
|
|
|
|
|
2018-06-27 00:43:27 -03:00
|
|
|
// save dev_id, used by SITL
|
|
|
|
void save_dev_id(uint8_t instance);
|
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
// set external state for an instance
|
|
|
|
void set_external(uint8_t instance, bool external);
|
|
|
|
|
2016-01-28 13:07:00 -04:00
|
|
|
// tell if instance is an external compass
|
|
|
|
bool is_external(uint8_t instance);
|
|
|
|
|
2016-11-06 06:04:48 -04:00
|
|
|
// set rotation of an instance
|
|
|
|
void set_rotation(uint8_t instance, enum Rotation rotation);
|
2017-04-02 11:56:15 -03:00
|
|
|
|
2018-07-19 04:24:21 -03:00
|
|
|
// get board orientation (for SITL)
|
|
|
|
enum Rotation get_board_orientation(void) const;
|
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
// access to frontend
|
|
|
|
Compass &_compass;
|
|
|
|
|
2016-11-03 21:47:43 -03:00
|
|
|
// semaphore for access to shared frontend data
|
2020-01-18 17:57:23 -04:00
|
|
|
HAL_Semaphore _sem;
|
2017-04-02 11:56:15 -03:00
|
|
|
|
2018-03-12 04:59:32 -03:00
|
|
|
// Check that the compass field is valid by using a mean filter on the vector length
|
|
|
|
bool field_ok(const Vector3f &field);
|
|
|
|
|
|
|
|
uint32_t get_error_count() const { return _error_count; }
|
2015-02-23 19:17:44 -04:00
|
|
|
private:
|
|
|
|
void apply_corrections(Vector3f &mag, uint8_t i);
|
2018-03-12 04:59:32 -03:00
|
|
|
|
|
|
|
// mean field length for range filter
|
|
|
|
float _mean_field_length;
|
|
|
|
// number of dropped samples. Not used for now, but can be usable to choose more reliable sensor
|
|
|
|
uint32_t _error_count;
|
2014-11-15 21:58:23 -04:00
|
|
|
};
|