1. 程式人生 > >移動機器人模擬梳理

移動機器人模擬梳理

最近系統重灌,重新配置模擬環境,因此再次梳理一下

1、模擬環境配置

建立ros工作空間:

source /opt/ros/kinetic/setup.bash
sh creat_pkg.sh
mkdir -p ~/ros_gazebo/src
cd ~/ros_gazebo
catkin_make
source ~/ros_gazebo/devel/setup.bash
echo $ROS_PACKAGE_PATH
#輸出/home/jk/ros_gazebo/src:/opt/ros/kinetic/share

新建package:

cd ~/ros_gazebo/src/
catkin_create_pkg my_ros_sim std_msgs rospy roscpp
cd ~/ros_gazebo/
catkin_make

執行以下命令,看看是否缺少一些包。

roslaunch turtlebot_gazebo turtlebot_world.launch
roslaunch turtlebot_teleop keyboard_teleop.launch
roslaunch turtlebot_gazebo gmapping_demo.launch
roslaunch turtlebot_rviz_launchers view_navigation.launch

安裝對應的缺少的包到src下(turtlebot_simulator包含turtlebot_gazebo,turtlebot_interactions包含turtlebot_rviz_launchers

)

cd ~/ros_gazebo/src
git clone https://github.com/turtlebot/turtlebot_simulator.git
git clone https://github.com/turtlebot/turtlebot.git
git clone https://github.com/turtlebot/turtlebot_interactions.git 

編譯:

#可能需要安裝以下包
sudo apt-get install ros-kinetic-joy
catkin_make

2、下載model,修改launch

下載:gazebo_model,並解壓到~/.gazebo下,如下圖:

cd ~/.gazebo/
tar -xzvf models.tar.gz 

2.1 修改turtlebot_world.launch

<launch>
<!--有修改-->
  <arg name="gui"        default="true"/>
  <arg name="world_file" default="$(env TURTLEBOT_GAZEBO_WORLD_FILE)"/>

  <arg name="base"       value="$(optenv TURTLEBOT_BASE kobuki)"/> <!-- create, roomba -->
  <arg name="battery"    value="$(optenv TURTLEBOT_BATTERY /proc/acpi/battery/BAT0)"/>  <!-- /proc/acpi/battery/BAT0 -->
  <arg name="stacks"     value="$(optenv TURTLEBOT_STACKS hexagons)"/>  <!-- circles, hexagons -->
  <!--<arg name="3d_sensor"  value="$(optenv TURTLEBOT_3D_SENSOR kinect)"/> --> <!-- kinect, asus_xtion_pro -->
 <!--有修改-->
  <arg name="3d_sensor" value="$(optenv TURTLEBOT_3D_SENSOR hokuyo)"/>

  <include file="$(find gazebo_ros)/launch/empty_world.launch">
    <arg name="use_sim_time" value="true"/>
    <arg name="debug" value="false"/>
    <arg name="gui" value="$(arg gui)" />
    <arg name="world_name" value="$(arg world_file)"/>
  </include>

  <include file="$(find turtlebot_gazebo)/launch/includes/$(arg base).launch.xml">
    <arg name="base" value="$(arg base)"/>
    <arg name="stacks" value="$(arg stacks)"/>
    <arg name="3d_sensor" value="$(arg 3d_sensor)"/>
  </include>

  <node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher">
    <param name="publish_frequency" type="double" value="30.0" />
  </node>

  <!-- Fake laser -->
  <node pkg="nodelet" type="nodelet" name="laserscan_nodelet_manager" args="manager"/>
  <!--有修改-->
  <!--<node pkg="nodelet" type="nodelet" name="depthimage_to_laserscan"
        args="load depthimage_to_laserscan/DepthImageToLaserScanNodelet laserscan_nodelet_manager">
    <param name="scan_height" value="10"/>
    <param name="output_frame_id" value="/camera_depth_frame"/>
    <param name="range_min" value="0.45"/>
    <remap from="image" to="/camera/depth/image_raw"/>
    <remap from="scan" to="/scan"/>
  </node>-->
</launch>

2.2 新建kobuki_hexagons_hokuyo.urdf.xacro

由於<include file="$(find turtlebot_gazebo)/launch/includes/$(arg base).launch.xml">可知,turtlebot_world.launch呼叫了kobuki.launch.xml檔案,尋找該檔案,

 find ~/ros_gazebo -name "kobuki.launch.xml

