From 404563f1315d1f67372e68765e765c8addad7d09 Mon Sep 17 00:00:00 2001 From: EmanueleDajko Date: Tue, 5 Dec 2023 12:24:28 +0100 Subject: [PATCH 01/15] Refactor WashingTask dependencies and initialization --- smart_bridge/src/tasks/WashingTask.cpp | 37 +++++++++++++++----------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/smart_bridge/src/tasks/WashingTask.cpp b/smart_bridge/src/tasks/WashingTask.cpp index 6164790..5b7978a 100644 --- a/smart_bridge/src/tasks/WashingTask.cpp +++ b/smart_bridge/src/tasks/WashingTask.cpp @@ -16,33 +16,40 @@ WashingTask::WashingTask(BlinkTask *blinkTask, this->blinkTask->setActive(false); // TODO: Check if this is necessary this->countDownTask = countDownTask; this->temperatureTask = temperatureTask; - this->init(1000); + this->countDownTask->setActive(false); + this->temperatureTask->setActive(false); + this->init(); this->setState(START_WASHING); } void WashingTask::tick() { - if (this->DependantTask::getDependency(0)->isCompleted()) + if (this->getDependency(0) != nullptr) { - switch (this->getState()) + if (this->DependantTask::getDependency(0)->isCompleted()) { - case START_WASHING: - handleStartWashing(); - break; + switch (this->getState()) + { + case START_WASHING: + handleStartWashing(); + break; - case STARTS_COUNTDOWN: - handleStartsCountdown(); - break; + case STARTS_COUNTDOWN: + handleStartsCountdown(); + break; - case ENDS_COUNTDOWN: - handleEndsCountdown(); - break; + case ENDS_COUNTDOWN: + handleEndsCountdown(); + break; - case ERROR: - handleError(); - break; + case ERROR: + handleError(); + break; + } } } + else + Serial.println("WashingTask::tick()::getDependency(0) is nullptr"); } void WashingTask::printWashingCompletedMessage() From b31ca16350f1fc377fe0543e5fc338c7ab5fdb45 Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 12:41:53 +0100 Subject: [PATCH 02/15] Refactor WashingTask class by making private methods as private. --- smart_bridge/src/tasks/WashingTask.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart_bridge/src/tasks/WashingTask.h b/smart_bridge/src/tasks/WashingTask.h index 5745b53..1e26aef 100644 --- a/smart_bridge/src/tasks/WashingTask.h +++ b/smart_bridge/src/tasks/WashingTask.h @@ -39,6 +39,7 @@ class WashingTask : public DependantTaskWithState */ void tick() override; +private: /** * @brief Handles the start of the washing process. * @@ -73,7 +74,6 @@ class WashingTask : public DependantTaskWithState */ void printWashingCompletedMessage(); -private: /** * @brief Enum for the possible states of the task. * From a46a96d8869b55e063b0b68be1c6c716996e7c5d Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 12:43:34 +0100 Subject: [PATCH 03/15] Refactor ExitTransitTask class by making private methods as private. --- smart_bridge/src/tasks/ExitTransitTask.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart_bridge/src/tasks/ExitTransitTask.h b/smart_bridge/src/tasks/ExitTransitTask.h index 4f49d1c..a84af50 100644 --- a/smart_bridge/src/tasks/ExitTransitTask.h +++ b/smart_bridge/src/tasks/ExitTransitTask.h @@ -33,6 +33,7 @@ class ExitTransitTask : public DependantTaskWithState */ void tick() override; +private: /** * @brief Handles the reading of the distance from the sonar sensor. * @@ -61,7 +62,6 @@ class ExitTransitTask : public DependantTaskWithState */ void handleSwitchOffL3(); -private: /** * @brief Enum for the possible states of the task. * From fdac672a51bee8264e713c6499a75cd02719c33f Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 12:44:46 +0100 Subject: [PATCH 04/15] Refactor CheckOutTask class by making private methods as private. --- smart_bridge/src/tasks/CheckOutTask.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart_bridge/src/tasks/CheckOutTask.h b/smart_bridge/src/tasks/CheckOutTask.h index be9086c..b61a266 100644 --- a/smart_bridge/src/tasks/CheckOutTask.h +++ b/smart_bridge/src/tasks/CheckOutTask.h @@ -32,6 +32,7 @@ class CheckOutTask : public DependantTaskWithState */ void tick() override; +private: /** * @brief Handles the turning on of the L3 LED. * @@ -46,7 +47,6 @@ class CheckOutTask : public DependantTaskWithState */ void handleOpensGate(); -private: /** * @brief Enum for the possible states of the task. * From 562cff62aad15027b9492f0d2f47bda973f2b012 Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 12:50:19 +0100 Subject: [PATCH 05/15] Refactor WashingTask code by removing not necessary comments --- smart_bridge/src/tasks/WashingTask.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/smart_bridge/src/tasks/WashingTask.cpp b/smart_bridge/src/tasks/WashingTask.cpp index 5b7978a..2634c2e 100644 --- a/smart_bridge/src/tasks/WashingTask.cpp +++ b/smart_bridge/src/tasks/WashingTask.cpp @@ -12,8 +12,8 @@ WashingTask::WashingTask(BlinkTask *blinkTask, this->lcd = new LCD(0x27, 16, 2); this->gate = new ServoImpl(SERVO_PIN); this->blinkTask = blinkTask; - this->blinkTask->init(500); // Blink every 500 ms the red led - this->blinkTask->setActive(false); // TODO: Check if this is necessary + this->blinkTask->init(500); + this->blinkTask->setActive(false); this->countDownTask = countDownTask; this->temperatureTask = temperatureTask; this->countDownTask->setActive(false); @@ -67,8 +67,6 @@ void WashingTask::handleStartWashing() void WashingTask::handleStartsCountdown() { - // this->countDownTask->tick(); - // this->temperatureTask->tick(); if (this->elapsedTime() >= checkInterval) { this->resetTime(); From de955cf2ad58d0f59952ebdb94c3a1ba6ebc813e Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 13:12:43 +0100 Subject: [PATCH 06/15] Add error handling for nullptr in ExitTransitTask::tick() --- smart_bridge/src/tasks/ExitTransitTask.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/smart_bridge/src/tasks/ExitTransitTask.cpp b/smart_bridge/src/tasks/ExitTransitTask.cpp index d068a46..3fbf91b 100644 --- a/smart_bridge/src/tasks/ExitTransitTask.cpp +++ b/smart_bridge/src/tasks/ExitTransitTask.cpp @@ -39,6 +39,8 @@ void ExitTransitTask::tick() } } } + else + Serial.println("ExitTransitTask::tick()::getDependency(0) is nullptr"); } void ExitTransitTask::handleReadingDistance() From e71c96e71c6c4e6421fac04380d2e3d40cb45c1f Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 13:12:51 +0100 Subject: [PATCH 07/15] Add error message for nullptr in CheckOutTask::tick() --- smart_bridge/src/tasks/CheckOutTask.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/smart_bridge/src/tasks/CheckOutTask.cpp b/smart_bridge/src/tasks/CheckOutTask.cpp index af7074f..3931c58 100644 --- a/smart_bridge/src/tasks/CheckOutTask.cpp +++ b/smart_bridge/src/tasks/CheckOutTask.cpp @@ -29,6 +29,8 @@ void CheckOutTask::tick() } } } + else + Serial.println("CheckOutTask::tick()::getDependency(0) is nullptr"); } void CheckOutTask::handleTurnOnL3() From 70b3d1c01755964dcc612aa2e086d20cc66ef949 Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 15:17:21 +0100 Subject: [PATCH 08/15] Fix initialization bug in ExitTransitTask --- smart_bridge/src/tasks/ExitTransitTask.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/smart_bridge/src/tasks/ExitTransitTask.cpp b/smart_bridge/src/tasks/ExitTransitTask.cpp index 3fbf91b..4cb9833 100644 --- a/smart_bridge/src/tasks/ExitTransitTask.cpp +++ b/smart_bridge/src/tasks/ExitTransitTask.cpp @@ -9,7 +9,7 @@ ExitTransitTask::ExitTransitTask() this->sonar = new Sonar(SONAR_TRIG_PIN, SONAR_ECHO_PIN, SONAR_MAX_TIME); this->L3 = new Led(L3_PIN); this->gate = new ServoImpl(SERVO_PIN); - this->init(1000); + this->init(); this->setState(READING_DISTANCE); }; @@ -54,15 +54,13 @@ void ExitTransitTask::handleCheckingDistance() { if (this->distance > MAXDIST) { - if (timeInExit == 0) - { - timeInExit = millis(); - } - else if (millis() - timeInExit >= N4_FOR_DIST * 1000) + if (this->elapsedTime() >= N4_FOR_DIST * 1000) { this->setState(CLOSES_GATE); } } + else + this->resetTime(); } void ExitTransitTask::handleClosesGate() From 4f04c561cc1ec5ec14812ee3c019ee4af646a26f Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 15:17:26 +0100 Subject: [PATCH 09/15] Fix gate position constants in CheckOutTask --- smart_bridge/src/tasks/CheckOutTask.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/smart_bridge/src/tasks/CheckOutTask.h b/smart_bridge/src/tasks/CheckOutTask.h index b61a266..35d8d94 100644 --- a/smart_bridge/src/tasks/CheckOutTask.h +++ b/smart_bridge/src/tasks/CheckOutTask.h @@ -57,13 +57,12 @@ class CheckOutTask : public DependantTaskWithState OPENS_GATE, ///< The task is currently opening the gate. }; - Led *L3; ///< Pointer to the L3 LED object - Sonar *sonar; ///< Pointer to the sonar object - ServoImpl *gate; ///< Pointer to the ServoImpl object that controls the gate. - float distance; ///< The current distance read from the sonar sensor. - unsigned long timeInExit = 0; ///< The time in milliseconds when the car entered the exit area. - const int GATE_OPEN_POSITION = 90; ///< The position of the gate when it is open. - const int GATE_CLOSE_POSITION = 0; ///< The position of the gate when it is closed. + Led *L3; ///< Pointer to the L3 LED object + Sonar *sonar; ///< Pointer to the sonar object + ServoImpl *gate; ///< Pointer to the ServoImpl object that controls the gate. + float distance; ///< The current distance read from the sonar sensor. + const int GATE_OPEN_POSITION; ///< The position of the gate when it is open. + const int GATE_CLOSE_POSITION; ///< The position of the gate when it is closed. }; #endif // __CHECK_OUT_TASK__ \ No newline at end of file From aa9bb1525ca9b6527f18c64c1e351ef67c5dad50 Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 15:17:32 +0100 Subject: [PATCH 10/15] Add gate open and close positions to CheckOutTask --- smart_bridge/src/tasks/CheckOutTask.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/smart_bridge/src/tasks/CheckOutTask.cpp b/smart_bridge/src/tasks/CheckOutTask.cpp index 3931c58..6b4a6c6 100644 --- a/smart_bridge/src/tasks/CheckOutTask.cpp +++ b/smart_bridge/src/tasks/CheckOutTask.cpp @@ -1,6 +1,9 @@ #include "CheckOutTask.h" -CheckOutTask::CheckOutTask() : DependantTaskWithState() +CheckOutTask::CheckOutTask() + : DependantTaskWithState(), + GATE_OPEN_POSITION(90), + GATE_CLOSE_POSITION(0) { Serial.println("CheckOutTask created"); this->sonar = new Sonar(SONAR_TRIG_PIN, SONAR_ECHO_PIN, SONAR_MAX_TIME); From 30e119d657af1820880a43684df2b17cdebf841a Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 15:47:34 +0100 Subject: [PATCH 11/15] Fix countdown display format --- smart_bridge/src/tasks/CountDown.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/smart_bridge/src/tasks/CountDown.cpp b/smart_bridge/src/tasks/CountDown.cpp index 1ffafef..a45f4eb 100644 --- a/smart_bridge/src/tasks/CountDown.cpp +++ b/smart_bridge/src/tasks/CountDown.cpp @@ -50,7 +50,9 @@ void CountDown::startCountDown() void CountDown::printCountDown() { int count = getCountDown(); - lcd->write(("Countdown: " + String(count)).c_str(), 0, 0); + char buffer[3]; + sprintf(buffer, "%02d", count); + lcd->write(("Countdown: " + String(buffer)).c_str(), 0, 0); } void CountDown::endsCountDown() From 2f5ed77aff50bc291b2f75b16802d0c20c88f308 Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 16:27:48 +0100 Subject: [PATCH 12/15] the setActive(false) method of CountDown and Temperature tasks have been removed --- smart_bridge/src/tasks/WashingTask.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smart_bridge/src/tasks/WashingTask.cpp b/smart_bridge/src/tasks/WashingTask.cpp index 2634c2e..85c4049 100644 --- a/smart_bridge/src/tasks/WashingTask.cpp +++ b/smart_bridge/src/tasks/WashingTask.cpp @@ -16,8 +16,8 @@ WashingTask::WashingTask(BlinkTask *blinkTask, this->blinkTask->setActive(false); this->countDownTask = countDownTask; this->temperatureTask = temperatureTask; - this->countDownTask->setActive(false); - this->temperatureTask->setActive(false); + // this->countDownTask->setActive(false); TODO: uncomment if you want to test + // this->temperatureTask->setActive(false); TODO: uncomment if you want to test this->init(); this->setState(START_WASHING); } From ef39cc1d8d2170538bd537389cd19d004b664c3d Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 16:27:58 +0100 Subject: [PATCH 13/15] Set active state to false in TemperatureTask constructor --- smart_bridge/src/tasks/TemperatureTask.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/smart_bridge/src/tasks/TemperatureTask.cpp b/smart_bridge/src/tasks/TemperatureTask.cpp index 5b60edd..bc4fc72 100644 --- a/smart_bridge/src/tasks/TemperatureTask.cpp +++ b/smart_bridge/src/tasks/TemperatureTask.cpp @@ -9,6 +9,7 @@ TemperatureTask::TemperatureTask() this->lcd = new LCD(0x27, 16, 2); this->setTemperature(this->getTemperature()); this->init(1000); + this->setActive(false); } float TemperatureTask::readVoltage() From 43a24b65f43f463b698c5042d8fe1d8e3c74d6fb Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 16:28:30 +0100 Subject: [PATCH 14/15] Set active state to false in CountDownTask constructor --- smart_bridge/src/tasks/CountDown.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/smart_bridge/src/tasks/CountDown.cpp b/smart_bridge/src/tasks/CountDown.cpp index a45f4eb..aa5e387 100644 --- a/smart_bridge/src/tasks/CountDown.cpp +++ b/smart_bridge/src/tasks/CountDown.cpp @@ -7,6 +7,7 @@ CountDown::CountDown(int countDown) : Task() this->lcd = new LCD(0x27, 16, 2); this->resetCountDown(N3); this->init(1000); + this->setActive(false); } int CountDown::getCountDown() From 20a40c1f71ed230a030666456bd418e5da947180 Mon Sep 17 00:00:00 2001 From: Emanuele Dajko Date: Tue, 5 Dec 2023 16:28:42 +0100 Subject: [PATCH 15/15] Refactor task dependencies in main.cpp --- smart_bridge/src/main.cpp | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/smart_bridge/src/main.cpp b/smart_bridge/src/main.cpp index a3b86ee..635a632 100644 --- a/smart_bridge/src/main.cpp +++ b/smart_bridge/src/main.cpp @@ -29,38 +29,33 @@ void setup() scheduler.init(50); // NOTE: Might be set higher to use less power, needs testing. /**CREATE TASKS**/ - 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(); CheckInTask *checkInTask = new CheckInTask(); - TransitTask *transitTask = new TransitTask(blinkTaskTransit); + BlinkTask *blinkTask = new BlinkTask(L2_PIN); + TransitTask *transitTask = new TransitTask(blinkTask); WaitForClickTask *waitForClickTask = new WaitForClickTask(); - // WashingTask *washingTask = new WashingTask(blinkTaskTransit, nullptr, temperatureTask); - // CheckOutTask *checkOutTask = new CheckOutTask(); - // ExitTransitTask *exitTransitTask = new ExitTransitTask(); + CountDown *countDownTask = new CountDown(N3); + TemperatureTask *temperatureTask = new TemperatureTask(); + WashingTask *washingTask = new WashingTask(blinkTask, countDownTask, temperatureTask); + CheckOutTask *checkOutTask = new CheckOutTask(); + ExitTransitTask *exitTransitTask = new ExitTransitTask(); /**DEPENDENCIES**/ - // checkInTask->addDependency(sleepingTask); transitTask->addDependency(checkInTask); waitForClickTask->addDependency(transitTask); - //washingTask->addDependency(waitForClickTask); - // checkOutTask->addDependency(washingTask); - // exitTransitTask->addDependency(checkOutTask); + washingTask->addDependency(waitForClickTask); + checkOutTask->addDependency(washingTask); + exitTransitTask->addDependency(checkOutTask); /**ADD TASKS TO THE SCHEDULER**/ - // scheduler.addTask(sleepingTask); scheduler.addTask(checkInTask); scheduler.addTask(transitTask); - scheduler.addTask(blinkTaskTransit); - scheduler.addTask(blinkTaskWashing); - // scheduler.addTask(countDownTask); - scheduler.addTask(temperatureTask); + scheduler.addTask(blinkTask); scheduler.addTask(waitForClickTask); - // scheduler.addTask(washingTask); - // scheduler.addTask(checkOutTask); - // scheduler.addTask(exitTransitTask); + scheduler.addTask(washingTask); + scheduler.addTask(countDownTask); + scheduler.addTask(temperatureTask); + scheduler.addTask(checkOutTask); + scheduler.addTask(exitTransitTask); } void loop()