ev3-drivebase
A high-level DriveBase abstraction for LEGO Mindstorms EV3 robots running ev3dev, written in Rust.
This crate provides an easy-to-use interface for controlling two-wheeled robots, similar to the DriveBase classes found in the Python and MicroPython ev3dev libraries.
Features
- Simple Movement Control: Drive forward/backward and turn with precise distance and angle control
- Configurable Brake Modes: Choose between Coast, Brake, and Hold modes for different stopping behaviors
- Acceleration/Deceleration Control: Set custom ramp rates for smooth motion
- In-Place and Arc Turns: Turn on the spot or follow circular paths with specified radii
- Color Sensor Support: Optional integration with color sensors for line-following applications
- Type-Safe API: Leverage Rust's type system for safer robot control
- Strict Linting: Enforces best practices with comprehensive Clippy rules
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use ;
use ;
Understanding Motor Directions
When setting up your drivebase, you need to specify which direction each motor should turn to make the robot move forward. This depends on how your motors are physically mounted:
Direction::Clockwise: Use if the motor shaft points toward the front of the robotDirection::CounterClockwise: Use if the motor shaft points toward the back of the robot
For a typical two-wheeled robot with motors on opposite sides, one motor will be Clockwise and the other CounterClockwise.
Physical Measurements
The drivebase requires two measurements:
- Wheel Diameter: Measure the diameter of your wheels in millimeters
- Axle Track: Measure the distance between the points where the left and right wheels touch the ground in millimeters
These values are critical for accurate distance and turning calculations.
Units
All measurements and parameters use the following units:
- Distances: millimeters (mm)
- Speeds: degrees per second (deg/s) - motor rotation speed, not robot speed
- Angles: degrees
- Acceleration/Deceleration: degrees per second squared (deg/s²)
Core Concepts
Brake Modes
The brake mode determines how motors behave when stopped:
BrakeMode::Coast: Motors freely coast to a stop with no active braking. Lowest power consumption but least precise.BrakeMode::Brake: Motors actively brake to stop quickly but don't hold position afterward.BrakeMode::Hold: Motors actively hold their position after stopping. Most precise but uses more power.
Driving
The drive() method moves the robot forward or backward:
// Drive 500mm forward at 200 deg/s and stop
drivebase.drive?;
// Drive 300mm backward at 150 deg/s and stop
drivebase.drive?;
// Drive forward continuously (don't stop after distance)
drivebase.drive?;
Turning
The turn() method provides two types of turns:
In-Place Turns (robot rotates around its center):
// Turn 90 degrees counter-clockwise
drivebase.turn?;
// Turn 180 degrees clockwise
drivebase.turn?;
Arc Turns (robot follows a circular path):
// Turn 90 degrees with a 200mm radius
drivebase.turn?;
// Sharp 45-degree turn with 50mm radius
drivebase.turn?;
- Positive angles turn counter-clockwise (left)
- Negative angles turn clockwise (right)
- Radius is measured from the turn center to the outer wheel
Acceleration Control
Set how quickly the robot speeds up and slows down:
// Smooth acceleration for delicate tasks
drivebase.set_acceleration?;
drivebase.set_deceleration?;
// Quick response for fast movements
drivebase.set_acceleration?;
drivebase.set_deceleration?;
Recommendations:
- Low (500-1500): Very smooth, good for delicate operations
- Medium (2000-4000): Balanced, good for general use
- High (4000-8000): Fast response, may cause wheel slip
Advanced Features
Color Sensors
Add color sensors for line following:
use ;
let mut drivebase = new?;
let left_sensor = get?;
let right_sensor = get?;
drivebase.add_colorsensor;
// Access sensors through drivebase.left_sensor and drivebase.right_sensor
Status Checks
Monitor the state of your robot:
// Check if motors are running
if drivebase.is_running?
// Check if motors are accelerating
if drivebase.is_ramping?
// Check if motors are holding position
if drivebase.is_holding?
// Check for problems
if drivebase.is_stalled?
if drivebase.is_overloaded?
Manual Control
Stop the robot at any time:
drivebase.stop?; // Stops according to brake mode
Reset all motor parameters:
drivebase.reset?; // Resets and stops motors
Building for EV3
To compile your program for the EV3, you'll need to cross-compile for the ARMv5TE architecture:
Setup
- Add the target:
- Install a linker (if not already available):
# On Ubuntu/Debian
# Or use rust-lld (recommended, no external dependencies)
- Configure Cargo (create
.cargo/config.tomlin your project):
[]
= "armv5te-unknown-linux-musleabi"
[]
= "rust-lld"
Building
Your binary will be in target/armv5te-unknown-linux-musleabi/release/.
Deployment
Transfer the binary to your EV3 and run it:
# Using scp
# SSH into the EV3
# Make executable and run
Tip: Use ev3-runner
For faster development, consider using ev3-runner to automatically upload and run your programs:
# On your EV3, start the server
# From your computer
Examples
Basic Movement
use ;
use ;
Obstacle Avoidance Pattern
use ;
use ;
Smooth Curved Path
use ;
use ;
Documentation
For detailed API documentation, run:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Projects
- ev3dev-lang-rust - The underlying Rust bindings for ev3dev
- ev3-runner - Fast development tool for uploading and running EV3 programs
Acknowledgments
This crate is inspired by the DriveBase implementations in:
- The official ev3dev Python library
- The Pybricks MicroPython library
Support
If you encounter any issues or have questions:
- Open an issue on GitHub
- Check the ev3dev documentation