ROS(Robot Operating System)命令与通信速查笔记(优化版)
0. 快速心智模型(Mental Model)
-
ROS Master(roscore):名称服务(Name service)+ 参数服务(Parameter server)等基础设施;节点通过它做“发现(discovery)”和“握手(handshake)”。
-
Node(节点):可执行进程(process),发布/订阅 Topic,提供/调用 Service,或作为 Action 的 client/server。
-
Topic(主题):异步流式消息(asynchronous streaming),多对多(many-to-many)。
-
Service(服务):同步请求-响应(synchronous request/response),一次性(one-shot),短事务(short transaction)。
-
Action(动作):面向长任务(long-running task),支持 goal / feedback / result / status / cancel。
1. 启动与运行(Launch & Run)
1.1 roscore
启动 ROS Master(ROS Master)。
roscore
要点(Notes):
-
只需要一个 roscore;重复启动会端口冲突(port conflict)。
-
分布式 ROS(distributed ROS)时,
ROS_MASTER_URI环境变量(environment variable)会影响连接。
1.2 roslaunch
通过 launch 文件启动多个节点(launch multiple nodes)。
roslaunch <package_name> <launch_file.launch>
roslaunch turtlebot3_gazebo turtlebot3_empty_world.launch
要点(Notes):
-
roslaunch会自动启动roscore(如果未启动)。 -
常用于一键拉起:仿真(Gazebo)、传感器(sensors)、导航栈(navigation stack)等。
1.3 rosrun
运行包内可执行文件(run executable in a package)。
rosrun <package_name> <executable>
rosrun rqt_graph rqt_graph
2. 节点(Node)观察与调试(Debugging)
2.1 rosnode
查看 ROS 网络中活跃节点(active nodes)。
rosnode list
rosnode info /<node_name>
3. Topic(主题)观察与调试(rostopic)
3.1 列表与过滤(List & Filter)
rostopic list
rostopic list | grep /camera
3.2 查看 Topic 详情(Type / Publisher / Subscriber)
rostopic info /cmd_vel
重点字段(Key fields):
-
Type:消息类型(message type),例如geometry_msgs/Twist -
Publishers / Subscribers:谁在发(publisher)、谁在收(subscriber)
3.3 查看消息内容(Echo)
rostopic echo /chatter
rostopic echo -n 2 /chatter # 只看 2 条(2 messages)
rostopic echo -c /odom # 清屏持续刷新(clear screen, good for odom)
rostopic echo -h # 查看更多参数(help)
3.4 发布一条消息(Pub)
从命令行向 topic 发布消息(publish message from CLI)。
rostopic pub /<topic_name> <msg_type> '<yaml_data>'
示例(给 /cmd_vel 发速度 / velocity command):
rostopic pub /cmd_vel geometry_msgs/Twist '{linear: {x: 0.2, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.5}}'
4. Message(消息)类型查看(rosmsg)
4.1 rosmsg info
查看某个消息类型的字段结构(message definition / fields)。
rosmsg info geometry_msgs/Twist
备注(Note):
Type往往来自rostopic info /xxx的输出。
5. Service(服务)观察与调用(rosservice / rossrv)
5.1 rosservice:列出与查看信息(List & Info)
rosservice list
rosservice info /<service_name>
5.2 调用 Service(Call)
rosservice call /<service_name> '<yaml_request>'
5.3 rossrv info:查看 srv 的请求/响应结构(Request/Response Schema)
用于确认 request/response 字段(fields)。
rossrv info <package_name>/<SrvType>
# Example:
rossrv info tuos_ros_msgs/SetBool
典型输出(Typical output):
bool request_signal
---
bool response_signal
string response_message
关键纠错(Important Fix):Client / Server 方向关系
-
Service Client:发送请求(request)
-
Service Server:接收请求并返回响应(response)
更严谨描述(Precise wording):
-
client.py发 request,并接收 response -
server.py接收 request,并返回 response
6. Action(动作)调试与流程(Actionlib)
Action 本质:一组约定 Topic + 状态机(state machine),通常包含:
-
/<action_name>/goal -
/<action_name>/feedback -
/<action_name>/result -
/<action_name>/status -
/<action_name>/cancel
6.1 用 rostopic 找 Action(实战技巧)
更精确的做法:找 goal/status/result 后缀(suffix)。
rostopic list | grep /goal
rostopic list | grep <action_name>
6.2 看 Action 的消息结构(Goal/Feedback/Result 类型)
步骤(Steps):
- 先看 goal topic 的类型(Type):
rostopic info /<action_name>/goal
- 把
Type拿去查结构(schema):
rosmsg info <Type>
6.3 Action Client 代码结构(建议模板)
核心元素(Key elements):
-
SimpleActionClient初始化(client init) -
wait_for_server()等 server 上线(wait for server) -
send_goal(goal, feedback_cb=...) -
get_result()获取最终结果(final result) -
可选(Optional):
cancel_goal()/cancel_all_goals()
术语对齐(Terminology alignment):
-
feedback_callback:由 server 推送触发(server-driven callback),不是 client 自己调用。 -
shutdown_ops:更标准命名建议shutdown_hook或on_shutdown。
6.4 Action 的消息流(Message Flow)
-
client → server:
goal(通常一次 / usually once) -
server → client:
feedback(多次 / multiple) -
server → client:
status(持续更新 / continuous) -
server → client:
result(最终一次 / final once)
7. Catkin(构建系统)与工作空间(Workspace)
7.1 创建包(catkin_create_pkg)
catkin_create_pkg week1_pubsub std_msgs rospy
含义(Meaning):
-
week1_pubsub:包名(package name) -
std_msgs rospy:依赖(dependencies)
7.2 构建(catkin build / catkin_make)
使用 catkin_tools 的 catkin build:
catkin build week1_pubsub
更稳的环境加载方式(Recommended)
构建后 source workspace 的 setup:
source devel/setup.bash
如果要每次开终端自动生效,把上面这行写进 ~/.bashrc(recommended persistent setup),而不是每次都 source ~/.bashrc。
8. Linux 常用辅助命令(Utilities)
8.1 chmod
给脚本可执行权限(make file executable)。
chmod +x publisher.py
8.2 eog
命令行打开图片(Eye of GNOME)。
eog .
8.3 backup / restore(自定义脚本)
这两个看起来是 WSL 下的自定义命令(custom alias/script),不是 ROS 自带命令:
wsl_ros backup
wsl_ros restore
建议在笔记中标注来源(where it comes from),避免误解。
9. 通信方式选型(When to Use What)
-
Topic(发布/订阅, Pub/Sub):低耦合(loose coupling)、持续数据流(streaming),适合传感器(sensors)、里程计(odometry)、控制指令(control command);允许一定丢包/延迟(loss/latency tolerance)。
-
Service(服务, RPC-like):短、确定性强(short & deterministic)的操作,例如查状态(query)、一次性计算(one-shot computation)、触发开关(toggle)。
-
Action(动作, Long task + progress):耗时任务(long task)、可取消(cancelable)、需要进度反馈(feedback),例如导航到点(navigation)、机械臂轨迹(trajectory execution)。
10. 最值得记住的修正点(Top Fixes)
-
Service 方向:client 发 request;server 回 response(client 同时会接收 response)。
-
Action 定位:优先 grep
/goal找 action,再用rostopic info→rosmsg info深挖字段结构。 -
构建后 source:推荐
source devel/setup.bash;需要持久化再写入~/.bashrc。