Skip to main content

MotorCodec

Trait MotorCodec 

Source
pub trait MotorCodec: Send + Sync {
Show 13 methods // Required methods fn vendor_name(&self) -> &'static str; fn supports(&self, motor_type: MotorTypeId) -> bool; fn limits(&self, motor_type: MotorTypeId) -> Result<Limits, CodecError>; fn bind_to_bus(&mut self, caps: BusCapabilities); fn encode_enable(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>; fn encode_disable( &self, motor: MotorRef<'_>, ) -> Result<CanFrame, CodecError>; fn encode_set_zero( &self, motor: MotorRef<'_>, ) -> Result<CanFrame, CodecError>; fn encode_command( &self, motor: MotorRef<'_>, cmd: &Command, ) -> Result<CanFrame, CodecError>; fn decode(&self, frame: &CanFrame) -> Result<Option<Event>, CodecError>; // Provided methods fn encode_refresh( &self, motor: MotorRef<'_>, ) -> Result<Option<CanFrame>, CodecError> { ... } fn encode_set_mode( &self, motor: MotorRef<'_>, mode: CommandKind, ) -> Result<Option<CanFrame>, CodecError> { ... } fn encode_control_mode_readback( &self, motor: MotorRef<'_>, ) -> Result<Option<CanFrame>, CodecError> { ... } fn decode_control_mode_readback( &self, frame: &CanFrame, motor: MotorRef<'_>, ) -> Result<Option<u32>, CodecError> { ... }
}
Expand description

The vendor-agnostic codec contract.

Every vendor codec (Damiao, Robostride, MyActuator, …) implements this trait. can-motor-control uses Box<dyn MotorCodec> exclusively — it never depends on a specific vendor’s crate.

The trait is intentionally object-safe (no generics, no Self: Sized constraints on the methods) so a Box<dyn MotorCodec> is a valid field type.

Required Methods§

Source

fn vendor_name(&self) -> &'static str

Vendor short name used in error messages and the TOML registry (e.g. "damiao").

Source

fn supports(&self, motor_type: MotorTypeId) -> bool

True iff this codec can encode commands for and decode events from the supplied motor type.

Source

fn limits(&self, motor_type: MotorTypeId) -> Result<Limits, CodecError>

Per-motor-type physical limits.

Returns CodecError::UnknownMotorType when the motor type is not in this codec’s vendor space (or is an unknown SKU within the vendor space).

Source

fn bind_to_bus(&mut self, caps: BusCapabilities)

Called exactly once when this codec is bound to a crate::caps::BusCapabilities.

The codec MAY remember the capabilities for later use (e.g. to decide whether to emit CAN-FD frames). Codecs may assume bind_to_bus has been called by the time any encode method is invoked.

Source

fn encode_enable(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>

Encode the lifecycle “enable motor” command.

Source

fn encode_disable(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>

Encode the lifecycle “disable motor” command.

Source

fn encode_set_zero(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>

Encode the lifecycle “set this position as zero” command.

Source

fn encode_command( &self, motor: MotorRef<'_>, cmd: &Command, ) -> Result<CanFrame, CodecError>

Encode a control-mode command.

Source

fn decode(&self, frame: &CanFrame) -> Result<Option<Event>, CodecError>

Decode an inbound frame.

Returns Ok(Some(event)) for a recognized inbound message, Ok(None) for frames the codec does not recognize (foreign vendor, or a CAN ID outside the codec’s address range), and Err(CodecError::DecodeFailed { .. }) for frames that look like the codec’s vendor but fail to parse.

Provided Methods§

Source

fn encode_refresh( &self, motor: MotorRef<'_>, ) -> Result<Option<CanFrame>, CodecError>

Encode a “request current motor state” frame, if the vendor protocol has one.

The returned frame MUST request a state-feedback reply without commanding any motion. Returns Ok(None) (the default) when the codec has no such query; callers skip those motors. This lets a read loop poll state via refresh then MotorCodec::decode without applying torque.

Source

fn encode_set_mode( &self, motor: MotorRef<'_>, mode: CommandKind, ) -> Result<Option<CanFrame>, CodecError>

Encode a “set the motor’s persistent control mode” frame, if the vendor protocol supports it.

mode selects which control law the motor will accept (MIT / PosVel / Vel / PosForce). Returns Ok(None) (the default) when the codec has no such command; callers skip those motors. This commands no motion — call it once at startup, before the matching MotorCodec::encode_command mode.

Source

fn encode_control_mode_readback( &self, motor: MotorRef<'_>, ) -> Result<Option<CanFrame>, CodecError>

Encode a private control-mode read-back query, when supported.

Source

fn decode_control_mode_readback( &self, frame: &CanFrame, motor: MotorRef<'_>, ) -> Result<Option<u32>, CodecError>

Decode a private control-mode read-back response for motor.

Implementors§