Implement pause switch and button press

This commit is contained in:
Blizzard Finnegan 2023-05-24 08:34:04 -04:00
parent 3d9779d483
commit d16aa4fefb
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
3 changed files with 121 additions and 14 deletions

69
Cargo.lock generated
View file

@ -320,6 +320,21 @@ dependencies = [
"log",
]
[[package]]
name = "futures"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.28"
@ -327,6 +342,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
@ -335,6 +351,17 @@ version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c"
[[package]]
name = "futures-executor"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.28"
@ -356,6 +383,47 @@ dependencies = [
"waker-fn",
]
[[package]]
name = "futures-macro"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.16",
]
[[package]]
name = "futures-sink"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e"
[[package]]
name = "futures-task"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65"
[[package]]
name = "futures-util"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "glob"
version = "0.3.1"
@ -677,6 +745,7 @@ dependencies = [
"configparser",
"derivative",
"fern",
"futures",
"leptess",
"log",
"opencv",

View file

@ -10,6 +10,7 @@ async-std = "1.12.0"
configparser = "3.0.2"
derivative = "2.2.0"
fern = "0.6.2"
futures = "0.3.28"
leptess = "0.14.0"
log = "0.4.17"
opencv = { version = "0.82.0", features = ["clang-runtime"] }

View file

@ -3,6 +3,7 @@ use async_std::sync::*;
use rppal::gpio::{Gpio,OutputPin,InputPin,Trigger,Level};
use std::thread;
use std::result::Result;
use futures::executor;
use std::fmt;
const TRAVEL_DISTANCE_FACTOR:f64 = 0.95;
@ -41,12 +42,12 @@ pub struct FixtureInitError;
impl fmt::Display for GpioError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Bad GPIO settings for fixture")
write!(f, "Bad fixture GPIO settings")
}
}
impl fmt::Display for FixtureInitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Invalid PWM value for fixture")
write!(f, "Invalid fixture PWM value")
}
}
@ -74,24 +75,22 @@ impl Fixture{
match possible_run_pin{
Ok(run_pin) =>{
_ = run_pin.into_input_pulldown().set_async_interrupt(Trigger::Both, |switch_state|{
_ = async{
let mut move_allowed = MOVE_LOCK.write().await;
match switch_state {
Level::Low=> {
*move_allowed = false;
}
Level::High => {
*move_allowed = true;
}
};
let mut move_allowed = executor::block_on(MOVE_LOCK.write());
match switch_state {
Level::Low=> {
*move_allowed = false;
}
Level::High => {
*move_allowed = true;
}
};
});
},
Err(_) => {}
}
while let Err(_) = output.find_distance() {};
while let Err(_) = output.find_distance(){};
return Ok(output);
}
@ -101,16 +100,24 @@ impl Fixture{
(self.upper_limit.as_mut(), self.motor_direction.as_mut(), self.motor_enable.as_mut()){
if upper_limit.is_high(){
{
while !*executor::block_on(MOVE_LOCK.read()) {}
motor_direction_pin.set_low();
motor_enable_pin.set_high()
}
thread::sleep(Duration::from_millis(500));
motor_enable_pin.set_low()
}
while !*executor::block_on(MOVE_LOCK.read()){}
motor_direction_pin.set_high();
motor_enable_pin.set_high();
let mut counter = 0;
for _ in 0..TIMEOUT {
while !*executor::block_on(MOVE_LOCK.read()){
motor_enable_pin.set_low();
}
if *executor::block_on(MOVE_LOCK.read()) && motor_enable_pin.is_set_low(){
motor_enable_pin.set_high();
}
if upper_limit.is_high() { break; }
counter += 1;
thread::sleep(POLL_DELAY);
@ -129,9 +136,16 @@ impl Fixture{
//Time travel time to lower limit switch
if let (Some(lower_limit), Some(motor_direction_pin), Some(motor_enable_pin)) =
(self.lower_limit.as_mut(), self.motor_direction.as_mut(), self.motor_enable.as_mut()){
while !*executor::block_on(MOVE_LOCK.read()){}
motor_direction_pin.set_low();
motor_enable_pin.set_high();
for _ in 0..TIMEOUT{
while !*executor::block_on(MOVE_LOCK.read()){
motor_enable_pin.set_low();
}
if *executor::block_on(MOVE_LOCK.read()) && motor_enable_pin.is_set_low(){
motor_enable_pin.set_high();
}
if lower_limit.is_high() { break; }
down_counter += 1;
thread::sleep(POLL_DELAY);
@ -141,9 +155,16 @@ impl Fixture{
//Time travel time to upper limit switch
if let (Some(upper_limit), Some(motor_direction_pin), Some(motor_enable_pin)) =
(self.upper_limit.as_mut(), self.motor_direction.as_mut(), self.motor_enable.as_mut()){
while !*executor::block_on(MOVE_LOCK.read()){}
motor_direction_pin.set_low();
motor_enable_pin.set_high();
for _ in 0..TIMEOUT{
while !*executor::block_on(MOVE_LOCK.read()){
motor_enable_pin.set_low();
}
if *executor::block_on(MOVE_LOCK.read()) && motor_enable_pin.is_set_low(){
motor_enable_pin.set_high();
}
if upper_limit.is_high() { break; }
up_counter += 1;
thread::sleep(POLL_DELAY);
@ -178,9 +199,16 @@ impl Fixture{
if let (Some(motor_direction_pin), Some(motor_enable_pin)) =
(self.motor_direction.as_mut(), self.motor_enable.as_mut()){
while !*executor::block_on(MOVE_LOCK.read()){}
motor_direction_pin.set_low();
motor_enable_pin.set_high();
for _ in 0..move_polls{
while !*executor::block_on(MOVE_LOCK.read()){
motor_enable_pin.set_low();
}
if *executor::block_on(MOVE_LOCK.read()) && motor_enable_pin.is_set_low(){
motor_enable_pin.set_high();
}
if limit_sense.is_high() { break; }
thread::sleep(POLL_DELAY);
}
@ -188,4 +216,13 @@ impl Fixture{
return limit_sense.is_high();
}
pub fn push_button(&mut self){
if let Some(piston_enable) = self.piston_enable.as_mut(){
while !*executor::block_on(MOVE_LOCK.read()){}
piston_enable.set_high();
thread::sleep(Duration::from_secs(1));
piston_enable.set_low();
}
}
}