ROS 2 Lyrical Luth · Lesson 44 of 86
ros2_control hardware interfaces
Writing a SystemInterface: on_init, export interfaces, read and write.
Estimated time · 90–120 min
Learning goals
What you will understand
- Explain ros2_control hardware interfaces 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 ros2_control hardware interface is a C++ plugin that talks to the real motors and sensors. Derive from hardware_interface::SystemInterface (for a whole robot), and implement on_init (read the URDF <ros2_control> parameters), export_state_interfaces, export_command_interfaces, read (hardware → state) and write (command → hardware).
The controller manager calls read() and write() every cycle in a real-time loop, so they must not allocate memory, block or log heavily. Register the class with PLUGINLIB_EXPORT_CLASS and describe it in the URDF with <ros2_control type="system">. Start with a mock that echoes commands back as states.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the ros2_control hardware interfaces example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 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
class MockArm : public hardware_interface::SystemInterface {
// A hardware plugin for a simulated arm.
public:
// The functions the controller manager calls.
std::vector<hardware_interface::StateInterface> export_state_interfaces() override {
// Tell ROS which values the hardware can report.
return {hardware_interface::StateInterface("shoulder", "position", &pos_)};
// The shoulder's position is readable from pos_.
}
// End of export_state_interfaces.
std::vector<hardware_interface::CommandInterface> export_command_interfaces() override {
// Tell ROS which values controllers can command.
return {hardware_interface::CommandInterface("shoulder", "position", &cmd_)};
// A controller writes the target into cmd_.
}
// End of export_command_interfaces.
hardware_interface::return_type write(const rclcpp::Time&, const rclcpp::Duration&) override { pos_ = cmd_; return hardware_interface::return_type::OK; }
// A mock: pretend the joint reaches its command at once.
private:
// State and command storage.
double pos_{0.0}, cmd_{0.0};
// Current position and commanded position.
};
// End of the class.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 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.
Demo for ros2_control hardware interfaces. Expected result: ros2 control list_hardware_interfaces shows shoulder/position as command and state
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.
<ros2_control name="mock_arm" type="system">
<hardware>
<!-- TODO: use the plugin mock_components/GenericSystem -->
</hardware>
<joint name="shoulder">
<command_interface name="position"/>
<state_interface name="position"/>
</joint>
</ros2_control>Expected output
ros2 control list_hardware_interfaces shows shoulder/position as command and stateStuck? Show the full solution (try it yourself first)
<ros2_control name="mock_arm" type="system">
<hardware>
<plugin>mock_components/GenericSystem</plugin>
</hardware>
<joint name="shoulder">
<command_interface name="position"/>
<state_interface name="position"/>
</joint>
</ros2_control>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
Write the MockArm plugin for a two-joint arm, register it with pluginlib, describe it in a <ros2_control> URDF block, load joint_state_broadcaster and a position controller, and command a joint from the CLI.
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
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.