CodeTheRobot

ROS 2 Lyrical Luth · Lesson 22 of 86

ros2_control basics

Controller manager, hardware interfaces, command and state interfaces.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain ros2_control basics 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_control separates 'what to do' from 'how the hardware works'. A hardware interface talks to motors and sensors and exposes command interfaces (position, velocity, effort) and state interfaces. Controllers use those interfaces without knowing the hardware. The controller manager loads, configures and runs them in a real-time loop.

Controllers are configured in a YAML file and loaded with spawner. You can list them with ros2 control list_controllers and list_hardware_interfaces. The same controller works on a mock, a simulator and a real robot by changing only the hardware plugin.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the ros2_control basics 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

controller_manager:
# Top-level YAML section for the controller manager node.
  ros__parameters:
# ROS parameters follow.
    update_rate: 100
# Run the control loop at 100 Hz.
    joint_state_broadcaster:
# Configure a controller instance by name.
      type: joint_state_broadcaster/JointStateBroadcaster
# It publishes /joint_states from the hardware state.
# ros2 run controller_manager spawner joint_state_broadcaster
# Load and start the broadcaster.
# ros2 control list_controllers
# Check it is active.
# ros2 control list_hardware_interfaces
# Show available command and state interfaces.

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 ros2_control basics. Expected result: joint_state_broadcaster ... active

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/controllers.yaml
controller_manager:
  ros__parameters:
# TODO: run the control loop at 100 Hz and define joint_state_broadcaster of type joint_state_broadcaster/JointStateBroadcaster

Expected output

joint_state_broadcaster ... active
Stuck? Show the full solution (try it yourself first)
# config/controllers.yaml
controller_manager:
  ros__parameters:
    update_rate: 100
    joint_state_broadcaster:
      type: joint_state_broadcaster/JointStateBroadcaster

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

Use the mock_components/GenericSystem hardware plugin with your two-joint URDF, load joint_state_broadcaster, and confirm with ros2 control list_controllers and list_hardware_interfaces.

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.