# Angle estimation using Kalman filter

Keywords: Sensor fushion, angle estimation, gyrometer, accelerometer, kalman filter

In order to estimate angle using gyrometer- and accelerometer data I decided to implement a Kalman filter. I won’t dig much into the theory as plenty other sources on the interwebs are more qualified for that.

Shown is the completed result of the Kalman filter in action during a 20 second period, where it can be seen that the resulting curve (blue) follows the gyro (green) short term but is being corrected by the accelerometer (red) long term. I decided to implement the system matrices as a struct so it can be easily modified for other sensors and purposes, though only in two dimensions meaning only two different sensor inputs can be used at the time.

typedef struct{ // Inputs float u; // Control input (for angle estimation use gyro data) float z; // Measurement input (for angle estimation use acc data) float dt; // Timestamp // Output matrix float x; // Output // Initialize variance for each sensor float Q; // Process (control) variance (for u) float R; // Measurement variance (for z) // Initialize models float H; // Observation model float F; // State transition model float B; // Control input model float P; // Covariance matrix // Don't initialize float y; float S; float K; } FilterKalman_t;

In order to initialize it, the state transition model F as well as the control model B has to be found. They can be found by looking at the units of our inputs and desired outputs, where our desired output vector is angle and gyro bias: The first element in the input vector z is acceleration which directly can be translated into an angle, hence is already the correct unit. Our control input u is the gyrometer (angular velocity) which can be translated into an angle by calculating the temporal integral. The full expression of our first output parameter would be:

angle = initial angle + (anglular velocity * dt)

You might recognize the formula from high school science class.

Since an angle can be directly extracted from acceleration, and our angular velocity (gyro) is drifting, we change the expression to:

angle = acceleration + ((gyro - gyro_bias) * dt)

The state of the system hence gives us the expression: The full initialization will be:

FilterKalman_t angleFilterKalman = { .F = 1, .F = -dt, .F = 0, .F = 1, .B = dt, .B = 0, .Q = 0.001, // Variance for gyro .Q = 0.003, // Variance for gyro bias .R = 0.03, // Variance for accelerometer .R = 0.03, // Variance for accelerometer .H = 1, .H = 0, .H = 0, .H = 0,   .P = 0, .P = 0, .P = 0, .P = 0 };

The filter function itself is implemented together with comments for it to be quite straight-forward.

void Filter_Kalman(FilterKalman_t *k) { // x = F*x + B*u float x_temp; x_temp = k->F * k->x + k->F * k->x + k->B*k->u; // Position x_temp = k->F * k->x + k->F * k->x + k->B*k->u; // Velocity k->x = x_temp; k->x = x_temp; //rate = u - x;   // P = F * P * Ft + Q float P_temp; P_temp = k->F * (k->F*k->P+k->F*k->P) + k->F * (k->F*k->P+k->F*k->P) + k->Q*k->dt; P_temp = k->F * (k->F*k->P+k->F*k->P) + k->F * (k->F*k->P+k->F*k->P); P_temp = k->F * (k->F*k->P+k->F*k->P) + k->F * (k->F*k->P+k->F*k->P); P_temp = k->F * (k->F*k->P+k->F*k->P) + k->F * (k->F*k->P+k->F*k->P) + k->Q*k->dt;   k->P = P_temp; k->P = P_temp; k->P = P_temp; k->P = P_temp;   // S = H*P * Ht + R k->S = k->H * k->H * k->P + k->R; k->S = k->H * k->H * k->P; k->S = k->H * k->H * k->P; k->S = k->H * k->H * k->P + k->R;   // K = P * Ht * S^-1 float S_inv = k->S*k->S - k->S*k->S;   k->K = ((k->H*k->P + k->H*k->P)*k->S - (k->H*k->P + k->H*k->P)*k->S) / S_inv; k->K = ((k->H*k->P + k->H*k->P)*k->S - (k->H*k->P + k->H*k->P)*k->S) / S_inv; k->K = ((k->H*k->P + k->H*k->P)*k->S - (k->H*k->P + k->H*k->P)*k->S) / S_inv; k->K = ((k->H*k->P + k->H*k->P)*k->S - (k->H*k->P + k->H*k->P)*k->S) / S_inv;   // y = z - (H*x) k->y = k->z - (k->H*k->x + k->H*k->x); k->y = k->z - (k->H*k->x + k->H*k->x);   // x = x + (K*y) k->x = k->x + k->K*k->y + k->K*k->y; k->x = k->x + k->K*k->y + k->K*k->y;   // P = (I - K*H)*P P_temp = k->P*(1 - k->H*k->K - k->H*k->K); P_temp = k->P*(0 - k->H*k->K - k->H*k->K); P_temp = k->P*(0 - k->H*k->K - k->H*k->K); P_temp = k->P*(1 - k->H*k->K - k->H*k->K);   k->P = P_temp; k->P = P_temp; k->P = P_temp; k->P = P_temp; }

The full source code can be found here, but has probably been improved since this post was written.