建造属于你的无人驾驶车——(六)使用ARBOTIX仿真器+RVIZ控制(第一种方式动起来,较简单)

建造属于你的无人驾驶车!
本专栏持续更新中…
程序源码:https://github.com/kkmd66/ZZX_RUN
Solidworks模型文件:https://github.com/kkmd66/ZZX_RUN/releases

使用ARBOTIX仿真器+RVIZ

第一步,源码安装arbotix_ros

源码位置:https://github.com/kkmd66/ZZX_RUN/tree/main/arbotix_ros
放在src文件下,编译。

第二步,添加arbotix配置文件

文件位置:zzx_run_description/config/fake_zzx_run_arbotix.yaml

controllers: {
   base_controller: {type: diff_controller, base_frame_id: base_link, base_width: 0.6, ticks_meter: 4100, Kp: 12, Kd: 12, Ki: 0, Ko: 50, accel_limit: 1.0 }
}

第三步,添加RVIZ配置文件

文件位置:zzx_run_description/config/zzx_run_arbotix.rviz

此时,arbotix控制器已经配置完成,下面配置键盘控制脚本。

第四步,创建teleop控制脚本

文件位置:zzx_run_teleop/scripts/zzx_run_robot_teleop.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
from geometry_msgs.msg import Twist
import sys, select, termios, tty

msg = """
控制 zzx_run_robot!
---------------------------
控制:

   u    i    o

   j    k    l

   m    ,    .

q/z :           增加/降低最大速度 by 10%
w/x :           仅增加/降低 线速度 by 10%
e/c :           仅增加/降低 角速度 by 10%
空格,k :         立即停止
anything else : 缓慢停止

CTRL-C to 退出
"""

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),
          }

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 = 1
turn = 1

def vels(speed,turn):
    return "currently:\tspeed %s\tturn %s " % (speed,turn)

if __name__=="__main__":
    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:
        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消息
            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)

第五步,创建键盘控制启动文件

文件位置:src/zzx_run_teleop/zzx_run_robot_teleop.launch

<launch>
  <node name="zzx_run_robot_teleop" pkg="zzx_run_teleop" type="zzx_run_robot_teleop.py" output="screen">
    <param name="scale_linear" value="1.0" type="double"/>
    <param name="scale_angular" value="1.0" type="double"/>
  node>
launch>

启动!

roslaunch zzx_run_description arbotix.launch
roslaunch zzx_run_teleop zzx_run_robot_teleop.launch

效果:

建造属于你的无人驾驶车——(六)使用ARBOTIX仿真器+RVIZ控制(第一种方式动起来,较简单)_第1张图片

最后

有问题可以和我交流哦~
WeChat ID:kkmd66-2

你可能感兴趣的:(ZZX_RUN,动态规划,人工智能,计算机视觉,算法)