## Using accelerometer data to correct for gyro drift

Gyroscopes do not measure acceleration, so they obviously cannot be used to calculate the direction of gravity. In other words, we can use gyro outputs to calculate an estimation of the direction cosine matrix, but the estimate will drift over time. A 3-axis accelerometer can be used to correct the roll and pitch drift, which is important in keeping the craft at least upright.

In order to achieve this, we wish to keep the global K vector codirectional with the negative gravity vector. That is, the cross product of the two vectors should equal zero, and we can use whatever the cross product actually is as our correctional rotation vector. It is important to keep in mind, however, that since the accelerometer reports acceleration in the body frame of reference, **we must express K in the same frame of reference as the acceleration vector**.

This body frame K vector is simply found by taking the third row of the transpose of the DCM we found earlier. We cross multiply K with the gravity vector, add the resulting correction vector to the angular displacement vector, and take a weighted average. Where wdt is the angular displacement vector (w * dt) and wA is the correction vector, we can recalculate wdt:

`for (int i=0; i<3; i++) {`

wdt[i] = (wdt[i] + ACC_WEIGHT*wA[i]) / (1.0 + ACC_WEIGHT);

}

An added benefit to using an accelerometer to correct for gyro drift is that we can initialize the IMU in any orientation and the DCM will tilt to agree with whatever the accelerometer thinks is up and down. On the flipside, this could be dangerous if the accelerometer is mounted to the chassis improperly (i.e., not perfectly in line with the robot’s vertical axis), but that can be remedied by careful setting of static offsets in code.

[...] provided your gyro isn’t drifting too badly, the DCM should be fairly accurate even without input from an accelerometer and magnetometer. Share this: Blog this! Bookmark on Delicious Recommend on Facebook Share on [...]

[...] a previous post, I explained how to correct for gyro drift by using the accelerometer’s reading of the [...]