Compare commits
2 Commits
6aa848fe3d
...
11eb3e2b17
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11eb3e2b17 | ||
|
|
59e405cb88 |
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 rppal::pwm::{Pwm, Channel, Polarity};
|
||||||
use std::{error::Error, thread, time::Duration};
|
use std::{error::Error, thread, time::Duration};
|
||||||
|
|
||||||
struct Motor {
|
// Own libraries.
|
||||||
in1: OutputPin,
|
mod motors;
|
||||||
in2: OutputPin,
|
use motors::Motor;
|
||||||
pwm: Pwm,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Motor {
|
// Config
|
||||||
fn forward(&mut self, speed: f64) {
|
mod config;
|
||||||
self.in1.set_high();
|
use config::Config;
|
||||||
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>> {
|
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()?;
|
let gpio = Gpio::new()?;
|
||||||
|
|
||||||
// LEFT
|
// 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