本次使用的硬件为
小车驱动部分参考了以下教程
https://www.ncnynl.com/archives/201903/2884.html
以下驱动需要在工控机和笔记本上都进行安装
首先安装依赖
sudo apt-get install ros-melodic-kobuki-*
sudo apt-get install ros-melodic-ecl-streams
sudo apt-get install libusb-dev
sudo apt-get install libspnav-dev
sudo apt-get install ros-melodic-joystick-drivers
sudo apt-get install bluetooth
sudo apt-get install libbluetooth-dev
sudo apt-get install libcwiid-dev
新建工作空间,准备相关包
mkdir -p ~/turtlebot_ws/src
cd ~/turtlebot_ws/src
git clone https://github.com/turtlebot/turtlebot_simulator
git clone https://github.com/turtlebot/turtlebot.git
git clone https://github.com/turtlebot/turtlebot_apps.git
git clone https://github.com/udacity/robot_pose_ekf
git clone https://github.com/ros-perception/depthimage_to_laserscan.git
git clone https://github.com/yujinrobot/kobuki_msgs.git
git clone https://github.com/yujinrobot/kobuki_desktop.git
cd kobuki_desktop/
rm -r kobuki_qtestsuite
git clone https://github.com/toeklk/orocos-bayesian-filtering.git
git clone https://github.com/turtlebot/turtlebot_msgs.git
git clone https://github.com/ros-drivers/joystick_drivers.git
复制kobuki和yujin_ocs依赖库到turtlebot_ws/src
工作空间下
mkdir -p ~/repos/
cd ~/repos/
git clone https://github.com/yujinrobot/kobuki.git
cp -r kobuki/* ~/turtlebot_ws/src/
git clone https://github.com/yujinrobot/yujin_ocs.git
cp -r yujin_ocs/yocs_cmd_vel_mux/ yujin_ocs/yocs_controllers ~/turtlebot_ws/src/
编译工作空间
cd ~/turtlebot_ws
catkin_make
添加工作空间到bashrc文件
echo "source ~/turtlebot_ws/devel/setup.bash" >> ~/.bashrc
启用环境变量
source ~/.bashrc
开启Kuboki底盘电源,将数据线插入电脑
打开一个新的终端窗口
# 启动底盘
roslaunch turtlebot_bringup minimal.launch
此时小车底盘发出提示声音表示可以使用
打开第二个终端窗口
# 启动键盘控制程序
roslaunch turtlebot_teleop keyboard_teleop.launch
此时可以使用键盘控制小车运动
小车总拖着一堆键盘鼠标总归是不方便的,ROS允许同一个局域网内的机器可以通过SSH共享一个Master,下面开始设置
首先确定IP地址,打开终端输入
ifconfig
获得IP地址
用户名即终端里@
后面的内容,我这里是
# 工控机用户名
workbox-Default-string
# 工控机IP地址
192.168.199.189
# 工控机用户名
work-QTJ5
# 工控机IP地址
192.168.199.225
首先测试一下,互相ping
一下对方的IP,例如
ping 192.168.199.225
ping 192.168.199.189
能ping
通则可继续
需要在两台机子上的/etc/hosts文件加入对方的IP地址和对应的计算机名字
在工控机(workbox-Default-string)上终端操作
# 打开hosts
sudo gedit /etc/hosts
加上笔记本电脑的IP和用户名,注意中间是一个tab而不是空格
192.168.199.225 work-QTJ5
在笔记本(work-QTJ5)上操作与之相似,打开终端
# 打开hosts
sudo gedit /etc/hosts
加上工控机的IP和用户名
192.168.199.189 workbox-Default-string
此时测试一下,互相ping
对方的用户名
ping workbox-Default-string
ping work-QTJ5
这里我们设置工控机为Master
把这段加到工控机中的 ~/.bashrc 文件中
export ROS_HOSTNAME=workbox-Default-string
export ROS_MASTER_URI=http://workbox-Default-string:11311
把这段加到笔记本中的 ~/.bashrc 文件中
export ROS_HOSTNAME=work-QTJ5
export ROS_MASTER_URI=http://workbox-Default-string:11311
此时可此时可测试一下
在工控机上
# 打开ROS
roscore
# 运行海龟模拟器
rosrun turtlesim turtlesim_node
在笔记本上
# 打开键盘控制节点
rosrun turtlesim turtle_teleop_key
此时可用笔记本的键盘控制工控机上的海龟
# 启动底盘
roslaunch turtlebot_bringup minimal.launch
# 启动键盘控制程序
roslaunch turtlebot_teleop keyboard_teleop.launch
# kincet测试程序
roslaunch kinect2_bridge kinect2_bridge.launch
roslaunch mbot_gazebo mbot_kinect_nav_gazebo.launch
这部分参考了
https://blog.csdn.net/sdhjqhjq1/article/details/79143498?spm=1001.2014.3001.5501
https://blog.csdn.net/sdhjqhjq1/article/details/79144505?spm=1001.2014.3001.5501
# 切换到工作空间目录
cd ~/turtlebot_ws/src
# 获取驱动
git clone https://github.com/ncnynl/rplidar_ros.git
# 返回上机目录
cd ..
# 编译
catkin_make
配置端口
将激光雷达插入USB,建议先插激光雷达
检查端口权限
ls -l /dev |grep ttyUSB
赋予端口权限
sudo chmod a+rw /dev/ttyUSB0
这里可以使用minicom配置端口号、波特率等,可以参考:
http://blog.csdn.net/sdhjqhjq1/article/details/79133502
roscore
cd ~/turtlebot_ws
. devel/setup.bash
roslaunch rplidar_ros view_rplidar.launch
此时可见激光雷达图像
建立工作空间(也可以利用现有的),编译包
mkdir -p ~/turtlebot_ws/src
cd ~/turtlebot_ws/src
## 激光雷达rplidar一代驱动
git clone https://github.com/ncnynl/rplidar_ros.git
# turtlebot建图依赖包
git clone https://github.com/turtlebot/turtlebot_apps
# 编译
cd ~/turtlebot_ws
catkin_make
添加环境变量,在~/.bashrc最后添加一行:
source /home/ubu/turtlebot_ws/devel/setup.bash
刷新配置
source ~/.bashrc
或
$ rospack profile
确认idVendor和idProduct,ID后面的部分idVendor:idProduct
# 输入
lsusb
# 显示类似下图
Bus 001 Device 006: ID 10c4:ea60
新建/etc/udev/rules.d/rplidar.rules
文件,内容如下:
# 别名为rplidar,实际名称为:/dev/rplidar
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
复制rplidar.launch到rplidar-laser.launch,并增加TF定义
roscd turtlebot_navigation
mkdir -p laser/driver
sudo cp ~/turtlebot_ws/src/rplidar_ros/launch/rplidar.launch laser/driver/rplidar_laser.launch
打开rplidar_laser.launch,并修改
rosed turtlebot_navigation rplidar_laser.launch
检查frame_id是否指定为laser
<param name="frame_id" type="string" value="laser"/>
查看serial_port是否指定正确端口,使用别名
检查端口:,设置好别名,或直接端口/dev/ttyUSB0
增加TF:
<node pkg="tf" type="static_transform_publisher" name="base_to_laser" args="0.0 0.0 0.18 0 0.0 0.0 base_link laser 100"/>
修改为args="0.0 0.0 0.18 0 0.0 0.0 为自己的实际安装位置。详情查看,static_transform_publisher部分
static_transform_publisher x y z qx qy qz qw frame_id child_frame_id period_in_ms
这里我假设底盘的中心点为0,雷达放在机器人托盘中心位置,X为0,高度为18CM,Z为0.18m
TF的单位使用米的,测量单位是CM
完整代码如下:
<launch>
<node name="rplidarNode" pkg="rplidar_ros" type="rplidarNode" output="screen">
<param name="serial_port" type="string" value="/dev/ttyUSB0"/>
<param name="serial_baudrate" type="int" value="115200"/>
<param name="frame_id" type="string" value="laser"/>
<param name="inverted" type="bool" value="false"/>
<param name="angle_compensate" type="bool" value="true"/>
</node>
<node pkg="tf" type="static_transform_publisher" name="base_to_laser" args="0.0 0.0 0.18 0 0.0 0.0 base_link laser 100"/>
</launch>
增加rplidar_gmapping_demo.launch文件,用于启动gmapping.
roscd turtlebot_navigation
touch launch/rplidar_gmapping_demo.launch
rosed launch/rplidar_gmapping_demo.launch
输入内容:
<launch>
<!-- Define laser type-->
<arg name="laser_type" default="rplidar" />
<!-- laser driver -->
<include file="$(find turtlebot_navigation)/laser/driver/$(arg laser_type)_laser.launch" />
<!-- Gmapping -->
<arg name="custom_gmapping_launch_file" default="$(find turtlebot_navigation)/launch/includes/gmapping/$(arg laser_type)_gmapping.launch.xml"/>
<include file="$(arg custom_gmapping_launch_file)"/>
<!-- Move base -->
<include file="$(find turtlebot_navigation)/launch/includes/move_base.launch.xml"/>
</launch>
设置laser_type为rplidar.
增加rplidar_gmapping.launch.xml文件,执行gmapping建图
roscd turtlebot_navigation
touch launch/includes/gmapping/rplidar_gmapping.launch.xml
rosed launch/includes/gmapping/rplidar_gmapping.launch.xml
输入内容:
<launch>
<arg name="scan_topic" default="scan" />
<arg name="base_frame" default="base_footprint"/>
<arg name="odom_frame" default="odom"/>
<node pkg="gmapping" type="slam_gmapping" name="slam_gmapping" output="screen">
<param name="base_frame" value="$(arg base_frame)"/>
<param name="odom_frame" value="$(arg odom_frame)"/>
<param name="map_update_interval" value="0.01"/>
<param name="maxUrange" value="4.0"/>
<param name="maxRange" value="5.0"/>
<param name="sigma" value="0.05"/>
<param name="kernelSize" value="3"/>
<param name="lstep" value="0.05"/>
<param name="astep" value="0.05"/>
<param name="iterations" value="5"/>
<param name="lsigma" value="0.075"/>
<param name="ogain" value="3.0"/>
<param name="lskip" value="0"/>
<param name="minimumScore" value="30"/>
<param name="srr" value="0.01"/>
<param name="srt" value="0.02"/>
<param name="str" value="0.01"/>
<param name="stt" value="0.02"/>
<param name="linearUpdate" value="0.05"/>
<param name="angularUpdate" value="0.0436"/>
<param name="temporalUpdate" value="-1.0"/>
<param name="resampleThreshold" value="0.5"/>
<param name="particles" value="8"/>
<!--
<param name="xmin" value="-50.0"/>
<param name="ymin" value="-50.0"/>
<param name="xmax" value="50.0"/>
<param name="ymax" value="50.0"/>
make the starting size small for the benefit of the Android client's memory...
-->
<param name="xmin" value="-1.0"/>
<param name="ymin" value="-1.0"/>
<param name="xmax" value="1.0"/>
<param name="ymax" value="1.0"/>
<param name="delta" value="0.05"/>
<param name="llsamplerange" value="0.01"/>
<param name="llsamplestep" value="0.01"/>
<param name="lasamplerange" value="0.005"/>
<param name="lasamplestep" value="0.005"/>
<remap from="scan" to="$(arg scan_topic)"/>
</node>
</launch>
工控机新开端口,启动turtlebot
roslaunch turtlebot_bringup minimal.launch
工控机新开端口,启动gmapping,用于构建地图
roslaunch turtlebot_navigation rplidar_gmapping_demo.launch
笔记本新开端口,启动键盘操作Turtlebot
roslaunch turtlebot_teleop keyboard_teleop.launch
笔记本新开端口,启动rviz,实时查看建图情况
rosrun rviz rviz
点击ADD,订阅map等topic即可观看建图。
# 保存地图
rosrun map_server map_saver -f cloister_gmapping
生成.pgm和.yaml文件,默认保存位置为home目录。