1. 程式人生 > >ROS小烏龜案例 學習筆記(三)

ROS小烏龜案例 學習筆記(三)

啟動roscare

$ roscore

獲得節點資訊:

獲得引數跟詳細解釋:

$ rosnode <param> -h

獲取正在執行的節點的相關資訊:

$ rosnode list

啟動新節點:

$ rosrun turtlesim turtlesim_node

檢視節點資訊,可以獲得很多用於程式除錯的資訊:

$ rosnode info /turtlesim

輸出

--------------------------------------------------------------------------------
Node [/turtlesim]
Publications:
 * /rosout [rosgraph_msgs/Log]
 * /turtle1/color_sensor [turtlesim/Color]
 * /turtle1/pose [turtlesim/Pose]

Subscriptions:
 * /turtle1/cmd_vel [unknown type]

Services:
 * /clear
 * /kill
 * /reset
 * /spawn
 * /turtle1/set_pen
 * /turtle1/teleport_absolute
 * /turtle1/teleport_relative
 * /turtlesim/get_loggers
 * /turtlesim/set_logger_level


contacting node http://ubuntu:33525/ ...
Pid: 3209
Connections:
 * topic: /rosout
    * to: /rosout
    * direction: outbound
    * transport: TCPROS

使用箭頭控制海龜移動指令:

$ rosrun turtlesim turtle_teleop_key

檢視節點teleop_turtle資訊

[email protected]:~$ rosnode info /teleop_turtle
--------------------------------------------------------------------------------
Node [/teleop_turtle]
Publications:
 * /rosout [rosgraph_msgs/Log]
 * /turtle1/cmd_vel [geometry_msgs/Twist]

Subscriptions: None

Services:
 * /teleop_turtle/get_loggers
 * /teleop_turtle/set_logger_level


contacting node http://ubuntu:42629/ ...
Pid: 3349
Connections:
 * topic: /rosout
    * to: /rosout
    * direction: outbound
    * transport: TCPROS
 * topic: /turtle1/cmd_vel
    * to: /turtlesim
    * direction: outbound
    * transport: TCPROS

使用命令檢視主清單:

執行一下命令列使用箭頭鍵檢視訊息產生時傳送的資料:

$ rostopic echo /turtle1/cmd_vel

檢視主題傳送的訊息型別:

$ rostopic type /turtle1/cmd_vel

顯示

geometry_msgs/Twist

檢視訊息欄位:

$ rosmsg show geometry_msgs/Twist

已知上述資訊,可通過rostopic pub [topic] [msg type] [args]命令直接釋出主題:

rostopic pub -r 1 /turtle1/cmd_vel geometry_msgs/Twist '{linear: {x: 2.0, y: 0.0, z: 0.0},angular:{x: 0.0, y: 0.0,z: 2.0}}'
登入谷歌搜尋rostopic可以檢視詳細資訊

服務

使用rosservice工具與服務互動

rosservice args/service 輸出服務引數
rosservice call/service 根據命令列引數呼叫服務
rosservice find msg-type 根據服務型別查詢服務
rosservice info /service 輸出服務資訊
rosservice list 列出服務清單
rosservice type/service 輸出服務型別
rosservice uri/service 輸出ROSRPC URI服務

[email protected]:~$ rosservice list

輸出:
/clear
/kill
/reset
/rosout/get_loggers
/rosout/set_logger_level
/rostopic_4742_1536450656570/get_loggers
/rostopic_4742_1536450656570/set_logger_level
/rostopic_4766_1536450738742/get_loggers
/rostopic_4766_1536450738742/set_logger_level
/rostopic_4786_1536450768102/get_loggers
/rostopic_4786_1536450768102/set_logger_level
/rostopic_4803_1536450811800/get_loggers
/rostopic_4803_1536450811800/set_logger_level
/rostopic_4818_1536450851920/get_loggers
/rostopic_4818_1536450851920/set_logger_level
/spawn
/teleop_turtle/get_loggers
/teleop_turtle/set_logger_level
/turtle1/set_pen
/turtle1/teleport_absolute
/turtle1/teleport_relative
/turtlesim/get_loggers
/turtlesim/set_logger_level
檢視某個服務型別,例

$ rosservice type /clear

輸出

std_srvs/Empty
呼叫服務,rosservice call [service] [args]命令:

$ rosservice call /clear

執行上邊命令之後,turtlesim 視窗中,小海龜移動產生的線條消失

[email protected]:~$ rosservice type /spawn | rossrv show
float32 x
float32 y
float32 theta
string name
---
string name
 

[email protected]:~$ rosservice type /spawn
turtlesim/Spawn
輸入下邊命令

[email protected]:~$ rossrv show turtlesim/Spawn
float32 x
float32 y
float32 theta
string name
---
string name
通過這些欄位,可以知道如何呼叫服務.

[email protected]:~$ rosservice call /spawn 3 3 0.2 "new_turtle"
name: "new_turtle"
結果圖:

引數伺服器使用:

引數伺服器儲存所有節點均可訪問的共享資料,ROS中用來管理引數伺服器的工具稱為rosparam

rosparam set parameter value 設定引數值
rosparam get parameter 獲取引數值
rosparam load file 從檔案載入引數
rosparam dump file 將引數儲存到一個檔案
rosparam delete parameter 刪除引數
rosparam list 列出了所有引數名

例如:
[email protected]:~$ rosparam list

輸出:
/background_b
/background_g
/background_r
/rosdistro
/roslaunch/uris/host_ubuntu__40827
/rosversion
/run_id
上邊的背景(backgrund)是turtlesim節點引數,這些引數可以改變視窗的顏色

讀取某個,可使用get引數,

$ rosparam get /background_b

為了設定一個新值可以使用set引數:

[email protected]:~$ rosparam set /background_b 1000
使用rosparam dump [file_name]來保持引數伺服器:

$ rosparam dump save.yaml

使用rosparm load [file_name] [namespace]向引數伺服器載入新的資料檔案:

$ rosparam load load.yaml namespace