From: Lukáš Jiřiště Date: Mon, 18 Nov 2024 14:36:16 +0000 (+0100) Subject: Implement interruptable_delay X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=96f7f2983b100d3c4b3c75438e3d8e91ae9728fc;p=Servomatic.git Implement interruptable_delay This enables a fast interruption of the sequence (mainly the wait_for_equilibrium method). --- diff --git a/Measurement.h b/Measurement.h index 4b5c596..a0813e6 100644 --- a/Measurement.h +++ b/Measurement.h @@ -34,7 +34,7 @@ class Measurement void get_measurement(); void thin_out(); void clear(); - void wait_for_equilibrium(float threshold = 0.01); + int wait_for_equilibrium(float threshold = 0.01); }; #endif // MEASUREMENT_H diff --git a/Measurement.ino b/Measurement.ino index 6eadc2d..3107f4b 100644 --- a/Measurement.ino +++ b/Measurement.ino @@ -1,4 +1,5 @@ #include "Measurement.h" +#include "Servomatic.h" Measurement::Measurement(const PressureSensor &sensor) : m_size{0} @@ -70,14 +71,16 @@ void Measurement::clear() // // Using this we can then numerically optimize a. Interval halving is used // because it is foolproof and the evaluation is not computationally demanding. -void Measurement::wait_for_equilibrium(float threshold = 0.01) +int Measurement::wait_for_equilibrium(float threshold = 0.01) { clear(); while (m_size < 20 || threshold < calc_equilibrium_dist()) { get_measurement(); - delay(m_interval_ms); + if (interuptable_delay(m_interval_ms)) + return (1); } + return (0); } float Measurement::calc_equilibrium_dist() const diff --git a/Servomatic.h b/Servomatic.h new file mode 100644 index 0000000..763cd4c --- /dev/null +++ b/Servomatic.h @@ -0,0 +1,6 @@ +#ifndef SERVOMATIC_H +# define SERVOMATIC_H + +int interuptable_delay(unsigned long ms); + +#endif // SERVOMATIC_H diff --git a/Servomatic.ino b/Servomatic.ino index 368bc60..a1d97da 100644 --- a/Servomatic.ino +++ b/Servomatic.ino @@ -1,36 +1,91 @@ #include "PressureSensor.h" #include "Valve.h" +#include "CellValve.h" #include "Measurement.h" +#include static const int PRESSURE_SENSOR1_PIN = A1; static const int PRESSURE_SENSOR2_PIN = A2; static const int PRESSURE_SENSOR3_PIN = A3; -static const int SWITCH_PIN = 8; +static const int SWITCH_PIN = 2; // Only pins 2; 3 have external interrupts -static const int VALVE_THREEWAY_PIN = 9; -static const int VALVE_CELL_PIN = 10; -static const int VALVE_PRESSURE_SENSOR_PIN = 11; +static const int THREEWAY_VALVE_PIN = 9; +static const int CELL_VALVE_PIN = 10; +static const int PRESSURE_SENSOR_VALVE_PIN = 11; +static const int VACUUM_VALVE_PIN = 12; -int main() +static volatile int interrupt_happened{0}; + +void interrupt_routine() { - init(); - Serial.begin(9600); + interrupt_happened = 1; +} - main_loop(); +int interruptable_delay(unsigned long ms) +{ + const unsigned int start_time{millis()}; + + while (ms > millis() - start_time) + if (interrupt_happened == 1) + return (1); return (0); } +void signal_going_to_sleep() +{ + delay(300); + digitalWrite(SWITCH_PIN, HIGH); + pinMode(SWITCH_PIN, OUTPUT); + delay(100); + pinMode(SWITCH_PIN, INPUT); +} + +void sleep() +{ + noInterrupts(); + if (digitalRead(SWITCH_PIN) == LOW) + { + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sleep_enable(); + interrupts(); + sleep_cpu(); + sleep_disable(); + } + else + interrupts(); +} + void main_loop() { PressureSensor sensor_1{PRESSURE_SENSOR1_PIN, 10}; - Valve test_valve{6, 760, 1600, 2400}; - int usec; Measurement measurement{sensor_1}; + Valve vacuum_valve{VACUUM_VALVE_PIN, 760, 2400}; + CellValve cell_valve{CELL_VALVE_PIN, 760, 1600, 2400}; + Valve gas_threeway_valve{THREEWAY_VALVE_PIN, 760, 2400}; while (1) { - measurement.wait_for_equilibrium(); - delay(10000); + while (!interrupt_happened && digitalRead(SWITCH_PIN) == HIGH) + { + if (measurement.wait_for_equilibrium()) + break ; + // Operate valves to prepare for next jump. + } + signal_going_to_sleep(); + sleep(); + interrupt_happened = 0; } } + +int main() +{ + init(); + Serial.begin(9600); + attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), interrupt_routine, + CHANGE); + pinMode(SWITCH_PIN, INPUT); + + main_loop(); + return (0); +}