Test 1 (ChatGPT Code)
This commit is contained in:
51
src/main.rs
51
src/main.rs
@@ -1,3 +1,50 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use rppal::gpio::{Gpio, OutputPin};
|
||||
use rppal::pwm::{Pwm, Channel, Polarity};
|
||||
use std::error::Error;
|
||||
|
||||
struct Motor {
|
||||
in1: OutputPin,
|
||||
in2: OutputPin,
|
||||
pwm: Pwm,
|
||||
}
|
||||
|
||||
impl Motor {
|
||||
fn forward(&mut self, speed: f64) {
|
||||
self.in1.set_high();
|
||||
self.in2.set_low();
|
||||
self.pwm.set_duty_cycle(speed).unwrap();
|
||||
}
|
||||
|
||||
fn backward(&mut self, speed: f64) {
|
||||
self.in1.set_low();
|
||||
self.in2.set_high();
|
||||
self.pwm.set_duty_cycle(speed).unwrap();
|
||||
}
|
||||
|
||||
fn stop(&mut self) {
|
||||
self.in1.set_low();
|
||||
self.in2.set_low();
|
||||
self.pwm.set_duty_cycle(0.0).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let gpio = Gpio::new()?;
|
||||
|
||||
let in1 = gpio.get(27)?.into_output();
|
||||
let in2 = gpio.get(22)?.into_output();
|
||||
|
||||
let pwm = Pwm::with_frequency(
|
||||
Channel::Pwm0,
|
||||
1000.0,
|
||||
0.0,
|
||||
Polarity::Normal,
|
||||
true,
|
||||
)?;
|
||||
|
||||
let mut left_motor = Motor { in1, in2, pwm };
|
||||
|
||||
left_motor.forward(0.7);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user