Intro to Inertial Measurement Unit(IMU) Part 1

Niranjan Reddy
14 min readSep 19, 2020

--

A complete guide to understanding accelerometer, gyroscope, and magnetometers.

Advanced Inertial Reference sphere Credits: link

The guidance system plays a crucial role in many activities like space crafts, airplanes, and even some of the common things like our phones and some of the gaming consoles. The guidance system built in the olden days took a lot of space and also were difficult to operate. However, technology has come a long way and now we can get an IMU(Inertial Measuring Unit), the size of our thumb, or even smaller.

IMU helps in giving an accurate orientation of an object with respect to its environment. Though orientation might seem trivial for humans, it is the very thing that automatically shifts our phone to portrait or landscape mode. It plays a crucial role in the navigation of planes and also spacecrafts. We will have a deep dive into the various components of a basic IMU and how to use it in this series of blogs.

There is a wide range of IMUs depending upon the usage and some costing around thousands of dollars, while others(hobbyists) costing around a few dollars. Most of the hobbyist’s IMUs consist of an accelerometer and Gyroscope. Some also contain a magnetometer. We will be mostly talking about easily available IMUs. The outline of contents ->

  1. Accelerometer and its calibration
  2. Magnetometer and its calibration
  3. Gyroscope and its calibration
  4. Finding roll and pitch from Accelerometer
  5. Finding Yaw from Magnetometer

Few Notations before we get started

roll(ɸ rotation along X-axis)

pitch(θ rotation along Y-axis)

Yaw(Ѱ rotation along Z-axis)

Accelerometers

They measure the acceleration that is being applied on the sensor in 3 perpendicular directions. The good part of these sensors is that they are very small with close to accurate values. However, they do have a good amount of noise associated with them. The working of them can be understood by seeing the below gif. The acceleration will cause the movement of the mass which inturn produces varying voltage due to change in capacitance. This voltage is mapped to the acceleration. These values that map the change in voltage to the real world acceleration are called the sensitivity values.

Figure 1: Working of an accelerometer. The movement of mass due to acceleration causes the capacitance to change across plates and hence gives a varying output voltage which is interpreted as acceleration across that axis. Credits for gif link

Though most accelerometers are calibrated in the factory before sending for sale, the values might slightly differ after these are soldered to the breakout board or the PCB. It becomes important to calibrate the accelerometer to have close to accurate values from all the three axes.

Calibration of Accelerometer

The following equation represents how the accelerometer values are obtained from the internal analog-to-digital-converters(ADCs).

Here p_x, p_y, p_z are scale factors that were found during original factory calibration. Similarly, offset for all three axes are also found in the same calibration.

The values Gx, Gy, Gz do not give the acceleration in terms of m/s2, instead, it needs to be multiplied by the sensitivity factor depending upon the range of the acceleration and the bits used to represent Gx, Gy, Gz

If the sensor has a range of acceleration 2g, then the sensitivity factor is 2g/(2¹⁶ –1), if the value is represented in 16 bits.

The goal of the calibration is to find the scale and the bias to make the accelerometer accurate with the current breakout board or your own PCB. We will be assuming each axis to be independent of each other and hence looking at one specific axis. We will then apply the procedure for the remaining axis.

Let us take the z-axis ->

We will be taking values of Gfz when the z-axis accelerometer is parallel with gravity and point downwards and the second one when it is pointed upwards. The values of Gz in these two cases are known and it is +9.8 in the first case and -9.8 in the second case.

As we know the values of G[0]fz and G[1]fz by taking the mean of the values over a few seconds. We can solve the equation for Sz and bz by

Similarly for other axes, the equations are as follows -

While calibrating for X-direction, you will have to place the x-axis of the accelerometer parallel to the earth’s gravitational field.

While calibrating for Y-direction, you will have to place the y-axis of the accelerometer parallel to the earth’s gravitational field.

The code below does the complete calibration, you will have to place the accelerometer in 6 different positions based on the prompts by the code. However, the order of positions doesn’t matter as long as all different 6 positions are used. The code takes care of the order.

The above code calculates the mean values after every direction and based on a few checks and after collecting for all directions, uses the equations discussed above to get the new scale and bias for each direction.

Magnetometer

This sensor is built on the hall effect principle. It is based on the fact that a voltage is created when a magnetic field is passed perpendicular to the current flowing in the conductor. The voltage generated is measured to get an understanding of the earth’s magnetic field. Once we know the Earth’s magnetic field, we roughly know the true North.

