AP_Airspeed.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #pragma once
  2. #include <AP_Common/AP_Common.h>
  3. #include <AP_HAL/AP_HAL.h>
  4. #include <AP_Param/AP_Param.h>
  5. #include <GCS_MAVLink/GCS_MAVLink.h>
  6. #include <AP_Math/AP_Math.h>
  7. class AP_Airspeed_Backend;
  8. #define AIRSPEED_MAX_SENSORS 2
  9. class Airspeed_Calibration {
  10. public:
  11. friend class AP_Airspeed;
  12. // constructor
  13. Airspeed_Calibration();
  14. // initialise the calibration
  15. void init(float initial_ratio);
  16. // take current airspeed in m/s and ground speed vector and return
  17. // new scaling factor
  18. float update(float airspeed, const Vector3f &vg, int16_t max_airspeed_allowed_during_cal);
  19. private:
  20. // state of kalman filter for airspeed ratio estimation
  21. Matrix3f P; // covarience matrix
  22. const float Q0; // process noise matrix top left and middle element
  23. const float Q1; // process noise matrix bottom right element
  24. Vector3f state; // state vector
  25. const float DT; // time delta
  26. };
  27. class AP_Airspeed
  28. {
  29. public:
  30. friend class AP_Airspeed_Backend;
  31. // constructor
  32. AP_Airspeed();
  33. void init(void);
  34. // read the analog source and update airspeed
  35. void update(bool log);
  36. // calibrate the airspeed. This must be called on startup if the
  37. // altitude/climb_rate/acceleration interfaces are ever used
  38. void calibrate(bool in_startup);
  39. // return the current airspeed in m/s
  40. float get_airspeed(uint8_t i) const {
  41. return state[i].airspeed;
  42. }
  43. float get_airspeed(void) const { return get_airspeed(primary); }
  44. // return the unfiltered airspeed in m/s
  45. float get_raw_airspeed(uint8_t i) const {
  46. return state[i].raw_airspeed;
  47. }
  48. float get_raw_airspeed(void) const { return get_raw_airspeed(primary); }
  49. // return the current airspeed ratio (dimensionless)
  50. float get_airspeed_ratio(uint8_t i) const {
  51. return param[i].ratio;
  52. }
  53. float get_airspeed_ratio(void) const { return get_airspeed_ratio(primary); }
  54. // get temperature if available
  55. bool get_temperature(uint8_t i, float &temperature);
  56. bool get_temperature(float &temperature) { return get_temperature(primary, temperature); }
  57. // set the airspeed ratio (dimensionless)
  58. void set_airspeed_ratio(uint8_t i, float ratio) {
  59. param[i].ratio.set(ratio);
  60. }
  61. void set_airspeed_ratio(float ratio) { set_airspeed_ratio(primary, ratio); }
  62. // return true if airspeed is enabled, and airspeed use is set
  63. bool use(uint8_t i) const;
  64. bool use(void) const { return use(primary); }
  65. // return true if airspeed is enabled
  66. bool enabled(uint8_t i) const {
  67. if (i < AIRSPEED_MAX_SENSORS) {
  68. return param[i].type.get() != TYPE_NONE;
  69. }
  70. return false;
  71. }
  72. bool enabled(void) const { return enabled(primary); }
  73. // used by HIL to set the airspeed
  74. void set_HIL(float airspeed) {
  75. state[primary].airspeed = airspeed;
  76. }
  77. // return the differential pressure in Pascal for the last airspeed reading
  78. float get_differential_pressure(uint8_t i) const {
  79. return state[i].last_pressure;
  80. }
  81. float get_differential_pressure(void) const { return get_differential_pressure(primary); }
  82. // update airspeed ratio calibration
  83. void update_calibration(const Vector3f &vground, int16_t max_airspeed_allowed_during_cal);
  84. // return health status of sensor
  85. bool healthy(uint8_t i) const {
  86. return state[i].healthy && (fabsf(param[i].offset) > 0 || state[i].use_zero_offset) && enabled(i);
  87. }
  88. bool healthy(void) const { return healthy(primary); }
  89. // return true if all enabled sensors are healthy
  90. bool all_healthy(void) const;
  91. void setHIL(float pressure) { state[0].healthy=state[0].hil_set=true; state[0].hil_pressure=pressure; }
  92. // return time in ms of last update
  93. uint32_t last_update_ms(uint8_t i) const { return state[i].last_update_ms; }
  94. uint32_t last_update_ms(void) const { return last_update_ms(primary); }
  95. void setHIL(float airspeed, float diff_pressure, float temperature);
  96. static const struct AP_Param::GroupInfo var_info[];
  97. enum pitot_tube_order { PITOT_TUBE_ORDER_POSITIVE = 0,
  98. PITOT_TUBE_ORDER_NEGATIVE = 1,
  99. PITOT_TUBE_ORDER_AUTO = 2 };
  100. enum OptionsMask {
  101. ON_FAILURE_AHRS_WIND_MAX_DO_DISABLE = (1<<0), // If set then use airspeed failure check
  102. ON_FAILURE_AHRS_WIND_MAX_RECOVERY_DO_REENABLE = (1<<1), // If set then automatically enable the airspeed sensor use when healthy again.
  103. };
  104. enum airspeed_type {
  105. TYPE_NONE=0,
  106. TYPE_I2C_MS4525=1,
  107. TYPE_ANALOG=2,
  108. TYPE_I2C_MS5525=3,
  109. TYPE_I2C_MS5525_ADDRESS_1=4,
  110. TYPE_I2C_MS5525_ADDRESS_2=5,
  111. TYPE_I2C_SDP3X=6,
  112. TYPE_I2C_DLVR_5IN=7,
  113. TYPE_UAVCAN=8,
  114. TYPE_I2C_DLVR_10IN=9,
  115. };
  116. // get current primary sensor
  117. uint8_t get_primary(void) const { return primary; }
  118. static AP_Airspeed *get_singleton() { return _singleton; }
  119. private:
  120. static AP_Airspeed *_singleton;
  121. AP_Int8 primary_sensor;
  122. AP_Int32 _options; // bitmask options for airspeed
  123. struct {
  124. AP_Float offset;
  125. AP_Float ratio;
  126. AP_Float psi_range;
  127. AP_Int8 use;
  128. AP_Int8 type;
  129. AP_Int8 pin;
  130. AP_Int8 bus;
  131. AP_Int8 autocal;
  132. AP_Int8 tube_order;
  133. AP_Int8 skip_cal;
  134. } param[AIRSPEED_MAX_SENSORS];
  135. struct airspeed_state {
  136. float raw_airspeed;
  137. float airspeed;
  138. float last_pressure;
  139. float filtered_pressure;
  140. float corrected_pressure;
  141. bool healthy:1;
  142. bool hil_set:1;
  143. float hil_pressure;
  144. uint32_t last_update_ms;
  145. bool use_zero_offset;
  146. // state of runtime calibration
  147. struct {
  148. uint32_t start_ms;
  149. uint16_t count;
  150. float sum;
  151. uint16_t read_count;
  152. } cal;
  153. Airspeed_Calibration calibration;
  154. float last_saved_ratio;
  155. uint8_t counter;
  156. struct {
  157. uint32_t last_check_ms;
  158. float health_probability;
  159. int8_t param_use_backup;
  160. bool has_warned;
  161. } failures;
  162. } state[AIRSPEED_MAX_SENSORS];
  163. // current primary sensor
  164. uint8_t primary;
  165. void read(uint8_t i);
  166. // return the differential pressure in Pascal for the last airspeed reading for the requested instance
  167. // returns 0 if the sensor is not enabled
  168. float get_pressure(uint8_t i);
  169. // return the current corrected pressure
  170. float get_corrected_pressure(uint8_t i) const {
  171. return state[i].corrected_pressure;
  172. }
  173. float get_corrected_pressure(void) const {
  174. return get_corrected_pressure(primary);
  175. }
  176. // get the failure health probability
  177. float get_health_failure_probability(uint8_t i) const {
  178. return state[i].failures.health_probability;
  179. }
  180. float get_health_failure_probability(void) const {
  181. return get_health_failure_probability(primary);
  182. }
  183. void update_calibration(uint8_t i, float raw_pressure);
  184. void update_calibration(uint8_t i, const Vector3f &vground, int16_t max_airspeed_allowed_during_cal);
  185. void send_airspeed_calibration(const Vector3f &vg);
  186. // return the current calibration offset
  187. float get_offset(uint8_t i) const {
  188. return param[i].offset;
  189. }
  190. float get_offset(void) const { return get_offset(primary); }
  191. void check_sensor_failures();
  192. void check_sensor_ahrs_wind_max_failures(uint8_t i);
  193. AP_Airspeed_Backend *sensor[AIRSPEED_MAX_SENSORS];
  194. void Log_Airspeed();
  195. };
  196. namespace AP {
  197. AP_Airspeed *airspeed();
  198. };