Konsep Dasar ROS 2 | ROS 2 Core Concepts
Memahami Nodes, Topics, Services, dan Actions di ROS 2 | Understanding Nodes, Topics, Services, and Actions in ROS 2
Konsep Dasar ROS 2
Learning Objectives
Setelah materi ini, kamu akan memahami: (1) Nodes sebagai unit eksekusi, (2) Topics untuk komunikasi publish/subscribe, (3) Services untuk request/response, (4) Actions untuk task dengan feedback.
Nodes
Node adalah unit eksekusi terkecil di ROS 2. Setiap node menjalankan satu tugas spesifik dan berkomunikasi dengan node lain.
Click on nodes to see their connections. Hover over lines to see topic names.
Membuat Node Sederhana
# minimal_node.py
import rclpy
from rclpy.node import Node
class MinimalNode(Node):
def __init__(self):
super().__init__('minimal_node')
self.get_logger().info('Hello from MinimalNode!')
def main(args=None):
rclpy.init(args=args)
node = MinimalNode()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()CLI Commands untuk Nodes
# List semua nodes yang sedang berjalan
ros2 node list
# Info tentang node tertentu
ros2 node info /minimal_nodeTopics
Topics adalah channel untuk komunikasi publish/subscribe - one-way, continuous data streaming.
View Code
# Publisher
self.publisher = self.create_publisher(
String, '/camera/image', 10
)
self.publisher.publish(msg)
# Subscriber
self.subscription = self.create_subscription(
String, '/camera/image',
self.callback, 10
)Contoh: Publisher Node
import 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(0.5, self.timer_callback)
self.count = 0
def timer_callback(self):
msg = String()
msg.data = f'Hello BASCORRO #{self.count}'
self.publisher_.publish(msg)
self.count += 1Contoh: Subscriber Node
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}"')CLI Commands untuk Topics
ros2 topic list # List semua topics
ros2 topic echo /chatter # Lihat data yang di-publish
ros2 topic info /chatter # Info tentang topic
ros2 topic hz /chatter # Check publish rateServices
Services adalah komunikasi request/response - synchronous, two-way communication. Cocok untuk operasi yang membutuhkan konfirmasi.
Contoh: Service Server
from std_srvs.srv import SetBool
class ServiceServer(Node):
def __init__(self):
super().__init__('service_server')
self.srv = self.create_service(
SetBool, 'set_led', self.set_led_callback
)
self.led_on = False
def set_led_callback(self, request, response):
self.led_on = request.data
response.success = True
response.message = f'LED is now {"ON" if self.led_on else "OFF"}'
return responseCLI Commands untuk Services
ros2 service list
ros2 service call /set_led std_srvs/srv/SetBool "data: true"Actions
Actions adalah untuk task yang membutuhkan waktu lama dengan feedback dan kemampuan cancel.
| Use Case | Contoh |
|---|---|
| Task lama | Walking to goal, executing motion sequence |
| Butuh feedback | Navigation progress, motion percentage |
| Bisa di-cancel | Stop walking, abort kick |
CLI Commands untuk Actions
ros2 action list
ros2 action info /navigate_to_posePerbandingan
| Aspek | Topics | Services | Actions |
|---|---|---|---|
| Pola | Pub/Sub | Request/Response | Goal/Feedback/Result |
| Arah | One-way | Two-way | Two-way + Feedback |
| Blocking | Non-blocking | Blocking | Non-blocking |
| Use Case | Sensor data, continuous | Get/Set params | Long tasks |
| Cancel | N/A | N/A | Yes |
Latihan
Challenge: Ball Position Publisher
Buat sebuah node yang:
- Publish posisi bola (x, y) setiap 100ms ke topic
/ball_position - Posisi bola berubah secara random dalam range 0-10
Hints: Gunakan geometry_msgs/msg/Point dan random.uniform(0, 10)
Lihat Solusi
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Point
import random
class BallPublisher(Node):
def __init__(self):
super().__init__('ball_publisher')
self.publisher_ = self.create_publisher(Point, 'ball_position', 10)
self.timer = self.create_timer(0.1, self.publish_ball)
def publish_ball(self):
msg = Point()
msg.x = random.uniform(0, 10)
msg.y = random.uniform(0, 10)
msg.z = 0.0
self.publisher_.publish(msg)
def main(args=None):
rclpy.init(args=args)
rclpy.spin(BallPublisher())
rclpy.shutdown()Quiz
1. Kapan menggunakan Topic vs Service?
Topic: Untuk data continuous yang perlu di-broadcast ke banyak subscriber (sensor data, camera images, robot state).
Service: Untuk operasi request-response satu kali (get parameter, trigger action).
2. Apa keuntungan Actions dibanding Service?
Actions menyediakan: (1) Feedback progress selama eksekusi, (2) Kemampuan cancel task, (3) Non-blocking execution.