<p>This is the best damned reference on rotations: <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.110.5134" rel="nofollow noreferrer">Representing Attitude: Euler Angles, Unit Quaternions, and Rotation</a> by James Diebel of Stanford University (dated  2006-10-20).</p>
<p>You didn't specify, but you're almost certainly using the Tait-Bryan convention-- which is first yaw, then pitch, then roll-- for rotation axes. You're also possibly using NED for translation axes, but that's less clear.</p>
<p>You can see all the mappings in Sec. 5.6.1 in the above link. You might check that you have correctly mapped your quaternions to the rotation matrix. It's very easy to mess up a sign value. Pay special attention to the fact that the inverse of a rotation matrix is its transpose, so it's very easy to have the right rotation matrix values but in the transposed order.</p>
<p>To get to your specific question, using eqn. 35, you can see how to map an inertial frame acceleration into the body frame, using the rotation matrix. It's a simple matrix multiplication, and in your case, and assuming NED, the vector <code>z</code> is <code>[0;0;9.805]</code>.</p>
<p>Lastly, I would suggest getting this to work in simulation first, before trying empirical. Rotations are tricky because they're so pedantic, and it's easy to approach them backwards.</p>

CC BY-SA 4.0
2020-08-01T02:17:11.203Z
Here is the [pdf version of the paper you cited](https://www.astro.rug.nl/software/kapteyn/_downloads/fa29752e4cd69adcfa2fc03b1c020f4e/attitude.pdf) from the  University of Groningen.
sempaiscuba
CC BY-SA 4.0
2020-08-01T13:15:41.737Z
Looks good, although I prefer the  CiteSeer link (which also has the PDF) because I feel, without empirical evidence, that it is more likely to be operational in 10 or 20 years.
Kenn Sebesta
CC BY-SA 4.0
2020-08-01T16:31:47.417Z
Fair enough.  I've suggested an edit to have the link display the title of the paper - just in case CiteSeer also goes down!
sempaiscuba
CC BY-SA 4.0
2020-08-02T03:02:47.937Z
That's a great addition, thanks!
Kenn Sebesta
CC BY-SA 4.0
2020-07-31T20:16:11.453Z
2020-08-02T03:04:14.260Z
2020-08-02T03:04:14.260Z
Kenn Sebesta
<p>I am working on finding Velocity and Displacement from an IMU sensor (MPU9250). I have a Navio2 RPi4 setup, which gives me raw data (acceleration, Gyro, Magnetic).</p>
<p>As per my research this can be done in following method;</p>
<ol>
<li><p>Finding the Attitude from the raw data. (Using quaternion method or Transformation Matrix)</p>
</li>
<li><p>Once the attitude data is available, calculate the gravity force acting on the acceleration data and eliminate the gravity from Acceleration to find the Linear Acceleration</p>
</li>
<li><p>Integrate the computed Linear Acceleration to find the velocity and Position.</p>
</li>
</ol>
<p>Following the steps above I was able to compute the Attitude from the IMU sensor by following the  <a href="https://github.com/emlid/Navio2/blob/master/C%2B%2B/Examples/AHRS/AHRS.cpp" rel="noreferrer">example</a> provided by Navio2. The example uses Quaternion method (Mahony filter) to fuse the raw data and give the quaternions.</p>
<p>The output is verified and works perfectly. Now I have to move to step 2 i.e, finding the gravity acting on the acceleration.</p>
<p>Using quaternions to find the gravity :</p>
<pre><code>gx = (2 * (q1 * q3 - q0 * q2));
gy = (2 * (q0 * q1 + q2 * q3));
gz = ((q0 * q0) - (q1 * q1) - (q2 * q2) + (q3 * q3));
</code></pre>
<p>This gravity values are subtracted from the acceleration data to find the linear acceleration. Once the linear acceleration is available, integrating it with the time gives the velocity and position. The implementation is experimented by moving the IMU sensor to 100cm. However the position output is not as expected. The units of the raw data have been taken care just to have a sanity check with the implementation.</p>
<p>I have moved to convert the quaternions back to rotation matrix and compute the velocity and position data.</p>
<p><strong>My question is how to find the gravity acting on the acceleration using the Rotation Matrix converted from Quaternion.</strong></p>
<p>I request the community to help me with their valuable inputs and references.</p>
<p>Thanks.</p>

CC BY-SA 4.0
2020-07-19T15:38:55.230Z
Maybe try to ask this in Electronics.SE if you don't receive any answers here. You will need the gyroscope part of this sensor in addition to the acceleration data. You then have to calculate your current rotation relative to the earth's gravitation vector and then you have to take it away from your measured acceleration. Since there is no way of measuring speed, position or the current rotation directly, you can only assume that when the total acceleration is 9.81 +/- 2% it is probably still and facing downward, so you can calibrate it that way.
Bastian Springer
CC BY-SA 4.0
2023-02-09T20:58:12.907Z
Hi @thelazy, were you able to implement this? I am just starting my work on this and it would be really helpful if you can provide some input.
Catalyst
<software><imu>
Finding the velocity and position for small distance from IMU sensors
TheLazy
# Finding the velocity and position for small distance from IMU sensors

(Asked by: username_0 on 2020-07-13)

I am working on finding Velocity and Displacement from an IMU sensor (MPU9250). I have a Navio2 RPi4 setup, which gives me raw data (acceleration, Gyro, Magnetic).

As per my research this can be done in following method;

1. Finding the Attitude from the raw data. (Using quaternion method or Transformation Matrix)
2. Once the attitude data is available, calculate the gravity force acting on the acceleration data and eliminate the gravity from Acceleration to find the Linear Acceleration
3. Integrate the computed Linear Acceleration to find the velocity and Position.

Following the steps above I was able to compute the Attitude from the IMU sensor by following the [example](https://github.com/emlid/Navio2/blob/master/C%2B%2B/Examples/AHRS/AHRS.cpp) provided by Navio2. The example uses Quaternion method (Mahony filter) to fuse the raw data and give the quaternions.

The output is verified and works perfectly. Now I have to move to step 2 i.e, finding the gravity acting on the acceleration.

Using quaternions to find the gravity :

```
gx = (2 * (q1 * q3 - q0 * q2));
gy = (2 * (q0 * q1 + q2 * q3));
gz = ((q0 * q0) - (q1 * q1) - (q2 * q2) + (q3 * q3));

```

This gravity values are subtracted from the acceleration data to find the linear acceleration. Once the linear acceleration is available, integrating it with the time gives the velocity and position. The implementation is experimented by moving the IMU sensor to 100cm. However the position output is not as expected. The units of the raw data have been taken care just to have a sanity check with the implementation.

I have moved to convert the quaternions back to rotation matrix and compute the velocity and position data.

**My question is how to find the gravity acting on the acceleration using the Rotation Matrix converted from Quaternion.**

I request the community to help me with their valuable inputs and references.

Thanks.

**Tags:** software, imu

> **Comments:**
> **username_1**: Maybe try to ask this in Electronics.SE if you don't receive any answers here. You will need the gyroscope part of this sensor in addition to the acceleration data. You then have to calculate your current rotation relative to the earth's gravitation vector and then you have to take it away from your measured acceleration. Since there is no way of measuring speed, position or the current rotation directly, you can only assume that when the total acceleration is 9.81 +/- 2% it is probably still and facing downward, so you can calibrate it that way.  
> **username_2**: Hi @username_0, were you able to implement this? I am just starting my work on this and it would be really helpful if you can provide some input.  

---

## 1 answers

### Answer by username_3 on 2020-07-31. Score: 3

This is the best damned reference on rotations: [Representing Attitude: Euler Angles, Unit Quaternions, and Rotation](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.110.5134) by James Diebel of Stanford University (dated 2006-10-20).

You didn't specify, but you're almost certainly using the Tait-Bryan convention-- which is first yaw, then pitch, then roll-- for rotation axes. You're also possibly using NED for translation axes, but that's less clear.

You can see all the mappings in Sec. 5.6.1 in the above link. You might check that you have correctly mapped your quaternions to the rotation matrix. It's very easy to mess up a sign value. Pay special attention to the fact that the inverse of a rotation matrix is its transpose, so it's very easy to have the right rotation matrix values but in the transposed order.

To get to your specific question, using eqn. 35, you can see how to map an inertial frame acceleration into the body frame, using the rotation matrix. It's a simple matrix multiplication, and in your case, and assuming NED, the vector `z` is `[0;0;9.805]`.

Lastly, I would suggest getting this to work in simulation first, before trying empirical. Rotations are tricky because they're so pedantic, and it's easy to approach them backwards.

> **Comments:**
> **username_4**: Here is the [pdf version of the paper you cited](https://www.astro.rug.nl/software/kapteyn/_downloads/fa29752e4cd69adcfa2fc03b1c020f4e/attitude.pdf) from the  University of Groningen.  
> **username_3**: Looks good, although I prefer the  CiteSeer link (which also has the PDF) because I feel, without empirical evidence, that it is more likely to be operational in 10 or 20 years.  
> **username_4**: Fair enough.  I've suggested an edit to have the link display the title of the paper - just in case CiteSeer also goes down!  
> **username_3**: That's a great addition, thanks!  

---

