From 8beae83270aa9845cb590161a0205cd00c05fc77 Mon Sep 17 00:00:00 2001
From: Guilhem Saurel <guilhem.saurel@laas.fr>
Date: Thu, 14 Oct 2021 17:37:24 +0200
Subject: [PATCH] joystick launcher

---
 scripts/joystick-launcher/README.md           | 17 ++++++++
 ...gepetto-quadruped-reactive-walking.service |  7 ++++
 .../joystick-launcher/joystick-launcher.py    | 41 +++++++++++++++++++
 .../joystick-launcher.service                 | 13 ++++++
 4 files changed, 78 insertions(+)
 create mode 100644 scripts/joystick-launcher/README.md
 create mode 100644 scripts/joystick-launcher/gepetto-quadruped-reactive-walking.service
 create mode 100755 scripts/joystick-launcher/joystick-launcher.py
 create mode 100644 scripts/joystick-launcher/joystick-launcher.service

diff --git a/scripts/joystick-launcher/README.md b/scripts/joystick-launcher/README.md
new file mode 100644
index 00000000..7667eb84
--- /dev/null
+++ b/scripts/joystick-launcher/README.md
@@ -0,0 +1,17 @@
+# Joystick Launcher
+
+Start and stop the controller from the a joystick.
+
+## Install
+
+```bash
+sudo cp *.service /etc/systemd/system
+sudo systemctl daemon-reload
+sudo systemctl enable joystick-launcher
+sudo systemctl start joystick-launcher
+```
+
+## Use
+
+To start the controller, press LR1 + north (triangle on a ps-like gamepad)
+To stop the controller, press LR1 + west (square on a ps-like gamepad)
diff --git a/scripts/joystick-launcher/gepetto-quadruped-reactive-walking.service b/scripts/joystick-launcher/gepetto-quadruped-reactive-walking.service
new file mode 100644
index 00000000..a02507bc
--- /dev/null
+++ b/scripts/joystick-launcher/gepetto-quadruped-reactive-walking.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Gepetto Quadruped Reactive Walking
+
+[Service]
+Type=simple
+WorkingDirectory=/home/pi/src/quadruped-reactive-walking/build
+ExecStart=/home/pi/src/quadruped-reactive-walking/build/gepetto-quadruped-reactive-walking
diff --git a/scripts/joystick-launcher/joystick-launcher.py b/scripts/joystick-launcher/joystick-launcher.py
new file mode 100755
index 00000000..0182250e
--- /dev/null
+++ b/scripts/joystick-launcher/joystick-launcher.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+"""Start and stop the odri-controller service."""
+
+import dbus
+from inputs import get_gamepad
+
+SERVICE = "gepetto-quadruped-reactive-walking.service"
+SYSTEMD = "org.freedesktop.systemd1"
+SYSTEMDS = '/' + SYSTEMD.replace('.', '/')
+
+
+def get_manager():
+    sysbus = dbus.SystemBus()
+    systemd1 = sysbus.get_object(SYSTEMD, SYSTEMDS)
+    return dbus.Interface(systemd1, SYSTEMD + ".Manager")
+
+
+def start(manager):
+    manager.StartUnit(SERVICE, "replace")
+
+
+def stop(manager):
+    manager.StopUnit(SERVICE, "replace")
+
+
+def main():
+    manager = get_manager()
+    security = False  # ensure no accidental trigger
+    while True:
+        for event in get_gamepad():
+            if event.code == "BTN_TR":
+                security = event.state == 1
+            elif security and event.state == 1:
+                if event.code == "BTN_NORTH":
+                    start(manager)
+                elif event.code == "BTN_WEST":
+                    stop(manager)
+
+
+if __name__ == "__main__":
+    main()
diff --git a/scripts/joystick-launcher/joystick-launcher.service b/scripts/joystick-launcher/joystick-launcher.service
new file mode 100644
index 00000000..16358613
--- /dev/null
+++ b/scripts/joystick-launcher/joystick-launcher.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Start the Gepetto Quadruped Reactive Walking service from a joystick
+StartLimitIntervalSec=2
+
+[Service]
+Type=simple
+Restart=always
+RestartSec=5
+WorkingDirectory=/home/pi/src/quadruped-reactive-walking/scripts/joystick-launcher
+ExecStart=/home/pi/src/quadruped-reactive-walking/scripts/joystick-launcher/joystick-launcher.py
+
+[Install]
+WantedBy=multi-user.target
-- 
GitLab