moteus 0.5.0

Rust client library for moteus brushless motor controllers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// Copyright 2026 mjbots Robotic Systems, LLC.  info@mjbots.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! FdCanUSB transport for communicating with moteus controllers.
//!
//! The fdcanusb is a USB-to-CAN-FD adapter that uses a simple text protocol:
//! - Send: `can send AAAA HEXDATA FLAGS\n`
//! - Receive: `rcv AAAA HEXDATA FLAGS`
//! - OK response after each send

use crate::error::{Error, Result};
use crate::transport::device::{TransportDevice, TransportDeviceInfo};
use crate::transport::transaction::{dispatch_frame, Request};
use moteus_protocol::CanFdFrame;
use std::io::{BufRead, BufReader};
use std::time::{Duration, Instant};

/// FdCanUSB text protocol encoder/decoder.
pub struct FdcanusbProtocol;

impl FdcanusbProtocol {
    /// Encodes a CAN-FD frame into the fdcanusb text protocol format.
    ///
    /// Returns a string like: `can send 8001 0123456789ABCDEF bF\n`
    pub fn encode_frame(frame: &CanFdFrame, disable_brs: bool) -> String {
        let hex_data = Self::hexify(&frame.data[..frame.size as usize]);

        // Pad to valid CAN-FD DLC size
        let on_wire_size = CanFdFrame::round_up_dlc(frame.size as usize);
        let padding = "50".repeat(on_wire_size - frame.size as usize);

        // Build flags
        let mut flags = String::new();
        if frame.brs_enabled() && !disable_brs {
            flags.push('B');
        } else {
            flags.push('b');
        }
        if frame.fdcan_enabled() {
            flags.push('F');
        }

        format!(
            "can send {:04x} {}{} {}\n",
            frame.arbitration_id, hex_data, padding, flags
        )
    }

    /// Parses a received frame line from fdcanusb.
    ///
    /// Input format: `rcv AAAA HEXDATA [E] [B] [F]`
    ///
    /// When the servo responds with no data, the line has an empty data
    /// field: `rcv 0100  e B F`.  Split on single space (matching
    /// Python's `str.split(' ')`) so the empty field is preserved.
    pub fn parse_frame(line: &str) -> Option<CanFdFrame> {
        let parts: Vec<&str> = line.trim().split(' ').collect();
        if parts.len() < 3 || parts[0] != "rcv" {
            return None;
        }

        let arbitration_id = u32::from_str_radix(parts[1], 16).ok()?;
        let data = Self::dehexify(parts[2])?;

        let mut frame = CanFdFrame::new();
        frame.arbitration_id = arbitration_id;
        frame.size = data.len() as u8;
        frame.data[..data.len()].copy_from_slice(&data);

        // Parse flags
        for flag in parts.iter().skip(3) {
            match *flag {
                "E" => frame.arbitration_id |= 0x80000000, // Extended ID marker
                "B" => frame.set_brs(true),
                "F" => frame.set_fdcan(true),
                _ => {}
            }
        }

        Some(frame)
    }

    /// Checks if a line is an OK response.
    pub fn is_ok_response(line: &str) -> bool {
        line.trim().starts_with("OK")
    }

    /// Checks if a line is an error response.
    pub fn is_error_response(line: &str) -> bool {
        let trimmed = line.trim();
        !trimmed.is_empty() && !trimmed.starts_with("OK") && !trimmed.starts_with("rcv")
    }

    /// Converts bytes to hex string.
    fn hexify(data: &[u8]) -> String {
        use std::fmt::Write;
        let mut s = String::with_capacity(data.len() * 2);
        for b in data {
            write!(s, "{:02X}", b).unwrap();
        }
        s
    }

    /// Converts hex string to bytes.
    fn dehexify(hex: &str) -> Option<Vec<u8>> {
        let hex = hex.trim();
        if hex.len() % 2 != 0 {
            return None;
        }

        (0..hex.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).ok())
            .collect()
    }
}

