| Crates.io | motor-driver-hal |
| lib.rs | motor-driver-hal |
| version | 0.1.3 |
| created_at | 2025-08-12 09:07:00.989566+00 |
| updated_at | 2025-09-01 06:11:43.502799+00 |
| description | motor-driver using embedded-hal for linux |
| homepage | https://github.com/garfield0xff |
| repository | https://github.com/garfield0xff/motor-driver-hal |
| max_upload_size | |
| id | 1791723 |
| size | 84,396 |
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.
[dependencies]
motor-driver-hal = "0.1.3"
For Raspberry Pi projects:
[dependencies]
motor-driver-hal = { version = "0.1.3", features = ["rppal"] }
For Linux GPIO projects:
[dependencies]
motor-driver-hal = { version = "0.1.3", features = ["linux-embedded-hal"] }
For embedded/no_std environments:
[dependencies]
motor-driver-hal = { version = "0.1.3", default-features = false }
use motor_driver_hal::driver::rppal::RppalMotorDriverBuilder;
use motor_driver_hal::MotorDriver;
use rppal::gpio::Gpio;
use rppal::pwm::Channel;
// Initialize GPIO interface
let gpio = Gpio::new()?;
// Create motor driver with builder pattern
let mut motor = RppalMotorDriverBuilder::new_rppal()
.with_dual_gpio_enable(&gpio, 23, 24)? // Enable pins
.with_dual_pwm_channels( // PWM configuration:
Channel::Pwm1, // - Channel1
Channel::Pwm2, // - Channel2
1000.0, // - Frequency
1000 // - Max duty
)?
.with_encoder_pins(&gpio, 25, 8)? // Encoder pins
.with_ppr(1000) // Pulses per revolution: 1000
.build_and_init()?;
// Control the motor
motor.enable()?;
motor.set_speed(300)?; // 30% forward speed
motor.set_speed(-300)?; // 30% reverse speed
motor.stop()?;
motor.disable()?;
use motor_driver_hal::driver::linux::LinuxMotorDriverBuilder;
use motor_driver_hal::MotorDriver;
use linux_embedded_hal::gpio_cdev::Chip;
// Initialize GPIO chip
let mut chip = Chip::new("/dev/gpiochip0")?;
// Create motor driver with builder pattern
let mut motor = LinuxMotorDriverBuilder::new_linux()
.with_dual_gpio_enable(&mut chip, 23, 24)? // Enable pins: GPIO 23, 24
.with_dual_pwm_channels( // PWM configuration:
0, // - PWM chip 0
0, 1, // - Channels 0, 1
1000 // - Max duty: 1000
)
.build_and_init()?;
// Control the motor
motor.enable()?;
motor.set_speed(300)?; // 30% forward speed
motor.stop()?;
motor.disable()?;
The example/ directory contains practical Raspberry Pi implementations:
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# Navigate to examples directory
cd example/
# Run Raspberry Pi examples with rppal feature
cargo run --features rppal --bin rpi_basic_motor
cargo run --features rppal --bin rpi_speed_control
cargo run --features rppal --bin rpi_encoder_monitor
# Run Linux examples with linux-embedded-hal feature
cargo run --features linux-embedded-hal --bin linux_basic_motor
cargo run --features linux-embedded-hal --bin linux_speed_control
Note: Examples require appropriate hardware with proper GPIO connections.
MotorDriverAll motor drivers implement the MotorDriver trait:
pub trait MotorDriver {
type Error;
// Initialization and control
fn initialize(&mut self) -> Result<(), Self::Error>;
fn enable(&mut self) -> Result<(), Self::Error>;
fn disable(&mut self) -> Result<(), Self::Error>;
// Speed and direction control
fn set_speed(&mut self, speed: i16) -> Result<(), Self::Error>;
fn set_direction(&mut self, forward: bool) -> Result<(), Self::Error>;
fn stop(&mut self) -> Result<(), Self::Error>;
fn brake(&mut self) -> Result<(), Self::Error>;
// Encoder support
fn set_ppr(&mut self, ppr: i16) -> Result<bool, Self::Error>;
fn check_ppr(&mut self) -> Result<(), Self::Error>;
// Get Status
fn get_speed(&self) -> Result<i16, Self::Error>;
fn get_direction(&self) -> Result<bool, Self::Error>;
fn get_current(&self) -> Result<f32, Self::Error>;
fn get_voltage(&self) -> Result<f32, Self::Error>;
fn get_temperature(&self) -> Result<f32, Self::Error>;
fn get_fault_status(&self) -> Result<u8, Self::Error>;
}
Speed is controlled using signed 16-bit integers:
For motors with encoders:
This crate provides wrapper types to adapt platform-specific implementations to embedded-hal traits:
GpioWrapper - Wraps GPIO pins implementing OutputPinPwmWrapper - Wraps PWM channels implementing SetDutyCyclerppal crate - included wrappers)linux-embedded-hal - optional feature)esp-hal - bring your own wrappers) Testing in progressstm32-hal family - bring your own wrappers) Testing in progressembedded-hal support Testing in progressEnable platform-specific features in your Cargo.toml:
# For Raspberry Pi
motor-driver-hal = { version = "0.1.0", features = ["rppal"] }
# For Linux GPIO
motor-driver-hal = { version = "0.1.0", features = ["linux-embedded-hal"] }
# For no_std embedded systems
motor-driver-hal = { version = "0.1.0", default-features = false }
Licensed under either of
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.