kobuki.launch.xml:(不用修改)

<launch>
  <arg name="base"/>
  <arg name="stacks"/>
  <arg name="3d_sensor"/>

  <arg name="urdf_file" default="$(find xacro)/xacro --inorder '$(find turtlebot_description)/robots/$(arg base)_$(arg stacks)_$(arg 3d_sensor).urdf.xacro'"/>
  <param name="robot_description" command="$(arg urdf_file)"/>

  <!-- Gazebo model spawner -->
  <node name="spawn_turtlebot_model" pkg="gazebo_ros" type="spawn_model"
        args="$(optenv ROBOT_INITIAL_POSE) -unpause -urdf -param robot_description -model mobile_base"/>

  <!-- Velocity muxer -->
  <node pkg="nodelet" type="nodelet" name="mobile_base_nodelet_manager" args="manager"/>
  <node pkg="nodelet" type="nodelet" name="cmd_vel_mux"
        args="load yocs_cmd_vel_mux/CmdVelMuxNodelet mobile_base_nodelet_manager">
    <param name="yaml_cfg_file" value="$(find turtlebot_bringup)/param/mux.yaml"/>
    <remap from="cmd_vel_mux/output" to="mobile_base/commands/velocity"/>
  </node>

  <!-- Bumper/cliff to pointcloud (not working, as it needs sensors/core messages) -->
  <include file="$(find turtlebot_bringup)/launch/includes/kobuki/bumper2pc.launch.xml"/>
</launch>

通過其中的兩行程式碼可知,kobuki.launch.xml檔案呼叫了引數檔案:kobuki_hexagons_hokuyo.urdf.xacro(turtlebot_world.launch已經將kinetic改成了hokuyo)

<arg name="urdf_file" default="$(find xacro)/xacro --inorder '$(find turtlebot_description)/robots/$(arg base)_$(arg stacks)_$(arg 3d_sensor).urdf.xacro'"/>
<param name="robot_description" command="$(arg urdf_file)"/>

找到kobuki_hexagons_kinect.urdf.xacro,

其路徑為/home/jk/ros_gazebo/src/turtlebot/turtlebot_description/robots/kobuki_hexagons_kinect.urdf.xacro,

在該路徑下新建檔案kobuki_hexagons_hokuyo.urdf.xacro,並複製

kobuki_hexagons_kinect.urdf.xacro程式碼進去,

kobuki_hexagons_hokuyo.urdf.xacro:

<?xml version="1.0"?>
<!--
    - Base      : kobuki
    - Stacks    : hexagons
    - 3d Sensor : kinect
-->    
<robot name="turtlebot" xmlns:xacro="http://ros.org/wiki/xacro">

  <!--修改<xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_common_library.urdf.xacro" />-->

  <xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_library.urdf.xacro" />

  <xacro:include filename="$(find kobuki_description)/urdf/kobuki.urdf.xacro" />
  <xacro:include filename="$(find turtlebot_description)/urdf/stacks/hexagons.urdf.xacro"/>
  <xacro:include filename="$(find turtlebot_description)/urdf/sensors/kinect.urdf.xacro"/>
  <!--增加-->
  <xacro:include filename="$(find turtlebot_description)/urdf/sensors/hokuyo.urdf.xacro"/>
  
  <kobuki/>
  <stack_hexagons parent="base_link"/>
  <sensor_kinect  parent="base_link"/>
</robot>

上述程式碼做了如下修改:

  1. 尋找turtlebot_common_library.urdf.xacro改成尋找turtlebot_library.urdf.xacro
  2. 增加尋找hokuyo.urdf.xacro的程式碼

2.3 新建turtlebot_common_library.urdf.xacrohokuyo.urdf.xacro

2.3.1 turtlebot_common_library.urdf.xacro

cd ~ros_gazebo/src/turtlebot/turtlebot_description/urdf/
gedit turtlebot_library.urdf.xacro
<?xml version="1.0"?>
<!--
  The complete turtlebot library of xacros for easy reference
 -->
