Topic 관련 명령어
ros2 topic list
ros2 topic info <토픽명>
Type:
Publisher count:
Subscription count:
ros2 topic echo <토픽명>
ros2 interface show <type>
Topic
msg type이 geometry_msgs/msg/Twist인 메시지를 사용
from geometry_msg.msg import Twist
Publisher
create_publisher(msg_type, topic_name, queue_size)
self.twist_publisher = self.create_publisher(Twist, '/turtle1/cmd_vel', 10)
Callback 함수 내에서 msg publish
msg = Twist()
msg.linear.x = random.random()
msg.angular.z = random.uniform(-1.0, 1.0)
self.get_logger().info(
f'Linear X velocity : {msg.linear.x} / Angular Z velocity : {msg.angular.z}'
)
self.twist_publisher.publish(msg)
Subscriber
create_subscription(msg_type, topic_name, callback_function, queue_size)
self.pose_subscriber = self.create_subscription(
Pose, 'turtle1/pose', self.sub_callback, queue_size
)
callback함수는 topic msg를 받을 때 마다 호출되고 msg를 매개변수로 받음
def sub_callback(self, msg):
self.get_logger().info(f"""x : {msg.x:.3f} / y : {msg.y:.3f} / z : {msg.theta:.3f}
linear_velocity : {msg.linear_velocity} / angular_velocity : {msg.angular_velocity }""")
rclpy.spin(Node)로 상태 갱신을 해야함
'ROS2' 카테고리의 다른 글
6일차 & 7일차 (0) | 2022.07.19 |
---|---|
ROS2 Service 프로그래밍 (0) | 2022.07.18 |
ROS2 Launch file (0) | 2022.07.17 |
ROS2 Node 프로그래밍 (0) | 2022.07.17 |