CodeTheRobot

ROS 2 Lyrical Luth · Lesson 28 of 86

Spawning robots in Gazebo

ros_gz_sim create, spawning from robot_description and launch files.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain spawning robots in gazebo 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

To put a robot into a running world you spawn it: ros_gz_sim's create tool reads a description from a file or from the robot_description topic and asks Gazebo to instantiate it with a name and a pose. Do this from a launch file so world, robot_state_publisher and spawner start together.

Use simulated time (use_sim_time: true) for every node so ROS follows Gazebo's clock, which is bridged with ros_gz_bridge. If the robot falls through the floor or explodes, check inertias, collision geometry and the spawn height.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the spawning robots in gazebo example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

ros2 launch ros_gz_sim gz_sim.launch.py gz_args:='-r empty.sdf'
# Start an empty world, already running.
ros2 run ros_gz_sim create -name mini_bot -topic robot_description -z 0.1
# Spawn from the robot_description topic, 10 cm above the ground.
ros2 run ros_gz_bridge parameter_bridge /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock
# Bridge Gazebo's clock to ROS 2.
ros2 param set /robot_state_publisher use_sim_time true
# Make ROS follow simulated time.
gz model --list
# List models inside the simulator to confirm the spawn.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

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.

goalMOBILE ROBOT

Demo for Spawning robots in Gazebo. Expected result: The model mini_bot appears in Gazebo

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.

# Start the world first, then run:
# TODO: spawn a model named mini_bot from the robot_description topic at z = 0.1

Expected output

The model mini_bot appears in Gazebo
Stuck? Show the full solution (try it yourself first)
# Start the world first, then run:
ros2 run ros_gz_sim create -name mini_bot -topic robot_description -z 0.1

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Write a launch file that starts Gazebo, robot_state_publisher, spawns your robot 0.1 m above the ground, and bridges /clock. Confirm the robot appears and rests on the floor.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.