%%
Title: IMU
Created: 2022-04-07 11:32
Status:
Parent: [[Resources/Hardware/Robot]]
Tags:
Source:
%%
# IMU
An IMU is typically a 9dof sensor:
- 3-axis gyro (e.g. $G_x$, $G_y$, $G_z$)
- 3-axis magnetometer (e.g. $M_x$, $M_y$, $M_z$)
- 3-axis accelerometer (e.g. $A_x$, $A_y$, $A_z$)
## Magnetometer
### Orientation
This [[assets/papers/AN4248.pdf|application note]] from NXP has a lot of discussion.
The standard orientation is with $x$ pointing forward, $y$ pointing right, and $z$ pointing down. This is the NED (North, East, Down) orientation for an $x,y,z$ coordinate system. Assuming everything else is zero, a positive $x$ corresponds to north, a positive $y$ corresponds to east, and a positive $z$ corresponds to down.
The IMU should have markings indicating the orientation; for example, a Sparkfun MPU09150 breakout board and part of the control board for a [[Projects/Robots/3Pi|Pololu 3Pi+]].
![[assets/images/IMUOrientation.jpg]]
### Compass heading
Follow something like this (where $V_x$ etc are the [hard iron effects](https://www.nxp.com/files-static/sensors/doc/app_note/AN4246.pdf)):
- $roll = atan^2(G_y, G_z)$
- $pitch = atan(\frac{-G_x}{G_y sin(roll) + G_z cos(roll)})$
- $yaw =$
$
\begin{aligned}
atan^2((M_z - V_z)sin(roll) - (M_y - V_y)cos(roll),\\
(M_x-V_x)cos(pitch)+\\(M_y - V_y)sin(pitch)*sin(roll) +\\ (M_z - V_z)sin(pitch)*cos(roll))
\end{aligned}
$
- Clamp the heading
```
if (yaw < 0) {
yaw += 2*PI;
}
```