CodeTheRobot

Ubuntu Linux for Robotics · Lesson 66 of 96

IMU drivers

Serial and I2C IMUs, the sensor_msgs/Imu message and checking orientation and units.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain imu drivers in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

An IMU driver publishes sensor_msgs/Imu: angular velocity (rad/s), linear acceleration (m/s²) and optionally an orientation quaternion, in the IMU's frame. Common sensors use I2C, SPI or USB serial; on a Raspberry Pi enable I2C with raspi-config and check the device address with i2cdetect -y 1.

Verify the physical facts: a still IMU should read about 9.81 m/s² on the vertical axis and near zero angular velocity; rotating it about z should change angular_velocity.z with the right sign (REP 103: x forward, y left, z up). Calibrate or estimate the gyro bias before using it for navigation.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the imu drivers commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

sudo apt install i2c-tools
# Install the I2C debugging tools.
sudo i2cdetect -y 1
# Scan I2C bus 1 for the sensor's address (for example 0x68).
ros2 run imu_driver imu_node --ros-args -p frame_id:=imu_link
# Start the driver (name depends on your sensor).
ros2 topic echo /imu --once
# Print one message and check units and axes.
ros2 topic hz /imu
# Check the sample rate (often 100–200 Hz).

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · Sensor view

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

SENSOR VIEW

Demo for IMU drivers. Expected result: A list of I2C addresses with the sensor at 68

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: scan I2C bus 1 with i2cdetect (-y answers yes to prompts)

Expected output

A list of I2C addresses with the sensor at 68
Stuck? Show the full solution (try it yourself first)
sudo i2cdetect -y 1

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Read one IMU message while the sensor lies still and confirm gravity is on the z axis. Rotate it by hand about z and note the sign of angular_velocity.z. Compute the gyro bias from 10 seconds of still data.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.