赵虚左老师ROS导航教程中详细介绍了仿真环境、机器人的搭建,以及SLAM模块的加载。由于涉及的内容较多,将实现过程进行记录整理,以供参考。
安装需要的包
sudo apt install ros-melodic-gmapping
sudo apt install ros-melodic-teleop-twist-keyboard
新建demo工作空间
mkdir -p ~/demo_ws/src
cd ~/demo_ws/src
catkin_init_workspace
启动vs
cd ~/demo_ws/src
code .
ctrl+shift+b配置vs中ros构建,修改task.json文件
{
// 有关 tasks.json 格式的文档,请参见
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"label": "catkin_make:debug", //代表提示的描述性信息
"type": "shell", //可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
"command": "catkin_make",//这个是我们需要运行的命令
"args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
"group": {"kind":"build","isDefault":true},
"presentation": {
"reveal": "always"//可选always或者silence,代表是否输出信息
},
"problemMatcher": "$msCompile"
}
]
}
修改c_cpp_properties.json
{
"configurations": [
{
"browse": {
"databaseFilename": "${default}",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/opt/ros/melodic/include/**",
"/usr/include/**"
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
src中右键create catkin package,建立urdf02_gazebo项目,依赖为urdf、xacro、gazebo_ros、gazebo_ros_control、gazebo_plugins
在urdf02_gazebo项目下新建worlds文件夹,并在文件夹下新建box_house.world文件
1
0 0 10 0 -0 0
0.8 0.8 0.8 1
0.2 0.2 0.2 1
1000
0.9
0.01
0.001
-0.5 0.1 -0.9
0
0
0
1
0 0 1
100 100
65535
100
50
10
0
0 0 1
100 100
0
0
0
0 0 -9.8
6e-06 2.3e-05 -4.2e-05
0.001
1
1000
0.4 0.4 0.4 1
0.7 0.7 0.7 1
1
EARTH_WGS84
0
0
0
0
-1.97643 0.059964 0 0 -0 0
13 0.15 2.5
0 0 1.25 0 -0 0
10
0 0 1.25 0 -0 0
13 0.15 2.5
1 1 1 1
0
0 3.925 0 0 -0 0
0
0
0
8 0.15 2.5
0 0 1.25 0 -0 0
10
0 0 1.25 0 -0 0
8 0.15 2.5
1 1 1 1
0
6.425 -0 0 0 -0 -1.5708
0
0
0
13 0.15 2.5
0 0 1.25 0 -0 0
10
0 0 1.25 0 -0 0
13 0.15 2.5
1 1 1 1
0
0 -3.925 0 0 -0 3.14159
0
0
0
8 0.15 2.5
0 0 1.25 0 -0 0
10
0 0 1.25 0 -0 0
8 0.15 2.5
1 1 1 1
0
-6.425 -0 0 0 -0 1.5708
0
0
0
1
-7.27143 3.09748 0.5 0 -0 0
1
0.166667
0
0
0.166667
0
0.166667
0 0 0 0 -0 0
1 1 1
10
1 1 1
0
0
0
-4.57746 0.509886 0.5 0 -0 0
1
0.145833
0
0
0.145833
0
0.125
0 0 0 0 -0 0
0.5
1
10
0.5
1
0
0
0
257 459000000
196 129745222
1593764309 76438721
194518
-1.97643 0.05996 0 0 -0 0
1 1 1
-1.97643 3.98496 0 0 -0 0
0 0 0 0 -0 0
0 0 0 0 -0 0
0 0 0 0 -0 0
4.44857 0.059964 0 0 0 -1.5708
0 0 0 0 -0 0
0 0 0 0 -0 0
0 0 0 0 -0 0
-1.97643 -3.86504 0 0 -0 3.14159
0 0 0 0 -0 0
0 0 0 0 -0 0
0 0 0 0 -0 0
-8.40143 0.059964 0 0 -0 1.5708
0 0 0 0 -0 0
0 0 0 0 -0 0
0 0 0 0 -0 0
0 0 0 0 -0 0
1 1 1
0 0 0 0 -0 0
0 0 0 0 -0 0
0 0 0 0 -0 0
0 0 0 0 -0 0
-7.27142 3.09748 0.499995 0 1e-05 0
1 1 1
-7.27142 3.09748 0.499995 0 1e-05 0
0 0 0 0 -0 0
0.010615 -0.006191 -9.78231 0.012424 0.021225 1.8e-05
0.010615 -0.006191 -9.78231 0 -0 0
-4.96407 -2.00353 0.499995 -1e-05 -0 -0
1 1 1
-4.96407 -2.00353 0.499995 -1e-05 -0 -0
0 0 0 0 -0 0
0.004709 0.011055 -9.78158 -0.022108 0.009414 1e-06
0.004709 0.011055 -9.78158 0 -0 0
3.83312 3.3034 0.499995 0 1e-05 0
1 1 1
3.83312 3.3034 0.499995 0 1e-05 0
0 0 0 0 -0 0
0.010615 -0.006191 -9.78231 0.012424 0.021225 1.8e-05
0.010615 -0.006191 -9.78231 0 -0 0
-4.57746 0.509884 0.499998 3e-06 4e-06 -0
1 1 1
-4.57746 0.509884 0.499998 3e-06 4e-06 -0
0 0 0 0 -0 0
0 0 -9.8 0 -0 0
0 0 -9.8 0 -0 0
-0.988146 2.12658 0.499993 -3e-06 -4e-06 -0
1 1 1
-0.988146 2.12658 0.499993 -3e-06 -4e-06 -0
0 0 0 0 -0 0
0 0 -9.8 0 -0 0
0 0 -9.8 0 -0 0
-0.890576 -1.90634 0.499997 3e-06 4e-06 -0
1 1 1
-0.890576 -1.90634 0.499997 3e-06 4e-06 -0
0 0 0 0 -0 0
0 0 -9.8 0 -0 0
0 0 -9.8 0 -0 0
0 0 10 0 -0 0
-2.68744 -4.4037 20.9537 -0 1.31164 1.40819
orbit
perspective
-4.96407 -2.00354 0.5 0 -0 0
1
0.166667
0
0
0.166667
0
0.166667
0 0 0 0 -0 0
1 1 1
10
1 1 1
0
0
0
3.83312 3.3034 0.5 0 -0 0
1
0.166667
0
0
0.166667
0
0.166667
0 0 0 0 -0 0
1 1 1
10
1 1 1
0
0
0
-0.988144 2.12658 0.5 0 -0 0
1
0.145833
0
0
0.145833
0
0.125
0 0 0 0 -0 0
0.5
1
10
0.5
1
0
0
0
-0.890578 -1.90634 0.5 0 -0 0
1
0.145833
0
0
0.145833
0
0.125
0 0 0 0 -0 0
0.5
1
10
0.5
1
0
0
0
在urdf02_gazebo项目urdf文件夹下,新建demo05_car_base.urdf.xacro文件
Gazebo/Yellow
Gazebo/Red
Gazebo/Red
在urdf02_gazebo项目urdf文件夹下,新建demo06_car_camera.urdf.xacro文件
Gazebo/Blue
在urdf02_gazebo项目urdf文件夹下,新建demo07_car_laser.urdf.xacro文件
Gazebo/White
Gazebo/Black
在urdf02_gazebo项目urdf文件夹下,新建head.xacro文件
在urdf02_gazebo项目urdf文件夹下,新建car.xacro文件
在urdf02_gazebo项目urdf文件夹下新建gazebo文件夹,新建move.xacro
transmission_interface/SimpleTransmission
hardware_interface/VelocityJointInterface
hardware_interface/VelocityJointInterface
1
Debug
true
/
1
true
true
100.0
true
left_wheel2base_link
right_wheel2base_link
${base_link_radius * 2}
${wheel_radius * 2}
1
30
1.8
world
true
cmd_vel
odom
odom
base_footprint
在urdf02_gazebo\urdf\gazebo\,新建camera.xacro
30.0
1.3962634
1280
720
R8G8B8
0.02
300
gaussian
0.0
0.007
true
0.0
/camera
image_raw
camera_info
camera
0.07
0.0
0.0
0.0
0.0
0.0
在urdf02_gazebo\urdf\gazebo\,新建kinect.xacro
true
20.0
${60.0*PI/180.0}
R8G8B8
640
480
0.05
8.0
camera
true
10
rgb/image_raw
depth/image_raw
depth/points
rgb/camera_info
depth/camera_info
support_depth
0.1
0.0
0.0
0.0
0.0
0.0
0.4
在urdf02_gazebo\urdf\gazebo\,新建laser.xacro
0 0 0 0 0 0
true
5.5
360
1
-3
3
0.10
30.0
0.01
gaussian
0.0
0.01
/scan
laser
在urdf02_gazebo项目launch文件夹下,新建demo03_env.launch文件
在urdf02_gazebo项目launch文件夹下,新建demo04_sensor.launch文件
注意,若xacro中存在中文字符,可能会导致'ascii' codec can't encode character u'\uff0c' in position 27: ordinal not in range(128)类的错误,该问题可通过下述方法解决:
ROS关于UnicodeEncodeError的问题_weixin_47236464的博客-CSDN博客
使用roslaunch加载03环境仿真
source ./devel/setup.bash
roslaunch urdf02_gazebo demo03_env.launch
可在Gazebo中看到建立的机器人与环境
同时,另外打开terminal,使用roslaunch加载04传感器数据
source ./devel/setup.bash
roslaunch urdf02_gazebo demo04_sensor.launch
在RVIZ中进行下述操作
可看到传感器的结果
src中右键create catkin package,建立nav_demo项目
在nav_demo项目下新建launch文件夹,并新建nav01_slam.launch
在terminal中加载仿真环境
source ./devel/setup.bash
roslaunch urdf02_gazebo demo03_env.launch
在新开terminal中加载导航模块
source ./devel/setup.bash
roslaunch nav_demo nav01_slam.launch
在RVIZ中进行下述操作
启动teleop_twist_keyboard用键盘控制机器人移动
rosrun teleop_twist_keyboard teleop_twist_keyboard.py
伴随机器人移动,可看到构建的地图逐渐完整
参考链接
ROS入门教程-理论与实践(6.6.3 URDF集成Gazebo实操) - 知乎 (zhihu.com)
ROS入门教程-理论与实践(7.2.1 导航实现01_SLAM建图) - 知乎 (zhihu.com)