Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Compass/AP_Compass_Backend.h
Views: 1798
/*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,78};7980#if AP_COMPASS_MSP_ENABLED81virtual void handle_msp(const MSP::msp_compass_data_message_t &pkt) {}82#endif8384#if AP_COMPASS_EXTERNALAHRS_ENABLED85virtual void handle_external(const AP_ExternalAHRS::mag_data_message_t &pkt) {}86#endif8788protected:8990/*91* A compass measurement is expected to pass through the following functions:92* 1. rotate_field - this rotates the measurement in-place from sensor frame93* to body frame94* 2. publish_raw_field - this provides an uncorrected point-sample for95* calibration libraries96* 3. correct_field - this corrects the measurement in-place for hard iron,97* soft iron, motor interference, and non-orthogonality errors98* 4. publish_filtered_field - legacy filtered magnetic field99*100* All those functions expect the mag field to be in milligauss.101*/102103void rotate_field(Vector3f &mag, uint8_t instance);104void publish_raw_field(const Vector3f &mag, uint8_t instance);105void correct_field(Vector3f &mag, uint8_t i);106void publish_filtered_field(const Vector3f &mag, uint8_t instance);107void set_last_update_usec(uint32_t last_update, uint8_t instance);108109void accumulate_sample(Vector3f &field, uint8_t instance,110uint32_t max_samples = 10);111void drain_accumulated_samples(uint8_t instance, const Vector3f *scale = NULL);112113// register a new compass instance with the frontend114bool register_compass(int32_t dev_id, uint8_t& instance) const;115116// set dev_id for an instance117void set_dev_id(uint8_t instance, uint32_t dev_id);118119// save dev_id, used by SITL120void save_dev_id(uint8_t instance);121122// set external state for an instance123void set_external(uint8_t instance, bool external);124125// tell if instance is an external compass126bool is_external(uint8_t instance);127128// set rotation of an instance129void set_rotation(uint8_t instance, enum Rotation rotation);130131// get board orientation (for SITL)132enum Rotation get_board_orientation(void) const;133134// access to frontend135Compass &_compass;136137// semaphore for access to shared frontend data138HAL_Semaphore _sem;139140// Check that the compass field is valid by using a mean filter on the vector length141bool field_ok(const Vector3f &field);142143uint32_t get_error_count() const { return _error_count; }144private:145void apply_corrections(Vector3f &mag, uint8_t i);146147// mean field length for range filter148float _mean_field_length;149// number of dropped samples. Not used for now, but can be usable to choose more reliable sensor150uint32_t _error_count;151};152153154