# Calibration

## Accelerometer and Magnetometer calibration

To calibrate the acelerometer and the magnetometer, we measure the earth’s magnetic field components and the gravity field components in various positions of the sensor/stick. We assume these fields to be constant (constant magnitude, constant direction) in an earth fixed coordinate system. We further assume some idealizations:

- homogenous fields
- no magnetic distorsion
- no linear acceleration measured during calibration
- sensors are orthogonal to each other
- no cross-talk between sensors
- sensors are ideal linear
- etc.

Errors induced by deviations from these idealizations are tolerable for our purposes.

The (magnetic or gravitational) field vector \overrightarrow{f}=\left[f_x \quad f_y \quad f_z \right]^T is calculated from the sensor signals \left[x \quad y \quad z \right] by applying offset and gain values:

$$\overrightarrow{f}=\left[f_x \quad f_y \quad f_z \right]^T=\left[g_x\cdot(x+o_x)\quad g_y\cdot(y+o_y)\quad g_z\cdot(z+o_z)\right]^T$$

Scaled to a unit vector this field vector is always of length 1 for all rotational positions. All measured vectors lay on a sphere with radius 1:

$$\left|\overrightarrow{f}\right|^2=1$$

$$\left(x^2+y^2+z^2\right)=1$$

This leads to an ellipsoid description in “standard form”:

$$g_x^2\cdot(x+o_x)^2+g_y^2\cdot(y+o_y)^2+g_z^2\cdot(z+o_z)^2=1$$

This quadratic euqation can be expressed as a “general” axis-aligned ellipsoid equation (The ellipsoid’s pricipal axes are aligned with the sensors x-y-z-axes):

#### General ellipsoid:

$$Ax^2+By^2+Cz^2+2Dx+2Ey+2Fz=1$$

Both equations (standard form and general form) are equivalent and can be transformed into each other (by completing the squares)

$$A\cdot(x^2+\frac{2D}{A}x+\frac{D^2}{A^2})+A\cdot(y^2+\frac{2E}{B}y+\frac{E^2}{B^2})+C\cdot(z^2+\frac{2F}{C}+\frac{F^2}{C^2})=1+\frac{D^2}{A}+\frac{E^2}{B}+\frac{F^2}{C}$$

$$A\cdot(x+\frac{D}{A})^2+B\cdot(y+\frac{E}{B})+C\cdot(y+\frac{F}{C})=1+\frac{D^2}{A}+\frac{E^2}{B}+\frac{F^2}{C}$$

We define a “radius”

$$R^2=1+\frac{D^2}{A}+\frac{E^2}{B}+\frac{F^2}{C}$$

and get Offsets and Gains from the parameter of the general ellipsoid:

#### Offsets:

$$o_x=\frac{D}{A}\quad o_x=\frac{E}{B}\quad o_z=\frac{F}{C}$$

#### Gains:

$$g_x=\sqrt{\frac{A}{R^2}} \quad g_y=\sqrt{\frac{B}{R^2}} \quad g_z=\sqrt{\frac{C}{R^2}}$$

The general ellipsoid form is used to get the ellipsoid parameters from measuring the sensor values x,y,z and x^2,y^2,z^2 for different sensor positions. This can be done by solving a linear least square minimization:

$$\sum\left(Ax^2+By^2+Cz^2+2Dx+2Ey+2Fz-1\right)^2=min$$

The determined paramters A, B, C, D, E, F can easily be converted into gain and offset values as described above.

### Measurements

To get the required data for the magnetometer calibration, the sensor is moved in a figure-8 while continuosly measuring. It’s sort of 3D-eight, more like poi players do the 3-beat-weave, to get data from as many rotational positions as possible.

To record data for the accelerometer calibration, the sensor is measured in some (at least six) different static positions. The sensor must not be moved during the measurements to avoid mixing of the gravitational field vector with linear acceleration.

## Gyroscope calibration

To calibrate the gyroscope sensors, we do a fixed angle movement between two known positions for each gyroscope axis seperately. To do so, we use a 3D-printed bracket together with a UCF204 pillow block where we can rotate the sensor 90° from stop to stop. The gyro offsets are calculated from the mean values measured during the sensor is in static positions. The gyro gains are calculated by integrating the offset-corrected gyro signal to get rotational positions.

$$g=\frac{\frac{\pi}{2}}{\left|\overline{p_1}-\overline{p_2}\right|}$$

\overline{p_1}, \overline{p_2} : mean position values (integrated gyro) at static stops