智能小车建图导航-在Gazebo中仿真SLAM(代码详解gmapping)

733
0
2020年12月25日 09时02分

目录

一、解读launch文件:mrobot_laser_nav_gazebo.launch

二、解读launch文件:gmapping_demo.launch

打开launch文件gmapping.launch:

三、解读launch文件:mrobot_teleop.launch

打开Python文件mrobot_teleop.py:


 

一、解读launch文件:mrobot_laser_nav_gazebo.launch

 

<launch>
 
    <!-- 设置launch文件的参数 -->
<!-- 这里的设置的参数在接下来的配置当中都会用到 -->
    <arg name="world_name" value="$(find mrobot_gazebo)/worlds/cloister.world"/>
    <arg name="paused" default="false"/>
    <arg name="use_sim_time" default="true"/>
    <arg name="gui" default="true"/>
    <arg name="headless" default="false"/>
    <arg name="debug" default="false"/>
 
    <!-- 运行gazebo仿真环境 -->
<!-- 这个基本操作一般都不会变,我们直接按照这个写下来就行,因为只要改上面的参数就行 -->
    <include file="$(find gazebo_ros)/launch/empty_world.launch">
        <arg name="world_name" value="$(arg world_name)" />
        <arg name="debug" value="$(arg debug)" />
        <arg name="gui" value="$(arg gui)" />
        <arg name="paused" value="$(arg paused)"/>
        <arg name="use_sim_time" value="$(arg use_sim_time)"/>
        <arg name="headless" value="$(arg headless)"/>
    </include>
 
    <!-- 加载机器人模型描述参数 -->
<!-- 这个机器人模型参数在后面gazebo加载机器人模型的语句中会用到 -->
    <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find mrobot_gazebo)/urdf/mrobot_with_rplidar.urdf.xacro'" /> 
 
    <!-- 运行joint_state_publisher节点,发布机器人的关节状态(/joint_states的话题),然后由robot_state_publisher节点订阅 -->
    <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node> 
 
    <!-- 运行robot_state_publisher节点,发布tf  -->
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher"  output="screen" >
        <param name="publish_frequency" type="double" value="50.0" />
    </node>
 
    <!-- 在gazebo中加载机器人模型-->
    <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
          args="-urdf -model mrobot -param robot_description"/> 
 
</launch>

 

二、解读launch文件:gmapping_demo.launch

 

<launch>
 
    <include file="$(find mrobot_navigation)/launch/gmapping.launch"/>
 
    <!-- 启动rviz -->
    <node pkg="rviz" type="rviz" name="rviz" args="-d $(find mrobot_navigation)/rviz/gmapping.rviz"/>
 
</launch>

 

这个文件重点分为两个部分:一个是打开gmapping.launch文件,另一个是启动rviz

 

打开launch文件gmapping.launch:

 

<launch>
    <arg name="scan_topic" default="scan" />
<!-- 设置变量激光雷达话题的名称 -->
 
    <node pkg="gmapping" type="slam_gmapping" name="slam_gmapping" output="screen" clear_params="true">
<!-- 以下的参数全部都是配置slam_gmapping这个可执行文件用的,这个是一个c语言编译好的一个可执行文件。直接用,配置参数就好 -->
 
        <param name="odom_frame" value="odom"/>
<!-- 配置里程计坐标系的名称值 -->
        <param name="map_update_interval" value="5.0"/>
<!-- 配置地图更新的时间间隔默认值都是5.0 -->
 
        <!-- Set maxUrange < actual maximum range of the Laser -->
<!-- 设置激光雷达的参数范围,这些参数在激光雷达和gmapping的那篇介绍的博客里面都有 -->
        <param name="maxRange" value="5.0"/>
        <param name="maxUrange" value="4.5"/>
        <param name="sigma" value="0.05"/>
        <param name="kernelSize" value="1"/>
        <param name="lstep" value="0.05"/>
        <param name="astep" value="0.05"/>
        <param name="iterations" value="5"/>
        <param name="lsigma" value="0.075"/>
        <param name="ogain" value="3.0"/>
        <param name="lskip" value="0"/>
        <param name="srr" value="0.01"/>
        <param name="srt" value="0.02"/>
        <param name="str" value="0.01"/>
        <param name="stt" value="0.02"/>
        <param name="linearUpdate" value="0.5"/>
        <param name="angularUpdate" value="0.436"/>
        <param name="temporalUpdate" value="-1.0"/>
        <param name="resampleThreshold" value="0.5"/>
        <param name="particles" value="80"/>
        <param name="xmin" value="-1.0"/>
        <param name="ymin" value="-1.0"/>
        <param name="xmax" value="1.0"/>
        <param name="ymax" value="1.0"/>
        <param name="delta" value="0.05"/>
        <param name="llsamplerange" value="0.01"/>
        <param name="llsamplestep" value="0.01"/>
        <param name="lasamplerange" value="0.005"/>
        <param name="lasamplestep" value="0.005"/>
        <remap from="scan" to="$(arg scan_topic)"/>
    </node>
