ev3dev-lang-rust 0.8.0

Rust language bindings for ev3dev
Documentation

Rust language bindings for ev3dev

Build Status Latest version

Notice

Currently this project is not compatible with the BrickPi platform.

Usage

extern crate ev3dev_lang_rust;

use ev3dev_lang_rust::Ev3Result;
use ev3dev_lang_rust::motors::{LargeMotor, MotorPort};
use ev3dev_lang_rust::sensors::ColorSensor;

fn main() -> Ev3Result<()> {

    // Get large motor on port outA.
    let large_motor = LargeMotor::get(MotorPort::OutA)?;

    // Set command "run-direct".
    large_motor.run_direct()?;

    // Run motor.
    large_motor.set_duty_cycle_sp(50)?;

    // Find color sensor. Always returns the first recognised one.
    let color_sensor = ColorSensor::find()?;

    // Switch to rgb mode.
    color_sensor.set_mode_rgb_raw()?;

    // Get current rgb color tuple.
    println!("Current rgb color: {:?}", color_sensor.get_rgb()?);

    Ok(())
}

Cross compile for ev3 brick

  1. Create target configuration .cargo/config

    [target.armv5te-unknown-linux-gnueabi]
    linker = "/usr/bin/arm-linux-gnueabi-gcc"
    
  2. Create Dockerfile

    FROM debian:stretch
    
    RUN dpkg --add-architecture armel
    RUN apt update
    
    # Fix debian package alias
    RUN sed -i "s#deb http://security.debian.org/debian-security stretch/updates main#deb http://deb.debian.org/debian-security stretch/updates main#g" /etc/apt/sources.list
    
    # Install curl for rust installation
    # Install g++ as buildscript compiler
    # Install g++-arm-linux-gnueabi as cross compiler
    RUN apt --yes install curl g++ g++-arm-linux-gnueabi crossbuild-essential-armel
    
    # Instull rust for host platform
    RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
    
    ENV PATH "$PATH:/root/.cargo/bin"
    
    # Add stdlib for target platform
    RUN rustup target add armv5te-unknown-linux-gnueabi
    
    # docker run -it --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust-cross
    # cargo build --release --target armv5te-unknown-linux-gnueabi
    
  3. Build docker image

    docker build . -t pixix4/ev3dev-rust-cross --no-cache
    
  4. Start docker image

    docker run -it --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust-cross
    
  5. Build binary for ev3dev

    cargo build --release --target armv5te-unknown-linux-gnueabi
    

    The --release flag is optional. However, it can speedup the execution time by a factor of 30.

The target binary is now in target/armv5te-unknown-linux-gnueabi/release/{application_name}