三、实现分析
按照上面的仿真过程,我们详细分析每一步的代码实现。
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.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
|