Alter lightTest.rs to comply with 4-pin model, add ultrasonicTest.rs, add motorTest.rs with raslib::L298n library
This commit is contained in:
@@ -5,3 +5,4 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rppal = "0.17"
|
rppal = "0.17"
|
||||||
|
raslib = "*"
|
||||||
|
|||||||
@@ -4,21 +4,6 @@ use std::time::Duration;
|
|||||||
use rppal::gpio::Gpio;
|
use rppal::gpio::Gpio;
|
||||||
|
|
||||||
let gpio = Gpio::new()?;
|
let gpio = Gpio::new()?;
|
||||||
let mut fanFiveVoltRail = gpio.get(2)?.into_output();
|
let mut examplePin = gpio.get(0)?.into_output();
|
||||||
|
|
||||||
// Quick toggle fans 5 seconds long, 0.5s interval
|
//TODO: Add relay switching
|
||||||
|
|
||||||
let mut i = 0;
|
|
||||||
|
|
||||||
while i < 11 {
|
|
||||||
fanFiveVoltRail.set_high();
|
|
||||||
thread::sleep(Duration::from_secs(0.5));
|
|
||||||
fanFiveVoltRail.set_low();
|
|
||||||
i = i + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run fans for five more seconds constant
|
|
||||||
|
|
||||||
fanFiveVoltRail.set_high();
|
|
||||||
thread::sleep(Duration::from_secs(5));
|
|
||||||
fanFiveVoltRail.set_low();
|
|
||||||
@@ -7,10 +7,8 @@ let gpio = Gpio::new()?;
|
|||||||
|
|
||||||
let mut indicatorLeftBack = gpio.get(21)?.into_output();
|
let mut indicatorLeftBack = gpio.get(21)?.into_output();
|
||||||
let mut indicatorRightBack = gpio.get(22)?.into_output();
|
let mut indicatorRightBack = gpio.get(22)?.into_output();
|
||||||
let mut brakeLeftBack = gpio.get(23)?.into_output();
|
let mut brakeLights = gpio.get(23)?.into_output();
|
||||||
let mut brakeRightBack = gpio.get(24)?.into_output();
|
let mut headLights = gpio.get(25)?.into_output();
|
||||||
let mut frontLeftHead = gpio.get(25)?.into_output();
|
|
||||||
let mut frontRightHead = gpio.get(26)?.into_output();
|
|
||||||
|
|
||||||
// Blink both indicators
|
// Blink both indicators
|
||||||
|
|
||||||
@@ -51,35 +49,35 @@ i = 0;
|
|||||||
|
|
||||||
// Hold brake lights and blink twice
|
// Hold brake lights and blink twice
|
||||||
|
|
||||||
brakeLeftBack.set_high();
|
brakeLights.set_high();
|
||||||
brakeRightBack.set_high();
|
brakeLights.set_high();
|
||||||
thread::sleep(Duration::from_secs(3));
|
thread::sleep(Duration::from_secs(3));
|
||||||
brakeLeftBack.set_low();
|
brakeLights.set_low();
|
||||||
brakeRightBack.set_low();
|
brakeLights.set_low();
|
||||||
|
|
||||||
while i < 3 {
|
while i < 3 {
|
||||||
brakeLeftBack.set_high();
|
brakeLights.set_high();
|
||||||
brakeRightBack.set_high();
|
brakeLights.set_high();
|
||||||
thread::sleep(Duration::from_secs(0.7))
|
thread::sleep(Duration::from_secs(0.7))
|
||||||
brakeLeftBack.set_low();
|
brakeLights.set_low();
|
||||||
brakeRigthBack.set_low();
|
brakeLights.set_low();
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
// Turn on headlights and flash twice
|
// Turn on headlights and flash twice
|
||||||
|
|
||||||
frontLeftHead.set_high();
|
headLights.set_high();
|
||||||
frontRightHead.set_high();
|
headLights.set_high();
|
||||||
thread::sleep(Duration::from_secs(3))
|
thread::sleep(Duration::from_secs(3))
|
||||||
frontLeftHead.set_low();
|
headLights.set_low();
|
||||||
frontRightHead.set_low();
|
headLights.set_low();
|
||||||
|
|
||||||
while i < 3 {
|
while i < 3 {
|
||||||
frontLeftHead.set_high();
|
headLights.set_high();
|
||||||
frontRightHead.set_high();
|
headLights.set_high();
|
||||||
thread::sleep(Duration::from_secs(0.7))
|
thread::sleep(Duration::from_secs(0.7))
|
||||||
frontLeftHead.set_low();
|
headLights.set_low();
|
||||||
frontRightHead.set_low();
|
headLights.set_low();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
50
src/moduleTests/motorTest.rs
Normal file
50
src/moduleTests/motorTest.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
use raslib::L298n;
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
// struct L298n {
|
||||||
|
// in1: Gpio,
|
||||||
|
// in2: Gpio,
|
||||||
|
// ena: Gpio,
|
||||||
|
// }
|
||||||
|
|
||||||
|
fn main() -> Result<(), std::io::Error> {
|
||||||
|
let mut motorFrontLeft = L298n::new(0, 0, 0);
|
||||||
|
let mut motorFrontRight = L298n::new(0, 0, 0);
|
||||||
|
let mut motorBackLeft = L298n::new(0, 0, 0);
|
||||||
|
let mut motorBackRight = L298n::new(0, 0, 0);
|
||||||
|
|
||||||
|
// Front left test
|
||||||
|
|
||||||
|
motorFrontLeft.forward()?;
|
||||||
|
thread::sleep(Duration::from_secs(2));
|
||||||
|
motorFrontLeft.backward()?;
|
||||||
|
thread::sleep(Duration::from_secs(2));
|
||||||
|
motorFrontLeft.stop()?;
|
||||||
|
|
||||||
|
// Front right test
|
||||||
|
|
||||||
|
motorFrontRight.forward()?;
|
||||||
|
thread::sleep(Duration::from_secs(2));
|
||||||
|
motorFrontRight.backward()?;
|
||||||
|
thread::sleep(Duration::from_secs(2));
|
||||||
|
motorFrontRight.stop()?;
|
||||||
|
|
||||||
|
// Back left test
|
||||||
|
|
||||||
|
motorBackLeft.forward()?;
|
||||||
|
thread::sleep(Duration::from_secs(2));
|
||||||
|
motorBackLeft.backward()?;
|
||||||
|
thread::sleep(Duration::from_secs(2));
|
||||||
|
motorBackLeft.stop()?;
|
||||||
|
|
||||||
|
// Back right test
|
||||||
|
|
||||||
|
motorBackRight.forward()?;
|
||||||
|
thread::sleep(Duration::from_secs(2));
|
||||||
|
motorBackRight.backward()?;
|
||||||
|
thread::sleep(Duration::from_secs(2));
|
||||||
|
motorBackRight.stop()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
45
src/moduleTests/ultrasonicTest.rs
Normal file
45
src/moduleTests/ultrasonicTest.rs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
use hc_sr04::{HcSr04, Unit};
|
||||||
|
|
||||||
|
let mut ultrasonicFront = HcSr04::new(
|
||||||
|
7, // TRIGGER
|
||||||
|
1, // ECHO
|
||||||
|
Some(23_f32)
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let mut ultrasonicLeft = HcSr04::new(
|
||||||
|
0,
|
||||||
|
4,
|
||||||
|
Some(23_f32)
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let mut ultrasonicRight = HcSr04::new(
|
||||||
|
2,
|
||||||
|
5,
|
||||||
|
Some(23_f32)
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let mut ultrasonicBack = HcSr04::new(
|
||||||
|
3,
|
||||||
|
6,
|
||||||
|
Some(23_f32)
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
match ultrasonicFront.measure_distance(Unit::Meters).unwrap() {
|
||||||
|
Some(dist) => println!("[FRONT] Distance: {.2}m", dist),
|
||||||
|
None => println!("Object out of range"),
|
||||||
|
}
|
||||||
|
|
||||||
|
match ultrasonicLeft.measure_distance(Unit::Meters).unwrap() {
|
||||||
|
Some(dist) => println!("[LEFT] Distance: {.2}m", dist),
|
||||||
|
None => println!("Object out of range"),
|
||||||
|
}
|
||||||
|
|
||||||
|
match ultrasonicRight.measure_distance(Unit::Meters).unwrap() {
|
||||||
|
Some(dist) => println!("[RIGHT] Distance: {.2}m", dist),
|
||||||
|
None => println!("Object out of range"),
|
||||||
|
}
|
||||||
|
|
||||||
|
match ultrasonicBack.measure_distance(Unit::Meters).unwrap() {
|
||||||
|
Some(dist) => println!("[BACK] Distance: {.2}m", dist),
|
||||||
|
None => println!("Object out of range"),
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user