<robot xmlns:xacro="http://ros.org/wiki/xacro">
  <!-- General -->
  <xacro:include filename="$(find turtlebot_description)/urdf/common_properties.urdf.xacro"/>
  <xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_properties.urdf.xacro"/>
  <!-- Bases -->
  <xacro:include filename="$(find create_description)/urdf/create.urdf.xacro"/>
  <xacro:include filename="$(find kobuki_description)/urdf/kobuki.urdf.xacro" />
  <!-- Stacks -->
  <xacro:include filename="$(find turtlebot_description)/urdf/stacks/circles.urdf.xacro"/>
  <xacro:include filename="$(find turtlebot_description)/urdf/stacks/hexagons.urdf.xacro"/>
  <!-- 3D Sensors -->
  <xacro:include filename="$(find turtlebot_description)/urdf/sensors/kinect.urdf.xacro"/>
  <xacro:include filename="$(find turtlebot_description)/urdf/sensors/asus_xtion_pro.urdf.xacro"/>
  <xacro:include filename="$(find turtlebot_description)/urdf/sensors/asus_xtion_pro_offset.urdf.xacro"/>
  <xacro:include filename="$(find turtlebot_description)/urdf/sensors/rplidar.urdf.xacro"/>
  <!--xacro:include filename="$(find turtlebot_description)/urdf/sensors/hokuyo.urdf.xacro"/-->
  <xacro:include filename="$(find turtlebot_description)/urdf/sensors/hokuyo.urdf.xacro"/>
</robot>

2.3.2 hokuyo.urdf.xacro

cd ~/ros_gazebo/src/turtlebot/turtlebot_description/urdf/sensors/
gedit hokuyo.urdf.xacro
<?xml version="1.0"?>
    <!-- script_version=1.1 -->
    <robot name="sensor_hokuyo" xmlns:xacro="http://ros.org/wiki/xacro">
      <xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_gazebo.urdf.xacro"/>
      <xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_properties.urdf.xacro"/>

      <!-- RPLidar 2D LIDAR -->
      <xacro:macro name="sensor_hokuyo" params="parent">
        <joint name="laser" type="fixed">
          <origin xyz="0.10 0 0.435" rpy="0 0.0 0.0" />
          <parent link="base_link" />
            <child link="hokuyo_link" />
        </joint>

        <!-- Hokuyo Laser -->
       <link name="hokuyo_link">
         <collision>
           <origin xyz="0 0 0" rpy="0 0 0"/>
           <geometry>
         <box size="0.1 0.1 0.1"/>
           </geometry>
         </collision>

       <visual>
           <origin xyz="0 0 0" rpy="0 0 0"/>
         <geometry>
        <mesh filename="package://turtlebot_description/meshes/sensors/hokuyo.dae"/>
         </geometry>
       </visual>

       <inertial>
           <mass value="1e-5" />
         <origin xyz="0 0 0" rpy="0 0 0"/>
        <inertia ixx="1e-6" ixy="0" ixz="0" iyy="1e-6" iyz="0" izz="1e-6" />
        </inertial>
       </link>

        <!-- Set up laser gazebo details -->
        <turtlebot_sim_2dsensor/>
      </xacro:macro>
</robot>

下載hokuyo.dae:

<mesh filename="package://turtlebot_description/meshes/sensors/hokuyo.dae"/>

我們需要新增這個hokuyo.dae,可以先下載模型包,然後將檔案拷貝過來

cd ~/.gazebo/models
wget -q -R *index.html*,*.tar.gz --no-parent -r -x -nH http://models.gazebosim.org/hokuyo/

將/home/jk/.gazebo/models/hokuyo/meshes下的兩個檔案拷貝到~/ros_gazebo/src.../turtlebot_description/meshes/sensors目錄

cd ~/.gazebo/models/hokuyo/meshes/
cp hokuyo_convex.stl hokuyo.dae ~/ros_gazebo/src/turtlebot/turtlebot_description/urdf/sensors/

2.4 配置turtlebot_gazebo.urdf.xacro新增laser