However, the magnetometer is sensitive to other magnetic fields from its surroundings apart from the Earth’s magnetic field. The two types of effects on magnetometers are normally categorized as hard iron interference and soft iron interference.

Hard-Iron Interference

Some magnetic fields are generated by permanently magnetized ferromagnetic components which are normally placed on PCB. Also, it is very common for magnetometers to come with some amount of field offset. Though the manufacturer calibrates it, there will still be some offset when it is soldered to the PCB or used in robots or elsewhere. The good thing about these interferences is that they can be modeled as another vector of magnetic field in a specific direction. Dealing with this type of interference is more like finding the bias in each direction.

Soft Iron Interference -

This type of magnetic field comes up when an electric current is passed through unmagnetized ferromagnetic components. These fields normally occur from batteries or other high power carrying lines on PCB. These are also a bit tougher to model as they affect a specific axis. In some approximations it can be considered as a scale for each axis, however, a better way to model it would be by using a symmetric transformation matrix(3*3).

Plotting magnetometer values at various roll and pitch angles, before calibration, gives the idea of the hard iron effects and soft iron effects.

Representing three different projections of the ellipsoid by using two axes at once. The separation shows the hard iron effects and the elongation along one axis shows the soft iron effects.

The difference in the alignment is what represents the hard iron effects. By combining these points, you get a 3-dimensional vector that can center all the three ellipses to zero.

It shows the correction of the hard iron effect.

There is still some problem with the magnetometer values, there is some deviation along some axes. To correct this we will be using the transformation matrix A. The complete equation as follows -

Mag = (M — V)*A

Where M represents raw mag values, V represents the 3-dimensional bias vector and A represents the symmetric transformation matrix.

It shows correction of both hard-iron and soft-iron effects

Applying both soft iron and hard iron correction.

There two ways to find the bias and the transformation vector from magnetometer values.

  1. A simple way -> In this method, we assume the transformation vector(V) to be a diagonal matrix. This basically means that we are assuming that all axes are independent. This basically breaks down to finding the scale parameter for each axis. The bias can be found by simply finding the mean of the magnetic values obtained in various roll and pitch configurations. The scale can be found by averaging the mean along diameter across each axis and scaling each axis to fit into that average length.

2. Slightly Complex(Fitting the ellipsoid) -> The mag values mostly represent a 3-dimensional ellipsoid. The idea is to transform it into a sphere. However, to transform it into a sphere, we first need to find the parameters of its initial ellipsoid. The equation for a 3D ellipsoid is as follows.

Here, M is the mag values collected over a period of time, V is considered the offset, and W is the correlation with other axes and β is the length of the ellipsoid. Once we fit a good ellipsoid, we can easily get the hard iron and soft iron corrections as Bias = V and the transformation matrix as below

We use the least-squares method to find the best possible parameters.

Note: In both cases of calibration it is important to rotate the magnetometer in an eight shape so that the sensor is exposed to various combinations of roll and pitch.

Credits to ellipsoid fit function link

Gyroscope -

This sensor measures angular velocity plays which a crucial role in tracking the orientation during movement. It is often used in various filters with an accelerometer and magnetometer to get a more accurate and stable orientation. It is based on the Coriolis principle where a force is acted on by a mass that is moving with velocity and angular velocity is applied to it. In the gif below, the spring moves constantly in one direction. As angular velocity is applied, it starts slightly displacing in the perpendicular direction which changes the capacitance and thus gives a difference in electrical voltage. This difference is mapped to the angular velocity through the sensitivity factor.

Gif: The spring moves only in the driving direction when no angular rate, however when it is applied the spring changes the capacitance between electrodes. credits link

Calibration of Gyroscope -

The gyroscope mainly has a bias that needs to be subtracted from the sensor readings to give an accurate estimate. This bias can be calculated by taking a bunch of sensor readings when the sensor is still. Average these readings in all the directions to get a 3 element vector of bias of the gyroscope. This bias has to be later subtracted for every reading to get the actual angular velocity.

If all the gyro values display zero readings(when IMU is stationary), it confirms that calibration has been done.

Estimating roll and pitch from Accelerometer values:

This part involves some math and it requires an understanding of rotation matrices. I try to give a brief understanding of them here, however, if you want to dig deep in them, here is a link.

