728x90
1.Topic이란 ?
간단한 설명으로는 Node 간의 메시지 기반으로 정보를 공유하는 방법이다.
특징은
- 단방향 통신이다. (publisher / subscriber)
- 익명성, broadcasting 방식으로 전송되는 데이터로 누가 보냈는지는모른다
- 메세지 형식
- Node 안에서 구현된다
- Node는 여러개의 Topic에 대한 publisher / subscriber를 구현할 수 있다.
Topic list
현재 publish 되고 있는 topic 리스트 정보를 출력한다
ros2 topic list
Topic echo
현재 publish 되고 있는 topic 의 데이터를 출력한다
ros2 topic echo [topic이름]
2. Interface
Topic 을 메시지의 프로토콜이다. 메시지의 데이터가 어떤 format 으로 전송되는지를 정의한 데이터이다.
아래의 명령으로 제공되는 interface 의 프로토콜 구조를 확인할 수 있다.
ros2 interface show [interface경로]
3. Publisher
메시지를 전송하는 주체이다. Timer를 이용하여 일정 간격의 시간마다 지정된 interface의 프로토콜에 데이터를 실어서 publish 함수로 전송한다.
class RobotNewsStation(Node):
def __init__(self):
super().__init__("robot_news_station")
self.robot_name = "Richard"
self.create_timer(0.5, self.publish_news)
self.publisher = self.create_publisher(String, "robot_news",10)
self.get_logger().info('Robot News Station has been started')
def publish_news(self):
msg = String()
msg.data = "Hi, this is " + self.robot_name + " from robot news station"
self.publisher.publish(msg)
3. Subscriber
Broadcasting 으로 전송되는 Topic 메시지를 수신한다. Subscribe는 callback 을 이용하여 메세지가 감지되면 callback이 실행되는 구조이다.
class Smartphone(Node):
def __init__(self):
super().__init__("smartphone")
self.subscriber = self.create_subscription(String, "robot_news", self.subscribe_news, 10)
self.get_logger().info('Smartphone is on to listen the news from robot news station')
def subscribe_news(self, msg):
self.get_logger().info(msg.data)
위에서 구현한 Publisher 와 Subscriber 의 Graph를 그리면 아래와 같이 나타난다.
728x90
728x90