- 
                            
                            ROS2 Topic 기초 사용법ROS/ROS2 2025. 1. 8. 19:50ROS2 version: Jazzy 1. Topic?ROS2에서 Topic은 가장 기본이 되는 단방향 데이터 송수신 방법으로, Publisher가 데이터 전송, Subscriber가 데이터 수신을 하게 된다. 기본적으로 1:1 통신을 하지만 상황에 따라 1:N, N:1, N:N 통신도 가능하다.  Topic 1:1 통신  Topic 1:N 통신 Topic은 주로 센서데이터를 받아오거나 모터에 명령을 보낼때 사용한다. 2. Topic 실습이전처럼 Turtlesim과 Teleopkey를 실행해보자. Terminal 1 ros2 run turtlesim turtlesim_nodeTerminal 2 ros2 run turtlesim turtle_teleop_keyTerminal 3 rqt_graphrqt_graph를 통해 다음의 node graph를 볼 수 있다.  rqt_graph /teleop_turtle node로 부터 /turtle1/cmd_vel Topic을 Publisher 했고, /turtlesim node가 이를 Subscriber 하는 것을 볼 수 있다. 2.1 listros2 topic list # topic list를 출력. ros2 topic list -t # topic list와 메시지 타입 출력. topic list 2.2 echoros2 topic echo <topic_name>topic이 발행하는 데이터를 볼 수 있다. 2.3 inforos2 topic info <topic_name>topic이 발행하는 데이터의 정보를 볼 수 있다.  topic info 위의 정보를 통해서 발행된 msg의 type은 geometry_msgs/msg/Twist. 해당 Topic을 게시하는 노드가 하나, 해당 Topic을 구독하는 노드가 하나 있다는 뜻. 2.4 interface showros2 interface show geometry_msgs/msg/Twist위의 info 명령어로 얻은 msg type의 interface를 볼 수 있다.  interface show 2.5 pubros2 topic pub <topic_name> <msg_type> '<args>'Topic으로 명령을 Publisher 할 수 있다. '<args>'는 interface show로 확인한 interface. # 연속적으로 pub ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}" # 1번 실행 ros2 topic pub -1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}" # 하나의 메시지를 publisher 후 종료 ros2 topic pub --once -w 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"마지막 메시지의 경우 옵션은 다음과 같다. - --once: 메시지를 한번반 publisher
- -w 2: 구독자의 수가 2개인 경우. (현재 /turtlesim, ros2 topic echo로 Subscription의 수는 2개이다.)
  topic info로 확인한 Subscription의 수  rqt_graph로 확인한 /turtle1/cmd_vel의 Subscription 즉, 구독자의 수가 2개 이상인 경우 한 번만 실행하라는 뜻이다. 이번에는 pose를 echo 명령어로 확인해보자. ros2 topic echo /turtle1/pose /turtle1/pose 확인 1. /pose topic으로 메시지 게시ros2 topic pub /pose geometry_msgs/msg/PoseStamped '{header: "auto", pose: {position: {x: 1.0, y: 2.0, z: 3.0}}}'결과:  2. /reference topic으로 메시지 게시결과:  2.6 hzros2 topic hz /turtle1/posehz명령어로 데이터가 publish되는 속도(Hz)를 확인할 수 있다. 결과:  hz 확인 평균 62hz 정도가 나오며 약 0.016초에 한번씩 토픽을 발행하고 있다. 2.7 bwros2 topic bw /turtle1/poseTopic이 사용하는 대역폭(bandwidth)을 확인할 수 있다. 결과:  bw 확인 결과를 보면 평균 1.50 KB/s의 대역폭으로 /turtle1/pose 토픽이 사용되는 것을 확인할 수 있다. 2.8 findros2 topic find <topic_type>특정 메시지 타입을 어떤 topic이 이용하는지 보려면 위의 명령어를 사용하면 된다. ros2 topic find geometry_msgs/msg/Twist결과:  2.9 delaytopic은 RMW(Robot MiddleWare) 및 네트워크 장비를 거치기 때문에 지연(latency)시간이 반드시 존재한다. 사용하는 msg에 header[6]라는 stamp msg를 사용하고 있다면 다음의 명령어를 이용하여 지연 시간을 확인할 수 있다. ros2 topic delay <topic_name>지연 시간 = |발행한 시간 - 구독한 시간| ref: Understanding topics — ROS 2 Documentation: Jazzy documentation © Copyright 2025, Open Robotics. docs.ros.org 'ROS > ROS2' 카테고리의 다른 글ROS2 Action 기초 사용법 (0) 2025.02.04 ROS2 Service 기초 사용법 (0) 2025.01.13 ROS2 Node 기초 사용법 (0) 2025.01.07 ROS2 유용한 명령어 (0) 2025.01.07 ROS2 DDS (Data Distribution Service) (0) 2025.01.06