Repository navigation
Bus servo management. Improvements to framework including temperature management #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 d18189c
Update config
danic85 60d524a
Added SC servo examples + compatibility
danic85 3e4dfc0
Configure initial positions
danic85 0be65a3
Allow other poses to be stored and accessed
danic85 91fac95
Added change_id script
danic85 69f2d7c
Calibrate servos on boot
danic85 9a57f8a
adjustments
danic85 0cf906e
Dynamic servo calibration + center on change_id
danic85 8d62b54
Controller support and WIP recording
danic85 f02d0db
Working with Pi
danic85 b052ed0
Adjust config
danic85 6841f63
Cleanup
danic85 c1ed384
Working input recorder
danic85 5e503e5
Support servo control (WIP - calibration needed)
danic85 008c369
Callibration + allow modules to be disabled on test environments
danic85 af0fdb2
Disable test
danic85 40040c8
Add documentation and tests
danic85 f645caa
Fix test
danic85 8a22781
Timeout controller connection after 30 seconds
danic85 d81c07c
Enable on robot
danic85 6a84d43
Fix for environment input to resolve pi issue
danic85 4803d58
Calibrate new leg position
danic85 dc4cb27
Improvements to gamepad controller modules. Temperature alert in disp…
danic85 d48ccd8
Fix tests
danic85 4a5a0ee
Fixes and improvements to loop and temperature management
danic85 5349956
Fix tests
danic85 cd4331b
Resolve PR comments
danic85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix tests
- Loading branch information
commit d48ccd8c39832a38a4645c07b496667ef7b1fd03
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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']] | ||
| # 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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.