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: ds4 layout (a DualShock 4 to the kernel, the GameSir receiver's DInput mode) and MOSS_PAD_LAYOUT=auto, picked from the pad's kernel name; the launcher names the GameSir receiver's XInput mode (no Linux driver) and the pad-less receiver

metrox1950103adb92
diff --git a/benches/bench_gamepad_cold.py b/benches/bench_gamepad_cold.py
index 1df2a63..25d3461 100644
--- a/benches/bench_gamepad_cold.py
+++ b/benches/bench_gamepad_cold.py
@@ -80,13 +80,23 @@ 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, "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({}) == {"deadman": None, "axis_right_x": 2, "axis_r2": 5, "y_sign": 1, "turn_sign": 1, "name": "android"}, "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, "name": "xinput"}, "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, "name": "xinput"}, "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"] == 5
and 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, "y_sign": 1, "turn_sign": 1}, "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, "name": "nope"}, "unknown layout / bad number -> android")
+check(gp.pad_layout({"MOSS_PAD_LAYOUT": "ds4"}) == {"deadman": None, "axis_right_x": 2, "axis_r2": 4, "y_sign": 1, "turn_sign": 1, "name": "ds4"}
+ and gp.pad_layout({"MOSS_PAD_LAYOUT": "ds4", "MOSS_PAD_DEADMAN": "r1"})["deadman"] == 5,
+ "ds4 layout (a DualShock 4 to the kernel): right stick X = axis 2, R2 = axis 4, R1 = button 5")
+check((gp.layout_for_name("Sony Computer Entertainment Wireless Controller"), gp.layout_for_name("Microsoft X-Box 360 pad"),
+ gp.layout_for_name("shanwan Android GamePad"), gp.layout_for_name(None)) == ("ds4", "xinput", "android", "android"),
+ "layout_for_name: Sony -> ds4, X-Box -> xinput, shanwan / none -> android")
+check(gp.pad_layout({"MOSS_PAD_LAYOUT": "auto"}, pad_name="Sony Computer Entertainment Wireless Controller")["name"] == "ds4"
+ and gp.pad_layout({"MOSS_PAD_LAYOUT": "auto"}, pad_name="Microsoft X-Box 360 pad")["axis_right_x"] == 3
+ and gp.pad_layout({"MOSS_PAD_LAYOUT": "auto"}, pad_name=None)["name"] in ("android", "ds4", "xinput"),
+ "MOSS_PAD_LAYOUT=auto picks the layout from the pad's kernel name (no pad on this machine -> 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),
diff --git a/docs/base.md b/docs/base.md
index a0cfb7c..4bb966a 100644
--- a/docs/base.md
+++ b/docs/base.md
@@ -20,7 +20,11 @@ Pad (a shanwan receiver in Android mode): **no deadman**, a stick out of its
deadzone drives and centred sticks stop (`MOSS_PAD_DEADMAN=r1` brings a held
button 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)
+(`MOSS_PAD_INVERT_Y=1` / `MOSS_PAD_INVERT_TURN=1` in `robot.env` flip a stick whose sign is the other way round).
+`MOSS_PAD_LAYOUT` names the button and axis numbers: `android` (the shanwan receiver), `xinput` (an Xbox 360 pad to the
+kernel), `ds4` (a DualShock 4 to the kernel, which is what the GameSir 2.4G receiver is in its DInput mode), or `auto`,
+which picks one from the pad's kernel name at launch. The GameSir receiver in its XInput mode (`3537:1098`) has no Linux
+driver: hold View + Menu on the pad to cycle modes until it shows up as a DualShock 4.
(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 that
limit), so the stick stays proportional across its travel. With the default
diff --git a/moss_dimos/gamepad.py b/moss_dimos/gamepad.py
index 71ac1e0..a230145 100644
--- a/moss_dimos/gamepad.py
+++ b/moss_dimos/gamepad.py
@@ -80,10 +80,35 @@ RESCAN_PERIOD_S = 2.0 # how often we re-scan SDL for a pad
# 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},
+ "android": {"r1": 7, "axis_right_x": 2, "axis_r2": 5}, # the shanwan receiver in Android mode (2563:0526)
+ "xinput": {"r1": 5, "axis_right_x": 3, "axis_r2": 5}, # an Xbox 360 pad to the kernel (xpad): the shanwan pad on its cable
+ "ds4": {"r1": 5, "axis_right_x": 2, "axis_r2": 4}, # a DualShock 4 to the kernel (hid-generic): the GameSir 2.4G receiver in its DInput mode
}
DEADMAN_OFF = ("", "none", "off", "no") # MOSS_PAD_DEADMAN values that mean: no deadman
+JS_SYSFS_GLOB = "/sys/class/input/js*/device/name"
+
+
+def layout_for_name(name: str | None) -> str:
+ """The layout a pad's kernel name calls for: "Sony ... Wireless Controller" / DualShock -> ds4,
+ "X-Box" / Xbox -> xinput, anything else (the shanwan Android GamePad included) -> android. Pure."""
+ n = (name or "").lower()
+ if "sony" in n or "dualshock" in n or "wireless controller" in n:
+ return "ds4"
+ if "x-box" in n or "xbox" in n or "x box" in n:
+ return "xinput"
+ return "android"
+
+
+def detect_pad_name() -> str | None:
+ """The kernel name of the first /dev/input/js* (sysfs), None without one."""
+ import glob
+ for path in sorted(glob.glob(JS_SYSFS_GLOB)):
+ try:
+ with open(path) as f:
+ return f.read().strip()
+ except OSError:
+ continue
+ return None
def _on(raw: str | None) -> bool:
@@ -91,14 +116,19 @@ def _on(raw: str | None) -> bool:
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.
+def pad_layout(env: Mapping[str, str] | None = None, pad_name: str | None = None) -> dict[str, int | None]:
+ """The button / axis numbers in force: a named layout, then one-value overrides. Pure when
+ the layout is named; MOSS_PAD_LAYOUT=auto picks it from the pad's kernel name (pad_name, else
+ the first js device in sysfs), so the same robot.env serves a pad that shows up as a
+ DualShock 4 one day and as an Xbox 360 pad the next.
"deadman" is None by default (the sticks drive); MOSS_PAD_DEADMAN=r1 is the layout's R1,
an integer is that SDL button, none / off / empty is no deadman."""
e = os.environ if env is None else env
name = (e.get("MOSS_PAD_LAYOUT") or "android").strip().lower()
+ if name == "auto":
+ name = layout_for_name(detect_pad_name() if pad_name is None else pad_name)
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"], "name": name,
"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()
@@ -129,6 +159,7 @@ def pad_index(env: Mapping[str, str] | None = None) -> int:
_LAYOUT = pad_layout()
+LAYOUT_NAME = _LAYOUT["name"] # the layout in force (auto resolved at import, from the first js device)
AXIS_LEFT_X = 0
AXIS_LEFT_Y = 1
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)
@@ -441,6 +472,7 @@ class GamepadTeleop(Module):
brake_until = 0.0 # publish zeros until this instant, then shut up
prev_active = False # to see the RISING edge of a command (stick out, or deadman pressed)
waiting_logged = False
+ logger.info("gamepad layout: %s (right stick X = axis %d, R2 = axis %d)", LAYOUT_NAME, AXIS_RIGHT_X, AXIS_R2)
if DEADMAN_BUTTON is None:
logger.info("gamepad: no deadman - a stick out of its deadzone drives, centred sticks stop "
"(MOSS_PAD_DEADMAN=r1 brings the held button back)")
diff --git a/tools/moss-teleop.sh b/tools/moss-teleop.sh
index e628bdd..c9f63d2 100755
--- a/tools/moss-teleop.sh
+++ b/tools/moss-teleop.sh
@@ -64,6 +64,14 @@ echo "== 1/5 gamepad =="
# any /dev/input/js*: the pad on its cable (an Xbox 360 pad to the kernel) keeps the number after the
# dongle's, and SDL numbers the pads it can see from 0 whatever the js number (MOSS_PAD_INDEX picks)
if ! ls /dev/input/js* >/dev/null 2>&1; then
+ # the GameSir 2.4G receiver in its XInput mode (3537:1098, "2.4G XBOX 360 For Windows") has no Linux
+ # driver: the kernel sees a keyboard and nothing else. Its DInput mode shows up as a DualShock 4 and works.
+ if command -v lsusb >/dev/null 2>&1 && lsusb -d 3537:1098 >/dev/null 2>&1; then
+ echo "NO PAD: the GameSir receiver is in XInput mode (3537:1098, no Linux driver) - on the pad, hold View + Menu to cycle modes (or reset the pad) until the receiver shows up as a Sony DualShock 4 (lsusb)"; exit 1
+ fi
+ if command -v lsusb >/dev/null 2>&1 && lsusb -d 3537:2106 >/dev/null 2>&1; then
+ echo "NO PAD: the GameSir receiver is plugged but no pad is connected to it (3537:2106) - switch the pad on (Home); if it keeps blinking: Home + Share held 3 s, then the receiver's button"; exit 1
+ fi
echo "NO PAD (no /dev/input/js*) - receiver or cable plugged into the rover? if plugged but invisible: replug it, else sudo reboot"; exit 1
fi
echo " pad(s): $(for j in /dev/input/js*; do n=$(basename $j); printf '%s=%s ' "$n" "$(cat /sys/class/input/$n/device/name 2>/dev/null | tr ' ' '_')"; done)(dongle mute: button UNDER the pad, then move a stick; cable: MOSS_PAD_LAYOUT=xinput)"
diff --git a/tools/moss_preflight.py b/tools/moss_preflight.py
index e0504eb..d3f9928 100755
--- a/tools/moss_preflight.py
+++ b/tools/moss_preflight.py
@@ -470,11 +470,16 @@ def pad_verdict(name: str, init_axes, layout: str) -> tuple[bool, str]:
init_axes: the joystick API's initial axis values (None = could not read)."""
lay = (layout or "android").strip().lower() or "android"
n = (name or "").lower()
+ if lay == "auto":
+ from moss_dimos.gamepad import layout_for_name
+ lay = f"auto -> {layout_for_name(name)}"
if init_axes is None:
return False, f"{name}: cannot read it (permissions? held exclusively?)"
if init_axes and all(int(v) == PAD_MUTE_AXIS for v in init_axes):
return False, (f"{name}: MUTE - every axis at its never-reported value: the receiver sees no pad "
"(pad off, or not paired with this dongle); the stack will never see it at neutral")
+ if lay.startswith("auto"):
+ return True, f"{name} alive, layout {lay}"
xbox = ("x-box" in n or "xbox" in n or "x box" in n)
if xbox and lay != "xinput":
return False, f"{name}: an Xbox 360 pad to the kernel (the pad on its cable), MOSS_PAD_LAYOUT is {lay}: set MOSS_PAD_LAYOUT=xinput in robot.env"