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§
- Reply
Kind - 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,
0xFFin 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..=32767maps 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 aReplyKind::Queryreply. - drive_
floor - The longest a wheel may go between drive frames before it coasts: the
period of the
DRIVE_HZ_MINfloor. 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_BYTEin the brake position. - frame_
current - Current drive frame.
valueis clamped toCUR_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/0x64frame or the broadcast ID query): the encode-side inverse ofparse_feedbackforReplyKind::Drive. - frame_
feedback - Feedback query frame: the addressed motor replies with telemetry.
- frame_
from_ bytes - Build a
Framefrom 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_modedoes so). - frame_
position - Position drive frame.
rawis clamped to0..=POS_MAX(0°..360°). - frame_
query_ reply - Encode a synthetic query-layout reply frame (the layout elicited by a
CMD_QUERY/0x74request): the encode-side inverse ofparse_feedbackforReplyKind::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_idhandles both). - frame_
time - Time on the wire for one
FRAME_LEN-byte frame atBAUD— 8N1 sends 10 bits per byte (1 start + 8 data + 1 stop). This is the unit every bus-occupancy budget is built from (seecrate::bus::bus_period); the same wire time that sizesDEFAULT_MIN_GAP. - frame_
velocity - Velocity drive frame.
rpmis clamped toRPM_MIN..=RPM_MAX. - parse_
feedback - Parse a telemetry frame from raw reply bytes, decoding bytes 6–7
according to
kind— seeReplyKindfor 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 decodedFeedbackwithcrc_ok == falsebecomesNone. - raw8_
to_ deg - 8-bit position → degrees (
raw × 360 / 255), as carried by aReplyKind::Queryreply. - raw_
to_ amps - Raw current value → amps (
raw × 8 / 32767). - raw_
to_ deg - 16-bit position → degrees (
raw × 360 / 32767), as carried by aReplyKind::Drivereply. - validate_
id - Check that
idis an assignable motor ID (0x01..=0xFE).
Type Aliases§
- Frame
- A complete 10-byte bus frame.