ArtusAPI 1.3.17__tar.gz → 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. artusapi-2.0.0/ArtusAPI/__init__.py +1 -0
  2. artusapi-2.0.0/ArtusAPI/api_tests/__init__.py +1 -0
  3. artusapi-2.0.0/ArtusAPI/api_tests/mocks.py +86 -0
  4. artusapi-2.0.0/ArtusAPI/api_tests/test_artus_api_v2.py +343 -0
  5. artusapi-2.0.0/ArtusAPI/api_tests/test_modbus_map.py +60 -0
  6. artusapi-2.0.0/ArtusAPI/api_tests/test_new_commands.py +146 -0
  7. artusapi-2.0.0/ArtusAPI/api_tests/test_new_communication.py +102 -0
  8. artusapi-2.0.0/ArtusAPI/api_tests/test_robot_models.py +49 -0
  9. artusapi-2.0.0/ArtusAPI/api_tests/test_rtu_packet_parity.py +370 -0
  10. artusapi-2.0.0/ArtusAPI/api_tests/test_slave_id_map.py +55 -0
  11. artusapi-2.0.0/ArtusAPI/artus_api_new.py +942 -0
  12. artusapi-2.0.0/ArtusAPI/commands/__init__.py +7 -0
  13. artusapi-2.0.0/ArtusAPI/commands/new_commands.py +457 -0
  14. artusapi-2.0.0/ArtusAPI/common/ModbusMap.py +122 -0
  15. artusapi-2.0.0/ArtusAPI/common/SlaveIDMap.py +76 -0
  16. artusapi-2.0.0/ArtusAPI/common/__init__.py +15 -0
  17. artusapi-2.0.0/ArtusAPI/communication/Modbus_TCP/__init__.py +4 -0
  18. artusapi-2.0.0/ArtusAPI/communication/Modbus_TCP/modbus_tcp.py +292 -0
  19. artusapi-2.0.0/ArtusAPI/communication/RS485_RTU/__init__.py +1 -0
  20. artusapi-2.0.0/ArtusAPI/communication/RS485_RTU/rs485_rtu.py +309 -0
  21. artusapi-2.0.0/ArtusAPI/communication/RS485_RTU/rs485_rtu_minimalmodbus.py +222 -0
  22. artusapi-2.0.0/ArtusAPI/communication/__init__.py +4 -0
  23. artusapi-2.0.0/ArtusAPI/communication/new_communication.py +230 -0
  24. artusapi-2.0.0/ArtusAPI/firmware_update/FirmwareUpdaterNew.py +277 -0
  25. artusapi-2.0.0/ArtusAPI/firmware_update/__init__.py +8 -0
  26. artusapi-2.0.0/ArtusAPI/firmware_update/upload_esptool.py +76 -0
  27. artusapi-2.0.0/ArtusAPI/robot/artus_dex/__init__.py +5 -0
  28. artusapi-2.0.0/ArtusAPI/robot/artus_dex/artus_dex.py +74 -0
  29. artusapi-2.0.0/ArtusAPI/robot/artus_dex/artus_dex_left.py +26 -0
  30. artusapi-2.0.0/ArtusAPI/robot/artus_dex/artus_dex_right.py +26 -0
  31. artusapi-2.0.0/ArtusAPI/robot/artus_lite/__init__.py +8 -0
  32. artusapi-2.0.0/ArtusAPI/robot/artus_lite/artus_lite.py +81 -0
  33. artusapi-2.0.0/ArtusAPI/robot/artus_lite/artus_lite_left.py +31 -0
  34. artusapi-2.0.0/ArtusAPI/robot/artus_lite/artus_lite_plus.py +46 -0
  35. artusapi-2.0.0/ArtusAPI/robot/artus_lite/artus_lite_plus_left.py +32 -0
  36. artusapi-2.0.0/ArtusAPI/robot/artus_lite/artus_lite_plus_right.py +32 -0
  37. artusapi-2.0.0/ArtusAPI/robot/artus_lite/artus_lite_right.py +31 -0
  38. artusapi-2.0.0/ArtusAPI/robot/artus_scorpion/__init__.py +1 -0
  39. artusapi-2.0.0/ArtusAPI/robot/artus_scorpion/artus_scorpion.py +147 -0
  40. artusapi-2.0.0/ArtusAPI/robot/artus_talos/__init__.py +5 -0
  41. artusapi-2.0.0/ArtusAPI/robot/artus_talos/artus_talos.py +81 -0
  42. artusapi-2.0.0/ArtusAPI/robot/artus_talos/artus_talos_left.py +28 -0
  43. artusapi-2.0.0/ArtusAPI/robot/artus_talos/artus_talos_right.py +28 -0
  44. artusapi-2.0.0/ArtusAPI/robot/bldc_robot/__init__.py +1 -0
  45. artusapi-2.0.0/ArtusAPI/robot/bldc_robot/bldcrobot.py +358 -0
  46. artusapi-2.0.0/ArtusAPI/robot/robot.py +153 -0
  47. artusapi-2.0.0/ArtusAPI/sensors/ForceSensor.py +19 -0
  48. artusapi-2.0.0/PKG-INFO +193 -0
  49. artusapi-2.0.0/README.md +174 -0
  50. artusapi-2.0.0/artusapi.egg-info/PKG-INFO +193 -0
  51. artusapi-2.0.0/artusapi.egg-info/SOURCES.txt +56 -0
  52. artusapi-2.0.0/artusapi.egg-info/requires.txt +29 -0
  53. artusapi-2.0.0/setup.py +54 -0
  54. ArtusAPI-1.3.17/ArtusAPI/__init__.py +0 -1
  55. ArtusAPI-1.3.17/ArtusAPI/artus_api.py +0 -790
  56. ArtusAPI-1.3.17/ArtusAPI/commands/__init__.py +0 -1
  57. ArtusAPI-1.3.17/ArtusAPI/commands/commands.py +0 -229
  58. ArtusAPI-1.3.17/ArtusAPI/communication/UART/__init__.py +0 -1
  59. ArtusAPI-1.3.17/ArtusAPI/communication/UART/uart.py +0 -124
  60. ArtusAPI-1.3.17/ArtusAPI/communication/WiFi/__init__.py +0 -1
  61. ArtusAPI-1.3.17/ArtusAPI/communication/WiFi/wifi_server.py +0 -227
  62. ArtusAPI-1.3.17/ArtusAPI/communication/__init__.py +0 -1
  63. ArtusAPI-1.3.17/ArtusAPI/communication/communication.py +0 -254
  64. ArtusAPI-1.3.17/ArtusAPI/firmware_update/__init__.py +0 -1
  65. ArtusAPI-1.3.17/ArtusAPI/firmware_update/firmware_updater.py +0 -143
  66. ArtusAPI-1.3.17/ArtusAPI/firmware_update/run_full_firmware_update.py +0 -117
  67. ArtusAPI-1.3.17/ArtusAPI/robot/artus_lite/__init__.py +0 -5
  68. ArtusAPI-1.3.17/ArtusAPI/robot/artus_lite/artus_lite.py +0 -218
  69. ArtusAPI-1.3.17/ArtusAPI/robot/artus_lite/artus_lite_left.py +0 -21
  70. ArtusAPI-1.3.17/ArtusAPI/robot/artus_lite/artus_lite_plus.py +0 -88
  71. ArtusAPI-1.3.17/ArtusAPI/robot/artus_lite/artus_lite_plus_left.py +0 -10
  72. ArtusAPI-1.3.17/ArtusAPI/robot/artus_lite/artus_lite_plus_right.py +0 -10
  73. ArtusAPI-1.3.17/ArtusAPI/robot/artus_lite/artus_lite_right.py +0 -21
  74. ArtusAPI-1.3.17/ArtusAPI/robot/robot.py +0 -88
  75. ArtusAPI-1.3.17/ArtusAPI/sensors/ForceSensor.py +0 -12
  76. ArtusAPI-1.3.17/ArtusAPI.egg-info/PKG-INFO +0 -212
  77. ArtusAPI-1.3.17/ArtusAPI.egg-info/SOURCES.txt +0 -32
  78. ArtusAPI-1.3.17/ArtusAPI.egg-info/entry_points.txt +0 -4
  79. ArtusAPI-1.3.17/ArtusAPI.egg-info/requires.txt +0 -6
  80. ArtusAPI-1.3.17/PKG-INFO +0 -212
  81. ArtusAPI-1.3.17/README.md +0 -197
  82. ArtusAPI-1.3.17/setup.py +0 -32
  83. {ArtusAPI-1.3.17 → artusapi-2.0.0}/ArtusAPI/robot/__init__.py +0 -0
  84. {ArtusAPI-1.3.17 → artusapi-2.0.0}/ArtusAPI/sensors/__init__.py +0 -0
  85. {ArtusAPI-1.3.17/ArtusAPI.egg-info → artusapi-2.0.0/artusapi.egg-info}/dependency_links.txt +0 -0
  86. {ArtusAPI-1.3.17/ArtusAPI.egg-info → artusapi-2.0.0/artusapi.egg-info}/top_level.txt +0 -0
  87. {ArtusAPI-1.3.17 → artusapi-2.0.0}/setup.cfg +0 -0
