跳转至

Motor

robodyno.components.webots.motor

Provides a class to control webots motor.

The webots motor has the same interface as the can bus motor. The webots motor is used for simulation.

Examples:

from robodyno.components import Motor
from robodyno.interfaces import Webots
webots = Webots()
motor = Motor(webots, 0x10)

motor.position_track_mode(5, 2, 2)
motor.enable()
motor.set_pos(31.4)

while True:
    print(f'pos: {motor.get_pos():.2f}, vel: {motor.get_vel():.2f}')
    if webots.sleep(0.5) == -1:
        break

Motor

Bases: WebotsDevice

Webots motor component.

Attributes:

Name Type Description
id int

ID of the motor.

type Model

Type of the motor.

reduction float

Reduction of the motor.

available_velocity float

The maximum velocity of the motor.

available_torque float

The maximum torque of the motor.

available_current float

The maximum phase current of the motor.

torque_constant float

The torque constant of the motor in Nm/A. This is the torque provided by the motor when the current is 1A.

with_brake bool

Whether the motor has a brake.

state Motor.MotorState

The working state of the motor.

mode Motor.MotorControlMode

The control mode of the motor.

__init__(webots, id_=16, type_=None, twin=None)

Initialize the motor.

Parameters:

Name Type Description Default
webots Webots

The instance of Webots class.

required
id_ int

ID of the motor. Defaults to 0x10.

16
type_ Optional[str]

Force the motor type. Defaults to None.

None
twin Optional[CanBus]

The twin motor. Defaults to None.

None

Raises:

Type Description
ValueError

The motor type is invalid.

parallel_step()

See base class.

step()

See base class.

enable()

Enables the motor.

disable()

Disables the motor.

twin_mode()

Sets the motor to digital twin mode.

position_mode()

Sets the motor to position mode.

position_filter_mode(bandwidth)

Sets the motor to position filter mode.

Parameters:

Name Type Description Default
bandwidth float

The bandwidth of the filter in Hz. The value is suggested to be the control loop frequency in Hz.

required

position_track_mode(vel, acc, dec)

Sets the motor to position track mode.

Parameters:

Name Type Description Default
vel float

The velocity limit in rad/s.

required
acc float

The acceleration in rad/s^2. The value must be positive.

required
dec float

The deceleration in rad/s^2. The value must be positive.

required

velocity_mode()

Sets the motor to velocity mode.

velocity_ramp_mode(ramp)

Sets the motor to velocity ramp mode.

Parameters:

Name Type Description Default
ramp float

The velocity ramp in rad/s^2. The value must be positive.

required

torque_mode()

Sets the motor to torque mode.

set_pos(pos, vel_ff=0, torque_ff=0)

Sets the target position of the motor.

velocity feedforward and torque feedforward are not supported in Webots.

Parameters:

Name Type Description Default
pos float

The target position in rad.

required

set_abs_pos(pos, vel_ff=0, torque_ff=0)

Sets the target absolute position of the motor.

velocity feedforward and torque feedforward are not supported in Webots.

Parameters:

Name Type Description Default
pos float

The target absolute position in rad.

required

set_vel(vel, torque_ff=0)

Sets the target velocity of the motor.

Parameters:

Name Type Description Default
vel float

The target velocity in rad/s.

required

set_torque(torque)

Sets the target torque of the motor.

Parameters:

Name Type Description Default
torque float

The target torque in Nm.

required

set_vel_limit(vel_lim)

Sets the velocity limit of the motor.

Parameters:

Name Type Description Default
vel_lim float

The velocity limit in rad/s. The value must be positive.

required

Raises:

Type Description
ValueError

If the velocity limit is out of range.

set_current_limit(current_lim)

Sets the current limit of the motor.

Parameters:

Name Type Description Default
current_lim float

The current limit in amps. The value must be positive.

required

Raises:

Type Description
ValueError

If the current limit is out of range.

get_mode(timeout=None)

Reads the control mode and the mode parameters of the motor.

Timeout is not supported in Webots.

Returns:

Type Description
tuple

The control mode and the mode parameters.

get_pos(timeout=None)

Reads the position of the motor.

Timeout is not supported in Webots.

Returns:

Type Description
float

The position in rad.

get_abs_pos(timeout=None)

Reads the absolute position of the motor.

Timeout is not supported in Webots.

Returns:

Type Description
float

The absolute position in rad.

get_vel(timeout=None)

Reads the velocity of the motor.

Timeout is not supported in Webots.

Returns:

Type Description
float

The velocity in rad/s.

get_torque(timeout=None)

Reads the torque of the motor.

Timeout is not supported in Webots.

Returns:

Type Description
float

The torque in Nm.

get_vel_limit(timeout=None)

Reads the velocity limit of the motor.

Timeout is not supported in Webots.

Returns:

Type Description
float

The velocity limit in rad/s.

get_current_limit(timeout=None)

Reads the current limit of the motor.

Timeout is not supported in Webots.

Returns:

Type Description
float

The current limit in amps.

MotorState

Bases: Enum

Enumerates motor working states.

UNKNOWN = -1 class-attribute instance-attribute

The state of the motor is unknown.

DISABLED = 1 class-attribute instance-attribute

The motor is disabled.

CALIBRATE = 3 class-attribute instance-attribute

The motor is preparing for calibration.

MOTOR_CALIBRATING = 4 class-attribute instance-attribute

The motor is calibrating.

OFFSET_CALIBRATING = 7 class-attribute instance-attribute

The motor is calibrating the encoder.

ENABLED = 8 class-attribute instance-attribute

The motor is enabled.

HOMING = 11 class-attribute instance-attribute

The motor is aligning the encoders.

MotorControlMode

Bases: Enum

Enumerates motor control modes.

UNKNOWN = (0, 0) class-attribute instance-attribute

The motor control mode is unknown.

POSITION_MODE = (3, 1) class-attribute instance-attribute

Control the motor in position mode.

POSITION_FILTER_MODE = (3, 3) class-attribute instance-attribute

Control the motor in position filter mode.

POSITION_TRACK_MODE = (3, 5) class-attribute instance-attribute

Control the motor in position track mode.

VELOCITY_MODE = (2, 1) class-attribute instance-attribute

Control the motor in velocity mode.

VELOCITY_RAMP_MODE = (2, 2) class-attribute instance-attribute

Control the motor in velocity ramp mode.

TORQUE_MODE = (1, 1) class-attribute instance-attribute

Control the motor in torque mode.

TWIN_MODE = (255, 255) class-attribute instance-attribute

Digital twin mode.