Contents1. Install Gazebo and create the ROS package
The fastest way to check a differential-drive robot in ROS 2 is to start with one command and one observation: send a forward velocity and see whether odometry changes. Here, Gazebo Harmonic moves the simulated wheels; ROS 2 Jazzy sends commands and receives odometry through ros_gz_bridge. You will build the model, world, bridge and launch file from the listings below.
Source package: Get the model, world, bridge configuration and launch files from the Gazebo differential-drive source package on GitHub. The example was checked in headless simulation, not on physical hardware.
If topics, messages and overlays are new to you, start with the ROS 2 beginner’s guide to nodes and topics. The official Gazebo and ROS installation matrix pairs Jazzy with Harmonic. These commands assume the Jazzy apt repository is configured on Ubuntu 24.04.
1. Install Gazebo and create the ROS package
sudo apt updatesudo apt install python3-colcon-common-extensions python3-rosdep \ ros-jazzy-ros-base ros-jazzy-ros-gz ros-jazzy-xacro \ ros-jazzy-teleop-twist-keyboardgz sim --versionsource /opt/ros/jazzy/setup.bashmkdir -p ~/rtj_ws/src/rtj_diffbot/{config,launch,urdf,worlds}cd ~/rtj_ws/src/rtj_diffbotSave these two files at the package root. The manifest covers both tutorial variants, including the Python launch APIs and the message packages used by the bridges. Fill in your maintainer details and choose the applicable license before distributing your own package.
package.xml
<?xml version="1.0"?><package format="3"> <name>rtj_diffbot</name> <version>0.1.0</version> <description>Differential-drive robot simulation smoke test</description> <maintainer email="maintainer@example.invalid">Maintainer TBD</maintainer> <license>TODO: choose a license before distributing your package</license> <buildtool_depend>ament_cmake</buildtool_depend> <exec_depend>ament_index_python</exec_depend> <exec_depend>controller_manager</exec_depend> <exec_depend>diff_drive_controller</exec_depend> <exec_depend>gz_ros2_control</exec_depend> <exec_depend>joint_state_broadcaster</exec_depend> <exec_depend>geometry_msgs</exec_depend> <exec_depend>launch</exec_depend> <exec_depend>launch_ros</exec_depend> <exec_depend>nav_msgs</exec_depend> <exec_depend>ros_gz_bridge</exec_depend> <exec_depend>ros_gz_sim</exec_depend> <exec_depend>ros2controlcli</exec_depend> <exec_depend>robot_state_publisher</exec_depend> <exec_depend>rosgraph_msgs</exec_depend> <exec_depend>sensor_msgs</exec_depend> <exec_depend>tf2_msgs</exec_depend> <exec_depend>tf2_tools</exec_depend> <exec_depend>teleop_twist_keyboard</exec_depend> <exec_depend>xacro</exec_depend> <export><build_type>ament_cmake</build_type></export></package>CMakeLists.txt
cmake_minimum_required(VERSION 3.8)project(rtj_diffbot) find_package(ament_cmake REQUIRED) install(DIRECTORY config launch urdf worlds DESTINATION share/${PROJECT_NAME}) ament_package()CMake installs the model, world, configuration and launch directories into the package share directory. Rebuild after editing these source files so the launch sees the installed versions.
worlds/rtj_world.sdf
The world supplies a ground plane and a static obstacle. Its physics and user-command systems let the simulator run and accept a spawn request without a GUI.
<sdf version="1.9"> <world name="rtj_world"> <physics name="1ms" type="ode"> <max_step_size>0.001</max_step_size> <real_time_factor>1.0</real_time_factor> </physics> <plugin filename="gz-sim-physics-system" name="gz::sim::systems::Physics"/> <plugin filename="gz-sim-user-commands-system" name="gz::sim::systems::UserCommands"/> <plugin filename="gz-sim-scene-broadcaster-system" name="gz::sim::systems::SceneBroadcaster"/> <light type="directional" name="sun"> <cast_shadows>true</cast_shadows> <pose>0 0 10 0 0 0</pose> <diffuse>0.9 0.9 0.9 1</diffuse> <specular>0.2 0.2 0.2 1</specular> <direction>-0.5 0.1 -0.9</direction> </light> <model name="ground_plane"> <static>true</static> <link name="link"> <collision name="collision"> <geometry><plane><normal>0 0 1</normal><size>100 100</size></plane></geometry> </collision> <visual name="visual"> <geometry><plane><normal>0 0 1</normal><size>100 100</size></plane></geometry> <material> <ambient>0.8 0.8 0.8 1</ambient> <diffuse>0.8 0.8 0.8 1</diffuse> </material> </visual> </link> </model> <model name="obstacle_box"> <static>true</static> <pose>2 0 0.25 0 0 0</pose> <link name="link"> <collision name="collision"> <geometry><box><size>0.5 0.5 0.5</size></box></geometry> </collision> <visual name="visual"> <geometry><box><size>0.5 0.5 0.5</size></box></geometry> <material> <ambient>0.8 0.3 0.2 1</ambient> <diffuse>0.8 0.3 0.2 1</diffuse> </material> </visual> </link> </model> </world></sdf>2. Define the chassis, wheels and drive system
Save the model as urdf/diffbot.urdf.xacro. The wheel centres are 0.24 m apart and the wheels have a 0.05 m radius. The drive system uses those same dimensions to convert body motion into wheel motion and report odometry. Each simulated body has mass and inertia; the passive caster model has low friction and is positioned to meet the same ground plane as the wheel bottoms. These are example dimensions, not a recommendation for a real chassis.
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="diffbot"> <!-- Dimensions in metres, masses in kg --> <xacro:property name="chassis_x" value="0.30"/> <xacro:property name="chassis_y" value="0.20"/> <xacro:property name="chassis_z" value="0.08"/> <xacro:property name="chassis_mass" value="2.0"/> <xacro:property name="chassis_offset_x" value="0.05"/> <xacro:property name="wheel_radius" value="0.05"/> <xacro:property name="wheel_width" value="0.03"/> <xacro:property name="wheel_mass" value="0.2"/> <xacro:property name="wheel_separation" value="0.24"/> <xacro:property name="wheel_z" value="-0.03"/> <xacro:property name="caster_radius" value="0.025"/> <xacro:property name="caster_mass" value="0.1"/> <xacro:property name="caster_x" value="0.12"/> <material name="blue"><color rgba="0.1 0.3 0.8 1"/></material> <material name="dark"><color rgba="0.1 0.1 0.1 1"/></material> <material name="grey"><color rgba="0.6 0.6 0.6 1"/></material> <xacro:macro name="box_inertia" params="m x y z"> <inertial> <origin xyz="${chassis_offset_x} 0 0" rpy="0 0 0"/> <mass value="${m}"/> <inertia ixx="${m*(y*y+z*z)/12}" ixy="0" ixz="0" iyy="${m*(x*x+z*z)/12}" iyz="0" izz="${m*(x*x+y*y)/12}"/> </inertial> </xacro:macro> <!-- Cylinder whose axis lies along the link's y axis --> <xacro:macro name="wheel_inertia" params="m r h"> <inertial> <mass value="${m}"/> <inertia ixx="${m*(3*r*r+h*h)/12}" ixy="0" ixz="0" iyy="${m*r*r/2}" iyz="0" izz="${m*(3*r*r+h*h)/12}"/> </inertial> </xacro:macro> <xacro:macro name="sphere_inertia" params="m r"> <inertial> <mass value="${m}"/> <inertia ixx="${2*m*r*r/5}" ixy="0" ixz="0" iyy="${2*m*r*r/5}" iyz="0" izz="${2*m*r*r/5}"/> </inertial> </xacro:macro> <link name="base_link"> <visual> <origin xyz="${chassis_offset_x} 0 0" rpy="0 0 0"/> <geometry><box size="${chassis_x} ${chassis_y} ${chassis_z}"/></geometry> <material name="blue"/> </visual> <collision> <origin xyz="${chassis_offset_x} 0 0" rpy="0 0 0"/> <geometry><box size="${chassis_x} ${chassis_y} ${chassis_z}"/></geometry> </collision> <xacro:box_inertia m="${chassis_mass}" x="${chassis_x}" y="${chassis_y}" z="${chassis_z}"/> </link> <xacro:macro name="wheel" params="name side"> <link name="${name}_wheel"> <visual> <origin xyz="0 0 0" rpy="${pi/2} 0 0"/> <geometry><cylinder radius="${wheel_radius}" length="${wheel_width}"/></geometry> <material name="dark"/> </visual> <collision> <origin xyz="0 0 0" rpy="${pi/2} 0 0"/> <geometry><cylinder radius="${wheel_radius}" length="${wheel_width}"/></geometry> </collision> <xacro:wheel_inertia m="${wheel_mass}" r="${wheel_radius}" h="${wheel_width}"/> </link> <joint name="${name}_wheel_joint" type="continuous"> <parent link="base_link"/> <child link="${name}_wheel"/> <origin xyz="0 ${side*wheel_separation/2} ${wheel_z}" rpy="0 0 0"/> <axis xyz="0 1 0"/> </joint> </xacro:macro> <xacro:wheel name="left" side="1"/> <xacro:wheel name="right" side="-1"/> <link name="caster_link"> <visual> <geometry><sphere radius="${caster_radius}"/></geometry> <material name="grey"/> </visual> <collision> <geometry><sphere radius="${caster_radius}"/></geometry> </collision> <xacro:sphere_inertia m="${caster_mass}" r="${caster_radius}"/> </link> <joint name="caster_joint" type="fixed"> <parent link="base_link"/> <child link="caster_link"/> <origin xyz="${caster_x} 0 ${wheel_z - wheel_radius + caster_radius}" rpy="0 0 0"/> </joint> <!-- Near-frictionless caster so it does not fight the drive wheels --> <gazebo reference="caster_link"> <mu1>0.001</mu1> <mu2>0.001</mu2> </gazebo> <gazebo> <plugin filename="gz-sim-diff-drive-system" name="gz::sim::systems::DiffDrive"> <left_joint>left_wheel_joint</left_joint> <right_joint>right_wheel_joint</right_joint> <wheel_separation>${wheel_separation}</wheel_separation> <wheel_radius>${wheel_radius}</wheel_radius> <topic>cmd_vel</topic> <odom_topic>odom</odom_topic> <tf_topic>tf</tf_topic> <frame_id>odom</frame_id> <child_frame_id>base_link</child_frame_id> <odom_publish_frequency>50</odom_publish_frequency> <max_linear_velocity>1.0</max_linear_velocity> <min_linear_velocity>-1.0</min_linear_velocity> <max_angular_velocity>2.0</max_angular_velocity> <min_angular_velocity>-2.0</min_angular_velocity> <max_linear_acceleration>1.0</max_linear_acceleration> <min_linear_acceleration>-1.0</min_linear_acceleration> <max_angular_acceleration>2.0</max_angular_acceleration> <min_angular_acceleration>-2.0</min_angular_acceleration> </plugin> <plugin filename="gz-sim-joint-state-publisher-system" name="gz::sim::systems::JointStatePublisher"> <topic>joint_states</topic> </plugin> </gazebo> </robot>3. Bridge commands and telemetry
Save the mapping as config/bridge.yaml. The command travels from ROS to Gazebo. Clock, odometry, transforms and wheel joint states travel the other way. The Jazzy ros_gz_bridge message mapping reference lists the supported message pairs.
/clock:gz.msgs.Clock→rosgraph_msgs/msg/Clock/cmd_vel:geometry_msgs/msg/Twist→gz.msgs.Twist/odom:gz.msgs.Odometry→nav_msgs/msg/Odometry/tf:gz.msgs.Pose_V→tf2_msgs/msg/TFMessage/joint_states:gz.msgs.Model→sensor_msgs/msg/JointState
- ros_topic_name: "clock" gz_topic_name: "clock" ros_type_name: "rosgraph_msgs/msg/Clock" gz_type_name: "gz.msgs.Clock" direction: GZ_TO_ROS - ros_topic_name: "cmd_vel" gz_topic_name: "cmd_vel" ros_type_name: "geometry_msgs/msg/Twist" gz_type_name: "gz.msgs.Twist" direction: ROS_TO_GZ - ros_topic_name: "odom" gz_topic_name: "odom" ros_type_name: "nav_msgs/msg/Odometry" gz_type_name: "gz.msgs.Odometry" direction: GZ_TO_ROS - ros_topic_name: "tf" gz_topic_name: "tf" ros_type_name: "tf2_msgs/msg/TFMessage" gz_type_name: "gz.msgs.Pose_V" direction: GZ_TO_ROS - ros_topic_name: "joint_states" gz_topic_name: "joint_states" ros_type_name: "sensor_msgs/msg/JointState" gz_type_name: "gz.msgs.Model" direction: GZ_TO_ROS4. Launch the headless simulation
Save as launch/sim.launch.py. The launch starts Gazebo with -r -s (running, server only), starts robot state publisher with the expanded Xacro, requests creation from the robot_description topic, and starts the bridge. This list starts the processes; it is not a guarantee that every process is ready before the next starts. Check the create process output for a successful spawn.
import os from ament_index_python.packages import get_package_share_directoryfrom launch import LaunchDescriptionfrom launch.actions import IncludeLaunchDescriptionfrom launch.launch_description_sources import PythonLaunchDescriptionSourcefrom launch.substitutions import Commandfrom launch_ros.actions import Nodefrom launch_ros.parameter_descriptions import ParameterValue def generate_launch_description(): pkg_share = get_package_share_directory('rtj_diffbot') ros_gz_sim_share = get_package_share_directory('ros_gz_sim') xacro_file = os.path.join(pkg_share, 'urdf', 'diffbot.urdf.xacro') world_file = os.path.join(pkg_share, 'worlds', 'rtj_world.sdf') bridge_file = os.path.join(pkg_share, 'config', 'bridge.yaml') robot_description = ParameterValue( Command(['xacro ', xacro_file]), value_type=str) # Start Gazebo Sim with our world, running immediately (-r) gz_sim = IncludeLaunchDescription( PythonLaunchDescriptionSource( os.path.join(ros_gz_sim_share, 'launch', 'gz_sim.launch.py')), launch_arguments={ 'gz_args': f'-r -s {world_file}', 'on_exit_shutdown': 'true', }.items()) # Publishes the robot_description topic and TF for every non-moving link robot_state_publisher = Node( package='robot_state_publisher', executable='robot_state_publisher', parameters=[{ 'robot_description': robot_description, 'use_sim_time': True, }], output='screen') # Spawns the robot into the running simulation from /robot_description spawn_robot = Node( package='ros_gz_sim', executable='create', arguments=['-topic', 'robot_description', '-name', 'diffbot', '-z', '0.15'], output='screen') # Bridges Gazebo topics to ROS 2 topics using the YAML file bridge = Node( package='ros_gz_bridge', executable='parameter_bridge', parameters=[{'config_file': bridge_file}], output='screen') return LaunchDescription([ gz_sim, robot_state_publisher, spawn_robot, bridge, ])On a fresh setup, run sudo rosdep init once if rosdep has not been initialized. Then resolve dependencies, build and source the overlay:
cd ~/rtj_wssource /opt/ros/jazzy/setup.bashrosdep updaterosdep install --from-paths src --ignore-src --rosdistro jazzy -r -yrosdep check --from-paths src --ignore-src --rosdistro jazzycolcon build --packages-select rtj_diffbotsource install/setup.bashros2 launch rtj_diffbot sim.launch.py5. Drive and check the result
Open another terminal. With the base initially still, inspect the topics and record a baseline odometry sample. Publish a forward command, then a zero command to stop. Compare the pose in the two samples; receiving a message alone does not prove the robot moved.
source /opt/ros/jazzy/setup.bashsource ~/rtj_ws/install/setup.bashros2 topic list -tros2 topic echo --once /odomros2 topic pub --once /cmd_vel geometry_msgs/msg/Twist '{linear: {x: 0.2}}'ros2 topic echo --once /odomros2 topic pub --once /cmd_vel geometry_msgs/msg/Twist '{}'ros2 run tf2_tools view_framesFor interactive input in a sourced terminal, run ros2 run teleop_twist_keyboard teleop_twist_keyboard. For this launch keep the default unstamped Twist type. Send a stop command when you finish rather than assuming that closing a publisher stops the simulator.
The recorded review reports that colcon and rosdep checks passed, the robot spawned, the configured topics appeared with the expected types, and a velocity command produced nonzero odometry. That is a headless integration check, not a measurement of real traction, accuracy or motor performance.
Troubleshoot before changing the model
- No robot: inspect the Xacro and create-process output. Check that the workspace overlay is sourced and the world started.
- No odometry: compare
gz topic -lwithros2 topic list -t. If Gazebo publishes odometry but ROS does not receive it, check the bridge file, type and direction. - Wrong speed or turning: check left/right joint names, joint axes, wheel radius and wheel separation before tuning anything else.
- Stale TF: check that
/clockis advancing and that nodes consuming simulation data use simulation time.
When the command and odometry path is working, the ROS 2 Nav2 costmap configuration guide explains the next layer for navigation. This world does not include a range sensor or a Nav2 setup.


