ROS 2 (Robot Operating System 2) is an open-source middleware framework used for building robotic applications.
ROS 2 provides:
- Hardware abstraction
- Device drivers
- Communication between processes
- Package management
- Simulation tools
- Visualization tools
- Real-time support
- Multi-platform compatibility
- DDS-based communication
- Improved security
- Better performance for distributed systems
ROS 2 consists of:
A node is an executable that performs a specific task.
Examples:
- Camera Node
- Motor Controller Node
- Sensor Node
Topics enable asynchronous communication between nodes.
Example:
Camera Node ---> /image_raw ---> Image Processing Node
A node that sends data to a topic.
A node that receives data from a topic.
Request-response communication.
Example:
Client --> Request
Server --> Response
Long-running tasks with feedback.
Example:
- Robot Navigation
- Path Planning
mkdir -p ~/ros_ws/src
cd ~/ros_ws
colcon build
source install/setup.bashCreate a package:
cd ~/ros_ws/src
ros2 pkg create --build-type ament_python pub_sub_demoimport rclpy
from rclpy.node import Node
from std_msgs.msg import String
class PublisherNode(Node):
def __init__(self):
super().__init__('publisher_node')
self.publisher_ = self.create_publisher(
String,
'chatter',
10
)
self.timer = self.create_timer(
1.0,
self.publish_message
)
def publish_message(self):
msg = String()
msg.data = "Hello from ROS 2 Publisher"
self.publisher_.publish(msg)
self.get_logger().info(
f'Publishing: {msg.data}'
)
def main(args=None):
rclpy.init(args=args)
node = PublisherNode()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class SubscriberNode(Node):
def __init__(self):
super().__init__('subscriber_node')
self.subscription = self.create_subscription(
String,
'chatter',
self.listener_callback,
10
)
def listener_callback(self, msg):
self.get_logger().info(
f'Received: {msg.data}'
)
def main(args=None):
rclpy.init(args=args)
node = SubscriberNode()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()The ROS 2 Publisher-Subscriber model is a practical implementation of the Producer-Consumer pattern.
Produces data continuously.
msg.data = "Sensor Reading"
publisher.publish(msg)Consumes data whenever available.
def callback(msg):
print(msg.data)Producer (Publisher)
|
v
Topic
|
v
Consumer (Subscriber)
Benefits:
- Loose coupling
- Scalability
- Real-time communication
- Distributed processing
ros2 node listros2 topic listros2 topic echo /chatterros2 topic info /chatterros2 service listTurtleSim is a lightweight simulator used to learn ROS concepts.
sudo apt update
sudo apt install ros-humble-turtlesimVerify installation:
ros2 pkg executables turtlesimTerminal 1:
ros2 run turtlesim turtlesim_nodeYou should see a window containing a turtle.
Terminal 2:
ros2 run turtlesim turtle_teleop_keyControls:
Arrow Up -> Forward
Arrow Down -> Backward
Arrow Left -> Turn Left
Arrow Right -> Turn Right
ros2 node listOutput:
/turtlesim
/teleop_turtleros2 topic listImportant Topics:
/turtle1/cmd_vel
/turtle1/pose
/turtle1/color_sensorros2 topic echo /turtle1/poseThis displays:
- X coordinate
- Y coordinate
- Orientation
- Velocity
ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist \
"{linear: {x: 2.0}, angular: {z: 1.0}}"The turtle will move automatically.
ros2 service call /spawn turtlesim/srv/Spawn \
"{x: 5.0, y: 5.0, theta: 0.0, name: 'turtle2'}"ros2 service call /clear std_srvs/srv/Empty "{}"ros2 service call /reset std_srvs/srv/Empty "{}"- ROS 2 Installation
- Workspace Setup
- Nodes
- Topics
- Publishers
- Subscribers
- TurtleSim
- Services
- Actions
- Parameters
- Launch Files
- Custom Messages
- Navigation Stack
- SLAM
- Gazebo Simulation
- MoveIt
- Multi-Robot Systems
- ROS 2 Control
ros_ws/
│
├── src/
│ ├── pub_sub_demo/
│ ├── turtlesim_examples/
│ └── custom_packages/
│
├── build/
├── install/
├── log/
│
└── README.md
Official ROS 2 Documentation: https://docs.ros.org
TurtleSim Package: https://github.com/ros/ros_tutorials
Happy Learning ROS 2 🚀