/// FdCanUSB transport implementation.
///
/// This provides a synchronous interface for communicating with moteus
/// controllers through an fdcanusb device.
pub struct FdcanusbDevice<S: std::io::Read + std::io::Write> {
    reader: BufReader<S>,
    writer: S,
    timeout: Duration,
    disable_brs: bool,
    line_buffer: String,
    /// Buffer for frames received during wait_for_ok.
    pending_frames: Vec<CanFdFrame>,
    /// Device info for TransportDevice trait.
    pub(crate) info: TransportDeviceInfo,
}

impl<S: std::io::Read + std::io::Write> std::fmt::Debug for FdcanusbDevice<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FdcanusbDevice")
            .field("info", &self.info)
            .field("timeout", &self.timeout)
            .field("disable_brs", &self.disable_brs)
            .field("pending_frames", &self.pending_frames.len())
            .finish()
    }
}

impl<S: std::io::Read + std::io::Write + Clone> FdcanusbDevice<S> {
    /// Creates an FdCanUSB transport from a pre-opened stream.
    ///
    /// This is primarily useful for testing with mock streams.
    /// For normal use, prefer [`FdcanusbDevice::new`] which opens the serial port
    /// directly from a device path.
    pub fn from_stream(stream: S) -> Self {
        let reader = BufReader::new(stream.clone());
        FdcanusbDevice {
            reader,
            writer: stream,
            timeout: crate::transport::factory::DEFAULT_TIMEOUT,
            disable_brs: false,
            line_buffer: String::new(),
            pending_frames: Vec::new(),
            info: TransportDeviceInfo::new(0, "Fdcanusb"),
        }
    }

    /// Creates an FdCanUSB transport from a pre-opened stream with options.
    ///
    /// This is primarily useful for testing with mock streams.
    /// For normal use, prefer [`FdcanusbDevice::new`] which opens the serial port
    /// directly from a device path.
    pub fn from_stream_with_options(stream: S, timeout: Duration, disable_brs: bool) -> Self {
        let reader = BufReader::new(stream.clone());
        FdcanusbDevice {
            reader,
            writer: stream,
            timeout,
            disable_brs,
            line_buffer: String::new(),
            pending_frames: Vec::new(),
            info: TransportDeviceInfo::new(0, "Fdcanusb"),
        }
    }

    /// Enables or disables bit rate switching.
    pub fn set_disable_brs(&mut self, disable: bool) {
        self.disable_brs = disable;
    }

    /// Writes a frame without waiting for OK response.
    fn write_frame(&mut self, frame: &CanFdFrame) -> Result<()> {
        let cmd = FdcanusbProtocol::encode_frame(frame, self.disable_brs);
        self.writer.write_all(cmd.as_bytes())?;
        Ok(())
    }

    /// Sends a single frame and waits for OK response.
    fn send_frame(&mut self, frame: &CanFdFrame) -> Result<()> {
        self.write_frame(frame)?;
        self.writer.flush()?;
        self.wait_for_ok()?;
        Ok(())
    }

    /// Waits for an OK response.
    /// Any received frames (rcv lines) are buffered for later retrieval.
    fn wait_for_ok(&mut self) -> Result<()> {
        let deadline = Instant::now() + self.timeout;

        loop {
            if Instant::now() > deadline {
                return Err(Error::Timeout);
            }

            self.line_buffer.clear();
            match self.reader.read_line(&mut self.line_buffer) {
                Ok(0) => continue, // No data yet
                Ok(_) => {
                    let line = self.line_buffer.trim();
                    if FdcanusbProtocol::is_ok_response(line) {
                        return Ok(());
                    }
                    if FdcanusbProtocol::is_error_response(line) {
                        return Err(Error::Protocol(format!("fdcanusb error: {}", line)));
                    }
                    // Save any received frames for later retrieval
                    if let Some(frame) = FdcanusbProtocol::parse_frame(&self.line_buffer) {
                        self.pending_frames.push(frame);
                    }
                }
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => continue,
                Err(e) => return Err(Error::Io(e)),
            }
        }
    }

