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
Fix tests
  • Loading branch information
danic85 committed Dec 18, 2025
commit d48ccd8c39832a38a4645c07b496667ef7b1fd03
290 changes: 169 additions & 121 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
@@ -1,143 +1,191 @@

import sys
import os
import unittest
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
sys.modules['yaml'] = MagicMock()
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from modules.controller import Controller

from modules.xbox_controller import XboxController


class TestController(unittest.TestCase):
class TestXboxController(unittest.TestCase):
def setUp(self):
# Patch publish and log methods
self._publish_patch = unittest.mock.patch('modules.base_module.BaseModule.publish', MagicMock())
self._publish_patch.start()
self._log_patch = unittest.mock.patch('modules.base_module.BaseModule.log', lambda *a, **k: None)
self._log_patch.start()
self.addCleanup(self._publish_patch.stop)
self.addCleanup(self._log_patch.stop)

# Patch BaseModule.log to avoid side effects
self.log_patch = patch('modules.base_module.BaseModule.log', lambda *a, **k: None)
self.log_patch.start()
self.addCleanup(self.log_patch.stop)

def test_dynamic_remapping(self):
config = {
# Standard config for most tests
self.config = {
'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'
8: 'BTN_MODE', 9: 'BTN_THUMBL', 10: 'BTN_THUMBR', 11: 'KEY_RECORD'
},
'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'
},
'button_action_map': {
'default': {
'BTN_A': [ {'topic': 'tts', 'args': {'msg': 'Default'}} ]
},
'BTN_TL': {
'BTN_A': [ {'topic': 'tts', 'args': {'msg': 'Modifier'}} ]
}
},
'modifier_buttons': ['BTN_TL']
'axis_min': -32767.0,
'axis_max': 32767.0,
'deadzone': 4000,
'device': '/dev/input/js0',
'normalised': True,
'debug': False
}
ctrl = Controller(**config)
ctrl.running = False
ctrl.publish = MagicMock()
# Default mapping: simulate BTN_A press (button 0)
ctrl.handle_js_event(0, 1, 1, 0) # time_ms, value, type_, number
ctrl.publish.assert_any_call('tts', msg='Default')

def test_button_and_axis_names_required(self):
# button_names missing
with self.assertRaises(ValueError):
XboxController(axis_names={})
# axis_names missing
with self.assertRaises(ValueError):
XboxController(button_names={})

def test_modifier_mapping(self):
config = {
'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'
},
'button_action_map': {
'default': {
'BTN_A': [ {'topic': 'tts', 'args': {'msg': 'Default'}} ]
},
'BTN_TL': {
'BTN_A': [ {'topic': 'tts', 'args': {'msg': 'Modifier'}} ]
}
},
'modifier_buttons': ['BTN_TL']
}
ctrl = Controller(**config)
ctrl.running = False
ctrl.publish = MagicMock()
# Simulate BTN_BL press (let's use button 4 for BTN_TL as modifier)
ctrl.handle_js_event(0, 1, 1, 4) # BTN_TL
# Now press BTN_A with modifier active
ctrl.handle_js_event(0, 1, 1, 0) # BTN_A
ctrl.publish.assert_any_call('tts', msg='Modifier')


def test_controller_event_published(self):
# This test is not relevant for jsdev polling, as handle_js_event does not publish 'controller/event'.
# We skip this test or could adapt it if needed.
pass


# def test_delta_handled_correctly(self):
# config = {
# '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'
# },
# 'button_action_map': {
# 'default': {
# 'ABS_X': [ {'topic': 'eye/move', 'args': {'axis': 'x'}, 'deadzone': 10, 'modifier': {'scale': 1.0}} ],
# 'ABS_Y': [ {'topic': 'eye/move', 'args': {'axis': 'y'}, 'deadzone': 10, 'modifier': {'scale': 1.0}} ]
# }
# },
# 'deadzone': 0
# }
# ctrl = Controller(**config)
# ctrl.running = False
# ctrl.publish = MagicMock()
# # Test movement beyond deadzone: simulate ABS_X axis move (axis 0)
# ctrl.handle_js_event(0, 50, 2, 0) # time_ms, value, type_, number
# ctrl.publish.assert_any_call('eye/move')

