Files
vslam_cpp/test/gen_teleop_vectors.py
trung09122002 48bd5377dc upload
2026-09-18 18:14:42 +07:00

83 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Sinh vector kiem tu CHINH ban Python goc.
Chay mot chuoi thao tac tren teleop_model.py cua vSLAM_ws roi ghi ket qua ra
test/data/teleop_vectors.txt. test_teleop_model.cc doc file do va bat ban C++
cho ket qua giong het.
Day la cach duy nhat kiem duoc loi hua "logic giu NGUYEN VEN" cua ban port --
doc hai file roi gat dau thi khong phai kiem chung.
"""
import sys, os
sys.path.insert(0, os.path.expanduser(
"~/projects/vSLAM_ws/src/robot_bringup/scripts"))
from teleop_model import TeleopModel
out = []
def emit(tag, m):
lin, ang = m.command()
h, v = m.joystick_position()
out.append("%s %.9f %.9f %.9f %.9f %.9f %.9f" %
(tag, lin, ang, h, v, m.linear_speed, m.angular_speed))
m = TeleopModel()
emit("init", m)
# Phim mui ten, tung phim va to hop
for k in ("Up", "Down", "Left", "Right"):
m.set_key(k, True); emit("key_down_" + k, m)
m.set_key(k, False); emit("key_up_" + k, m)
m.set_key("Up", True); m.set_key("Left", True); emit("up_left", m)
m.set_key("Down", True); emit("up_left_down", m) # Up + Down triet tieu
m.clear_keys(); emit("clear_keys", m)
# Joystick
for hv in [(0.0,-1.0), (0.0,1.0), (-1.0,0.0), (1.0,0.0),
(0.5,-0.5), (-0.3,0.7), (2.0,-2.0), (-2.0,2.0)]:
m.set_joystick(*hv); emit("joy_%.1f_%.1f" % hv, m)
m.release_joystick(); emit("joy_release", m)
# Joystick de len phim
m.set_key("Up", True); emit("key_then", m)
m.set_joystick(0.0, -1.0); emit("joy_over_key", m)
m.release_joystick(); emit("joy_released_key_remains", m)
m.clear_keys()
# Chinh toc do
for _ in range(5):
m.adjust_linear_speed(+1); emit("lin_up", m)
for _ in range(12):
m.adjust_linear_speed(-1); emit("lin_down", m) # cham day min
for _ in range(3):
m.adjust_angular_speed(+1); emit("ang_up", m)
for _ in range(20):
m.adjust_angular_speed(-1); emit("ang_down", m)
m.set_linear_speed(1.5); emit("set_lin_1.5", m)
m.set_linear_speed(99.0); emit("set_lin_99", m) # kep ve max
m.set_angular_speed(-5.0); emit("set_ang_-5", m) # kep ve min
# Lenh manual + thu tu uu tien
m.set_manual_command(linear_x=0.25); emit("manual_lin", m)
m.set_manual_command(angular_z=-0.4); emit("manual_ang", m)
m.set_joystick(0.0, -1.0); emit("joy_clears_manual", m)
m.set_manual_command(linear_x=0.1, angular_z=0.1); emit("manual_over_joy", m)
m.set_key("Up", True); emit("key_clears_manual", m)
m.clear_motion(); emit("clear_motion", m)
# Gioi han
m.set_max_linear_speed(0.5); emit("max_lin_0.5", m)
m.set_max_angular_speed(0.3); emit("max_ang_0.3", m)
m.set_min_linear_command(-0.2); emit("min_lin_cmd", m)
m.set_min_angular_command(-0.1); emit("min_ang_cmd", m)
m.set_joystick(1.0, 1.0); emit("joy_at_limits", m)
m.set_manual_command(linear_x=-99.0, angular_z=99.0); emit("manual_clamped", m)
os.makedirs("data", exist_ok=True)
with open("data/teleop_vectors.txt", "w") as f:
f.write("# sinh boi gen_teleop_vectors.py tu teleop_model.py goc\n")
f.write("# tag linear angular joy_h joy_v linear_speed angular_speed\n")
f.write("\n".join(out) + "\n")
print("da ghi %d vector vao data/teleop_vectors.txt" % len(out))