環(huán)境變量
啟動前要檢查環(huán)境變量:ROS_DOMAIN_ID
和 ROS_LOCALHOST_ONLY
。如果通信時PIN不同,應(yīng)該首先考慮是不是環(huán)境變量設(shè)置錯誤。 Configuring environment
記得source一下ros2。
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
turtlesim和rqt
Turtlesim 是一款用于學(xué)習(xí) ROS2 的輕量級模擬器。 它說明了 ROS 2 在最基本的層面上做了什么,讓您了解以后將如何處理真實機器人或機器人模擬。
ros2 工具是用戶管理、內(nèi)省和與 ROS 系統(tǒng)交互的方式。 它支持針對系統(tǒng)及其操作的不同方面的多個命令。 人們可能會使用它來啟動節(jié)點、設(shè)置參數(shù)、收聽話題等等。 ros2工具是核心 ROS2 安裝的一部分。
rqt 是 ROS2 的圖形用戶界面 (GUI)工具。 在 rqt 中完成的所有操作都可以在命令行上完成,但 rqt 提供了一種更用戶友好的方式來操作 ROS2 元素。
以初始狀態(tài)打開rqt
rqt每次啟動都會以上次關(guān)閉時的狀態(tài)打開,如果rqt界面出現(xiàn)了什么異常卡頓(比如某個頁面無法點擊,或者無法滑動),可以通過下面命令像第一次打開rqt一樣。
rqt --clear-config
node
啟動節(jié)點
ros2 run <package_name> <executable_name>
查看節(jié)點列表
ros2 node list
查看節(jié)點更多信息
可以看到節(jié)點的話題信息(subscribers and publishers),服務(wù)信息(services), 動作信息(action servers and action clients)。
ros2 node info <node_name> "一般是/node_name"
命令行參數(shù) --ros-args
ROS的命令行參數(shù)
--remap
ros2 run some_package some_ros_executable --ros-args --remap foo:=bar
ros2 run some_package some_ros_executable --ros-args -r foo:=bar
ros2 run some_package some_ros_executable --ros-args -r some_node:foo:=bar
--param
ros2 run some_package some_ros_executable --ros-args --param string_param:=test
ros2 run some_package some_ros_executable --ros-args -p string_param:=test
ros2 run some_package some_ros_executable --ros-args -p some_node:string_param:=test
--params-file
ros2 run some_package some_ros_executable --ros-args --params-file params_file.yaml
<params_file.yaml>
node0_name:
ros__parameters:
param0_name: param0_value
...
paramN_name: paramN_value
...
nodeM_name:
ros__parameters:
...
* 匹配由斜線劃分的單個令牌。** 匹配由斜線劃分的零或更多令牌。不允許部分匹配(例如foo*)。
例如:
/**:
ros__parameters:
string_param: foo
將在所有節(jié)點上設(shè)置參數(shù)string_param.
/**/some_node:
ros__parameters:
string_param: foo
將在任何名稱空間中的some_node上設(shè)置parameter string_param.
/foo/*:
ros__parameters:
string_param: foo
將在名稱空間 /foo下的任何節(jié)點上設(shè)置參數(shù)string_param(例如只匹配到foo/foo1 而不會匹配到foo/foo1/foo11).
topic
話題可以理解為是ROS中節(jié)點交換消息的總線。
在圖中查看節(jié)點和話題之間的結(jié)構(gòu):
rqt_graph
圓的是節(jié)點,方的是話題。
話題列表
ros2 topic list
話題類型
ros2 topic type <topic_name>
話題列表,附加話題類型
類型其實就是消息結(jié)構(gòu)(消息屬于哪個類)。
ros2 topic list -t
根據(jù)類型查找話題名
ros2 topic find <type_name>
查看話題發(fā)布的數(shù)據(jù)
ros2 topic echo <topic_name>
注意:數(shù)據(jù)只有在發(fā)布時終端才可以看到。
此命令會發(fā)布一個新話題,話題名字類似于/_ros2cli_26646
,在rqt_graph中取消選中Debug即可看見。
查看話題的詳細(xì)信息
可以查看話題的消息類型,以及其被多少個節(jié)點發(fā)布消息和被多少個節(jié)點訂閱。
ros2 topic info <topic_name>
查看類型的詳細(xì)信息
這里的類型不只是話題的類型可以被查看,其他類型也可以使用此方法看到具體的結(jié)構(gòu)信息。
ros2 interface show <msg type>
查看所有類型的列表
ros2 interface list
以ros2 interface show geometry_msgs/msg/Twist
為例,顯示的類型如下:
# This expresses velocity in free space broken into its linear and angular parts.
Vector3 linear
float64 x
float64 y
float64 z
Vector3 angular
float64 x
float64 y
float64 z
其中,linear
和angular
可以理解為一個變量,Vector3
是變量的類型,x,y,z
是linear/angular
的成員,也是變量。
給話題發(fā)布消息(在命令行)
'<args>'
參數(shù)需要以yaml語法作為輸入。
ros2 topic pub <topic_name> <msg_type> '<args>'
'<args>'
使用單引號和雙引號是等價的,都可以使用。
例如:ros2 topic pub --once /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}}"
(格式和echo顯示的結(jié)果是一樣的)
-
--once
是一個可選的參數(shù),意為“發(fā)布一條消息然后退出”。 -
--rate 1
:以1Hz的頻率持續(xù)發(fā)布消息(為默認(rèn)參數(shù)設(shè)置)。 - <args>是yaml格式的,每一個冒號后面的空格都不可以省略。
在rqt-graph中顯示如下:
帶有時間戳的消息
如果消息的header為完整的標(biāo)題類型std_msgs/msg/Header
(有沒有可以通過interface show 查看),設(shè)置為auto
會自動填充為當(dāng)前時間。
ros2 topic pub /pose geometry_msgs/msg/PoseStamped '{header: "auto", pose: {position: {x: 1.0, y: 2.0, z: 3.0}}}'
如果消息中的類型為builtin_interfaces/msg/Time
,可以設(shè)置now
ros2 topic pub /reference sensor_msgs/msg/TimeReference '{header: "auto", time_ref: "now", source: "dumy"}'
sensor_msgs/msg/TimeReference
結(jié)構(gòu)如下:
查看話題發(fā)布的頻率Hz
ros2 topic hz <topic_name>
service
服務(wù)是節(jié)點的另一種通信方法,基于呼叫和響應(yīng)(call-and-response)模型,話題是publisher-subscriber模型。服務(wù)只有在客戶端專門調(diào)用時才提供數(shù)據(jù)。
服務(wù)列表
ros2 service list
服務(wù)類型
服務(wù)類型的定義與話題類型類似,但是服務(wù)類型有兩個部分:一條是請求的消息,另一條是響應(yīng)的消息。
ros2 service type <service_name>
服務(wù)列表,附加服務(wù)類型
ros2 service list -t
根據(jù)類型查找服務(wù)名
ros2 service find <type_name>
服務(wù)類型結(jié)構(gòu)
查看方法:
ros2 interface show <type_name>
結(jié)構(gòu):
request structure
---
response structure
-
---
是request structure 和 response structure 的分隔符。
調(diào)用服務(wù)
ros2 service call <service_name> <service_type> <arguments>
例如:ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: ''}"
會返回:
requester: making request: turtlesim.srv.Spawn_Request(x=2.0, y=2.0, theta=0.2, name='')
response:
turtlesim.srv.Spawn_Response(name='turtle2')
parameters
參數(shù)是節(jié)點(node)的配置值,每個節(jié)點都維護(hù)自己的參數(shù),也有共享參數(shù)(和C++很像,有私有成員,也有靜態(tài)成員)。
參數(shù)列表
ros2 param list
獲取參數(shù)值
ros2 param get <node_name> <parameter_name>
設(shè)置參數(shù)值
ros2 param set <node_name> <parameter_name> <value>
獲取節(jié)點所有參數(shù)
ros2 param dump <node_name>
如果想將參數(shù)存入到文件中,可以使用linux中的>
運算符:
ros2 param dump /turtlesim > turtlesim.yaml
導(dǎo)出為yaml結(jié)構(gòu),以后使用時可以直接導(dǎo)入。
為節(jié)點加載參數(shù)
ros2 param load <node_name> <parameter_file>
例如:ros2 param load /turtlesim turtlesim.yaml
需要注意的是只讀的參數(shù)只能在啟動時進(jìn)行修改,所以那些參數(shù)將不會被修改。
啟動時指定節(jié)點參數(shù)
ros2 run <package_name> <executable_name> --ros-args --params-file <file_name>
例如:ros2 run turtlesim turtlesim_node --ros-args --params-file turtlesim.yaml
這將使得所有參數(shù)根據(jù)給定的文件更新,具體細(xì)節(jié)見ROS的命令行參數(shù)。
action
action基于topic和service。它的功能類似于service。但是action的特點是:動作不僅可以被取消,還提供了穩(wěn)定的反饋(service只能返回一個單獨的回復(fù)response)。
action由三個部分組成:目標(biāo),反饋和結(jié)果。
操作使用client-server模型,類似于Publisher-Subscriber模型。 “action client”節(jié)點將目標(biāo)發(fā)送到“action server”節(jié)點,該節(jié)點確認(rèn)目標(biāo)并返回反饋和結(jié)果。
當(dāng)調(diào)用:
ros2 run turtlesim turtle_teleop_key
終端會顯示:
Reading from keyboard
---------------------------
Use arrow keys to move the turtle.
Use G|B|V|C|D|E|R|T keys to rotate to absolute orientations. 'F' to cancel a rotation.'Q' to quit.
Use arrow keys to move the turtle.對應(yīng)topic,
Use G|B|V|C|D|E|R|T keys to rotate to absolute orientations. ‘F’ to cancel a rotation. ‘Q’ to quit.對應(yīng)action。
每次按下這些按鍵時,都會將目標(biāo)發(fā)送到/turtlesim節(jié)點的action服務(wù)器上。一旦烏龜旋轉(zhuǎn)完成,烏龜節(jié)點會返回一個信息,比如:[INFO] [turtlesim]: Rotation goal completed successfully
,實際上不同的動作對應(yīng)不同的反饋信息和結(jié)果。
對于動作而言,client可以取消目標(biāo)(按下F
),server也可以取消目標(biāo)(連續(xù)按下D G
,server會自動放棄第一個目標(biāo),但不是所有的server都是這樣處理)。
通信方式
執(zhí)行ros2 node info /turtlesim
可以看到最下方關(guān)于action的信息:
Action Servers:
/turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
Action Clients:
這說明/turtlesim
節(jié)點會接受/turtle1/rotate_absolute
提供的目標(biāo)并且給它提供反饋。
執(zhí)行ros2 node info /teleop_turtle
可以看到最下方關(guān)于action的信息:
Action Servers:
Action Clients:
/turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
這說明/teleop_turtle
節(jié)點會發(fā)送目標(biāo)給/turtle1/rotate_absolute
。
動作列表
ros2 action list
動作列表,附加類型
ros2 action list -t
動作更多信息
會返回動作的客戶端和服務(wù)端節(jié)點。
ros2 action info /turtle1/rotate_absolute
動作類型結(jié)構(gòu)
以turtlesim/action/RotateAbsolute
類型為例:
ros2 interface show turtlesim/action/RotateAbsolute
返回:
# The desired heading in radians
float32 theta
---
# The angular displacement in radians to the starting position
float32 delta
---
# The remaining rotation in radians
float32 remaining
分成了三部分,其中---
是分隔符,第一部分是目標(biāo),第二部分是結(jié)果,第三部分是反饋信息。
發(fā)送動作目標(biāo)
ros2 action send_goal <action_name> <action_type> '<values>'
<values>需要采用YAML格式。
例如:ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}"
發(fā)送動作目標(biāo),要求有穩(wěn)定的反饋
ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: -1.57}" --feedback
會返回:
Sending goal:
theta: -1.57
Goal accepted with ID: e6092c831f994afda92f0086f220da27
Feedback:
remaining: -3.1268222332000732
Feedback:
remaining: -3.1108222007751465
…
Result:
delta: 3.1200008392333984
Goal finished with status: SUCCEEDED
其中Feedback會持續(xù)提供反饋信息,直到目標(biāo)達(dá)到。
topic,service,action的區(qū)別
通信機制 | 描述 | 使用場景 |
---|---|---|
Topic | 一種異步消息隊列,分為publisher(發(fā)送信息)和subscriber(接受消息) | 處理連續(xù)數(shù)據(jù)流,多對多的形式 |
Service | 一種同步請求/響應(yīng)交互模式(發(fā)送一次,反饋/響應(yīng)一次) | 一部分定義請求部分,一部分定義回應(yīng)部分 |
Action | 一種異步請求/響應(yīng)交互模式,帶有反饋機制(發(fā)送一個目標(biāo),執(zhí)行過程中連續(xù)反饋,直到達(dá)到目標(biāo)) | 需要反饋/狀態(tài)跟蹤,需要花費大量時間,可以被中斷 |
使用 rqt_console 查看特定日志消息
啟動
ros2 run rqt_console rqt_console
過濾某些級別的消息
只查看級別大于等于WARN的日志消息。
ros2 run turtlesim turtlesim_node --ros-args --log-level WARN #大小寫都可以
ROS2的日志級別按嚴(yán)重性排序:
Fatal
Error
Warn
Info
Debug
默認(rèn)級別是Info
,因此看不到級別為Debug
的消息。
如果設(shè)置顯示的消息級別是Warn
,那么Info
和Debug
級別的消息都會被顯示。
啟動節(jié)點(Launching nodes)
使用命令行工具一次啟動多個節(jié)點。
ros2 launch turtlesim multisim.launch.py
這會啟動兩個turtlesim node,使用ros2 topic list
可以看到兩個node分別為turtlesim1
和turtlesim2
:
/parameter_events
/rosout
/turtlesim1/turtle1/cmd_vel
/turtlesim1/turtle1/color_sensor
/turtlesim1/turtle1/pose
/turtlesim2/turtle1/cmd_vel
/turtlesim2/turtle1/color_sensor
/turtlesim2/turtle1/pose
因此我們可以開兩個終端來分別控制兩個節(jié)點:
ros2 topic pub /turtlesim1/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}}"
ros2 topic pub /turtlesim2/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}}"
注意:
也可以使用XML和YAML來創(chuàng)建啟動文件。 具體細(xì)節(jié)可以查看Using Python, XML, and YAML for ROS 2 Launch Files
對于launch的更多信息可以查看ROS2 launch tutorials
記錄和播放數(shù)據(jù)
記錄有關(guān)topic
的數(shù)據(jù),因此可以隨時重播和檢查。
記錄單個topic
ros2 bag record <topic_name>
例如:ros2 bag record /turtle1/cmd_vel
這會根據(jù)時間戳來創(chuàng)建bag文件的名稱,想要指定文件名稱可以用-o
來指定。
記錄多個topic
記錄多個topic時直接輸入多個topic名稱,用空格隔開。
ros2 bag record -o subset <topic1_name> <topic2_name>
-o
選項允許為bag文件選擇一個唯一的名稱。這里的subset
就是要創(chuàng)建的文件目錄名稱。
例如:ros2 bag record -o subset /turtle1/cmd_vel /turtle1/pose
查看記錄信息
ros2 bag info <bag_file_name>
例如:ros2 bag info subset
文章來源:http://www.zghlxwxcb.cn/news/detail-812891.html
記錄回放
ros2 bag play <bag_file_name>
有關(guān)ros bag
更詳細(xì)的信息在這里找到:https://github.com/ros2/rosbag2。文章來源地址http://www.zghlxwxcb.cn/news/detail-812891.html
到了這里,關(guān)于ros2學(xué)習(xí)筆記-CLI工具,記錄命令對應(yīng)操作。的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!