turtlebot的程序默认搭载xtion或kinect深度摄像头。现在我们把它修改为激光雷达。市面上可选的激光雷达有:Hokuyo、Sick和Rplidar 一代。我们以Rplidar为例进行讲解。
安装依赖包
sudo apt-get install ros-indigo-joy
sudo apt-get install ros-indigo-turtlebot-msgs
sudo apt-get install ros-indigo-turtlebot
sudo apt-get install ros-indigo-yocs-velocity-smoother
sudo apt-get install ros-indigo-turtlebot-rviz-launchers
下载源码包
mkdir -p ~/turtlebot_ws/src
cd ~/turtlebot_ws/src
## 激光雷达rplidar 一代驱动
git clone https://github.com/robopeak/rplidar_ros.git
cd rplidar_ros
git checkout 1.0.1
## turtlebot建图依赖包
git clone https://github.com/turtlebot/turtlebot_apps
git clone https://github.com/ros-perception/slam_gmapping
git clone https://github.com/ros-perception/openslam_gmapping
git clone https://github.com/ros-planning/navigation.git
编译
cd ~/turtlebot_ws
catkin_make
添加环境变量
在~/.bashrc最后添加一行:
source /home/harney/turtlebot_ws/devel/setup.bash
然后刷新配置
source ~/.bashrc
新建/etc/udev/rules.d/57-kobuki.rules,内容如下:
# On precise, for some reason, USER and GROUP are getting ignored.
# So setting mode = 0666 for now.
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="kobuki*", MODE:="0666", GROUP:="dialout", SYMLINK+="kobuki"
新建 /etc/udev/rules.d/rplidar.rules文件,内容如下:
KERNEL=="ttyUSB*", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", MODE:="0666", GROUP:="dialout", SYMLINK+="rplidar"
sudo usermod -a -G dialout 用户名
使udev配置生效
sudo service udev reload
sudo service udev restart
使串口的默认访问权限生效,需要重启机器
修改~/turtlebot_ws/src/turtlebot_apps/turtlebot_navigation/launch/gmapping_demo.launch,内容如下:
<include file="$(find turtlebot_bringup)/launch/minimal.launch">
include>
"rplidarNode" pkg="rplidar_ros" type="rplidarNode" output="screen">
"serial_port" type="string" value="/dev/rplidar"/>
"serial_baudrate" type="int" value="115200"/>
"frame_id" type="string" value="laser_frame"/>
"inverted" type="bool" value="false"/>
"angle_compensate" type="bool" value="true"/>
<include file="$(find turtlebot_navigation)/launch/includes/gmapping.launch.xml"/>
<include file="$(find turtlebot_navigation)/launch/includes/move_base.launch.xml"/>
"tf" type="static_transform_publisher" name="base_link_to_laser4"
args="0.0 0.0 0.2 0.0 3.1415926 0.0 /base_link /laser_frame 40" />
修改~/turtlebot_ws/src/turtlebot_apps/turtlebot_navigation/launch/includes/gmapping.launch.xml,内容如下:
<launch>
name="scan_topic" default="scan" />
name="base_frame" default="base_footprint"/>
name="odom_frame" default="odom"/>
"gmapping" type="slam_gmapping" name="slam_gmapping" output="screen">
name="base_frame" value="$(arg base_frame)"/>
name="odom_frame" value="$(arg odom_frame)"/>
name="map_update_interval" value="0.01"/>
name="maxUrange" value="4.0"/>
name="maxRange" value="5.0"/>
name="sigma" value="0.05"/>
name="kernelSize" value="3"/>
name="lstep" value="0.05"/>
name="astep" value="0.05"/>
name="iterations" value="5"/>
name="lsigma" value="0.075"/>
name="ogain" value="3.0"/>
name="lskip" value="0"/>
name="minimumScore" value="30"/>
name="srr" value="0.01"/>
name="srt" value="0.02"/>
name="str" value="0.01"/>
name="stt" value="0.02"/>
name="linearUpdate" value="0.05"/>
name="angularUpdate" value="0.0436"/>
name="temporalUpdate" value="-1.0"/>
name="resampleThreshold" value="0.5"/>
name="particles" value="8"/>
--
name="xmin" value="-50.0"/>
name="ymin" value="-50.0"/>
name="xmax" value="50.0"/>
name="ymax" value="50.0"/>
make the starting size small for the benefit of the Android client's memory...
-->
name="xmin" value="-1.0"/>
name="ymin" value="-1.0"/>
name="xmax" value="1.0"/>
name="ymax" value="1.0"/>
name="delta" value="0.05"/>
name="llsamplerange" value="0.01"/>
name="llsamplestep" value="0.01"/>
name="lasamplerange" value="0.005"/>
name="lasamplestep" value="0.005"/>
from="scan" to="$(arg scan_topic)"/>
launch>
进行控制机器人行走进行制图
roslaunch turtlebot_navigation gmapping_demo.launch
roslaunch turtlebot_teleop keyboard_teleop.launch
建好地图之后我们将map保存
rosrun map_server map_saver -f 地图的保持位置
通过下面命令查看实时地图绘制
roslaunch turtlebot_rviz_launchers view_navigation.launch
如果通过远程查看实时地图绘制,除了在终端指定ROS_MASTER_URI的IP外,还需在/etc/hosts为ros的master主机和远程机器相互配置对方的ip和对应的主机名。
export ROS_MASTER_URI=http://192.168.xx.xx:11311
我们也可以录制数据包用于后续建图使用。
方法如下:
1.创建文件~/turtlebot_ws/src/rosbag/recore_bag.launch
<include file="$(find turtlebot_bringup)/launch/minimal.launch">
include>
"rplidarNode" pkg="rplidar_ros" type="rplidarNode" output="screen">
"serial_port" type="string" value="/dev/rplidar"/>
"serial_baudrate" type="int" value="115200"/>
"frame_id" type="string" value="laser_frame"/>
"inverted" type="bool" value="false"/>
"angle_compensate" type="bool" value="true"/>
<include file="$(find turtlebot_teleop)/launch/keyboard_teleop.launch">
include>
2.创建文件~/turtlebot_ws/src/rosbag/gmapping_bag.launch
<param name = "/use_sim_time" value = "true"/>
<include file="$(find turtlebot_rviz_launchers)/launch/view_navigation.launch">
include>
"rosbag" type="play" name="play1"
args="/home/eaibot/turtlebot_ws/src/rosbag/xxx.bag --delay=5 --clock"/>
"tf" type="static_transform_publisher" name="base_link_to_laser4"
args="0.0 0.0 0.0 0.0 0.0 0.0 /base_link /laser_frame 40" />
<include file="$(find turtlebot_navigation)/launch/includes/gmapping.launch.xml"/>
<include file="$(find turtlebot_navigation)/launch/includes/move_base.launch.xml"/>
cd ~/turtlebot_ws/src/rosbag
roslaunch recore_bag.launch
在另一个终端运行下面程序记录数据
rosrun rosbag record -a
录制完成后,用CTL+C取消结束数据的记录
最后,修改对应的bag文件,根据录的rosbag进行gmapping生成地图
cd ~/turtlebot_ws/src/rosbag
roslaunch gmapping_bag.launch
———-其他的说明——————–
Rplidar的坐标系:三角形的电机为X轴的正向,顺时针90度为Y轴的正向,因为是二维激光,没有Z轴。
turtlebot坐标系:右手原则, 前方(带碰撞挡板)X轴的正向,逆时针90度为Y轴的正向,朝上为Z轴的正向。
由于Rplidar的坐标系和turtlebot坐标系不一致,所以需要沿X轴做180度旋转。
我们安装Rplidar的方法: 把Rplidar固定在turtlebot上方的最中心的位置,Rplidar三角形电机的朝向和turtlebot 前方碰撞挡板的中心位置一致,测量Rplidar和turtlebot底座的高度为20cm。
发布Rplidar和turtlebot的坐标关系有两种方式:
方式一:
<node pkg="tf" type="static_transform_publisher" name="base_link_to_laser4"
args="0.0 0.0 0.2 0.0 3.1415926 0.0 /base_link /laser_frame 40" />
launch>
方式二:
在turtlebot_description/urdf/turtlebot_library.urdf.xacro的标签之前加入如下内容
<joint name="laser" type="fixed">
<origin xyz="0.00 0.00 0.20" rpy="0 3.1415926 0" />
<parent link="base_link" />
<child link="base_laser_link" />
joint>
<link name="base_laser_link">
<visual>
<geometry>
<box size="0.00 0.05 0.06" />
geometry>
<material name="Green" />
visual>
<inertial>
<mass value="0.000001" />
<origin xyz="0 0 0" />
<inertia ixx="0.0001" ixy="0.0" ixz="0.0"
iyy="0.0001" iyz="0.0"
izz="0.0001" />
inertial>
link>
注意:Rplidar的安装位置并不唯一, 如果修改安装位置,修改对应的坐标关系即可