cd ~/ros_gazebo/src/turtlebot/turtlebot_description/urdf/
gedit turtlebot_gazebo.urdf.xacro 
<?xml version="1.0"?>
<robot name="turtlebot_gazebo" xmlns:xacro="http://ros.org/wiki/xacro">
  <!-- Microsoft Kinect / ASUS Xtion PRO Live for simulation -->
  <xacro:macro name="turtlebot_sim_3dsensor">
    <gazebo reference="camera_link">
      <sensor type="depth" name="camera">
        <always_on>true</always_on>
        <update_rate>20.0</update_rate>
        <camera>
          <horizontal_fov>${60.0*M_PI/180.0}</horizontal_fov>
          <image>
            <format>B8G8R8</format>
            <width>640</width>
            <height>480</height>
          </image>
          <clip>
            <near>0.05</near>
            <far>8.0</far>
          </clip>
        </camera>
        <plugin name="kinect_camera_controller" filename="libgazebo_ros_openni_kinect.so">
          <cameraName>camera</cameraName>
          <alwaysOn>true</alwaysOn>
          <updateRate>10</updateRate>
          <imageTopicName>rgb/image_raw</imageTopicName>
          <depthImageTopicName>depth/image_raw</depthImageTopicName>
          <pointCloudTopicName>depth/points</pointCloudTopicName>
          <cameraInfoTopicName>rgb/camera_info</cameraInfoTopicName>
          <depthImageCameraInfoTopicName>depth/camera_info</depthImageCameraInfoTopicName>
          <frameName>camera_depth_optical_frame</frameName>
          <baseline>0.1</baseline>
          <distortion_k1>0.0</distortion_k1>
          <distortion_k2>0.0</distortion_k2>
          <distortion_k3>0.0</distortion_k3>
          <distortion_t1>0.0</distortion_t1>
          <distortion_t2>0.0</distortion_t2>
          <pointCloudCutoff>0.4</pointCloudCutoff>
        </plugin>
      </sensor>
    </gazebo>
  </xacro:macro>

<!-- RPLidar LIDAR for simulation   
  <xacro:macro name="rplidar_laser">  
    <gazebo reference="base_laser_link">  
      <sensor type="ray" name="laser">  
        <pose>0 0 0 0 0 0</pose>  
        <visualize>false</visualize>  
<update_rate>5.5</update_rate>  
        <ray>  
          <scan>  
            <horizontal>  
              <samples>360</samples>  
              <resolution>1</resolution>  
              <min_angle>-3.1415926</min_angle>  
              <max_angle>3.1415926</max_angle>  
            </horizontal>  
          </scan>  
          <range>  
            <min>0.10</min>  
            <max>6.0</max>  
            <resolution>0.01</resolution>  
          </range>  
          <noise>  
            <type>Gaussian</type>  
            <mean>0.0</mean>  
            <stddev>0.01</stddev>  
          </noise>  
        </ray>  
        <plugin name="rplidar_node" filename="libgazebo_ros_laser.so">  
          <topicName>/scan</topicName>  
          <frameName>base_laser_link</frameName>  
        </plugin>  
      </sensor>  
    </gazebo>  
  </xacro:macro>  
 -->
<xacro:macro name="turtlebot_sim_2dsensor">
 <gazebo reference="hokuyo_link">
        <sensor type="ray" name="head_hokuyo_sensor">
          <pose>0 0 0 0 0 0</pose>
          <visualize>false</visualize>
          <update_rate>40</update_rate>
          <ray>
            <scan>
              <horizontal>
                <samples>720</samples>
                <resolution>1</resolution>
                <min_angle>-1.570796</min_angle>
                <max_angle>1.570796</max_angle>
              </horizontal>
</scan>
            <range>
              <min>0.10</min>
              <max>30.0</max>
              <resolution>0.01</resolution>
            </range>
            <noise>
              <type>gaussian</type>

               <mean>0.0</mean>
              <stddev>0.01</stddev>
            </noise>
          </ray>
            <plugin name="gazebo_ros_head_hokuyo_controller" filename="libgazebo_ros_laser.so">
            <!--topicName>/turtlebot/laser/scan</topicName-->
            <topicName>scan</topicName>
            <frameName>hokuyo_link</frameName>
        </plugin>
        </sensor>
      </gazebo>
  </xacro:macro>
</robot>

此處注意hokuyo_link部件名稱和前邊的hokuyo_link對應,才能模擬處實際感測器資料。此處資料釋出在<topicName>/turtlebot/laser/scan</topicName>中的/turtlebot/laser/scan主題上。

修改環境變數

sudo vi .bashrc 

#在最後新增以下內容
#For gazebo 
export  TURTLEBOT_BASE=kobuki
export  TURTLEBOT_STACKS=hexagons
export  TURTLEBOT_3D_SENSOR=hokuyo

之後執行,正常,鐳射有資料,話題/scan正常

檢查kobuki_hexagons_hokuyo.urdf.xacro問題,以及執行一下程式碼(需要修改)

roslaunch turtlebot_gazebo explorer.launch
roslaunch turtlebot_teleop keyboard_teleop.launch
roslaunch turtlebot_gazebo gmapping_demo.launch
roslaunch turtlebot_rviz_launchers view_navigation.launch