Exercises: ROS 2 Architecture and Communication Patterns
Exercise 1: Basic Publisher-Subscriber
Objective
Create a simple publisher-subscriber system to understand the basic communication pattern in ROS 2.
Prerequisites
- Basic Python knowledge
- ROS 2 Humble Hawksbill installed
- Understanding of ROS 2 nodes and topics
Problem Statement
Create two nodes: a publisher that sends temperature readings and a subscriber that receives and processes these readings.
Instructions
- Create a publisher node that publishes temperature values (Float64 messages) to the topic
/temperature - The publisher should send a new temperature value every 2 seconds
- The temperature values should simulate realistic room temperatures (between 18°C and 25°C)
- Create a subscriber node that listens to the
/temperaturetopic - The subscriber should log received temperature values and indicate if they are within a comfortable range (>20°C and <24°C)
- Run both nodes and observe the communication
Expected Outcome
Two nodes communicating via the /temperature topic, with the subscriber successfully receiving and processing temperature values.
Hints
- Use
std_msgs.msg.Float64for temperature messages - Use
self.create_publisher()andself.create_subscription()methods - Remember to initialize rclpy and spin the nodes
Solution Approach
- Create a node class inheriting from
rclpy.node.Node - Initialize a publisher in the constructor
- Create a timer to publish messages periodically
- For the subscriber, create a callback function to handle incoming messages
- Run both nodes using
rclpy.spin()
Exercise 2: Service-Based Calculator
Objective
Implement a calculator service that can perform basic arithmetic operations.
Prerequisites
- Understanding of ROS 2 services
- Knowledge of creating service servers and clients
Problem Statement
Create a service that accepts two numbers and an operation type (add, subtract, multiply, divide) and returns the result.
Instructions
- Define a custom service interface with fields for two numbers, operation type, and result
- Create a service server that implements the calculator functionality
- Create a service client that sends requests to the server
- Test the service with various operations
- Handle division by zero appropriately
Expected Outcome
A working service system that can perform arithmetic operations on demand.
Hints
- Create a
.srvfile in asrvdirectory - Use
ros2 runto test the service client - Handle edge cases like division by zero
Solution Approach
- Define the service interface in a
.srvfile - Create a service server node with the callback function
- Create a service client node that sends requests
- Test with various inputs and verify correct results
Exercise 3: Action-Based Navigation
Objective
Implement a navigation action that simulates moving a robot to a target position.
Prerequisites
- Understanding of ROS 2 actions
- Knowledge of action servers and clients
Problem Statement
Create an action that accepts a target position (x, y coordinates) and simulates navigating to that position, providing feedback on progress.
Instructions
- Define a custom action interface for navigation
- Create an action server that simulates navigation to the target
- The server should provide feedback on current progress (percentage complete)
- Create an action client that sends navigation goals
- The client should handle feedback and final results
- Test with various target positions
Expected Outcome
A working action system that simulates robot navigation with progress feedback.
Hints
- Use
action_msgsfor action interfaces - An action interface has three parts: Goal, Feedback, Result
- Implement proper feedback publishing during execution
Solution Approach
- Define the action interface in a
.actionfile - Create an action server with execution callback
- Implement feedback publishing during navigation simulation
- Create an action client that sends goals and monitors progress
- Test with different target positions
Exercise 4: QoS Configuration Challenge
Objective
Experiment with different Quality of Service settings and observe their effects.
Prerequisites
- Understanding of ROS 2 QoS profiles
- Working publisher-subscriber system
Problem Statement
Modify your publisher-subscriber system from Exercise 1 to experiment with different QoS settings and observe the effects on communication.
Instructions
- Modify your temperature publisher to use RELIABLE reliability
- Change to BEST_EFFORT reliability and observe differences
- Experiment with different history policies (KEEP_ALL, KEEP_LAST)
- Test with different depth settings for message queues
- Document the differences in behavior
Expected Outcome
Understanding of how QoS settings affect message delivery and communication behavior.
Hints
- Import QoS profiles from
rclpy.qos - Use
QoSProfileto customize settings - Observe behavior changes when modifying QoS parameters
Solution Approach
- Import necessary QoS classes
- Create different QoS profiles
- Apply profiles to publishers and subscribers
- Run experiments and compare results
- Document observations
Exercise 5: Parameter-Based Configuration
Objective
Create a node that uses parameters for runtime configuration.
Prerequisites
- Understanding of ROS 2 parameters
- Working knowledge of node creation
Problem Statement
Create a node that adjusts its behavior based on runtime parameters.
Instructions
- Create a node that publishes messages at a configurable rate
- Use a parameter for the publication frequency (Hz)
- Allow the parameter to be changed at runtime
- Add a callback to handle parameter changes
- Test changing the parameter while the node is running
Expected Outcome
A node that responds to parameter changes without restarting.
Hints
- Use
declare_parameter()to declare parameters - Use
add_on_set_parameters_callback()for parameter change handling - Use
ros2 paramcommand to change parameters at runtime
Solution Approach
- Declare parameters in the node constructor
- Use parameters to control node behavior
- Implement parameter callback to handle changes
- Test parameter changes using ROS 2 command line tools
Assessment Criteria
For all exercises:
- Code follows ROS 2 best practices
- Proper error handling is implemented
- Code is well-documented with comments
- Nodes are properly initialized and cleaned up
- Appropriate message types are used
Exercise 1:
- Publisher sends messages at regular intervals
- Subscriber receives and processes messages correctly
- Temperature values are within expected range
Exercise 2:
- Service responds correctly to requests
- All arithmetic operations work properly
- Edge cases (like division by zero) are handled
Exercise 3:
- Action provides appropriate feedback during execution
- Result is returned when navigation completes
- Client properly handles feedback and results
Exercise 4:
- Different QoS settings are properly applied
- Effects of different settings are observed and documented
Exercise 5:
- Parameters are properly declared and used
- Runtime parameter changes are handled correctly
- Parameter callback functions work as expected
Extension Activities
- Advanced Exercise: Combine multiple communication patterns (topics, services, actions) in a single application
- Challenge Exercise: Implement a distributed system with multiple nodes coordinating via different communication methods
- Research Exercise: Investigate the performance implications of different QoS settings in various network conditions