Tech Arcanist
Get in touch

75Chapter 75 of 76

Simulate IMU Sensor

Introduction

Chapters 73 and 74 explained how a gyroscope and an accelerometer work, and how both sit together in one chip called an IMU. This chapter gives the simulated BumperBot an IMU of its own and looks at what it measures:

  • a new imu_link in the robot model, fixed on top of base_link,
  • an IMU sensor in Gazebo that publishes 100 readings per second, with a small random noise like a real chip,
  • a bridge that carries the readings from Gazebo into ROS 2 on the topic /imu/out,
  • a look at the readings in the terminal and in PlotJuggler while the robot drives.

What must already work

  • The robot in Gazebo with its launch file (chapter 25) and the Humble or Iron version check (chapter 28).
  • The controllers and the joystick, started the same way as in chapter 71.
  • PlotJuggler, installed in chapter 71.

The four files that change

FileWhat is added
bumperbot_description/urdf/bumperbot.urdf.xacrothe imu_link and the fixed imu_joint
bumperbot_description/urdf/bumperbot_gazebo.xacrothe IMU system plugin and the IMU sensor with its noise
bumperbot_description/launch/gazebo.launch.pythe /imu topic in the bridge, renamed to /imu/out
bumperbot_description/package.xmlthe dependency on ros_gz_bridge

Add the IMU to the robot model

Open bumperbot.urdf.xacro

In VS Code, open src, then bumperbot_description, then urdf, and open bumperbot.urdf.xacro.

VS Code explorer with bumperbot.urdf.xacro selected in the urdf folder
bumperbot.urdf.xacro in the urdf folder

Add this code at the end of the file, just before the closing </robot> tag:

<link name="imu_link">
  <inertial>
    <origin xyz="-0.00552433659106688 0.000168210391520346 0.000514000497342681" rpy="0 0 0" />
    <mass value="0.000528415362211671" />
    <inertia ixx="1.46176048428261E-08" ixy="1.40015117949421E-10" ixz="-1.99633872937403E-12"
             iyy="8.59662482954888E-09" iyz="7.52375112767959E-12"
             izz="2.30279421279312E-08" />
  </inertial>
  <visual>
    <origin xyz="0 0 0" rpy="0 0 0" />
    <geometry>
      <mesh filename="package://bumperbot_description/meshes/imu_link.STL" />
    </geometry>
    <material name="">
      <color rgba="0.792156862745098 0.819607843137255 0.933333333333333 1" />
    </material>
  </visual>
  <collision>
    <origin xyz="0 0 0" rpy="0 0 0" />
    <geometry>
      <mesh filename="package://bumperbot_description/meshes/imu_link.STL" />
    </geometry>
  </collision>
</link>

<joint name="imu_joint" type="fixed">
  <origin xyz="0 0 0.0698986241758014" rpy="0 0 0" />
  <parent link="base_link" />
  <child link="imu_link" />
  <axis xyz="0 0 0" />
</joint>
bumperbot.urdf.xacro with the new imu_link and imu_joint highlighted at the end of the file
The new imu_link and imu_joint at the end of bumperbot.urdf.xacro

What the new lines mean

The IMU is added the same way as the caster wheels: a link that describes the part, and a fixed joint that says where it sits.

PartWhat it says
<inertial>how heavy the chip is and how its mass is spread: 0.000528 kg, about half a gram, so it does not change how the robot moves
<visual>what it looks like: the imu_link.STL mesh in a light blue grey colour
<collision>the shape Gazebo uses for touching: the same mesh
imu_joint, type="fixed"the chip is screwed to the robot and never moves on its own
origin xyz="0 0 0.0699"it sits 0.0699 m (about 7 cm) straight above the origin of base_link, with no turn (rpy="0 0 0"), so its x, y and z axes point the same way as the robot's. The origin of base_link is at the height of the wheel axles (the wheel joints have z = 0), which is the wheel radius, 0.033 m, above the floor. So the IMU ends up near the top of the body: 0.033 + 0.0699 = 0.1029 m above the floor.
Side view of the robot to scale: base_link at the wheel axles 0.033 m above the floor, imu_link 0.0699 m higher near the top of the body
Where the imu_link sits: 0.0699 m above base_link, which is at the wheel axles, so near the top of the body

Add the IMU sensor in Gazebo

Open bumperbot_gazebo.xacro

Open bumperbot_gazebo.xacro in the same urdf folder.

VS Code explorer with bumperbot_gazebo.xacro selected
bumperbot_gazebo.xacro in the urdf folder

Load the IMU system plugin

Gazebo needs a system plugin that simulates IMU sensors. Add it inside the <gazebo> block, right after the ROS 2 Control plugin from chapter 28. The name depends on the ROS 2 version.

For ROS 2 Humble:

<plugin filename="ignition-gazebo-imu-system" name="ignition::gazebo::systems::Imu">
</plugin>

For ROS 2 Iron and newer:

<plugin filename="gz-sim-imu-system" name="gz::sim::systems::Imu">
</plugin>
bumperbot_gazebo.xacro with the IMU plugin added below the ROS 2 Control plugin for Humble and for Iron and newer
The IMU plugin, once in the Humble block and once in the Iron and newer block

