Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4c8c7d9
WIP bus servo management
danic85 Jul 12, 2025
d18189c
Update config
danic85 Jul 12, 2025
60d524a
Added SC servo examples + compatibility
danic85 Jul 13, 2025
3e4dfc0
Configure initial positions
danic85 Jul 14, 2025
0be65a3
Allow other poses to be stored and accessed
danic85 Jul 14, 2025
91fac95
Added change_id script
danic85 Aug 3, 2025
69f2d7c
Calibrate servos on boot
danic85 Oct 4, 2025
9a57f8a
adjustments
danic85 Oct 4, 2025
0cf906e
Dynamic servo calibration + center on change_id
danic85 Oct 5, 2025
8d62b54
Controller support and WIP recording
danic85 Dec 13, 2025
f02d0db
Working with Pi
danic85 Dec 13, 2025
b052ed0
Adjust config
danic85 Dec 13, 2025
6841f63
Cleanup
danic85 Dec 14, 2025
c1ed384
Working input recorder
danic85 Dec 14, 2025
5e503e5
Support servo control (WIP - calibration needed)
danic85 Dec 14, 2025
008c369
Callibration + allow modules to be disabled on test environments
danic85 Dec 15, 2025
af0fdb2
Disable test
danic85 Dec 15, 2025
40040c8
Add documentation and tests
danic85 Dec 16, 2025
f645caa
Fix test
danic85 Dec 16, 2025
8a22781
Timeout controller connection after 30 seconds
danic85 Dec 16, 2025
d81c07c
Enable on robot
danic85 Dec 16, 2025
6a84d43
Fix for environment input to resolve pi issue
danic85 Dec 17, 2025
4803d58
Calibrate new leg position
danic85 Dec 17, 2025
dc4cb27
Improvements to gamepad controller modules. Temperature alert in disp…
danic85 Dec 18, 2025
d48ccd8
Fix tests
danic85 Dec 18, 2025
4a5a0ee
Fixes and improvements to loop and temperature management
danic85 Dec 21, 2025
5349956
Fix tests
danic85 Dec 21, 2025
cd4331b
Resolve PR comments
danic85 Dec 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Working with Pi
  • Loading branch information
