节点与话题

ros2是一个机器人操作系统,用于打通各个硬件模块之间的通信。本文主要写的是ros2中的节点与话题。
截图 2025-07-15 16-55-38.png
节点:节点通常负责着某一模块:例如电机控制。其中节点可以通过话题、服务、参数、动作等传递或接受数据。

ros2 run <package_name> <executable_name> 打开包中的可执行文件
ros2 node list    可以返回运行中的节点名称

节点的重映射可用于需要多次用到某一节点的场景:例如:需要两个小海龟同时运行。

ros2 run turtlemism turtlesim_node --ros-args --remap __node:=my_turtle
ros2 node list

当此前先开启一个小海龟,此时再运行此命令会返回两个节点名称/turtlesim/my_turtle

对于一个节点通常需要获取它话题的订阅者、发布者、服务、参数等信息。可通过ros2 node info /my_turtle显示出来。例子:

/my_turtle
  Subscribers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/turtle1/cmd_vel: geometry_msgs/msg/Twist
  Publishers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/rosout: rcl_interfaces/msg/Log
/turtle1/color_sensor: turtlesim/msg/Color
/turtle1/pose: turtlesim/msg/Pose
  Services:
/clear: std_srvs/srv/Empty
/kill: turtlesim/srv/Kill
/reset: std_srvs/srv/Empty
/spawn: turtlesim/srv/Spawn
/turtle1/set_pen: turtlesim/srv/SetPen
/turtle1/teleport_absolute: turtlesim/srv/TeleportAbsolute
/turtle1/teleport_relative: turtlesim/srv/TeleportRelative
/my_turtle/describe_parameters: rcl_interfaces/srv/DescribeParameters
/my_turtle/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
/my_turtle/get_parameters: rcl_interfaces/srv/GetParameters
/my_turtle/list_parameters: rcl_interfaces/srv/ListParameters
/my_turtle/set_parameters: rcl_interfaces/srv/SetParameters
/my_turtle/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Action Servers:
/turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
  Action Clients:

话题:充当节点信息交换的总线。
2025-07-15T10:14:00.png
通过命令rqt_graph可以可视化变化的节点与话题
2025-07-15T10:51:23.png
ros2 topic list -t会返回话题,并且在最后面会返回消息的类型。
2025-07-15T11:01:39.png
ros2 topic echo <topic_nmae>此命令能返回特定话题的具体数据
2025-07-15T12:39:40.png
ros2 topic info <topic_name>此命令能返回话题的有几个订阅者与发布者,通常可以有多个订阅者。
2025-07-15T12:51:08.png
ros2 interface show <msg_type>此命令能返回某一数据类型例如geometry_msgs/msg/Twist的数据结构,包含linearangular两个速度向量。
2025-07-15T12:56:40.png
直接将数据从命令行发布到话题上采用命令ros2 topic pub --once <topic_name> <msg_type> <args>,其中--once表示仅仅发布一次就退出,而args需要按照上面命令ros2 interface show <msg_type>获取的数据结构进行填充。
2025-07-15T13:10:05.png
而若想要一直稳定运行,则需要在一定时间发布一次而将--rate 1代替--once表示以1hz的命令流发布命令。
2025-07-15T13:14:14.png
ros2 topic hz <topic_name>此命令可以获取到数据发布到话题的速率。
2025-07-15T13:20:49.png

最后修改:2025 年 07 月 15 日
如果觉得我的文章对你有用,请随意赞赏