MOSS: a small, mostly 3D-printed litter-picking rover. Cleaner streets, one maker at a time.
Python
gamepad: MOSS_PAD_INVERT_Y / MOSS_PAD_INVERT_TURN flip a stick whose sign is the other way round (a pad that reports stick up as positive)
metroxa697a1995956
diff --git a/benches/bench_gamepad_cold.py b/benches/bench_gamepad_cold.pyindex 3ec2d32..1df2a63 100644--- a/benches/bench_gamepad_cold.py+++ b/benches/bench_gamepad_cold.py@@ -80,13 +80,17 @@ check(gp.DEADMAN_BUTTON is None,f"no deadman by default: the sticks drive (MOSS_PAD_DEADMAN=r1 brings R1 back): {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": None, "axis_right_x": 2, "axis_r2": 5}, "default layout = android, no deadman (None, 2, 5)")-check(gp.pad_layout({"MOSS_PAD_LAYOUT": "xinput"}) == {"deadman": None, "axis_right_x": 3, "axis_r2": 5}, "xinput layout = (None, 3, 5)")-check(gp.pad_layout({"MOSS_PAD_LAYOUT": "xinput", "MOSS_PAD_DEADMAN": "4"}) == {"deadman": 4, "axis_right_x": 3, "axis_r2": 5}, "MOSS_PAD_DEADMAN=4 -> that SDL button")+check(gp.pad_layout({}) == {"deadman": None, "axis_right_x": 2, "axis_r2": 5, "y_sign": 1, "turn_sign": 1}, "default layout = android, no deadman (None, 2, 5)")+check(gp.pad_layout({"MOSS_PAD_LAYOUT": "xinput"}) == {"deadman": None, "axis_right_x": 3, "axis_r2": 5, "y_sign": 1, "turn_sign": 1}, "xinput layout = (None, 3, 5)")+check(gp.pad_layout({"MOSS_PAD_LAYOUT": "xinput", "MOSS_PAD_DEADMAN": "4"}) == {"deadman": 4, "axis_right_x": 3, "axis_r2": 5, "y_sign": 1, "turn_sign": 1}, "MOSS_PAD_DEADMAN=4 -> that SDL button")check(gp.pad_layout({"MOSS_PAD_DEADMAN": "r1"})["deadman"] == 7 and gp.pad_layout({"MOSS_PAD_LAYOUT": "xinput", "MOSS_PAD_DEADMAN": "R1"})["deadman"] == 5and gp.pad_layout({"MOSS_PAD_DEADMAN": "none"})["deadman"] is None and gp.pad_layout({"MOSS_PAD_DEADMAN": "x"})["deadman"] is None,"MOSS_PAD_DEADMAN=r1 -> the layout's R1 (7 android, 5 xinput); none / unparseable -> no deadman")-check(gp.pad_layout({"MOSS_PAD_LAYOUT": "nope", "MOSS_PAD_AXIS_R2": "x"}) == {"deadman": None, "axis_right_x": 2, "axis_r2": 5}, "unknown layout / bad number -> android")+check(gp.pad_layout({"MOSS_PAD_LAYOUT": "nope", "MOSS_PAD_AXIS_R2": "x"}) == {"deadman": None, "axis_right_x": 2, "axis_r2": 5, "y_sign": 1, "turn_sign": 1}, "unknown layout / bad number -> android")+check((gp.pad_layout({})["y_sign"], gp.pad_layout({})["turn_sign"]) == (1, 1)+ and (gp.pad_layout({"MOSS_PAD_INVERT_Y": "1"})["y_sign"], gp.pad_layout({"MOSS_PAD_INVERT_TURN": "yes"})["turn_sign"]) == (-1, -1)+ and gp.pad_layout({"MOSS_PAD_INVERT_Y": "0"})["y_sign"] == 1 and (gp.Y_SIGN, gp.TURN_SIGN) == (1, 1),+ "MOSS_PAD_INVERT_Y / MOSS_PAD_INVERT_TURN: off by default (sign +1), 1/yes -> -1, 0 -> +1")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)")diff --git a/docs/base.md b/docs/base.mdindex 1681609..a0cfb7c 100644--- a/docs/base.md+++ b/docs/base.md@@ -20,6 +20,7 @@ Pad (a shanwan receiver in Android mode): **no deadman**, a stick out of itsdeadzone drives and centred sticks stop (`MOSS_PAD_DEADMAN=r1` brings a heldbutton back: R1 is SDL button 7 on this pad, 5 on its USB cable),left stick up/down = forward/back, right stick left/right = turn, R2 = boost+(`MOSS_PAD_INVERT_Y=1` / `MOSS_PAD_INVERT_TURN=1` in `robot.env` flip a stick whose sign is the other way round)(capped at `MOSS_MAX_PCT`). The stick scale comes from the base's envelope(`teleop_speeds`: full stick = `MOSS_MPS_AT_LIMIT`, full turn = a pivot at thatlimit), so the stick stays proportional across its travel. With the defaultdiff --git a/moss_dimos/gamepad.py b/moss_dimos/gamepad.pyindex b34571d..71ac1e0 100644--- a/moss_dimos/gamepad.py+++ b/moss_dimos/gamepad.py@@ -75,8 +75,10 @@ RESCAN_PERIOD_S = 2.0 # how often we re-scan SDL for a pad# 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_AXIS_RIGHT_X / MOSS_PAD_AXIS_R2 override one# value each; MOSS_PAD_DEADMAN adds a held button (none by default: r1 = the layout's R1, or an SDL-# button number); MOSS_PAD_INDEX picks the SDL joystick when several are plugged (the mute dongle is-# usually index 0). All read once at import.+# button number); MOSS_PAD_INVERT_Y=1 / MOSS_PAD_INVERT_TURN=1 flip a stick whose sign is the other way+# round (a pad that reports stick UP as positive drives backwards on a forward push); 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": {"r1": 7, "axis_right_x": 2, "axis_r2": 5},"xinput": {"r1": 5, "axis_right_x": 3, "axis_r2": 5},@@ -84,6 +86,11 @@ PAD_LAYOUTS = {DEADMAN_OFF = ("", "none", "off", "no") # MOSS_PAD_DEADMAN values that mean: no deadman+def _on(raw: str | None) -> bool:+ """An on/off switch as the stack reads them: 1 / true / yes / on."""+ return (raw or "").strip().lower() in ("1", "true", "yes", "on")++def pad_layout(env: Mapping[str, str] | None = None) -> dict[str, int | None]:"""The button / axis numbers in force: a named layout, then one-value overrides. Pure."deadman" is None by default (the sticks drive); MOSS_PAD_DEADMAN=r1 is the layout's R1,@@ -91,7 +98,9 @@ def pad_layout(env: Mapping[str, str] | None = None) -> dict[str, int | None]:e = os.environ if env is None else envname = (e.get("MOSS_PAD_LAYOUT") or "android").strip().lower()base = PAD_LAYOUTS.get(name, PAD_LAYOUTS["android"])- out: dict[str, int | None] = {"deadman": None, "axis_right_x": base["axis_right_x"], "axis_r2": base["axis_r2"]}+ out: dict[str, int | None] = {"deadman": None, "axis_right_x": base["axis_right_x"], "axis_r2": base["axis_r2"],+ "y_sign": -1 if _on(e.get("MOSS_PAD_INVERT_Y")) else 1,+ "turn_sign": -1 if _on(e.get("MOSS_PAD_INVERT_TURN")) else 1}raw = (e.get("MOSS_PAD_DEADMAN") or "").strip().lower()if raw == "r1":out["deadman"] = base["r1"]@@ -124,6 +133,8 @@ AXIS_LEFT_X = 0AXIS_LEFT_Y = 1AXIS_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"]+Y_SIGN = _LAYOUT["y_sign"] # -1 with MOSS_PAD_INVERT_Y: the pad reports stick up as positive+TURN_SIGN = _LAYOUT["turn_sign"] # -1 with MOSS_PAD_INVERT_TURN: the pad reports stick right as negative@dataclass(frozen=True)@@ -457,8 +468,8 @@ class GamepadTeleop(Module):"before trusting a single command", pad.get_name())try:pygame.event.pump()- axes = (_axis(pad, AXIS_LEFT_X), _axis(pad, AXIS_LEFT_Y),- _axis(pad, AXIS_RIGHT_X), _axis(pad, AXIS_R2))+ axes = (_axis(pad, AXIS_LEFT_X), Y_SIGN * _axis(pad, AXIS_LEFT_Y),+ TURN_SIGN * _axis(pad, AXIS_RIGHT_X), _axis(pad, AXIS_R2))if DEADMAN_BUTTON is None:deadman = True # no held button requiredelse: