PX4 Firmware
PX4 Autopilot Software http://px4.io
airspeed_fusion.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015 Estimation and Control Library (ECL). All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name ECL nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file airspeed_fusion.cpp
36  * airspeed fusion methods.
37  *
38  * @author Carl Olsson <carlolsson.co@gmail.com>
39  * @author Roman Bast <bapstroman@gmail.com>
40  * @author Paul Riseborough <p_riseborough@live.com.au>
41  *
42  */
43 #include "../ecl.h"
44 #include "ekf.h"
45 #include <mathlib/mathlib.h>
46 
48 {
49  // Initialize variables
50  float vn; // Velocity in north direction
51  float ve; // Velocity in east direction
52  float vd; // Velocity in downwards direction
53  float vwn; // Wind speed in north direction
54  float vwe; // Wind speed in east direction
55  float v_tas_pred; // Predicted measurement
57  10.0f)); // Variance for true airspeed measurement - (m/sec)^2
58  float SH_TAS[3] = {}; // Variable used to optimise calculations of measurement jacobian
59  float H_TAS[24] = {}; // Observation Jacobian
60  float SK_TAS[2] = {}; // Variable used to optimise calculations of the Kalman gain vector
61  float Kfusion[24] = {}; // Kalman gain vector
62 
63  // Copy required states to local variable names
64  vn = _state.vel(0);
65  ve = _state.vel(1);
66  vd = _state.vel(2);
67  vwn = _state.wind_vel(0);
68  vwe = _state.wind_vel(1);
69 
70  // Calculate the predicted airspeed
71  v_tas_pred = sqrtf((ve - vwe) * (ve - vwe) + (vn - vwn) * (vn - vwn) + vd * vd);
72 
73  // Perform fusion of True Airspeed measurement
74  if (v_tas_pred > 1.0f) {
75  // determine if we need the sideslip fusion to correct states other than wind
76  bool update_wind_only = !_is_wind_dead_reckoning;
77 
78  // Calculate the observation jacobian
79  // intermediate variable from algebraic optimisation
80  SH_TAS[0] = 1.0f/v_tas_pred;
81  SH_TAS[1] = (SH_TAS[0]*(2.0f*ve - 2.0f*vwe))*0.5f;
82  SH_TAS[2] = (SH_TAS[0]*(2.0f*vn - 2.0f*vwn))*0.5f;
83 
84  for (uint8_t i = 0; i < _k_num_states; i++) { H_TAS[i] = 0.0f; }
85 
86  H_TAS[4] = SH_TAS[2];
87  H_TAS[5] = SH_TAS[1];
88  H_TAS[6] = vd*SH_TAS[0];
89  H_TAS[22] = -SH_TAS[2];
90  H_TAS[23] = -SH_TAS[1];
91 
92  // We don't want to update the innovation variance if the calculation is ill conditioned
93  float _airspeed_innov_var_temp = (R_TAS + SH_TAS[2]*(P[4][4]*SH_TAS[2] + P[5][4]*SH_TAS[1] - P[22][4]*SH_TAS[2] - P[23][4]*SH_TAS[1] + P[6][4]*vd*SH_TAS[0]) + SH_TAS[1]*(P[4][5]*SH_TAS[2] + P[5][5]*SH_TAS[1] - P[22][5]*SH_TAS[2] - P[23][5]*SH_TAS[1] + P[6][5]*vd*SH_TAS[0]) - SH_TAS[2]*(P[4][22]*SH_TAS[2] + P[5][22]*SH_TAS[1] - P[22][22]*SH_TAS[2] - P[23][22]*SH_TAS[1] + P[6][22]*vd*SH_TAS[0]) - SH_TAS[1]*(P[4][23]*SH_TAS[2] + P[5][23]*SH_TAS[1] - P[22][23]*SH_TAS[2] - P[23][23]*SH_TAS[1] + P[6][23]*vd*SH_TAS[0]) + vd*SH_TAS[0]*(P[4][6]*SH_TAS[2] + P[5][6]*SH_TAS[1] - P[22][6]*SH_TAS[2] - P[23][6]*SH_TAS[1] + P[6][6]*vd*SH_TAS[0]));
94 
95  if (_airspeed_innov_var_temp >= R_TAS) { // Check for badly conditioned calculation
96  SK_TAS[0] = 1.0f / _airspeed_innov_var_temp;
98 
99  } else { // Reset the estimator covariance matrix
101 
102  // if we are getting aiding from other sources, warn and reset the wind states and covariances only
103  if (update_wind_only) {
104  resetWindStates();
106  ECL_ERR_TIMESTAMPED("EKF airspeed fusion badly conditioned - wind covariance reset");
107 
108  } else {
110  _state.wind_vel.setZero();
111  ECL_ERR_TIMESTAMPED("EKF airspeed fusion badly conditioned - full covariance reset");
112  }
113 
114  return;
115  }
116 
117  SK_TAS[1] = SH_TAS[1];
118 
119  if (update_wind_only) {
120  // If we are getting aiding from other sources, then don't allow the airspeed measurements to affect the non-windspeed states
121  for (unsigned row = 0; row <= 21; row++) {
122  Kfusion[row] = 0.0f;
123  }
124 
125  } else {
126  // we have no other source of aiding, so use airspeed measurements to correct states
127  Kfusion[0] = SK_TAS[0]*(P[0][4]*SH_TAS[2] - P[0][22]*SH_TAS[2] + P[0][5]*SK_TAS[1] - P[0][23]*SK_TAS[1] + P[0][6]*vd*SH_TAS[0]);
128  Kfusion[1] = SK_TAS[0]*(P[1][4]*SH_TAS[2] - P[1][22]*SH_TAS[2] + P[1][5]*SK_TAS[1] - P[1][23]*SK_TAS[1] + P[1][6]*vd*SH_TAS[0]);
129  Kfusion[2] = SK_TAS[0]*(P[2][4]*SH_TAS[2] - P[2][22]*SH_TAS[2] + P[2][5]*SK_TAS[1] - P[2][23]*SK_TAS[1] + P[2][6]*vd*SH_TAS[0]);
130  Kfusion[3] = SK_TAS[0]*(P[3][4]*SH_TAS[2] - P[3][22]*SH_TAS[2] + P[3][5]*SK_TAS[1] - P[3][23]*SK_TAS[1] + P[3][6]*vd*SH_TAS[0]);
131  Kfusion[4] = SK_TAS[0]*(P[4][4]*SH_TAS[2] - P[4][22]*SH_TAS[2] + P[4][5]*SK_TAS[1] - P[4][23]*SK_TAS[1] + P[4][6]*vd*SH_TAS[0]);
132  Kfusion[5] = SK_TAS[0]*(P[5][4]*SH_TAS[2] - P[5][22]*SH_TAS[2] + P[5][5]*SK_TAS[1] - P[5][23]*SK_TAS[1] + P[5][6]*vd*SH_TAS[0]);
133  Kfusion[6] = SK_TAS[0]*(P[6][4]*SH_TAS[2] - P[6][22]*SH_TAS[2] + P[6][5]*SK_TAS[1] - P[6][23]*SK_TAS[1] + P[6][6]*vd*SH_TAS[0]);
134  Kfusion[7] = SK_TAS[0]*(P[7][4]*SH_TAS[2] - P[7][22]*SH_TAS[2] + P[7][5]*SK_TAS[1] - P[7][23]*SK_TAS[1] + P[7][6]*vd*SH_TAS[0]);
135  Kfusion[8] = SK_TAS[0]*(P[8][4]*SH_TAS[2] - P[8][22]*SH_TAS[2] + P[8][5]*SK_TAS[1] - P[8][23]*SK_TAS[1] + P[8][6]*vd*SH_TAS[0]);
136  Kfusion[9] = SK_TAS[0]*(P[9][4]*SH_TAS[2] - P[9][22]*SH_TAS[2] + P[9][5]*SK_TAS[1] - P[9][23]*SK_TAS[1] + P[9][6]*vd*SH_TAS[0]);
137  Kfusion[10] = SK_TAS[0]*(P[10][4]*SH_TAS[2] - P[10][22]*SH_TAS[2] + P[10][5]*SK_TAS[1] - P[10][23]*SK_TAS[1] + P[10][6]*vd*SH_TAS[0]);
138  Kfusion[11] = SK_TAS[0]*(P[11][4]*SH_TAS[2] - P[11][22]*SH_TAS[2] + P[11][5]*SK_TAS[1] - P[11][23]*SK_TAS[1] + P[11][6]*vd*SH_TAS[0]);
139  Kfusion[12] = SK_TAS[0]*(P[12][4]*SH_TAS[2] - P[12][22]*SH_TAS[2] + P[12][5]*SK_TAS[1] - P[12][23]*SK_TAS[1] + P[12][6]*vd*SH_TAS[0]);
140  Kfusion[13] = SK_TAS[0]*(P[13][4]*SH_TAS[2] - P[13][22]*SH_TAS[2] + P[13][5]*SK_TAS[1] - P[13][23]*SK_TAS[1] + P[13][6]*vd*SH_TAS[0]);
141  Kfusion[14] = SK_TAS[0]*(P[14][4]*SH_TAS[2] - P[14][22]*SH_TAS[2] + P[14][5]*SK_TAS[1] - P[14][23]*SK_TAS[1] + P[14][6]*vd*SH_TAS[0]);
142  Kfusion[15] = SK_TAS[0]*(P[15][4]*SH_TAS[2] - P[15][22]*SH_TAS[2] + P[15][5]*SK_TAS[1] - P[15][23]*SK_TAS[1] + P[15][6]*vd*SH_TAS[0]);
143  Kfusion[16] = SK_TAS[0]*(P[16][4]*SH_TAS[2] - P[16][22]*SH_TAS[2] + P[16][5]*SK_TAS[1] - P[16][23]*SK_TAS[1] + P[16][6]*vd*SH_TAS[0]);
144  Kfusion[17] = SK_TAS[0]*(P[17][4]*SH_TAS[2] - P[17][22]*SH_TAS[2] + P[17][5]*SK_TAS[1] - P[17][23]*SK_TAS[1] + P[17][6]*vd*SH_TAS[0]);
145  Kfusion[18] = SK_TAS[0]*(P[18][4]*SH_TAS[2] - P[18][22]*SH_TAS[2] + P[18][5]*SK_TAS[1] - P[18][23]*SK_TAS[1] + P[18][6]*vd*SH_TAS[0]);
146  Kfusion[19] = SK_TAS[0]*(P[19][4]*SH_TAS[2] - P[19][22]*SH_TAS[2] + P[19][5]*SK_TAS[1] - P[19][23]*SK_TAS[1] + P[19][6]*vd*SH_TAS[0]);
147  Kfusion[20] = SK_TAS[0]*(P[20][4]*SH_TAS[2] - P[20][22]*SH_TAS[2] + P[20][5]*SK_TAS[1] - P[20][23]*SK_TAS[1] + P[20][6]*vd*SH_TAS[0]);
148  Kfusion[21] = SK_TAS[0]*(P[21][4]*SH_TAS[2] - P[21][22]*SH_TAS[2] + P[21][5]*SK_TAS[1] - P[21][23]*SK_TAS[1] + P[21][6]*vd*SH_TAS[0]);
149  }
150  Kfusion[22] = SK_TAS[0]*(P[22][4]*SH_TAS[2] - P[22][22]*SH_TAS[2] + P[22][5]*SK_TAS[1] - P[22][23]*SK_TAS[1] + P[22][6]*vd*SH_TAS[0]);
151  Kfusion[23] = SK_TAS[0]*(P[23][4]*SH_TAS[2] - P[23][22]*SH_TAS[2] + P[23][5]*SK_TAS[1] - P[23][23]*SK_TAS[1] + P[23][6]*vd*SH_TAS[0]);
152 
153 
154  // Calculate measurement innovation
155  _airspeed_innov = v_tas_pred -
157 
158  // Calculate the innovation variance
159  _airspeed_innov_var = 1.0f / SK_TAS[0];
160 
161  // Compute the ratio of innovation to gate size
162  _tas_test_ratio = sq(_airspeed_innov) / (sq(fmaxf(_params.tas_innov_gate, 1.0f)) * _airspeed_innov_var);
163 
164  // If the innovation consistency check fails then don't fuse the sample and indicate bad airspeed health
165  if (_tas_test_ratio > 1.0f) {
167  return;
168 
169  } else {
171  }
172 
173  // Airspeed measurement sample has passed check so record it
175 
176  // apply covariance correction via P_new = (I -K*H)*P
177  // first calculate expression for KHP
178  // then calculate P - KHP
179  float KHP[_k_num_states][_k_num_states];
180  float KH[5];
181 
182  for (unsigned row = 0; row < _k_num_states; row++) {
183 
184  KH[0] = Kfusion[row] * H_TAS[4];
185  KH[1] = Kfusion[row] * H_TAS[5];
186  KH[2] = Kfusion[row] * H_TAS[6];
187  KH[3] = Kfusion[row] * H_TAS[22];
188  KH[4] = Kfusion[row] * H_TAS[23];
189 
190  for (unsigned column = 0; column < _k_num_states; column++) {
191  float tmp = KH[0] * P[4][column];
192  tmp += KH[1] * P[5][column];
193  tmp += KH[2] * P[6][column];
194  tmp += KH[3] * P[22][column];
195  tmp += KH[4] * P[23][column];
196  KHP[row][column] = tmp;
197  }
198  }
199 
200  // if the covariance correction will result in a negative variance, then
201  // the covariance matrix is unhealthy and must be corrected
202  bool healthy = true;
204 
205  for (int i = 0; i < _k_num_states; i++) {
206  if (P[i][i] < KHP[i][i]) {
207  // zero rows and columns
208  zeroRows(P, i, i);
209  zeroCols(P, i, i);
210 
211  //flag as unhealthy
212  healthy = false;
213 
214  // update individual measurement health status
216 
217  }
218  }
219 
220  // only apply covariance and state corrections if healthy
221  if (healthy) {
222  // apply the covariance corrections
223  for (unsigned row = 0; row < _k_num_states; row++) {
224  for (unsigned column = 0; column < _k_num_states; column++) {
225  P[row][column] = P[row][column] - KHP[row][column];
226  }
227  }
228 
229  // correct the covariance matrix for gross errors
231 
232  // apply the state corrections
233  fuse(Kfusion, _airspeed_innov);
234 
235  }
236  }
237 }
238 
239 void Ekf::get_wind_velocity(float *wind)
240 {
241  wind[0] = _state.wind_vel(0);
242  wind[1] = _state.wind_vel(1);
243 }
244 
245 void Ekf::get_wind_velocity_var(float *wind_var)
246 {
247  wind_var[0] = P[22][22];
248  wind_var[1] = P[23][23];
249 }
250 
251 void Ekf::get_true_airspeed(float *tas)
252 {
253  float tempvar = sqrtf(sq(_state.vel(0) - _state.wind_vel(0)) + sq(_state.vel(1) - _state.wind_vel(1)) + sq(_state.vel(2)));
254  memcpy(tas, &tempvar, sizeof(float));
255 }
256 
257 /*
258  * Reset the wind states using the current airspeed measurement, ground relative nav velocity, yaw angle and assumption of zero sideslip
259 */
261 {
262  // get euler yaw angle
263  Eulerf euler321(_state.quat_nominal);
264  float euler_yaw = euler321(2);
265 
267  // estimate wind using zero sideslip assumption and airspeed measurement if airspeed available
268  _state.wind_vel(0) = _state.vel(0) - _airspeed_sample_delayed.true_airspeed * cosf(euler_yaw);
269  _state.wind_vel(1) = _state.vel(1) - _airspeed_sample_delayed.true_airspeed * sinf(euler_yaw);
270 
271  } else {
272  // If we don't have an airspeed measurement, then assume the wind is zero
273  _state.wind_vel(0) = 0.0f;
274  _state.wind_vel(1) = 0.0f;
275  }
276 }
float tas_innov_gate
True Airspeed innovation consistency gate size (STD)
Definition: common.h:280
constexpr _Tp constrain(_Tp val, _Tp min_val, _Tp max_val)
Definition: Limits.hpp:66
stateSample _state
state struct of the ekf running at the delayed time horizon
Definition: ekf.h:288
#define ECL_ERR_TIMESTAMPED
Definition: ecl.h:95
void get_true_airspeed(float *tas) override
void initialiseCovariance()
Definition: covariance.cpp:49
void fuseAirspeed()
airspeedSample _airspeed_sample_delayed
void resetWindStates()
struct estimator::fault_status_u::@57 flags
float P[_k_num_states][_k_num_states]
state covariance matrix
Definition: ekf.h:367
void zeroCols(float(&cov_mat)[_k_num_states][_k_num_states], uint8_t first, uint8_t last)
void get_wind_velocity(float *wind) override
float eas_noise
EAS measurement noise standard deviation used for airspeed fusion (m/s)
Definition: common.h:281
void fuse(float *K, float innovation)
uint64_t time_us
timestamp of the measurement (uSec)
Definition: common.h:144
float eas2tas
equivalent to true airspeed factor
Definition: common.h:143
bool bad_airspeed
5 - true if fusion of the airspeed has encountered a numerical error
Definition: common.h:385
uint64_t time_us
timestamp of the measurement (uSec)
Definition: common.h:111
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
Euler< float > Eulerf
Definition: Euler.hpp:156
Vector3f vel
NED velocity in earth frame in m/s.
Definition: common.h:369
static constexpr float sq(float var)
Definition: ekf.h:695
bool _tas_data_ready
true when new true airspeed data has fallen behind the fusion time horizon and is available to be fus...
Definition: ekf.h:323
fault_status_u _fault_status
float _airspeed_innov_var
airspeed measurement innovation variance ((m/sec)**2)
Definition: ekf.h:380
void fixCovarianceErrors()
Definition: covariance.cpp:728
Quatf quat_nominal
quaternion defining the rotation from body to earth frame
Definition: common.h:368
void resetWindCovariance()
Definition: covariance.cpp:929
innovation_fault_status_u _innov_check_fail_status
float true_airspeed
true airspeed measurement (m/sec)
Definition: common.h:142
Kfusion[0]
Definition: K_VELX.c:93
uint64_t _time_last_arsp_fuse
time the last fusion of airspeed measurements were performed (uSec)
Definition: ekf.h:335
void zeroRows(float(&cov_mat)[_k_num_states][_k_num_states], uint8_t first, uint8_t last)
float _airspeed_innov
airspeed measurement innovation (m/sec)
Definition: ekf.h:379
void get_wind_velocity_var(float *wind_var) override
struct estimator::innovation_fault_status_u::@58 flags
Vector2f wind_vel
wind velocity in m/s
Definition: common.h:375
bool reject_airspeed
7 - true if the airspeed observation has been rejected
Definition: common.h:411
static constexpr uint8_t _k_num_states
number of EKF states
Definition: ekf.h:270
Class for core functions for ekf attitude and position estimator.