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()
相关推荐
高级程序源4 小时前
django校园寻物通失物招领平台32261-计算机课程设计、毕业设计
vue.js·python·mysql·小程序·django·flask·课程设计
WeiXin_DZbishe4 小时前
基于springboot宠物领养系统-计算机毕设 附源码【课程设计】94217
vue.js·spring boot·python·django·php·课程设计
kimnoic4 小时前
Python数据库学习心得:SQLite、MySQL、PostgreSQL优缺点
开发语言·python
snhna4 小时前
向量数据库深度实战:从 HNSW 到 pgvector、Milvus 选型与 RAG 落地
数据库·人工智能·python·postgresql·milvus
别动我齐刘海5 小时前
C++ 工程化学习
java·linux·前端·c++·学习·机器学习·机器人
程序员老陆5 小时前
C++ std::promise深度解析
c++·程序设计
必须得开心呀5 小时前
python编写带界面的软件,需要安装哪些插件
开发语言·python
Wx-bishekaifayuan5 小时前
express皖美特色农产品网售系统53118-计算机课程设计、毕业设计
人工智能·spring boot·后端·python·课程设计·express·旅游
青少儿编程课堂6 小时前
迪杰斯特拉(Dijkstra)算法:单源最短路与赛场路线规划
c++·python·算法·bfs·信息学竞赛
计算机编程指导师6 小时前
【计算机毕设推荐】基于Hadoop+Spark的公共交通运营数据分析与可视化系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·hadoop·python·计算机·数据分析·spark·交通