danic85 committed Dec 13, 2025
commit f02d0db451a0d4fa1d2acb6623e1488fd935ed28
38 changes: 17 additions & 21 deletions config/controller.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
controller:
enabled: true
path: 'modules.controller.Controller'
dependencies:
python:
- inputs
# sudo systemctl enable xboxdrv.service
#
unix:
- xboxdrv
config:
deadzone: 500
modifier_buttons:
- BTN_TL
- BTN_TR
button_action_map:
default:
BTN_TL:
Expand All @@ -36,16 +32,16 @@ controller:
ABS_HAT0Y: # DPAD Y (-1, 0 or 1)
- topic: ''
args: {}
BTN_SOUTH: # A
BTN_A:
- topic: 'tts'
msg: 'Test Default'
BTN_NORTH: # X
BTN_X:
- topic: 'eye/blink'
args: {}
BTN_WEST: # Y
- topic: ''
BTN_Y:
- topic: 'gpio/laser/toggle'
args: {}
BTN_EAST: # B
BTN_B:
- topic: ''
args: {}
ABS_RX:
Expand All @@ -60,23 +56,23 @@ controller:
modifier:
scale: 1.0
ABS_X:
- topic: 'eye/look'
args: {axis: 'x'}
- topic: 'eye/move'
args: {axis: 'y'}
modifier:
scale: 0.03
scale: -0.03
ABS_Y:
- topic: 'eye/look'
args: {axis: 'y'}
- topic: 'eye/move'
args: {axis: 'x'}
modifier:
scale: 0.03
scale: -0.03
ABS_Z: # Left trigger
- topic: ''
args: {}
ABS_RZ: # Right trigger
- topic: ''
args: {}
BTN_BL:
BTN_SOUTH:
BTN_TR:
BTN_A:
- topic: 'tts'
msg: 'Left Shoulder Held'
ABS_X:
Expand All @@ -97,4 +93,4 @@ controller:
scale: 1.0
- topic: 'servo:leg_r_hip:mv'
modifier:
scale: -1.0
scale: -1.0
2 changes: 1 addition & 1 deletion config/gpio_laser.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
motion:
enabled: false
enabled: true
path: 'modules.gpio.laser.Laser'
config:
pin: 6
Expand Down
4 changes: 2 additions & 2 deletions config/tft_display.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tft_display:
enabled: false
enabled: true
path: modules.display.tft_display_eye.TFTDisplayEye
config:
bus: 0 # Use `ls /dev/spi*` to identify. (/dev/spidev0.0) is bus 0, device 0
Expand All @@ -8,7 +8,7 @@ tft_display:
dc_pin: 25
bl_pin: 18 # Not used if no backlight pin on device
rotation: -90.0
test_on_boot: false
test_on_boot: true
colors:
red: (255, 32, 32)
blue: (0, 245, 255)
Expand Down
54 changes: 51 additions & 3 deletions docs/Controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,59 @@ The `Controller` module provides an interface for using a game controller (such

## Enable controller on ubuntu:

<!-- sudo apt install dkms linux-headers-$(uname -r)
I'm not sure what helped with this situation, but I had immense difficulty getting bluetooth working for the Xbox Series S/X controller. I had to upgrade the firmware using this tool on windows 10: https://apps.microsoft.com/detail/9nblggh30xj3?hl=en-GB&gl=GB

In addition, I installed xpadneo:

```
sudo apt install dkms linux-headers-$(uname -r)
git clone https://github.com/atar-axis/xpadneo.git
cd xpadneo
sudo ./install.sh -->
<!-- Did not work -->
sudo ./install.sh
```

Disabling ERTM may also have been required:
```
sudo nano /etc/modprobe.d/bluetooth.conf

# Add this line and save
options bluetooth disable_ertm=Y

sudo reboot
```

Then use bluetoothctl to test:
```
sudo bluetoothctl
# Turn on agent and set as default
agent on
default-agent

# Start scanning for devices
scan on

# Replace XX:XX:XX:XX:XX:XX with your controller's MAC address
pair XX:XX:XX:XX:XX:XX
trust XX:XX:XX:XX:XX:XX
connect XX:XX:XX:XX:XX:X

exit
```

Further adjustments that may have helped:

```
# edit /etc/bluetooth/main.conf
[General]
ControllerMode = dual
JustWorksRepairing = confirm
```

The package xboxdrv was also suggested, but I couldn't get that to detect the controller, even once the script was working...

Testing with `jstest /dev/input/js0` (requires `sudo apt-get install joystick`) showed the controller worked, but I could not get the python modules `inputs` or `pygame` to recognise any input from the controller.

In the end I polled the /dev/input/js0 stream into python.

## Configuration

Expand Down
115 changes: 106 additions & 9 deletions modules/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
from modules.base_module import BaseModule
import yaml

try:
import inputs # pip install inputs
except ImportError:
raise ImportError("Please install the 'inputs' package: pip install inputs")
import struct

class Controller(BaseModule):
def __init__(self, **kwargs):
Expand All @@ -19,21 +16,121 @@ def __init__(self, **kwargs):
self.pressed_buttons = set()
self.axis_last_value = {} # Track last value for each axis
self.axis_last_time = {} # Track last event time (ms) for each axis
self.device = kwargs.get('device', "/dev/input/js0")

def setup_messaging(self):
self.running = True
self.thread.start()

def listen(self):
self.log("Controller listening for input...")
JS_EVENT_FORMAT = "IhBB"
JS_EVENT_SIZE = struct.calcsize(JS_EVENT_FORMAT)
while self.running:
try:
events = inputs.get_gamepad()
for event in events:
self.handle_event(event)
except inputs.UnpluggedError:
self.log("No gamepad found. Waiting for connection...", level='warning')
with open(self.device, "rb") as jsdev:
while self.running:
evbuf = jsdev.read(JS_EVENT_SIZE)
if not evbuf:
continue
time_ms, value, type_, number = struct.unpack(JS_EVENT_FORMAT, evbuf)
# print(f"time={time} value={value} type={type_} number={number}")
self.handle_js_event(time_ms, value, type_, number)
except FileNotFoundError:
self.log(f"No gamepad found at {self.device}. Waiting for connection...", level='warning')
time.sleep(1)
except Exception as e:
self.log(f"Error reading {self.device}: {e}", level='error')
time.sleep(1)

def handle_js_event(self, time_ms, value, type_, number):
# type_: 0x01 = button, 0x02 = axis, 0x80 = init
JS_EVENT_BUTTON = 1
JS_EVENT_AXIS = 0x02
JS_EVENT_INIT = 0x80

is_init = (type_ & JS_EVENT_INIT) != 0
event_type = type_ & ~JS_EVENT_INIT

# Map button/axis numbers to names as needed for your controller
button_names = {
0: 'BTN_A', 1: 'BTN_B', 2: 'BTN_X', 3: 'BTN_Y',
4: 'BTN_TL', 5: 'BTN_TR', 6: 'BTN_SELECT', 7: 'BTN_START',
8: 'BTN_MODE', 9: 'BTN_THUMBL', 10: 'BTN_THUMBR'
}
axis_names = {
0: 'ABS_X', 1: 'ABS_Y', 2: 'ABS_Z', 3: 'ABS_RZ',
4: 'ABS_RX', 5: 'ABS_RY', 6: 'ABS_HAT0X', 7: 'ABS_HAT0Y'
}

# self.log(f"JS Event: time={time_ms} value={value} type={event_type} number={number} (init={is_init})")

if event_type == JS_EVENT_BUTTON:
# self.log(f"JS Button Event: number={number} value={value} (init={is_init})")
button = button_names.get(number, f'BTN_{number}')
self.log(f"Button event: {button} value={value} (jsdev)")
# value: 1=pressed, 0=released
if value == 1:
self.pressed_buttons.add(button)
elif value == 0 and button in self.pressed_buttons:
self.pressed_buttons.remove(button)

active_mods = sorted([b for b in self.pressed_buttons if b in self.modifier_buttons])
mapping_key = '+'.join(active_mods) if active_mods else 'default'
self.log(f"Active modifiers: {active_mods} (jsdev)")
button_map = self.button_action_map.get(mapping_key, self.button_action_map.get('default', {}))

self.log(button)
if button in button_map and value == 1:
actions = button_map[button]
for mapping in actions:
topic = mapping.get('topic')
if not topic:
self.log(f"Empty topic for button {button} mapping, skipping.", level='warning')
continue # Skip empty topic
args = mapping.get('args', {})
self.publish(topic, **args)
self.log(f"Published to topic {topic} with args {args} (jsdev)")

elif event_type == JS_EVENT_AXIS:
axis = axis_names.get(number, f'AXIS_{number}')
# self.log(f"Axis event: {axis} value={value} (jsdev)")
# value: -32767..32767 for sticks, -1/0/1 for hats
active_mods = sorted([b for b in self.pressed_buttons if b in self.modifier_buttons])
mapping_key = '+'.join(active_mods) if active_mods else 'default'
button_map = self.button_action_map.get(mapping_key, self.button_action_map.get('default', {}))
self.log(f"Active modifiers: {active_mods} (jsdev)")

if axis in button_map:
actions = button_map[axis]
for mapping in actions:
deadzone = mapping.get('deadzone', self.global_deadzone)
if deadzone > abs(value):
continue # Skip if within deadzone

debounce = mapping.get('debounce', self.global_debounce) # debounce in ms
now = int(time.time() * 1000) # current time in ms
last_time = self.axis_last_time.get(axis, None)

# Only allow if enough time has passed
if last_time is not None and (now - last_time) < debounce:
continue # Skip if within debounce interval
self.axis_last_time[axis] = now
last_value = self.axis_last_value.get(axis, 0)
delta = value - last_value
self.axis_last_value[axis] = value

topic = mapping.get('topic')
if not topic:
continue # Skip empty topic
args = dict(mapping.get('args', {})) # Copy to avoid mutation
modifier = mapping.get('modifier', None)
if modifier is not None:
scale = modifier.get('scale', 1.0)
args['delta'] = delta * scale
# self.log(f"Axis {axis} moved to {args.get('delta', value)} (jsdev)")
self.publish(topic, **args)
self.log(f"Published to topic {topic} with args {args} (jsdev)")

def handle_event(self, event):
# if event.code == 'ABS_Y':
Expand Down
Loading