[][src]Crate saberrs

saberrs is a library for interfacing with Dimension Engineering Sabertooth motor driver.

Currently only the Sabertooth 2x32 is supported.

Simple usage

use saberrs::sabertooth2x32::{Sabertooth2x32, PacketSerial};

// Create a handle. This will use "PacketSerial" protocol.
let mut saber = PacketSerial::new("/dev/ttyS0")?;

// Go forward at half-speed (50.0%)
saber.set_drive(0.5)?;
saber.set_turn(0.0)?;

// Request the battery voltage from motor 1.
let vbat : f32 = saber.get_voltage(1)?;

// Stop the motors
saber.stop_motors()?;

Other protocol variants can be used:

use saberrs::sabertooth2x32::{Sabertooth2x32, PacketSerial, PacketType, PlainText};

// "PacketSerial" with specified address and frame protection type.
let mut saber = PacketSerial::new("/dev/ttyS0")?
    .with_packet_type(PacketType::Checksum)
    .with_address(129);

// "PlainText" protocol
let mut sabertext = PlainText::new("/dev/ttyS1")?;

Customizing the IO: the SabertoothSerial trait

The handles rely on the trait SabertoothSerial, which abstract the low-level IO communication with the device.

By default, the library provides SabertoothPort and SabertoothPortShared. In most cases the application writer shouldn't need to care about those, but they may be used for applying custom baud rates or timeout values for example.

SabertoothSerial can be implemented manually for even more customization. For example stubs can be implemented for debugging purpose:

use std::time::Duration;
use std::io::{self, Read, Write};
use saberrs::{SabertoothSerial};

struct SerialStub {
    timeout: Duration,
    baudrate: u32,
}

impl SerialStub {
    pub fn new() -> Self {
        SerialStub {
            timeout: Duration::from_millis(100),
            baudrate: 9600,
        }
    }
}

impl Read for SerialStub {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        println!("SerialStub.read()");
        Ok(0)
    }
}

impl Write for SerialStub {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        println!("SerialStub.write({:?})", buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> { Ok(()) }
}

impl SabertoothSerial for SerialStub {
    fn set_timeout(&mut self, timeout: Duration) -> saberrs::Result<()> {
        println!("SerialStub.set_timeout({:?})", timeout);
        self.timeout = timeout;
        Ok(())
    }

    fn timeout(&self) -> Duration {
        println!("SerialStub.timeout() -> {:?}", self.timeout);
        self.timeout
    }

    fn set_baud_rate(&mut self, baud_rate: u32) -> saberrs::Result<()> {
        println!("SerialStub.set_baudrate({})", baud_rate);
        self.baudrate = baud_rate;
        Ok(())
    }

    fn baud_rate(&self) -> saberrs::Result<u32> {
        println!("SerialStub.baud_rate() -> {}", self.baudrate);
        Ok(self.baudrate)
    }

    fn clear_all(&self) -> saberrs::Result<()> { Ok(()) }
}

Features and dependencies

Features:

Dependencies:

Disclaimer

This library is not affiliated or associated in any way with Dimension Engineering.

All product and company names are trademarks or registered trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.

Modules

sabertooth2x32

Interface for the Sabertooth 2x32.

Structs

Error

Error type used in the crate.

SabertoothPort

Raw Sabertooth controller.

SabertoothPortShared

Clonable variant of SabertoothPort.

Enums

ErrorKind

Types of errors.

Traits

SabertoothSerial

Minimal serial port trait.

Type Definitions

Result

Result type used in the crate.