This crate provides control over the DRV8833 Dual H-Bridge Motor Driver.
The crate requires no external or standard library and only depends on the esp-hal crate. The code uses the espressif LEDC peripheral to control the DRV8833.
The crate uses the slow clock as default:
- It is better suited for motor control, since the frenquency is quite low, e.g. < 20kHz.
- It is more power efficient.
- It can still work under sleep modes.
The followig example shows how to use the crate to drive a brushed motor forward with max duty cycle (100%), using the GPIO1 and GPIO2:
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
let mut ledc = Ledc::new(peripherals.LEDC);
ledc.set_global_slow_clock(LSGlobalClkSource::APBClk);
let motor_conf = MotorTimerConfig::new(
&ledc,
timer::Number::Timer0,
timer::config::Duty::Duty12Bit,
Rate::from_khz(1),
)?;
let motor: MotorFastDecay = Motor::new(
&ledc,
&motor_conf,
MotorLink::new(channel::Number::Channel0, peripherals.GPIO1),
MotorLink::new(channel::Number::Channel1, peripherals.GPIO2)
)?;
motor.forward(100)?;motor.backward(50)?;motor.brake()?;let motor: MotorSlowDecay = Motor::new(
&ledc,
&motor_conf,
MotorLink::new(channel::Number::Channel0, peripherals.GPIO1),
MotorLink::new(channel::Number::Channel1, peripherals.GPIO2)
)?;// A channel number from 0-7;
let motor_right: MotorFastDecay = Motor::new(
&ledc,
&motor_timer_conf,
MotorLink::new(channel::Number::Channel0, peripherals.GPIO1),
MotorLink::new(channel::Number::Channel1, peripherals.GPIO2),
)?;
let motor_left: MotorFastDecay = Motor::new(
&ledc,
&motor_timer_conf,
MotorLink::new(channel::Number::Channel2, peripherals.GPIO3),
MotorLink::new(channel::Number::Channel3, peripherals.GPIO4),
)?;let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
let mut stepper = Stepper::new(
peripherals.GPIO0,
peripherals.GPIO1,
peripherals.GPIO10,
peripherals.GPIO9,
Rate::from_hz(200), // 200hz, i.e. 5ms
200, // steps per rev
);
let delay = Delay::new();
loop {
stepper.angle(30.0, &delay);
}cargo build --example forward --features esp32c6