Skip to content

Commit ae9605f

Browse files
authored
fix setting motor id with new dataclass config (#668)
1 parent 3c0a209 commit ae9605f

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

lerobot/scripts/configure_motor.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,53 @@
1616
import time
1717

1818

19-
def configure_motor(port, brand, model, motor_idx_des, baudrate_des):
19+
def get_motor_bus_cls(brand: str) -> tuple:
2020
if brand == "feetech":
21-
from lerobot.common.robot_devices.motors.feetech import MODEL_BAUDRATE_TABLE
21+
from lerobot.common.robot_devices.motors.configs import FeetechMotorsBusConfig
2222
from lerobot.common.robot_devices.motors.feetech import (
23-
SCS_SERIES_BAUDRATE_TABLE as SERIES_BAUDRATE_TABLE,
23+
MODEL_BAUDRATE_TABLE,
24+
SCS_SERIES_BAUDRATE_TABLE,
25+
FeetechMotorsBus,
2426
)
25-
from lerobot.common.robot_devices.motors.feetech import FeetechMotorsBus as MotorsBusClass
27+
28+
return FeetechMotorsBusConfig, FeetechMotorsBus, MODEL_BAUDRATE_TABLE, SCS_SERIES_BAUDRATE_TABLE
29+
2630
elif brand == "dynamixel":
27-
from lerobot.common.robot_devices.motors.dynamixel import MODEL_BAUDRATE_TABLE
31+
from lerobot.common.robot_devices.motors.configs import DynamixelMotorsBusConfig
2832
from lerobot.common.robot_devices.motors.dynamixel import (
29-
X_SERIES_BAUDRATE_TABLE as SERIES_BAUDRATE_TABLE,
33+
MODEL_BAUDRATE_TABLE,
34+
X_SERIES_BAUDRATE_TABLE,
35+
DynamixelMotorsBus,
3036
)
31-
from lerobot.common.robot_devices.motors.dynamixel import DynamixelMotorsBus as MotorsBusClass
37+
38+
return DynamixelMotorsBusConfig, DynamixelMotorsBus, MODEL_BAUDRATE_TABLE, X_SERIES_BAUDRATE_TABLE
39+
3240
else:
3341
raise ValueError(
3442
f"Currently we do not support this motor brand: {brand}. We currently support feetech and dynamixel motors."
3543
)
3644

45+
46+
def configure_motor(port, brand, model, motor_idx_des, baudrate_des):
47+
motor_bus_config_cls, motor_bus_cls, model_baudrate_table, series_baudrate_table = get_motor_bus_cls(
48+
brand
49+
)
50+
3751
# Check if the provided model exists in the model_baud_rate_table
38-
if model not in MODEL_BAUDRATE_TABLE:
52+
if model not in model_baudrate_table:
3953
raise ValueError(
40-
f"Invalid model '{model}' for brand '{brand}'. Supported models: {list(MODEL_BAUDRATE_TABLE.keys())}"
54+
f"Invalid model '{model}' for brand '{brand}'. Supported models: {list(model_baudrate_table.keys())}"
4155
)
4256

4357
# Setup motor names, indices, and models
4458
motor_name = "motor"
4559
motor_index_arbitrary = motor_idx_des # Use the motor ID passed via argument
4660
motor_model = model # Use the motor model passed via argument
4761

62+
config = motor_bus_config_cls(port=port, motors={motor_name: (motor_index_arbitrary, motor_model)})
63+
4864
# Initialize the MotorBus with the correct port and motor configurations
49-
motor_bus = MotorsBusClass(port=port, motors={motor_name: (motor_index_arbitrary, motor_model)})
65+
motor_bus = motor_bus_cls(config=config)
5066

5167
# Try to connect to the motor bus and handle any connection-specific errors
5268
try:
@@ -59,7 +75,7 @@ def configure_motor(port, brand, model, motor_idx_des, baudrate_des):
5975
# Motor bus is connected, proceed with the rest of the operations
6076
try:
6177
print("Scanning all baudrates and motor indices")
62-
all_baudrates = set(SERIES_BAUDRATE_TABLE.values())
78+
all_baudrates = set(series_baudrate_table.values())
6379
motor_index = -1 # Set the motor index to an out-of-range value.
6480

6581
for baudrate in all_baudrates:
@@ -89,7 +105,7 @@ def configure_motor(port, brand, model, motor_idx_des, baudrate_des):
89105

90106
if baudrate != baudrate_des:
91107
print(f"Setting its baudrate to {baudrate_des}")
92-
baudrate_idx = list(SERIES_BAUDRATE_TABLE.values()).index(baudrate_des)
108+
baudrate_idx = list(series_baudrate_table.values()).index(baudrate_des)
93109

94110
# The write can fail, so we allow retries
95111
motor_bus.write_with_motor_ids(motor_bus.motor_models, motor_index, "Baud_Rate", baudrate_idx)

0 commit comments

Comments
 (0)