Summary
In this chapter, you worked on ROS 2 topics.
Topics allow nodes to communicate with each other using a publish/subscribe mechanism. Topics are made for unidirectional data streams and are anonymous.
You can write topic publishers and subscribers directly in your nodes by using rclpy
for Python and rclcpp
for C++.
To write a publisher, you must do the following:
- First, check what topic name and interface you must send. Import the interface into the code and create a publisher in the node’s constructor.
- To publish, you must create a message, fill in the different fields, and publish the message with your publisher.
You can potentially publish a message from anywhere in the code. A common structure is to add a timer and publish from the timer callback. If it makes sense, you can also publish from a subscriber callback directly.
To write a subscriber, you must do the following:
- As for the publisher, you need to know what name and interface...