    /// Receives frames with timeout.
    fn receive_frames(&mut self, expected_count: usize) -> Result<Vec<CanFdFrame>> {
        // First, drain any frames that were buffered during wait_for_ok
        let mut frames: Vec<CanFdFrame> = self.pending_frames.drain(..).collect();

        if frames.len() >= expected_count {
            return Ok(frames);
        }

        let deadline = Instant::now() + self.timeout;

        while frames.len() < expected_count {
            if Instant::now() > deadline {
                break; // Timeout - return what we have
            }

            self.line_buffer.clear();
            match self.reader.read_line(&mut self.line_buffer) {
                Ok(0) => continue,
                Ok(_) => {
                    if let Some(frame) = FdcanusbProtocol::parse_frame(&self.line_buffer) {
                        frames.push(frame);
                    }
                }
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => continue,
                Err(e) => return Err(Error::Io(e)),
            }
        }

        Ok(frames)
    }
}

impl<S: std::io::Read + std::io::Write + Clone + Send> TransportDevice for FdcanusbDevice<S> {
    fn transaction(&mut self, requests: &mut [Request]) -> Result<()> {
        debug_assert!(
            requests.iter().all(|r| r.child_device.is_none()),
            "FdcanusbDevice does not support child devices"
        );

        // Pipeline: write all frames first
        let mut frames_sent = 0usize;
        for req in requests.iter() {
            if let Some(frame) = &req.frame {
                self.write_frame(frame)?;
                frames_sent += 1;
            }
        }

        // Single flush for all frames
        if frames_sent > 0 {
            self.writer.flush()?;
        }

        // Wait for all OKs
        for _ in 0..frames_sent {
            self.wait_for_ok()?;
        }

        // Calculate expected replies
        let expected: usize = Request::total_expected_replies(requests);

        // Receive responses and dispatch to matching requests
        if expected > 0 {
            let responses = self.receive_frames(expected)?;
            for frame in responses {
                dispatch_frame(&frame, requests);
            }
        }

        Ok(())
    }

    fn write(&mut self, frame: &CanFdFrame) -> Result<()> {
        self.send_frame(frame)
    }

    fn read(&mut self) -> Result<Option<CanFdFrame>> {
        // Check pending frames first
        if let Some(frame) = self.pending_frames.pop() {
            return Ok(Some(frame));
        }

        // Try to read one frame with a short timeout
        let deadline = Instant::now() + self.timeout;

        while Instant::now() < deadline {
            self.line_buffer.clear();
            match self.reader.read_line(&mut self.line_buffer) {
                Ok(0) => continue,
                Ok(_) => {
                    if let Some(frame) = FdcanusbProtocol::parse_frame(&self.line_buffer) {
                        return Ok(Some(frame));
                    }
                }
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => continue,
                Err(e) => return Err(Error::Io(e)),
            }
        }

        Ok(None)
    }

    fn flush(&mut self) -> Result<()> {
        // Clear pending frames
        self.pending_frames.clear();

        // Drain any incoming data for a short period
        let deadline = Instant::now() + Duration::from_millis(50);

        while Instant::now() < deadline {
            self.line_buffer.clear();
            match self.reader.read_line(&mut self.line_buffer) {
                Ok(0) => break,    // No more data
                Ok(_) => continue, // Discard
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
                Err(_) => break,
            }
        }

        Ok(())
    }

    fn info(&self) -> &TransportDeviceInfo {
        &self.info
    }

    fn set_timeout(&mut self, timeout: Duration) {
        self.timeout = timeout;
    }

    fn timeout(&self) -> Duration {
        self.timeout
    }
}

// =============================================================================
// Linux serial port
// =============================================================================

/// Linux serial port implementation for fdcanusb.
#[cfg(target_os = "linux")]
mod linux_serial {
    use std::fs::OpenOptions;
    use std::io::{Read, Result, Write};
    use std::os::raw::{c_int, c_void};
    use std::os::unix::io::{AsRawFd, RawFd};

    // termios constants
    const NCCS: usize = 32;
    const VMIN: usize = 6;
    const VTIME: usize = 5;
    const TCSANOW: c_int = 0;
    const TCIOFLUSH: c_int = 2;

