Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 38 ยท Sensors and Perception

Inertial Sensors (IMU)

Learn how accelerometers, gyroscopes and magnetometers measure motion and orientation, why gyro angles drift, and compute tilt from gravity in Python.

An inertial measurement unit (IMU) is a small chip that feels motion from the inside. It does not look at the world; it senses how the robot is accelerating and rotating. Phones use IMUs to rotate the screen; drones use them to stay level; legged robots use them to balance. A typical IMU holds a 3-axis accelerometer and a 3-axis gyroscope (6 axes), and often a 3-axis magnetometer (9 axes). This lesson explains what each part measures, what goes wrong, and how to get tilt from the data.

The accelerometer

An accelerometer measures specific force: acceleration relative to free fall, in the sensor's own axes. Lying still on a table, it reads about 9.81 m/sยฒ (1 g) pointing up, because the table pushes against gravity; in free fall it would read zero. That means a still accelerometer is a gravity sensor: the direction of the reading gives tilt. When the robot also accelerates, the reading is gravity plus the motion, so tilt from the accelerometer is only reliable when the robot is not accelerating much.

For roll ฯ† (about x) and pitch ฮธ (about y) from readings (ax, ay, az):

Text
roll  = atan2(ay, az)
pitch = atan2(-ax, sqrt(ayยฒ + azยฒ))

Yaw (heading) cannot be found from gravity, since rotating about the vertical axis does not change the gravity direction.

Python Tilt from the accelerometer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows the weakness: acceleration looks like tilt. Accelerometers are also noisy and pick up vibration from motors.

The gyroscope

A gyroscope measures angular velocity (degrees or radians per second) about each axis. It is smooth and fast, and unaffected by linear acceleration. To get an angle you integrate: angle += ฯ‰ ร— dt. Because of this, every tiny error accumulates. Two error sources matter:

  • Bias: a constant offset (a still gyro reads 0.3 deg/s instead of 0). Integrated, it becomes a steadily growing angle error: drift.
  • Noise: random jitter that integrates into a random walk.

You measure the bias while the robot stands still and subtract it, but bias also changes with temperature, so drift can never be fully removed.

Python Gyro integration drifts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two sensors complement each other: the accelerometer is noisy but does not drift (on average), while the gyro is smooth but drifts. Combining them is the first example in Sensor Fusion.

The magnetometer

A magnetometer measures the Earth's magnetic field and gives a compass heading, the only inertial way to know yaw. In practice it is the hardest sensor to use well: motors, wires and steel frames add hard-iron (constant offset) and soft-iron (distortion) errors, and the field direction varies from place to place. Calibration means turning the robot through all orientations and fitting a sphere. Indoors, many robots skip the magnetometer and use other cues for heading.

Python Compass heading from a magnetometer with an offset

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading IMU data in practice

  • Use the datasheet range settings: a larger range (ยฑ16 g) loses resolution, a smaller one (ยฑ2 g) can saturate during a bump.
  • Sample at a fixed rate (200 Hz or faster for balancing) and use the real time difference in dt, not a nominal one.
  • Mount the IMU rigidly and away from vibration, or low-pass filter it.
  • Keep the axes convention straight: right-handed, and which way is forward, left, up. Wrong axes give tilt with the wrong sign.
  • Remember the units: gyros often report in degrees per second, but math libraries expect radians.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a stationary accelerometer on a flat table read on its vertical axis?

    The sensor measures specific force: the table pushes up with 1 g, so the reading is about +9.81 m/s^2 up.
  2. 2. Why does integrating a gyroscope reading drift?

    Any constant offset in rate becomes an ever-growing angle error.
  3. 3. Which angle can an accelerometer NOT provide?

    Gravity direction is unchanged by rotation about the vertical axis, so yaw needs a compass or other sensor.
  4. 4. What is a good way to estimate gyro bias?

    When still, the true rate is zero, so the average reading is the bias.

Summary

  • An accelerometer measures specific force: a gravity sensor that gives roll and pitch when the robot is not accelerating.
  • A gyroscope measures angular rate; integrating gives angle but drifts because of bias and noise.
  • A magnetometer gives heading but is easily disturbed and needs calibration.
  • Accelerometer and gyro have opposite strengths, which is why they are fused.

Next: Cameras and Computer Vision