ROS2极简总结-命令行接口基础
Posted zhangrelay
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROS2极简总结-命令行接口基础相关的知识,希望对你有一定的参考价值。
参考文献:ROS 2 CLI BASICS - A quick dive into ROS 2 Command Line Interface
之前,个人博客大量教程分节介绍过这些命令行,此文做一个总述,更方便了解全貌。
大纲
- 安装和导入
- 基本命令结构
- 发布者和订阅者
- 服务
- 行动和参数
- 工具
安装和导入
- 两种安装类型 - 二进制包(推荐)或源代码编译。(通常源代码编译为专业开发者喜爱)
- Live USB 具有 ROS2 Foxy 功能 - 验证:
- ll /opt/ros/foxy.
- 导入 ROS2 使用命令、库、包等:
- source /opt/ros/foxy/setup.bash
- 根据需要获取本地工作空间(也可以导入 ROS):
- source /path/to/ws/install/local_setup.bash
别名
如何快速导入这些环境呢?
- 别名是自定义定义的命令缩写。
- 只需输入 source_foxy(或希望的任何字符串)而不是 source /opt/ros/foxy/setup.bash (或希望的任何命令)
- 有关语法,请参阅官方文档。
ROS_DOMAIN_ID
- 环境变量
- 每台机器的唯一标识符号
- 物理隔离网络
- 停止ROS2节点干扰
- 一劳永逸:echo "export ROS_DOMAIN_ID=<your_id>" >> ~/.bashrc
编译 ROS2(回顾)
colcon build --symlink-install
C:\\ros_ws\\webots_ros2>colcon build
[2.178s] root DEBUG Using proactor: IocpProactor
Starting >>> control_msgs
Starting >>> controller_manager_msgs
Starting >>> vision_msgs
Starting >>> realtime_tools
Starting >>> ros2_control_test_assets
Starting >>> dynamixel_sdk
Starting >>> webots_ros2_ur_e_description
Starting >>> ackermann_msgs
Finished <<< webots_ros2_ur_e_description [5.11s]
Starting >>> angles
Finished <<< ros2_control_test_assets [7.39s]
Starting >>> turtlebot3_description
Finished <<< dynamixel_sdk [8.98s]
Starting >>> turtlebot3_node
Finished <<< angles [7.53s]
Starting >>> dynamixel_sdk_custom_interfaces
Finished <<< turtlebot3_description [7.91s]
Starting >>> turtlebot3_cartographer
Finished <<< turtlebot3_cartographer [7.69s]
Starting >>> turtlebot3_example
Finished <<< realtime_tools [23.6s]
Starting >>> turtlebot3_navigation2
Finished <<< turtlebot3_example [5.89s]
Starting >>> turtlebot3_teleop
Finished <<< turtlebot3_node [20.3s]
Starting >>> webots_ros2_importer
Finished <<< turtlebot3_navigation2 [11.0s]
Starting >>> turtlebot3_bringup
Finished <<< ackermann_msgs [35.2s]
Finished <<< controller_manager_msgs [36.6s]
Finished <<< turtlebot3_teleop [7.88s]
Finished <<< webots_ros2_importer [7.44s]
Finished <<< vision_msgs [39.6s]
Starting >>> webots_ros2_msgs
Finished <<< turtlebot3_bringup [5.92s]
Starting >>> turtlebot3
Finished <<< dynamixel_sdk_custom_interfaces [28.8s]
Starting >>> dynamixel_sdk_examples
Finished <<< control_msgs [43.3s]
Starting >>> hardware_interface
Finished <<< turtlebot3 [5.70s]
Finished <<< dynamixel_sdk_examples [8.50s]
Finished <<< hardware_interface [15.6s]
Starting >>> controller_interface
Starting >>> transmission_interface
Finished <<< webots_ros2_msgs [20.9s]
Starting >>> webots_ros2_core
Starting >>> webots_ros2_driver
Finished <<< webots_ros2_core [4.45s]
Starting >>> webots_ros2_abb
Starting >>> webots_ros2_universal_robot
Starting >>> webots_ros2_epuck
Starting >>> webots_ros2_examples
Starting >>> webots_ros2_mavic
Finished <<< transmission_interface [15.8s]
Starting >>> webots_ros2_tesla
Finished <<< controller_interface [18.1s]
Starting >>> controller_manager
Finished <<< webots_ros2_abb [12.0s]
Finished <<< webots_ros2_universal_robot [11.9s]
Starting >>> webots_ros2_tiago
Starting >>> webots_ros2_tutorials
Finished <<< webots_ros2_examples [11.9s]
Finished <<< webots_ros2_epuck [12.2s]
Starting >>> webots_ros2_demos
Finished <<< webots_ros2_mavic [17.3s]
Finished <<< webots_ros2_driver [49.4s]
Finished <<< webots_ros2_tiago [34.5s]
Finished <<< webots_ros2_tesla [37.1s]
Finished <<< webots_ros2_tutorials [34.5s]
Finished <<< webots_ros2_demos [31.5s]
Finished <<< controller_manager [48.0s]
Starting >>> forward_command_controller
Starting >>> joint_state_broadcaster
Starting >>> diff_drive_controller
Starting >>> webots_ros2_control
Starting >>> force_torque_sensor_broadcaster
Starting >>> imu_sensor_broadcaster
Starting >>> joint_trajectory_controller
Starting >>> ros2controlcli
Finished <<< ros2controlcli [9.67s]
Starting >>> gripper_controllers
[143.592s] colcon.colcon_cmake.task.cmake.build WARNING Could not run installation step for package 'gripper_controllers' because it has no 'install' target
--- stderr: gripper_controllers
CMake Warning at CMakeLists.txt:5 (message):
gripper controllers are not available on OSX or Windows
---
Finished <<< gripper_controllers [5.92s]
Starting >>> ros2_control
Finished <<< joint_state_broadcaster [25.6s]
Starting >>> joint_state_controller
Finished <<< forward_command_controller [26.8s]
Starting >>> effort_controllers
Finished <<< force_torque_sensor_broadcaster [26.9s]
Finished <<< ros2_control [10.9s]
Starting >>> position_controllers
Starting >>> velocity_controllers
Finished <<< imu_sensor_broadcaster [28.1s]
Finished <<< diff_drive_controller [29.3s]
Finished <<< webots_ros2_control [29.5s]
Starting >>> webots_ros2_turtlebot
Finished <<< joint_trajectory_controller [32.0s]
Finished <<< webots_ros2_turtlebot [11.7s]
Starting >>> webots_ros2
Finished <<< joint_state_controller [19.0s]
Finished <<< effort_controllers [21.8s]
Finished <<< position_controllers [21.2s]
Finished <<< velocity_controllers [21.2s]
Starting >>> ros2_controllers
Finished <<< webots_ros2 [11.6s]
Finished <<< ros2_controllers [5.81s]
Summary: 53 packages finished [3min 1s]
1 package had stderr output: gripper_controllers
C:\\ros_ws\\webots_ros2>
基本命令结构
为简单起见,只讲解二进制安装包附带的默认包。
所有 ROS2 命令都遵循以下语法:
ros2 <main_command> <sub_command> <<arguments>>
实例 :
ros2 pkg list
ros2 pkg executables
使用 Tab 补全完成命令,或查看可用选项列表。
示例:
- ros2 tab tab - 列出所有 ROS2 命令。
- ros2 pkg tab tab - pkg 的子命令列表。
- -h 几乎可以在任何时候获得简短的帮助说明。
- ros2 -h
- ros2 pkg -h
- ros2 pkg executable -h
发布者和订阅者
这是最基础的ROS示例,必须掌握。
发布者
ROS 最基本的例子——发布者/订阅者
- 记得导入ROS2环境。
- 命令启动在包中的可执行文件(C++ 编译对象/Python 脚本):
ros2 run <package_name> <executable_name> <<optional_command_line_arguments>>
*注意空格
- 尝试使用 ROS2 默认的示例启动发布者节点。
- 首先需要找出包的名称。
- 使用过滤器运行包列表命令:
- ros2 pkg list | grep demo
- 接下来通过运行找到可以使用的可用可执行文件:
- ros2 pkg executables demo_nodes_cpp
- 运行这些可执行文件之一 - 发布者:
- ros2 run demo_nodes_cpp talker
观察 ROS 2 运行时系统到目前为止的状态...
C:\\ros_ws>ros2 run demo_nodes_cpp talker
[INFO] [1627943926.877007500] [talker]: Publishing: 'Hello World: 1'
[INFO] [1627943927.881649300] [talker]: Publishing: 'Hello World: 2'
[INFO] [1627943928.882885900] [talker]: Publishing: 'Hello World: 3'
[INFO] [1627943929.875583600] [talker]: Publishing: 'Hello World: 4'
[INFO] [1627943930.888021900] [talker]: Publishing: 'Hello World: 5'
[INFO] [1627943931.887165700] [talker]: Publishing: 'Hello World: 6'
[INFO] [1627943932.871433900] [talker]: Publishing: 'Hello World: 7'
[INFO] [1627943933.875443600] [talker]: Publishing: 'Hello World: 8'
[INFO] [1627943934.874058100] [talker]: Publishing: 'Hello World: 9'
[INFO] [1627943935.871674100] [talker]: Publishing: 'Hello World: 10'
[INFO] [1627943936.877424900] [talker]: Publishing: 'Hello World: 11'
[INFO] [1627943937.883121100] [talker]: Publishing: 'Hello World: 12'
[INFO] [1627943938.874110100] [talker]: Publishing: 'Hello World: 13'
[INFO] [1627943939.879654100] [talker]: Publishing: 'Hello World: 14'
[INFO] [1627943940.886396700] [talker]: Publishing: 'Hello World: 15'
[INFO] [1627943941.875835300] [talker]: Publishing: 'Hello World: 16'
[INFO] [1627943942.876223700] [talker]: Publishing: 'Hello World: 17'
[INFO] [1627943943.872789300] [talker]: Publishing: 'Hello World: 18'
[INFO] [1627943944.884865900] [talker]: Publishing: 'Hello World: 19'
[INFO] [1627943945.878195100] [talker]: Publishing: 'Hello World: 20'
[INFO] [1627943946.881809600] [talker]: Publishing: 'Hello World: 21'
[INFO] [1627943947.874536300] [talker]: Publishing: 'Hello World: 22'
[INFO] [1627943948.871196200] [talker]: Publishing: 'Hello World: 23'
检查工具 - 发布者
节点
- 查看当前正在运行的节点:
- ros2 node list
- 要查看有关节点的更多信息:
- ros2 node info <node_name>
主题
- 要查看当前正在运行的主题及其消息类型:
- ros2 topic list -t
- 要获取有关主题的更多信息(Foxy:详细输出):
- ros2 topic info -v <topic_name>
- 要查看关于某个主题发布的内容:
- ros2 topic echo <topic_name>
订阅者
- 启动一个监听这个主题的订阅者:
- ros2 run demo_nodes_cpp listener
- 订阅者在终端上回放它在该主题上听到的内容。
- 再次检查正在运行的节点和主题查看更新。
检查工具 - 终端发布
- 发布者也可以从终端创建。
- ros2 topic pub <topic_nam> <msg_type> "{<field1_key>: <field1_value>, <field2_key>: <field2_value>, ...}"
- 首先必须正确理解消息类型。
- ros2 topic list -t
- /topic 的消息类型是:
- std_msgs/msg/String
- 更多信息:
- ros2 interface show std_msgs/msg/String
- 它只有一个字段——数据 data ——字符串类型 string 。
C:\\ros_ws\\webots_ros2>ros2 interface show std_msgs/msg/String
# This was originally provided as an example message.
# It is deprecated as of Foxy
# It is recommended to create your own semantically meaningful message.
# However if you would like to continue using this please use the equivalent in example_msgs.string data
- 消息正文是字典类型,消息字段具有键值对。
- 使用 ros2 interface proto std_msgs/msg/String 获取要使用的字典示例。
C:\\ros_ws\\webots_ros2>ros2 interface proto std_msgs/msg/String
"data: ''
"
- 请注意,空格间距非常重要!!
- 以 10 Hz 的速率在 /topic 上生成发布者:
- ros2 topic pub -r 10 /topic std_msgs/msg/String "data: "Hello""
启动文件
- 从一个终端启动多个节点的方法:
- ros2 launch <package_name> <launch_file_name.launch> <<optional_command_line_arguments>>
- 从一个点同时运行订阅者和发布者:
- ros2 launch demo_nodes_cpp talker_listener.launch.py
- 使用检查工具来验证结果并进行比较。
C:\\ros_ws>ros2 launch demo_nodes_cpp talker_listener.launch.py
[INFO] [launch]: All log files can be found below C:\\Users\\zhangrelay\\.ros/log\\2021-08-03-15-15-10-692151-LAPTOP-5REQ7K1L-9852
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [talker.EXE-1]: process started with pid [9608]
[INFO] [listener.EXE-2]: process started with pid [16824]
[talker.EXE-1] [INFO] [1627974912.070719100] [talker]: Publishing: 'Hello World: 1'
[listener.EXE-2] [INFO] [1627974912.086495000] [listener]: I heard: [Hello World: 1]
[talker.EXE-1] [INFO] [1627974913.090884400] [talker]: Publishing: 'Hello World: 2'
[listener.EXE-2] [INFO] [1627974913.122120700] [listener]: I heard: [Hello World: 2]
[talker.EXE-1] [INFO] [1627974914.085433000] [talker]: Publishing: 'Hello World: 3'
[listener.EXE-2] [INFO] [1627974914.101354000] [listener]: I heard: [Hello World: 3]
[talker.EXE-1] [INFO] [1627974915.084586200] [talker]: Publishing: 'Hello World: 4'
[listener.EXE-2] [INFO] [1627974915.100422500] [listener]: I heard: [Hello World: 4]
服务
简介
- 用于在节点之间进行简短但有保证的消息传输。(更可靠)
- 它涉及两个实体 - 提供服务的服务器端和使用此服务的客户端。
- 像典型节点一样启动这些:
ros2 run demo_nodes_cpp add_two_ints_server
ros2 run demo_nodes_cpp add_two_ints_client
- 主题是数据流方式传输,而服务则每次调用进行一次交换。
检查工具
- 列出服务器提供的服务:
- ros2 service list -t
- 演示服务器提供的服务 - add_two_ints
- 通过运行一个客户端节点来调用这个服务
- 也可以通过 CLI 执行此操作。
- 语法:
- ros2 service call <service_name> <service_type_name> "{<field1_key>: <field1_value>, <field2_key>: <field2_value>, ...}"
- 要了解服务类型:
- ros2 interface list -s(-s 表示服务。另外,-m、-a)
- ros2 interface show <service_type_name>
- 示例:
- ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 5, b: 6}"
- 请注意,空格间距非常重要!!
C:\\ros_ws\\webots_ros2>ros2 run demo_nodes_cpp add_two_ints_server
[INFO] [1627975189.301810900] [add_two_ints_server]: Incoming request
a: 2 b: 3
[INFO] [1627975198.975293000] [add_two_ints_server]: Incoming request
a: 2 b: 3
[INFO] [1627975216.994539900] [add_two_ints_server]: Incoming request
a: 5 b: 6
行动和参数
行动
- 对于需要反馈并涉及较长任务很有用,具有行动服务器端和行动客户端。
- 接口有 3 个部分 - 目标、反馈和结果。
- 典型序列 - 发布目标,观察反馈并在执行完成/中止时接收结果
- 使用 ros2 action [..] 命令进行交互。 (ROS2 中的新功能)
- 类似于 ros2 topic [..]
更多内容,参考官方文档。
参数
- 用于存储运行时配置信息的值。
- 例如:激光扫描设备、相机标定、导航障碍层宽度等...
- 参数按每个节点存储,可以通过服务访问
- 与具有由 rosmaster 管理的专用参数服务器的 ROS1 不同。
- 使用 ros2 param [..] 命令从 CLI 进行交互。
更多内容,参考官方文档。
工具
工具 - RQT
- 基于 QT 的 ROS GUI 工具。
- 提供多种用途的插件:
- 节点图监视器,
- 服务调用者/类型查看器,
- 主题发布者/监听器/类型查看器,
- 图像视图,
- 情节等....
- 类似于 ROS1,但仍然缺少一些插件 - 目前还不如ROS1完善哦
- rqt & 启动默认主窗口(& -> 分离进程)或 ros2 run rqt_<plugin> rqt_<plugin> 启动特定插件
- 加载插件 : Plugins -> <Library> -> <Plugin>
- 监控节点图的示例 Plugins -> Introspection -> Node Graph
- 根据需要探索和使用所有工具。
工具——ros2doctor
功能包医生本地化!
- 识别系统中的问题(类似于roswtf,可以使用wtf作为别名)
- 分析 ROS2 安装以及运行系统,显示报告
- ros2 doctor - 显示关于当前状态的警告和错误
- 悬而未决的主题、不正确的配置、缺少系统文件等......
- ros2 doctor -r - 安装的完整技术报告
- ros2 doctor hello - 检查多个主机之间的网络连接
工具——ros2bag
- 与 ROS1 中的 rosbag 具有相同的功能和类似的 API
- 现在作为 > Foxy 核心的一部分安装
- 将一段时间内在主题上发布的消息记录到文件中。 稍后以几乎相同的时间特征重播。
- 存储为 .db3 + metadata.yaml (ROS2)而不是单个 *.bag (ROS1)文件。
工具——ros1_bridge
- “双宿主(dual homed)”包 - 同时支持 ROS1 和 ROS2。
- ROS1 <=主题/服务=> ros1_bridge <=主题/服务=> ROS2
- 二进制包安装支持默认的msg/srv类型,源码安装可以扩展自定义类型。
Shell 1 $ Source ROS1, Run ROS1 stuff
Shell 2 $ Source ROS1, Source ROS2, Run bridge
Shell 3 $ Source ROS2, Run ROS2 stuff
魔法般神奇(⊙o⊙)?!
-End-
以上是关于ROS2极简总结-命令行接口基础的主要内容,如果未能解决你的问题,请参考以下文章