版权声明:本文为博主原创文章,未经博主允许不得转载。
https://blog.csdn.net/Changer_sun/article/details/79264388
ROS中给turtlebot添加传感器:hokuyo,进行gazebo仿真
最初进行仿真的四个步骤:
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
1.编写启动launch文件
由于是apt-get的方式安装的这些包,所以roscd的位置在/opt/ros/kinetic/share
这里,不方便的是必须需要sudo的权限才能对文件进行修改
roscd turtlebot_gazebo
cd launch
/opt/ros/kinetic/share/turtlebot_gazebo/launch
在这里,我不对turtlebot_world.launch
进行直接的修改,拷贝一份并改名为explorer.launch
,对这个文件进行操作和修改,以后也运行的是这个launch
<launch>
<arg name="world_file" default="$(env TURTLEBOT_GAZEBO_WORLD_FILE)"/>
<arg name="base" value="$(optenv TURTLEBOT_BASE kobuki)"/> <!--kobuki create, roomba -->
<arg name="battery" value="$(optenv TURTLEBOT_BATTERY /proc/acpi/battery/BAT0)"/> <!-- /proc/acpi/battery/BAT0 -->
<arg name="gui" default="true"/>
<arg name="stacks" value="$(optenv TURTLEBOT_STACKS hexagons)"/> <!-- circles, hexagons -->
<arg name="3d_sensor" value="$(optenv TURTLEBOT_3D_SENSOR hokuyo)"/> <!-- 在这里修改,添加可一个新的_hokuyo, kinect, asus_xtion_pro -->
<!--这里是启动的gazebo地图的地方,原始的没问题,我这里改成了我自己选的地图-->
<include file="$(find hector_nist_arena_worlds)/launch/start.launch">
<arg name="world" value="go2011_arena_changer.world"/>
<arg name="use_sim_time" value="true"/>
<arg name="debug" value="false"/>
<arg name="gui" value="$(arg gui)" />
</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>
<include file="$(find turtlebot_gazebo)/launch/includes/$(arg base).launch.xml">
由上边这一句我们可以知道,调用了了一个配置文件打开
2.查看kobuki.launch.xml
/opt/ros/kinetic/share/turtlebot_gazebo/launch/includes
sudo vi kobuki.launch.xml
<launch>
<arg name="base"/>
<arg name="stacks"/>
<arg name="3d_sensor"/>
<arg name="urdf_file" default="$(find xacro)/xacro.py '$(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>
这个文件并没有修改,<arg name="urdf_file" default="$(find xacro)/xacro.py '$(find turtlebot_description)/robots/$(arg base)_$(arg stacks)_$(arg 3d_sensor).urdf.xacro'" />
根据这一行,我们可以知道,启动的 urdf位置和名称
3.修改kobuki_hexagons_hokuyo.urdf.xacro
/opt/ros/kinetic/share/turtlebot_description/robots
sudo vi kobuki_hexagons_hokuyo.urdf.xacro
<?xml version="1.0"?>
<!-- - Base : kobuki
- Stacks : hexagons
- 3d Sensor : kinect+hokuyo
-->
<robot name="turtlebot" xmlns:xacro="http://ros.org/wiki/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"/>
<sensor_hokuyo parent="base_link"/>
</robot>
这个文件相比于kobuki_hexagons_kinect.urdf.xacro
进行了两个地方的修改
<xacro:include filename="$(find turtlebot_description)/urdf/turtlebot_library.urdf.xacro" />
<!--这里从turtlebot_common_library改到了turtlebot_library-->
<xacro:include filename="$(find turtlebot_description)/urdf/sensors/hokuyo.urdf.xacro"/>
<!--这个是加上的新的-->
4.修改turtlebot_library.urdf.xacro
之后我们去修改turtlebot_library.urdf.xacro
/opt/ros/kinetic/share/turtlebot_description/urdf
sudo vi 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>
添加了最后一行,意思是引入了hokuyo
的urdf,那么一开始是没有这个urdf的,所以我们需要新建这个文件
5.添加hokuyo的.urdf.xacro文件
/opt/ros/kinetic/share/turtlebot_description/urdf/sensors
sudo vi 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>
<mesh filename="package://turtlebot_description/meshes/sensors/hokuyo.dae"/>
我们需要添加这个hokuyo.dae,可以先下载模型包,然后将文件拷贝过来
可以参考gazebo官方教程:http://gazebosim.org/tutorials/?tut=add_laser
cd ~/.gazebo/models
wget -q -R *index.html*,*.tar.gz --no-parent -r -x -nH http://models.gazebosim.org/hokuyo/
sun@sun:~/.gazebo/models$ ls
cube_20k ground_plane jersey_barrier pioneer2dx willowgarage
dumpster hokuyo my_robot sun
sun@sun:~/.gazebo/models$ cd hokuyo/
sun@sun:~/.gazebo/models/hokuyo$ ls
meshes model-1_2.sdf model-1_3.sdf model-1_4.sdf model.config model.sdf
sun@sun:~/.gazebo/models/hokuyo$ cd meshes/
sun@sun:~/.gazebo/models/hokuyo/meshes$ ls
hokuyo_convex.stl hokuyo.dae
查看/opt/ros/kinetic/share/turtlebot_description/meshes/sensors
目录,将hokuyo_convex.stl hokuyo.dae
两个文件拷贝到该目录即可。
6.配置turtlebot_gazebo.urdf.xacro添加laser
/opt/ros/kinetic/share/turtlebot_description/urdf
sudo vi 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 .zshrc #我用的是zsh,如果是bash就修改.bashrc
#在最后添加以下内容
#For gazebo
export TURTLEBOT_BASE=kobuki
export TURTLEBOT_STACKS=hexagons
export TURTLEBOT_3D_SENSOR=hokuyo
之后运行,正常,激光有数据,话题/scan正常
roslaunch turtlebot_gazebo explorer.launch
roslaunch turtlebot_teleop keyboard_teleop.launch
roslaunch turtlebot_gazebo gmapping_demo.launch
roslaunch turtlebot_rviz_launchers view_navigation.launch
参考文章:
http://blog.csdn.net/qq_36355662/article/details/61413860
http://blog.csdn.net/qq_36355662/article/details/61616513