MiMotor
robodyno.components.can_bus.mi_motor
MiMotor
Bases: CanBusDevice
__init__(can, id_=127)
Initializes the Robodyno motor based on the device id and type.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
can
|
CanBus
|
The CAN bus interface. |
必需 |
id_
|
int
|
The device id. The default factory id of the mi_motor is 0x7F. |
127
|
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
If device id is not in range [0x01, 0x80). |
TypeError
|
If can is not CanBus. |
get_mcu_uuid(can, can_id, timeout=0.1)
staticmethod
Retrieves the MCU UUID of a Xiaomi motor from the CAN bus for a specified device ID. This is commonly used to check if the device is online.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
can
|
CanBus
|
The CAN bus instance used to send and receive messages. |
必需 |
can_id
|
int
|
The target device ID on the CAN bus to query. |
必需 |
timeout
|
float
|
The time to wait for a response, in seconds (default is 0.1s). |
0.1
|
返回:
| 类型 | 描述 |
|---|---|
Optional[int]
|
The MCU UUID of the device if successful, otherwise None if the request times out |
Optional[int]
|
or the device ID does not match the expected CAN ID. |
get_version(timeout=0.1)
Get device firmware version and type.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The time to wait for a response, in seconds (default is 0.1s). |
0.1
|
返回:
| 类型 | 描述 |
|---|---|
dict | None
|
Device firmware version and type. |
enable()
Enables the motor.
After enabled, the motor can be controlled.
disable()
Disables the motor.
This is the default state after power on. After disabled, the motor will stop working and loose torque.
unlock()
Unlocks the motor.
This command only works on motors with brake. After unlocked, the motor can be rotated freely.
引发:
| 类型 | 描述 |
|---|---|
NotImplementedError
|
If the motor does not support brake. |
init_pos()
Sets the current position of the motor as the initial position.
init_abs_pos()
Sets the current absolute position of the motor as the initial position.
引发:
| 类型 | 描述 |
|---|---|
NotImplementedError
|
If the device is mi motor. |
calibrate()
Calibrates the motor.
config_can_bus(new_id)
Configures the CAN bus settings of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
new_id
|
int
|
The new device id. |
必需 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
If the new CAN id is not in the range of 0x01-0x7f. |
save()
Saves the motor configurations.
save_configuration()
Saves the motor configurations.
This method will be deprecated. Use save instead.
clear_errors()
Clears the motor errors.
Most errors are cleared automatically if the cause is resolved.
estop()
Requests an emergency stop.
The motor will loose torque immediately and report an ESTOP error.
引发:
| 类型 | 描述 |
|---|---|
NotImplementedError
|
If the device is mi motor. |
reboot()
Reboots the motor.
引发:
| 类型 | 描述 |
|---|---|
NotImplementedError
|
If the device is mi motor. |
reset()
Resets the motor.
This command will reset the motor to the factory settings. All configurations will be lost and the id will be reset to 0x7F. The motor must be calibrated again after reset.
mit_mode()
Sets the motor to mit mode.
position_mode()
Sets the motor to position mode.
position_filter_mode(bandwidth)
Sets the motor to position filter mode. todo: implement the position filter mode Note: The position track mode has not been implemented yet. The current implementation only sets the motor to position mode.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
bandwidth
|
float
|
The bandwidth of the filter in Hz. The value is suggested to be the control loop frequency in Hz. |
必需 |
position_track_mode(vel, acc, dec)
Sets the motor to position track mode. todo: implement the position track mode Note: The position track mode has not been implemented yet. The current implementation only sets the motor to position mode.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
vel
|
float
|
The velocity limit in rad/s. |
必需 |
acc
|
float
|
The acceleration in rad/s^2. The value must be positive. |
必需 |
dec
|
float
|
The deceleration in rad/s^2. The value must be positive. |
必需 |
velocity_mode()
Sets the motor to velocity mode.
velocity_ramp_mode(ramp)
Sets the motor to velocity ramp mode. todo: implement the velocity ramp mode
Note
The velocity ramp mode has not been implemented yet. The current implementation only sets the motor to velocity mode.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
ramp
|
float
|
The velocity ramp in rad/s^2. The value must be positive. |
必需 |
torque_mode()
Sets the motor to torque mode.
set_mit(torque, position, velocity, kp, kd)
Sets the MIT control parameters of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
torque
|
float
|
The torque reference in Nm. |
必需 |
position
|
float
|
The position reference in rad. |
必需 |
velocity
|
float
|
The velocity reference in rad/s. |
必需 |
kp
|
float
|
The proportional gain. |
必需 |
kd
|
float
|
The derivative gain. |
必需 |
set_pos(position)
Sets the target position of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
position
|
float
|
The target position in rad. |
必需 |
set_abs_pos(pos, vel_ff=0, torque_ff=0)
Sets the target absolute position of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
pos
|
float
|
The target absolute position in rad. |
必需 |
vel_ff
|
float
|
The velocity feedforward in rad/s. |
0
|
torque_ff
|
float
|
The torque feedforward in Nm. |
0
|
引发:
| 类型 | 描述 |
|---|---|
NotImplementedError
|
If the motor is mi motor. |
set_vel(velocity)
Sets the target velocity of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
vel
|
float
|
The target velocity in rad/s. |
必需 |
set_torque(torque)
Sets the target torque of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
torque
|
float
|
The target torque in Nm. |
必需 |
set_pid(pos_kp, vel_kp, vel_ki)
Sets the PID parameters of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
pos_kp
|
float
|
The Kp of the position control loop. |
必需 |
vel_kp
|
float
|
The Kp of the velocity control loop. |
必需 |
vel_ki
|
float
|
The Ki of the velocity control loop. |
必需 |
set_vel_limit(velocity_limit)
Sets the velocity limit of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
vel_lim
|
float
|
The velocity limit in rad/s. The value must be positive. |
必需 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
If the velocity limit is out of range. |
set_current_limit(current_limit)
Sets the current limit of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
current_lim
|
float
|
The current limit in amps. The value must be positive. |
必需 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
If the current limit is out of range. |
get_state(timeout=None)
Reads the motor state and error.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
tuple | None
|
The motor state, error, and control mode. Returns None if the read times out. |
get_voltage(timeout=None)
Reads the DC bus voltage of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
float | None
|
The DC bus voltage in volts. Returns None if the read times out. |
get_temperature(timeout=None)
Reads the temperature of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
float | None
|
The temperature in degrees Celsius. Returns None if the read times out. |
get_mode(timeout=None)
Reads the control mode and the mode parameters of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
tuple | None
|
The control mode and the mode parameters. Returns None if the read times out. |
get_feedback(timeout=None)
Reads the feedbacks of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
tuple | None
|
The position, velocity, and torque. Returns None if the read times out. |
get_pos(timeout=None)
Reads the position of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
float | None
|
The position in rad. Returns None if the read times out. |
get_abs_pos(timeout=None)
Reads the absolute position of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
float | None
|
The absolute position in rad. Returns None if the read times out. |
引发:
| 类型 | 描述 |
|---|---|
NotImplementedError
|
If the device is mi motor. |
get_vel(timeout=None)
Reads the velocity of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
float | None
|
The velocity in rad/s. Returns None if the read times out. |
get_torque(timeout=None)
Reads the torque of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
float | None
|
The torque in Nm. Returns None if the read times out. |
get_pid(timeout=None)
Reads the PID parameters of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
tuple | None
|
The position KP, velocity KP, and velocity KI. Returns None if the read times out. |
get_vel_limit(timeout=None)
Reads the velocity limit of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
float | None
|
The velocity limit in rad/s. Returns None if the read times out. |
get_current_limit(timeout=None)
Reads the current limit of the motor.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float
|
The timeout in seconds. |
None
|
返回:
| 类型 | 描述 |
|---|---|
float | None
|
The current limit in A. Returns None if the read times out. |
MotorState
Bases: Enum
Enumerates motor working states.
UNKNOWN = -1
class-attribute
instance-attribute
The state of the motor is unknown.
DISABLED = 0
class-attribute
instance-attribute
The motor is disabled.
CALIBRATE = 1
class-attribute
instance-attribute
The motor is calibrating.
ENABLED = 2
class-attribute
instance-attribute
The motor is enabled.
MotorError
Bases: Enum
Enumerates motor errors.
NONE = 0
class-attribute
instance-attribute
No error.
UNDER_VOLTAGE = 1
class-attribute
instance-attribute
The motor is under voltage.
OVER_CURRENT = 2
class-attribute
instance-attribute
The motor is over current.
OVER_TEMP = 4
class-attribute
instance-attribute
The motor is over temperature.
MAGNETIC_ENCODER = 8
class-attribute
instance-attribute
The magnetic encoder is error.
HALL_ENCODER = 16
class-attribute
instance-attribute
The hall encoder is error.
UNCALIBRATEED = 32
class-attribute
instance-attribute
The motor is uncalibrated .
MotorMotorError
Bases: Enum
Enumerates motor driver errors.
NONE = 0
class-attribute
instance-attribute
No error.
MotorEncoderError
Bases: Enum
Enumerates motor encoder errors.
NONE = 0
class-attribute
instance-attribute
No error.
MotorControllerError
Bases: Enum
Enumerates motor controller errors.
NONE = 0
class-attribute
instance-attribute
No error.
MotorControlMode
Bases: Enum
Enumerates motor control modes.
UNKNOWN = -1
class-attribute
instance-attribute
The motor control mode is unknown.
MIT_MODE = 0
class-attribute
instance-attribute
Control the motor in MIT mode.
POSITION_MODE = 1
class-attribute
instance-attribute
Control the motor in position mode.
VELOCITY_MODE = 2
class-attribute
instance-attribute
Control the motor in velocity mode.
TORQUE_MODE = 3
class-attribute
instance-attribute
Control the motor in torque mode.
MiMotorProtocol
command0(master_id, id)
staticmethod
Get device ID
parse_command0_response(msg)
staticmethod
Parse command0 response
command1(torque, position, velocity, kp, kd, id)
staticmethod
Set MIT control command, device will return the current state by command 2 when received
command2(master_id, id)
staticmethod
Get current state, device will return the current state by command 2 when received
parse_command2_response(msg)
staticmethod
Parse command0 response
返回:
| 类型 | 描述 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
command3(master_id, id)
staticmethod
enable motor, device will return the current state by command 2 when received
command4(master_id, id, data_cmd=0)
staticmethod
disable motor, device will return the current state by command 2 when received
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
- data_cmd
|
int
|
Command data for disabling motor. Default is 0 - 0: Disable motor - 1: Disable motor and clear error |
必需 |
command5(master_id, id, data_cmd=0)
staticmethod
Encoder Calibration
command6(master_id, id)
staticmethod
set mechanical zero, device will return the current state by command 2 when received
command7(master_id, id, new_id)
staticmethod
set device id, device will return the mcu uuid by command 0 when received
command8(master_id, id, cmd, param=None, param_data=None)
staticmethod
Creates a CAN message to set, save, or reset a parameter on a device.
This function generates a CAN message with a specific command (cmd) to instruct
the device to perform a parameter-related action. The device will respond based on
the command type:
- Write parameter (cmd = 0)
- Save parameter (cmd = 2)
- Reset parameter (cmd = 3)
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
master_id
|
int
|
The ID of the master device. |
必需 |
id
|
int
|
The ID of the target device (e.g., motor). |
必需 |
cmd
|
int
|
The command type. Valid values are: - 0: Write parameter - 2: Save parameter - 3: Reset parameter |
必需 |
param
|
dict
|
A dictionary containing parameter details,
required for write commands ( |
None
|
param_data
|
any
|
The data to write to the parameter, required for |
None
|
返回:
| 类型 | 描述 |
|---|---|
Message
|
can.Message: The generated CAN message with the appropriate arbitration ID and data payload based on the command. |
Example
msg = command8(0xFF, 0x7F, cmd=0, param={"format": "B", "index": 0x200A}, param_data=0x70)
Sends a write command with parameter index 0x200A and data 0x70 to device 0x7F
command9(master_id, id, param=None)
staticmethod
read param, device will return the param value by command 9 when received
parse_command9_response(msg)
staticmethod
Parse command9 response
command17(master_id, id, parameter)
staticmethod
read parameter, device will return the parameter value by command 17 when received
parse_command17_response(msg)
staticmethod
Parse command17 response
command18(master_id, id, parameter, data)
staticmethod
write parameter, device will return the parameter value by command 18 when received