    // c_iflag bits
    const IGNBRK: u32 = 0o000001;
    const BRKINT: u32 = 0o000002;
    const PARMRK: u32 = 0o000010;
    const ISTRIP: u32 = 0o000040;
    const INLCR: u32 = 0o000100;
    const IGNCR: u32 = 0o000200;
    const ICRNL: u32 = 0o000400;
    const IXON: u32 = 0o002000;

    // c_oflag bits
    const OPOST: u32 = 0o000001;

    // c_lflag bits
    const ECHO: u32 = 0o000010;
    const ECHONL: u32 = 0o000100;
    const ICANON: u32 = 0o000002;
    const ISIG: u32 = 0o000001;
    const IEXTEN: u32 = 0o100000;

    // c_cflag bits
    const CSIZE: u32 = 0o000060;
    const PARENB: u32 = 0o000400;
    const CS8: u32 = 0o000060;

    #[repr(C)]
    struct Termios {
        c_iflag: u32,
        c_oflag: u32,
        c_cflag: u32,
        c_lflag: u32,
        c_line: u8,
        c_cc: [u8; NCCS],
        c_ispeed: u32,
        c_ospeed: u32,
    }

    extern "C" {
        fn tcgetattr(fd: c_int, termios: *mut Termios) -> c_int;
        fn tcsetattr(fd: c_int, optional_actions: c_int, termios: *const Termios) -> c_int;
        fn tcflush(fd: c_int, queue_selector: c_int) -> c_int;
        fn dup(fd: c_int) -> c_int;
    }

    /// A simple serial port wrapper for Linux.
    pub struct LinuxSerialPort {
        fd: RawFd,
    }

    impl LinuxSerialPort {
        /// Opens a serial port with appropriate settings for fdcanusb.
        pub fn open(path: &str) -> Result<Self> {
            let file = OpenOptions::new().read(true).write(true).open(path)?;

            let fd = file.as_raw_fd();

            // Configure the serial port using termios
            unsafe {
                let mut termios: Termios = std::mem::zeroed();
                if tcgetattr(fd, &mut termios) != 0 {
                    return Err(std::io::Error::last_os_error());
                }

                // Raw mode - disable canonical processing, echo, signals
                termios.c_iflag &=
                    !(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
                termios.c_oflag &= !OPOST;
                termios.c_lflag &= !(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
                termios.c_cflag &= !(CSIZE | PARENB);
                termios.c_cflag |= CS8;

                // Non-blocking reads: VMIN=0 means return immediately
                // VTIME=1 means 0.1s inter-character timeout
                termios.c_cc[VMIN] = 0;
                termios.c_cc[VTIME] = 1;

                if tcsetattr(fd, TCSANOW, &termios) != 0 {
                    return Err(std::io::Error::last_os_error());
                }

                // Flush any pending data
                tcflush(fd, TCIOFLUSH);
            }

            // Transfer ownership of fd - don't let File close it
            std::mem::forget(file);

            Ok(LinuxSerialPort { fd })
        }
    }

    impl Clone for LinuxSerialPort {
        fn clone(&self) -> Self {
            let new_fd = unsafe { dup(self.fd) };
            LinuxSerialPort { fd: new_fd }
        }
    }

    impl Drop for LinuxSerialPort {
        fn drop(&mut self) {
            unsafe {
                extern "C" {
                    fn close(fd: c_int) -> c_int;
                }
                close(self.fd);
            }
        }
    }

    impl Read for LinuxSerialPort {
        fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
            let n = unsafe {
                extern "C" {
                    fn read(fd: c_int, buf: *mut c_void, count: usize) -> isize;
                }
                read(self.fd, buf.as_mut_ptr() as *mut c_void, buf.len())
            };
            if n < 0 {
                Err(std::io::Error::last_os_error())
            } else {
                Ok(n as usize)
            }
        }
    }

