3 #include "AP_Airspeed_config.h"
5 #if AP_AIRSPEED_ENABLED
7 #include <AP_Param/AP_Param.h>
8 #include <AP_Math/AP_Math.h>
10 #if AP_AIRSPEED_MSP_ENABLED
11 #include <AP_MSP/msp.h>
13 #if AP_AIRSPEED_EXTERNAL_ENABLED
14 #include <AP_ExternalAHRS/AP_ExternalAHRS.h>
17 class AP_Airspeed_Backend
;
19 class AP_Airspeed_Params
{
22 AP_Airspeed_Params(void);
24 // parameters for each instance
26 #ifndef HAL_BUILD_AP_PERIPH
31 #ifndef HAL_BUILD_AP_PERIPH
39 #if AP_AIRSPEED_AUTOCAL_ENABLE
43 static const struct AP_Param::GroupInfo var_info
[];
47 class Airspeed_Calibration
{
49 friend class AP_Airspeed
;
51 Airspeed_Calibration();
53 // initialise the calibration
54 void init(float initial_ratio
);
56 // take current airspeed in m/s and ground speed vector and return
58 float update(float airspeed
, const Vector3f
&vg
, int16_t max_airspeed_allowed_during_cal
);
61 // state of kalman filter for airspeed ratio estimation
62 Matrix3f P
; // covariance matrix
63 const float Q0
; // process noise matrix top left and middle element
64 const float Q1
; // process noise matrix bottom right element
65 Vector3f state
; // state vector
66 const float DT
; // time delta
72 friend class AP_Airspeed_Backend
;
77 void set_fixedwing_parameters(const class AP_FixedWing
*_fixed_wing_parameters
);
83 // indicate which bit in LOG_BITMASK indicates we should log airspeed readings
84 void set_log_bit(uint32_t log_bit
) { _log_bit
= log_bit
; }
86 #if AP_AIRSPEED_AUTOCAL_ENABLE
87 // inflight ratio calibration
88 void set_calibration_enabled(bool enable
) {calibration_enabled
= enable
;}
89 #endif //AP_AIRSPEED_AUTOCAL_ENABLE
91 // read the analog source and update airspeed
94 // calibrate the airspeed. This must be called on startup if the
95 // altitude/climb_rate/acceleration interfaces are ever used
96 void calibrate(bool in_startup
);
98 // return the current airspeed in m/s
99 float get_airspeed(uint8_t i
) const;
100 float get_airspeed(void) const { return get_airspeed(primary
); }
102 // return the unfiltered airspeed in m/s
103 float get_raw_airspeed(uint8_t i
) const;
104 float get_raw_airspeed(void) const { return get_raw_airspeed(primary
); }
106 // return the current airspeed ratio (dimensionless)
107 float get_airspeed_ratio(uint8_t i
) const {
108 #ifndef HAL_BUILD_AP_PERIPH
109 return param
[i
].ratio
;
114 float get_airspeed_ratio(void) const { return get_airspeed_ratio(primary
); }
116 // get temperature if available
117 bool get_temperature(uint8_t i
, float &temperature
);
118 bool get_temperature(float &temperature
) { return get_temperature(primary
, temperature
); }
120 // set the airspeed ratio (dimensionless)
121 #ifndef HAL_BUILD_AP_PERIPH
122 void set_airspeed_ratio(uint8_t i
, float ratio
) {
123 param
[i
].ratio
.set(ratio
);
125 void set_airspeed_ratio(float ratio
) { set_airspeed_ratio(primary
, ratio
); }
128 // return true if airspeed is enabled, and airspeed use is set
129 bool use(uint8_t i
) const;
130 bool use(void) const { return use(primary
); }
132 // force disabling of all airspeed sensors
133 void force_disable_use(bool value
) {
134 _force_disable_use
= value
;
137 // return true if airspeed is enabled
138 bool enabled(uint8_t i
) const;
139 bool enabled(void) const { return enabled(primary
); }
141 // return the differential pressure in Pascal for the last airspeed reading
142 float get_differential_pressure(uint8_t i
) const;
143 float get_differential_pressure(void) const { return get_differential_pressure(primary
); }
145 // update airspeed ratio calibration
146 void update_calibration(const Vector3f
&vground
, int16_t max_airspeed_allowed_during_cal
);
148 // return health status of sensor
149 bool healthy(uint8_t i
) const;
150 bool healthy(void) const { return healthy(primary
); }
152 // return true if all enabled sensors are healthy
153 bool all_healthy(void) const;
155 // return time in ms of last update
156 uint32_t last_update_ms(uint8_t i
) const { return state
[i
].last_update_ms
; }
157 uint32_t last_update_ms(void) const { return last_update_ms(primary
); }
159 #if AP_AIRSPEED_HYGROMETER_ENABLE
160 bool get_hygrometer(uint8_t i
, uint32_t &last_sample_ms
, float &temperature
, float &humidity
) const;
163 static const struct AP_Param::GroupInfo var_info
[];
165 enum pitot_tube_order
{ PITOT_TUBE_ORDER_POSITIVE
= 0,
166 PITOT_TUBE_ORDER_NEGATIVE
= 1,
167 PITOT_TUBE_ORDER_AUTO
= 2 };
170 ON_FAILURE_AHRS_WIND_MAX_DO_DISABLE
= (1<<0), // If set then use airspeed failure check
171 ON_FAILURE_AHRS_WIND_MAX_RECOVERY_DO_REENABLE
= (1<<1), // If set then automatically enable the airspeed sensor use when healthy again.
172 DISABLE_VOLTAGE_CORRECTION
= (1<<2),
173 USE_EKF_CONSISTENCY
= (1<<3),
174 REPORT_OFFSET
= (1<<4), // report offset cal to GCS
182 TYPE_I2C_MS5525_ADDRESS_1
=4,
183 TYPE_I2C_MS5525_ADDRESS_2
=5,
187 TYPE_I2C_DLVR_10IN
=9,
188 TYPE_I2C_DLVR_20IN
=10,
189 TYPE_I2C_DLVR_30IN
=11,
190 TYPE_I2C_DLVR_60IN
=12,
198 // get current primary sensor
199 uint8_t get_primary(void) const { return primary
; }
201 // get number of sensors
202 uint8_t get_num_sensors(void) const { return num_sensors
; }
204 static AP_Airspeed
*get_singleton() { return _singleton
; }
206 // return the current corrected pressure, public for AP_Periph
207 float get_corrected_pressure(uint8_t i
) const;
208 float get_corrected_pressure(void) const {
209 return get_corrected_pressure(primary
);
212 #if AP_AIRSPEED_MSP_ENABLED
213 void handle_msp(const MSP::msp_airspeed_data_message_t
&pkt
);
216 #if AP_AIRSPEED_EXTERNAL_ENABLED
217 void handle_external(const AP_ExternalAHRS::airspeed_data_message_t
&pkt
);
220 enum class CalibrationState
{
226 // get aggregate calibration state for the Airspeed library:
227 CalibrationState
get_calibration_state() const;
230 static AP_Airspeed
*_singleton
;
233 bool lib_enabled() const;
235 AP_Int8 primary_sensor
;
236 AP_Int8 max_speed_pcnt
;
237 AP_Int32 _options
; // bitmask options for airspeed
242 AP_Airspeed_Params param
[AIRSPEED_MAX_SENSORS
];
244 CalibrationState calibration_state
[AIRSPEED_MAX_SENSORS
];
246 struct airspeed_state
{
250 float filtered_pressure
;
251 float corrected_pressure
;
252 uint32_t last_update_ms
;
253 bool use_zero_offset
;
256 // state of runtime calibration
264 #if AP_AIRSPEED_AUTOCAL_ENABLE
265 Airspeed_Calibration calibration
;
266 float last_saved_ratio
;
268 #endif // AP_AIRSPEED_AUTOCAL_ENABLE
271 uint32_t last_check_ms
;
272 float health_probability
;
274 int8_t param_use_backup
;
275 uint32_t last_warn_ms
;
278 #if AP_AIRSPEED_HYGROMETER_ENABLE
279 uint32_t last_hygrometer_log_ms
;
281 } state
[AIRSPEED_MAX_SENSORS
];
283 bool calibration_enabled
;
285 // can be set to true to disable the use of the airspeed sensor
286 bool _force_disable_use
;
288 // current primary sensor
292 uint32_t _log_bit
= -1; // stores which bit in LOG_BITMASK is used to indicate we should log airspeed readings
294 void read(uint8_t i
);
295 // return the differential pressure in Pascal for the last airspeed reading for the requested instance
296 // returns 0 if the sensor is not enabled
297 float get_pressure(uint8_t i
);
299 // get the health probability
300 float get_health_probability(uint8_t i
) const {
301 return state
[i
].failures
.health_probability
;
303 float get_health_probability(void) const {
304 return get_health_probability(primary
);
307 // get the consistency test ratio
308 float get_test_ratio(uint8_t i
) const {
309 return state
[i
].failures
.test_ratio
;
311 float get_test_ratio(void) const {
312 return get_test_ratio(primary
);
315 void update_calibration(uint8_t i
, float raw_pressure
);
316 void update_calibration(uint8_t i
, const Vector3f
&vground
, int16_t max_airspeed_allowed_during_cal
);
317 void send_airspeed_calibration(const Vector3f
&vg
);
318 // return the current calibration offset
319 float get_offset(uint8_t i
) const {
320 #ifndef HAL_BUILD_AP_PERIPH
321 return param
[i
].offset
;
326 float get_offset(void) const { return get_offset(primary
); }
328 void check_sensor_failures();
329 void check_sensor_ahrs_wind_max_failures(uint8_t i
);
331 AP_Airspeed_Backend
*sensor
[AIRSPEED_MAX_SENSORS
];
335 bool add_backend(AP_Airspeed_Backend
*backend
);
337 const AP_FixedWing
*fixed_wing_parameters
;
339 void convert_per_instance();
344 AP_Airspeed
*airspeed();
347 #endif // AP_AIRSPEED_ENABLED