lerobot/tests/motors/test_dynamixel.py

435 lines
13 KiB
Python
Raw Normal View History

2025-03-20 16:38:36 +08:00
import sys
2025-03-23 01:16:13 +08:00
from typing import Generator
2025-03-16 04:45:05 +08:00
from unittest.mock import patch
2025-03-20 02:02:25 +08:00
import dynamixel_sdk as dxl
2025-03-16 04:45:05 +08:00
import pytest
2025-03-23 17:20:08 +08:00
from lerobot.common.motors import CalibrationMode, Motor
2025-03-25 03:42:43 +08:00
from lerobot.common.motors.dynamixel import MODEL_NUMBER, DynamixelMotorsBus
2025-03-20 16:40:58 +08:00
from tests.mocks.mock_dynamixel import MockMotors, MockPortHandler
2025-03-16 04:45:05 +08:00
@pytest.fixture(autouse=True)
2025-03-20 02:02:25 +08:00
def patch_port_handler():
2025-03-20 16:38:36 +08:00
if sys.platform == "darwin":
with patch.object(dxl, "PortHandler", MockPortHandler):
yield
else:
yield
2025-03-16 04:45:05 +08:00
2025-03-23 01:16:13 +08:00
@pytest.fixture
def mock_motors() -> Generator[MockMotors, None, None]:
motors = MockMotors()
motors.open()
yield motors
motors.close()
2025-03-21 19:13:44 +08:00
@pytest.fixture
def dummy_motors() -> dict[str, Motor]:
return {
2025-03-23 17:20:08 +08:00
"dummy_1": Motor(1, "xl430-w250", CalibrationMode.RANGE_M100_100),
"dummy_2": Motor(2, "xm540-w270", CalibrationMode.RANGE_M100_100),
"dummy_3": Motor(3, "xl330-m077", CalibrationMode.RANGE_M100_100),
2025-03-21 19:13:44 +08:00
}
2025-03-20 16:38:36 +08:00
@pytest.mark.skipif(sys.platform != "darwin", reason=f"No patching needed on {sys.platform=}")
2025-03-20 02:02:25 +08:00
def test_autouse_patch():
2025-03-20 16:38:36 +08:00
"""Ensures that the autouse fixture correctly patches dxl.PortHandler with MockPortHandler."""
2025-03-20 02:02:25 +08:00
assert dxl.PortHandler is MockPortHandler
@pytest.mark.parametrize(
"value, n_bytes, expected",
[
2025-03-20 16:44:47 +08:00
(0x12, 1, [0x12]),
(0x1234, 2, [0x34, 0x12]),
(0x12345678, 4, [0x78, 0x56, 0x34, 0x12]),
(0, 1, [0x00]),
(0, 2, [0x00, 0x00]),
(0, 4, [0x00, 0x00, 0x00, 0x00]),
(255, 1, [0xFF]),
(65535, 2, [0xFF, 0xFF]),
(4294967295, 4, [0xFF, 0xFF, 0xFF, 0xFF]),
],
ids=[
"1 byte",
"2 bytes",
"4 bytes",
"0 with 1 byte",
"0 with 2 bytes",
"0 with 4 bytes",
"max single byte",
"max two bytes",
"max four bytes",
2025-03-20 02:02:25 +08:00
],
) # fmt: skip
2025-03-24 18:57:12 +08:00
def test_split_int_to_bytes(value, n_bytes, expected):
assert DynamixelMotorsBus._split_int_to_bytes(value, n_bytes) == expected
2025-03-20 02:02:25 +08:00
2025-03-24 18:57:12 +08:00
def test_split_int_to_bytes_invalid_n_bytes():
2025-03-20 02:02:25 +08:00
with pytest.raises(NotImplementedError):
2025-03-24 18:57:12 +08:00
DynamixelMotorsBus._split_int_to_bytes(100, 3)
2025-03-20 02:02:25 +08:00
2025-03-24 18:57:12 +08:00
def test_split_int_to_bytes_negative_numbers():
2025-03-20 02:02:25 +08:00
with pytest.raises(ValueError):
2025-03-24 18:57:12 +08:00
neg = DynamixelMotorsBus._split_int_to_bytes(-1, 1)
2025-03-20 02:02:25 +08:00
print(neg)
2025-03-24 18:57:12 +08:00
def test_split_int_to_bytes_large_number():
2025-03-20 02:02:25 +08:00
with pytest.raises(ValueError):
2025-03-24 18:57:12 +08:00
DynamixelMotorsBus._split_int_to_bytes(2**32, 4) # 4-byte max is 0xFFFFFFFF
2025-03-16 04:45:05 +08:00
2025-03-21 19:13:44 +08:00
def test_abc_implementation(dummy_motors):
2025-03-20 16:44:47 +08:00
"""Instantiation should raise an error if the class doesn't implement abstract methods/properties."""
2025-03-21 19:13:44 +08:00
DynamixelMotorsBus(port="/dev/dummy-port", motors=dummy_motors)
2025-03-22 07:39:41 +08:00
2025-03-22 07:40:22 +08:00
2025-03-25 03:42:43 +08:00
@pytest.mark.parametrize("idx", [1, 2, 3])
def test_ping(idx, mock_motors, dummy_motors):
2025-03-25 18:12:52 +08:00
expected_model_nb = MODEL_NUMBER[dummy_motors[f"dummy_{idx}"].model]
stub_name = mock_motors.build_ping_stub(idx, expected_model_nb)
2025-03-22 07:40:22 +08:00
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-22 07:40:22 +08:00
ping_model_nb = motors_bus.ping(idx)
2025-03-25 18:12:52 +08:00
assert ping_model_nb == expected_model_nb
2025-03-23 01:53:57 +08:00
assert mock_motors.stubs[stub_name].called
2025-03-22 07:40:22 +08:00
2025-03-23 01:16:13 +08:00
def test_broadcast_ping(mock_motors, dummy_motors):
2025-03-25 18:12:52 +08:00
models = {m.id: m.model for m in dummy_motors.values()}
expected_model_nbs = {id_: MODEL_NUMBER[model] for id_, model in models.items()}
stub_name = mock_motors.build_broadcast_ping_stub(expected_model_nbs)
2025-03-22 07:40:22 +08:00
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-22 07:40:22 +08:00
2025-03-25 03:42:43 +08:00
ping_model_nbs = motors_bus.broadcast_ping()
2025-03-22 07:40:22 +08:00
2025-03-25 18:12:52 +08:00
assert ping_model_nbs == expected_model_nbs
2025-03-23 01:53:57 +08:00
assert mock_motors.stubs[stub_name].called
2025-03-16 05:19:50 +08:00
2025-03-23 23:11:24 +08:00
def test_sync_read_none(mock_motors, dummy_motors):
expected_positions = {
"dummy_1": 1337,
"dummy_2": 42,
"dummy_3": 4016,
}
ids_values = dict(zip([1, 2, 3], expected_positions.values(), strict=True))
stub_name = mock_motors.build_sync_read_stub("Present_Position", ids_values)
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-23 23:11:24 +08:00
2025-03-24 00:05:08 +08:00
read_positions = motors_bus.sync_read("Present_Position")
2025-03-23 23:11:24 +08:00
assert mock_motors.stubs[stub_name].called
2025-03-24 00:05:08 +08:00
assert read_positions == expected_positions
2025-03-23 23:11:24 +08:00
2025-03-20 02:02:25 +08:00
@pytest.mark.parametrize(
2025-03-24 00:05:08 +08:00
"id_, position",
2025-03-20 02:02:25 +08:00
[
2025-03-23 23:11:24 +08:00
(1, 1337),
(2, 42),
(3, 4016),
2025-03-20 02:02:25 +08:00
],
)
2025-03-24 00:05:08 +08:00
def test_sync_read_by_id(id_, position, mock_motors, dummy_motors):
expected_position = {id_: position}
2025-03-23 23:11:24 +08:00
stub_name = mock_motors.build_sync_read_stub("Present_Position", expected_position)
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-23 23:11:24 +08:00
2025-03-24 00:05:08 +08:00
read_position = motors_bus.sync_read("Present_Position", id_)
2025-03-23 23:11:24 +08:00
assert mock_motors.stubs[stub_name].called
2025-03-24 00:05:08 +08:00
assert read_position == expected_position
2025-03-23 23:11:24 +08:00
@pytest.mark.parametrize(
"ids, positions",
[
([1], [1337]),
([1, 2], [1337, 42]),
([1, 2, 3], [1337, 42, 4016]),
],
ids=["1 motor", "2 motors", "3 motors"],
) # fmt: skip
def test_sync_read_by_ids(ids, positions, mock_motors, dummy_motors):
assert len(ids) == len(positions)
expected_positions = dict(zip(ids, positions, strict=True))
2025-03-22 07:39:41 +08:00
stub_name = mock_motors.build_sync_read_stub("Present_Position", expected_positions)
2025-03-20 02:02:25 +08:00
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
2025-03-21 19:13:44 +08:00
motors=dummy_motors,
2025-03-20 02:02:25 +08:00
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-20 02:02:25 +08:00
2025-03-24 00:05:08 +08:00
read_positions = motors_bus.sync_read("Present_Position", ids)
2025-03-20 02:02:25 +08:00
2025-03-22 07:39:41 +08:00
assert mock_motors.stubs[stub_name].called
2025-03-24 00:05:08 +08:00
assert read_positions == expected_positions
2025-03-16 05:19:50 +08:00
2025-03-20 02:02:25 +08:00
2025-03-20 16:40:58 +08:00
@pytest.mark.parametrize(
2025-03-23 23:11:24 +08:00
"id_, position",
2025-03-20 16:40:58 +08:00
[
2025-03-23 23:11:24 +08:00
(1, 1337),
(2, 42),
(3, 4016),
2025-03-20 16:40:58 +08:00
],
)
2025-03-23 23:11:24 +08:00
def test_sync_read_by_name(id_, position, mock_motors, dummy_motors):
expected_position = {f"dummy_{id_}": position}
stub_name = mock_motors.build_sync_read_stub("Present_Position", {id_: position})
2025-03-20 02:02:25 +08:00
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
2025-03-21 19:13:44 +08:00
motors=dummy_motors,
2025-03-20 02:02:25 +08:00
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-16 05:19:50 +08:00
2025-03-24 00:05:08 +08:00
read_position = motors_bus.sync_read("Present_Position", f"dummy_{id_}")
2025-03-20 02:02:25 +08:00
2025-03-22 07:39:41 +08:00
assert mock_motors.stubs[stub_name].called
2025-03-24 00:05:08 +08:00
assert read_position == expected_position
2025-03-20 02:02:25 +08:00
2025-03-16 05:19:50 +08:00
2025-03-20 16:40:58 +08:00
@pytest.mark.parametrize(
2025-03-23 23:11:24 +08:00
"ids, positions",
2025-03-20 16:40:58 +08:00
[
2025-03-23 23:11:24 +08:00
([1], [1337]),
([1, 2], [1337, 42]),
([1, 2, 3], [1337, 42, 4016]),
2025-03-20 16:40:58 +08:00
],
2025-03-23 23:11:24 +08:00
ids=["1 motor", "2 motors", "3 motors"],
) # fmt: skip
def test_sync_read_by_names(ids, positions, mock_motors, dummy_motors):
assert len(ids) == len(positions)
names = [f"dummy_{dxl_id}" for dxl_id in ids]
expected_positions = dict(zip(names, positions, strict=True))
ids_values = dict(zip(ids, positions, strict=True))
stub_name = mock_motors.build_sync_read_stub("Present_Position", ids_values)
2025-03-20 02:02:25 +08:00
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
2025-03-21 19:13:44 +08:00
motors=dummy_motors,
2025-03-20 02:02:25 +08:00
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-20 02:02:25 +08:00
2025-03-24 00:05:08 +08:00
read_positions = motors_bus.sync_read("Present_Position", names)
2025-03-20 02:02:25 +08:00
2025-03-22 07:39:41 +08:00
assert mock_motors.stubs[stub_name].called
2025-03-24 00:05:08 +08:00
assert read_positions == expected_positions
2025-03-20 02:02:25 +08:00
@pytest.mark.parametrize(
2025-03-20 16:40:58 +08:00
"num_retry, num_invalid_try, pos",
2025-03-20 02:02:25 +08:00
[
2025-03-23 23:11:24 +08:00
(0, 2, 1337),
(2, 3, 42),
(3, 2, 4016),
(2, 1, 999),
2025-03-20 02:02:25 +08:00
],
)
2025-03-23 20:32:08 +08:00
def test_sync_read_num_retry(num_retry, num_invalid_try, pos, mock_motors, dummy_motors):
2025-03-22 07:39:41 +08:00
expected_position = {1: pos}
stub_name = mock_motors.build_sync_read_stub(
"Present_Position", expected_position, num_invalid_try=num_invalid_try
2025-03-20 02:02:25 +08:00
)
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
2025-03-21 19:13:44 +08:00
motors=dummy_motors,
2025-03-20 02:02:25 +08:00
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-20 02:02:25 +08:00
if num_retry >= num_invalid_try:
pos_dict = motors_bus.sync_read("Present_Position", 1, num_retry=num_retry)
2025-03-22 07:39:41 +08:00
assert pos_dict == {1: pos}
2025-03-20 02:02:25 +08:00
else:
with pytest.raises(ConnectionError):
_ = motors_bus.sync_read("Present_Position", 1, num_retry=num_retry)
2025-03-20 02:02:25 +08:00
2025-03-22 07:39:41 +08:00
expected_calls = min(1 + num_retry, 1 + num_invalid_try)
assert mock_motors.stubs[stub_name].calls == expected_calls
2025-03-22 21:50:05 +08:00
@pytest.mark.parametrize(
2025-03-23 23:11:24 +08:00
"data_name, value",
2025-03-22 21:50:05 +08:00
[
2025-03-23 23:11:24 +08:00
("Torque_Enable", 0),
("Torque_Enable", 1),
("Goal_Position", 1337),
("Goal_Position", 42),
2025-03-22 21:50:05 +08:00
],
)
2025-03-23 23:11:24 +08:00
def test_sync_write_single_value(data_name, value, mock_motors, dummy_motors):
ids_values = {m.id: value for m in dummy_motors.values()}
stub_name = mock_motors.build_sync_write_stub(data_name, ids_values)
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-23 23:11:24 +08:00
motors_bus.sync_write(data_name, value)
assert mock_motors.stubs[stub_name].wait_called()
@pytest.mark.parametrize(
"id_, position",
[
(1, 1337),
(2, 42),
(3, 4016),
],
)
def test_sync_write_by_id(id_, position, mock_motors, dummy_motors):
value = {id_: position}
stub_name = mock_motors.build_sync_write_stub("Goal_Position", value)
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-23 23:11:24 +08:00
motors_bus.sync_write("Goal_Position", value)
assert mock_motors.stubs[stub_name].wait_called()
@pytest.mark.parametrize(
"ids, positions",
[
([1], [1337]),
([1, 2], [1337, 42]),
([1, 2, 3], [1337, 42, 4016]),
],
ids=["1 motor", "2 motors", "3 motors"],
) # fmt: skip
def test_sync_write_by_ids(ids, positions, mock_motors, dummy_motors):
assert len(ids) == len(positions)
values = dict(zip(ids, positions, strict=True))
stub_name = mock_motors.build_sync_write_stub("Goal_Position", values)
2025-03-22 21:50:05 +08:00
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-22 21:50:05 +08:00
motors_bus.sync_write("Goal_Position", values)
2025-03-22 21:50:05 +08:00
assert mock_motors.stubs[stub_name].wait_called()
@pytest.mark.parametrize(
2025-03-23 23:11:24 +08:00
"id_, position",
2025-03-22 21:50:05 +08:00
[
2025-03-23 23:11:24 +08:00
(1, 1337),
(2, 42),
(3, 4016),
2025-03-22 21:50:05 +08:00
],
)
2025-03-23 23:11:24 +08:00
def test_sync_write_by_name(id_, position, mock_motors, dummy_motors):
id_value = {id_: position}
stub_name = mock_motors.build_sync_write_stub("Goal_Position", id_value)
2025-03-22 21:50:05 +08:00
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-22 21:50:05 +08:00
2025-03-23 23:11:24 +08:00
write_value = {f"dummy_{id_}": position}
motors_bus.sync_write("Goal_Position", write_value)
assert mock_motors.stubs[stub_name].wait_called()
@pytest.mark.parametrize(
"ids, positions",
[
([1], [1337]),
([1, 2], [1337, 42]),
([1, 2, 3], [1337, 42, 4016]),
],
ids=["1 motor", "2 motors", "3 motors"],
) # fmt: skip
def test_sync_write_by_names(ids, positions, mock_motors, dummy_motors):
assert len(ids) == len(positions)
ids_values = dict(zip(ids, positions, strict=True))
stub_name = mock_motors.build_sync_write_stub("Goal_Position", ids_values)
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-23 23:11:24 +08:00
write_values = {f"dummy_{id_}": pos for id_, pos in ids_values.items()}
motors_bus.sync_write("Goal_Position", write_values)
2025-03-22 21:50:05 +08:00
assert mock_motors.stubs[stub_name].wait_called()
2025-03-23 23:12:24 +08:00
@pytest.mark.parametrize(
"data_name, dxl_id, value",
[
("Torque_Enable", 1, 0),
("Torque_Enable", 1, 1),
("Goal_Position", 2, 1337),
("Goal_Position", 3, 42),
],
)
def test_write_by_id(data_name, dxl_id, value, mock_motors, dummy_motors):
stub_name = mock_motors.build_write_stub(data_name, dxl_id, value)
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-23 23:12:24 +08:00
motors_bus.write(data_name, dxl_id, value)
assert mock_motors.stubs[stub_name].called
@pytest.mark.parametrize(
"data_name, dxl_id, value",
[
("Torque_Enable", 1, 0),
("Torque_Enable", 1, 1),
("Goal_Position", 2, 1337),
("Goal_Position", 3, 42),
],
)
def test_write_by_name(data_name, dxl_id, value, mock_motors, dummy_motors):
stub_name = mock_motors.build_write_stub(data_name, dxl_id, value)
motors_bus = DynamixelMotorsBus(
port=mock_motors.port,
motors=dummy_motors,
)
2025-03-25 18:12:52 +08:00
motors_bus.connect(assert_motors_exist=False)
2025-03-23 23:12:24 +08:00
motors_bus.write(data_name, f"dummy_{dxl_id}", value)
assert mock_motors.stubs[stub_name].called