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

目录

一、解读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文件:gmapping_demo.launch



    

    
    

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

打开launch文件gmapping.launch:


    


    


        

        


        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

三、解读launch文件:mrobot_teleop.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)

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

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(ROS)