medir-la-inclinacion-imu-arduino-filtro-complementario

Measure inclination with IMU, Arduino and complementary filter

  • 9 min

In previous posts, we have seen the operation of accelerometers and gyroscopes as sensors that can be used to determine the orientation of a device.

We also saw the advantages and limitations of each device, and hinted that they combine very well because the characteristics of their measurements complement each other.

In this final post of the series, we will look at IMUs, devices that combine the advantages of both sensors to obtain better functionality than we could get using an accelerometer or gyroscope independently.

Today, their low cost has made them common components in a large number of everyday devices. There are many manufacturers of IMUs, such as Panasonic, Robert Bosch GmbH, InvenSense, Seiko Epson, Sensonor, STMicroelectronics, Freescale Semiconductor, and AnalogDevices.

We will use them frequently in our electronics and Arduino projects, for example, to determine the direction of travel of a vehicle, the orientation of an actuator, the tilt of a platform, or even as a way to interact with a computer using a glove or a 3D mouse.

What is an IMU?

An Inertial Measurement Unit (IMU) is the generic name for a device capable of measuring the velocity, orientation, and acceleration of a system.

Actually, accelerometers and gyroscopes can be considered simple IMUs, but in general, the name IMU is reserved for devices that integrate more than one measurement system.

When talking about IMUs, it is common to refer to the number of degrees of freedom (DOF) it has. The degrees of freedom of a sensor represent the number of independent magnitudes it can measure.

Thus, a 3-axis orthogonal accelerometer is a 3 DOF sensor. Similarly, a gyroscope that measures angles on 3 orthogonal axes is a 3DOF sensor.

The IMUs we will most frequently encounter are:

  • 6 DOF IMU, combining a 3-axis accelerometer and a 3-axis gyroscope.
  • 9 DOF IMU, adding a 3-axis magnetic compass.
  • 10 DOF IMU, which adds a barometer for estimating the sensor’s altitude.

As we can see, most IMUs start from a combination of accelerometer and gyroscope, this being the most common IMU. The reason is that both devices combine very well and compensate for each other’s limitations.

  • Accelerometers do not have drift in the medium or long term, as they perform an absolute measurement of the angle the sensor forms with the vertical direction, marked by gravity. However, they are influenced by sensor movements and noise, so they are not reliable in the short term.
  • Gyroscopes work very well for short or abrupt movements, but since they use vibration gyroscopes that actually measure angular velocity and obtain the angle by integration over time, they accumulate errors and noise in the measurement, so they have drift in the medium or long term.

Therefore, combining the measurements of both devices allows IMUs to obtain more precise orientation measurements than an accelerometer and a gyroscope separately (we will see this next, but first we will reflect on what orientation is).

Obtaining Orientation with an IMU

To determine the distribution of an object relative to a base in three-dimensional space, we must establish its position and orientation. Three parameters are required to determine the relative position and another three to determine its orientation.

In everyday life, we are used to dealing with the position of an object, and we know there is more than one way to express its relative position mathematically. For example, we can express its coordinates relative to another base using Cartesian (X, Y, Z), cylindrical, or spherical coordinates.

However, we are not as accustomed to working with the parameterization of orientation, as mathematically it is somewhat more complex. Like position, there is more than one way to express the relative orientation of two systems (such as Euler angles, rotation matrices, or quaternions).

Probably the most widespread and intuitive are the navigation angles, or Tait-Bryan angles, where orientation is represented as three orthogonal rotations around the X (roll), Y (pitch), and Z (yaw) axes.

Tait-Bryan angles are a variation of Euler angles, introduced by the mathematician Leonhard Euler during his study of rigid body mechanics.

arduino-imu-3-ejes

It is common to incorrectly find Tait-Bryan angles referred to as Euler angles. Euler angles are a more general formulation. Another difference is that Euler angles take the vertical as the measurement origin, while Tait-Bryan measures angles relative to the horizontal.

Tait-Bryan angles are intuitive and simple. They also have the advantage of being commutative, meaning the final obtained orientation is independent of the order in which we apply the rotation (something that normally does not happen in the representation of rotations).

However, they suffer from a major disadvantage called gimbal lock, which affects all Euler angle systems. When the Y axis rotates +-90º, the X and Z axes coincide, so the system degenerates to 2DOF, which results in a non-differentiable singular point that can cause stability problems in calculations.

Gimbal lock is such a significant disadvantage that the usual way to represent rotation is by using quaternions. Quaternions are an extension of complex numbers introduced by Hamilton in 1843, which use three imaginary units i, j, and k such that,

It is known that complex numbers can be used to represent vectors in the plane. Equivalently, a quaternion allows expressing a vector in three-dimensional space.

To express the rotation of a point by an angle α, around an arbitrary vector with coordinates Ex, Ey, Ez, the resulting quaternion adopts the following expression

Compared to Euler angles, quaternions are simpler to compose and avoid the gimbal lock problem. Compared to rotation matrices, they are more efficient and numerically stable.

For this reason, quaternions are one of the most common ways to express orientation in computer graphics and will be the preferred method to use when we want to work with orientations in IMUs and Arduino projects.

Signal Filtering in an IMU

To have the short-term advantages of the gyroscope and the medium and long-term advantages of the accelerometer, it is necessary to combine and filter the raw (RAW) recorded signal.

There are several possible filters, the most famous being the Kalman filter, developed in 1960 by Rudolf E. Kalman. It is considered one of the great discoveries of the 20th century for its implications in sensor filtering (not just IMUs) and is one of the architects of the space race.

In broad terms, the Kalman filter makes an estimate of the future value of the measurement and then compares the actual value through statistical analysis to compensate for the error in future measurements.

However, the general version of the Kalman filter involves performing complex calculations (see Wikipedia) that represent an excessive implementation and calculation time for Arduino.

For this reason, it is common to use a simpler filter called the complementary filter. In fact, the complementary filter can be considered a simplification of the Kalman filter that completely dispenses with statistical analysis.

There are several formulations for a complementary filter. In its simplest expression, the complementary filter can be expressed as.

Where A and B are two constants that, initially, can be taken as 0.98 and 0.02 respectively. We can calibrate the filter simply by varying the values of A and B as long as we meet the condition that they sum to 1.

arduino-giroscopio-filtro-complementario

The complementary filter behaves as a high-pass filter for the gyroscope measurement and a low-pass filter for the accelerometer signal. That is, the gyroscope signal dominates in the short term, and the accelerometer in the medium and long term, which is exactly what we want to compensate for their advantages and defects.

In most domestic applications, the complementary filter is sufficient and provides values very similar to the Kalman filter, as long as the values of A and B are properly calibrated.

There are more sophisticated versions of the complementary filter, as well as simplified one-dimensional versions of the Kalman filter that can be implemented in Arduino. We will see this in a future advanced post.

Meanwhile, we will use IMUs like the MPU6050 and the filters used in our electronics and Arduino projects.