CodeTheRobot

Ubuntu Linux for Robotics · Lesson 63 of 96

Simulated differential drive

Driving the simulated robot with cmd_vel and checking odometry.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain simulated differential drive in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Gazebo's DiffDrive plugin (or the ros2_control diff_drive_controller) turns a Twist on /cmd_vel into wheel motion and publishes odometry. Bridge /cmd_vel from ROS 2 into Gazebo (the ] direction) and /odom back to ROS 2. Wheel separation and radius in the plugin must match the URDF.

Test from the terminal with ros2 topic pub -r 10 /cmd_vel and read /odom with ros2 topic echo. Check that the distance in the odometry matches the distance the robot really moved in the simulator. Use teleop_twist_keyboard for manual driving.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the simulated differential drive commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

ros2 run ros_gz_bridge parameter_bridge /cmd_vel@geometry_msgs/msg/Twist]gz.msgs.Twist
# Send Twist messages from ROS 2 into Gazebo.
ros2 run ros_gz_bridge parameter_bridge /odom@nav_msgs/msg/Odometry[gz.msgs.Odometry
# Bring the odometry back.
ros2 topic pub -r 10 /cmd_vel geometry_msgs/msg/Twist '{linear: {x: 0.2}}'
# Drive forward at 0.2 m/s.
ros2 topic echo /odom --field pose.pose.position
# Watch the position change.
ros2 run teleop_twist_keyboard teleop_twist_keyboard
# Drive with the keyboard.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

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 Simulated differential drive. Expected result: The robot drives forward and /odom position x increases

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: publish a forward velocity of 0.2 m/s on /cmd_vel at 10 Hz

Expected output

The robot drives forward and /odom position x increases
Stuck? Show the full solution (try it yourself first)
ros2 topic pub -r 10 /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 0.2}}"

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Drive the robot forward for 5 seconds at 0.2 m/s, compare the odometry distance with the simulator's true distance, and explain any difference.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.