</launch>

 

三、解读launch文件:mrobot_teleop.launch

 

<launch>
  <node name="mrobot_teleop" pkg="mrobot_teleop" type="mrobot_teleop.py" output="screen">
<!-- 设置节点的名字,运行Python可执行文件 -->
    <param name="scale_linear" value="0.1" type="double"/>
    <param name="scale_angular" value="0.4" type="double"/>
  </node>
</launch>

 

打开Python文件mrobot_teleop.py:

其实这个Python文件还是很简单的,主要是通过键盘的控制由话题Twist来发布机器人角速度和线速度的消息

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
from geometry_msgs.msg import Twist
import sys, select, termios, tty
 
#这个msg是最一开始发布在终端的提示
msg = """
Control mrobot!
---------------------------
Moving around:
   u    i    o
   j    k    l
   m    ,    .
q/z : increase/decrease max speeds by 10%
w/x : increase/decrease only linear speed by 10%
e/c : increase/decrease only angular speed by 10%
space key, k : force stop
anything else : stop smoothly
CTRL-C to quit
"""
#元组里面的第一个参数表示前后(1表示向前),元组里面的第二个参数表示转向左右(1表示左转)
moveBindings = {
        'i':(1,0),
        'o':(1,-1),
        'j':(0,1),
        'l':(0,-1),
        'u':(1,1),
        ',':(-1,0),
        '.':(-1,1),
        'm':(-1,-1),
           }
#可以理解为速度增减的步长
speedBindings={
        'q':(1.1,1.1),
        'z':(.9,.9),
        'w':(1.1,1),
        'x':(.9,1),
        'e':(1,1.1),
        'c':(1,.9),
          }
#下面这个函数主要是监听以及得到键盘的输入,在px4中的那篇博客中已经讨论过了,直接套用就可以
def getKey():
    tty.setraw(sys.stdin.fileno())
    rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
    if rlist:
        key = sys.stdin.read(1)
    else:
        key = ''
 
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
    return key
 
#设置速度和方向的初始值
speed = .2
turn = 1
 
def vels(speed,turn):
    return "currently:\tspeed %s\tturn %s " % (speed,turn)
 
if __name__=="__main__":
#这个settings和代码最后的那一句都是配合getKey()函数来获得键盘的输入用的
    settings = termios.tcgetattr(sys.stdin)
    
#节点初始化
    rospy.init_node('mrobot_teleop')
#创建速度控制的话题    
    pub = rospy.Publisher('/cmd_vel', Twist, queue_size=5)
#初始化速度角度状态方向等等
    x = 0
    th = 0
    status = 0
    count = 0
    acc = 0.1
    target_speed = 0
    target_turn = 0
    control_speed = 0
    control_turn = 0
#代码接下来主要靠try、except、finally来完成
    try:
        print msg
        print vels(speed,turn)
        while(1):
            key = getKey()
            # 运动控制方向键(1:正方向,-1负方向)
            if key in moveBindings.keys():
                x = moveBindings[key][0]
                th = moveBindings[key][1]
                count = 0
            # 速度修改键
            elif key in speedBindings.keys():
                speed = speed * speedBindings[key][0]  # 线速度增加0.1倍
                turn = turn * speedBindings[key][1]    # 角速度增加0.1倍
                count = 0
 
                print vels(speed,turn)
                if (status == 14):
                    print msg
                status = (status + 1) % 15
            # 停止键
            elif key == ' ' or key == 'k' :
                x = 0
                th = 0
                control_speed = 0
                control_turn = 0
            else:
                count = count + 1
                if count > 4:
                    x = 0
                    th = 0
                if (key == '\x03'):
                    break
 
            # 目标速度=速度值*方向值
            target_speed = speed * x
            target_turn = turn * th
 
            # 速度限位,防止速度增减过快
            if target_speed > control_speed:
                control_speed = min( target_speed, control_speed + 0.02 )
            elif target_speed < control_speed:
                control_speed = max( target_speed, control_speed - 0.02 )
            else:
                control_speed = target_speed
 
            if target_turn > control_turn:
                control_turn = min( target_turn, control_turn + 0.1 )
            elif target_turn < control_turn:
                control_turn = max( target_turn, control_turn - 0.1 )
            else:
                control_turn = target_turn
 
# 创建并发布twist消息,由于这个是一个二维平面,所以只涉及到以z轴为轴的转向,以及只涉及x方向的线速度(因为前面只有设置了前后)
            twist = Twist()
            twist.linear.x = control_speed; 
            twist.linear.y = 0; 
            twist.linear.z = 0
            twist.angular.x = 0; 
            twist.angular.y = 0; 
            twist.angular.z = control_turn
            pub.publish(twist)
 
    except:
        print e
 
    finally:
        twist = Twist()
        twist.linear.x = 0; twist.linear.y = 0; twist.linear.z = 0
        twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = 0
        pub.publish(twist)
 
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)

 

 

发表评论

后才能评论