C++ Advanced · Lesson 19 of 23
Gazebo simulation
Spawning a URDF in Gazebo, bridging topics and controlling a simulated robot.
Estimated time · 90–120 min
Learning goals
What you will understand
- Explain gazebo simulation in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
Gazebo is a physics simulator: it loads a world, spawns your robot from its URDF/SDF and simulates gravity, contacts and sensors. The ros_gz bridge connects Gazebo topics to ROS 2 topics, so your nodes talk to a simulated robot exactly as they will to a real one.
Use simulated time (use_sim_time:=true) so that ROS 2 nodes follow Gazebo's clock. Simulation is safe for testing controllers, but verify on hardware carefully because friction, latency and sensor noise are never perfect.
Guided lesson
Work through these steps
- 1.
Read the gazebo simulation example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
ros2 launch ros_gz_sim gz_sim.launch.py gz_args:='-r empty.sdf'
<!-- Start Gazebo with an empty world, running immediately (-r). -->
ros2 run ros_gz_sim create -name mini_arm -topic robot_description
<!-- Spawn the robot from the robot_description topic. -->
ros2 run ros_gz_bridge parameter_bridge /cmd_vel@geometry_msgs/msg/Twist]gz.msgs.Twist
<!-- Bridge the ROS 2 /cmd_vel topic into Gazebo. -->
ros2 topic pub -r 10 /cmd_vel geometry_msgs/msg/Twist '{linear: {x: 0.2}}'
<!-- Drive forward at 0.2 m/s, publishing at 10 Hz. -->
ros2 param set /my_node use_sim_time true
<!-- Make a node follow simulated time. -->What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
Watch it run · Mobile robot
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.
Demo for Gazebo simulation. Expected result: ros2 topic echo /odom -> the pose x grows by about 0.2 m every second
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
# Run each line in its own sourced terminal
ros2 launch ros_gz_sim gz_sim.launch.py gz_args:='-r empty.sdf'
# TODO: bridge /cmd_vel into Gazebo, then publish a forward velocity of 0.2 m/s at 10 HzExpected output
ros2 topic echo /odom -> the pose x grows by about 0.2 m every secondStuck? Show the full solution (try it yourself first)
# Run each line in its own sourced terminal
ros2 launch ros_gz_sim gz_sim.launch.py gz_args:='-r empty.sdf'
ros2 run ros_gz_bridge parameter_bridge /cmd_vel@geometry_msgs/msg/Twist]gz.msgs.Twist
ros2 topic pub -r 10 /cmd_vel geometry_msgs/msg/Twist '{linear: {x: 0.2}}'Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Spawn a differential-drive robot in Gazebo, bridge /cmd_vel and /odom, and write a C++ node that drives 1 metre forward and stops using the odometry. Record the actual distance and the overshoot.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.