Skip to content

Commit cb6d86b

Browse files
f5sohfilnet
authored andcommitted
Merged in f5soh/librepilot/LP-589_INS13_Yaw_spin (pull request #504)
LP-588 INS13 - Scale down Mag variance setting by Be² + Small fixes Approved-by: Lalanne Laurent <f5soh@free.fr> Approved-by: Philippe Renon <philippe_renon@yahoo.fr>
2 parents fadfae3 + 8f50452 commit cb6d86b

3 files changed

Lines changed: 14 additions & 17 deletions

File tree

flight/libraries/inc/insgps.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void INSGPSInit();
5858
void INSStatePrediction(const float gyro_data[3], const float accel_data[3], float dT);
5959
void INSCovariancePrediction(float dT);
6060
void INSCorrection(const float mag_data[3], const float Pos[3], const float Vel[3],
61-
float BaroAlt, uint16_t SensorsUsed);
61+
const float BaroAlt, uint16_t SensorsUsed);
6262
void INSResetP(const float PDiag[13]);
6363
void INSGetVariance(float PDiag[13]);
6464
void INSSetState(const float pos[3], const float vel[3], const float q[4], const float gyro_bias[3], const float accel_bias[3]);

flight/libraries/insgps13state.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ static struct EKFData {
9292
float H[NUMV][NUMX];
9393
// local magnetic unit vector in NED frame
9494
float Be[3];
95-
float BeScaleFactor;
9695
// covariance matrix and state vector
9796
float P[NUMX][NUMX];
9897
float X[NUMX];
@@ -281,26 +280,25 @@ void INSSetGyroBiasVar(const float gyro_bias_var[3])
281280
ekf.Q[8] = gyro_bias_var[2];
282281
}
283282

284-
// must be called AFTER SetMagNorth
285-
void INSSetMagVar(const float mag_var[3])
283+
void INSSetMagVar(const float scaled_mag_var[3])
286284
{
287-
ekf.R[6] = mag_var[0] * ekf.BeScaleFactor;
288-
ekf.R[7] = mag_var[1] * ekf.BeScaleFactor;
289-
ekf.R[8] = mag_var[2] * ekf.BeScaleFactor;
285+
ekf.R[6] = scaled_mag_var[0];
286+
ekf.R[7] = scaled_mag_var[1];
287+
ekf.R[8] = scaled_mag_var[2];
290288
}
291289

292-
void INSSetBaroVar(float baro_var)
290+
void INSSetBaroVar(const float baro_var)
293291
{
294292
ekf.R[9] = baro_var;
295293
}
296294

297295
void INSSetMagNorth(const float B[3])
298296
{
299-
ekf.BeScaleFactor = invsqrtf(B[0] * B[0] + B[1] * B[1] + B[2] * B[2]);
297+
float invmag = invsqrtf(B[0] * B[0] + B[1] * B[1] + B[2] * B[2]);
300298

301-
ekf.Be[0] = B[0] * ekf.BeScaleFactor;
302-
ekf.Be[1] = B[1] * ekf.BeScaleFactor;
303-
ekf.Be[2] = B[2] * ekf.BeScaleFactor;
299+
ekf.Be[0] = B[0] * invmag;
300+
ekf.Be[1] = B[1] * invmag;
301+
ekf.Be[2] = B[2] * invmag;
304302
}
305303

306304
void INSStatePrediction(const float gyro_data[3], const float accel_data[3], float dT)
@@ -402,8 +400,6 @@ void INSCorrection(const float mag_data[3], const float Pos[3], const float Vel[
402400
Z[3] = Vel[0];
403401
Z[4] = Vel[1];
404402
Z[5] = Vel[2];
405-
// magnetometer data in any units (use unit vector) and in body frame
406-
407403

408404
if (SensorsUsed & MAG_SENSORS) {
409405
// magnetometer data in any units (use unit vector) and in body frame

flight/modules/StateEstimation/filterekf.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,10 @@ static filterResult filter(stateFilter *self, stateEstimation *state)
232232
// Reset the INS algorithm
233233
INSGPSInit();
234234
// variance is measured in mGaus, but internally the EKF works with a normalized vector. Scale down by Be^2
235-
INSSetMagVar((float[3]) { this->ekfConfiguration.R.MagX,
236-
this->ekfConfiguration.R.MagY,
237-
this->ekfConfiguration.R.MagZ }
235+
float Be2 = this->homeLocation.Be[0] * this->homeLocation.Be[0] + this->homeLocation.Be[1] * this->homeLocation.Be[1] + this->homeLocation.Be[2] * this->homeLocation.Be[2];
236+
INSSetMagVar((float[3]) { this->ekfConfiguration.R.MagX / Be2,
237+
this->ekfConfiguration.R.MagY / Be2,
238+
this->ekfConfiguration.R.MagZ / Be2 }
238239
);
239240
INSSetAccelVar((float[3]) { this->ekfConfiguration.Q.AccelX,
240241
this->ekfConfiguration.Q.AccelY,

0 commit comments

Comments
 (0)