While developing the autonomous tracked robot, I needed a way to obtain the vehicle’s orientation. This could be accomplished through an inertial measurement unit (IMU). The MPU-6050- is a widely available and inexpensive sensor that contains an accelerometer, a rate gyroscope, and a temperature sensor. As this sensor is common, there are multiple libraries available for Arduino to retrieve its measurements. However, while these libraries worked well, they were often overly complicated and included overhead and functions I did not need. Moreover, they tended to consume a lot of the limited memory available on the Arduino. I found this wasteful, especially since I only needed the Arduino to receive data from the sensor. It seemed unnecessary to use more resources than necessary for what appeared to be a simple task.

image

A commonly sold MPU-6050 module

To circumvent this issue, I decided to write my own library to interface with the IMU sensor. Its primary objective was to provide a simple interface for retrieving data from the sensor while keeping the overhead very low. To achieve this, I used template variables along with constant expressions. This allowed me to load constants into a class without declaring them as variables inside the class, preventing the consumption of volatile memory on the Arduino. The use of constant expressions also helped, as the variables were stored in the larger read-only memory of the Arduino. Consequently, the library’s use of volatile memory was minimal. Furthermore, the functions that retrieved data from the sensor were designed not to store a state; any data retrieved was immediately manipulated into its final form and provided as output, with no persistent data stored. Once written, the library could do the following:

  • Configure the built-in low-pass filter of the MPU-6050.
  • Configure the sensitivity of the accelerometer and gyroscope.
  • Retrieve the raw outputs of the sensor.
  • Output scaled accelerometer and gyro values in g-units and radians per second.

To provide accurate measurements, I developed two functions to calibrate the gyroscope and remove sensor bias. The first function is called when the sensor is turned on and not moving; it takes a long-term average of each axis’s output and subtracts these values from the raw signals. The second function updates these initial averages using a moving average. These updates are modulated through a gain controlled by a method akin to a Kalman filter. Whenever the gyroscope measurements are small, the bias averages slowly update to ensure the sensor outputs are close to zero. However, once measurements exceed a threshold, the bias averages stop updating. This means any long-term bias in the sensor is eventually canceled out, while larger instantaneous measurements remain unaffected by the corrections. By polling the bias function, it’s possible to correct the gyroscope drift over time.

However, the accelerometer needs to be calibrated manually by correcting the bias and scale. This is necessary because the main component of acceleration, gravity, is a vector that depends on the accelerometer’s orientation. Any calibration algorithm must know the acceleration vector to remove the actual bias. Since determining the gravity vector is complex, it is simpler to calibrate the sensor manually than to create an algorithm for this purpose.

image

Connection of the MPU-6050 with an Arduino MEGA

Note: The library was originally written for an Arduino MEGA, but it has functioned well on Arduino Uno and Nano without issues. For more details on the sensor, see the sensor’s documentation: MPU 6050 register map

In practice, this custom library was capable of maintaining fairly steady acceleration and gyroscope readings, even under strong vibrations. A great example of its robust behavior was its use in a helicopter flight controller, where it ensured steady measurements for 30 minutes or more. In the calmer conditions of a ground vehicle, the library performed exceptionally well for its intended purpose in the autonomous tracked robot.

GitHub Repository

The library can be downloaded directly from the GitHub repository or through the Arduino library catalog in the IDE. View the repository here: basicMPU6050