Compare commits

...

2 Commits

Author SHA1 Message Date
Fast-Blast
11eb3e2b17 Updated TODO.md 2026-03-26 18:10:58 +01:00
Fast-Blast
59e405cb88 Added more modularity and config. 2026-03-26 18:10:58 +01:00
4 changed files with 66 additions and 25 deletions

1
TODO.md Normal file
View File

@@ -0,0 +1 @@
- [ ] Improve config file as Rust is not ideal.

19
src/config.rs Normal file
View 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
}
}
}

View File

@@ -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
View 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();
}
}