tutlebot3仿真

1.安装tutlebot3

sudo apt-get install ros-melodic-turtlebot3-*
# 安装地图算法依赖
apt-get install ros-melodic-gmapping 
# 安装dwa路径规划算法
apt-get install ros-melodic-dwa-local-planner 


sudo apt-get install ros-melodic-turtlebot3-*
# 安装地图算法依赖
apt-get install ros-melodic-gmapping 
# 安装dwa路径规划算法
apt-get install ros-melodic-dwa-local-planner 

2. 控制小车运动

首先我们可以在仿真环境中启动tutlebot3小车

export TURTLEBOT3_MODEL=waffle_pi
roslaunch turtlebot3_gazebo turtlebot3_empty_world.launch

然后执行下面的命令,根据输出提示即可控制小车运动

rosrun teleop_twist_keyboard teleop_twist_keyboard.py

若小车不能运动,则有可能是缺少依赖

sudo apt-get install ros-melodic-gazebo-ros-pkgs ros-melodic-gazebo-ros-control
sudo apt-get install ros-melodic-teleop-twist-keyboard 

若安装出现[Err] [REST.cc:205] Error in REST request则需要进行如下配置:

sudo gedit ~/.ignition/fuel/config.yaml

将文件中https://api.ignitionfuel.org修改为https://api.ignitionrobotics.org

若经过上述步骤还是无法打开gazebo ,如果是虚拟机的话,就需要关闭3D加速.

先执行如下命令

export SVGA_VGPU10=0

然后在虚拟机设置的显示中关闭3D加速

3. slam构建地图

  1. 开启仿真环境
export TURTLEBOT3_MODEL=waffle_pi
roslaunch turtlebot3_gazebo turtlebot3_house.launch
  1. 开启实时建图功能,算法选择gmapping, 还有多种slam算法, 如cartographer, hector_slam
export TURTLEBOT3_MODEL=waffle_pi
roslaunch turtlebot3_slam turtlebot3_slam.launch slam_methods:=gmapping
  1. 控制turtlebot3
rosrun teleop_twist_keyboard teleop_twist_keyboard.py
或者
export TURTLEBOT3_MODEL=waffle_pi
roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
  1. 尽可能保证地图闭合,然后保存地图为文件
rosrun map_server map_saver -f ~/map

4. slam自动导航

# 打开仿真环境
export TURTLEBOT3_MODEL=waffle_pi
roslaunch turtlebot3_gazebo turtlebot3_house.launch
# 打开自动导航功能
export TURTLEBOT3_MODEL=waffle_pi
roslaunch turtlebot3_navigation turtlebot3_navigation.launch map_file:=/home/kaijun/map.yaml

在rviz界面上,设定目标点,小车就会自动运行到目标位置去。

5. 代码实现导航

这一块的内容和我们刚才讲过的内容很类似,这里我们通过代码的方式来实现小车的导航功能

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy
import actionlib
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
from actionlib_msgs.msg import *
from geometry_msgs.msg import Point

# 导航移动api
def move_to_goal(xGoal,yGoal):
    #simpleactionclient
    ac = actionlib.SimpleActionClient("move_base", MoveBaseAction)
    #等待5秒 ,actionserver启动
    while(not ac.wait_for_server(rospy.Duration.from_sec(5.0))):
        rospy.loginfo("等待move_base actionserver启动")
    goal = MoveBaseGoal()
    #指定地图参考系
    goal.target_pose.header.frame_id = "map"
    goal.target_pose.header.stamp = rospy.Time.now()
    #移动目标设定位姿 xyz和四元数
    goal.target_pose.pose.position = Point(xGoal,yGoal,0)
    goal.target_pose.pose.orientation.x = 0.0
    goal.target_pose.pose.orientation.y = 0.0
    goal.target_pose.pose.orientation.z = 0.0
    goal.target_pose.pose.orientation.w = 1.0
    rospy.loginfo("发送目标到actionserver ...")
    ac.send_goal(goal)
    #设置超时时间为60s
    ac.wait_for_result(rospy.Duration(60))
    if(ac.get_state() == GoalStatus.SUCCEEDED):
        rospy.loginfo("成功到达")
        return True
    else:
        rospy.loginfo("未在规定时间内到达目的地,失败了")
        return False

if __name__ == '__main__':
    rospy.init_node('map_navigation', anonymous=False)
    x_goal = 1
    y_goal = 0
    print'start go to goal'
    move_to_goal(x_goal,y_goal)
    rospy.spin()

执行如下代码即可让小车运动到指定的地图坐标:

kaijun@kaijun-pc:~/Documents/slam/slam_ws$ rosrun heima_slam slam_demo.py