Added more modularity and config.
This commit is contained in:
19
src/config.rs
Normal file
19
src/config.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
pub struct Config {
|
||||
// Wheel GPIO pins.
|
||||
pub left1: u8,
|
||||
pub left2: u8,
|
||||
pub right1: u8,
|
||||
pub right2: u8
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
// Wheel GPIO pins.
|
||||
left1: 27,
|
||||
left2: 22,
|
||||
right1: 23,
|
||||
right2: 24
|
||||
}
|
||||
}
|
||||
}
|
||||
42
src/main.rs
42
src/main.rs
@@ -1,34 +1,26 @@
|
||||
use rppal::gpio::{Gpio, OutputPin};
|
||||
// External libraries.
|
||||
use rppal::gpio::{Gpio};
|
||||
use rppal::pwm::{Pwm, Channel, Polarity};
|
||||
use std::{error::Error, thread, time::Duration};
|
||||
|
||||
struct Motor {
|
||||
in1: OutputPin,
|
||||
in2: OutputPin,
|
||||
pwm: Pwm,
|
||||
}
|
||||
// Own libraries.
|
||||
mod motors;
|
||||
use motors::Motor;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
// Config
|
||||
mod config;
|
||||
use config::Config;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let config = Config::new();
|
||||
println!(
|
||||
"Starting with GPIO pins:\n - Motors\n - Right 1: {}\n - Right 2: {}\n - Left 1: {}\n - Left 2: {}",
|
||||
config.right1,
|
||||
config.right2,
|
||||
config.left1,
|
||||
config.left2
|
||||
);
|
||||
|
||||
let gpio = Gpio::new()?;
|
||||
|
||||
// LEFT
|
||||
|
||||
29
src/motors.rs
Normal file
29
src/motors.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user