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_Airspeed/AP_Airspeed.h
Views: 1798
#pragma once12#include "AP_Airspeed_config.h"34#if AP_AIRSPEED_ENABLED56#include <AP_Param/AP_Param.h>7#include <AP_Math/AP_Math.h>89#if AP_AIRSPEED_MSP_ENABLED10#include <AP_MSP/msp.h>11#endif12#if AP_AIRSPEED_EXTERNAL_ENABLED13#include <AP_ExternalAHRS/AP_ExternalAHRS.h>14#endif1516class AP_Airspeed_Backend;1718class AP_Airspeed_Params {19public:20// Constructor21AP_Airspeed_Params(void);2223// parameters for each instance24AP_Int32 bus_id;25#ifndef HAL_BUILD_AP_PERIPH26AP_Float offset;27AP_Float ratio;28#endif29AP_Float psi_range;30#ifndef HAL_BUILD_AP_PERIPH31AP_Int8 use;32AP_Int8 pin;33AP_Int8 skip_cal;34AP_Int8 tube_order;35#endif36AP_Int8 type;37AP_Int8 bus;38#if AP_AIRSPEED_AUTOCAL_ENABLE39AP_Int8 autocal;40#endif4142static const struct AP_Param::GroupInfo var_info[];43};444546class Airspeed_Calibration {47public:48friend class AP_Airspeed;49// constructor50Airspeed_Calibration();5152// initialise the calibration53void init(float initial_ratio);5455// take current airspeed in m/s and ground speed vector and return56// new scaling factor57float update(float airspeed, const Vector3f &vg, int16_t max_airspeed_allowed_during_cal);5859private:60// state of kalman filter for airspeed ratio estimation61Matrix3f P; // covariance matrix62const float Q0; // process noise matrix top left and middle element63const float Q1; // process noise matrix bottom right element64Vector3f state; // state vector65const float DT; // time delta66};6768class AP_Airspeed69{70public:71friend class AP_Airspeed_Backend;7273// constructor74AP_Airspeed();7576void set_fixedwing_parameters(const class AP_FixedWing *_fixed_wing_parameters);7778void init(void);79void allocate();808182// indicate which bit in LOG_BITMASK indicates we should log airspeed readings83void set_log_bit(uint32_t log_bit) { _log_bit = log_bit; }8485#if AP_AIRSPEED_AUTOCAL_ENABLE86// inflight ratio calibration87void set_calibration_enabled(bool enable) {calibration_enabled = enable;}88#endif //AP_AIRSPEED_AUTOCAL_ENABLE8990// read the analog source and update airspeed91void update(void);9293// calibrate the airspeed. This must be called on startup if the94// altitude/climb_rate/acceleration interfaces are ever used95void calibrate(bool in_startup);9697// return the current airspeed in m/s98float get_airspeed(uint8_t i) const;99float get_airspeed(void) const { return get_airspeed(primary); }100101// return the unfiltered airspeed in m/s102float get_raw_airspeed(uint8_t i) const;103float get_raw_airspeed(void) const { return get_raw_airspeed(primary); }104105// return the current airspeed ratio (dimensionless)106float get_airspeed_ratio(uint8_t i) const {107#ifndef HAL_BUILD_AP_PERIPH108return param[i].ratio;109#else110return 0.0;111#endif112}113float get_airspeed_ratio(void) const { return get_airspeed_ratio(primary); }114115// get temperature if available116bool get_temperature(uint8_t i, float &temperature);117bool get_temperature(float &temperature) { return get_temperature(primary, temperature); }118119// set the airspeed ratio (dimensionless)120#ifndef HAL_BUILD_AP_PERIPH121void set_airspeed_ratio(uint8_t i, float ratio) {122param[i].ratio.set(ratio);123}124void set_airspeed_ratio(float ratio) { set_airspeed_ratio(primary, ratio); }125#endif126127// return true if airspeed is enabled, and airspeed use is set128bool use(uint8_t i) const;129bool use(void) const { return use(primary); }130131// force disabling of all airspeed sensors132void force_disable_use(bool value) {133_force_disable_use = value;134}135136// return true if airspeed is enabled137bool enabled(uint8_t i) const;138bool enabled(void) const { return enabled(primary); }139140// return the differential pressure in Pascal for the last airspeed reading141float get_differential_pressure(uint8_t i) const;142float get_differential_pressure(void) const { return get_differential_pressure(primary); }143144// update airspeed ratio calibration145void update_calibration(const Vector3f &vground, int16_t max_airspeed_allowed_during_cal);146147// return health status of sensor148bool healthy(uint8_t i) const;149bool healthy(void) const { return healthy(primary); }150151// return true if all enabled sensors are healthy152bool all_healthy(void) const;153154// return time in ms of last update155uint32_t last_update_ms(uint8_t i) const { return state[i].last_update_ms; }156uint32_t last_update_ms(void) const { return last_update_ms(primary); }157158#if AP_AIRSPEED_HYGROMETER_ENABLE159bool get_hygrometer(uint8_t i, uint32_t &last_sample_ms, float &temperature, float &humidity) const;160#endif161162static const struct AP_Param::GroupInfo var_info[];163164enum pitot_tube_order { PITOT_TUBE_ORDER_POSITIVE = 0,165PITOT_TUBE_ORDER_NEGATIVE = 1,166PITOT_TUBE_ORDER_AUTO = 2 };167168enum OptionsMask {169ON_FAILURE_AHRS_WIND_MAX_DO_DISABLE = (1<<0), // If set then use airspeed failure check170ON_FAILURE_AHRS_WIND_MAX_RECOVERY_DO_REENABLE = (1<<1), // If set then automatically enable the airspeed sensor use when healthy again.171DISABLE_VOLTAGE_CORRECTION = (1<<2),172USE_EKF_CONSISTENCY = (1<<3),173REPORT_OFFSET = (1<<4), // report offset cal to GCS174};175176enum airspeed_type {177TYPE_NONE=0,178TYPE_I2C_MS4525=1,179TYPE_ANALOG=2,180TYPE_I2C_MS5525=3,181TYPE_I2C_MS5525_ADDRESS_1=4,182TYPE_I2C_MS5525_ADDRESS_2=5,183TYPE_I2C_SDP3X=6,184TYPE_I2C_DLVR_5IN=7,185TYPE_UAVCAN=8,186TYPE_I2C_DLVR_10IN=9,187TYPE_I2C_DLVR_20IN=10,188TYPE_I2C_DLVR_30IN=11,189TYPE_I2C_DLVR_60IN=12,190TYPE_NMEA_WATER=13,191TYPE_MSP=14,192TYPE_I2C_ASP5033=15,193TYPE_EXTERNAL=16,194TYPE_SITL=100,195};196197// get current primary sensor198uint8_t get_primary(void) const { return primary; }199200// get number of sensors201uint8_t get_num_sensors(void) const { return num_sensors; }202203static AP_Airspeed *get_singleton() { return _singleton; }204205// return the current corrected pressure, public for AP_Periph206float get_corrected_pressure(uint8_t i) const;207float get_corrected_pressure(void) const {208return get_corrected_pressure(primary);209}210211#if AP_AIRSPEED_MSP_ENABLED212void handle_msp(const MSP::msp_airspeed_data_message_t &pkt);213#endif214215#if AP_AIRSPEED_EXTERNAL_ENABLED216void handle_external(const AP_ExternalAHRS::airspeed_data_message_t &pkt);217#endif218219enum class CalibrationState {220NOT_STARTED,221IN_PROGRESS,222SUCCESS,223FAILED224};225// get aggregate calibration state for the Airspeed library:226CalibrationState get_calibration_state() const;227228private:229static AP_Airspeed *_singleton;230231AP_Int8 _enable;232bool lib_enabled() const;233234AP_Int8 primary_sensor;235AP_Int8 max_speed_pcnt;236AP_Int32 _options; // bitmask options for airspeed237AP_Float _wind_max;238AP_Float _wind_warn;239AP_Float _wind_gate;240241AP_Airspeed_Params param[AIRSPEED_MAX_SENSORS];242243CalibrationState calibration_state[AIRSPEED_MAX_SENSORS];244245struct airspeed_state {246float raw_airspeed;247float airspeed;248float last_pressure;249float filtered_pressure;250float corrected_pressure;251uint32_t last_update_ms;252bool use_zero_offset;253bool healthy;254255// state of runtime calibration256struct {257uint32_t start_ms;258float sum;259uint16_t count;260uint16_t read_count;261} cal;262263#if AP_AIRSPEED_AUTOCAL_ENABLE264Airspeed_Calibration calibration;265float last_saved_ratio;266uint8_t counter;267#endif // AP_AIRSPEED_AUTOCAL_ENABLE268269struct {270uint32_t last_check_ms;271float health_probability;272float test_ratio;273int8_t param_use_backup;274uint32_t last_warn_ms;275} failures;276277#if AP_AIRSPEED_HYGROMETER_ENABLE278uint32_t last_hygrometer_log_ms;279#endif280} state[AIRSPEED_MAX_SENSORS];281282bool calibration_enabled;283284// can be set to true to disable the use of the airspeed sensor285bool _force_disable_use;286287// current primary sensor288uint8_t primary;289uint8_t num_sensors;290291uint32_t _log_bit = -1; // stores which bit in LOG_BITMASK is used to indicate we should log airspeed readings292293void read(uint8_t i);294// return the differential pressure in Pascal for the last airspeed reading for the requested instance295// returns 0 if the sensor is not enabled296float get_pressure(uint8_t i);297298// get the health probability299float get_health_probability(uint8_t i) const {300return state[i].failures.health_probability;301}302float get_health_probability(void) const {303return get_health_probability(primary);304}305306// get the consistency test ratio307float get_test_ratio(uint8_t i) const {308return state[i].failures.test_ratio;309}310float get_test_ratio(void) const {311return get_test_ratio(primary);312}313314void update_calibration(uint8_t i, float raw_pressure);315void update_calibration(uint8_t i, const Vector3f &vground, int16_t max_airspeed_allowed_during_cal);316void send_airspeed_calibration(const Vector3f &vg);317// return the current calibration offset318float get_offset(uint8_t i) const {319#ifndef HAL_BUILD_AP_PERIPH320return param[i].offset;321#else322return 0.0;323#endif324}325float get_offset(void) const { return get_offset(primary); }326327void check_sensor_failures();328void check_sensor_ahrs_wind_max_failures(uint8_t i);329330AP_Airspeed_Backend *sensor[AIRSPEED_MAX_SENSORS];331332void Log_Airspeed();333334bool add_backend(AP_Airspeed_Backend *backend);335336const AP_FixedWing *fixed_wing_parameters;337338void convert_per_instance();339340};341342namespace AP {343AP_Airspeed *airspeed();344};345346#endif // AP_AIRSPEED_ENABLED347348349