2025-04-14 17:56:53 +08:00
|
|
|
import re
|
2025-03-16 04:45:05 +08:00
|
|
|
import sys
|
2025-03-23 01:16:13 +08:00
|
|
|
from typing import Generator
|
2025-04-03 18:14:15 +08:00
|
|
|
from unittest.mock import MagicMock, patch
|
2025-03-16 04:45:05 +08:00
|
|
|
|
|
|
|
import pytest
|
2025-03-20 21:00:10 +08:00
|
|
|
import scservo_sdk as scs
|
2025-03-16 04:45:05 +08:00
|
|
|
|
2025-04-03 23:35:10 +08:00
|
|
|
from lerobot.common.motors import Motor, MotorCalibration, MotorNormMode
|
2025-04-14 17:56:53 +08:00
|
|
|
from lerobot.common.motors.feetech import MODEL_NUMBER, MODEL_NUMBER_TABLE, FeetechMotorsBus
|
|
|
|
from lerobot.common.motors.feetech.tables import STS_SMS_SERIES_CONTROL_TABLE
|
2025-04-03 18:14:15 +08:00
|
|
|
from lerobot.common.utils.encoding_utils import encode_sign_magnitude
|
2025-03-20 21:00:10 +08:00
|
|
|
from tests.mocks.mock_feetech import MockMotors, MockPortHandler
|
2025-03-16 04:45:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2025-03-20 21:00:10 +08:00
|
|
|
def patch_port_handler():
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
with patch.object(scs, "PortHandler", MockPortHandler):
|
|
|
|
yield
|
|
|
|
else:
|
2025-03-16 04:45:05 +08:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
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-22 08:19:51 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def dummy_motors() -> dict[str, Motor]:
|
|
|
|
return {
|
2025-03-26 00:42:18 +08:00
|
|
|
"dummy_1": Motor(1, "sts3215", MotorNormMode.RANGE_M100_100),
|
|
|
|
"dummy_2": Motor(2, "sts3215", MotorNormMode.RANGE_M100_100),
|
|
|
|
"dummy_3": Motor(3, "sts3215", MotorNormMode.RANGE_M100_100),
|
2025-03-22 08:19:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-04-03 23:35:10 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def dummy_calibration(dummy_motors) -> dict[str, MotorCalibration]:
|
|
|
|
homings = [-709, -2006, 1624]
|
|
|
|
mins = [43, 27, 145]
|
|
|
|
maxes = [1335, 3608, 3999]
|
|
|
|
calibration = {}
|
|
|
|
for name, motor in dummy_motors.items():
|
|
|
|
calibration[name] = MotorCalibration(
|
|
|
|
id=motor.id,
|
|
|
|
drive_mode=0,
|
|
|
|
homing_offset=homings[motor.id - 1],
|
|
|
|
range_min=mins[motor.id - 1],
|
|
|
|
range_max=maxes[motor.id - 1],
|
|
|
|
)
|
|
|
|
return calibration
|
|
|
|
|
|
|
|
|
2025-03-20 21:00:10 +08:00
|
|
|
@pytest.mark.skipif(sys.platform != "darwin", reason=f"No patching needed on {sys.platform=}")
|
|
|
|
def test_autouse_patch():
|
|
|
|
"""Ensures that the autouse fixture correctly patches scs.PortHandler with MockPortHandler."""
|
|
|
|
assert scs.PortHandler is MockPortHandler
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2025-04-11 17:58:09 +08:00
|
|
|
"protocol, value, length, expected",
|
2025-03-20 21:00:10 +08:00
|
|
|
[
|
2025-04-11 17:58:09 +08:00
|
|
|
(0, 0x12, 1, [0x12]),
|
|
|
|
(1, 0x12, 1, [0x12]),
|
|
|
|
(0, 0x1234, 2, [0x34, 0x12]),
|
|
|
|
(1, 0x1234, 2, [0x12, 0x34]),
|
|
|
|
(0, 0x12345678, 4, [0x78, 0x56, 0x34, 0x12]),
|
|
|
|
(1, 0x12345678, 4, [0x56, 0x78, 0x12, 0x34]),
|
2025-03-20 21:00:10 +08:00
|
|
|
],
|
|
|
|
ids=[
|
2025-04-11 17:58:09 +08:00
|
|
|
"P0: 1 byte",
|
|
|
|
"P1: 1 byte",
|
|
|
|
"P0: 2 bytes",
|
|
|
|
"P1: 2 bytes",
|
|
|
|
"P0: 4 bytes",
|
|
|
|
"P1: 4 bytes",
|
2025-03-20 21:00:10 +08:00
|
|
|
],
|
|
|
|
) # fmt: skip
|
2025-04-11 17:58:09 +08:00
|
|
|
def test__split_into_byte_chunks(protocol, value, length, expected):
|
|
|
|
bus = FeetechMotorsBus("", {}, protocol_version=protocol)
|
|
|
|
assert bus._split_into_byte_chunks(value, length) == expected
|
2025-03-16 04:45:05 +08:00
|
|
|
|
|
|
|
|
2025-03-22 08:19:51 +08:00
|
|
|
def test_abc_implementation(dummy_motors):
|
2025-03-20 21:00:10 +08:00
|
|
|
"""Instantiation should raise an error if the class doesn't implement abstract methods/properties."""
|
2025-03-22 08:19:51 +08:00
|
|
|
FeetechMotorsBus(port="/dev/dummy-port", motors=dummy_motors)
|
|
|
|
|
|
|
|
|
2025-03-25 21:20:12 +08:00
|
|
|
@pytest.mark.parametrize("id_", [1, 2, 3])
|
|
|
|
def test_ping(id_, mock_motors, dummy_motors):
|
2025-04-08 16:46:29 +08:00
|
|
|
expected_model_nb = MODEL_NUMBER_TABLE[dummy_motors[f"dummy_{id_}"].model]
|
2025-04-14 17:56:53 +08:00
|
|
|
addr, length = MODEL_NUMBER
|
2025-03-25 21:20:12 +08:00
|
|
|
ping_stub = mock_motors.build_ping_stub(id_)
|
2025-04-14 17:56:53 +08:00
|
|
|
mobel_nb_stub = mock_motors.build_read_stub(addr, length, id_, expected_model_nb)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus = FeetechMotorsBus(
|
2025-03-22 08:19:51 +08:00
|
|
|
port=mock_motors.port,
|
|
|
|
motors=dummy_motors,
|
|
|
|
)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-03-22 08:19:51 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
ping_model_nb = bus.ping(id_)
|
2025-03-22 08:19:51 +08:00
|
|
|
|
2025-03-25 18:12:52 +08:00
|
|
|
assert ping_model_nb == expected_model_nb
|
2025-03-25 14:26:34 +08:00
|
|
|
assert mock_motors.stubs[ping_stub].called
|
|
|
|
assert mock_motors.stubs[mobel_nb_stub].called
|
2025-03-22 08:19:51 +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()}
|
2025-04-14 17:56:53 +08:00
|
|
|
addr, length = MODEL_NUMBER
|
2025-03-25 18:12:52 +08:00
|
|
|
ping_stub = mock_motors.build_broadcast_ping_stub(list(models))
|
2025-04-14 17:56:53 +08:00
|
|
|
mobel_nb_stubs = []
|
|
|
|
expected_model_nbs = {}
|
|
|
|
for id_, model in models.items():
|
|
|
|
model_nb = MODEL_NUMBER_TABLE[model]
|
|
|
|
stub = mock_motors.build_read_stub(addr, length, id_, model_nb)
|
|
|
|
expected_model_nbs[id_] = model_nb
|
|
|
|
mobel_nb_stubs.append(stub)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus = FeetechMotorsBus(
|
2025-03-22 08:19:51 +08:00
|
|
|
port=mock_motors.port,
|
|
|
|
motors=dummy_motors,
|
|
|
|
)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-03-22 08:19:51 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
ping_model_nbs = bus.broadcast_ping()
|
2025-03-22 08:19:51 +08:00
|
|
|
|
2025-03-25 18:12:52 +08:00
|
|
|
assert ping_model_nbs == expected_model_nbs
|
2025-03-25 14:26:34 +08:00
|
|
|
assert mock_motors.stubs[ping_stub].called
|
2025-04-14 17:56:53 +08:00
|
|
|
assert all(mock_motors.stubs[stub].called for stub in mobel_nb_stubs)
|
2025-03-16 05:19:50 +08:00
|
|
|
|
|
|
|
|
2025-04-14 17:56:53 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"addr, length, id_, value",
|
|
|
|
[
|
|
|
|
(0, 1, 1, 2),
|
|
|
|
(10, 2, 2, 999),
|
|
|
|
(42, 4, 3, 1337),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test__read(addr, length, id_, value, mock_motors, dummy_motors):
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_read_stub(addr, length, id_, value)
|
|
|
|
bus = FeetechMotorsBus(
|
2025-03-23 23:25:12 +08:00
|
|
|
port=mock_motors.port,
|
|
|
|
motors=dummy_motors,
|
|
|
|
)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-03-23 23:25:12 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
read_value, _, _ = bus._read(addr, length, id_)
|
2025-03-23 23:25:12 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].called
|
2025-04-14 17:56:53 +08:00
|
|
|
assert read_value == value
|
2025-03-23 23:25:12 +08:00
|
|
|
|
|
|
|
|
2025-04-14 17:56:53 +08:00
|
|
|
@pytest.mark.parametrize("raise_on_error", (True, False))
|
|
|
|
def test__read_error(raise_on_error, mock_motors, dummy_motors):
|
|
|
|
addr, length, id_, value, error = (10, 4, 1, 1337, scs.ERRBIT_VOLTAGE)
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_read_stub(addr, length, id_, value, error=error)
|
|
|
|
bus = FeetechMotorsBus(
|
2025-03-20 21:00:10 +08:00
|
|
|
port=mock_motors.port,
|
2025-03-22 08:19:51 +08:00
|
|
|
motors=dummy_motors,
|
2025-03-20 21:00:10 +08:00
|
|
|
)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-03-20 21:00:10 +08:00
|
|
|
|
2025-04-14 17:56:53 +08:00
|
|
|
if raise_on_error:
|
|
|
|
with pytest.raises(RuntimeError, match=re.escape("[RxPacketError] Input voltage error!")):
|
2025-04-14 21:26:29 +08:00
|
|
|
bus._read(addr, length, id_, raise_on_error=raise_on_error)
|
2025-04-14 17:56:53 +08:00
|
|
|
else:
|
2025-04-14 21:26:29 +08:00
|
|
|
_, _, read_error = bus._read(addr, length, id_, raise_on_error=raise_on_error)
|
2025-04-14 17:56:53 +08:00
|
|
|
assert read_error == error
|
2025-03-20 21:00:10 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].called
|
2025-03-20 21:00:10 +08:00
|
|
|
|
|
|
|
|
2025-04-14 17:56:53 +08:00
|
|
|
@pytest.mark.parametrize("raise_on_error", (True, False))
|
|
|
|
def test__read_comm(raise_on_error, mock_motors, dummy_motors):
|
|
|
|
addr, length, id_, value = (10, 4, 1, 1337)
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_read_stub(addr, length, id_, value, reply=False)
|
|
|
|
bus = FeetechMotorsBus(
|
2025-03-20 21:00:10 +08:00
|
|
|
port=mock_motors.port,
|
2025-03-22 08:19:51 +08:00
|
|
|
motors=dummy_motors,
|
2025-03-20 21:00:10 +08:00
|
|
|
)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-03-16 05:19:50 +08:00
|
|
|
|
2025-04-14 17:56:53 +08:00
|
|
|
if raise_on_error:
|
|
|
|
with pytest.raises(ConnectionError, match=re.escape("[TxRxResult] There is no status packet!")):
|
2025-04-14 21:26:29 +08:00
|
|
|
bus._read(addr, length, id_, raise_on_error=raise_on_error)
|
2025-04-14 17:56:53 +08:00
|
|
|
else:
|
2025-04-14 21:26:29 +08:00
|
|
|
_, read_comm, _ = bus._read(addr, length, id_, raise_on_error=raise_on_error)
|
2025-04-14 17:56:53 +08:00
|
|
|
assert read_comm == scs.COMM_RX_TIMEOUT
|
2025-03-20 21:00:10 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].called
|
2025-03-20 21:00:10 +08:00
|
|
|
|
2025-03-16 05:19:50 +08:00
|
|
|
|
2025-03-20 21:00:10 +08:00
|
|
|
@pytest.mark.parametrize(
|
2025-04-14 17:56:53 +08:00
|
|
|
"addr, length, id_, value",
|
2025-03-20 21:00:10 +08:00
|
|
|
[
|
2025-04-14 17:56:53 +08:00
|
|
|
(0, 1, 1, 2),
|
|
|
|
(10, 2, 2, 999),
|
|
|
|
(42, 4, 3, 1337),
|
2025-03-20 21:00:10 +08:00
|
|
|
],
|
|
|
|
)
|
2025-04-14 17:56:53 +08:00
|
|
|
def test__write(addr, length, id_, value, mock_motors, dummy_motors):
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_write_stub(addr, length, id_, value)
|
|
|
|
bus = FeetechMotorsBus(
|
2025-04-14 17:56:53 +08:00
|
|
|
port=mock_motors.port,
|
|
|
|
motors=dummy_motors,
|
2025-03-20 21:00:10 +08:00
|
|
|
)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-04-14 17:56:53 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
comm, error = bus._write(addr, length, id_, value)
|
2025-04-14 17:56:53 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].called
|
2025-04-14 17:56:53 +08:00
|
|
|
assert comm == scs.COMM_SUCCESS
|
|
|
|
assert error == 0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("raise_on_error", (True, False))
|
|
|
|
def test__write_error(raise_on_error, mock_motors, dummy_motors):
|
|
|
|
addr, length, id_, value, error = (10, 4, 1, 1337, scs.ERRBIT_VOLTAGE)
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_write_stub(addr, length, id_, value, error=error)
|
|
|
|
bus = FeetechMotorsBus(port=mock_motors.port, motors=dummy_motors)
|
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-03-20 21:00:10 +08:00
|
|
|
|
2025-04-14 17:56:53 +08:00
|
|
|
if raise_on_error:
|
|
|
|
with pytest.raises(RuntimeError, match=re.escape("[RxPacketError] Input voltage error!")):
|
2025-04-14 21:26:29 +08:00
|
|
|
bus._write(addr, length, id_, value, raise_on_error=raise_on_error)
|
2025-03-20 21:00:10 +08:00
|
|
|
else:
|
2025-04-14 21:26:29 +08:00
|
|
|
_, write_error = bus._write(addr, length, id_, value, raise_on_error=raise_on_error)
|
2025-04-14 17:56:53 +08:00
|
|
|
assert write_error == error
|
2025-03-20 21:00:10 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].called
|
2025-03-23 00:02:01 +08:00
|
|
|
|
|
|
|
|
2025-04-14 17:56:53 +08:00
|
|
|
@pytest.mark.parametrize("raise_on_error", (True, False))
|
|
|
|
def test__write_comm(raise_on_error, mock_motors, dummy_motors):
|
|
|
|
addr, length, id_, value = (10, 4, 1, 1337)
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_write_stub(addr, length, id_, value, reply=False)
|
|
|
|
bus = FeetechMotorsBus(port=mock_motors.port, motors=dummy_motors)
|
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-03-23 23:25:12 +08:00
|
|
|
|
2025-04-14 17:56:53 +08:00
|
|
|
if raise_on_error:
|
|
|
|
with pytest.raises(ConnectionError, match=re.escape("[TxRxResult] There is no status packet!")):
|
2025-04-14 21:26:29 +08:00
|
|
|
bus._write(addr, length, id_, value, raise_on_error=raise_on_error)
|
2025-04-14 17:56:53 +08:00
|
|
|
else:
|
2025-04-14 21:26:29 +08:00
|
|
|
write_comm, _ = bus._write(addr, length, id_, value, raise_on_error=raise_on_error)
|
2025-04-14 17:56:53 +08:00
|
|
|
assert write_comm == scs.COMM_RX_TIMEOUT
|
2025-03-23 23:25:12 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].called
|
2025-03-23 23:25:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2025-04-14 17:56:53 +08:00
|
|
|
"addr, length, ids_values",
|
2025-03-23 23:25:12 +08:00
|
|
|
[
|
2025-04-14 17:56:53 +08:00
|
|
|
(0, 1, {1: 4}),
|
|
|
|
(10, 2, {1: 1337, 2: 42}),
|
|
|
|
(42, 4, {1: 1337, 2: 42, 3: 4016}),
|
2025-03-23 23:25:12 +08:00
|
|
|
],
|
|
|
|
ids=["1 motor", "2 motors", "3 motors"],
|
2025-04-14 17:56:53 +08:00
|
|
|
)
|
|
|
|
def test__sync_read(addr, length, ids_values, mock_motors, dummy_motors):
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_sync_read_stub(addr, length, ids_values)
|
|
|
|
bus = FeetechMotorsBus(port=mock_motors.port, motors=dummy_motors)
|
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-03-23 23:25:12 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
read_values, _ = bus._sync_read(addr, length, list(ids_values))
|
2025-03-23 00:02:01 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].called
|
2025-04-14 17:56:53 +08:00
|
|
|
assert read_values == ids_values
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("raise_on_error", (True, False))
|
|
|
|
def test__sync_read_comm(raise_on_error, mock_motors, dummy_motors):
|
|
|
|
addr, length, ids_values = (10, 4, {1: 1337})
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_sync_read_stub(addr, length, ids_values, reply=False)
|
|
|
|
bus = FeetechMotorsBus(port=mock_motors.port, motors=dummy_motors)
|
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-04-14 17:56:53 +08:00
|
|
|
|
|
|
|
if raise_on_error:
|
|
|
|
with pytest.raises(ConnectionError, match=re.escape("[TxRxResult] There is no status packet!")):
|
2025-04-14 21:26:29 +08:00
|
|
|
bus._sync_read(addr, length, list(ids_values), raise_on_error=raise_on_error)
|
2025-04-14 17:56:53 +08:00
|
|
|
else:
|
2025-04-14 21:26:29 +08:00
|
|
|
_, read_comm = bus._sync_read(addr, length, list(ids_values), raise_on_error=raise_on_error)
|
2025-04-14 17:56:53 +08:00
|
|
|
assert read_comm == scs.COMM_RX_TIMEOUT
|
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].called
|
2025-03-23 23:48:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2025-04-14 17:56:53 +08:00
|
|
|
"addr, length, ids_values",
|
2025-03-23 23:48:32 +08:00
|
|
|
[
|
2025-04-14 17:56:53 +08:00
|
|
|
(0, 1, {1: 4}),
|
|
|
|
(10, 2, {1: 1337, 2: 42}),
|
|
|
|
(42, 4, {1: 1337, 2: 42, 3: 4016}),
|
2025-03-23 23:48:32 +08:00
|
|
|
],
|
2025-04-14 17:56:53 +08:00
|
|
|
ids=["1 motor", "2 motors", "3 motors"],
|
2025-03-23 23:48:32 +08:00
|
|
|
)
|
2025-04-14 17:56:53 +08:00
|
|
|
def test__sync_write(addr, length, ids_values, mock_motors, dummy_motors):
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_sync_write_stub(addr, length, ids_values)
|
|
|
|
bus = FeetechMotorsBus(port=mock_motors.port, motors=dummy_motors)
|
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-03-23 23:48:32 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
comm = bus._sync_write(addr, length, ids_values)
|
2025-03-23 23:48:32 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].wait_called()
|
2025-04-14 17:56:53 +08:00
|
|
|
assert comm == scs.COMM_SUCCESS
|
2025-04-03 18:14:15 +08:00
|
|
|
|
|
|
|
|
2025-04-03 23:35:10 +08:00
|
|
|
def test_is_calibrated(mock_motors, dummy_motors, dummy_calibration):
|
|
|
|
encoded_homings = {m.id: encode_sign_magnitude(m.homing_offset, 11) for m in dummy_calibration.values()}
|
|
|
|
mins = {m.id: m.range_min for m in dummy_calibration.values()}
|
|
|
|
maxes = {m.id: m.range_max for m in dummy_calibration.values()}
|
2025-04-14 17:56:53 +08:00
|
|
|
offsets_stub = mock_motors.build_sync_read_stub(
|
|
|
|
*STS_SMS_SERIES_CONTROL_TABLE["Homing_Offset"], encoded_homings
|
|
|
|
)
|
|
|
|
mins_stub = mock_motors.build_sync_read_stub(*STS_SMS_SERIES_CONTROL_TABLE["Min_Position_Limit"], mins)
|
|
|
|
maxes_stub = mock_motors.build_sync_read_stub(*STS_SMS_SERIES_CONTROL_TABLE["Max_Position_Limit"], maxes)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus = FeetechMotorsBus(
|
2025-04-03 23:35:10 +08:00
|
|
|
port=mock_motors.port,
|
|
|
|
motors=dummy_motors,
|
|
|
|
calibration=dummy_calibration,
|
|
|
|
)
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-04-03 23:35:10 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
is_calibrated = bus.is_calibrated
|
2025-04-03 23:35:10 +08:00
|
|
|
|
|
|
|
assert is_calibrated
|
|
|
|
assert mock_motors.stubs[offsets_stub].called
|
|
|
|
assert mock_motors.stubs[mins_stub].called
|
|
|
|
assert mock_motors.stubs[maxes_stub].called
|
|
|
|
|
|
|
|
|
2025-04-03 18:14:15 +08:00
|
|
|
def test_reset_calibration(mock_motors, dummy_motors):
|
|
|
|
write_homing_stubs = []
|
|
|
|
write_mins_stubs = []
|
|
|
|
write_maxes_stubs = []
|
|
|
|
for motor in dummy_motors.values():
|
2025-04-14 17:56:53 +08:00
|
|
|
write_homing_stubs.append(
|
|
|
|
mock_motors.build_write_stub(*STS_SMS_SERIES_CONTROL_TABLE["Homing_Offset"], motor.id, 0)
|
|
|
|
)
|
|
|
|
write_mins_stubs.append(
|
|
|
|
mock_motors.build_write_stub(*STS_SMS_SERIES_CONTROL_TABLE["Min_Position_Limit"], motor.id, 0)
|
|
|
|
)
|
|
|
|
write_maxes_stubs.append(
|
|
|
|
mock_motors.build_write_stub(*STS_SMS_SERIES_CONTROL_TABLE["Max_Position_Limit"], motor.id, 4095)
|
|
|
|
)
|
2025-04-03 18:14:15 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
bus = FeetechMotorsBus(port=mock_motors.port, motors=dummy_motors)
|
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-04-03 18:14:15 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.reset_calibration()
|
2025-04-03 18:14:15 +08:00
|
|
|
|
|
|
|
assert all(mock_motors.stubs[stub].called for stub in write_homing_stubs)
|
|
|
|
assert all(mock_motors.stubs[stub].called for stub in write_mins_stubs)
|
|
|
|
assert all(mock_motors.stubs[stub].called for stub in write_maxes_stubs)
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_half_turn_homings(mock_motors, dummy_motors):
|
|
|
|
"""
|
|
|
|
For this test, we assume that the homing offsets are already 0 such that
|
|
|
|
Present_Position == Actual_Position
|
|
|
|
"""
|
|
|
|
current_positions = {
|
|
|
|
1: 1337,
|
|
|
|
2: 42,
|
|
|
|
3: 3672,
|
|
|
|
}
|
|
|
|
expected_homings = {
|
|
|
|
1: -710, # 1337 - 2047
|
|
|
|
2: -2005, # 42 - 2047
|
|
|
|
3: 1625, # 3672 - 2047
|
|
|
|
}
|
2025-04-14 17:56:53 +08:00
|
|
|
read_pos_stub = mock_motors.build_sync_read_stub(
|
|
|
|
*STS_SMS_SERIES_CONTROL_TABLE["Present_Position"], current_positions
|
|
|
|
)
|
2025-04-03 18:14:15 +08:00
|
|
|
write_homing_stubs = []
|
|
|
|
for id_, homing in expected_homings.items():
|
|
|
|
encoded_homing = encode_sign_magnitude(homing, 11)
|
2025-04-14 17:56:53 +08:00
|
|
|
stub = mock_motors.build_write_stub(
|
|
|
|
*STS_SMS_SERIES_CONTROL_TABLE["Homing_Offset"], id_, encoded_homing
|
|
|
|
)
|
2025-04-03 18:14:15 +08:00
|
|
|
write_homing_stubs.append(stub)
|
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
bus = FeetechMotorsBus(port=mock_motors.port, motors=dummy_motors)
|
|
|
|
bus.connect(assert_motors_exist=False)
|
|
|
|
bus.reset_calibration = MagicMock()
|
2025-04-03 18:14:15 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.set_half_turn_homings()
|
2025-04-03 18:14:15 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
bus.reset_calibration.assert_called_once()
|
2025-04-03 18:14:15 +08:00
|
|
|
assert mock_motors.stubs[read_pos_stub].called
|
|
|
|
assert all(mock_motors.stubs[stub].called for stub in write_homing_stubs)
|
|
|
|
|
|
|
|
|
|
|
|
def test_record_ranges_of_motion(mock_motors, dummy_motors):
|
|
|
|
positions = {
|
|
|
|
1: [351, 42, 1337],
|
|
|
|
2: [28, 3600, 2444],
|
|
|
|
3: [4002, 2999, 146],
|
|
|
|
}
|
|
|
|
expected_mins = {
|
|
|
|
"dummy_1": 42,
|
|
|
|
"dummy_2": 28,
|
|
|
|
"dummy_3": 146,
|
|
|
|
}
|
|
|
|
expected_maxes = {
|
|
|
|
"dummy_1": 1337,
|
|
|
|
"dummy_2": 3600,
|
|
|
|
"dummy_3": 4002,
|
|
|
|
}
|
2025-04-14 21:26:29 +08:00
|
|
|
stub = mock_motors.build_sequential_sync_read_stub(
|
2025-04-14 17:56:53 +08:00
|
|
|
*STS_SMS_SERIES_CONTROL_TABLE["Present_Position"], positions
|
|
|
|
)
|
2025-04-03 18:14:15 +08:00
|
|
|
with patch("lerobot.common.motors.motors_bus.enter_pressed", side_effect=[False, True]):
|
2025-04-14 21:26:29 +08:00
|
|
|
bus = FeetechMotorsBus(port=mock_motors.port, motors=dummy_motors)
|
|
|
|
bus.connect(assert_motors_exist=False)
|
2025-04-03 18:14:15 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
mins, maxes = bus.record_ranges_of_motion(display_values=False)
|
2025-04-03 18:14:15 +08:00
|
|
|
|
2025-04-14 21:26:29 +08:00
|
|
|
assert mock_motors.stubs[stub].calls == 3
|
2025-04-03 18:14:15 +08:00
|
|
|
assert mins == expected_mins
|
|
|
|
assert maxes == expected_maxes
|