Apollo学习——键盘控制速度

python 复制代码
# keyboard_control.py
import time
import keyboard # 键盘输入模块 pip install keyboard
from getkey import getkey, keys
from cyber.python.cyber_py3 import cyber_time
from cyber.python.cyber_py3 import cyber
from modules.common_msgs.control_msgs import control_cmd_pb2
from modules.common_msgs.chassis_msgs import chassis_pb2
import sys, select, os
if os.name == 'nt':
  import msvcrt, time
else:
  import tty, termios
derta_speed = 2.0  # 单位:m/s
derta_heading = 2.0  # 单位:m/s
def getKey():
    if os.name == 'nt':
        timeout = 0.1
        startTime = time.time()
        while(1):
            if msvcrt.kbhit():
                if sys.version_info[0] >= 3:
                    return msvcrt.getch().decode()
                else:
                    return msvcrt.getch()
            elif time.time() - startTime > timeout:
                return ''

    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
class CarController:
    def __init__(self):
        # 控制参数初始化
        self.throttle = 0.0    # 油门 (0.0~1.0)
        self.brake = 0.0       # 刹车 (0.0~1.0)
        self.steering = 0.0    # 转向 (-1.0左 ~ 1.0右)
        self.max_throttle = 100.0  # 最大油门限制(安全阈值)
        self.steering_rate = 0.1  # 转向灵敏度

        # 初始化 Cyber RT
        cyber.init()
        self.node = cyber.Node("keyboard_control")
        self.control_writer = self.node.create_writer("/apollo/control", control_cmd_pb2.ControlCommand)
    
    def send_control_command(self):
        """发布控制指令至 Apollo 控制模块"""
        cmd = control_cmd_pb2.ControlCommand()
        cmd.header.timestamp_sec = cyber_time.Time.now().to_sec()
        cmd.header.module_name = "keyboard_control"
        
        # 指令映射
        cmd.throttle = self.throttle
        cmd.brake = self.brake
        cmd.steering_target = self.steering
        cmd.gear_location = 1  # 前进挡
        
        self.control_writer.write(cmd)
        print(f"指令: 油门={self.throttle:.2f}, 刹车={self.brake:.2f}, 转向={self.steering:.2f}")

    def keyboard_listener(self):
        """监听键盘输入并更新控制参数"""
        print("使用 WASD 控制车辆,Q 退出...")

        while not cyber.is_shutdown():
            key = getKey()
            # 油门控制(W/S)
            if key == 'w':
                self.throttle = min(self.throttle + derta_speed, self.max_throttle)
                self.brake = 0.0
            elif key == 's':
                self.brake = min(self.brake + derta_speed, self.max_throttle)
                self.throttle = 0.0
            # else:
            #     self.throttle = max(self.throttle - derta_speed, 0.0)
            #     self.brake = max(self.brake - derta_speed, 0.0)
            
            # 转向控制(A/D)
            if key == 'a':
                self.steering = max(self.steering - self.steering_rate, -1.0)
            elif key == 'd':
                self.steering = min(self.steering + self.steering_rate, 1.0)
            else:
                self.steering *= 0.9  # 自动回正
            
            # 退出条件
            if key == 'q':
                break
            
            self.send_control_command()
            time.sleep(0.1)  # 控制频率10Hz

if __name__ == '__main__':
    controller = CarController()
    controller.keyboard_listener()
    cyber.shutdown()
相关推荐
好奇龙猫2 分钟前
【大学院-筆記試験練習:数据库(データベース問題訓練) と 软件工程(ソフトウェア)(2)】
学习·大学院
菩提小狗3 分钟前
第1天:基础入门-操作系统&名词&文件下载&反弹SHELL&防火墙绕过|小迪安全笔记|网络安全|
网络·笔记·学习·安全·web安全
Q741_1478 分钟前
C++ 栈 模拟 力扣 394. 字符串解码 每日一题 题解
c++·算法·leetcode·模拟·
iconball8 分钟前
个人用云计算学习笔记 --31 华为云运维服务
运维·笔记·学习·华为云·云计算
阿闽ooo9 分钟前
桥接模式实战:用万能遥控器控制多品牌电视
c++·设计模式·桥接模式
csbysj202010 分钟前
DOM 验证
开发语言
codists11 分钟前
《Grokking Concurrency》读后感
python
幺零九零零18 分钟前
压测-JMeter学习
学习·jmeter
Wuliwuliii18 分钟前
闵可夫斯基和、需存储的最小状态集
c++·算法·动态规划·闵可夫斯基和
想做后端的小C20 分钟前
Linux:期末考点
linux·运维·服务器