From 705fb7e3a88e3ac3d273a1e07ecfba40a65dc738 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Fri, 24 Nov 2023 19:22:43 +0100 Subject: [PATCH 01/13] [FIX] const -> constexpr --- smart_bridge/platformio.ini | 2 ++ smart_bridge/src/components/api/Sonar.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/smart_bridge/platformio.ini b/smart_bridge/platformio.ini index 6b6d241..6d5015a 100644 --- a/smart_bridge/platformio.ini +++ b/smart_bridge/platformio.ini @@ -9,6 +9,8 @@ ; https://docs.platformio.org/page/projectconf.html [env:uno] +monitor_speed = 9600 +monitor_filters = direct platform = atmelavr board = uno framework = arduino diff --git a/smart_bridge/src/components/api/Sonar.h b/smart_bridge/src/components/api/Sonar.h index 38d2976..966a3e6 100644 --- a/smart_bridge/src/components/api/Sonar.h +++ b/smart_bridge/src/components/api/Sonar.h @@ -111,7 +111,7 @@ class Sonar /** * @brief Constant representing no object detected by the sonar sensor. */ - static const float NO_OBJECT_DETECTED = -1.0f; + static constexpr float NO_OBJECT_DETECTED = -1.0f; }; #endif // __SONAR__ \ No newline at end of file From d096cfb3ab951df89afbdec7c99a8350142e549e Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Fri, 24 Nov 2023 19:23:41 +0100 Subject: [PATCH 02/13] [CONFIG] Add debug option --- smart_bridge/src/config/config.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/smart_bridge/src/config/config.h b/smart_bridge/src/config/config.h index 079a1ba..6b284f5 100644 --- a/smart_bridge/src/config/config.h +++ b/smart_bridge/src/config/config.h @@ -1,6 +1,9 @@ #ifndef __CONFIG__ #define __CONFIG__ +//DEBUG +//#define __DEBUG + // LED #define L1_PIN 7 // Green diode led #define L2_PIN 6 // Red diode led From 4078085fb434f4d42bb5f71f963049badef4d653 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Fri, 24 Nov 2023 19:24:26 +0100 Subject: [PATCH 03/13] [KERNEL] Add getter to TaskWithState --- smart_bridge/src/kernel/TaskWithState.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/smart_bridge/src/kernel/TaskWithState.h b/smart_bridge/src/kernel/TaskWithState.h index 2b23a1d..551eee0 100644 --- a/smart_bridge/src/kernel/TaskWithState.h +++ b/smart_bridge/src/kernel/TaskWithState.h @@ -4,6 +4,9 @@ #include "Task.h" #include +// TODO: Check if state managing is necessary +// TODO: Create a stateless timed task class + class TaskWithState : public Task { public: @@ -15,6 +18,11 @@ class TaskWithState : public Task this->stateTimestamp = millis(); } + int getState() + { + return this->state; + } + long timeInState() { return millis() - stateTimestamp; From 88a0c554b5d7a8043b1c07e09cf0cfcc02713b5f Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Fri, 24 Nov 2023 19:25:17 +0100 Subject: [PATCH 04/13] [CHECKIN] Add CheckInTask interface --- smart_bridge/src/tasks/CheckInTask.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 smart_bridge/src/tasks/CheckInTask.h diff --git a/smart_bridge/src/tasks/CheckInTask.h b/smart_bridge/src/tasks/CheckInTask.h new file mode 100644 index 0000000..c542f90 --- /dev/null +++ b/smart_bridge/src/tasks/CheckInTask.h @@ -0,0 +1,28 @@ +#ifndef __CHECK_IN_TASK__ +#define __CHECK_IN_TASK__ + +#include "kernel/TaskWithState.h" +#include "components/api/Led.h" +#include "config/config.h" +#include + +class CheckInTask : public TaskWithState +{ +public: + CheckInTask() : TaskWithState() + { + this->L1 = new Led(L1_PIN); + this->L2 = new Led(L2_PIN); + this->gate = new Servo(); + gate->attach(SERVO_PIN); + this->setState(0); + }; + void tick() override; + +private: + Led *L1; + Led *L2; + Servo *gate; +}; + +#endif \ No newline at end of file From 000377bcf0bc9c4a3c1e4c5f0c0a982668af8f88 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Fri, 24 Nov 2023 19:25:43 +0100 Subject: [PATCH 05/13] [CHECKIN] Impl CheckInTask --- smart_bridge/src/tasks/CheckInTask.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 smart_bridge/src/tasks/CheckInTask.cpp diff --git a/smart_bridge/src/tasks/CheckInTask.cpp b/smart_bridge/src/tasks/CheckInTask.cpp new file mode 100644 index 0000000..31aa53e --- /dev/null +++ b/smart_bridge/src/tasks/CheckInTask.cpp @@ -0,0 +1,25 @@ +#include "CheckInTask.h" + +void CheckInTask::tick() +{ + Serial.begin(9600); +#ifdef __DEBUG + Serial.println("CheckInTask::tick(): Turning on L1"); + Serial.println("CheckInTask::tick(): Time in state: " + String(timeInState())); +#endif + L1->switchOn(); + if (timeInState() > N1 * 1000) + { +#ifdef __DEBUG + Serial.println("CheckInTask::tick(): Turning off L1"); + Serial.println("CheckInTask::tick(): Turning on L2"); +#endif + L1->switchOff(); + L2->switchOn(); +#ifdef __DEBUG + Serial.println("CheckInTask::tick(): Opening gate: "); + Serial.println("CheckInTask::tick(): Time in state: " + String(timeInState())); +#endif + gate->write(90); + } +} \ No newline at end of file From ec9e74ab7f4a7b33185b0e2889cb1eb9668cf413 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Sat, 25 Nov 2023 16:43:55 +0100 Subject: [PATCH 06/13] [CONFIG] N1 is 2s now --- smart_bridge/src/config/config.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/smart_bridge/src/config/config.h b/smart_bridge/src/config/config.h index 6b284f5..0265046 100644 --- a/smart_bridge/src/config/config.h +++ b/smart_bridge/src/config/config.h @@ -29,7 +29,8 @@ /** * TODO: TIMING, time constants for example N4.... etc */ -#define N1 10 // Time for the gate to open after car presence (in seconds) +#define N1 2 + // Time for the gate to open after car presence (in seconds) #define MINDIST 10 // Minimum distance for car fully entering (in cm) #define N2 10 // Time to consider car fully entered (in seconds) #define N3 15 // Time for the washing process (in seconds) From 18d46ae219f8e124b81dbcae5769dd4050a37a44 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Sat, 25 Nov 2023 16:45:42 +0100 Subject: [PATCH 07/13] [MAIN] Example of task creation --- smart_bridge/src/main.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/smart_bridge/src/main.cpp b/smart_bridge/src/main.cpp index 6d2c6c3..d980545 100644 --- a/smart_bridge/src/main.cpp +++ b/smart_bridge/src/main.cpp @@ -1,12 +1,28 @@ #include "Arduino.h" +#include "kernel/Scheduler.h" +#include "tasks/CheckInTask.h" +Scheduler *scheduler = new Scheduler(); void setup() { Serial.begin(9600); + scheduler->init(1900); + CheckInTask *checkInTask = new CheckInTask(); + checkInTask->init(); + checkInTask->setActive(true); + + if (scheduler->addTask(checkInTask)) + { + Serial.println("Task added"); + } + else + { + Serial.println("Task not added"); + } } void loop() { - + scheduler->schedule(); } \ No newline at end of file From 5b05020046ac38e417dbbd2309529cfd897a4a40 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Sat, 25 Nov 2023 16:50:46 +0100 Subject: [PATCH 08/13] [TASKS] Create stateless TaskWithTimer --- smart_bridge/src/kernel/TaskWithTimer.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 smart_bridge/src/kernel/TaskWithTimer.h diff --git a/smart_bridge/src/kernel/TaskWithTimer.h b/smart_bridge/src/kernel/TaskWithTimer.h new file mode 100644 index 0000000..84ac120 --- /dev/null +++ b/smart_bridge/src/kernel/TaskWithTimer.h @@ -0,0 +1,21 @@ +#ifndef __TASK_WITH_TIMER__ +#define __TASK_WITH_TIMER__ + +#include "Task.h" +#include + +class TaskWithTimer : public Task +{ +public: + TaskWithTimer() : Task(){ + this->timerTimestamp = millis(); + }; // Default constructor + + long elapsedTime() + { + return millis() - timerTimestamp; + } +private: + long timerTimestamp; +}; +#endif \ No newline at end of file From eb4aa23dacbb54b31c57158373c470e866f44ea9 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Sat, 25 Nov 2023 18:00:17 +0100 Subject: [PATCH 09/13] [SCHEDULER] Fix scheduler --- smart_bridge/src/kernel/Scheduler.cpp | 67 ++++++++------------------- smart_bridge/src/kernel/Scheduler.h | 19 ++++---- smart_bridge/src/main.cpp | 10 ++-- 3 files changed, 35 insertions(+), 61 deletions(-) diff --git a/smart_bridge/src/kernel/Scheduler.cpp b/smart_bridge/src/kernel/Scheduler.cpp index bbbda57..4efd8a1 100644 --- a/smart_bridge/src/kernel/Scheduler.cpp +++ b/smart_bridge/src/kernel/Scheduler.cpp @@ -1,57 +1,30 @@ #include "Scheduler.h" #include -volatile bool timerFlag = false; +Scheduler::Scheduler() : numTasks(0), lastMillis(0) {} -void timeHandler(void) -{ - timerFlag = true; +void Scheduler::init(int intervalMillis) { + // Set up the interval for the scheduler + // You may want to use a timer or other mechanisms for more accurate timing + // For simplicity, using millis() in this example + this->intervalMillis = intervalMillis; + lastMillis = millis() - intervalMillis; } -void Scheduler::init(int period) -{ - this->period = period; - timerFlag = false; - long u_period = 1000L * period; - Timer1.initialize(u_period); - Timer1.attachInterrupt(timeHandler); - this->numTasks = 0; -} - -bool Scheduler::addTask(Task *task) -{ - if (this->numTasks < MAX_TASKS - 1) - { - this->tasks[this->numTasks++] = task; - return true; +void Scheduler::addTask(Task *task) { + if (numTasks < sizeof(tasks) / sizeof(tasks[0])) { + tasks[numTasks++] = task; } - return false; } -void Scheduler::schedule() -{ - while (!timerFlag); - timerFlag = false; - - for (int i = 0; i < this->numTasks; i++) - { - if (this->tasks[i]->isActive()) - { - if (this->tasks[i]->isPeriodic()) - { - if (this->tasks[i]->updateAndCheckTime(this->period)) - { - this->tasks[i]->tick(); - } - } - else - { - this->tasks[i]->tick(); - } - if (this->tasks[i]->isCompleted()) - { - this->tasks[i]->setActive(false); - } - } +void Scheduler::schedule() { + unsigned long currentMillis = millis(); + if (currentMillis - lastMillis >= intervalMillis) { // Adjust the interval as needed + for (int i = 0; i < numTasks; ++i) { + if (tasks[i] != nullptr && tasks[i]->isActive()) { + tasks[i]->tick(); + } } -} + lastMillis = currentMillis; + } +} \ No newline at end of file diff --git a/smart_bridge/src/kernel/Scheduler.h b/smart_bridge/src/kernel/Scheduler.h index 03be893..02dd969 100644 --- a/smart_bridge/src/kernel/Scheduler.h +++ b/smart_bridge/src/kernel/Scheduler.h @@ -3,17 +3,18 @@ #include "Task.h" -#define MAX_TASKS 30 class Scheduler { - //A simple cooperative scheduler to execute tasks - Task *tasks[MAX_TASKS]; - int numTasks; - int period; +private: + Task *tasks[10]; // Adjust the size based on the number of tasks + int numTasks; + unsigned long lastMillis; + int intervalMillis; - public: - void init(int period); - virtual bool addTask(Task *task); - virtual void schedule(); +public: + Scheduler(); + void init(int intervalMillis); + void addTask(Task *task); + void schedule(); }; #endif \ No newline at end of file diff --git a/smart_bridge/src/main.cpp b/smart_bridge/src/main.cpp index d980545..1fecbf6 100644 --- a/smart_bridge/src/main.cpp +++ b/smart_bridge/src/main.cpp @@ -2,17 +2,17 @@ #include "kernel/Scheduler.h" #include "tasks/CheckInTask.h" -Scheduler *scheduler = new Scheduler(); +Scheduler scheduler; void setup() { Serial.begin(9600); - scheduler->init(1900); + scheduler.init(100); CheckInTask *checkInTask = new CheckInTask(); - checkInTask->init(); + checkInTask->init(2100); checkInTask->setActive(true); - if (scheduler->addTask(checkInTask)) + if (scheduler.addTask(checkInTask)) { Serial.println("Task added"); } @@ -24,5 +24,5 @@ void setup() void loop() { - scheduler->schedule(); + scheduler.schedule(); } \ No newline at end of file From 059c8570e6efec33ca8398ec6c03af1d654b7f36 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Sat, 25 Nov 2023 18:00:58 +0100 Subject: [PATCH 10/13] [STATETASK] Rename method --- smart_bridge/src/kernel/TaskWithState.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart_bridge/src/kernel/TaskWithState.h b/smart_bridge/src/kernel/TaskWithState.h index 551eee0..3f9b8b8 100644 --- a/smart_bridge/src/kernel/TaskWithState.h +++ b/smart_bridge/src/kernel/TaskWithState.h @@ -23,7 +23,7 @@ class TaskWithState : public Task return this->state; } - long timeInState() + long elapsedTime() { return millis() - stateTimestamp; } From 64d61dc1682c65525fffe1c542e91498a8672394 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Sat, 25 Nov 2023 18:01:35 +0100 Subject: [PATCH 11/13] [CHECKIN] Make CheckInTask a stateful task --- smart_bridge/src/tasks/CheckInTask.cpp | 45 +++++++++++++++----------- smart_bridge/src/tasks/CheckInTask.h | 8 ++++- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/smart_bridge/src/tasks/CheckInTask.cpp b/smart_bridge/src/tasks/CheckInTask.cpp index 31aa53e..f27b686 100644 --- a/smart_bridge/src/tasks/CheckInTask.cpp +++ b/smart_bridge/src/tasks/CheckInTask.cpp @@ -1,25 +1,32 @@ #include "CheckInTask.h" +// Implement the CheckInTask class that executes the checkin process. +// The checkin process is as follows: +// 1. Turn on the L1 led. +// 2. Wait for N1 seconds. +// 3. Turn on the L2 led. +// 4. Open the gate. + void CheckInTask::tick() { - Serial.begin(9600); -#ifdef __DEBUG - Serial.println("CheckInTask::tick(): Turning on L1"); - Serial.println("CheckInTask::tick(): Time in state: " + String(timeInState())); -#endif - L1->switchOn(); - if (timeInState() > N1 * 1000) - { -#ifdef __DEBUG - Serial.println("CheckInTask::tick(): Turning off L1"); - Serial.println("CheckInTask::tick(): Turning on L2"); -#endif - L1->switchOff(); - L2->switchOn(); -#ifdef __DEBUG - Serial.println("CheckInTask::tick(): Opening gate: "); - Serial.println("CheckInTask::tick(): Time in state: " + String(timeInState())); -#endif - gate->write(90); + Serial.println("CheckInTask::started"); + switch(this->getState()) { + + case STARTED: + L1->switchOn(); // Turn on L1 + Serial.println("CheckInTask::Turned on L1"); + this->setState(WAITING); // Set the state to STATE1 + break; + case WAITING: + if (this->elapsedTime() >= (N1 * 1000)) // After N1 seconds have elapsed + { + L1->switchOff(); // Turn off L1 + Serial.println("CheckInTask::Turned off L1"); + L2->switchOn(); // Turn on L2 + Serial.println("CheckInTask::Turned on L2"); + gate->write(90); // Open the gate + Serial.println("CheckInTask::Opened the gate"); + this->setCompleted(); // Mark the task as completed + } } } \ No newline at end of file diff --git a/smart_bridge/src/tasks/CheckInTask.h b/smart_bridge/src/tasks/CheckInTask.h index c542f90..25d61bd 100644 --- a/smart_bridge/src/tasks/CheckInTask.h +++ b/smart_bridge/src/tasks/CheckInTask.h @@ -15,11 +15,17 @@ class CheckInTask : public TaskWithState this->L2 = new Led(L2_PIN); this->gate = new Servo(); gate->attach(SERVO_PIN); - this->setState(0); + Serial.println("CheckInTask created"); + this->setState(STARTED); }; void tick() override; private: + enum states + { + STARTED, + WAITING + }; Led *L1; Led *L2; Servo *gate; From 032ba8173ab0dcc0e93d1198c5dc934c89cd15ff Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Sat, 25 Nov 2023 18:01:56 +0100 Subject: [PATCH 12/13] [TEST TASK] Add a simple blinking task --- smart_bridge/src/tasks/BlinkTask.cpp | 28 ++++++++++++++++++++++++++++ smart_bridge/src/tasks/BlinkTask.h | 20 ++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 smart_bridge/src/tasks/BlinkTask.cpp create mode 100644 smart_bridge/src/tasks/BlinkTask.h diff --git a/smart_bridge/src/tasks/BlinkTask.cpp b/smart_bridge/src/tasks/BlinkTask.cpp new file mode 100644 index 0000000..2e40344 --- /dev/null +++ b/smart_bridge/src/tasks/BlinkTask.cpp @@ -0,0 +1,28 @@ +#include "BlinkTask.h" + +BlinkTask::BlinkTask(int pin) +{ + this->pin = pin; +} + +void BlinkTask::init(int period) +{ + Task::init(period); + led = new Led(pin); + state = OFF; +} + +void BlinkTask::tick() +{ + switch (state) + { + case OFF: + led->switchOn(); + state = ON; + break; + case ON: + led->switchOff(); + state = OFF; + break; + } +} diff --git a/smart_bridge/src/tasks/BlinkTask.h b/smart_bridge/src/tasks/BlinkTask.h new file mode 100644 index 0000000..9143c4f --- /dev/null +++ b/smart_bridge/src/tasks/BlinkTask.h @@ -0,0 +1,20 @@ +#ifndef __BLINK_TASK__ +#define __BLINK_TASK__ + +#include "kernel/Task.h" +#include "components/api/Led.h" + +class BlinkTask: public Task { + + int pin; + Light* led; + enum { ON, OFF} state; + +public: + + BlinkTask(int pin); + void init(int period); + void tick(); +}; + +#endif \ No newline at end of file From 75acd7f657fdb2e553189c104a6fd488a18d02f8 Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Sat, 25 Nov 2023 18:02:16 +0100 Subject: [PATCH 13/13] [MAIN] Correct usage of Scheduler and tasks --- smart_bridge/src/main.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/smart_bridge/src/main.cpp b/smart_bridge/src/main.cpp index 1fecbf6..354e257 100644 --- a/smart_bridge/src/main.cpp +++ b/smart_bridge/src/main.cpp @@ -1,25 +1,25 @@ #include "Arduino.h" #include "kernel/Scheduler.h" +#include "tasks/BlinkTask.h" #include "tasks/CheckInTask.h" +#include "config/config.h" Scheduler scheduler; void setup() { Serial.begin(9600); - scheduler.init(100); + scheduler.init(500); + //NOTE: THIS IS JUST A TEST TASK + BlinkTask *blinkTask = new BlinkTask(L3_PIN); + blinkTask->init(500); + blinkTask->setActive(true); + //NOTE: THIS IS THE REAL TASK CheckInTask *checkInTask = new CheckInTask(); - checkInTask->init(2100); + checkInTask->init(); checkInTask->setActive(true); - - if (scheduler.addTask(checkInTask)) - { - Serial.println("Task added"); - } - else - { - Serial.println("Task not added"); - } + scheduler.addTask(checkInTask); + scheduler.addTask(blinkTask); } void loop()