本文记录在之前已经安装完毕cartographer和turtlebot3的完整仿真环境的workspace的基础上安装和使用自主建图包的过程,包括m-explore和rrt-exploration两种包,可以分别独立使用。
cd ~/carto_ws/src
git clone -b melodic-devel https://github.com/hrnr/m-explore.git
cd ..
rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y
catkin_make_isolated
m-explore包在计算frontier时使用的地图格式,假定地图中的栅格仅包含三个值:
因此,在 查找时仅会查找在[EMPTY]空间附近的[UNKNOWN]空间作为frontier。而cartographer生成的地图栅格值包含灰度。因此需要进行转换。
cd ~/carto_ws/
catkin_create_pkg mypatches
roscd mypatches
mkdir scripts
touch carto_to_gmapping.py
编辑carto_to_gmapping.py文件,增加如下内容:
#!/usr/bin/python
import rospy
from nav_msgs.msg import OccupancyGrid
# ***************
# OBSTACLE
M = 75
# unknown
N = 50
# free
# ----0-----
# unknown
# ***************
def callback(cmap):
data = list(cmap.data)
for y in range(cmap.info.height):
for x in range(cmap.info.width):
i = x + (cmap.info.height - 1 - y) * cmap.info.width
if data[i] >= M:
data[i] = 100
elif (data[i] >= 0) and (data[i] < N): # free
data[i] = 0
else: # unknown
data[i] = -1
cmap.data = tuple(data)
pub.publish(cmap)
rospy.init_node('mapc_node', anonymous=True)
sub = rospy.Subscriber('/map', OccupancyGrid, callback)
pub = rospy.Publisher('/cmap', OccupancyGrid, queue_size=20)
rospy.spin()
修改~/carto_ws/src/m-explore/explore/launch/explore.launch文件,将其中的
修改为
新建一个Terminal窗口,运行如下命令
source ~/carto_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=burger
roslaunch turtlebot3_gazebo turtlebot3_house.launch
新建一个Terminal窗口,运行如下命令
source ~/carto_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=burger
roslaucn turtlebot3_slam turtlebot3_slam.launch slam_methods:=cartographer configuration_basename:=turtlebot3_lds_2d_gazebo.lua
新建一个Terminal窗口,运行如下命令
source ~/carto_ws/devel_isolated/setup.bash
rosrun mypatches carto_to_gmapping
新建一个Terminal窗口,运行如下命令
source ~/carto_ws/devel_isolated/setup.bash
roslaunch explore_lite explore.launch
此时可以从rviz上看到turtlebot3开始自主建图。
cd ~/carto_ws/src
git clone https://github.com/hrnr/rrt-exploration.git
cd ..
rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y
catkin_make_isolated
由于rrt-exploration没有针对melodic版本的分支,需要做相应的手动修改。
修改~/carto_ws/src/rrt_exploration/launch/simple.launch文件,将
修改为
将
修改为
在"
按下图修改~/carto_ws/src/rrt_exploration/scripts/functions.py文件:
rrt_exploration模块需要订阅接收5个/clicked_point消息来确定需要探索的空间区域和起始点,这个动作一般通过rviz上的PublishPoint来完成。为了简化这个过程,可以写一个脚本自动进行此动作:
新建一个文件~/carto_ws/src/mypatches/scripts/pub_clicked_points.sh,增加如下内容:
#!/bin/sh
rostopic pub -1 /clicked_point geometry_msgs/PointStamped "header:
seq: 0
stamp:
secs: 0
nsecs: 0
frame_id: 'map'
point:
x: -5.0
y: 5.0
z: 0.0"
rostopic pub -1 /clicked_point geometry_msgs/PointStamped "header:
seq: 0
stamp:
secs: 0
nsecs: 0
frame_id: 'map'
point:
x: -5.0
y: -5.0
z: 0.0"
rostopic pub -1 /clicked_point geometry_msgs/PointStamped "header:
seq: 0
stamp:
secs: 0
nsecs: 0
frame_id: 'map'
point:
x: 5.0
y: -5.0
z: 0.0"
rostopic pub -1 /clicked_point geometry_msgs/PointStamped "header:
seq: 0
stamp:
secs: 0
nsecs: 0
frame_id: 'map'
point:
x: 5.0
y: 5.0
z: 0.0"
rostopic pub -1 /clicked_point geometry_msgs/PointStamped "header:
seq: 0
stamp:
secs: 0
nsecs: 0
frame_id: 'map'
point:
x: 0.0
y: 0.0
z: 0.0"
新建一个Terminal窗口,运行如下命令
source ~/carto_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=burger
roslaunch turtlebot3_gazebo turtlebot3_house.launch
新建一个Terminal窗口,运行如下命令
source ~/carto_ws/devel_isolated/setup.bash
export TURTLEBOT3_MODEL=burger
roslaucn turtlebot3_slam turtlebot3_slam.launch slam_methods:=cartographer configuration_basename:=turtlebot3_lds_2d_gazebo.lua
新建一个Terminal窗口,运行如下命令
source ~/carto_ws/devel_isolated/setup.bash
roslaunch rrt_exploration simple.launch
新建一个Terminal窗口,运行如下命令
cd ~/carto_ws/src/mypatches/scripts
source ./pub_clicked_points.sh
此时可以从rviz上看到turtlebot3开始自主建图。