    impl Write for LinuxSerialPort {
        fn write(&mut self, buf: &[u8]) -> Result<usize> {
            let n = unsafe {
                extern "C" {
                    fn write(fd: c_int, buf: *const c_void, count: usize) -> isize;
                }
                write(self.fd, buf.as_ptr() as *const c_void, buf.len())
            };
            if n < 0 {
                Err(std::io::Error::last_os_error())
            } else {
                Ok(n as usize)
            }
        }

        fn flush(&mut self) -> Result<()> {
            // For serial ports, write is typically unbuffered
            Ok(())
        }
    }
}

#[cfg(target_os = "linux")]
use linux_serial::LinuxSerialPort;

#[cfg(target_os = "linux")]
impl FdcanusbDevice<LinuxSerialPort> {
    /// Opens an fdcanusb device at the given serial port path.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use moteus::transport::fdcanusb::FdcanusbDevice;
    ///
    /// fn main() -> Result<(), moteus::Error> {
    ///     let fdcanusb = FdcanusbDevice::new("/dev/ttyACM0")?;
    ///     Ok(())
    /// }
    /// ```
    pub fn new(path: &str) -> Result<Self> {
        let port = LinuxSerialPort::open(path)?;
        Ok(Self::from_stream(port))
    }

    /// Opens an fdcanusb device with explicit timeout and BRS settings.
    pub fn with_options(path: &str, timeout: Duration, disable_brs: bool) -> Result<Self> {
        let port = LinuxSerialPort::open(path)?;
        Ok(Self::from_stream_with_options(port, timeout, disable_brs))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hexify() {
        assert_eq!(FdcanusbProtocol::hexify(&[0x01, 0x23, 0xAB]), "0123AB");
        assert_eq!(FdcanusbProtocol::hexify(&[]), "");
    }

    #[test]
    fn test_dehexify() {
        assert_eq!(
            FdcanusbProtocol::dehexify("0123AB"),
            Some(vec![0x01, 0x23, 0xAB])
        );
        assert_eq!(FdcanusbProtocol::dehexify(""), Some(vec![]));
        assert_eq!(FdcanusbProtocol::dehexify("0"), None); // Odd length
    }

    #[test]
    fn test_encode_frame() {
        let mut frame = CanFdFrame::new();
        frame.arbitration_id = 0x8001;
        frame.data[0..3].copy_from_slice(&[0x01, 0x00, 0x0A]);
        frame.size = 3;
        frame.set_brs(true);
        frame.set_fdcan(true);

        let encoded = FdcanusbProtocol::encode_frame(&frame, false);
        assert!(encoded.starts_with("can send 8001 01000A"));
        assert!(encoded.contains("BF"));
    }

    #[test]
    fn test_parse_frame() {
        let line = "rcv 8001 01000A0000000000 B F";
        let frame = FdcanusbProtocol::parse_frame(line).unwrap();

        assert_eq!(frame.arbitration_id, 0x8001);
        assert_eq!(frame.data[0], 0x01);
        assert_eq!(frame.data[1], 0x00);
        assert_eq!(frame.data[2], 0x0A);
        assert!(frame.brs_enabled());
        assert!(frame.fdcan_enabled());
    }

    #[test]
    fn test_round_up_dlc() {
        assert_eq!(CanFdFrame::round_up_dlc(0), 0);
        assert_eq!(CanFdFrame::round_up_dlc(8), 8);
        assert_eq!(CanFdFrame::round_up_dlc(9), 12);
        assert_eq!(CanFdFrame::round_up_dlc(12), 12);
        assert_eq!(CanFdFrame::round_up_dlc(13), 16);
        assert_eq!(CanFdFrame::round_up_dlc(33), 48);
        assert_eq!(CanFdFrame::round_up_dlc(49), 64);
    }

    #[test]
    fn test_is_ok_response() {
        assert!(FdcanusbProtocol::is_ok_response("OK\n"));
        assert!(FdcanusbProtocol::is_ok_response("OK"));
        assert!(FdcanusbProtocol::is_ok_response("  OK  "));
        assert!(!FdcanusbProtocol::is_ok_response("rcv 8001 00"));
    }
}