Skip to content

armgit / moss

Public Imported from GitHub
0

MOSS: a small, mostly 3D-printed litter-picking rover. Cleaner streets, one maker at a time.

Python

gamepad: android/xinput layouts and MOSS_PAD_* overrides (the pad on its USB cable is an Xbox 360 pad to the kernel); bench E follows the measured speed

metroxfb94801c8006
diff --git a/benches/bench_gamepad_cold.py b/benches/bench_gamepad_cold.py
index 878f05e..8807a26 100644
--- a/benches/bench_gamepad_cold.py
+++ b/benches/bench_gamepad_cold.py
@@ -78,6 +78,13 @@ check((LIN, ANG) == (0.45, 0.8), f"ceilings: {LIN} m/s, {ANG} rad/s")
# gp.DEADMAN_BUTTON, so without these a moved constant would still pass.
check(gp.DEADMAN_BUTTON == 7,
f"deadman = R1 = SDL button 7 on the MOSS pad (moss_pi's L1 is a different path): {gp.DEADMAN_BUTTON}")
+# the pad on its USB cable is an Xbox 360 pad to the kernel (xpad): R1 = button 5, right stick X = axis 3,
+# triggers at rest = -1 on axes 2 and 5 (measured: js1 initial state axes 2 and 5 at -32767, sticks at 0)
+check(gp.pad_layout({}) == {"deadman": 7, "axis_right_x": 2, "axis_r2": 5}, "default layout = android (7, 2, 5)")
+check(gp.pad_layout({"MOSS_PAD_LAYOUT": "xinput"}) == {"deadman": 5, "axis_right_x": 3, "axis_r2": 5}, "xinput layout = (5, 3, 5)")
+check(gp.pad_layout({"MOSS_PAD_LAYOUT": "xinput", "MOSS_PAD_DEADMAN": "4"}) == {"deadman": 4, "axis_right_x": 3, "axis_r2": 5}, "one-value override wins")
+check(gp.pad_layout({"MOSS_PAD_LAYOUT": "nope", "MOSS_PAD_AXIS_R2": "x"}) == {"deadman": 7, "axis_right_x": 2, "axis_r2": 5}, "unknown layout / bad number -> android")
+check(gp.pad_index({}) == 0 and gp.pad_index({"MOSS_PAD_INDEX": "1"}) == 1 and gp.pad_index({"MOSS_PAD_INDEX": "-3"}) == 0, "MOSS_PAD_INDEX: 0 by default, clamped at 0")
check((gp.AXIS_LEFT_X, gp.AXIS_LEFT_Y, gp.AXIS_RIGHT_X, gp.AXIS_R2) == (0, 1, 2, 5),
"strafe = left X (axis 0), throttle = left Y (axis 1), turn = right X (axis 2), boost = R2 (axis 5)")
@@ -158,32 +165,36 @@ check(abs(rim - gp.DEFAULT_RIM_MS) < 1e-9 and abs(vx / wz - 0.45 / 0.5) < 1e-9,
# --- E. the MOSS envelope: moss_adapter's scale and ceilings --------------
-print("\nE. the MOSS envelope (default geometry: track 0.266 m, 0.45 m/s at 100 %, turn gain 2)")
+print("\nE. the MOSS envelope (default geometry: track 0.266 m, the measured speed at 100 %, turn gain 2)")
from moss_dimos.moss_adapter import MossGeometry, teleop_envelope, teleop_speeds # noqa: E402
G = MossGeometry.from_env()
+V = G.mps_at_limit # the measured plateau at 100 % (0.32 m/s since the 90:1 motors)
+LEVER = G.track_m / 2.0 # 0.133 m
m_lin, m_ang = teleop_speeds(G)
m_env = teleop_envelope(G)
-check(close(m_lin, 0.45, 1e-9) and close(m_ang, 2.0 * 2.0 * 0.45 / 0.266, 1e-9),
- f"scale: {m_lin:.3f} m/s and {m_ang:.4f} rad/s at full stick (6.7669 = 2 x the spin rate)")
+check(close(m_lin, V, 1e-9) and close(m_ang, 2.0 * 2.0 * V / G.track_m, 1e-9),
+ f"scale: {m_lin:.3f} m/s and {m_ang:.4f} rad/s at full stick (2 x the spin rate of the tracks)")
check(set(m_env) == {"linear", "angular", "lever", "rim"},
f"teleop_envelope keys = TwistEnvelope's fields: {sorted(m_env)}")
moss_pad = GamepadTeleop(linear_speed=m_lin, angular_speed=m_ang, envelope=m_env)
+RIM = V + LEVER * m_ang # full forward + full turn sits exactly on the rim
check(isinstance(moss_pad.envelope, gp.TwistEnvelope)
- and close(moss_pad.envelope.rim, 1.35, 1e-9) and close(moss_pad.envelope.lever, 0.133, 1e-9),
+ and close(moss_pad.envelope.rim, RIM, 1e-9) and close(moss_pad.envelope.lever, LEVER, 1e-9),
f"the envelope dict (what a blueprint carries) becomes a TwistEnvelope: "
f"lever {moss_pad.envelope.lever:.3f} m, rim {moss_pad.envelope.rim:.3f} m/s")
MCFG = moss_pad.cfg
got = axes_to_twist(0.0, -1.0, 1.0, -1.0, MCFG)
-check(twist_close(got, (0.45, 0.0, -m_ang), 1e-9),
+check(twist_close(got, (V, 0.0, -m_ang), 1e-9),
f"full forward + full turn -> ({got[0]:.3f} m/s, {got[2]:.4f} rad/s): NOT scaled, "
- f"the rim (0.45 + 0.133 x 6.7669 = 1.35) is exactly the envelope")
+ f"the rim ({V:.2f} + {LEVER:.3f} x {m_ang:.4f} = {RIM:.3f}) is exactly the envelope")
got = axes_to_twist(-1.0, -1.0, 1.0, -1.0, MCFG)
-check(twist_close(got, (0.3375, 0.3375, -0.75 * m_ang), 1e-9),
- f"... plus full left stick X -> rim 1.80, the whole command x 0.75: "
+k = RIM / (2.0 * V + LEVER * m_ang) # ... plus a full strafe: the whole command scales down to the rim
+check(twist_close(got, (k * V, k * V, -k * m_ang), 1e-9),
+ f"... plus full left stick X -> rim {2.0 * V + LEVER * m_ang:.2f}, the whole command x {k:.2f}: "
f"({got[0]:.4f}, {got[1]:.4f}, {got[2]:.4f}); the tracks drop vy, the scale stays")
-check(twist_close(axes_to_twist(0.0, -1.0, 0.0, 1.0, MCFG), (0.45, 0.0, 0.0), 1e-9),
- "boost on the MOSS scale -> vx still 0.45 m/s (the base's limit is the ceiling)")
+check(twist_close(axes_to_twist(0.0, -1.0, 0.0, 1.0, MCFG), (V, 0.0, 0.0), 1e-9),
+ f"boost on the MOSS scale -> vx still {V:.2f} m/s (the base's limit is the ceiling)")
# --- B. hot-plug cycle on a fake pygame -----------------------------------
diff --git a/moss_dimos/gamepad.py b/moss_dimos/gamepad.py
index 124a259..aa0ee6b 100644
--- a/moss_dimos/gamepad.py
+++ b/moss_dimos/gamepad.py
@@ -36,6 +36,7 @@ from __future__ import annotations
from dataclasses import dataclass
import os
+from collections.abc import Mapping
import threading
import time
from typing import Any
@@ -66,10 +67,47 @@ DEFAULT_BOOST = 2.0
RESCAN_PERIOD_S = 2.0 # how often we re-scan SDL for a pad
# SDL axis indices on a standard dual-stick pad (DS4/DS5, Xbox, 8BitDo).
+# The pad's LAYOUT depends on how it shows up: the MOSS pad on its 2.4 GHz dongle in Android mode
+# (2563:0526) is the default; the same pad on its USB cable enumerates as an Xbox 360 pad (045e:028e,
+# kernel xpad), where R1 is button 5, the right stick X is axis 3 and the triggers are axes 2 and 5.
+# MOSS_PAD_LAYOUT=android|xinput picks a set; MOSS_PAD_DEADMAN / MOSS_PAD_AXIS_RIGHT_X / MOSS_PAD_AXIS_R2
+# override one value each; MOSS_PAD_INDEX picks the SDL joystick when several are plugged (the mute
+# dongle is usually index 0). All read once at import.
+PAD_LAYOUTS = {
+ "android": {"deadman": 7, "axis_right_x": 2, "axis_r2": 5},
+ "xinput": {"deadman": 5, "axis_right_x": 3, "axis_r2": 5},
+}
+
+
+def pad_layout(env: Mapping[str, str] | None = None) -> dict[str, int]:
+ """The button / axis numbers in force: a named layout, then one-value overrides. Pure."""
+ e = os.environ if env is None else env
+ name = (e.get("MOSS_PAD_LAYOUT") or "android").strip().lower()
+ out = dict(PAD_LAYOUTS.get(name, PAD_LAYOUTS["android"]))
+ for key, var in (("deadman", "MOSS_PAD_DEADMAN"), ("axis_right_x", "MOSS_PAD_AXIS_RIGHT_X"), ("axis_r2", "MOSS_PAD_AXIS_R2")):
+ raw = (e.get(var) or "").strip()
+ if raw:
+ try:
+ out[key] = int(raw)
+ except ValueError:
+ pass
+ return out
+
+
+def pad_index(env: Mapping[str, str] | None = None) -> int:
+ """MOSS_PAD_INDEX, else 0. Pure."""
+ raw = ((os.environ if env is None else env).get("MOSS_PAD_INDEX") or "").strip()
+ try:
+ return max(0, int(raw)) if raw else 0
+ except ValueError:
+ return 0
+
+
+_LAYOUT = pad_layout()
AXIS_LEFT_X = 0
AXIS_LEFT_Y = 1
-AXIS_RIGHT_X = 2 # measured: on the 2563:0526 pad in Android mode, right-stick X sweeps axis 2 over its full travel (axis 3 is its vertical, which leaks a little)
-AXIS_R2 = 5
+AXIS_RIGHT_X = _LAYOUT["axis_right_x"] # android: measured on the 2563:0526 pad, right-stick X sweeps axis 2 over its full travel (axis 3 is its vertical, which leaks a little)
+AXIS_R2 = _LAYOUT["axis_r2"]
@dataclass(frozen=True)
@@ -108,7 +146,7 @@ CLAMP_ANGULAR_RADS = 0.8
# passes its own envelope, sized to the tracks (moss_adapter.teleop_envelope).
DEFAULT_LEVER_M = 0.50 # m of rim speed per rad/s of rotation
DEFAULT_RIM_MS = 0.45 # max rim speed however the sticks are mixed
-DEADMAN_BUTTON = 7 # R1: SDL button 7 on the 2563:0526 pad in Android mode. Held = commands allowed; released = zeros, always
+DEADMAN_BUTTON = _LAYOUT["deadman"] # R1: SDL button 7 on the 2563:0526 pad in Android mode (5 on the cable, xinput). Held = commands allowed; released = zeros, always
BRAKE_S = 0.5 # how long the zeros keep flowing after the deadman is
# released - then TOTAL SILENCE until it is held again.
# dimOS's MovementManager reads ANY tele_cmd_vel message,
@@ -287,7 +325,7 @@ class GamepadTeleop(Module):
deadzone: float = DEFAULT_DEADZONE,
rate_hz: float = DEFAULT_RATE_HZ,
boost_multiplier: float = DEFAULT_BOOST,
- joystick_index: int = 0, brake_s: float | None = None,
+ joystick_index: int | None = None, brake_s: float | None = None,
envelope: Any = None,
**kwargs: Any) -> None:
super().__init__(**kwargs)
@@ -302,7 +340,7 @@ class GamepadTeleop(Module):
boost_multiplier=boost_multiplier,
envelope=self.envelope)
self.rate_hz = rate_hz
- self.joystick_index = joystick_index
+ self.joystick_index = pad_index() if joystick_index is None else joystick_index
# None -> MOSS_BRAKE_S, else BRAKE_S (0.5 s). Resolved HERE, at
# construction, so the value is fixed and logged once per run - not
# re-read per tick.