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
| File | What is added |
bumperbot_description/urdf/bumperbot.urdf.xacro | the imu_link and the fixed imu_joint |
bumperbot_description/urdf/bumperbot_gazebo.xacro | the IMU system plugin and the IMU sensor with its noise |
bumperbot_description/launch/gazebo.launch.py | the /imu topic in the bridge, renamed to /imu/out |
bumperbot_description/package.xml | the 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.

Add the imu_link and the imu_joint
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>

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.
| Part | What 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. |
Add the IMU sensor in Gazebo
Open bumperbot_gazebo.xacro
Open bumperbot_gazebo.xacro in the same 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>

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>

What the sensor settings mean
| Setting | Value | What it means |
always_on | true | the sensor works from the start of the simulation |
update_rate | 100 | 100 readings per second: one reading every 1 ÷ 100 = 0.01 s |
gz_frame_id | imu_link | each reading is stamped with the frame of the IMU, so ROS 2 knows where on the robot it was measured |
topic | imu | the Gazebo topic the readings are sent on |
angular_velocity noise | mean 0, stddev 2e-4 | the gyroscope part: every turning speed gets a random error, on average 0, spread 2e-4 = 0.0002 rad/s |
linear_acceleration noise | mean 0, stddev 1.7e-2 | the 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.

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'),
]
)

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>

Build and look at the readings
Terminal 1: build the workspace
Go to the workspace:
cd bumperbot_wsBuild it:
colcon buildWhat 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 2: start the simulation
Open a new terminal in the workspace and source it:
. install/setup.bashStart the robot in Gazebo:
ros2 launch bumperbot_description gazebo.launch.pyWhat you should see
[gazebo-2] [Dbg] [ImuSensor.cc:150] IMU data for [bumperbot::base_footprint::imu] advertised on [imu]
Terminal 3: read the IMU topic
Open a third terminal in the workspace and source it:
. install/setup.bashPrint every message on /imu/out:
ros2 topic echo /imu/outWhat 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.


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:
| Reading | stddev in the sensor block | stddev × stddev | printed covariance |
| angular_velocity | 2e-4 = 0.0002 | 0.0002 × 0.0002 = 0.00000004 = 4e-8 | 3.999999975690116e-08 |
| linear_acceleration | 1.7e-2 = 0.017 | 0.017 × 0.017 = 0.000289 | 0.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
In PlotJuggler, as in chapter 71:
- Start the ROS 2 Topic Subscriber.
- Select the message
/imu/out. - Click OK.
- In the list on the left, open imu, then out, then angular_velocity, and drag z into 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.bashStart the controllers, with the same option as in chapter 71:
ros2 launch bumperbot_controller controller.launch.py use_simple_controller:=false
Terminal 5: the joystick
Open a fifth terminal in the workspace and source it:
. install/setup.bashStart the joystick:
ros2 launch bumperbot_controller joystick_teleop.launch.py
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.
Remember
- The IMU is a new
imu_linkwith a fixedimu_joint, 0.0699 m abovebase_link, with the same axes as the robot. - Gazebo needs the IMU system plugin:
ignition-gazebo-imu-systemon Humble,gz-sim-imu-systemon 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.IMUcarries the readings into ROS 2, and the remapping renames the topic to/imu/out. ros_gz_bridgegoes intopackage.xmlas anexec_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.