|
| 1 | +import unittest |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +from canopen.objectdictionary import ObjectDictionary, ODVariable |
| 5 | +from canopen.objectdictionary.datatypes import UNSIGNED16, UNSIGNED32, INTEGER8 |
| 6 | +from canopen.profiles.p402 import BaseNode402, State402, OperationMode, Homing |
| 7 | + |
| 8 | + |
| 9 | +def _make_od(): |
| 10 | + """Create a minimal OD with DS402 objects for testing.""" |
| 11 | + od = ObjectDictionary() |
| 12 | + # Controlword |
| 13 | + var = ODVariable("Controlword", 0x6040) |
| 14 | + var.data_type = UNSIGNED16 |
| 15 | + var.access_type = "rw" |
| 16 | + od.add_object(var) |
| 17 | + # Statusword |
| 18 | + var = ODVariable("Statusword", 0x6041) |
| 19 | + var.data_type = UNSIGNED16 |
| 20 | + var.access_type = "ro" |
| 21 | + od.add_object(var) |
| 22 | + # Modes of operation |
| 23 | + var = ODVariable("Modes of operation", 0x6060) |
| 24 | + var.data_type = INTEGER8 |
| 25 | + var.access_type = "rw" |
| 26 | + od.add_object(var) |
| 27 | + # Modes of operation display |
| 28 | + var = ODVariable("Modes of operation display", 0x6061) |
| 29 | + var.data_type = INTEGER8 |
| 30 | + var.access_type = "ro" |
| 31 | + od.add_object(var) |
| 32 | + # Supported drive modes |
| 33 | + var = ODVariable("Supported drive modes", 0x6502) |
| 34 | + var.data_type = UNSIGNED32 |
| 35 | + var.access_type = "ro" |
| 36 | + od.add_object(var) |
| 37 | + return od |
| 38 | + |
| 39 | + |
| 40 | +class TestState402(unittest.TestCase): |
| 41 | + """Tests for the State402 static helper and its lookup tables.""" |
| 42 | + |
| 43 | + def test_sw_mask_all_states_defined(self): |
| 44 | + expected = { |
| 45 | + 'NOT READY TO SWITCH ON', |
| 46 | + 'SWITCH ON DISABLED', |
| 47 | + 'READY TO SWITCH ON', |
| 48 | + 'SWITCHED ON', |
| 49 | + 'OPERATION ENABLED', |
| 50 | + 'FAULT', |
| 51 | + 'FAULT REACTION ACTIVE', |
| 52 | + 'QUICK STOP ACTIVE', |
| 53 | + } |
| 54 | + self.assertEqual(set(State402.SW_MASK.keys()), expected) |
| 55 | + |
| 56 | + def test_sw_mask_values_are_unique(self): |
| 57 | + """Each state must produce a unique (mask, value) pair.""" |
| 58 | + pairs = list(State402.SW_MASK.values()) |
| 59 | + self.assertEqual(len(pairs), len(set(pairs))) |
| 60 | + |
| 61 | + def test_cw_code_commands_round_trip(self): |
| 62 | + """CW_CODE_COMMANDS and CW_COMMANDS_CODE should be inverses.""" |
| 63 | + for code, name in State402.CW_CODE_COMMANDS.items(): |
| 64 | + self.assertEqual(State402.CW_COMMANDS_CODE[name], code) |
| 65 | + for name, code in State402.CW_COMMANDS_CODE.items(): |
| 66 | + self.assertEqual(State402.CW_CODE_COMMANDS[code], name) |
| 67 | + |
| 68 | + def test_next_state_indirect_from_all_known_states(self): |
| 69 | + """Every known state should have an indirect next state.""" |
| 70 | + for state in State402.SW_MASK: |
| 71 | + result = State402.next_state_indirect(state) |
| 72 | + # All states except OPERATION ENABLED should have a path |
| 73 | + if state != 'OPERATION ENABLED': |
| 74 | + self.assertIsNotNone(result, f"No indirect path from {state}") |
| 75 | + self.assertIn(result, State402.SW_MASK, |
| 76 | + f"Indirect state {result} is not a known state") |
| 77 | + |
| 78 | + def test_next_state_indirect_specific_paths(self): |
| 79 | + self.assertEqual( |
| 80 | + State402.next_state_indirect('SWITCH ON DISABLED'), |
| 81 | + 'READY TO SWITCH ON') |
| 82 | + self.assertEqual( |
| 83 | + State402.next_state_indirect('READY TO SWITCH ON'), |
| 84 | + 'SWITCHED ON') |
| 85 | + self.assertEqual( |
| 86 | + State402.next_state_indirect('SWITCHED ON'), |
| 87 | + 'OPERATION ENABLED') |
| 88 | + self.assertEqual( |
| 89 | + State402.next_state_indirect('FAULT'), |
| 90 | + 'SWITCH ON DISABLED') |
| 91 | + self.assertEqual( |
| 92 | + State402.next_state_indirect('FAULT REACTION ACTIVE'), |
| 93 | + 'FAULT') |
| 94 | + self.assertEqual( |
| 95 | + State402.next_state_indirect('QUICK STOP ACTIVE'), |
| 96 | + 'SWITCH ON DISABLED') |
| 97 | + |
| 98 | + def test_next_state_indirect_unknown_state(self): |
| 99 | + self.assertIsNone(State402.next_state_indirect('NONEXISTENT')) |
| 100 | + |
| 101 | + def test_transition_table_keys_are_valid_states(self): |
| 102 | + known = set(State402.SW_MASK.keys()) | {'START', 'DISABLE VOLTAGE'} |
| 103 | + for from_state, to_state in State402.TRANSITIONTABLE: |
| 104 | + self.assertIn(from_state, known, |
| 105 | + f"Unknown from-state: {from_state}") |
| 106 | + self.assertIn(to_state, known, |
| 107 | + f"Unknown to-state: {to_state}") |
| 108 | + |
| 109 | + |
| 110 | +class TestBaseNode402State(unittest.TestCase): |
| 111 | + """Test state property reading from simulated statusword.""" |
| 112 | + |
| 113 | + def setUp(self): |
| 114 | + self.node = BaseNode402(1, _make_od()) |
| 115 | + |
| 116 | + def test_state_from_statusword(self): |
| 117 | + """Verify all state decoding from statusword bits.""" |
| 118 | + test_cases = [ |
| 119 | + (0x0000, 'NOT READY TO SWITCH ON'), |
| 120 | + (0x0040, 'SWITCH ON DISABLED'), |
| 121 | + (0x0021, 'READY TO SWITCH ON'), |
| 122 | + (0x0023, 'SWITCHED ON'), |
| 123 | + (0x0027, 'OPERATION ENABLED'), |
| 124 | + (0x0008, 'FAULT'), |
| 125 | + (0x000F, 'FAULT REACTION ACTIVE'), |
| 126 | + (0x0007, 'QUICK STOP ACTIVE'), |
| 127 | + ] |
| 128 | + for sw, expected_state in test_cases: |
| 129 | + with self.subTest(statusword=hex(sw)): |
| 130 | + self.node.tpdo_values[0x6041] = sw |
| 131 | + self.assertEqual(self.node.state, expected_state) |
| 132 | + |
| 133 | + def test_state_unknown_statusword(self): |
| 134 | + # A statusword that doesn't match any known mask |
| 135 | + self.node.tpdo_values[0x6041] = 0xFFFF |
| 136 | + self.assertEqual(self.node.state, 'UNKNOWN') |
| 137 | + |
| 138 | + def test_is_faulted_true(self): |
| 139 | + self.node.tpdo_values[0x6041] = 0x0008 # FAULT |
| 140 | + self.assertTrue(self.node.is_faulted()) |
| 141 | + |
| 142 | + def test_is_faulted_false(self): |
| 143 | + self.node.tpdo_values[0x6041] = 0x0040 # SWITCH ON DISABLED |
| 144 | + self.assertFalse(self.node.is_faulted()) |
| 145 | + |
| 146 | + def test_controlword_read_raises(self): |
| 147 | + with self.assertRaises(RuntimeError): |
| 148 | + _ = self.node.controlword |
| 149 | + |
| 150 | + |
| 151 | +class TestBaseNode402NextState(unittest.TestCase): |
| 152 | + """Test _next_state logic for state transitions.""" |
| 153 | + |
| 154 | + def setUp(self): |
| 155 | + self.node = BaseNode402(1, _make_od()) |
| 156 | + |
| 157 | + def _set_state(self, state_name): |
| 158 | + _, bits = State402.SW_MASK[state_name] |
| 159 | + self.node.tpdo_values[0x6041] = bits |
| 160 | + |
| 161 | + def test_direct_transition(self): |
| 162 | + """When a direct transition exists, _next_state returns the target.""" |
| 163 | + self._set_state('SWITCH ON DISABLED') |
| 164 | + self.assertEqual( |
| 165 | + self.node._next_state('READY TO SWITCH ON'), |
| 166 | + 'READY TO SWITCH ON') |
| 167 | + |
| 168 | + def test_indirect_transition(self): |
| 169 | + """When no direct path, _next_state returns the indirect next step.""" |
| 170 | + self._set_state('SWITCH ON DISABLED') |
| 171 | + # No direct path to OPERATION ENABLED |
| 172 | + result = self.node._next_state('OPERATION ENABLED') |
| 173 | + self.assertEqual(result, 'READY TO SWITCH ON') |
| 174 | + |
| 175 | + def test_illegal_target_fault(self): |
| 176 | + self._set_state('SWITCH ON DISABLED') |
| 177 | + with self.assertRaises(ValueError): |
| 178 | + self.node._next_state('FAULT') |
| 179 | + |
| 180 | + def test_illegal_target_not_ready(self): |
| 181 | + self._set_state('SWITCH ON DISABLED') |
| 182 | + with self.assertRaises(ValueError): |
| 183 | + self.node._next_state('NOT READY TO SWITCH ON') |
| 184 | + |
| 185 | + def test_illegal_target_fault_reaction(self): |
| 186 | + self._set_state('SWITCH ON DISABLED') |
| 187 | + with self.assertRaises(ValueError): |
| 188 | + self.node._next_state('FAULT REACTION ACTIVE') |
| 189 | + |
| 190 | + def test_full_path_to_operation_enabled(self): |
| 191 | + """Walk the state machine from SWITCH ON DISABLED to OPERATION ENABLED.""" |
| 192 | + path = [] |
| 193 | + self._set_state('SWITCH ON DISABLED') |
| 194 | + target = 'OPERATION ENABLED' |
| 195 | + current = self.node.state |
| 196 | + while current != target: |
| 197 | + next_s = self.node._next_state(target) |
| 198 | + path.append(next_s) |
| 199 | + self._set_state(next_s) |
| 200 | + current = self.node.state |
| 201 | + self.assertEqual(path, [ |
| 202 | + 'READY TO SWITCH ON', |
| 203 | + 'SWITCHED ON', |
| 204 | + 'OPERATION ENABLED', |
| 205 | + ]) |
| 206 | + |
| 207 | + def test_path_from_fault_to_operation_enabled(self): |
| 208 | + """Walk from FAULT to OPERATION ENABLED.""" |
| 209 | + path = [] |
| 210 | + self._set_state('FAULT') |
| 211 | + target = 'OPERATION ENABLED' |
| 212 | + current = self.node.state |
| 213 | + while current != target: |
| 214 | + next_s = self.node._next_state(target) |
| 215 | + path.append(next_s) |
| 216 | + self._set_state(next_s) |
| 217 | + current = self.node.state |
| 218 | + self.assertEqual(path, [ |
| 219 | + 'SWITCH ON DISABLED', |
| 220 | + 'READY TO SWITCH ON', |
| 221 | + 'SWITCHED ON', |
| 222 | + 'OPERATION ENABLED', |
| 223 | + ]) |
| 224 | + |
| 225 | + |
| 226 | +class TestBaseNode402HomingStatus(unittest.TestCase): |
| 227 | + """Test homing status word interpretation.""" |
| 228 | + |
| 229 | + def setUp(self): |
| 230 | + self.node = BaseNode402(1, _make_od()) |
| 231 | + |
| 232 | + def test_homing_states(self): |
| 233 | + test_cases = [ |
| 234 | + (0x0000, 'IN PROGRESS'), |
| 235 | + (0x0400, 'INTERRUPTED'), |
| 236 | + (0x1000, 'ATTAINED'), |
| 237 | + (0x1400, 'TARGET REACHED'), |
| 238 | + (0x2000, 'ERROR VELOCITY IS NOT ZERO'), |
| 239 | + (0x2400, 'ERROR VELOCITY IS ZERO'), |
| 240 | + ] |
| 241 | + for sw, expected in test_cases: |
| 242 | + with self.subTest(statusword=hex(sw)): |
| 243 | + self.node.tpdo_values[0x6041] = sw |
| 244 | + # Test the bitmask logic directly without calling _homing_status, |
| 245 | + # which would require a fully configured TPDO setup |
| 246 | + status = None |
| 247 | + for key, value in Homing.STATES.items(): |
| 248 | + bitmask, bits = value |
| 249 | + if sw & bitmask == bits: |
| 250 | + status = key |
| 251 | + self.assertEqual(status, expected) |
| 252 | + |
| 253 | + |
| 254 | +class TestOperationMode(unittest.TestCase): |
| 255 | + """Test OperationMode lookup tables.""" |
| 256 | + |
| 257 | + def test_code2name_name2code_round_trip(self): |
| 258 | + for code, name in OperationMode.CODE2NAME.items(): |
| 259 | + self.assertEqual(OperationMode.NAME2CODE[name], code) |
| 260 | + |
| 261 | + def test_supported_bitmask_unique(self): |
| 262 | + values = list(OperationMode.SUPPORTED.values()) |
| 263 | + # All bitmasks should be unique (each is a single bit) |
| 264 | + self.assertEqual(len(values), len(set(values))) |
| 265 | + |
| 266 | + def test_all_named_modes_have_support_bit(self): |
| 267 | + for name in OperationMode.NAME2CODE: |
| 268 | + self.assertIn(name, OperationMode.SUPPORTED) |
| 269 | + |
| 270 | + |
| 271 | +class TestBaseNode402TPDOCallback(unittest.TestCase): |
| 272 | + """Test the TPDO update callback.""" |
| 273 | + |
| 274 | + def setUp(self): |
| 275 | + self.node = BaseNode402(1, _make_od()) |
| 276 | + |
| 277 | + def test_on_tpdos_update_callback(self): |
| 278 | + fake_obj = MagicMock(index=0x6041, raw=0x0027) |
| 279 | + fake_map = MagicMock() |
| 280 | + fake_map.__iter__ = lambda s: iter([fake_obj]) |
| 281 | + self.node.on_TPDOs_update_callback(fake_map) |
| 282 | + self.assertEqual(self.node.tpdo_values[0x6041], 0x0027) |
| 283 | + |
| 284 | + |
| 285 | +if __name__ == '__main__': |
| 286 | + unittest.main() |
0 commit comments