Course: ROS2 Fundamentals
Build System: ament_cmake
The goal of this assignment is to verify your understanding of:
- How to create a ROS2 Publisher node.
- How to publish messages using
std_msgs/msg/String. - How to configure topics and publish rates.
You must complete the provided skeleton code to create a ROS2 node that publishes a counter message to a topic.
-
Source Code (
src/publisher_node.cpp):- Implement a class
PublisherNodethat inherits fromrclcpp::Node. - Initialize the node with the name
"publisher_node". - Create a publisher to the topic
"/counter"with typestd_msgs::msg::String. - Create a timer that fires every 500ms.
- Inside the timer callback:
- Increment a counter (starting from 0).
- Publish a message in the format:
"Count: X"where X is the counter value. - Log the published message using
RCLCPP_INFO.
- Implement a class
-
Build Configuration (
CMakeLists.txt):- Add an executable target named
publisher_nodecompiled fromsrc/publisher_node.cpp. - Link dependencies for
rclcppandstd_msgs. - Ensure the executable is installed to
lib/${PROJECT_NAME}.
- Add an executable target named
-
Package Metadata (
package.xml):- Add the missing dependency tags for
rclcppandstd_msgs.
- Add the missing dependency tags for
Before pushing to GitHub, ensure your code runs locally:
# 1. Build the package
colcon build --packages-select ros2_publisher
# 2. Source the setup file
source install/setup.bash
# 3. Run the node
ros2 run ros2_publisher publisher_node[INFO] [1700000000.123456789] [publisher_node]: Publishing: 'Count: 0'
[INFO] [1700000000.623456789] [publisher_node]: Publishing: 'Count: 1'
[INFO] [1700000001.123456789] [publisher_node]: Publishing: 'Count: 2'
...# In another terminal
ros2 topic echo /counterExpected:
data: 'Count: 0'
---
data: 'Count: 1'
---
...