def test_deadzone_handled(self):
config = {
'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'
},
'button_action_map': {
'default': {
'ABS_X': [ {'topic': 'eye/move', 'args': {'axis': 'x'}, 'deadzone': 10, 'modifier': {'scale': 1.0}} ],
'ABS_Y': [ {'topic': 'eye/move', 'args': {'axis': 'y'}, 'deadzone': 10, 'modifier': {'scale': 1.0}} ]
}
},
'deadzone': 0
}
ctrl = Controller(**config)
ctrl.running = False
ctrl.publish = MagicMock()
# Test movement within deadzone (should not publish)
ctrl.handle_js_event(0, 5, 2, 0) # ABS_X axis, value within deadzone
calls = [call[0][0] for call in ctrl.publish.call_args_list]
self.assertNotIn('eye/move', calls)
def test_get_name(self):
ctrl = XboxController(**self.config)
# Button
self.assertEqual(ctrl._get_name(ctrl.JS_EVENT_BUTTON, 0), 'BTN_A')
# Axis
self.assertEqual(ctrl._get_name(ctrl.JS_EVENT_AXIS, 1), 'ABS_Y')
# Unknown
self.assertTrue(ctrl._get_name(99, 99).startswith('unknown'))

def test_axis_normalization(self):
ctrl = XboxController(**self.config)
# Simulate axis event at min, max, and center
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, -32767, 0) # ABS_X min
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, 0, 0) # ABS_X center
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, 32767, 0) # ABS_X max
vals = [ctrl.current_status_normalized['ABS_X']]
Comment thread
danic85 marked this conversation as resolved.
Resolved
# After last event, should be 1.0
self.assertAlmostEqual(ctrl.current_status_normalized['ABS_X'], 1.0, places=2)
# Center event
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, 0, 0)
self.assertAlmostEqual(ctrl.current_status_normalized['ABS_X'], 0.0, places=2)
# Min event
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, -32767, 0)
self.assertAlmostEqual(ctrl.current_status_normalized['ABS_X'], -1.0, places=2)

def test_button_event_normalization(self):
ctrl = XboxController(**self.config)
ctrl._handle_button_event(0, ctrl.JS_EVENT_BUTTON, 1, 0) # BTN_A pressed
self.assertEqual(ctrl.current_status_normalized['BTN_A'], 1.0)
ctrl._handle_button_event(0, ctrl.JS_EVENT_BUTTON, 0, 0) # BTN_A released
self.assertEqual(ctrl.current_status_normalized['BTN_A'], 0.0)

def test_get_current_status(self):
ctrl = XboxController(**self.config)
ctrl._handle_button_event(0, ctrl.JS_EVENT_BUTTON, 1, 0)
# Normalized
self.assertIn('BTN_A', ctrl.get_current_status(normalized=True))
# Raw (should be empty unless _listen is running)
self.assertIsInstance(ctrl.get_current_status(normalized=False), dict)

def test_get_changed_inputs(self):
ctrl = XboxController(**self.config)
ctrl._handle_button_event(0, ctrl.JS_EVENT_BUTTON, 1, 0)
changed = ctrl.get_changed_inputs()
self.assertIn(('BTN_A', 1.0), changed)
# No change on second call
changed2 = ctrl.get_changed_inputs()
self.assertEqual(changed2, [])
# Change again
ctrl._handle_button_event(0, ctrl.JS_EVENT_BUTTON, 0, 0)
changed3 = ctrl.get_changed_inputs()
self.assertIn(('BTN_A', 0.0), changed3)

def test_output_normalized_status_prints(self):
ctrl = XboxController(**self.config)
ctrl._handle_button_event(0, ctrl.JS_EVENT_BUTTON, 1, 0)
# Should not raise
ctrl.output_normalized_status()

def test_output_current_status_prints(self):
ctrl = XboxController(**self.config)
# Simulate raw event
import struct
evbuf = struct.pack(ctrl.JS_EVENT_FORMAT, 0, 1, ctrl.JS_EVENT_BUTTON, 0)
ctrl.current_status[(ctrl.JS_EVENT_BUTTON, 0)] = evbuf
ctrl.output_current_status()

def test_listen_file_not_found(self):
# Should log warning and stop after timeout
ctrl = XboxController(**self.config)
ctrl.CONNECT_TIMEOUT = 1 # Speed up test
ctrl.running = True
with patch('builtins.open', side_effect=FileNotFoundError):
# Should stop after timeout
ctrl._listen()
self.assertFalse(ctrl.running)

def test_axis_start_value(self):
# Test that axis start value is respected via mapping
config = self.config.copy()
config['start'] = -32767
ctrl = XboxController(**config)
# Provide a mapping for ABS_X with a start value
class DummyMapping:
start = -32767
ctrl.mapping = {'ABS_X': DummyMapping()}
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, -32767, 0)
self.assertAlmostEqual(ctrl.current_status_normalized['ABS_X'], 0.0, places=2)

