# Does anyone have code that successfully calculates velocity from an accelerometer

I'm trying to calculate the speed of a rocket for my project. I've been given the task of calculating this with an adxl3xxx accelerometer sensor. I'm not sure how to go about this as an accelerometer takes gravity acceleration into account. Can someone provide some form of code to help me understand. I've heard some theory like caliculatong yaw, pitch and roll then using trig to factor out gravity. Not sure how that works. I've also heard about quartenions not sure what they are. If someone could help me out that would be great thx

Calculating velocity from acceleration is not easy. Simply put, velocity is derived by calculating the integral of acceleration with respect to time. In practice, however, this leads to accumulated errors which generally render the process impractical for any but the shortest amount of time.

For further reading, this Wikipedia Kinematics page does a good job at relating acceleration, velocity and position using math.

… calculating yaw, pitch and roll then using trig to factor out gravity …

Essentially, this is the first of two steps in coding up a tilt compass (the type you find in your cell phone). You need the yaw, pitch and roll information before you can make sense of the magnetometer data. Consider reading the first half of a tilt compass description to understand how to calculate yaw, pitch and roll. Like this one.

I've also heard about quartenions not sure what they are.

Quaternion are an alternative to using trigonometry to derive (in this case) yaw, pitch and roll. There are two advantages (in this case). First, Quaternions are easier for a computer to calculate. Second, Quaternion yaw, pitch and roll calculations do not go to infinity for certain situations. In the real world using physical gyroscopes this is commonly known as Gimbal lock.

How would i go about using trig?

Trigonometry is not necessary if it is physically arranged such that all acceleration to gain the velocity of interest occurs only in 1 of the X, Y or Z directions of the accelerometer. Then all that is need is to integrate the accelerometer readings in that direction with respect to time.

If this happens to be in the same direction as earth's gravity, simply subtract 9.8m/s2 out.

If you can't physically arrange it, then you need to read all 3 (X, Y & Z) accelerometer values. From this data pitch and roll can be calculated. The trigonometry equations for this is on page 21 of the "tilt compass description" link above.

That said we notice on that same page an equation to calculate the magnitude of acceleration.

So it may be possible that we can calculate the velocity due to the magnitude of the overall acceleration by sampling the accelerometer X,Y & Z sensors over time. And avoid trigonometry entirely.

However there is a problem. We have not accounted for gravity. Again, if we assume gravity is in the same direction as travel we can subtract it it out. But if travel is, say, 45 degrees off from the gravity vector, we would only want to subtract cos(45) * 9.8m/s2. Consider only knowing the overall acceleration magnitude does not supply enough information to determine the angle between the acceleration due to gravity and acceleration due to a change of velocity from zero to an unknown velocity with respect to earth.

At this point decisions need to be made regarding how to account for gravity. 9.8m/s2 may be trivial when compared to the vehicle's acceleration and therefor can be ignored. Or it may be so critical that extreme measures like tracking where the sun is in the sky become a feasible way to find the actual pitch and roll of the vehicle.

• OMGoodness, did I really say "gumball lock"? Mar 28, 2022 at 18:57
• How would i go about using trig? Mar 31, 2022 at 9:58
• I can not answer that in the space of a comment. So I will add it to the end of the above answer. Mar 31, 2022 at 13:34
• Also, this video may help. But pay attention to what he says at this point in that video. Mar 31, 2022 at 14:37