Lecture Exercises¶
This page contains five exercises that reinforce the concepts introduced in Lecture 10: ROS 2 (Introduction).
Each exercise is designed to be small but concrete, so that you can practice both the conceptual and hands-on aspects of ROS 2.
Exercise 1 – Conceptual ROS 2 Graph (Nodes, Topics, Messages)¶
Goal¶
Reinforce your understanding of the ROS 2 computation graph: nodes, topics, and messages.
Task¶
Consider a simple mobile robot with the following functionality:
A node that reads wheel encoders and publishes odometry.
A node that receives velocity commands and sends low-level motor commands.
A node that runs a teleoperation interface (keyboard or joystick).
A node that estimates the robot’s pose from odometry and IMU.
On paper or using a diagramming tool, draw the ROS 2 graph for this system:
Show each node as a box.
Show each topic as a named arrow between nodes.
Label message types when possible (for example,
nav_msgs/msg/Odometry,geometry_msgs/msg/Twist).
Identify for each topic:
Which node(s) publish.
Which node(s) subscribe.
Whether the data flow is “sensor-like” (one-way streaming) or “command-like” (actuation).
Deliverables¶
A one-page diagram of your ROS 2 graph (PDF, PNG, or clear photo).
A short paragraph (4–6 sentences) explaining why ROS 2’s modular design is helpful for this system (for example, easier debugging, swapping nodes, simulation vs. real robot).
Exercise 2 – Create and Build a ROS 2 Workspace¶
Goal¶
Create a ROS 2 workspace from scratch, add a minimal C++ package, and
build it with colcon.
Task¶
Create a workspace:
mkdir -p ~/enpm702_ws/src cd ~/enpm702_ws
Source your ROS 2 installation (adjust to your distribution):
source /opt/ros/humble/setup.bash
In the
srcdirectory, create a new package namedenpm702_l10_demo:cd ~/enpm702_ws/src ros2 pkg create enpm702_l10_demo --build-type ament_cmake --dependencies rclcpp
Return to the workspace root and build:
cd ~/enpm702_ws colcon build
After a successful build, source the workspace:
source install/setup.bash
Use
ros2 pkg listto confirm that your package is visible.
Deliverables¶
A short text file (for example,
exercise2_summary.txt) that includes:The exact commands you used.
The output of:
ros2 pkg list | grep enpm702_l10_demo
A brief note (2–3 sentences) describing what “overlaying” the workspace means and why you must source
install/setup.bash.
Exercise 3 – Minimal C++ Node with Logging¶
Goal¶
Write, build, and run a minimal C++ ROS 2 node using rclcpp and
verify that spinning works.
Task¶
In the package
enpm702_l10_demo, create a source filesrc/minimal_node.cppthat:Initializes ROS 2.
Creates a node named
"minimal_node".Logs a message at startup.
Spins until you stop it.
Example structure:
#include "rclcpp/rclcpp.hpp" int main(int argc, char * argv[]) { rclcpp::init(argc, argv); auto node = std::make_shared<rclcpp::Node>("minimal_node"); RCLCPP_INFO(node->get_logger(), "Minimal node has started."); rclcpp::spin(node); rclcpp::shutdown(); return 0; }
Update
CMakeLists.txtto:Add an executable target called
minimal_node.Link it with
rclcpp.Install the target to
lib/${PROJECT_NAME}.
Rebuild your workspace and source it.
Run the node:
ros2 run enpm702_l10_demo minimal_node
In a separate terminal, list running nodes:
ros2 node list
Confirm that
/minimal_nodeappears.
Deliverables¶
The
minimal_node.cppfile.The relevant snippet from
CMakeLists.txtthat shows how you added and installed the executable.A brief note (2–3 sentences) explaining what happens if you omit
rclcpp::spin(node);frommain.
Exercise 4 – String Publisher and Subscriber Pair¶
Goal¶
Create a pair of nodes: a publisher that periodically publishes a string message, and a subscriber that prints whatever it receives.
Task¶
In
enpm702_l10_demo, add two C++ source files:src/minimal_publisher.cppsrc/minimal_subscriber.cpp
Implement the publisher:
Node name:
"minimal_publisher".Topic name:
"chatter".Message type:
std_msgs/msg/String.Use a timer to publish a message every 0.5 seconds, for example
"Hello from ENPM702 L10".
Implement the subscriber:
Node name:
"minimal_subscriber".Topic name:
"chatter".Message type:
std_msgs/msg/String.In the callback, print the received string using
RCLCPP_INFO.
Update
CMakeLists.txtto:Add executables for both nodes.
Link them with
rclcppandstd_msgs.Install the executables.
Rebuild the workspace and source it.
Run the nodes in two terminals:
# Terminal 1 ros2 run enpm702_l10_demo minimal_publisher # Terminal 2 ros2 run enpm702_l10_demo minimal_subscriber
Use ROS 2 CLI tools to inspect the system:
ros2 node listros2 topic listros2 topic info /chatter
Deliverables¶
The two C++ source files for publisher and subscriber.
The updated
CMakeLists.txtsnippet.A short text note:
Describe what happens if the subscriber uses topic name
"chatter2"instead of"chatter".Describe what happens if you change the message type in the subscriber to
geometry_msgs/msg/Twistwhile leaving the publisher unchanged.
Exercise 5 – Debugging and Introspection Scenario¶
Goal¶
Practice using ROS 2 introspection tools to debug a small system.
Scenario¶
Assume the following:
You start your publisher node (from Exercise 4:
minimal_publisher).You start your subscriber node (from Exercise 4:
minimal_subscriber).The subscriber prints nothing, and you see no log output.
Task¶
Using only ROS 2 CLI tools (do not modify the code at this stage), list and record the outputs of:
ros2 node listros2 topic listros2 topic info /chatter
Consider the following possible causes and explain how you would detect each one:
The publisher and subscriber are using different topic names.
The publisher and subscriber are using different message types.
One of the nodes did not start or crashed immediately.
You forgot to source the workspace after building.
Propose a systematic checklist (4–6 steps) that you would follow whenever “I see no messages on my subscriber” happens in ROS 2.
Deliverables¶
A short report (about half a page) that includes:
The CLI commands you would run and what information you expect.
A numbered checklist for debugging publisher–subscriber issues.
A final paragraph connecting this process back to the concepts from Lecture 10 (nodes, topics, the ROS 2 daemon, overlays).