forked from tangger/lerobot
Add support for feetech scs series + various fixes
This commit is contained in:
@@ -5,7 +5,7 @@ import scservo_sdk as scs
|
||||
import serial
|
||||
from mock_serial import MockSerial
|
||||
|
||||
from lerobot.common.motors.feetech import SCS_SERIES_CONTROL_TABLE, FeetechMotorsBus
|
||||
from lerobot.common.motors.feetech import STS_SMS_SERIES_CONTROL_TABLE, FeetechMotorsBus
|
||||
from lerobot.common.motors.feetech.feetech import patch_setPacketTimeout
|
||||
|
||||
from .mock_serial_patch import WaitableStub
|
||||
@@ -297,7 +297,7 @@ class MockMotors(MockSerial):
|
||||
instruction packets. It is meant to test MotorsBus classes.
|
||||
"""
|
||||
|
||||
ctrl_table = SCS_SERIES_CONTROL_TABLE
|
||||
ctrl_table = STS_SMS_SERIES_CONTROL_TABLE
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@@ -338,12 +338,6 @@ class MockMotors(MockSerial):
|
||||
def build_read_stub(
|
||||
self, data_name: str, scs_id: int, value: int | None = None, num_invalid_try: int = 0
|
||||
) -> str:
|
||||
"""
|
||||
'data_name' supported:
|
||||
- Model_Number
|
||||
"""
|
||||
if data_name != "Model_Number":
|
||||
raise NotImplementedError
|
||||
address, length = self.ctrl_table[data_name]
|
||||
read_request = MockInstructionPacket.read(scs_id, address, length)
|
||||
return_packet = MockStatusPacket.read(scs_id, value, length)
|
||||
|
||||
@@ -6,7 +6,7 @@ import pytest
|
||||
import scservo_sdk as scs
|
||||
|
||||
from lerobot.common.motors import Motor, MotorCalibration, MotorNormMode
|
||||
from lerobot.common.motors.feetech import MODEL_NUMBER, FeetechMotorsBus
|
||||
from lerobot.common.motors.feetech import MODEL_NUMBER_TABLE, FeetechMotorsBus
|
||||
from lerobot.common.utils.encoding_utils import encode_sign_magnitude
|
||||
from tests.mocks.mock_feetech import MockMotors, MockPortHandler
|
||||
|
||||
@@ -129,7 +129,7 @@ def test_scan_port(mock_motors):
|
||||
|
||||
@pytest.mark.parametrize("id_", [1, 2, 3])
|
||||
def test_ping(id_, mock_motors, dummy_motors):
|
||||
expected_model_nb = MODEL_NUMBER[dummy_motors[f"dummy_{id_}"].model]
|
||||
expected_model_nb = MODEL_NUMBER_TABLE[dummy_motors[f"dummy_{id_}"].model]
|
||||
ping_stub = mock_motors.build_ping_stub(id_)
|
||||
mobel_nb_stub = mock_motors.build_read_stub("Model_Number", id_, expected_model_nb)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
@@ -147,7 +147,7 @@ def test_ping(id_, mock_motors, dummy_motors):
|
||||
|
||||
def test_broadcast_ping(mock_motors, dummy_motors):
|
||||
models = {m.id: m.model for m in dummy_motors.values()}
|
||||
expected_model_nbs = {id_: MODEL_NUMBER[model] for id_, model in models.items()}
|
||||
expected_model_nbs = {id_: MODEL_NUMBER_TABLE[model] for id_, model in models.items()}
|
||||
ping_stub = mock_motors.build_broadcast_ping_stub(list(models))
|
||||
mobel_nb_stub = mock_motors.build_sync_read_stub("Model_Number", expected_model_nbs)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
@@ -191,55 +191,7 @@ def test_sync_read_none(mock_motors, dummy_motors):
|
||||
(3, 4016),
|
||||
],
|
||||
)
|
||||
def test_sync_read_by_id(id_, position, mock_motors, dummy_motors):
|
||||
expected_position = {id_: position}
|
||||
stub_name = mock_motors.build_sync_read_stub("Present_Position", expected_position)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
motors=dummy_motors,
|
||||
)
|
||||
motors_bus.connect(assert_motors_exist=False)
|
||||
|
||||
read_position = motors_bus.sync_read("Present_Position", id_, normalize=False)
|
||||
|
||||
assert mock_motors.stubs[stub_name].called
|
||||
assert read_position == expected_position
|
||||
|
||||
|
||||
@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))
|
||||
stub_name = mock_motors.build_sync_read_stub("Present_Position", expected_positions)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
motors=dummy_motors,
|
||||
)
|
||||
motors_bus.connect(assert_motors_exist=False)
|
||||
|
||||
read_positions = motors_bus.sync_read("Present_Position", ids, normalize=False)
|
||||
|
||||
assert mock_motors.stubs[stub_name].called
|
||||
assert read_positions == expected_positions
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"id_, position",
|
||||
[
|
||||
(1, 1337),
|
||||
(2, 42),
|
||||
(3, 4016),
|
||||
],
|
||||
)
|
||||
def test_sync_read_by_name(id_, position, mock_motors, dummy_motors):
|
||||
def test_sync_read_single_value(id_, position, mock_motors, dummy_motors):
|
||||
expected_position = {f"dummy_{id_}": position}
|
||||
stub_name = mock_motors.build_sync_read_stub("Present_Position", {id_: position})
|
||||
motors_bus = FeetechMotorsBus(
|
||||
@@ -263,7 +215,7 @@ def test_sync_read_by_name(id_, position, mock_motors, dummy_motors):
|
||||
],
|
||||
ids=["1 motor", "2 motors", "3 motors"],
|
||||
) # fmt: skip
|
||||
def test_sync_read_by_names(ids, positions, mock_motors, dummy_motors):
|
||||
def test_sync_read(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))
|
||||
@@ -291,9 +243,9 @@ def test_sync_read_by_names(ids, positions, mock_motors, dummy_motors):
|
||||
],
|
||||
)
|
||||
def test_sync_read_num_retry(num_retry, num_invalid_try, pos, mock_motors, dummy_motors):
|
||||
expected_position = {1: pos}
|
||||
expected_position = {"dummy_1": pos}
|
||||
stub_name = mock_motors.build_sync_read_stub(
|
||||
"Present_Position", expected_position, num_invalid_try=num_invalid_try
|
||||
"Present_Position", {1: pos}, num_invalid_try=num_invalid_try
|
||||
)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
@@ -302,11 +254,11 @@ def test_sync_read_num_retry(num_retry, num_invalid_try, pos, mock_motors, dummy
|
||||
motors_bus.connect(assert_motors_exist=False)
|
||||
|
||||
if num_retry >= num_invalid_try:
|
||||
pos_dict = motors_bus.sync_read("Present_Position", 1, normalize=False, num_retry=num_retry)
|
||||
assert pos_dict == {1: pos}
|
||||
pos_dict = motors_bus.sync_read("Present_Position", "dummy_1", normalize=False, num_retry=num_retry)
|
||||
assert pos_dict == expected_position
|
||||
else:
|
||||
with pytest.raises(ConnectionError):
|
||||
_ = motors_bus.sync_read("Present_Position", 1, normalize=False, num_retry=num_retry)
|
||||
_ = motors_bus.sync_read("Present_Position", "dummy_1", normalize=False, num_retry=num_retry)
|
||||
|
||||
expected_calls = min(1 + num_retry, 1 + num_invalid_try)
|
||||
assert mock_motors.stubs[stub_name].calls == expected_calls
|
||||
@@ -335,28 +287,6 @@ def test_sync_write_single_value(data_name, value, mock_motors, dummy_motors):
|
||||
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 = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
motors=dummy_motors,
|
||||
)
|
||||
motors_bus.connect(assert_motors_exist=False)
|
||||
|
||||
motors_bus.sync_write("Goal_Position", value, normalize=False)
|
||||
|
||||
assert mock_motors.stubs[stub_name].wait_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"ids, positions",
|
||||
[
|
||||
@@ -366,54 +296,7 @@ def test_sync_write_by_id(id_, position, mock_motors, dummy_motors):
|
||||
],
|
||||
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)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
motors=dummy_motors,
|
||||
)
|
||||
motors_bus.connect(assert_motors_exist=False)
|
||||
|
||||
motors_bus.sync_write("Goal_Position", values, normalize=False)
|
||||
|
||||
assert mock_motors.stubs[stub_name].wait_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"id_, position",
|
||||
[
|
||||
(1, 1337),
|
||||
(2, 42),
|
||||
(3, 4016),
|
||||
],
|
||||
)
|
||||
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)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
motors=dummy_motors,
|
||||
)
|
||||
motors_bus.connect(assert_motors_exist=False)
|
||||
|
||||
write_value = {f"dummy_{id_}": position}
|
||||
motors_bus.sync_write("Goal_Position", write_value, normalize=False)
|
||||
|
||||
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):
|
||||
def test_sync_write(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)
|
||||
@@ -438,29 +321,7 @@ def test_sync_write_by_names(ids, positions, mock_motors, dummy_motors):
|
||||
("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 = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
motors=dummy_motors,
|
||||
)
|
||||
motors_bus.connect(assert_motors_exist=False)
|
||||
|
||||
motors_bus.write(data_name, dxl_id, value, normalize=False)
|
||||
|
||||
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):
|
||||
def test_write(data_name, dxl_id, value, mock_motors, dummy_motors):
|
||||
stub_name = mock_motors.build_write_stub(data_name, dxl_id, value)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
|
||||
Reference in New Issue
Block a user