Path: blob/master/libraries/AP_Compass/AP_Compass_Backend.h
9557 views
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415/*16Compass driver backend class. Each supported compass sensor type17needs to have an object derived from this class.18*/19#pragma once2021#include "AP_Compass_config.h"2223#if AP_COMPASS_EXTERNALAHRS_ENABLED24#include <AP_ExternalAHRS/AP_ExternalAHRS.h>25#endif2627#if AP_COMPASS_MSP_ENABLED28#include <AP_MSP/msp.h>29#endif3031#include <AP_Math/AP_Math.h>3233class Compass; // forward declaration34class AP_Compass_Backend35{36public:37AP_Compass_Backend();3839// we declare a virtual destructor so that drivers can40// override with a custom destructor if need be.41virtual ~AP_Compass_Backend(void) {}4243// read sensor data44virtual void read(void) = 0;4546/*47device driver IDs. These are used to fill in the devtype field48of the device ID, which shows up as COMPASS*ID* parameters to49users. The values are chosen for compatibility with existing PX450drivers.51If a change is made to a driver that would make existing52calibration values invalid then this number must be changed.53*/54enum DevTypes {55DEVTYPE_HMC5883_OLD = 0x01,56DEVTYPE_HMC5883 = 0x07,57DEVTYPE_LSM303D = 0x02,58DEVTYPE_AK8963 = 0x04,59DEVTYPE_BMM150 = 0x05,60DEVTYPE_LSM9DS1 = 0x06,61DEVTYPE_LIS3MDL = 0x08,62DEVTYPE_AK09916 = 0x09,63DEVTYPE_IST8310 = 0x0A,64DEVTYPE_ICM20948 = 0x0B,65DEVTYPE_MMC3416 = 0x0C,66DEVTYPE_QMC5883L = 0x0D,67DEVTYPE_MAG3110 = 0x0E,68DEVTYPE_SITL = 0x0F,69DEVTYPE_IST8308 = 0x10,70DEVTYPE_RM3100 = 0x11,71DEVTYPE_RM3100_2 = 0x12, // unused, past mistake72DEVTYPE_MMC5983 = 0x13,73DEVTYPE_AK09918 = 0x14,74DEVTYPE_AK09915 = 0x15,75DEVTYPE_QMC5883P = 0x16,76DEVTYPE_BMM350 = 0x17,77DEVTYPE_IIS2MDC = 0x18,78DEVTYPE_LIS2MDL = 0x19,79};8081#if AP_COMPASS_MSP_ENABLED82virtual void handle_msp(const MSP::msp_compass_data_message_t &pkt) {}83#endif8485#if AP_COMPASS_EXTERNALAHRS_ENABLED86virtual void handle_external(const AP_ExternalAHRS::mag_data_message_t &pkt) {}87#endif8889protected:9091// this backend's instance number92uint8_t instance;9394/*95* A compass measurement is expected to pass through the following functions:96* 1. rotate_field - this rotates the measurement in-place from sensor frame97* to body frame98* 2. publish_raw_field - this provides an uncorrected point-sample for99* calibration libraries100* 3. correct_field - this corrects the measurement in-place for hard iron,101* soft iron, motor interference, and non-orthogonality errors102* 4. publish_filtered_field - legacy filtered magnetic field103*104* All those functions expect the mag field to be in milligauss.105*/106107void rotate_field(Vector3f &mag);108void publish_raw_field(const Vector3f &mag);109void correct_field(Vector3f &mag);110void publish_filtered_field(const Vector3f &mag);111void set_last_update_usec(uint32_t last_update);112113void accumulate_sample(Vector3f &field,114uint32_t max_samples = 10);115void drain_accumulated_samples(const Vector3f *scale = NULL);116117// register compass instance with the frontend118bool register_compass(int32_t dev_id) WARN_IF_UNUSED;119120// set dev_id for an instance121void set_dev_id(uint32_t dev_id);122123// save dev_id, used by SITL124void save_dev_id();125126// set external state for an instance127void set_external(bool external);128129// tell if instance is an external compass130bool is_external();131132// set rotation of an instance133void set_rotation(enum Rotation rotation);134135// get board orientation (for SITL)136enum Rotation get_board_orientation(void) const;137138// access to frontend139Compass &_compass;140141// semaphore for access to shared frontend data142HAL_Semaphore _sem;143144// Check that the compass field is valid by using a mean filter on the vector length145bool field_ok(const Vector3f &field);146147uint32_t get_error_count() const { return _error_count; }148private:149void apply_corrections(Vector3f &mag, uint8_t i);150151// mean field length for range filter152float _mean_field_length;153// number of dropped samples. Not used for now, but can be usable to choose more reliable sensor154uint32_t _error_count;155};156157158