Describe the IMU sensor

After that, add the sensor itself. It is attached to the imu_link from the robot model:

<!-- IMU -->
<gazebo reference="imu_link">
  <sensor name="imu" type="imu">
    <always_on>true</always_on>
    <update_rate>100</update_rate>
    <gz_frame_id>imu_link</gz_frame_id>
    <topic>imu</topic>
    <imu>
      <angular_velocity>
        <x>
          <noise type="gaussian">
            <mean>0.0</mean>
            <stddev>2e-4</stddev>
          </noise>
        </x>
        <y>
          <noise type="gaussian">
            <mean>0.0</mean>
            <stddev>2e-4</stddev>
          </noise>
        </y>
        <z>
          <noise type="gaussian">
            <mean>0.0</mean>
            <stddev>2e-4</stddev>
          </noise>
        </z>
      </angular_velocity>
      <linear_acceleration>
        <x>
          <noise type="gaussian">
            <mean>0.0</mean>
            <stddev>1.7e-2</stddev>
          </noise>
        </x>
        <y>
          <noise type="gaussian">
            <mean>0.0</mean>
            <stddev>1.7e-2</stddev>
          </noise>
        </y>
        <z>
          <noise type="gaussian">
            <mean>0.0</mean>
            <stddev>1.7e-2</stddev>
          </noise>
        </z>
      </linear_acceleration>
    </imu>
  </sensor>
</gazebo>
bumperbot_gazebo.xacro with the IMU sensor block highlighted
The IMU sensor block in bumperbot_gazebo.xacro

What the sensor settings mean

SettingValueWhat it means
always_ontruethe sensor works from the start of the simulation
update_rate100100 readings per second: one reading every 1 ÷ 100 = 0.01 s
gz_frame_idimu_linkeach reading is stamped with the frame of the IMU, so ROS 2 knows where on the robot it was measured
topicimuthe Gazebo topic the readings are sent on
angular_velocity noisemean 0, stddev 2e-4the gyroscope part: every turning speed gets a random error, on average 0, spread 2e-4 = 0.0002 rad/s
linear_acceleration noisemean 0, stddev 1.7e-2the accelerometer part: every acceleration gets a random error, on average 0, spread 1.7e-2 = 0.017 m/s²

The noise is a gaussian (normal) random number, the bell curve of chapter 68: the mean says where the errors are centred and the standard deviation (stddev) says how far they spread. A perfect simulated sensor would hide the problems a real IMU has, so the noise makes the simulation more honest.

Bring the readings into ROS 2

Open gazebo.launch.py

The IMU now sends its readings on a Gazebo topic. ROS 2 nodes cannot read Gazebo topics directly, so the bridge from chapter 25 must carry them over. Open the launch folder and then gazebo.launch.py.

VS Code explorer with gazebo.launch.py selected in the launch folder
gazebo.launch.py in the launch folder

Add the IMU to the bridge

Change the bridge node so it also carries the IMU topic:

gz_ros2_bridge = Node(
    package="ros_gz_bridge",
    executable="parameter_bridge",
    arguments=[
        "/clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock",
        "/imu@sensor_msgs/msg/Imu[gz.msgs.IMU"
    ],
    remappings=[
        ('/imu', '/imu/out'),
    ]
)
gazebo.launch.py with the gz_ros2_bridge node highlighted, carrying /clock and /imu
The bridge node in gazebo.launch.py, now with the /imu topic

