Skip to content

Commit

Permalink
Merge branch 'estromb-EKF_fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
ataffanel committed Nov 29, 2016
2 parents 39831c3 + df4abcf commit 72e4ed0
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/modules/src/estimator_kalman.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,45 +619,59 @@ static void stateEstimatorPredict(float cmdThrust, Axis3f *acc, Axis3f *gyro, fl
}
quadIsFlying = (xTaskGetTickCount()-lastFlightCmd) < IN_FLIGHT_TIME_THRESHOLD;

float dx, dy, dz;
float tmpSPX, tmpSPY, tmpSPZ;
float zacc;

if (quadIsFlying) // only acceleration in z direction
{
// TODO: In the next lines, can either use cmdThrust/mass, or acc->z. Need to test which is more reliable.
// cmdThrust's error comes from poorly calibrated mass, and inexact cmdThrust -> thrust map
// acc->z's error comes from measurement noise and accelerometer scaling
// float zacc = cmdThrust;
float zacc = acc->z;
zacc = acc->z;

// position updates in the body frame (will be rotated to inertial frame)
float dx = S[STATE_PX] * dt;
float dy = S[STATE_PY] * dt;
float dz = S[STATE_PZ] * dt + zacc * dt2 / 2.0f; // thrust can only be produced in the body's Z direction
dx = S[STATE_PX] * dt;
dy = S[STATE_PY] * dt;
dz = S[STATE_PZ] * dt + zacc * dt2 / 2.0f; // thrust can only be produced in the body's Z direction

// position update
S[STATE_X] += R[0][0] * dx + R[0][1] * dy + R[0][2] * dz;
S[STATE_Y] += R[1][0] * dx + R[1][1] * dy + R[1][2] * dz;
S[STATE_Z] += R[2][0] * dx + R[2][1] * dy + R[2][2] * dz - GRAVITY_MAGNITUDE * dt2 / 2.0f;

// keep previous time step's state for the update
tmpSPX = S[STATE_PX];
tmpSPY = S[STATE_PY];
tmpSPZ = S[STATE_PZ];

// body-velocity update: accelerometers + gyros cross velocity - gravity in body frame
S[STATE_PX] += dt * (gyro->z * S[STATE_PY] - gyro->y * S[STATE_PZ] - GRAVITY_MAGNITUDE * R[2][0]);
S[STATE_PY] += dt * (gyro->z * S[STATE_PX] + gyro->x * S[STATE_PZ] - GRAVITY_MAGNITUDE * R[2][1]);
S[STATE_PZ] += dt * (zacc + gyro->y * S[STATE_PX] - gyro->x * S[STATE_PY] - GRAVITY_MAGNITUDE * R[2][2]);
S[STATE_PX] += dt * (gyro->z * tmpSPY - gyro->y * tmpSPZ - GRAVITY_MAGNITUDE * R[2][0]);
S[STATE_PY] += dt * (gyro->z * tmpSPX + gyro->x * tmpSPZ - GRAVITY_MAGNITUDE * R[2][1]);
S[STATE_PZ] += dt * (zacc + gyro->y * tmpSPX - gyro->x * tmpSPY - GRAVITY_MAGNITUDE * R[2][2]);
}
else // Acceleration can be in any direction, as measured by the accelerometer. This occurs, eg. in freefall or while being carried.
{
// position updates in the body frame (will be rotated to inertial frame)
float dx = S[STATE_PX] * dt + acc->x * dt2 / 2.0f;
float dy = S[STATE_PY] * dt + acc->y * dt2 / 2.0f;
float dz = S[STATE_PZ] * dt + acc->z * dt2 / 2.0f; // thrust can only be produced in the body's Z direction
dx = S[STATE_PX] * dt + acc->x * dt2 / 2.0f;
dy = S[STATE_PY] * dt + acc->y * dt2 / 2.0f;
dz = S[STATE_PZ] * dt + acc->z * dt2 / 2.0f; // thrust can only be produced in the body's Z direction

// position update
S[STATE_X] += R[0][0] * dx + R[0][1] * dy + R[0][2] * dz;
S[STATE_Y] += R[1][0] * dx + R[1][1] * dy + R[1][2] * dz;
S[STATE_Z] += R[2][0] * dx + R[2][1] * dy + R[2][2] * dz - GRAVITY_MAGNITUDE * dt2 / 2.0f;

// keep previous time step's state for the update
tmpSPX = S[STATE_PX];
tmpSPY = S[STATE_PY];
tmpSPZ = S[STATE_PZ];

// body-velocity update: accelerometers + gyros cross velocity - gravity in body frame
S[STATE_PX] += dt * (acc->x + gyro->z * S[STATE_PY] - gyro->y * S[STATE_PZ] - GRAVITY_MAGNITUDE * R[2][0]);
S[STATE_PY] += dt * (acc->y + gyro->z * S[STATE_PX] + gyro->x * S[STATE_PZ] - GRAVITY_MAGNITUDE * R[2][1]);
S[STATE_PZ] += dt * (acc->z + gyro->y * S[STATE_PX] - gyro->x * S[STATE_PY] - GRAVITY_MAGNITUDE * R[2][2]);
S[STATE_PX] += dt * (acc->x + gyro->z * tmpSPY - gyro->y * tmpSPZ - GRAVITY_MAGNITUDE * R[2][0]);
S[STATE_PY] += dt * (acc->y + gyro->z * tmpSPX + gyro->x * tmpSPZ - GRAVITY_MAGNITUDE * R[2][1]);
S[STATE_PZ] += dt * (acc->z + gyro->y * tmpSPX - gyro->x * tmpSPY - GRAVITY_MAGNITUDE * R[2][2]);
}

if(S[STATE_Z] < 0) {
Expand Down

0 comments on commit 72e4ed0

Please sign in to comment.