def test_normalized_flag_false(self):
config = self.config.copy()
config['normalised'] = False
ctrl = XboxController(**config)
# Simulate event: should update current_status, not current_status_normalized
import struct
evbuf = struct.pack(ctrl.JS_EVENT_FORMAT, 0, 1, ctrl.JS_EVENT_BUTTON, 0)
with patch('builtins.open', create=True) as mock_open:
mock_open.return_value.__enter__.return_value.read.return_value = evbuf
ctrl.running = True
# Only run one loop iteration
with patch.object(ctrl, '_listen', wraps=ctrl._listen) as listen_patch:
ctrl.running = False
# Should not raise

def test_axis_range_configurable(self):
config = self.config.copy()
config['axis_min'] = -100.0
config['axis_max'] = 100.0
ctrl = XboxController(**config)
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, 100, 0)
self.assertAlmostEqual(ctrl.current_status_normalized['ABS_X'], 1.0, places=2)
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, -100, 0)
self.assertAlmostEqual(ctrl.current_status_normalized['ABS_X'], -1.0, places=2)

def test_multiple_axes_and_buttons(self):
ctrl = XboxController(**self.config)
# Simulate several events
ctrl._handle_button_event(0, ctrl.JS_EVENT_BUTTON, 1, 0) # BTN_A
ctrl._handle_button_event(0, ctrl.JS_EVENT_BUTTON, 1, 1) # BTN_B
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, 10000, 0) # ABS_X
ctrl._handle_axis_event(0, ctrl.JS_EVENT_AXIS, -10000, 1) # ABS_Y
status = ctrl.get_current_status()
self.assertIn('BTN_A', status)
self.assertIn('BTN_B', status)
self.assertIn('ABS_X', status)
self.assertIn('ABS_Y', status)

def test_handle_event_dispatch(self):
ctrl = XboxController(**self.config)
with patch.object(ctrl, '_handle_button_event') as btn_patch, \
patch.object(ctrl, '_handle_axis_event') as axis_patch:
ctrl._handle_event(0, ctrl.JS_EVENT_BUTTON, 1, 0)
btn_patch.assert_called()
ctrl._handle_event(0, ctrl.JS_EVENT_AXIS, 100, 0)
axis_patch.assert_called()

def test_get_mapping_returns_none(self):
ctrl = XboxController(**self.config)
self.assertIsNone(ctrl._get_mapping('not_a_real_input'))

def test_has_input_changed(self):
ctrl = XboxController(**self.config)
prev = {'BTN_A': 1.0}
self.assertTrue(ctrl._has_input_changed('BTN_A', 0.0, prev))
self.assertFalse(ctrl._has_input_changed('BTN_A', 1.0, prev))

# InputRecorder tests omitted for brevity and because they are unrelated to jsdev polling.
if __name__ == '__main__':
unittest.main()
6 changes: 3 additions & 3 deletions tests/test_pitemperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_monitor_critical_temp(self, mock_popen):
with mock.patch.object(temp_module, 'publish') as mock_publish, \
mock.patch.object(temp_module, 'log') as mock_log:
temp_module.monitor()
mock_publish.assert_called_with('system/temperature', '86')
mock_publish.assert_called_with('system/temperature', value='86')
mock_log.assert_called_with('Temperature is critical: 86°C', 'critical')

@mock.patch('modules.pitemperature.os.popen')
Expand All @@ -25,7 +25,7 @@ def test_monitor_high_temp(self, mock_popen):
with mock.patch.object(temp_module, 'publish') as mock_publish, \
mock.patch.object(temp_module, 'log') as mock_log:
temp_module.monitor()
mock_publish.assert_called_with('system/temperature', '81')
mock_publish.assert_called_with('system/temperature', value='81')
mock_log.assert_called_with('Temperature is high: 81°C', 'warning')

@mock.patch('modules.pitemperature.os.popen')
Expand All @@ -37,7 +37,7 @@ def test_monitor_normal_temp(self, mock_popen):
with mock.patch.object(temp_module, 'publish') as mock_publish, \
mock.patch.object(temp_module, 'log') as mock_log:
temp_module.monitor()
mock_publish.assert_called_with('system/temperature', '50')
mock_publish.assert_called_with('system/temperature', value='50')
mock_log.assert_called_with('Temperature: 50°C', 'debug')

if __name__ == '__main__':
Expand Down