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§
Sourcefn vendor_name(&self) -> &'static str
fn vendor_name(&self) -> &'static str
Vendor short name used in error messages and the TOML registry
(e.g. "damiao").
Sourcefn supports(&self, motor_type: MotorTypeId) -> bool
fn supports(&self, motor_type: MotorTypeId) -> bool
True iff this codec can encode commands for and decode events from the supplied motor type.
Sourcefn limits(&self, motor_type: MotorTypeId) -> Result<Limits, CodecError>
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).
Sourcefn bind_to_bus(&mut self, caps: BusCapabilities)
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.
Sourcefn encode_enable(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>
fn encode_enable(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>
Encode the lifecycle “enable motor” command.
Sourcefn encode_disable(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>
fn encode_disable(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>
Encode the lifecycle “disable motor” command.
Sourcefn encode_set_zero(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>
fn encode_set_zero(&self, motor: MotorRef<'_>) -> Result<CanFrame, CodecError>
Encode the lifecycle “set this position as zero” command.
Sourcefn encode_command(
&self,
motor: MotorRef<'_>,
cmd: &Command,
) -> Result<CanFrame, CodecError>
fn encode_command( &self, motor: MotorRef<'_>, cmd: &Command, ) -> Result<CanFrame, CodecError>
Encode a control-mode command.
Sourcefn decode(&self, frame: &CanFrame) -> Result<Option<Event>, CodecError>
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§
Sourcefn encode_refresh(
&self,
motor: MotorRef<'_>,
) -> Result<Option<CanFrame>, CodecError>
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.
Sourcefn encode_set_mode(
&self,
motor: MotorRef<'_>,
mode: CommandKind,
) -> Result<Option<CanFrame>, CodecError>
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.
Sourcefn encode_control_mode_readback(
&self,
motor: MotorRef<'_>,
) -> Result<Option<CanFrame>, CodecError>
fn encode_control_mode_readback( &self, motor: MotorRef<'_>, ) -> Result<Option<CanFrame>, CodecError>
Encode a private control-mode read-back query, when supported.
Sourcefn decode_control_mode_readback(
&self,
frame: &CanFrame,
motor: MotorRef<'_>,
) -> Result<Option<u32>, CodecError>
fn decode_control_mode_readback( &self, frame: &CanFrame, motor: MotorRef<'_>, ) -> Result<Option<u32>, CodecError>
Decode a private control-mode read-back response for motor.