79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""实时监听遥控器状态 (按键 + 摇杆 + L2).
|
|
|
|
用法: python examples/monitor_remote.py --duration 60
|
|
"""
|
|
import argparse
|
|
import signal
|
|
import sys
|
|
import time
|
|
|
|
from go1_pro_sdk import MCUClient, LowCmd
|
|
|
|
running = True
|
|
|
|
|
|
def sigint(s, f):
|
|
global running
|
|
running = False
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument('--state', default=None)
|
|
p.add_argument('--duration', type=float, default=60)
|
|
p.add_argument('--raw', action='store_true')
|
|
args = p.parse_args()
|
|
signal.signal(signal.SIGINT, sigint)
|
|
|
|
with MCUClient(state_path=args.state) as client:
|
|
recv = client.wake_mcu(50)
|
|
if recv == 0:
|
|
print('❌ 无回包')
|
|
return 1
|
|
|
|
damping = LowCmd().all_damping()
|
|
last_btn = 0
|
|
last_print = 0
|
|
start = time.time()
|
|
|
|
print(f'📡 监听遥控器 {args.duration}s, Ctrl+C 退出\n')
|
|
|
|
while running and time.time() - start < args.duration:
|
|
client.send(damping)
|
|
time.sleep(0.005)
|
|
state = client.recv_latest()
|
|
if state is None:
|
|
continue
|
|
|
|
r = state.remote
|
|
now = time.time()
|
|
|
|
# 按键事件
|
|
if r.btn != last_btn:
|
|
pressed_now = set(r.pressed)
|
|
pressed_before = set(n for m, n in __import__('go1_pro_sdk').BUTTON_NAMES if last_btn & m)
|
|
just_p = pressed_now - pressed_before
|
|
just_r = pressed_before - pressed_now
|
|
if just_p:
|
|
print(f"[{now-start:6.2f}s] ⬇ {'+'.join(sorted(just_p))}")
|
|
if just_r:
|
|
print(f"[{now-start:6.2f}s] ⬆ {'+'.join(sorted(just_r))}")
|
|
last_btn = r.btn
|
|
|
|
# 摇杆 (0.2s 一次)
|
|
if now - last_print > 0.2:
|
|
active = (abs(r.lx) > 0.02 or abs(r.ly) > 0.02 or
|
|
abs(r.rx) > 0.02 or abs(r.ry) > 0.02 or r.L2 > 0.02)
|
|
if active:
|
|
print(f'[{now-start:6.2f}s] L=({r.lx:+.2f},{r.ly:+.2f}) '
|
|
f'R=({r.rx:+.2f},{r.ry:+.2f}) L2={r.L2:+.2f}'
|
|
+ (f' raw={state.wirelessRemote.hex()}' if args.raw else ''))
|
|
last_print = now
|
|
|
|
print('\n✅ 退出')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main() or 0)
|