diff --git a/smart_bridge/src/components/api/LCD.h b/smart_bridge/src/components/api/LCD.h index e69724d..d9a50e3 100644 --- a/smart_bridge/src/components/api/LCD.h +++ b/smart_bridge/src/components/api/LCD.h @@ -9,8 +9,8 @@ class LCD { /** * @brief Construct a new LCD object. * - * @param column the column of the LCD display - * @param row number of row + * @param columns the column of the LCD display + * @param rows number of row */ LCD(int address, int columns, int rows); void write(const char* string, int start_col, int start_rows); diff --git a/smart_bridge/src/components/api/Pir.h b/smart_bridge/src/components/api/Pir.h index 3b2d4dc..69a8161 100644 --- a/smart_bridge/src/components/api/Pir.h +++ b/smart_bridge/src/components/api/Pir.h @@ -76,6 +76,8 @@ class Pir * @brief The current detection status of the PIR sensor (true if movement is detected, false otherwise). */ bool pirDetectionStatus; + + }; #endif // __PIR__ \ No newline at end of file diff --git a/smart_bridge/src/components/impl/Pir.cpp b/smart_bridge/src/components/impl/Pir.cpp index 63b117e..c556b95 100644 --- a/smart_bridge/src/components/impl/Pir.cpp +++ b/smart_bridge/src/components/impl/Pir.cpp @@ -13,11 +13,13 @@ Pir::Pir(int pin) : pirDetectionStatus(OBJECT_NOT_DETECTED) bool Pir::checkDetectedStatus() { + updatePirState(); return pirDetectionStatus == OBJECT_DETECTED; } bool Pir::checkNotDetectedStatus() { + updatePirState(); return pirDetectionStatus == OBJECT_NOT_DETECTED; } diff --git a/smart_bridge/src/config/config.h b/smart_bridge/src/config/config.h index 608fbfc..df82e6f 100644 --- a/smart_bridge/src/config/config.h +++ b/smart_bridge/src/config/config.h @@ -31,13 +31,9 @@ // SERVO #define SERVO_PIN 9 -/** - * TODO: TIMING, time constants for example N4.... etc - */ -#define N1 2 -// Time for the gate to open after car presence (in seconds) -#define MINDIST 10.0f // Minimum distance for car fully entering (in cm) -#define N2 10 // Time to consider car fully entered (in seconds) +#define N1 2 // Time for the gate to open after car presence (in seconds) +#define MINDIST 5.0f // Minimum distance for car fully entering (in cm) +#define N2 5 // Time to consider car fully entered (in seconds) #define N3 15 // Time for the washing process (in seconds) #define MAXDIST 30 // Maximum distance to consider car leaving (in cm) #define N4_FOR_DIST 10 // Time to consider car fully left (in seconds) diff --git a/smart_bridge/src/main.cpp b/smart_bridge/src/main.cpp index fb75000..a3b86ee 100644 --- a/smart_bridge/src/main.cpp +++ b/smart_bridge/src/main.cpp @@ -7,63 +7,65 @@ #include "tasks/TransitTask.h" #include "tasks/WaitingTask.h" #include "tasks/WashingTask.h" +#include "tasks/SleepingTask.h" #include "tasks/CheckOutTask.h" #include "tasks/CountDown.h" #include "tasks/WaitForClickTask.h" +#include "tasks/WashingTask.h" +#include "tasks/CheckOutTask.h" +#include "tasks/ExitTransitTask.h" +#include "tasks/TemperatureTask.h" + // #include "tasks/ServoTestTask.h" Scheduler scheduler; SerialReceiver *serialReceiver; +LCD *lcd; void setup() { Serial.begin(9600); - scheduler.init(100); // NOTE: Might be set higher to use less power, needs testing. + scheduler.init(50); // NOTE: Might be set higher to use less power, needs testing. + /**CREATE TASKS**/ - // ServoTestTask *servoTask = new ServoTestTask(); - BlinkTask *blinkTask = new BlinkTask(L2_PIN); - // TODO: BlinkTask *blinkTaskForCheckOutTransit = new BlinkTask(L2_PIN); + BlinkTask *blinkTaskTransit = new BlinkTask(L2_PIN); + BlinkTask *blinkTaskWashing = new BlinkTask(L2_PIN); + // CountDown *countDownTask = new CountDown(N3); + TemperatureTask *temperatureTask = new TemperatureTask(); // SleepingTask *sleepingTask = new SleepingTask(); - // WaitingTask *waitingTask = new WaitingTask(); CheckInTask *checkInTask = new CheckInTask(); - TransitTask *transitTask = new TransitTask(blinkTask); + TransitTask *transitTask = new TransitTask(blinkTaskTransit); WaitForClickTask *waitForClickTask = new WaitForClickTask(); - // WashingTask *washingTask = new WashingTask(blinkTask, new CountDown(N3)); - // TODO: CheckOutTask *checkOutTask = new CheckOutTask(blinkTaskForCheckOutTransit); - // CountDown *countDown = new CountDown(N3); // NOTE: This is just a test. - // countDown->setActive(true); // NOTE: This is just a test. - - /// serialReceiver = new SerialReceiver(); //test test test test receiver - - + // WashingTask *washingTask = new WashingTask(blinkTaskTransit, nullptr, temperatureTask); + // CheckOutTask *checkOutTask = new CheckOutTask(); + // ExitTransitTask *exitTransitTask = new ExitTransitTask(); /**DEPENDENCIES**/ - // checkInTask->addDependency(waitingTask); + // checkInTask->addDependency(sleepingTask); transitTask->addDependency(checkInTask); waitForClickTask->addDependency(transitTask); - // washingTask->addDependency(transitTask); + //washingTask->addDependency(waitForClickTask); // checkOutTask->addDependency(washingTask); + // exitTransitTask->addDependency(checkOutTask); /**ADD TASKS TO THE SCHEDULER**/ - // scheduler.addTask(servoTask); - // scheduler.addTask(countDown); // NOTE: This is just a test. - // scheduler.addTask(waitingTask); + // scheduler.addTask(sleepingTask); scheduler.addTask(checkInTask); - //Serial.println("err:errore"); scheduler.addTask(transitTask); - scheduler.addTask(blinkTask); + scheduler.addTask(blinkTaskTransit); + scheduler.addTask(blinkTaskWashing); + // scheduler.addTask(countDownTask); + scheduler.addTask(temperatureTask); scheduler.addTask(waitForClickTask); - // scheduler.addTask(washingTask); - - // TODO: scheduler.addTask(checkOutTask); - // TODO: scheduler.addTask(blinkTaskForCheckOutTransit); + // scheduler.addTask(checkOutTask); + // scheduler.addTask(exitTransitTask); } void loop() { // serialReceiver->readData(); //@EMANUELE this is a test to try the serialReceiver, it must go instantiate when the arduino is in error state - //read the class briefs + // read the class briefs scheduler.schedule(); } \ No newline at end of file diff --git a/smart_bridge/src/tasks/SleepingTask.cpp b/smart_bridge/src/tasks/SleepingTask.cpp index c7b0822..d8e58c9 100644 --- a/smart_bridge/src/tasks/SleepingTask.cpp +++ b/smart_bridge/src/tasks/SleepingTask.cpp @@ -1,7 +1,48 @@ #include "SleepingTask.h" +SleepingTask* sleepingTaskInstance = nullptr; + +static void staticWakeUp() { + if (sleepingTaskInstance != nullptr) { + sleepingTaskInstance->wakeUp(); + } +} void SleepingTask::tick() { - /*TODO*/ -} \ No newline at end of file + switch (this->getState()) + { + case STARTING: + this->goInSleep(); + break; + + case ALIVE: + if (pir->checkDetectedStatus()) { + Serial.println("OBJECT revealed"); + this->setState(OBJECT_DETECTED); + } + break; + + case OBJECT_DETECTED: + this->setState(ALIVE); + this->setActive(false); + this->setCompleted(); + break; + } +} + +void SleepingTask::goInSleep() +{ + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sleepingTaskInstance = this; + attachInterrupt(digitalPinToInterrupt(2), staticWakeUp, RISING); + sei(); + sleep_enable(); + sleep_mode(); +} + +void SleepingTask::wakeUp() { + detachInterrupt(digitalPinToInterrupt(2)); + sleep_disable(); + this->setState(ALIVE); +} diff --git a/smart_bridge/src/tasks/SleepingTask.h b/smart_bridge/src/tasks/SleepingTask.h index 1990ce8..ab954ab 100644 --- a/smart_bridge/src/tasks/SleepingTask.h +++ b/smart_bridge/src/tasks/SleepingTask.h @@ -1,38 +1,47 @@ #ifndef __SLEEPING_TASK__ #define __SLEEPING_TASK__ -#define _MAX_TIME_BEFORE_SLEEP 10 +#define _MAX_TIME_BEFORE_SLEEP 10000 /*ms*/ #include "config/config.h" #include "components/api/Pir.h" +#include "components/api/LCD.h" #include "kernel/DependantTaskWithState.h" +#include "avr/interrupt.h" +#include "avr/sleep.h" /** * @class SleepingTask - * @brief This task handle the sleeping state using the pir as sensor to detect movement and - * wake up arduino + * @brief This task handle the sleeping state using the pir as sensor to + * detect movement and wake up arduino */ -class SleepingTask : public DependantTaskWithState +class SleepingTask : public TaskWithState { public: - SleepingTask(SleepingTask *sleepingTask) : DependantTaskWithState() + SleepingTask() : TaskWithState() { Serial.println("SleepingTask created"); this->pir = new Pir(PIR_PIN); - this->setState(WAITING_FOR_SOMEONE); + this->lcd=new LCD(0x27, 16,2); + this->init(); + this->setActive(true); + this->setState(STARTING); }; void tick() override; + void wakeUp(); + void goInSleep(); private: enum state { - WAITING_FOR_SOMEONE, - GO_IN_SLEEP, - WAKE_UP, + STARTING, + ALIVE, + OBJECT_DETECTED }; Pir *pir; + LCD *lcd; }; #endif diff --git a/smart_bridge/src/tasks/WaitingTask.cpp b/smart_bridge/src/tasks/WaitingTask.cpp index 47c4168..757cc09 100644 --- a/smart_bridge/src/tasks/WaitingTask.cpp +++ b/smart_bridge/src/tasks/WaitingTask.cpp @@ -3,5 +3,16 @@ void WaitingTask::tick() { - /*TODO*/ + if (this->getDependency(0) != nullptr) + { + if (this->getDependency(0)->isCompleted()) + { + switch (this->getState()) + { + case STARTED: + Serial.println("sono qui in waiting"); + } + + } + } } \ No newline at end of file diff --git a/smart_bridge/src/tasks/WaitingTask.h b/smart_bridge/src/tasks/WaitingTask.h index 8fe61ef..a9ab900 100644 --- a/smart_bridge/src/tasks/WaitingTask.h +++ b/smart_bridge/src/tasks/WaitingTask.h @@ -1,16 +1,21 @@ #ifndef __WAITING_TASK__ #define __WAITING_TASK__ -#include "kernel/TaskWithState.h" +#include "kernel/DependantTaskWithState.h" #include "components/api/Pir.h" +#include "SleepingTask.h" #include "config/config.h" -class WaitingTask : public TaskWithState +class WaitingTask : public DependantTaskWithState { public: - WaitingTask() : TaskWithState() +/** + * + * +*/ + WaitingTask(SleepingTask *SleepingTask) : DependantTaskWithState() { - this->pir=new Pir(PIR_PIN); //attached to the INTERRUPT_PIN + this->pir=new Pir(PIR_PIN); //attached to the INTERRUPT_PIN 2 this->init(); this->setState(STARTED); Serial.println("WaitingTask created"); @@ -22,7 +27,7 @@ class WaitingTask : public TaskWithState { STARTED, //is just left ... so IN_SUSPENSION, //is going to go in sleep mode - FINSHED //is going to go in checkin state + FINISHED //is going to go in checkin state }; Pir *pir; };