Path: blob/master/libraries/AP_Airspeed/AP_Airspeed.h
9773 views
#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;3334enum class SkipCalType : int8_t {35// Do not skip boot calibration, this is the default36None = 0,3738// Skip boot calibration, use saved offset, no calibration is required (but can be performed manually)39NoCalRequired = 1,4041// Skip boot calibration, require manual calibration once per boot42SkipBootCal = 2,43};44AP_Enum<SkipCalType> skip_cal;4546AP_Int8 tube_order;47#endif48AP_Int8 type;49AP_Int8 bus;50#if AP_AIRSPEED_AUTOCAL_ENABLE51AP_Int8 autocal;52#endif5354static const struct AP_Param::GroupInfo var_info[];55};565758class Airspeed_Calibration {59public:60friend class AP_Airspeed;61// constructor62Airspeed_Calibration();6364// initialise the calibration65void init(float initial_ratio);6667// take current airspeed in m/s and ground speed vector and return68// new scaling factor69float update(float airspeed, const Vector3f &vg, int16_t max_airspeed_allowed_during_cal);7071private:72// state of kalman filter for airspeed ratio estimation73Matrix3f P; // covariance matrix74const float Q0; // process noise matrix top left and middle element75const float Q1; // process noise matrix bottom right element76Vector3f state; // state vector77};7879class AP_Airspeed80{81public:82friend class AP_Airspeed_Backend;8384// constructor85AP_Airspeed();8687void set_fixedwing_parameters(const class AP_FixedWing *_fixed_wing_parameters);8889void init(void);90void allocate();919293// indicate which bit in LOG_BITMASK indicates we should log airspeed readings94void set_log_bit(uint32_t log_bit) { _log_bit = log_bit; }9596#if AP_AIRSPEED_AUTOCAL_ENABLE97// inflight ratio calibration98void set_calibration_enabled(bool enable) {calibration_enabled = enable;}99#endif //AP_AIRSPEED_AUTOCAL_ENABLE100101// read the analog source and update airspeed102void update(void);103104// calibrate the airspeed. This must be called on startup if the105// altitude/climb_rate/acceleration interfaces are ever used106void calibrate(bool in_startup);107108// return the current airspeed in m/s109float get_airspeed(uint8_t i) const;110float get_airspeed(void) const { return get_airspeed(primary); }111112// return the unfiltered airspeed in m/s113float get_raw_airspeed(uint8_t i) const;114float get_raw_airspeed(void) const { return get_raw_airspeed(primary); }115116// return the current airspeed ratio (dimensionless)117float get_airspeed_ratio(uint8_t i) const {118#ifndef HAL_BUILD_AP_PERIPH119return param[i].ratio;120#else121return 0.0;122#endif123}124float get_airspeed_ratio(void) const { return get_airspeed_ratio(primary); }125126// get temperature if available127bool get_temperature(uint8_t i, float &temperature) const;128bool get_temperature(float &temperature) const { return get_temperature(primary, temperature); }129130// set the airspeed ratio (dimensionless)131#ifndef HAL_BUILD_AP_PERIPH132void set_airspeed_ratio(uint8_t i, float ratio) {133param[i].ratio.set(ratio);134}135void set_airspeed_ratio(float ratio) { set_airspeed_ratio(primary, ratio); }136#endif137138// return true if airspeed is enabled, and airspeed use is set139bool use(uint8_t i) const;140bool use(void) const { return use(primary); }141142// force disabling of all airspeed sensors143void force_disable_use(bool value) {144_force_disable_use = value;145}146147// return true if airspeed is enabled148bool enabled(uint8_t i) const;149bool enabled(void) const { return enabled(primary); }150151// return the differential pressure in Pascal for the last airspeed reading152float get_differential_pressure(uint8_t i) const;153float get_differential_pressure(void) const { return get_differential_pressure(primary); }154155// update airspeed ratio calibration156void update_calibration(const Vector3f &vground, int16_t max_airspeed_allowed_during_cal);157158// return health status of sensor159bool healthy(uint8_t i) const;160bool healthy(void) const { return healthy(primary); }161162// return true if all enabled sensors are healthy163bool all_healthy(void) const;164165// return time in ms of last update166uint32_t last_update_ms(uint8_t i) const { return state[i].last_update_ms; }167uint32_t last_update_ms(void) const { return last_update_ms(primary); }168169#if AP_AIRSPEED_HYGROMETER_ENABLE170bool get_hygrometer(uint8_t i, uint32_t &last_sample_ms, float &temperature, float &humidity) const;171#endif172173static const struct AP_Param::GroupInfo var_info[];174175enum pitot_tube_order { PITOT_TUBE_ORDER_POSITIVE = 0,176PITOT_TUBE_ORDER_NEGATIVE = 1,177PITOT_TUBE_ORDER_AUTO = 2 };178179enum OptionsMask {180ON_FAILURE_AHRS_WIND_MAX_DO_DISABLE = (1<<0), // If set then use airspeed failure check181ON_FAILURE_AHRS_WIND_MAX_RECOVERY_DO_REENABLE = (1<<1), // If set then automatically enable the airspeed sensor use when healthy again.182DISABLE_VOLTAGE_CORRECTION = (1<<2),183USE_EKF_CONSISTENCY = (1<<3),184REPORT_OFFSET = (1<<4), // report offset cal to GCS185};186187enum airspeed_type {188TYPE_NONE=0,189#if AP_AIRSPEED_MS4525_ENABLED190TYPE_I2C_MS4525=1,191#endif // AP_AIRSPEED_MSP_ENABLED192#if AP_AIRSPEED_ANALOG_ENABLED193TYPE_ANALOG=2,194#endif // AP_AIRSPEED_ANALOG_ENABLED195#if AP_AIRSPEED_MS5525_ENABLED196TYPE_I2C_MS5525=3,197TYPE_I2C_MS5525_ADDRESS_1=4,198TYPE_I2C_MS5525_ADDRESS_2=5,199#endif // AP_AIRSPEED_MS5525_ENABLED200#if AP_AIRSPEED_SDP3X_ENABLED201TYPE_I2C_SDP3X=6,202#endif // AP_AIRSPEED_SDP3X_ENABLED203#if AP_AIRSPEED_DLVR_ENABLED204TYPE_I2C_DLVR_5IN=7,205#endif // AP_AIRSPEED_DLVR_ENABLED206#if AP_AIRSPEED_DRONECAN_ENABLED207TYPE_UAVCAN=8,208#endif // AP_AIRSPEED_DRONECAN_ENABLED209#if AP_AIRSPEED_DLVR_ENABLED210TYPE_I2C_DLVR_10IN=9,211TYPE_I2C_DLVR_20IN=10,212TYPE_I2C_DLVR_30IN=11,213TYPE_I2C_DLVR_60IN=12,214#endif // AP_AIRSPEED_DLVR_ENABLED215#if AP_AIRSPEED_NMEA_ENABLED216TYPE_NMEA_WATER=13,217#endif // AP_AIRSPEED_NMEA_ENABLED218#if AP_AIRSPEED_MSP_ENABLED219TYPE_MSP=14,220#endif // AP_AIRSPEED_MSP_ENABLED221#if AP_AIRSPEED_ASP5033_ENABLED222TYPE_I2C_ASP5033=15,223#endif // AP_AIRSPEED_ASP5033_ENABLED224#if AP_AIRSPEED_EXTERNAL_ENABLED225TYPE_EXTERNAL=16,226#endif // AP_AIRSPEED_EXTERNAL_ENABLED227#if AP_AIRSPEED_AUAV_ENABLED228TYPE_AUAV_10IN=17,229TYPE_AUAV_5IN=18,230TYPE_AUAV_30IN=19,231#endif // AP_AIRSPEED_AUAV_ENABLED232#if AP_AIRSPEED_SITL_ENABLED233TYPE_SITL=100,234#endif // AP_AIRSPEED_SITL_ENABLED235};236237// get current primary sensor238uint8_t get_primary(void) const { return primary; }239240// get number of sensors241uint8_t get_num_sensors(void) const { return num_sensors; }242243static AP_Airspeed *get_singleton() { return _singleton; }244245// return the current corrected pressure, public for AP_Periph246float get_corrected_pressure(uint8_t i) const;247float get_corrected_pressure(void) const {248return get_corrected_pressure(primary);249}250251#if AP_AIRSPEED_MSP_ENABLED252void handle_msp(const MSP::msp_airspeed_data_message_t &pkt);253#endif254255#if AP_AIRSPEED_EXTERNAL_ENABLED256void handle_external(const AP_ExternalAHRS::airspeed_data_message_t &pkt);257#endif258259enum class CalibrationState {260NOT_STARTED,261NOT_REQUIRED_ZERO_OFFSET,262IN_PROGRESS,263SUCCESS,264FAILED265};266267// get aggregate calibration state for the Airspeed library:268CalibrationState get_calibration_state() const;269270// returns false if we fail arming checks, in which case the buffer will be populated with a failure message271bool arming_checks(size_t buflen, char *buffer) const;272273private:274static AP_Airspeed *_singleton;275276AP_Int8 _enable;277bool lib_enabled() const;278279AP_Int8 primary_sensor;280AP_Int8 max_speed_pcnt;281AP_Int32 _options; // bitmask options for airspeed282AP_Float _wind_max;283AP_Float _wind_warn;284AP_Float _wind_gate;285286AP_Airspeed_Params param[AIRSPEED_MAX_SENSORS];287288struct airspeed_state {289float raw_airspeed;290float airspeed;291float last_pressure;292float filtered_pressure;293float corrected_pressure;294uint32_t last_update_ms;295bool healthy;296297// Pre-flight offset calibration298struct {299uint32_t start_ms;300float sum;301uint16_t count;302uint16_t read_count;303CalibrationState state;304} cal;305306#if AP_AIRSPEED_AUTOCAL_ENABLE307// In flight ratio calibration308Airspeed_Calibration calibration;309float last_saved_ratio;310uint8_t counter;311#endif // AP_AIRSPEED_AUTOCAL_ENABLE312313struct {314uint32_t last_check_ms;315float health_probability;316float test_ratio;317int8_t param_use_backup;318uint32_t last_warn_ms;319} failures;320321#if AP_AIRSPEED_HYGROMETER_ENABLE322uint32_t last_hygrometer_log_ms;323#endif324} state[AIRSPEED_MAX_SENSORS];325326bool calibration_enabled;327328// can be set to true to disable the use of the airspeed sensor329bool _force_disable_use;330331// current primary sensor332uint8_t primary;333uint8_t num_sensors;334335// Track primary parameter, this allows changes to be honored in flight336uint8_t last_user_primary;337338uint32_t _log_bit = -1; // stores which bit in LOG_BITMASK is used to indicate we should log airspeed readings339340void read(uint8_t i);341342// get the health probability343float get_health_probability(uint8_t i) const {344return state[i].failures.health_probability;345}346float get_health_probability(void) const {347return get_health_probability(primary);348}349350// get the consistency test ratio351float get_test_ratio(uint8_t i) const {352return state[i].failures.test_ratio;353}354float get_test_ratio(void) const {355return get_test_ratio(primary);356}357358void update_calibration(uint8_t i, float raw_pressure);359void update_calibration(uint8_t i, const Vector3f &vground, int16_t max_airspeed_allowed_during_cal);360void send_airspeed_calibration(const Vector3f &vg);361// return the current calibration offset362float get_offset(uint8_t i) const;363float get_offset(void) const { return get_offset(primary); }364365void check_sensor_failures();366void check_sensor_ahrs_wind_max_failures(uint8_t i);367368AP_Airspeed_Backend *sensor[AIRSPEED_MAX_SENSORS];369370void Log_Airspeed();371372bool add_backend(AP_Airspeed_Backend *backend);373374const AP_FixedWing *fixed_wing_parameters;375376void convert_per_instance();377378// Select primary sensor based on user parameters and health379uint8_t select_primary();380381};382383namespace AP {384AP_Airspeed *airspeed();385};386387#endif // AP_AIRSPEED_ENABLED388389390