Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…nment2 into emanuele
  • Loading branch information
23Emaaaa committed Dec 5, 2023
2 parents 404563f + a51d475 commit 7df3430
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 52 deletions.
4 changes: 2 additions & 2 deletions smart_bridge/src/components/api/LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions smart_bridge/src/components/api/Pir.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
2 changes: 2 additions & 0 deletions smart_bridge/src/components/impl/Pir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 3 additions & 7 deletions smart_bridge/src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 28 additions & 26 deletions smart_bridge/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
45 changes: 43 additions & 2 deletions smart_bridge/src/tasks/SleepingTask.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
#include "SleepingTask.h"

SleepingTask* sleepingTaskInstance = nullptr;

static void staticWakeUp() {
if (sleepingTaskInstance != nullptr) {
sleepingTaskInstance->wakeUp();
}
}

void SleepingTask::tick()
{
/*TODO*/
}
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);
}
27 changes: 18 additions & 9 deletions smart_bridge/src/tasks/SleepingTask.h
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion smart_bridge/src/tasks/WaitingTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}
}
}
15 changes: 10 additions & 5 deletions smart_bridge/src/tasks/WaitingTask.h
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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;
};
Expand Down

0 comments on commit 7df3430

Please sign in to comment.