Each bridge argument has the same shape: topic @ ROS 2 type [ Gazebo type. As far as I know, the [ means the messages flow one way only, from Gazebo to ROS 2, which is all a sensor needs. The remappings line renames the ROS 2 topic from /imu to /imu/out, so the readings arrive in ROS 2 on /imu/out. The bridge node must also stay in the list returned by LaunchDescription, as in the picture.

Add the dependency to package.xml

The launch file now needs the ros_gz_bridge package, so tell ROS 2 about it. Open package.xml of the same package, bumperbot_description, and add this line next to the other exec_depend lines:

<exec_depend>ros_gz_bridge</exec_depend>
package.xml of bumperbot_description with the ros_gz_bridge exec_depend line marked
The new exec_depend line in package.xml

Build and look at the readings

Terminal 1: build the workspace

Go to the workspace:

cd bumperbot_ws

Build it:

colcon build

What you should see

Starting >>> bumperbot_msgs
Starting >>> bumperbot_controller
Starting >>> bumperbot_cpp_examples
Starting >>> bumperbot_description
Finished <<< bumperbot_cpp_examples [0.37s]
Finished <<< bumperbot_controller [0.78s]
Finished <<< bumperbot_msgs [0.92s]
Starting >>> bumperbot_py_examples
Finished <<< bumperbot_description [1.20s]
Finished <<< bumperbot_py_examples [1.46s]

Summary: 5 packages finished [2.57s]
Terminal: colcon build finishing 5 packages
The workspace builds

Terminal 2: start the simulation

Open a new terminal in the workspace and source it:

. install/setup.bash

Start the robot in Gazebo:

ros2 launch bumperbot_description gazebo.launch.py

What you should see

[gazebo-2] [Dbg] [ImuSensor.cc:150] IMU data for [bumperbot::base_footprint::imu] advertised on [imu]
Gazebo running the BumperBot simulation
The simulation is running; the log line above says the IMU publishes on the Gazebo topic imu

Terminal 3: read the IMU topic

Open a third terminal in the workspace and source it:

. install/setup.bash

Print every message on /imu/out:

ros2 topic echo /imu/out

What you should see

angular_velocity:
  x: -0.0003625079190463711
  y: -0.00019713507617265803
  z: 0.0005380791268850312
angular_velocity_covariance:
- 3.999999975690116e-08
- 0.0
- 0.0
- 0.0
- 3.999999975690116e-08
- 0.0
- 0.0
- 0.0
- 3.999999975690116e-08
linear_acceleration:
  x: 0.12352216982227612
  y: -0.008985411455847058
  z: 9.800843644362653
linear_acceleration_covariance:
- 0.00028899998869746923
- 0.0
- 0.0
- 0.0
- 0.00028899998869746923
- 0.0
- 0.0
- 0.0
- 0.00028899998869746923
---

This is one message from the run in the pictures. New messages keep arriving 100 times per second, and the numbers change every time because of the noise.

Three terminals: the build, the simulation log and ros2 topic echo /imu/out being typed
Terminal 3 starts ros2 topic echo /imu/out
The /imu/out messages scrolling in the terminal: angular_velocity, linear_acceleration and their covariances
The IMU messages arriving on /imu/out

What the numbers say

  • angular_velocity is the gyroscope part, in radians per second. The robot stands still, so x, y and z are only tiny noise around 0.
  • linear_acceleration is the accelerometer part, in metres per second per second. z is about 9.8. As far as I know, this is gravity: a real accelerometer on a table feels the pull of the Earth on its up axis, and the simulated one does the same.
  • The covariance lists come from the noise settings. A covariance is the stddev multiplied by itself:
Readingstddev in the sensor blockstddev × stddevprinted covariance
angular_velocity2e-4 = 0.00020.0002 × 0.0002 = 0.00000004 = 4e-83.999999975690116e-08
linear_acceleration1.7e-2 = 0.0170.017 × 0.017 = 0.0002890.00028899998869746923

The printed values differ only in the last digits, which come from how the computer stores decimal numbers. Each list has nine numbers: a 3 × 3 table for x, y and z. Only the first, fifth and ninth are used, the x, y and z of the noise.

Plot the readings in PlotJuggler

Stop the echo with Ctrl+C. Then start PlotJuggler in the same terminal:

ros2 run plotjuggler plotjuggler
Terminal after stopping the echo, with ros2 run plotjuggler plotjuggler typed at the bottom
Starting PlotJuggler after stopping the echo

In PlotJuggler, as in chapter 71:

  1. Start the ROS 2 Topic Subscriber.
  2. Select the message /imu/out.
  3. Click OK.
  4. In the list on the left, open imu, then out, then angular_velocity, and drag z into the graph.
Notes on PlotJuggler: steps 1 to 4 marked on the Select ROS messages window and the topic tree, with the noisy z graph
The four steps in PlotJuggler, and the angular velocity z in the graph

Drive the robot and watch the IMU

Terminal 4: the controllers

Open a fourth terminal in the workspace and source it:

. install/setup.bash

Start the controllers, with the same option as in chapter 71:

ros2 launch bumperbot_controller controller.launch.py use_simple_controller:=false
Terminal 4 starting controller.launch.py with use_simple_controller:=false
Terminal 4 starts the controllers

Terminal 5: the joystick

Open a fifth terminal in the workspace and source it:

. install/setup.bash

Start the joystick:

ros2 launch bumperbot_controller joystick_teleop.launch.py
Terminal 5 starting joystick_teleop.launch.py while the controllers log in terminal 4
Terminal 5 starts the joystick

Watch the readings

Now drive the robot with the joystick and watch the angular velocity z in PlotJuggler. While the robot stands still, the line only shakes a little around 0: that is the noise of 0.0002 rad/s. When the robot turns, the line jumps up or down, because the gyroscope part of the IMU feels the turn.

Driving with the joystick while PlotJuggler shows the IMU readings

Remember

  • The IMU is a new imu_link with a fixed imu_joint, 0.0699 m above base_link, with the same axes as the robot.
  • Gazebo needs the IMU system plugin: ignition-gazebo-imu-system on Humble, gz-sim-imu-system on Iron and newer.
  • The sensor block sets 100 readings per second on the Gazebo topic imu, with gaussian noise: stddev 0.0002 rad/s for the angular velocity and 0.017 m/s² for the linear acceleration.
  • The bridge argument /imu@sensor_msgs/msg/Imu[gz.msgs.IMU carries the readings into ROS 2, and the remapping renames the topic to /imu/out.
  • ros_gz_bridge goes into package.xml as an exec_depend.
  • In the messages, the covariance is the stddev multiplied by itself (0.0002 × 0.0002 = 4e-8), and the z acceleration of about 9.8 is gravity.
  • Turning the robot shows up in the angular velocity z in PlotJuggler.