ROS学习【18】—–利用Gazebo搭建赛道,控制小车完成比赛目录
- 一、 ROS仿真功能包下载编译
- 1、racecar功能包下载
- 2、安装该功能包运行需要的控件
- 3、racecar功能包编译
- 4、测试是否编译成功
- 二、 运行功能包自带的赛道
- 1、运行赛道,控制小车进行位移
- 2、进行gmapping建图
- 三、 自行进行赛道搭建
- 1、新建终端,打开Gazebo
- 2、运行小车模型,导入刚刚的模型框架,然后保存world文件
- 3、创建launch文件,填写赛道配置参数
- 4、运行我们的地图
- 5、进行gmapping建图
- 四、 实现 小车自主定位导航
- 1、创建手工导航launch文件
- 2、运行手工导航
- 3、选择2D Nav Goal指针,进行手工导航
现在很多小伙伴在为全国大学生智能车比赛做准备,但在进行小车组装之前,一定会通过ROS进行赛道模拟,调试,一切准备就绪后才会进行现实中小车的组装,因此,测试是非常重要的,本次博客,林君学长主要带大家利用Gazebo搭建全国大学生智能车比赛的规则赛道,控制小车,跑完赛道,虽然只是简单的模拟,但对参赛的小伙伴来说,可以说非常重要了,有了一个入门的基础,接下来,一起看步骤了解吧!
- ubuntu版本:ubuntuKylin-16.04
- ROS版本:ROS-kinetic
- Gazebo版本:Gazebo-7.16
一、 ROS仿真功能包下载编译
一下ROS的仿真功能包需要通过GitHub克隆下载,当然是网络的大佬写好了的,小伙伴如需要自己写,可以参考其中的代码,进行自行构建,这里,林君学长推荐模仿该仿真功能包,进行自行搭建,毕竟对于才参赛的同学来说,动手能力非常的重要! 1、racecar功能包下载 (1)、新建终端进入ROS工作空间的src目录下
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros/src </code>
(2)、下载Gazebo搭建赛道功能包racecar
<code class="prism language-bash has-numbering"><span class="token function">git</span> clone https://github.com/xmy0916/racecar.git </code>
(3)、racecar功能包中资源展示
(4)、建议及推荐 (1)、小伙伴如果下载过慢,可以尝试通过手机开热点后,通过电脑进行连接,然后下载,这样可能会下载成功,因为林君学长就是这样下载成功的 (2)、小伙伴如果实在不能够下载,林君学长已经将该源码资源包啥贯穿到我的资源模块,小伙伴可以去林君学长的后台下载,下载链接如下所示: https://download.csdn.net/download/qq_42451251/12483862
以上资源包的内容和林君学长下面讲解资源包的内容完全一样哦,接下来,让我们进入正题吧!
2、安装该功能包运行需要的控件
在上面的终端不关闭的情况下,新建一个终端,进行控件的安装,控件的安装是为了本次实验任务需要,不安装,在编译或者运行的时候会出现错误的哦! (1)、driver_base的控件安装
<code class="prism language-bash has-numbering"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> ros-kinetic-driver-base </code>
输入y进行下载,以上截图为操作现状,后面的控件下载将不给出截图了哦,输入y对应下载就好! (2)、controllers控件安装
<code class="prism language-bash has-numbering"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> ros-kinetic-gazebo-ros-control <span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> ros-kinetic-effort-controllers <span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> ros-kinetic-joint-state-controller </code>
(3)、ackermann_msgs控件安装
<code class="prism language-bash has-numbering"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> ros-kinetic-ackermann-msgs </code>
(4)、planner控件安装
<code class="prism language-bash has-numbering"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> ros-kinetic-global-planner <span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> ros-kinetic-teb-local-planner </code>
以上就是需要编译之前准备的控件安装啦,控件安装成功后,关闭这个终端;有些控件是之前在进行ROS学习的时候安装过的,但也是本次任务需要的控件,小伙伴对应安装就好,如果安装过,他会提示已安装的哦。接下来,我们开始进行编译吧!
3、racecar功能包编译
(1)、返回ROS工作空间
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros </code>
(2)、编译
<code class="prism language-bash has-numbering">catkin_make </code>
(3)、编译成功如下所示:
编译过程中,如果遇到问题,则可根据提示修改,由于林君学长在编译之后,没有遇到过问题,编译很流畅,如果小伙伴遇到问题,可以给林君学长留言或者私信,看到后,能够解决的会帮大家解决的哦!
4、测试是否编译成功
(1)、程序注册
<code class="prism language-bash has-numbering"><span class="token function">source</span> ./devel/setup.bash </code>
(2)、在终端输入以下命令,运行小车模型,测试编译是否成功
<code class="prism language-bash has-numbering">roslaunch racecar_gazebo racecar.launch </code>
以上命令运行之后,系统将会打开一个tk的控制界面小窗,控制小车的移动,同时,系统会打开Gazebo仿真软件,里面会有一个小车模型,如下所示:
该tk窗口可以通过键盘的WSAD来控制小车进行位移!
二、 运行功能包自带的赛道
1、运行赛道,控制小车进行位移
(1)、在上面ctrl+c结束gazebo的运行,然后运行如下命令,运行赛道
<code class="prism language-bash has-numbering">roslaunch racecar_gazebo racecar_runway.launch </code>
(2)、运行结果如下:
(3)、控制小车进行位移:
2、进行gmapping建图
(1)、新建终端,运行rviz,进行gmapping建图
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros <span class="token function">source</span> ./devel/setup.bash roslaunch racecar_gazebo slam_gmapping.launch </code>
(3)、控制小车跑完全图,进行gmapping建图 (1)、赛道跑到一半
(2)、赛道跑完
现在gmapping地图就建立好了,操控小车进行跑图需要有点耐心,需要一定的时间,而且操作不到位,可能导致小车翻车,所以小心操控呀!接下来就进行地图的保存吧 (4)、通过以下命令,保存gmapping图
<code class="prism language-bash has-numbering">rosrun map_server map_saver -f test_map </code>
保存的地图如下所示:
注意: 由于我们是在ros下运行的,所以地图在ros工作空间下,小伙伴也可以自行定位地图保存的位置例如:
<code class="prism language-bash has-numbering">rosrun map_server map_saver -f xxx/xxx/test_map </code>
一般来说,地图需要保存到map文件夹内,该文件夹专门用来保存我们创建模型的gmapping地图,如下所示:
好了,接下来,我们关闭上面运行的所有程序及所有终端,然后我们就可以开始进行我们自己搭建地图了。 接下来,我们自己搭建一个地图,然后添加障碍物进行小车跑图吧!
三、 自行进行赛道搭建
为了区别刚刚的地图,我们自行创建一个较为简单的地图吧,这也是为了方便本次博客的讲解,下面,我们开始吧!
1、新建终端,打开Gazebo
(1)、输入如下命令,打开gazebo
<code class="prism language-bash has-numbering">gazebo </code>
(2)、点击Edit->Build Editor,进行模型创建
(3)、模型创建完成图如下所示: 对于gazebo怎么创建模型图,小伙伴们可以参考林君学长的另一篇博客,讲解了如何通过gazebo创建仿真模型图及对应的保存,博客链接如下所示: https://blog.csdn.net/qq_42451251/article/details/105161520
(4)、接下来,将创建好的地图模型保存
在对应的位置,我们便可以看到我们创建的赛道模型了:
(5)、关闭gazebo
2、运行小车模型,导入刚刚的模型框架,然后保存world文件
(1)、通过以下代码运行小车模型
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros <span class="token function">source</span> ./devel/setup.bash roslaunch racecar_gazebo racecar.launch </code>
(2)、拖入刚刚我们创建的地图到合适的位置
(3)、添加障碍物
(4)、保存world文件到下载功能包中的racecar_gazebo包中的worlds文件夹中 (1)、选择File->Save World AS
(2)、选择racecar_gazebo包中的worlds文件夹进行保存,自己命名吧,不要和里面的名字重复就好,学长命名为track1
(3)、查看是够保存成功
可以看到模型导入成功,接下来,我们就创建launch文件运行自己的地图吧!
3、创建launch文件,填写赛道配置参数
(1)、新建终端,进入racecar_gazebo工程包中的launch文件夹
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros/src/racecar/racecar_gazebo/launch </code>
(2)、创建track1.launch文件
<code class="prism language-bash has-numbering"><span class="token function">touch</span> track1.launch </code>
(3)、打开track1.launch文件,写入配置参数
<code class="prism language-bash has-numbering">gedit track1.launch </code>
写入如下代码:
<code class="prism language-bash has-numbering"><span class="token operator"><</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span>?<span class="token operator">></span> <span class="token operator"><</span>launch<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span>-- Launch the racecar --<span class="token operator">></span> <span class="token operator"><</span>include file<span class="token operator">=</span><span class="token string">"<span class="token variable">$(<span class="token function">find</span> racecar_gazebo)</span>/launch/racecar.launch"</span><span class="token operator">></span> <span class="token operator"><</span>arg name<span class="token operator">=</span><span class="token string">"world_name"</span> value<span class="token operator">=</span><span class="token string">"track1"</span>/<span class="token operator">></span> <span class="token operator"><</span>/include<span class="token operator">></span> <span class="token operator"><</span>/launch<span class="token operator">></span></code>
然后点击保存后关闭!接下来就可以运行我们自己的赛道了
4、运行我们的地图
(1)、运行地图
<code class="prism language-bash has-numbering"><span class="token function">source</span> ./devel/setup.bash roslaunch racecar_gazebo track1.launch </code>
(2)、控制小车跑图
5、进行gmapping建图
(1)、上面运行的终端不关闭,新建终端,接着输入如下命令,进行gmapping建图
<code class="prism language-bash has-numbering">roslaunch racecar_gazebo slam_gmapping.launch </code>
(2)、控制小车,跑完全图,全图如下所示:
(3)、新建终端,保存gmapping创建的地图
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros/src/racecar/racecar_gazebo/map rosrun map_server map_saver -f track1_map </code>
(4)查看地图是否保存成功
可以看到,地图已经完整的保存了哦;到这里,我们的基本任务就完成了哦,关闭以上运行的所有软件,及所有终端;接下来,我们就让小车自动导航吧,避免人工操作过于繁琐,让它自己跑起来!
四、 实现 小车自主定位导航
1、创建手工导航launch文件
(1)、新建终端,创建track2.launch文件
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros/src/racecar/racecar_gazebo/launch <span class="token function">touch</span> track2.launch gedit track2.launch </code>
(2)、写入配置文件代码
<code class="prism language-bash has-numbering"><span class="token operator"><</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span>?<span class="token operator">></span> <span class="token operator"><</span>launch<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span>-- Launch the racecar --<span class="token operator">></span> <span class="token operator"><</span>include file<span class="token operator">=</span><span class="token string">"<span class="token variable">$(<span class="token function">find</span> racecar_gazebo)</span>/launch/racecar.launch"</span><span class="token operator">></span> <span class="token operator"><</span>arg name<span class="token operator">=</span><span class="token string">"world_name"</span> value<span class="token operator">=</span><span class="token string">"track1"</span>/<span class="token operator">></span> <span class="token operator"><</span>/include<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span>-- Launch the built-map --<span class="token operator">></span> <span class="token operator"><</span>node name<span class="token operator">=</span><span class="token string">"map_server"</span> pkg<span class="token operator">=</span><span class="token string">"map_server"</span> type<span class="token operator">=</span><span class="token string">"map_server"</span> args<span class="token operator">=</span><span class="token string">"<span class="token variable">$(<span class="token function">find</span> racecar_gazebo)</span>/map/track1_map.yaml"</span> /<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span>--Launch the move base with <span class="token function">time</span> elastic band--<span class="token operator">></span> <span class="token operator"><</span>param name<span class="token operator">=</span><span class="token string">"/use_sim_time"</span> value<span class="token operator">=</span><span class="token string">"true"</span>/<span class="token operator">></span> <span class="token operator"><</span>node pkg<span class="token operator">=</span><span class="token string">"move_base"</span> type<span class="token operator">=</span><span class="token string">"move_base"</span> respawn<span class="token operator">=</span><span class="token string">"false"</span> name<span class="token operator">=</span><span class="token string">"move_base"</span> output<span class="token operator">=</span><span class="token string">"screen"</span><span class="token operator">></span> <span class="token operator"><</span>rosparam file<span class="token operator">=</span><span class="token string">"<span class="token variable">$(<span class="token function">find</span> racecar_gazebo)</span>/config/costmap_common_params.yaml"</span> command<span class="token operator">=</span><span class="token string">"load"</span> ns<span class="token operator">=</span><span class="token string">"global_costmap"</span> /<span class="token operator">></span> <span class="token operator"><</span>rosparam file<span class="token operator">=</span><span class="token string">"<span class="token variable">$(<span class="token function">find</span> racecar_gazebo)</span>/config/costmap_common_params.yaml"</span> command<span class="token operator">=</span><span class="token string">"load"</span> ns<span class="token operator">=</span><span class="token string">"local_costmap"</span> /<span class="token operator">></span> <span class="token operator"><</span>rosparam file<span class="token operator">=</span><span class="token string">"<span class="token variable">$(<span class="token function">find</span> racecar_gazebo)</span>/config/local_costmap_params.yaml"</span> command<span class="token operator">=</span><span class="token string">"load"</span> /<span class="token operator">></span> <span class="token operator"><</span>rosparam file<span class="token operator">=</span><span class="token string">"<span class="token variable">$(<span class="token function">find</span> racecar_gazebo)</span>/config/global_costmap_params.yaml"</span> command<span class="token operator">=</span><span class="token string">"load"</span> /<span class="token operator">></span> <span class="token operator"><</span>rosparam file<span class="token operator">=</span><span class="token string">"<span class="token variable">$(<span class="token function">find</span> racecar_gazebo)</span>/config/teb_local_planner_params.yaml"</span> command<span class="token operator">=</span><span class="token string">"load"</span> /<span class="token operator">></span> <span class="token operator"><</span>param name<span class="token operator">=</span><span class="token string">"base_global_planner"</span> value<span class="token operator">=</span><span class="token string">"global_planner/GlobalPlanner"</span> /<span class="token operator">></span> <span class="token operator"><</span>param name<span class="token operator">=</span><span class="token string">"planner_frequency"</span> value<span class="token operator">=</span><span class="token string">"0.01"</span> /<span class="token operator">></span> <span class="token operator"><</span>param name<span class="token operator">=</span><span class="token string">"planner_patience"</span> value<span class="token operator">=</span><span class="token string">"5.0"</span> /<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span>--param name<span class="token operator">=</span><span class="token string">"use_dijkstra"</span> value<span class="token operator">=</span><span class="token string">"false"</span> /--<span class="token operator">></span> <span class="token operator"><</span>param name<span class="token operator">=</span><span class="token string">"base_local_planner"</span> value<span class="token operator">=</span><span class="token string">"teb_local_planner/TebLocalPlannerROS"</span> /<span class="token operator">></span> <span class="token operator"><</span>param name<span class="token operator">=</span><span class="token string">"controller_frequency"</span> value<span class="token operator">=</span><span class="token string">"5.0"</span> /<span class="token operator">></span> <span class="token operator"><</span>param name<span class="token operator">=</span><span class="token string">"controller_patience"</span> value<span class="token operator">=</span><span class="token string">"15.0"</span> /<span class="token operator">></span> <span class="token operator"><</span>param name<span class="token operator">=</span><span class="token string">"clearing_rotation_allowed"</span> value<span class="token operator">=</span><span class="token string">"false"</span> /<span class="token operator">></span> <span class="token operator"><</span>/node<span class="token operator">></span> <span class="token operator"><</span>/launch<span class="token operator">></span></code>
(3)、如何改为你们的? 小伙伴只需要如下图所示,该其中两个地方,改为你们自己的模型名和保存地图的yaml文件就可以了:
2、运行手工导航
(1)、输入如下命令,运行我们的赛道
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros <span class="token function">source</span> ./devel/setup.bash roslaunch racecar_gazebo track2.launch </code>
(2)、新建终端,运行RVIZ进行手工导航
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros <span class="token function">source</span> ./devel/setup.bash roslaunch racecar_gazebo racecar_rviz.launch </code>
(3)、新建终端,启动导航的path_pursuit.py文件
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros <span class="token function">source</span> ./devel/setup.bash rosrun racecar_gazebo path_pursuit.py </code>
3、选择2D Nav Goal指针,进行手工导航
(1)、选择2D Nav Goal指针
(2)、通过2D Nav Goal指针选择地图上面的一个点进行手工导航
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200604175733458.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNDUxMjUx,size_16,color_FFFFFF,t_70)
<code class="prism language-bash has-numbering"><span class="token function">cd</span> ~/lenovo/ros <span class="token function">source</span> ./devel/setup.bash rosrun racecar_gazebo path_pursuit.py </code>
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200604175816466.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNDUxMjUx,size_16,color_FFFFFF,t_70)
![利用Gazebo搭建赛道,控制小车完成比赛(大学生智能车大赛室外光电组赛道仿真模拟) 利用Gazebo搭建赛道,控制小车完成比赛(大学生智能车大赛室外光电组赛道仿真模拟)](https://img-blog.csdnimg.cn/20200604180556895.png)