@@ -0,0 +1 @@
1
+ from .artus_api_new import ArtusAPI_V2
@@ -0,0 +1 @@
1
+ # Internal API tests (hardware-independent). See README.md in this folder.
@@ -0,0 +1,86 @@
1
+ """
2
+ Shared mocks for ArtusAPI tests (no hardware).
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import unittest.mock as mock
8
+ from contextlib import contextmanager
9
+ from typing import Any, Generator, Tuple, Type
10
+
11
+ from ArtusAPI.communication.new_communication import ActuatorState
12
+
13
+
14
+ def make_communication_mock() -> mock.MagicMock:
15
+ """Builds a MagicMock standing in for NewCommunication.
16
+
17
+ Returns:
18
+ A MagicMock with the NewCommunication methods used by ArtusAPI_V2
19
+ (open_connection, close_connection, send_data, receive_data,
20
+ wait_for_ready, _check_robot_state) pre-configured with sane defaults.
21
+ """
22
+ m = mock.MagicMock()
23
+ m.open_connection = mock.Mock()
24
+ m.close_connection = mock.Mock()
25
+ m.send_data = mock.Mock(return_value=True)
26
+ m.receive_data = mock.Mock(return_value=0)
27
+ m.send_receive_data = mock.Mock(return_value=0)
28
+ m.wait_for_ready = mock.Mock(return_value=ActuatorState.ACTUATOR_IDLE.value)
29
+ m._check_robot_state = mock.Mock(return_value=ActuatorState.ACTUATOR_IDLE.value)
30
+ return m
31
+
32
+
33
+ @contextmanager
34
+ def patched_artus_api_v2_constructor(
35
+ communication_mock: mock.MagicMock | None = None,
36
+ ) -> Generator[Tuple[Type, mock.MagicMock], None, None]:
37
+ """Patches NewCommunication, time.sleep, and signal.signal while constructing ArtusAPI_V2.
38
+
39
+ Args:
40
+ communication_mock: Mock to substitute for the real NewCommunication
41
+ instance. If None, one is created via make_communication_mock().
42
+
43
+ Yields:
44
+ A tuple of (ArtusAPI_V2 class, communication mock instance returned
45
+ to the API).
46
+ """
47
+ if communication_mock is None:
48
+ communication_mock = make_communication_mock()
49
+
50
+ import ArtusAPI.artus_api_new as api_mod
51
+
52
+ with mock.patch.object(api_mod, "NewCommunication", autospec=True) as nc_cls:
53
+ nc_cls.return_value = communication_mock
54
+ with mock.patch.object(api_mod.time, "sleep"):
55
+ with mock.patch.object(api_mod.signal, "signal"):
56
+ yield api_mod.ArtusAPI_V2, communication_mock
57
+
58
+
59
+ def build_api(
60
+ robot_type: str = "artus_lite",
61
+ hand_type: str = "left",
62
+ communication_mock: mock.MagicMock | None = None,
63
+ **kwargs: Any,
64
+ ):
65
+ """Constructs ArtusAPI_V2 with communication fully mocked.
66
+
67
+ Args:
68
+ robot_type: Robot type string passed to ArtusAPI_V2 (e.g. "artus_lite").
69
+ hand_type: Hand type string passed to ArtusAPI_V2 (e.g. "left").
70
+ communication_mock: Mock to substitute for the real NewCommunication
71
+ instance. If None, one is created via make_communication_mock().
72
+ **kwargs: Additional keyword arguments forwarded to ArtusAPI_V2.
73
+
74
+ Returns:
75
+ A tuple of (ArtusAPI_V2 instance, communication mock instance).
76
+ """
77
+ comm = communication_mock or make_communication_mock()
78
+ with patched_artus_api_v2_constructor(comm) as (Cls, _):
79
+ api = Cls(
80
+ robot_type=robot_type,
81
+ hand_type=hand_type,
82
+ communication_method="RS485_RTU",
83
+ communication_channel_identifier="MOCK",
84
+ **kwargs,
85
+ )
86
+ return api, comm
@@ -0,0 +1,343 @@
1
+ """ArtusAPI_V2 tests with mocked communication (no hardware)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import struct
6
+ import unittest
7
+ from unittest.mock import MagicMock, patch
8
+
9
+ from ArtusAPI.api_tests.mocks import build_api, make_communication_mock, patched_artus_api_v2_constructor
10
+ from ArtusAPI.common.ModbusMap import ModbusMap
11
+ from ArtusAPI.common.SlaveIDMap import expected_slave_id
12
+ from ArtusAPI.communication.new_communication import ActuatorState, CommandType
13
+
14
+
15
+ class TestArtusAPIV2Mocked(unittest.TestCase):
16
+ """Exercises ArtusAPI_V2 behavior with the communication layer mocked out."""
17
+
18
+ def test_connect_opens_transport(self):
19
+ """Verifies constructing the API opens the communication transport."""
20
+ comm = MagicMock()
21
+ api, comm = build_api(communication_mock=comm)
22
+ comm.open_connection.assert_called()
23
+
24
+ def test_disconnect_restores_signal(self):
25
+ """Verifies disconnect() re-registers the original signal handler."""
26
+ import ArtusAPI.artus_api_new as api_mod
27
+
28
+ comm = MagicMock()
29
+ with patched_artus_api_v2_constructor(comm):
30
+ with patch.object(api_mod.signal, "signal") as sig_mock:
31
+ api = api_mod.ArtusAPI_V2(
32
+ robot_type="artus_lite",
33
+ hand_type="left",
34
+ communication_method="RS485_RTU",
35
+ communication_channel_identifier="MOCK",
36
+ )
37
+ api.disconnect()
38
+ self.assertTrue(sig_mock.called)
39
+
40
+ def test_robot_type_hand_type_stored(self):
41
+ """Verifies robot_type and hand_type constructor args are stored on the instance."""
42
+ api, _ = build_api(robot_type="artus_talos", hand_type="right")
43
+ self.assertEqual(api.robot_type, "artus_talos")
44
+ self.assertEqual(api.hand_type, "right")
45
+
46
+ def test_set_control_type_valid(self):
47
+ """Verifies set_control_type accepts a valid control type and updates state."""
48
+ api, _ = build_api()
49
+ self.assertTrue(api.set_control_type(3))
50
+ self.assertEqual(api.control_type, 3)
51
+
52
+ def test_set_control_type_invalid(self):
53
+ """Verifies set_control_type rejects an unknown control type."""
54
+ api, _ = build_api()
55
+ self.assertFalse(api.set_control_type(99))
56
+
57
+ def test_wake_up_sets_awake_when_ready(self):
58
+ """Verifies wake_up() sets api.awake and sends the start command when the hand reports ready."""
59
+ comm = MagicMock()
60
+ comm.wait_for_ready.return_value = ActuatorState.ACTUATOR_IDLE.value
61
+ api, comm = build_api(communication_mock=comm)
62
+ api.wake_up(control_type=3)
63
+ self.assertTrue(api.awake)
64
+ comm.send_data.assert_called()
65
+
66
+ def test_sleep_sends_command(self):
67
+ """Verifies sleep() sends a command over the communication layer."""
68
+ api, comm = build_api()
69
+ api.sleep()
70
+ comm.send_data.assert_called()
71
+
72
+ def test_get_robot_status(self):
73
+ """Verifies get_robot_status decodes the raw state byte into a state name and trajectory return value."""
74
+ comm = MagicMock()
75
+ comm._check_robot_state.return_value = 0x21
76
+ api, comm = build_api(communication_mock=comm)
77
+ st, tr = api.get_robot_status()
78
+ self.assertEqual(st, ActuatorState(1).name)
79
+ self.assertIsNotNone(tr)
80
+
81
+ def test_new_communication_gets_slave_address_from_slave_id_map(self):
82
+ """Verifies NewCommunication is constructed with the slave address resolved from SlaveIDMap."""
83
+ import ArtusAPI.artus_api_new as api_mod
84
+
85
+ comm = make_communication_mock()
86
+ with patch.object(api_mod, "NewCommunication", autospec=True) as nc_cls:
87
+ nc_cls.return_value = comm
88
+ with patch.object(api_mod.time, "sleep"):
89
+ with patch.object(api_mod.signal, "signal"):
90
+ api_mod.ArtusAPI_V2(
91
+ robot_type="artus_talos",
92
+ hand_type="right",
93
+ communication_method="RS485_RTU",
94
+ communication_channel_identifier="MOCK",
95
+ )
96
+ nc_cls.assert_called_once()
97
+ kwargs = nc_cls.call_args.kwargs
98
+ self.assertEqual(
99
+ kwargs["slave_address"],
100
+ expected_slave_id("artus_talos", "right"),
101
+ )
102
+
103
+ def test_get_voltage(self):
104
+ """Verifies get_voltage decodes a two-register IEEE 754 float feedback value."""
105
+ comm = MagicMock()
106
+ raw = struct.pack("<f", 12.5)
107
+ w0, w1 = struct.unpack("<HH", raw)
108
+ comm.receive_data.return_value = [w0, w1]
109
+ api, comm = build_api(communication_mock=comm)
110
+ api.awake = True
111
+ v = api.get_voltage()
112
+ self.assertIsInstance(v, float)
113
+ self.assertAlmostEqual(v, 12.5, places=4)
114
+
115
+ def test_get_joint_angles_slave_id_path(self):
116
+ """Verifies get_joint_angles returns the raw slave ID when reading the slave_id_reg register."""
117
+ comm = MagicMock()
118
+ comm.receive_data.return_value = 0x0003
119
+ api, comm = build_api(robot_type="artus_lite_plus", hand_type="left", communication_mock=comm)
120
+ api.awake = True
121
+ reg = ModbusMap().modbus_reg_map["slave_id_reg"]
122
+ sid = api.get_joint_angles(start_reg=reg)
123
+ self.assertEqual(sid, 3)
124
+
125
+ def test_helper_fill_dict_from_feedback(self):
126
+ """Verifies helper_fill_dict_from_feedback_data maps feedback values to joint names in order."""
127
+ api, _ = build_api()
128
+ names = api._robot_handler.robot.joint_names
129
+ data = list(range(len(names)))
130
+ d = api.helper_fill_dict_from_feedback_data(data)
131
+ for i, n in enumerate(names):
132
+ self.assertEqual(d[n], data[i])
133
+
134
+ def test_set_joint_angles_sends_position_command(self):
135
+ """Verifies set_joint_angles sends a TARGET_COMMAND for a single joint target."""
136
+ comm = MagicMock()
137
+ api, comm = build_api()
138
+ api.awake = True
139
+ api.control_type = api.control_types["position"]
140
+ api.last_time = 0.0
141
+ with patch("ArtusAPI.artus_api_new.time.perf_counter", return_value=10.0):
142
+ api.set_joint_angles({"thumb_spread": {"target_angle": 5}})
143
+ comm.send_data.assert_called()
144
+ args = comm.send_data.call_args[0]
145
+ self.assertEqual(args[1], CommandType.TARGET_COMMAND.value)
146
+
147
+ def test_set_joint_angles_by_list_delegates_to_set_joint_angles(self):
148
+ """Verifies set_joint_angles_by_list builds an index-keyed target dict and delegates to set_joint_angles."""
149
+ comm = MagicMock()
150
+ api, comm = build_api()
151
+ api.awake = True
152
+ api.control_type = api.control_types["position"]
153
+ api.last_time = 0.0
154
+ with patch.object(api, "set_joint_angles", return_value=True) as m:
155
+ api.set_joint_angles_by_list([0, 0], control_type=3)
156
+ m.assert_called_once()
157
+ call_kw = m.call_args[0][0]
158
+ self.assertIn("0", call_kw)
159
+ self.assertIn("target_angle", call_kw["0"])
160
+
161
+ def test_set_home_position(self):
162
+ """Verifies set_home_position sends a command over the communication layer."""
163
+ comm = MagicMock()
164
+ api, comm = build_api()
165
+ api.awake = True
166
+ api.last_time = 0.0
167
+ with patch("ArtusAPI.artus_api_new.time.perf_counter", return_value=10.0):
168
+ api.set_home_position()
169
+ comm.send_data.assert_called()
170
+
171
+ def test_get_joint_forces_returns_dict(self):
172
+ """Verifies get_joint_forces returns one entry per joint."""
173
+ comm = MagicMock()
174
+ n = 16
175
+ regs = [0] * (2 * n)
176
+ comm.receive_data.return_value = regs
177
+ api, comm = build_api(communication_mock=comm)
178
+ api.awake = True
179
+ out = api.get_joint_forces()
180
+ self.assertIsInstance(out, dict)
181
+ self.assertEqual(len(out), n)
182
+
183
+ def test_get_fingertip_forces_lite_plus(self):
184
+ """Verifies get_fingertip_forces returns a dict for the artus_lite_plus hand."""
185
+ comm = MagicMock()
186
+ comm.receive_data.return_value = [0] * 30
187
+ api, comm = build_api(robot_type="artus_lite_plus", hand_type="left", communication_mock=comm)
188
+ api.awake = True
189
+ out = api.get_fingertip_forces()
190
+ self.assertIsInstance(out, dict)
191
+
192
+ def test_get_avg_temperature(self):
193
+ """Verifies get_avg_temperature returns the value read from the feedback register."""
194
+ comm = MagicMock()
195
+ comm.receive_data.return_value = [25]
196
+ api, comm = build_api(communication_mock=comm)
197
+ api.awake = True
198
+ t = api.get_avg_temperature()
199
+ self.assertEqual(t, 25)
200
+
201
+ def test_get_streamed_joint_angles_returns_none(self):
202
+ """Verifies get_streamed_joint_angles returns None when no streaming data has been received."""
203
+ api, _ = build_api()
204
+ self.assertIsNone(api.get_streamed_joint_angles())
205
+
206
+ def test_reset_with_joint_count(self):
207
+ """Verifies reset() sends a command over the communication layer when given a joint count."""
208
+ comm = MagicMock()
209
+ comm.wait_for_ready.return_value = ActuatorState.ACTUATOR_IDLE.value
210
+ api, comm = build_api(communication_mock=comm)
211
+ api.last_time = 0.0
212
+ with patch("ArtusAPI.artus_api_new.time.perf_counter", return_value=10.0):
213
+ api.reset(joints=0)
214
+ comm.send_data.assert_called()
215
+
216
+ def test_calibrate(self):
217
+ """Verifies calibrate() sends a command over the communication layer."""
218
+ comm = MagicMock()
219
+ comm.wait_for_ready.return_value = ActuatorState.ACTUATOR_READY.value
220
+ api, comm = build_api(communication_mock=comm)
221
+ api.awake = True
222
+ api.calibrate(joint=0)
223
+ comm.send_data.assert_called()
224
+
225
+ def test_get_hand_feedback_data_patched(self):
226
+ """Verifies get_hand_feedback_data succeeds with get_joint_angles patched, for a lite hand."""
227
+ api, _ = build_api(robot_type="artus_lite", hand_type="left")
228
+ api.awake = True
229
+ with patch.object(api, "get_joint_angles", return_value={}):
230
+ self.assertTrue(api.get_hand_feedback_data())
231
+
232
+ def test_get_config_writes_wifi_and_reads_ip(self):
233
+ """Verifies get_config sends a trigger and payload write for each of SSID and password."""
234
+ comm = MagicMock()
235
+ comm.wait_for_ready.side_effect = [
236
+ ActuatorState.ACTUATOR_CONFIG.value, ActuatorState.ACTUATOR_CONFIG_FINISH.value,
237
+ ActuatorState.ACTUATOR_CONFIG.value, ActuatorState.ACTUATOR_CONFIG_FINISH.value,
238
+ ]
239
+ comm.receive_data.return_value = [(192 << 8) | 168, (1 << 8) | 50]
240
+ api, comm = build_api(communication_mock=comm)
241
+
242
+ api.get_config("SSID", "PASS")
243
+
244
+ # 2 send_data calls per config value (trigger + payload) x 2 values
245
+ self.assertEqual(comm.send_data.call_count, 4)
246
+ payload_call_types = [c.args[1] for c in comm.send_data.call_args_list if len(c.args) > 1]
247
+ self.assertTrue(all(t == CommandType.CONFIG_COMMAND.value for t in payload_call_types))
248
+ self.assertEqual(len(payload_call_types), 2)
249
+
250
+ def test_string_to_registers(self):
251
+ """Verifies string_to_registers packs two chars per register and zero-pads odd-length strings."""
252
+ api, _ = build_api()
253
+ self.assertEqual(api.string_to_registers("AB"), [0x4142])
254
+ # odd-length strings are zero-padded to fill the last register
255
+ self.assertEqual(api.string_to_registers("A"), [0x4100])
256
+
257
+ def test_get_hand_feedback_data_talos_with_force_patch(self):
258
+ """Verifies get_hand_feedback_data succeeds for a talos hand with angles and fingertip forces patched."""
259
+ api, _ = build_api(robot_type="artus_talos", hand_type="left")
260
+ api.awake = True
261
+ with patch.object(api, "get_joint_angles", return_value={}):
262
+ with patch.object(api, "get_fingertip_forces", return_value={}):
263
+ self.assertTrue(api.get_hand_feedback_data())
264
+
265
+ def test_set_get_joint_angles_sends_fc17_request(self):
266
+ """Verifies set_get_joint_angles issues one send_receive_data call with matching read/write args."""
267
+ comm = MagicMock()
268
+ n = 16
269
+ comm.send_receive_data.return_value = [0] * 8 # position: 0.5 word/joint * 16 -> 8
270
+ api, comm = build_api(communication_mock=comm)
271
+ api.awake = True
272
+ api.last_time = 0.0
273
+ with patch("ArtusAPI.artus_api_new.time.perf_counter", return_value=10.0):
274
+ out = api.set_get_joint_angles({"thumb_spread": {"target_angle": 5}})
275
+ comm.send_receive_data.assert_called_once()
276
+ read_start, read_count, write_start, values = comm.send_receive_data.call_args[0]
277
+ self.assertEqual(read_start, ModbusMap().modbus_reg_map["feedback_position_start_reg"])
278
+ self.assertEqual(read_count, 8)
279
+ self.assertEqual(write_start, ModbusMap().modbus_reg_map["target_position_start_reg"])
280
+ self.assertEqual(len(values), n // 2)
281
+ self.assertIsInstance(out, dict)
282
+ self.assertEqual(len(out), n)
283
+
284
+ def test_set_get_joint_speeds_read_count(self):
285
+ """Verifies set_get_joint_speeds requests one register per joint."""
286
+ comm = MagicMock()
287
+ comm.send_receive_data.return_value = [0] * 16 # velocity: 1 word/joint * 16
288
+ api, comm = build_api(communication_mock=comm)
289
+ api.awake = True
290
+ api.last_time = 0.0
291
+ with patch("ArtusAPI.artus_api_new.time.perf_counter", return_value=10.0):
292
+ api.set_get_joint_speeds({"thumb_spread": {"target_velocity": 100}})
293
+ _, read_count, _, _ = comm.send_receive_data.call_args[0]
294
+ self.assertEqual(read_count, 16)
295
+
296
+ def test_set_get_joint_forces_read_count(self):
297
+ """Verifies set_get_joint_forces requests two registers per joint (float feedback)."""
298
+ comm = MagicMock()
299
+ comm.send_receive_data.return_value = [0] * 32 # force: 2 words/joint * 16
300
+ api, comm = build_api(communication_mock=comm)
301
+ api.awake = True
302
+ api.last_time = 0.0
303
+ with patch("ArtusAPI.artus_api_new.time.perf_counter", return_value=10.0):
304
+ out = api.set_get_joint_forces({"thumb_spread": {"target_force": 1.5}})
305
+ _, read_count, _, _ = comm.send_receive_data.call_args[0]
306
+ self.assertEqual(read_count, 32)
307
+ self.assertEqual(len(out), 16)
308
+
309
+ def test_set_get_joint_angles_partial_dict_still_sends_full_command(self):
310
+ """Verifies a single-joint update still packs/writes all joints (unset joints default to 0),
311
+ matching the behavior of the existing write-only set_joint_angles path."""
312
+ comm = MagicMock()
313
+ comm.send_receive_data.return_value = [0] * 8
314
+ api, comm = build_api(communication_mock=comm)
315
+ api.awake = True
316
+ api.last_time = 0.0
317
+ with patch("ArtusAPI.artus_api_new.time.perf_counter", return_value=10.0):
318
+ api.set_get_joint_angles({"thumb_spread": {"target_angle": 5}})
319
+ _, _, _, values = comm.send_receive_data.call_args[0]
320
+ # thumb_spread is joint index 0, packed high-byte in the first word alongside
321
+ # thumb_flex (index 1, untouched -> defaults to 0, low byte of the same word).
322
+ self.assertEqual(values[0] & 0xFF, 0)
323
+ self.assertNotEqual(values[0] >> 8, 0)
324
+
325
+ def test_set_get_joint_angles_no_valid_data_returns_false(self):
326
+ """Verifies set_get_joint_angles returns False and does not touch the wire when the dict has no usable data."""
327
+ comm = MagicMock()
328
+ api, comm = build_api(communication_mock=comm)
329
+ api.awake = True
330
+ self.assertFalse(api.set_get_joint_angles({}))
331
+ comm.send_receive_data.assert_not_called()
332
+
333
+ def test_set_get_joint_angles_not_awake_returns_none(self):
334
+ """Verifies set_get_joint_angles short-circuits and does not touch the wire when the hand is not awake."""
335
+ comm = MagicMock()
336
+ api, comm = build_api(communication_mock=comm)
337
+ with patch.object(api, "_check_awake", return_value=False):
338
+ self.assertIsNone(api.set_get_joint_angles({"thumb_spread": {"target_angle": 5}}))
339
+ comm.send_receive_data.assert_not_called()
340
+
341
+
342
+ if __name__ == "__main__":
343
+ unittest.main()
@@ -0,0 +1,60 @@
1
+ """Tests for ArtusAPI.common.ModbusMap."""
2
+
3
+ import unittest
4
+
5
+ from ArtusAPI.common.ModbusMap import ModbusMap, ActuatorState, CommandType, TrajectoryReturn
6
+
7
+
8
+ class TestModbusMap(unittest.TestCase):
9
+ """Verifies ModbusMap register layout and the related state/command enums."""
10
+
11
+ def setUp(self):
12
+ """Creates a fresh ModbusMap instance for each test."""
13
+ self.m = ModbusMap()
14
+
15
+ def test_slave_id_reg_after_avg_temperature(self):
16
+ """Verifies the slave ID register is addressed after the average temperature feedback register."""
17
+ regs = self.m.modbus_reg_map
18
+ self.assertLess(regs["feedback_avg_temperature_start_reg"], regs["slave_id_reg"])
19
+
20
+ def test_reg_maps_have_same_keys(self):
21
+ """Verifies every register has a corresponding data-type multiplier entry."""
22
+ self.assertEqual(
23
+ set(self.m.modbus_reg_map.keys()),
24
+ set(self.m.data_type_multiplier_map.keys()),
25
+ )
26
+
27
+ def test_command_register_zero(self):
28
+ """Verifies the command register is fixed at address 0."""
29
+ self.assertEqual(self.m.modbus_reg_map["command_register"], 0)
30
+
31
+ def test_actuator_state_enum(self):
32
+ """Verifies ACTUATOR_IDLE has the expected enum value."""
33
+ self.assertEqual(ActuatorState.ACTUATOR_IDLE.value, 1)
34
+
35
+ def test_command_type_values(self):
36
+ """Verifies SETUP_COMMANDS, TARGET_COMMAND, and READ_WRITE_COMMAND opcodes."""
37
+ self.assertEqual(CommandType.SETUP_COMMANDS.value, 6)
38
+ self.assertEqual(CommandType.TARGET_COMMAND.value, 16)
39
+ self.assertEqual(CommandType.READ_WRITE_COMMAND.value, 23)
40
+
41
+ def test_config_command_matches_update_config_opcode(self):
42
+ """Verifies CONFIG_COMMAND (68) equals the 0x44 update_config_command opcode."""
43
+ # CONFIG_COMMAND (68) must equal update_config_command's 0x44 opcode -
44
+ # both identify the same onboard-config write to the firmware.
45
+ self.assertEqual(CommandType.CONFIG_COMMAND.value, 0x44)
46
+
47
+ def test_actuator_config_states(self):
48
+ """Verifies the ACTUATOR_CONFIG and ACTUATOR_CONFIG_FINISH enum values."""
49
+ self.assertEqual(ActuatorState.ACTUATOR_CONFIG.value, 14)
50
+ self.assertEqual(ActuatorState.ACTUATOR_CONFIG_FINISH.value, 15)
51
+
52
+ def test_trajectory_return_enum(self):
53
+ """Verifies the TrajectoryReturn enum values for running, stopped, and complete states."""
54
+ self.assertEqual(TrajectoryReturn.TRAJECTORY_RUNNING.value, 0)
55
+ self.assertEqual(TrajectoryReturn.TRAJECTORY_STOPPED.value, 1)
56
+ self.assertEqual(TrajectoryReturn.TRAJECTORY_COMPLETE.value, 2)
57
+
58
+
59
+ if __name__ == "__main__":
60
+ unittest.main()
@@ -0,0 +1,146 @@
1
+ """Unit tests for ArtusAPI.commands.new_commands.NewCommands (no hardware)."""
2
+
3
+ import logging
4
+ import struct
5
+ import unittest
6
+ from types import SimpleNamespace
7
+ from ArtusAPI.commands.new_commands import NewCommands
8
+
9
+
10
+ class TestNewCommands(unittest.TestCase):
11
+ """Verifies NewCommands command serialization and feedback decoding."""
12
+
13
+ def setUp(self):
14
+ """Creates a 16-joint NewCommands instance for each test."""
15
+ self.nc = NewCommands(num_joints=16, logger=logging.getLogger("test_nc"))
16
+
17
+ def test_get_robot_start_command(self):
18
+ """Verifies the start command is packed as [start_command, control_type]."""
19
+ cmd = self.nc.get_robot_start_command(control_type=3)
20
+ self.assertEqual(cmd, [self.nc.commands["start_command"], 3])
21
+
22
+ def test_get_sleep_command(self):
23
+ """Verifies the sleep command is a single-element list."""
24
+ self.assertEqual(self.nc.get_sleep_command(), [self.nc.commands["sleep_command"]])
25
+
26
+ def test_get_clear_errors_command(self):
27
+ """Verifies the clear-errors command payload and its 0x1A opcode."""
28
+ # Single-byte payload mirroring get_sleep_command - firmware maps the
29
+ # 0x1A value through Actuator_event_e -> ACTUATOR_EVENT_CLEAR_ERRORS.
30
+ self.assertEqual(
31
+ self.nc.get_clear_errors_command(),
32
+ [self.nc.commands["clear_errors_command"]],
33
+ )
34
+ self.assertEqual(self.nc.commands["clear_errors_command"], 0x1A)
35
+
36
+ def test_get_calibration_command(self):
37
+ """Verifies the calibration command is a single-element list."""
38
+ self.assertEqual(self.nc.get_calibration_command(), [self.nc.commands["calibrate_command"]])
39
+
40
+ def test_get_firmware_command(self):
41
+ """Verifies the firmware command is packed as [firmware_update_command, value]."""
42
+ self.assertEqual(
43
+ self.nc.get_firmware_command(2),
44
+ [self.nc.commands["firmware_update_command"], 2],
45
+ )
46
+
47
+ def test_get_reset_command(self):
48
+ """Verifies the reset command is packed as [reset_command, joint_count]."""
49
+ self.assertEqual(self.nc.get_reset_command(4), [self.nc.commands["reset_command"], 4])
50
+
51
+ def test_get_target_position_single_joint(self):
52
+ """Verifies a single joint's target angle is packed into the high byte of one register."""
53
+ j = SimpleNamespace(target_angle=45)
54
+ hand = {"thumb_spread": j}
55
+ out = self.nc.get_target_position_command(hand)
56
+ self.assertEqual(out[0], self.nc.modbus_reg_map["target_position_start_reg"])
57
+ self.assertEqual(len(out), 2)
58
+ self.assertEqual(out[1], (45 << 8) | 0)
59
+
60
+ def test_get_target_position_two_joints(self):
61
+ """Verifies two joints' target angles are packed into the high and low bytes of one register."""
62
+ a = SimpleNamespace(target_angle=30)
63
+ b = SimpleNamespace(target_angle=60)
64
+ hand = {"j0": a, "j1": b}
65
+ out = self.nc.get_target_position_command(hand)
66
+ self.assertEqual(out[1], (30 << 8) | 60)
67
+
68
+ def test_decode_slave_id_reg(self):
69
+ """Verifies the slave ID register is decoded as its low byte."""
70
+ raw = self.nc.get_decoded_feedback_data(0x00AB, modbus_key="slave_id_reg")
71
+ self.assertEqual(raw, [0xAB & 0xFF])
72
+
73
+ def test_decode_feedback_register_int(self):
74
+ """Verifies a generic feedback register is decoded as a signed 16-bit int."""
75
+ raw = self.nc.get_decoded_feedback_data(0xFF09, modbus_key="feedback_register")
76
+ self.assertEqual(raw, [((0xFF09 + 2**15) % 2**16 - 2**15)])
77
+
78
+ def test_decode_signed_16b_list(self):
79
+ """Verifies a list of feedback registers is decoded element-wise as signed 16-bit ints."""
80
+ data = [0x0001, 0xFFFF]
81
+ raw = self.nc.get_decoded_feedback_data(data, modbus_key="feedback_velocity_start_reg")
82
+ self.assertEqual(raw[0], 1)
83
+ self.assertEqual(raw[1], -1)
84
+
85
+ def test_decode_float_feedback(self):
86
+ """Verifies a two-register pair is decoded as an IEEE 754 float."""
87
+ f = 1.25
88
+ b = struct.pack("<f", f)
89
+ w0 = struct.unpack("<H", b[0:2])[0]
90
+ w1 = struct.unpack("<H", b[2:4])[0]
91
+ out = self.nc.get_decoded_feedback_data([w0, w1], modbus_key="feedback_force_start_reg")
92
+ self.assertEqual(len(out), 1)
93
+ self.assertAlmostEqual(out[0], 1.25, places=5)
94
+
95
+ def test_decode_position_8b_packed(self):
96
+ """Verifies packed position registers are decoded into one value per joint (two joints per register)."""
97
+ nc4 = NewCommands(num_joints=4, logger=self.nc.logger)
98
+ regs = [(5 << 8) | 6, (7 << 8) | 8]
99
+ out = nc4.get_decoded_feedback_data(regs, modbus_key="feedback_position_start_reg")
100
+ self.assertEqual(len(out), 4)
101
+ self.assertEqual(out, [5, 6, 7, 8])
102
+
103
+ def test_get_target_velocity_command(self):
104
+ """Verifies the target velocity command starts at the correct register and includes the value."""
105
+ j = SimpleNamespace(target_velocity=100)
106
+ hand = {"thumb_spread": j}
107
+ out = self.nc.get_target_velocity_command(hand)
108
+ self.assertEqual(out[0], self.nc.modbus_reg_map["target_velocity_start_reg"])
109
+ self.assertIn(100, out)
110
+
111
+ def test_get_target_force_command_nonzero(self):
112
+ """Verifies the target force command starts at the correct register and packs a float payload."""
113
+ j = SimpleNamespace(target_force=2.5)
114
+ hand = {"thumb_spread": j}
115
+ out = self.nc.get_target_force_command(hand)
116
+ self.assertEqual(out[0], self.nc.modbus_reg_map["target_force_start_reg"])
117
+ self.assertGreater(len(out), 2)
118
+
119
+ def test_fc17_feedback_word_counts(self):
120
+ """Verifies register counts used by FC 0x17 set_get pairings for 16 joints."""
121
+ # position/temp: 0.5 words/joint -> 8; velocity: 1 -> 16; force: 2 -> 32
122
+ m = self.nc.data_type_multiplier_map
123
+ n = self.nc.num_joints
124
+ import math
125
+ self.assertEqual(math.ceil(m["feedback_position_start_reg"] * n), 8)
126
+ self.assertEqual(math.ceil(m["feedback_velocity_start_reg"] * n), 16)
127
+ self.assertEqual(math.ceil(m["feedback_force_start_reg"] * n), 32)
128
+
129
+ def test_update_config_command(self):
130
+ """Verifies the update-config command payload and its 0x44 opcode."""
131
+ self.assertEqual(
132
+ self.nc.update_config_command(1),
133
+ [self.nc.commands["update_config_command"], 1],
134
+ )
135
+ self.assertEqual(self.nc.commands["update_config_command"], 0x44)
136
+
137
+ def test_update_config_len_command(self):
138
+ """Verifies the update-config-length command prefixes the packed registers with a string length."""
139
+ # "AB" -> one packed register 0x4142
140
+ regs = [0x4142]
141
+ out = self.nc.update_config_len_command(regs, "AB")
142
+ self.assertEqual(out, [2, 0x4142])
143
+
144
+
145
+ if __name__ == "__main__":
146
+ unittest.main()