CodeTheRobot

ROS 2 Lyrical Luth · Lesson 45 of 86

Joint trajectory controllers

Trajectory messages, the follow_joint_trajectory action, limits and tolerances.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain joint trajectory controllers 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

A joint_trajectory_controller moves the joints through a list of waypoints, each with positions (and optional velocities) at a time from start. It interpolates between waypoints and checks goal and path tolerances. It accepts a trajectory on a topic or, better, as a goal to the follow_joint_trajectory action, which reports success or failure.

Set command_interfaces (position), state_interfaces (position, velocity) and constraints (goal_time, per-joint tolerance) in YAML. Plan trajectories so velocity and acceleration stay inside the joint limits, otherwise the controller aborts the goal.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the joint trajectory controllers 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

arm_controller:
# Controller name.
  ros__parameters:
# ROS parameters.
    joints: [shoulder, elbow]
# Joints this controller drives.
    command_interfaces: [position]
# It commands positions.
    state_interfaces: [position, velocity]
# It reads positions and velocities.
    constraints:
# Tolerances for success.
      goal_time: 0.5
# Allow 0.5 s after the last point to settle.
# ros2 topic pub --once /arm_controller/joint_trajectory trajectory_msgs/msg/JointTrajectory "{joint_names: [shoulder, elbow], points: [{positions: [0.5, -0.5], time_from_start: {sec: 2}}]}"
# Send one waypoint that takes 2 seconds.

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 · Robot arm

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.

tip x110 y58ROBOT ARM

Demo for Joint trajectory controllers. Expected result: The arm moves to 0.5 rad and -0.5 rad in 2 seconds

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.

# TODO: publish one trajectory point: positions 0.5 and -0.5 for shoulder and elbow, reached 2 seconds from start

Expected output

The arm moves to 0.5 rad and -0.5 rad in 2 seconds
Stuck? Show the full solution (try it yourself first)
ros2 topic pub --once /arm_controller/joint_trajectory trajectory_msgs/msg/JointTrajectory "{joint_names: [shoulder, elbow], points: [{positions: [0.5, -0.5], time_from_start: {sec: 2}}]}"

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 joint_trajectory_controller for a two-joint arm, send a three-point trajectory from the CLI, then send a point that violates a limit and read the controller's error.

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.