Motor Driver HAL
A hardware abstraction layer (HAL) for motor drivers built on top of embedded-hal traits. This crate provides a generic, platform-independent interface for controlling H-bridge motor drivers commonly used in embedded systems and robotics applications.
Installation
Basic Installation
[]
= "0.1.3"
Platform-Specific Installation
For Raspberry Pi projects:
[]
= { = "0.1.3", = ["rppal"] }
For Linux GPIO projects:
[]
= { = "0.1.3", = ["linux-embedded-hal"] }
For embedded/no_std environments:
[]
= { = "0.1.3", = false }
Quick Start
Raspberry Pi Example (Builder Pattern)
use RppalMotorDriverBuilder;
use MotorDriver;
use Gpio;
use Channel;
// Initialize GPIO interface
let gpio = new?;
// Create motor driver with builder pattern
let mut motor = new_rppal
.with_dual_gpio_enable? // Enable pins
.with_dual_pwm_channels?
.with_encoder_pins? // Encoder pins
.with_ppr // Pulses per revolution: 1000
.build_and_init?;
// Control the motor
motor.enable?;
motor.set_speed?; // 30% forward speed
motor.set_speed?; // 30% reverse speed
motor.stop?;
motor.disable?;
Linux Example (GPIO and PWM)
use LinuxMotorDriverBuilder;
use MotorDriver;
use Chip;
// Initialize GPIO chip
let mut chip = new?;
// Create motor driver with builder pattern
let mut motor = new_linux
.with_dual_gpio_enable? // Enable pins: GPIO 23, 24
.with_dual_pwm_channels
.build_and_init?;
// Control the motor
motor.enable?;
motor.set_speed?; // 30% forward speed
motor.stop?;
motor.disable?;
Examples
The example/ directory contains practical Raspberry Pi implementations:
Available Examples
rpi_basic_motor- Simple Raspberry Pi motor controlrpi_speed_control- Variable speed control on Raspberry Pirpi_direction_control- Forward/reverse direction control on Raspberry Pirpi_brake_test- Motor braking functionality on Raspberry Pirpi_encoder_monitor- Raspberry Pi motor with encoder feedbacklinux_basic_motor- Simple Linux GPIO motor controllinux_speed_control- Variable speed control on Linuxlinux_direction_control- Forward/reverse direction control on Linuxlinux_brake_test- Motor braking functionality on Linux
Running Examples
# Navigate to examples directory
# Run Raspberry Pi examples with rppal feature
# Run Linux examples with linux-embedded-hal feature
Note: Examples require appropriate hardware with proper GPIO connections.
API Overview
Core Trait: MotorDriver
All motor drivers implement the MotorDriver trait:
Speed Values
Speed is controlled using signed 16-bit integers:
- Positive values: Forward direction (0 to max_duty)
- Negative values: Reverse direction (-max_duty to 0)
- Zero: Motor stopped
Motor Control Modes
- Forward: Positive speed values, normal rotation
- Reverse: Negative speed values, opposite rotation
- Brake: Active braking (both PWM channels high for dual PWM)
- Coast: Free spinning (all PWM channels low)
Encoder Features
For motors with encoders:
- Quadrature encoder reading (A/B channels)
- Pulse counting with configurable PPR (Pulses Per Revolution)
- Encoder reset and target pulse positioning
- Real-time pulse monitoring
Hardware Integration
Platform Wrappers
This crate provides wrapper types to adapt platform-specific implementations to embedded-hal traits:
GpioWrapper- Wraps GPIO pins implementingOutputPinPwmWrapper- Wraps PWM channels implementingSetDutyCycle
Supported Platforms
- ✅ Raspberry Pi (via
rppalcrate - included wrappers) - ✅ Linux (via
linux-embedded-hal- optional feature) - 🧪 ESP32 (via
esp-hal- bring your own wrappers) Testing in progress - 🧪 STM32 (via
stm32-halfamily - bring your own wrappers) Testing in progress - 🧪 Any platform with
embedded-halsupport Testing in progress
Configuration Features
Enable platform-specific features in your Cargo.toml:
# For Raspberry Pi
= { = "0.1.0", = ["rppal"] }
# For Linux GPIO
= { = "0.1.0", = ["linux-embedded-hal"] }
# For no_std embedded systems
= { = "0.1.0", = false }
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.