CodeTheRobot

ROS 2 Lyrical Luth · Lesson 23 of 86

ros2_controllers: diff drive and joint trajectory

Configuring diff_drive_controller and joint_trajectory_controller.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain ros2_controllers: diff drive and joint trajectory 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

ros2_controllers is a library of ready-made controllers. diff_drive_controller turns a Twist on cmd_vel into left and right wheel velocities and publishes odometry. joint_trajectory_controller follows a list of timed joint positions, which is how arms are moved.

You set them up in YAML: joint names, wheel separation and radius for the diff drive; command interfaces (position) and joint limits for a trajectory controller. Send a trajectory with the follow_joint_trajectory action or the joint_trajectory topic.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the ros2_controllers: diff drive and joint trajectory 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

diff_drive_controller:
# Controller name.
  ros__parameters:
# ROS parameters.
    left_wheel_names: ["left_joint"]
# Joint that drives the left wheel.
    right_wheel_names: ["right_joint"]
# Joint that drives the right wheel.
    wheel_separation: 0.30
# Distance between wheels in metres.
    wheel_radius: 0.05
# Wheel radius in metres.
    use_stamped_vel: false
# Accept plain Twist messages on cmd_vel.
# ros2 topic pub -r 10 /diff_drive_controller/cmd_vel geometry_msgs/msg/Twist '{linear: {x: 0.2}}'
# Drive forward at 0.2 m/s.

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 ros2_controllers: diff drive and joint trajectory. Expected result: The wheels turn when a Twist is published

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.

# config/diff_drive.yaml
diff_drive_controller:
  ros__parameters:
    left_wheel_names: ["left_joint"]
    right_wheel_names: ["right_joint"]
# TODO: set wheel_separation to 0.30 and wheel_radius to 0.05

Expected output

The wheels turn when a Twist is published
Stuck? Show the full solution (try it yourself first)
# config/diff_drive.yaml
diff_drive_controller:
  ros__parameters:
    left_wheel_names: ["left_joint"]
    right_wheel_names: ["right_joint"]
    wheel_separation: 0.30
    wheel_radius: 0.05

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

Configure a diff_drive_controller for your two-wheel URDF and a joint_trajectory_controller for your arm, drive the base with a Twist, and send one trajectory point to the arm with ros2 topic pub.

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.