Python API¶
The reference below is generated from the installed can_motor_control package by
mkdocstrings. The prose comes from the
docstrings authored in the Rust PyO3 source, so it matches exactly what
help() and Jupyter ? show.
Note
This page requires the native extension to be importable. Build it first
with maturin develop (the make docs / make docs-build targets do this
for you).
Robot and builder¶
Robot ¶
The top-level handle to a configured robot.
Build one from a config file with from_config or in code with
builder. The lifecycle is connect →
enable → repeated tick → disable; using it
as a context manager (with Robot.from_config(...) as r:) connects and
enables on entry and disables on exit.
Index by group name to get the corresponding Arm,
Gripper, or MotorGroup; use in to test for a
group. Control methods on those groups only queue commands — call
tick to exchange frames with the hardware.
connect
method descriptor
¶
connect()
Open every bus and prepare the robot for control.
Call once before enable/tick. Raises
TransportError if a bus cannot be opened.
disable
method descriptor
¶
disable()
Disable every motor on the robot.
Safe to call during shutdown; the context-manager exit calls this automatically.
enable
method descriptor
¶
enable()
Enable every motor on the robot.
For grippers configured for normalized opening control, this also runs
the feedback-based opening calibration used by Gripper.set_opening,
Gripper.open, and Gripper.close. If calibration cannot observe enough
movement or establish a usable span, raises LifecycleError and clears
that gripper's opening calibration.
Requires the robot to be connected; otherwise raises
LifecycleError.
from_config
staticmethod
¶
from_config(path)
Load a robot from a TOML config file at path.
Raises ConfigError if the file is missing or invalid.
refresh
method descriptor
¶
refresh()
Send a state-refresh query to every motor in every group (commands no motion).
Send-only — pair with Robot.tick to receive the replies. The GIL is
released for its duration. Raises LifecycleError if the robot is not
connected, or TransportError on a bus failure.
set_mode
method descriptor
¶
set_mode(mode)
Set the persistent control mode on every motor in every group. mode
is one of "mit", "pos_vel", "vel", "pos_force". Commands
no motion; call once after connect and before commanding. The GIL is
released for its duration. Raises ValueError for an unknown mode,
LifecycleError if not connected.
tick
method descriptor
¶
tick(per_bus_deadline_us)
Exchange one round of frames with every bus.
Flushes all commands queued on the groups since the last tick and reads
back feedback, blocking up to per_bus_deadline_us microseconds per
bus. This is the call that drives a realtime control loop; the GIL is
released for its duration. Raises LifecycleError if the
robot is not connected, or TransportError on a bus failure.
RobotBuilder ¶
Fluent builder for assembling a Robot in code.
Add buses with add_bus, then attach groups with
add_arm, add_gripper, or add_generic, and
finish with build. Each method returns the builder so calls can
be chained. Obtain one from Robot.builder.
add_arm
method descriptor
¶
add_arm(name, *, bus, motors)
Attach an arm named name on bus bus from a list of
MotorSpec. Returns the builder for chaining.
add_bus
method descriptor
¶
add_bus(name, transport, codec)
Register a named bus from a transport and a codec.
transport is MockCanBus or the platform-native hardware bus, and codec is a
can_motor_control.damiao.DamiaoCodec or MockFeedbackCodec. Both are
consumed: passing one already added to another bus raises
ValueError. Returns the builder for chaining.
add_generic
method descriptor
¶
add_generic(name, *, bus, motors)
Attach a generic motor group named name on bus bus (no
arm/gripper semantics). Returns the builder for chaining.
add_gripper
method descriptor
¶
add_gripper(name, *, bus, motor, opening_direction=None, default_current=None)
Attach a single-motor gripper named name on bus bus. Optional
opening_direction enables normalized opening control, and
default_current supplies its default per-unit current. Returns the
builder for chaining.
build
method descriptor
¶
build()
Consume the builder and return the assembled Robot.
Raises ConfigError if the topology is invalid (e.g. a group
references an unknown bus).
Groups¶
Arm ¶
A named group of motors driven together as an arm.
Obtained by indexing a Robot by group name (robot["arm"]).
len(arm) is the motor count and arm["j1"] returns a
Motor. The control methods take a NumPy command array with one
row per motor, in declaration order; commands are queued and flushed on the
next Robot.tick.
disable_all
method descriptor
¶
disable_all()
Disable every motor in the arm (queued for the next tick).
enable_all
method descriptor
¶
enable_all()
Enable every motor in the arm (queued for the next tick).
mit_control
method descriptor
¶
mit_control(cmds)
Queue an MIT-mode (impedance) command for every motor.
cmds is a (n, 5) float64 array with one row [kp, kd, q, dq,
tau] per motor, in declaration order: position gain, velocity gain,
target position (rad), target velocity (rad/s), and feed-forward torque
(N·m). A wrong shape raises ValueError. The command is sent on the
next Robot.tick.
pos_force_control
method descriptor
¶
pos_force_control(cmds)
Queue a position-force command for every motor.
cmds is a (n, 3) float64 array with one row [q, dq, i_pu]
per motor, in declaration order: target position (rad), target velocity
(rad/s), and current in per-unit. A wrong shape raises ValueError.
The command is sent on the next Robot.tick.
pos_vel_control
method descriptor
¶
pos_vel_control(cmds)
Queue a position-velocity command for every motor.
cmds is a (n, 2) float64 array with one row [q, dq] per
motor, in declaration order: target position (rad) and target velocity
(rad/s). A wrong shape raises ValueError. The command is sent on the
next Robot.tick.
positions
method descriptor
¶
positions()
Latest measured positions (radians) as a (n,) float64 array, one
entry per motor in declaration order.
refresh
method descriptor
¶
refresh()
Send a state-refresh query to every motor in the arm (commands no
motion). Pair with Robot.tick to receive the replies — this is how a
read loop keeps state fresh without driving the motors.
set_mode
method descriptor
¶
set_mode(mode)
Set the persistent control mode on every motor in the arm. mode is
one of "mit", "pos_vel", "vel", "pos_force". Commands no
motion; call once at startup (after connect, before commanding) so
the matching control commands take effect. Raises ValueError for an
unknown mode.
set_zero_all
method descriptor
¶
set_zero_all()
Set the current position of every motor as its new zero reference.
torques
method descriptor
¶
torques()
Latest estimated torques (N·m) as a (n,) float64 array, one entry
per motor in declaration order.
vel_control
method descriptor
¶
vel_control(cmds)
Queue a velocity command for every motor.
cmds is a (n,) float64 array of target velocities (rad/s), one
per motor in declaration order. A wrong shape raises ValueError. The
command is sent on the next Robot.tick.
velocities
method descriptor
¶
velocities()
Latest measured velocities (rad/s) as a (n,) float64 array, one
entry per motor in declaration order.
Gripper ¶
A single-motor group driven as a gripper.
Obtained by indexing a Robot by group name. Unlike
Arm, the control methods take scalar arguments for the one
motor. Commands are queued and flushed on the next Robot.tick.
opening
property
¶
opening
Normalized opening from feedback received by the most recent
Robot.tick, where 0.0 is fully closed and 1.0 is fully open.
Call Gripper.refresh followed by Robot.tick before reading when
fresh feedback is required. The automatic opening calibration performed
by Robot.enable must have completed, otherwise this property raises
LifecycleError. Reading the property does not send CAN frames.
close
method descriptor
¶
close(*, current=None)
Queue a fully-closed normalized opening command.
Requires the automatic opening calibration performed by Robot.enable.
current uses the same configured-default-then-library-default
fallback as set_opening when omitted.
mit_control
method descriptor
¶
mit_control(kp, kd, q, dq, tau)
Queue an MIT-mode (impedance) command for the gripper motor.
kp/kd are the position and velocity gains, q/dq the
target position (rad) and velocity (rad/s), and tau the
feed-forward torque (N·m). Sent on the next Robot.tick.
open
method descriptor
¶
open(*, current=None)
Queue a fully-open normalized opening command.
Requires the automatic opening calibration performed by Robot.enable.
current uses the same configured-default-then-library-default
fallback as set_opening when omitted.
pos_force_control
method descriptor
¶
pos_force_control(q, dq, i_pu)
Queue a position-force command for the gripper motor.
q is target position (rad), dq is target velocity (rad/s), and
i_pu is current in per-unit. Sent on the next Robot.tick.
pos_vel_control
method descriptor
¶
pos_vel_control(q, dq)
Queue a position-velocity command (target position q rad, target
velocity dq rad/s) for the gripper motor.
refresh
method descriptor
¶
refresh()
Send a state-refresh query to the gripper motor (commands no motion).
Pair with Robot.tick to receive the reply.
set_mode
method descriptor
¶
set_mode(mode)
Set the gripper motor's persistent control mode ("mit", "pos_vel",
"vel", "pos_force"). Commands no motion. Raises ValueError for
an unknown mode.
set_opening
method descriptor
¶
set_opening(opening, *, current=None)
Queue a normalized opening command where 0.0 is fully closed and
1.0 is fully open. Opening-enabled grippers are calibrated
automatically during Robot.enable; calling this before calibration
raises LifecycleError. current is an optional per-unit motor
current; if omitted, the gripper's configured default opening current is
used, falling back to the library default when no default was configured.
MotorGroup ¶
A generic, named group of motors with no arm/gripper semantics.
Created via RobotBuilder.add_generic and obtained by indexing a
Robot by group name. Currently exposes only its motor count.
Motor ¶
A live view of one motor's most recent feedback.
Obtained by indexing an Arm by motor name (arm["j1"]) or via
Gripper.motor. Every attribute access reads the latest values
decoded by the most recent Robot.tick; the object holds no state
of its own.
Specs and frames¶
MotorSpec ¶
Declaration of one motor on a bus: its name, SKU, and CAN ids.
Used when building a robot in code to describe the motors of an arm or
gripper (see RobotBuilder.add_arm). The type is a
can_motor_control.damiao.MotorType; send_id is the id the host
sends commands on and recv_id is the id the motor replies on.
CanFrame ¶
A single CAN frame: an arbitration id, a payload, and flags.
Construct frames with classical (classical CAN, up to 8 payload
bytes) or fd (CAN-FD, up to 64 bytes). The id, flags and
len attributes are read-only.
Transports¶
MockCanBus ¶
An in-memory CAN bus for tests and dry runs.
Drop-in replacement for SocketCanBus that talks to no hardware,
so robot code can be exercised without a real CAN interface. Pass it to
RobotBuilder.add_bus. A bus is consumed by the builder and may
be added to only one robot.
The default constructor is classical-CAN; use MockCanBus.new_fd for an
FD-capable mock that accepts and loops back CAN-FD frames.
new_fd
staticmethod
¶
new_fd(name)
Create a CAN-FD-capable mock bus. It advertises FD capabilities and loops back FD frames, so the FD send/receive path is testable without an FD-capable interface.
SocketCanBus ¶
A CAN bus backed by a Linux SocketCAN interface.
Opens interface (e.g. "can0") on construction; a failure to open
raises TransportError. Pass it to
RobotBuilder.add_bus. A bus is consumed by the builder and may
be added to only one robot.
SocketCanBus is present on Linux. macOS builds replace it with GsUsbBus;
see Native macOS gs_usb for its constructor and counters.
Damiao codec¶
DamiaoCodec ¶
The Damiao protocol codec.
Encodes commands to and decodes feedback from Damiao-family motors. Pass an
instance to RobotBuilder.add_bus to bind a bus to the Damiao
protocol. A codec is consumed by the builder and may be added to only one
bus.
MotorType ¶
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM10010
class-attribute
¶
DM10010 = MotorType.DM10010
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM10010L
class-attribute
¶
DM10010L = MotorType.DM10010L
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM3507
class-attribute
¶
DM3507 = MotorType.DM3507
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM4310
class-attribute
¶
DM4310 = MotorType.DM4310
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM4310_48V
class-attribute
¶
DM4310_48V = MotorType.DM4310_48V
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM4340
class-attribute
¶
DM4340 = MotorType.DM4340
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM4340_48V
class-attribute
¶
DM4340_48V = MotorType.DM4340_48V
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM6006
class-attribute
¶
DM6006 = MotorType.DM6006
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM8006
class-attribute
¶
DM8006 = MotorType.DM8006
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DM8009
class-attribute
¶
DM8009 = MotorType.DM8009
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DMG6220
class-attribute
¶
DMG6220 = MotorType.DMG6220
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DMH3510
class-attribute
¶
DMH3510 = MotorType.DMH3510
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
DMH6215
class-attribute
¶
DMH6215 = MotorType.DMH6215
Damiao motor SKU identifiers.
An IntEnum-compatible class naming each supported Damiao motor model
(e.g. DM4310, DM8009). Pass a member as the type of a
can_motor_control.MotorSpec so the codec applies the right limits and
scaling for that model.
Errors¶
DmError ¶
Bases: builtins.Exception
Base class for every error raised by can_motor_control.
Catch this to handle any failure from the library regardless of cause.
TransportError ¶
Bases: can_motor_control.DmError
A CAN transport operation failed (e.g. the SocketCAN interface is down, a send/receive timed out, or a frame was malformed).
CodecError ¶
Bases: can_motor_control.DmError
A motor frame could not be encoded or decoded by the vendor codec.
ConfigError ¶
Bases: can_motor_control.DmError
The robot configuration is invalid: bad config file, unknown vendor, unknown bus name, or an unsupported group/bus option.
LifecycleError ¶
Bases: can_motor_control.DmError
An operation was attempted in the wrong lifecycle state, such as ticking before Robot.connect or mutating topology after the robot is built.