ROS入門筆記(四):ROS實踐(小海龜仿真)— ROS Topics
1 基礎知識
1.1 運行ROS Master所在主機的資源地址
1.2 ROS常用命令
1.2.1 查看所有以ros開頭的命令
1.2.2 命令的快捷方式
1.3 重要命令
2 ROS Topics
2.1 使用rqt_graph
2.2 rostopic
2.2.1 rostopic echo
2.2.2 rostopic list
2.3 ROS Messages
2.3.1 rostopic type
2.3.2 rostopic pub
2.3.3 rostopic hz
3 rqt_plot
4 rqt_plot出現的問題(kinetic)
4.1 問題
4.2 原因
4.3 解決方法
4.4 deb文件安裝方法
1 基礎知識
1.1 運行ROS Master所在主機的資源地址
ROS是分布式的軟件框架,ROS Master可能分布在某一台主機上,ROS Master在整個系統中只能存在一個,其他的很多主機必須要知道當前的ROS Master在什么位置。
- 退出ROS Master:
Ctrl C
1.2 ROS常用命令
命令 |
作用 |
catkincreatepkg |
創建功能包 |
rospack |
獲取功能包的信息 |
catkin_make |
編譯工作空間中的功能包 |
rosdep |
自動安裝功能包依賴的其他包 |
roscd |
功能包目錄跳轉 |
roscp |
拷貝功能包中的文件 |
rosed |
編輯功能包中的文件 |
rosrun |
運行功能包中的可執行文件 |
roslaunch |
運行啟動文件 |
1.2.1 查看所有以ros開頭的命令
- 在終端中輸入ros,按Tab鍵
1.2.2 命令的快捷方式
- rosnode的使用方法?
rosnode+空格+按下Tab鍵 # 查看rosnode支持的配置參數
- rosnode幫助信息:
rosnode --help
1.3 重要命令
1)查看當前系統當中存在的節點,終端輸入:
$ rosnode list
2)查看某一具體節點的具體信息,終端輸入:
rosnode info /
2 ROS Topics
- turtlesim_node節點和turtle_teleop_key節點之間是通過一個ROS話題來互相通信的。
- turtle_teleop_key在一個話題上發布按鍵輸入消息,而turtlesim則訂閱該話題以接收該消息。下面讓我們使用rqt_graph來顯示當前運行的節點和話題。
2.1 使用rqt_graph
rqt_graph能夠創建一個顯示當前系統運行情況的動態圖形。rqt_graph是rqt程序包中的一部分。
方法:新打開一個終端,輸入以下命令:(前提:必須保證小海龜三步走步驟開啟)
$ rqt_graph
- 鼠標放在/turtle1/command_velocity上方,相應的ROS節點(藍色和綠色)和話題(紅色)就會高亮顯示。
- turtlesim_node和turtle_teleop_key節點正通過一個名為 /turtle1/cmd_vel的話題來互相通信。
2.2 rostopic
rostopic命令工具能讓你獲取有關ROS話題的信息。
$ rostopic -help # 使用幫助選項查看rostopic的子命令
rostopic is a command-line tool for printing information about ROS Topics. Commands: rostopic bw display bandwidth used by topic rostopic delay display delay of topic from timestamp in header rostopic echo print messages to screen rostopic find find topics by type rostopic hz display publishing rate of topic rostopic info print information about active topic rostopic list list active topics rostopic pub publish data to topic rostopic type print topic or field type Type rostopic <command> -h for more detailed usage, e.g. 'rostopic echo -h'
2.2.1 rostopic echo
rostopic echo可以顯示在某個話題上發布的數據。
1)查看echo子命令需要的參數
$ rostopic echo -h
Usage: rostopic echo [options] /topic Options: -h, --help show this help message and exit -b BAGFILE, --bag=BAGFILE echo messages from .bag file -p echo in a plotting friendly format -w NUM_WIDTH fixed width for numeric values --filter=FILTER-EXPRESSION Python expression to filter messages that are printed. Expression can use Python builtins as well as m (the message) and topic (the topic name). --nostr exclude string fields --noarr exclude arrays -c, --clear clear screen before printing next message -a, --all display all message in bag, only valid with -b option -n COUNT number of messages to echo --offset display time as offsets from current time (in seconds)
2)用法
$ rostopic echo [topic] # 監聽某一話題的具體內容 $ rostopic echo /turtle1/cmd_vel
# 一個新終端中看一下turtle_teleop_key節點在/turtle1/cmd_vel話題上發布的數據
- 你可能看不到任何東西因為現在還沒有數據發布到該話題上。接下來我們通過按下方向鍵使turtle_teleop_key節點發布數據。
- 記住如果turtle沒有動起來的話就需要你重新選中turtle_teleop_key節點運行時所在的終端窗口。
現在再看一下rqt_graph(需要刷新一下ROS graph)。正如你所看到的,rostopic echo(綠色顯示部分)現在也訂閱了/turtle1/cmd_vel話題。
2.2.2 rostopic list
1)rostopic list能夠列出所有當前訂閱和發布的話題。
$ rostopic list #列出所有當前訂閱和發布的話題
2)查看list子命令需要的參數
$ rostopic list -h # 查看幫助信息
Usage: rostopic list [/namespace] Options: -h, --help show this help message and exit -b BAGFILE, --bag=BAGFILE list topics in .bag file -v, --verbose list full details about each topic -p list only publishers -s list only subscribers --host group by host name
3)在rostopic list中使用verbose選項
$ rostopic list -v # 顯示出有關所發布和訂閱的話題及其類型的詳細信息
2.3 ROS Messages
話題之間的通信是通過在節點之間發送ROS消息實現的。對於發布器(turtle_teleop_key)和訂閱器(turtulesim_node)之間的通信,發布器和訂閱器之間必須發送和接收相同類型的消息。這意味着話題的類型是由發布在它上面的消息類型決定的。使用rostopic type命令可以查看發布在某個話題上的消息類型。
2.3.1 rostopic type
rostopic type #查看所發布話題的消息類型。
1)用法
rostopic type [topic]
2)例子
$ rostopic type /turtle1/cmd_vel
看到:
geometry_msgs/Twist
3)rosmsg命令來查看消息的詳細情況
$ rosmsg show geometry_msgs/Twist
知道了turtlesim節點所期望的消息類型,接下來我們就可以給turtle發布命令了。
2.3.2 rostopic pub
rostopic pub可以把數據發布到當前某個正在廣播的話題上。
1)用法
rostopic pub [topic] [msg_type] [args]
2)示例
$ rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
# 以上命令會發送一條消息給turtlesim,告訴它以2.0大小的線速度和1.8大小的角速度開始移動。
這是一個非常復雜的例子,因此讓我們來詳細分析一下其中的每一個參數。
rostopic pub # 這條命令將會發布消息到某個給定的話題。
-1 #(單個破折號)這個參數選項使rostopic發布一條消息后馬上退出。
/turtle1/cmd_vel # 這是消息所發布到的話題名稱。
geometry_msgs/Twist # 這是所發布消息的類型。
--
#(雙破折號)這會告訴命令選項解析器接下來的參數部分都不是命令選項。這在參數里面包含有破折號-(比如負號)時是必須要添加的。
'[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]' # 在一個geometry_msgs/Twist消息里面包含有兩個浮點型元素:linear和angular。 # 在本例中,2.0是linear的值,1.8是angular的值。這些參數其實是按照YAML語法格式編寫的,這在YAML文檔中有更多的描述。
- 目前turtle已經停止移動了。因為turtle需要一個穩定的頻率為1Hz的命令流來保持移動狀態。
- 使用rostopic pub -r命令來發布一個穩定的命令流。
$ rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]' # 以1Hz的頻率發布速度命令到速度話題上。
2.3.3 rostopic hz
rostopic hz # 查看數據發布的頻率。
1)用法
rostopic hz [topic]
2)例子
$ rostopic hz /turtle1/pose # 查看turtlesim_node發布/turtle/pose時有多快
- turtlesim正以大約62Hz的頻率發布數據給turtle。
- 結合rostopic type和rosmsg show命令來獲取關於某個話題的更深層次的信息
rostopic type /turtle1/cmd_vel | rosmsg show
3 rqt_plot
- rqt_plot命令可以實時顯示一個發布到某個話題上的數據變化圖形。這里我們將使用rqt_plot命令來繪制正在發布到/turtle1/pose話題上的數據變化圖形。
- 首先,在一個新終端中運行rqt_plot命令:
$ rosrun rqt_plot rqt_plot
- 會彈出一個新窗口,在窗口左上角的一個文本框里你可以添加需要繪制的話題。在里面輸入/turtle1/pose/x后然后點擊 "+" 再輸入/turtle1/pose/y,點擊 "+"。
- 現在你會在圖形中看到turtle的x-y位置坐標圖。
- 按下減號按鈕會顯示一組菜單讓你隱藏圖形中指定的話題。
- http://wiki.ros.org/rqt_plot
4 rqt_plot出現的問題(kinetic)
4.1 問題
ggk@ggk-virtual-machine:~$ rosrun rqt_plot rqt_plot /usr/lib/python2.7/dist-packages/matplotlib/axis.py:1015: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed. warnings.warn("Unable to find pixel distance along axis " /usr/lib/python2.7/dist-packages/matplotlib/axis.py:1025: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed. warnings.warn("Unable to find pixel distance along axis " Traceback (most recent call last): File "/opt/ros/kinetic/lib/python2.7/dist-packages/rqt_plot/data_plot/mat_data_plot.py", line 107, in resizeEvent self.figure.tight_layout() File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1754, in tight_layout rect=rect) File "/usr/lib/python2.7/dist-packages/matplotlib/tight_layout.py", line 349, in get_tight_layout_figure pad=pad, h_pad=h_pad, w_pad=w_pad) File "/usr/lib/python2.7/dist-packages/matplotlib/tight_layout.py", line 128, in auto_adjust_subplotpars fig.transFigure.inverted()) File "/usr/lib/python2.7/dist-packages/matplotlib/transforms.py", line 1775, in inverted self._inverted = Affine2D(inv(mtx), shorthand_name=shorthand_name) File "/usr/lib/python2.7/dist-packages/numpy/linalg/linalg.py", line 526, in inv ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj) File "/usr/lib/python2.7/dist-packages/numpy/linalg/linalg.py", line 90, in _raise_linalgerror_singular raise LinAlgError("Singular matrix") numpy.linalg.linalg.LinAlgError: Singular matrix
4.2 原因
我們可以登錄Matplotlib官網發現,kinetic中的python2.7和Matplotlib不兼容,並且不再支持Python2。
因此,我們需要尋找另一個可視化工具,PyQtGraph。
4.3 解決方法
- 安裝PyQtGraph
- 官網:http://www.pyqtgraph.org/
- 我們下載之后安裝就可以了。
4.4 deb文件安裝方法
Ubuntu中deb文件詳細安裝方法:
1.首先打開電腦主界面。
2.然后找到"安裝包"后雙擊。
3.最后點擊"安裝"鍵進行安裝即可。
1)步驟1
2)步驟2
微信公眾號:喵哥解說
公眾號介紹:主要研究機器學習、計算機視覺、深度學習、ROS等相關內容,分享學習過程中的學習筆記和心得!期待您的關注,歡迎一起學習交流進步!同時還有1200G的Python視頻和書籍資料等你領取!!!