Most of the orientation is expressed in three terms where roll is for rotation about x-axis, pitch is for rotation about y-axis and yaw is rotation around z-axis. The combination of these three rotations can easily express any complex rotation. As discussed in the start, one of the use cases of IMU is to find the orientation. With an accelerometer, we know the acceleration vector Gs in the sensor frame. We also know that the acceleration on earth is g.

Where a is the external acceleration on the sensor and R is a rotation matrix representing roll, pitch, and yaw.

One of the common assumptions made with IMUs is that the external acceleration will be zero. This makes sense as we mostly measure the accelerometer in very small time intervals in which there isn’t much impact by external acceleration and thus this assumption mostly holds out. There are cases where this can be bad to have this assumption and that will be discussed at the end.

Considering a = 0

Intuitively the equation above suggests that the earth’s gravitational is rotated by R to give the specific accelerometer values in the sensor frame. Essentially we have to find R to get the orientation. Before we go on to find R, I would like to state that it is not possible to find yaw with accelerometer readings alone. The main reason for this is because the z-axis is parallel to the earth’s gravitational force. A change in roll and pitch will be seen in sensor values, but when you change yaw, there won’t be any difference in sensor values. Intuitively, it is like measuring an angle between 2 parallel lines, it is zero and will be zero when you rotate one line in the plane perpendicular to the lines. We will be using a magnetometer to get the heading(yaw) of the sensor.

The rotation matrix can be divided further -> R = Rx(ɸ)*Ry(θ)*Rz(ѱ)

Where

The above are standard equations of rotation where each angle is assumed to have a clockwise rotation.

Actually there are 6 possible ways for R, since the order of the rotation matters. It can be ->

The order of rotations in equations 5 to 8 will give the yaw in the equations and hence it doesn’t make sense to include them or compute them as the yaw obtained would be one of the many solutions. We will be mostly looking at equation rotation of 3 and 4.

Taking the order of equation 3

By dividing the second and third-row we get

Using sin(ɸ) we can get pitch by dividing 1 and 2 rows. Substituting for sin(ɸ) we get

Taking the order of equation 4

Dividing equation 1st and 3rd row, we get

Using a similar concept as in the first method we can get roll by -

Using tan inverse we can find both roll and pitch.

Estimating Yaw using magnetometer:

The IMU when placed flat on the ground/table and made to point towards the magnetic north, then it has only x and z components. The measurement during that time would be

Here Bs is the mag values by the sensor and Br is a scalar representing the magnetic strength in that area. In this case, the sensor is aligned with the magnetic field, but to represent a generic case, we can do it by adding the rotation matrix.

As we know roll and pitch already, we can try multiplying with its inverse on both sides. An inverse of a rotation matrix is basically replacing the angle with -1*angle.

Taking the LHS side:

Now taking RHS side:

Equating both LHS and RHS

By dividing 1st and 2nd row we get yaw as Br gets canceled.

We will be requiring both roll and pitch for estimating yaw from mag values.

Gimbal Lock

This is something that was very common in apollo missions and especially apollo 13 which was dangerous and also very difficult for astronauts to navigate avoiding it. Gimbal lock can be disastrous because it doesn’t completely reflect the changes happening in the real world rotations in terms of Euler angles. More precisely speaking, this normally happens by loosing one of the dimensions. It can be mathematically seen below

By taking θ as π/2, we can see the above equation can be reduced to

If you change the roll(ϕ) or the yaw(ψ), but the rotation happens along only the z-axis. This is also seen as losing one degree of freedom(in this case roll).

This basically happens when a plane starts pointing upwards or downwards. It is more common with some of our daily devices like phones or gaming consoles. In these cases, people use different configurations of roll, pitch, and yaw to avoid the gimbal lock or sometimes they use a completely different representation like quaternion(which we will cover in the next blog).

Conclusion

Now that we have figured out a way to get the orientation from the accelerometer and magnetometer, but these readings come with a good amount of noise. We can definitely use some smoothing functions over these values to reduce some noise. However, a better method is to use the gyroscope’s information to better estimate the orientation. For that, we will be talking about sensor fusion algorithms like Kalman and Madgwick in the next blog. We will be covering basic math with respect to the IMU. We will follow that up with a complete guide to implementing these on raspberry pi 3 with MPU9250.

Let me know your thoughts in the comments or any doubts related to the article.

--

--

Niranjan Reddy

Robotics, Statistics, ML and CV deployment on hardware.