本文开发环境为Ubuntu 16.04、ROS版本为kinetic。
主要参考链接:
http://emanual.robotis.com/docs/en/platform/turtlebot3/machine_learning/#machine-learning
本文内容主要包含:
1、Anaconda2 安装
中下载 python2.7版本的Anaconda2 ,安装过程如下:
chmod +x Anaconda2-5.2.0-Linux-x86_64.sh
bash Anaconda2-5.2.0-Linux-x86_64.sh
安装完成后,执行下面指令, 如果可以看到
Python 2.7.xx :: Anaconda, Inc.
.,说明安装成功。
source ~/.bashrc
python -V
接下来配置环境变量 PYTHONPATH,编辑 ~/.bashrc ,修改或者添加内容如下:
#export PYTHONPATH="/usr/lib/python2.7/dist-packages:$PYTHONPATH"
export PYTHONPATH="/home/wsc/anaconda2/lib/python2.7/dist-packages:$PYTHONPATH"
2、 ROS 依赖包安装
为了使ROS 与 Anaconda 一起工作,必须安装在Anaconda中安装ROS 依赖包:
pip install -U rosinstall msgpack empy defusedxml netifaces
3、Tensorflow、Keras等安装
本文中用到的机器学习算法是DQN(Deep Q-Learning),基于Tensorflow与Keras开发,为了避免包冲突,在Anaconda中构建虚拟环境(取名为tensorflow):
conda update -n base conda
conda create -n tensorflow pip python=2.7
进入或者离开虚拟环境的指令为:
# To activate this environment, use:
# > source activate tensorflow
#
# To deactivate an active environment, use:
# > source deactivate
在虚拟环境(envs)中安装Tensorflow、Keras以及ROS依赖包:
source activate tensorflow
pip install -U rosinstall msgpack empy defusedxml netifaces
pip install --ignore-installed --upgrade https://download.tensorflow.google.cn/linux/gpu/tensorflow_gpu-1.8.0-cp27-none-linux_x86_64.whl
pip install keras
source deactivate
我这里安装的是gpu版本的tensorflow,前提是有gpu显卡并安装配置好了相关驱动, 如果要安装cpu版本的tensorflow,指令中地址应为:https://download.tensorflow.google.cn/linux/cpu/tensorflow-1.8.0-cp27-none-linux_x86_64.whl。
4、下载并编译源码
本文先使用github中开源的机器学习的源码进行学习,下载编译过程如下:
cd ~/catkin_ws/src/
git clone https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3_simulations
git clone https://github.com/ROBOTIS-GIT/turtlebot3_turtlebot3_machine_learning
cd ~/catkin_ws && catkin_make
编译成功以后执行下面的脚本,也可以将其添加到~/.bashrc中:
source /home/wsc/catkin_ws/devel/setup.bash
5、设置参数并运行范例
设置参数:
打开源码文件 turtlebot3/turtlebot3_description/urdf/turtlebot3_burger.gazebo.xacro,修改一下两处:
<xacro:arg name="laser_visual" default="false"/> # 如果想看到激光扫描线,设置成 `true`
<scan>
<horizontal>
<samples>360</samples> # 修改成24
<resolution>1</resolution>
<min_angle>0.0</min_angle>
<max_angle>6.28319</max_angle>
</horizontal>
</scan>
打开一个终端,启动turtlebot3 gazebo环境等节点:
roslaunch turtlebot3_gazebo turtlebot3_stage_1.launch
打开另外一个终端,启动DQN算法等节点:
source activate tensorflow
roslaunch turtlebot3_dqn turtlebot3_dqn_stage_1.launch
打开第三个终端,启动数据图形显示节点:
pip install pyqtgraph
roslaunch turtlebot3_dqn result_graph.launch
注:源码范例中提供了turtlebot3_stage_1~turtlebot3_stage_4共4个环境,分别是无障碍、静态障碍、动态障碍、混合障碍环境。
6、训练与结果展示
这是训练约5个半小时后的结果: