博客轉自古-月:https://blog.csdn.net/hcx25909/article/details/8870552,本文針對Fuerte版本,Kinetic版本參考 https://www.cnblogs.com/flyinggod/p/11489250.html
前邊我們已經介紹了ROS的基本情況,以及新手入門ROS的初級教程,現在就要真正的使用ROS進入機器人世界了。接下來我們涉及到的很多例程都是《ROS by Example》這本書的內容,我是和群里的幾個人一起從國外的亞馬遜上買到的,還是很有參考價值的,不過前提是你已經熟悉之前的新手教程了。
一、ROS by Example
這本書是關於國外關於ROS出版的第一本書,主要針對Electric和Fuerte版本,使用機器人主要是TurtleBot。書中詳細講解了關於機器人的基本仿真、導航、路徑規划、圖像處理、語音識別等等,而且在google的svn上發布了所有代碼,可以通過以下命令下載、編譯:
git clone https://github.com/pirobot/ros-by-example export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$(pwd) rosmake rbx_vol_1 rospack profile
二、rviz簡單機器人模擬
1、安裝機器人模擬器
git clone https://github.com/vanadiumlabs/arbotix_ros rosmake arbotix_ros
可以將模擬器和rbx_vol_1源代碼放在同一個workspace內的src目錄下,直接catkin_make就可以。
mkdir -p ~/smartcar_description/src cd ~/smartcar_description/src git clone https://github.com/vanadiumlabs/arbotix_ros git clone https://github.com/pirobot/ros-by-example cd .. catkin_make source devel/setup.bash
2、TurtleBot機器人的模擬
roscore roslaunch rbx1_bringup fake_pi_robot.launch
然后在終端中可以看到,機器人已經開始運行了,打開rviz界面,才能看到機器人實體。
rosrun rviz rviz -d `rospack find rbx1_nav`/sim_fuerte.vcg
indogo/kinetic
rosrun rviz rviz `rospack find rbx1_nav`/nav.rviz
后面的參數是加載了rviz的配置文件sim_fuerte.vcg/nav.rviz效果如下:
此時的機器人是靜止的,需要發布一個消息才能讓它動起來。
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文件,由於文件比較長,這里只列舉重點的部分。.vcg配置文件只是Fuerte和Groovy舊版本的ROS使用,當前使用.yaml或者.rviz,在indigo或者kinetic版本ROS編譯時候可以將后綴.vcg改成.rviz,同時launch文件內加載配置的文件名后綴也要同步修改
Background\ ColorB=0.12549 Background\ ColorG=0.12549 Background\ ColorR=0.12549 Camera\ Config=158.108 0.814789 0.619682 -1.57034 Camera\ Type=rviz::FixedOrientationOrthoViewController Fixed\ Frame=/odom Grid.Alpha=0.5 Grid.Cell\ Size=0.5 Grid.ColorB=0.941176 Grid.ColorG=0.941176 Grid.ColorR=0.941176 Grid.Enabled=1 Grid.Line\ Style=0 Grid.Line\ Width=0.03 Grid.Normal\ Cell\ Count=0 Grid.OffsetX=0 Grid.OffsetY=0 Grid.OffsetZ=0 Grid.Plane=0
上面的代碼是配置背景顏色和網格屬性的,對應rviz中的選項如下圖所示。
其中比較重要的一個選項是Camera的type,這個選項是控制開發者的觀察角度的,書中用的是FixedOrientationOrthoViewController的方式,就是上面圖中的俯視角度,無法看到機器人的三維全景,所以可以改為OrbitViewController方式,如下圖所示:

3、發布topic
要讓機器人動起來,還需要給他一些運動需要的信息,這些信息都是通過topic的方式發布的。 這里的topic就是速度命令,針對這個topic,我們需要發布速度的信息,在ROS中已經為我們寫好了一些可用的數據結構,這里用的是Twist信息的數據結構。在終端中可以看到Twist的結構如下:
用下面的命令進行消息的發布,其中主要包括力的大小和方向。
Background\ ColorB=0.12549 Background\ ColorG=0.12549 Background\ ColorR=0.12549 Camera\ Config=158.108 0.814789 0.619682 -1.57034 Camera\ Type=rviz::FixedOrientationOrthoViewController Fixed\ Frame=/odom Grid.Alpha=0.5 Grid.Cell\ Size=0.5 Grid.ColorB=0.941176 Grid.ColorG=0.941176 Grid.ColorR=0.941176 Grid.Enabled=1 Grid.Line\ Style=0 Grid.Line\ Width=0.03 Grid.Normal\ Cell\ Count=0 Grid.OffsetX=0 Grid.OffsetY=0 Grid.OffsetZ=0 Grid.Plane=0
4、節點關系圖