前邊我們已經介紹了ROS的基本情況,以及新手入門ROS的初級教程,現在就要真正的使用ROS進入機器人世界了。接下來我們涉及到的很多常式都是《ROS by Example》這本書的內容,我是和群裡的幾個人一起從國外的亞馬遜上買到的,還是很有參考價值的,不過前提是你已經熟悉之前的新手教程了。
一、ROS by Example
這本書是關於國外關於ROS出版的第一本書,主要針對Electric和Fuerte版本,使用機器人主要是TurtleBot。書中詳細講解了關於機器人的基本模擬、導航、路徑規劃、影像處理、語音辨識等等,而且在google的svn上發布了所有代碼,可以通過以下命令下載、編譯:
svn checkout http://ros-by-example.googlecode.com/svn/trunk/rbx_vol_1rosmake rbx_vol_1rospack profile //加入ROS package路徑
二、rviz簡單機器人類比 1、安裝機器人模擬器 rviz是一個顯示機器人實體的工具,本身不具有類比的功能,需要安裝一個模擬器arbotix。
svn checkout http://vanadium-ros-pkg.googlecode.com/svn/trunk/arbotixrosmake arbotix
2、TurtleBot機器人的類比 在書中的rbx_vol_1包裡已經為我們寫好了類比的代碼,我們先進行實驗,完成後再仔細研究代碼。
機器人類比運行:
roscoreroslaunch rbx1_bringup fake_pi_robot.launch
然後在終端中可以看到,機器人已經開始運行了,開啟rviz介面,才能看到機器人實體。
rosrun rviz rviz -d `rospack find rbx1_nav`/sim_fuerte.vcg
後面的參數是載入了rviz的設定檔sim_fuerte.vcg。效果如下:
此時的機器人是靜止的,需要發布一個訊息才能讓它動起來。
rostopic pub -r 10 /cmd_vel geometry_msgs/Twist '{linear: {x: 0.2, y: 0, z: 0}, angular: {x: 0, y: 0, z: 0.5}}'
如果要讓機器人停下來,需要在中斷中按下“Ctrl+c”,然後輸入:
rostopic pub -1 /cmd_vel geometry_msgs/Twist '{}'
也可以改變發送的topic資訊,使機器人走出不同的軌跡。
三、實現分析 按照上面的模擬過程,我們詳細分析每一步的代碼實現。 1、TurtleBot機器人運行 機器人運行使用的是launch檔案,首先開啟fake_turtlebot.launch檔案。
<launch> <param name="/use_sim_time" value="false" /> <!-- Load the URDF/Xacro model of our robot --> <arg name="urdf_file" default="$(find xacro)/xacro.py '$(find turtlebot_description)/urdf/turtlebot.urdf.xacro'" /> <param name="robot_description" command="$(arg urdf_file)" /> <node name="arbotix" pkg="arbotix_python" type="driver.py" output="screen"> <rosparam file="$(find rbx1_bringup)/config/fake_turtlebot_arbotix.yaml" command="load" /> <param name="sim" value="true"/> </node> <node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher"> <param name="publish_frequency" type="double" value="20.0" /> </node> <!-- We need a static transforms for the wheels --> <node pkg="tf" type="static_transform_publisher" name="odom_left_wheel_broadcaster" args="0 0 0 0 0 0 /base_link /left_wheel_link 100" /> <node pkg="tf" type="static_transform_publisher" name="odom_right_wheel_broadcaster" args="0 0 0 0 0 0 /base_link /right_wheel_link 100" /></launch>
檔案可以大概分為四個部分:
(1) 從指定的包中載入urdf檔案
(2) 啟動arbotix模擬器
(3) 啟動狀態發布節點
(4) tf座標系配置
2、rviz設定檔 在開啟rviz的時候需要載入一個.vcg的設定檔,主要對rviz中的外掛程式選項進行預設的配置。這裡開啟的是sim_fuerte.vcg檔案,由於檔案比較長,這裡只列舉重點的部分。
Background\ ColorB=0.12549Background\ ColorG=0.12549Background\ ColorR=0.12549Camera\ Config=158.108 0.814789 0.619682 -1.57034Camera\ Type=rviz::FixedOrientationOrthoViewControllerFixed\ Frame=/odomGrid.Alpha=0.5Grid.Cell\ Size=0.5Grid.ColorB=0.941176Grid.ColorG=0.941176Grid.ColorR=0.941176Grid.Enabled=1Grid.Line\ Style=0Grid.Line\ Width=0.03Grid.Normal\ Cell\ Count=0Grid.OffsetX=0Grid.OffsetY=0Grid.OffsetZ=0Grid.Plane=0
上面的代碼是配置背景顏色和網格屬性的,對應rviz中的選項如所示。
其中比較重要的一個選項是Camera的type,這個選項是控制開發人員的觀察角度的,書中用的是FixedOrientationOrthoViewController的方式,就是上面圖中的俯視角度,無法看到機器人的三維全景,所以可以改為OrbitViewController方式,如所示:
3、發布topic 要讓機器人動起來,還需要給他一些運動需要的資訊,這些資訊都是通過topic的方式發布的。
這裡的topic就是速度命令,針對這個topic,我們需要發布速度的資訊,在ROS中已經為我們寫好了一些可用的資料結構,這裡用的是Twist資訊的資料結構。在終端中可以看到Twist的結構如下:
用下面的命令進行訊息的發布,其中主要包括力的大小和方向。
Background\ ColorB=0.12549Background\ ColorG=0.12549Background\ ColorR=0.12549Camera\ Config=158.108 0.814789 0.619682 -1.57034Camera\ Type=rviz::FixedOrientationOrthoViewControllerFixed\ Frame=/odomGrid.Alpha=0.5Grid.Cell\ Size=0.5Grid.ColorB=0.941176Grid.ColorG=0.941176Grid.ColorR=0.941176Grid.Enabled=1Grid.Line\ Style=0Grid.Line\ Width=0.03Grid.Normal\ Cell\ Count=0Grid.OffsetX=0Grid.OffsetY=0Grid.OffsetZ=0Grid.Plane=0
4、節點關係圖
----------------------------------------------------------------
歡迎大家轉載我的文章。
轉載請註明:轉自古-月
http://blog.csdn.net/hcx25909
歡迎繼續關注我的部落格