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 diff --git a/smart_bridge/src/config/config.h b/smart_bridge/src/config/config.h index 079a1ba..0265046 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 @@ -26,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) 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/kernel/TaskWithState.h b/smart_bridge/src/kernel/TaskWithState.h index 2b23a1d..3f9b8b8 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,7 +18,12 @@ class TaskWithState : public Task this->stateTimestamp = millis(); } - long timeInState() + int getState() + { + return this->state; + } + + long elapsedTime() { return millis() - stateTimestamp; } 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 diff --git a/smart_bridge/src/main.cpp b/smart_bridge/src/main.cpp index 6d2c6c3..354e257 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/BlinkTask.h" +#include "tasks/CheckInTask.h" +#include "config/config.h" +Scheduler scheduler; void setup() { Serial.begin(9600); + 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(); + checkInTask->setActive(true); + scheduler.addTask(checkInTask); + scheduler.addTask(blinkTask); } void loop() { - + scheduler.schedule(); } \ No newline at end of file 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 diff --git a/smart_bridge/src/tasks/CheckInTask.cpp b/smart_bridge/src/tasks/CheckInTask.cpp new file mode 100644 index 0000000..f27b686 --- /dev/null +++ b/smart_bridge/src/tasks/CheckInTask.cpp @@ -0,0 +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.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 new file mode 100644 index 0000000..25d61bd --- /dev/null +++ b/smart_bridge/src/tasks/CheckInTask.h @@ -0,0 +1,34 @@ +#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); + Serial.println("CheckInTask created"); + this->setState(STARTED); + }; + void tick() override; + +private: + enum states + { + STARTED, + WAITING + }; + Led *L1; + Led *L2; + Servo *gate; +}; + +#endif \ No newline at end of file