30 lines
607 B
Rust
30 lines
607 B
Rust
use rppal::gpio::OutputPin;
|
|
use rppal::pwm::Pwm;
|
|
|
|
pub struct Motor {
|
|
pub in1: OutputPin,
|
|
pub in2: OutputPin,
|
|
pub pwm: Pwm,
|
|
}
|
|
|
|
|
|
impl Motor {
|
|
pub fn forward(&mut self, speed: f64) {
|
|
self.in1.set_high();
|
|
self.in2.set_low();
|
|
self.pwm.set_duty_cycle(speed).unwrap();
|
|
}
|
|
|
|
pub fn backward(&mut self, speed: f64) {
|
|
self.in1.set_low();
|
|
self.in2.set_high();
|
|
self.pwm.set_duty_cycle(speed).unwrap();
|
|
}
|
|
|
|
pub fn stop(&mut self) {
|
|
self.in1.set_low();
|
|
self.in2.set_low();
|
|
self.pwm.set_duty_cycle(0.0).unwrap();
|
|
}
|
|
}
|