A comprehensive beginner-friendly ROS 2 package demonstrating fundamental concepts using the turtlesim simulator. This package includes examples of publishers, subscribers, services, clients, and launch files.
This package is designed as a complete learning resource for ROS 2 beginners. It demonstrates:
- Topic Publishers - Sending velocity commands to control turtle movement
- Topic Subscribers - Receiving and displaying turtle pose information
- Service Servers - Providing status checking services
- Service Clients - Calling status checking services
- Launch Files - Starting multiple nodes simultaneously
You should see output similar to: ros2 cli version: 0.18.x
turtle_controller/
βββ launch/
β βββ turtle_system_launch.py # Basic launch file
βββ turtle_controller/
β βββ __init__.py
β βββ velocity_publisher.py # Publishes velocity commands
β βββ pose_subscriber.py # Subscribes to turtle pose
β βββ turtle_status_service.py # Service server for status checking
β βββ turtle_status_client.py # Service client for status checking
βββ package.xml # Package metadata
βββ setup.py # Python package setup
βββ setup.cfg # Package configuration
βββ README.md # This file
# Create workspace
mkdir -p ~/turtle_ws/src
cd ~/turtle_ws/src
# Clone or copy this package into src/
# (If you created it manually, skip this step)
cd ~/turtle_wsrosdep install --from-paths src --ignore-src -r -ycd ~/turtle_ws
colcon build
source install/setup.bashros2 launch turtle_controller turtle_system_launch.pyYou should see:
- Turtlesim window opens
- Turtle moves in a circle
- Pose information printed to terminal
ros2 run turtlesim turtlesim_nodePublishes Twist messages to /turtle1/cmd_vel to make the turtle move in a circular pattern.
Run standalone:
ros2 run turtle_controller velocity_publisherTopics Published:
/turtle1/cmd_vel(geometry_msgs/msg/Twist) - Velocity commands
Subscribes to /turtle1/pose and displays the turtle's position and orientation.
Run standalone:
ros2 run turtle_controller pose_subscriberTopics Subscribed:
/turtle1/pose(turtlesim/msg/Pose) - Turtle position and orientation
Provides a service to check if the turtle is near the center of the window.
Run standalone:
ros2 run turtle_controller turtle_status_serviceServices Provided:
/check_turtle_status(std_srvs/srv/Trigger) - Check if turtle is near center
Call the service manually:
ros2 service call /check_turtle_status std_srvs/srv/TriggerA command-line client that calls the turtle status service.
Usage:
ros2 run turtle_controller turtle_status_clientExample output:
β Turtle is near center! Position: (5.54, 5.54)
Terminal 1 - Start turtlesim:
ros2 run turtlesim turtlesim_nodeTerminal 2 - Monitor pose:
ros2 run turtle_controller pose_subscriberTerminal 3 - Control with keyboard:
ros2 run turtlesim turtle_teleop_keyTerminal 1 - Launch system:
ros2 launch turtle_controller turtle_system_launch.pyTerminal 2 - Check turtle status:
ros2 run turtle_controller turtle_status_clientTerminal 3 - Move turtle manually:
ros2 run turtlesim turtle_teleop_keyTerminal 2 - Check status again after moving:
ros2 run turtle_controller turtle_status_clientAfter modifying any Python files:
cd ~/turtle_ws
colcon build --packages-select turtle_controller
source install/setup.bash- Create your Python script in
turtle_controller/ - Make it executable:
chmod +x your_script.py - Add entry point in
setup.py:entry_points={ 'console_scripts': [ 'your_node = turtle_controller.your_script:main', ], },
- Rebuild the package
Test publisher only:
# Terminal 1
ros2 run turtlesim turtlesim_node
# Terminal 2
ros2 run turtle_controller velocity_publisherTest service:
# Terminal 1
ros2 run turtlesim turtlesim_node
# Terminal 2
ros2 run turtle_controller turtle_status_service
# Terminal 3
ros2 run turtle_controller turtle_status_clientSolution:
cd ~/turtle_ws
source install/setup.bashSolution:
- Check that entry points are correctly added in
setup.py - Rebuild:
colcon build --packages-select turtle_controller - Source:
source install/setup.bash
Solution:
- Ensure turtlesim is running:
ros2 run turtlesim turtlesim_node - List available topics:
ros2 topic list - List available services:
ros2 service list
Solution:
chmod +x ~/turtle_ws/src/turtle_controller/turtle_controller/*.pySolution:
# Install missing dependencies
rosdep install --from-paths src --ignore-src -r -y
colcon build
source install/setup.bash# List all nodes
ros2 node list
# List all topics
ros2 topic list
# See topic info
ros2 topic info /turtle1/cmd_vel
# Echo topic messages
ros2 topic echo /turtle1/pose
# List services
ros2 service list
# See service type
ros2 service type /rotate_turtle
# View computational graph
rqt_graph# Run with debug output
ros2 run turtle_controller velocity_publisher --ros-args --log-level debug
# Check node info
ros2 node info /velocity_publisher
# Monitor topic frequency
ros2 topic hz /turtle1/cmd_vel