# IMU, Calibration and Drift

## Drifting output

On board a Head Tracker there is a sensor known as IMU or Inertial Measurement Unit. These integrated circuits are actually built up of up to three sensors on one chip.

* **Accelerometer** - measures acceleration and Earth's gravity
* **Gyroscope** - can measure the rotational speed of the board
* **Magnetometer** - measures the magnetic field, we are interested in the Earth's field here

Only when you incorporate all three of the above sensors it's possible to build an absolute orientation relative to the earth. The process of incorporating all these sensors is done in a Sensor Fusion algorithm. The algorithm used here is the [Madgwick](https://ahrs.readthedocs.io/en/latest/filters/madgwick.html) filter.\
\
A **Gyroscope** is really good at telling your head's rotational speed. You could use it alone but it has two issues: drift and integration error.

&#x20;\- Every gyroscope either in an IC or even a physical one used on a spacecraft all experience drift. We can counter act some of this drift with a calibration step. This is done as soon as you turn it on. Leave the board sitting still until the red light stops blinking.

\- Integration error is the other issue. The processor takes rotational readings as fast as possible. Each reading is multiplied by the time since the last one. This gives the change in degrees since the last measurement. The change is then added to the last one. Since it's stepped measurements all the data in between is lost causing some inaccuracy.\
\
The **Accelerometer** can be used to cancel out the Gyro's drift in **two axis**, tilt and roll. Because it's known that earths gravity always pulls straight down the algorithm can now cancel drift on the tilt and roll. The accelerometer readings are fairly clean, they always points straight down. You could even calculate the tilt and roll directly since as a Head Tracker you are stationary. This is why the tilt and roll are always quite stable.

The **Magnetometer** is used to cancel out the drift on the pan axis. **This sensor is where a lot of problems arise.** The problem lies in that the strength of the magnetic field of the Earth can easily be overpowered by local sources of magnetic field such as:

&#x20;   \- Electric currents\
&#x20;   \- Permanent magnets\
&#x20;   \- Iron and other permeable metals objects

**Every location on the earth has different direction of the Earth's magnetic field**. It will be pretty much unusable on the north and south poles as it will mostly just point downwards like an accelerometer.\
\
The calibration step can only alleviate two of these issues. You can only correct the **sensors offset's** and the **warping** caused by metal objects nearby. This is what the calibration step is doing. It is trying to find a way from the readings it took to incorporate them back into a perfect sphere.

## How to do a calibration.

{% embed url="https://www.youtube.com/watch?t=234s&v=DwtC1GqwQ4U" %}
How you can properly do a calibration.
{% endembed %}

## Magnetometer Calibration

![](https://1791219135-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FifYnTDP7IFJCZallDj96%2Fuploads%2FjLcsDzUTn5YGc8zR6sTs%2Fimage.png?alt=media\&token=42817781-bab0-46c7-93e5-70e9f525f6e3)

This is the most important step. Without a good magnetic calibration you are going to get drift/instability in the pan axis. **Be sure to do this far away from magnetic field sources (computers, speakers, power cables, etc.)... as far away as your USB cable length will allow.**

{% hint style="warning" %}
You need to do the calibration with the board mounted on your goggles. Everything including goggles should be powered up. If you cannot get the calibration step to pass like this it will not give good results while flying.\
\
You can test with the board alone but if you can't get a good calibration while mounted and powered up you need to re-mount the board in a different location.
{% endhint %}

{% hint style="danger" %}
The readings of the LSM9DS1 magnetometer on the Nano33BLE board are very noisy. The drift adjustment needs to be filtered quite a bit and is then slowly applied. Due to this you may see drift after startup if the initial orientation guess wasn't good. **Just let it sit, rotate around a bit and it should stabilize, then re-center.**
{% endhint %}

{% hint style="info" %}
When doing the calibration don't worry about trying to get all the values super low. It the save button shows up it has enough data.
{% endhint %}

{% hint style="info" %}
**If you cannot get it to work without drift in your setup,** disable the magnetometer during calibration. It will still work well enough for an average length flying session.\
\
**! It is important if the magnetometer is disabled,** on boot you must set it down still until the red led stops blinking.
{% endhint %}



