Skip to main content

Module protocol

Module protocol 

Source
Expand description

Pure protocol layer: frame construction and parsing. No I/O.

Everything here is a deterministic function of its inputs, which is what makes the wire format unit-testable byte-for-byte (see tests/vectors.rs, whose expected bytes are hand-derived from the DFRobot protocol documentation and an independent CRC-8/MAXIM implementation).

Out-of-range drive values are clamped, not rejected — a setpoint that is merely too large should saturate the wheel, never wrap it around to full reverse. This is part of the crate’s API contract.

Enums§

ReplyKind
Which command elicited a telemetry reply — and therefore how its bytes 6–7 must be decoded.

Constants§

BAUD
RS485 baud rate. The format is fixed: 115200 8N1, half-duplex.
BRAKE_BYTE
Brake byte value: in velocity mode, 0xFF in byte 7 engages the electric brake.
CMD_DRIVE
Drive command: the 16-bit value is interpreted per the active Mode.
CMD_HZ_MAX
Maximum command rate the motor accepts (Hz).
CMD_MODE
Mode-switch command. Its last byte is the mode, not a CRC.
CMD_QUERY
Feedback query command: the motor replies with a telemetry frame.
CUR_FULL_SCALE_A
Full-scale torque current in amps, at CUR_MAX (and −CUR_MAX).
CUR_MAX
Maximum current command; maps to roughly +8 A.
CUR_MIN
Minimum current command; maps to roughly −8 A.
DRIVE_HZ_MIN
Minimum drive-frame repetition rate (Hz) that sustains motion.
FRAME_LEN
Every frame on the bus, in both directions, is exactly this long.
POS_MAX
Maximum position command; 0..=32767 maps to 0°..360°.
RPM_MAX
Maximum velocity command, RPM.
RPM_MIN
Minimum velocity command, RPM.

Functions§

amps_to_raw
Amps → raw current setpoint, clamped to CUR_MIN..=CUR_MAX.
crc8_maxim
CRC-8/MAXIM (Dallas 1-Wire): polynomial x⁸+x⁵+x⁴+1, reflected (0x8C), init 0.
deg_to_raw
Degrees → raw position setpoint, clamped to 0..=POS_MAX.
deg_to_raw8
Degrees → raw 8-bit position, clamped to 0..=255, as carried in byte 7 of a ReplyKind::Query reply.
drive_floor
The longest a wheel may go between drive frames before it coasts: the period of the DRIVE_HZ_MIN floor. A periodic control loop’s cycle must not exceed this, or every cycle the motor slips below the floor and coasts a little.
frame_brake
Electric-brake frame (velocity mode only): value 0 with BRAKE_BYTE in the brake position.
frame_current
Current drive frame. value is clamped to CUR_MIN..=CUR_MAX (±32767, roughly −8 A..+8 A).
frame_drive_reply
Encode a synthetic drive-layout reply frame (the layout elicited by a CMD_DRIVE / 0x64 frame or the broadcast ID query): the encode-side inverse of parse_feedback for ReplyKind::Drive.
frame_feedback
Feedback query frame: the addressed motor replies with telemetry.
frame_from_bytes
Build a Frame from raw bytes: 9 bytes get a CRC-8/MAXIM appended, 10 bytes pass through untouched (byte 9 is not recomputed, so this can send deliberately corrupt frames).
frame_id_query
Broadcast ID-query frame (fixed bytes C8 64 00×7 DE). Any motor on the bus answers with a frame starting with its own ID.
frame_mode
Mode-switch frame. The last byte is the mode value, not a CRC — this is the protocol’s one deliberate deviation from the standard frame shape. Must be sent five times (M0601::set_mode does so).
frame_position
Position drive frame. raw is clamped to 0..=POS_MAX (0°..360°).
frame_query_reply
Encode a synthetic query-layout reply frame (the layout elicited by a CMD_QUERY / 0x74 request): the encode-side inverse of parse_feedback for ReplyKind::Query.
frame_set_id
Set-ID frame (AA 55 53 <new_id> 00×6, no CRC). Persistent; must be sent five times with only one motor on the bus (Bus::set_id handles both).
frame_time
Time on the wire for one FRAME_LEN-byte frame at BAUD — 8N1 sends 10 bits per byte (1 start + 8 data + 1 stop). This is the unit every bus-occupancy budget is built from (see crate::bus::bus_period); the same wire time that sizes DEFAULT_MIN_GAP.
frame_velocity
Velocity drive frame. rpm is clamped to RPM_MIN..=RPM_MAX.
parse_feedback
Parse a telemetry frame from raw reply bytes, decoding bytes 6–7 according to kind — see ReplyKind for why the caller must know which command the reply answers.
parse_feedback_strict
Like parse_feedback, but rejects a frame whose byte 9 does not match its CRC-8/MAXIM: a decoded Feedback with crc_ok == false becomes None.
raw8_to_deg
8-bit position → degrees (raw × 360 / 255), as carried by a ReplyKind::Query reply.
raw_to_amps
Raw current value → amps (raw × 8 / 32767).
raw_to_deg
16-bit position → degrees (raw × 360 / 32767), as carried by a ReplyKind::Drive reply.
validate_id
Check that id is an assignable motor ID (0x01..=0xFE).

Type Aliases§

Frame
A complete 10-byte bus frame.