Skip to content

Commit

Permalink
Merge pull request #36 from aleemont1/alessandro
Browse files Browse the repository at this point in the history
Optimize ram consumption
  • Loading branch information
aleemont1 authored Dec 17, 2023
2 parents ae7961d + 004fbc6 commit 13b02cf
Show file tree
Hide file tree
Showing 28 changed files with 107 additions and 112 deletions.
2 changes: 1 addition & 1 deletion smart_bridge/lib/README
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.

The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
("lib/your_library_name/[here are source files]")).

For example, see a structure of the following two libraries `Foo` and `Bar`:

Expand Down
3 changes: 1 addition & 2 deletions smart_bridge/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ board = uno
framework = arduino
lib_deps =
marcoschwartz/LiquidCrystal_I2C@^1.1.4
Wire
paulstoffregen/TimerOne@^1.1.1
nabontra/ServoTimer2@0.0.0-alpha+sha.2bf7fb3c17
Wire
2 changes: 1 addition & 1 deletion smart_bridge/src/UI/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def start_serial():
global serialInst
serialInst = serial.Serial()
serialInst.baudrate = 9600
serialInst.port = 'COM3' #COM3 depends on the port where Arduino is connected
serialInst.port = '/dev/ttyACM0' #COM3 depends on the port where Arduino is connected
serialInst.open()
root.after(100, update_serial)

Expand Down
3 changes: 2 additions & 1 deletion smart_bridge/src/components/impl/LCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ void LCD::clear()

void LCD::write(const char *string, int start_col, int start_rows)
{
Serial.println("LCD::Sto scrivendo sul display" + String(string));
// Serial.print(F("LCD::Sto scrivendo sul display "));
// Serial.println(String(string));
lcd.setCursor(start_col, start_rows);
lcd.print(string);
}
2 changes: 1 addition & 1 deletion smart_bridge/src/kernel/DependantTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DependantTask : virtual public Task
{
if (nDependencies <= MAX_DEPENDENCIES)
{
Serial.println("DependantTask::addDependency()");
// Serial.println(F("DependantTask::addDependency()"));
this->dependencies[nDependencies++] = dependency;
}
}
Expand Down
8 changes: 4 additions & 4 deletions smart_bridge/src/kernel/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void Scheduler::addTask(Task *task)
{
tasks[numTasks++] = task;
#ifdef __DEBUG
Serial.println("tasks[" + String(numTasks - 1) + "]: [" + String(reinterpret_cast<uintptr_t>(tasks[numTasks - 1])) + "]");
// Serial.println(F("tasks[" + String(numTasks - 1) + "]: [" + String(reinterpret_cast<uintptr_t>(tasks[numTasks - 1])) + "]"));
#endif
}
}
Expand All @@ -35,16 +35,16 @@ void Scheduler::schedule()
if (tasks[i]->updateAndCheckTime(schedulerPeriod)) // Check if it's time to execute
{
#ifdef __DEBUG
Serial.println("Scheduler::tasks[" + String(i) + "]: " + String(reinterpret_cast<uintptr_t>(tasks[i])) + " tick");
// Serial.println(F("Scheduler::tasks[" + String(i) + "]: " + String(reinterpret_cast<uintptr_t>(tasks[i])) + " tick"));
#endif
tasks[i]->tick(); // Execute task
// Serial.println("Scheduler::numTasks: " + numTasks);
// // Serial.println(F("Scheduler::numTasks: " + numTasks);
}
}
else // Task is aperiodic
{
#ifdef __DEBUG
Serial.println("Scheduler::tasks[" + String(i) + "]: " + String(reinterpret_cast<uintptr_t>(tasks[i])) + " tick");
// Serial.println(F("Scheduler::tasks[" + String(i) + "]: " + String(reinterpret_cast<uintptr_t>(tasks[i])) + " tick"));
#endif
tasks[i]->tick(); // Execute task without checking time
}
Expand Down
2 changes: 1 addition & 1 deletion smart_bridge/src/kernel/Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "Task.h"

#define MAX_TASKS 50
#define MAX_TASKS 15

/**
* @class Scheduler
Expand Down
28 changes: 14 additions & 14 deletions smart_bridge/src/kernel/SerialReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

bool SerialReceiver::readData() {

if (this->simulateDataReceived) {
this->simulateDataReceived = false;
return true;
}

// if (Serial.available() > 0) { //if some data has been received
// String data = Serial.readString();
// data.trim();
// if (data=="mnt:done") {
// Serial.println("Arduino received message: " + data);
// return true;
// }
// if (this->simulateDataReceived) {
// this->simulateDataReceived = false;
// return true;
// }
// return false;

if (Serial.available() > 0) { //if some data has been received
String data = Serial.readString();
data.trim();
if (data=="mnt:done") {
// Serial.println(F("Arduino received message: " + data);
return true;
}
}
return false;
}

void SerialReceiver::simulateReadData() {
this->simulateDataReceived = true;
Serial.println("Arduino received message: simulates mnt:done");
// Serial.println(F("Arduino received message: simulates mnt:done"));
}
9 changes: 1 addition & 8 deletions smart_bridge/src/kernel/TaskWithState.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
class TaskWithState : public TaskWithTimer
{
public:
TaskWithState() : TaskWithTimer(), previousState(0){}; // Default constructor
TaskWithState() : TaskWithTimer(){}; // Default constructor

void setState(const int state)
{
this->previousState = this->state;
this->state = state;
this->stateTimestamp = millis();
}
Expand All @@ -28,13 +27,7 @@ class TaskWithState : public TaskWithTimer
return this->state;
}

int getPreviousState()
{
return this->previousState;
}

private:
int previousState;
int state;
long stateTimestamp;
};
Expand Down
72 changes: 35 additions & 37 deletions smart_bridge/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,64 @@
#include "Arduino.h"
// #include "Arduino.h"

#include "config/config.h"

#include "kernel/Scheduler.h"
#include "kernel/SerialReceiver.h"

#include "components/api/LCD.h"

#include "tasks/BlinkTask.h"
#include "tasks/CountDown.h"
#include "tasks/TemperatureTask.h"
#include "tasks/SleepingTask.h"
#include "tasks/CheckInTask.h"
#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(50); // NOTE: Might be set higher to use less power, needs testing.

/**CREATE TASKS**/
CheckInTask *checkInTask = new CheckInTask();
BlinkTask *blinkTask = new BlinkTask(L2_PIN);
TransitTask *transitTask = new TransitTask(blinkTask);
WaitForClickTask *waitForClickTask = new WaitForClickTask();
CountDown *countDownTask = new CountDown(N3);
TemperatureTask *temperatureTask = new TemperatureTask();
WashingTask *washingTask = new WashingTask(blinkTask, countDownTask, temperatureTask);
CheckOutTask *checkOutTask = new CheckOutTask();
ExitTransitTask *exitTransitTask = new ExitTransitTask();

BlinkTask blinkTask = BlinkTask(L2_PIN);
CountDown countDownTask = CountDown(N3);
TemperatureTask temperatureTask = TemperatureTask();
SleepingTask sleepingTask = SleepingTask();
CheckInTask checkInTask = CheckInTask();
TransitTask transitTask = TransitTask(&blinkTask);
WaitForClickTask waitForClickTask = WaitForClickTask();
WashingTask washingTask = WashingTask(&blinkTask, nullptr, &temperatureTask);
CheckOutTask checkOutTask = CheckOutTask();
ExitTransitTask exitTransitTask = ExitTransitTask();
/**DEPENDENCIES**/
transitTask->addDependency(checkInTask);
waitForClickTask->addDependency(transitTask);
washingTask->addDependency(waitForClickTask);
checkOutTask->addDependency(washingTask);
exitTransitTask->addDependency(checkOutTask);
checkInTask.addDependency(&sleepingTask);
transitTask.addDependency(&checkInTask);
waitForClickTask.addDependency(&transitTask);
washingTask.addDependency(&waitForClickTask);
checkOutTask.addDependency(&washingTask);
exitTransitTask.addDependency(&checkOutTask);

/**ADD TASKS TO THE SCHEDULER**/
scheduler.addTask(checkInTask);
scheduler.addTask(transitTask);
scheduler.addTask(blinkTask);
scheduler.addTask(waitForClickTask);
scheduler.addTask(washingTask);
scheduler.addTask(countDownTask);
scheduler.addTask(temperatureTask);
scheduler.addTask(checkOutTask);
scheduler.addTask(exitTransitTask);
scheduler.addTask(&sleepingTask);
scheduler.addTask(&checkInTask);
scheduler.addTask(&transitTask);
scheduler.addTask(&blinkTask);
scheduler.addTask(&countDownTask);
scheduler.addTask(&temperatureTask);
scheduler.addTask(&waitForClickTask);
scheduler.addTask(&washingTask);
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
scheduler.schedule();
}
4 changes: 2 additions & 2 deletions smart_bridge/src/tasks/BlinkTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BlinkTask : public TaskWithState
*/
BlinkTask(int pin, int period) : TaskWithState()
{
Serial.println("BlinkTask created");
// Serial.println(F("BlinkTask created"));
this->pin = pin;
this->init(period);
}
Expand All @@ -31,7 +31,7 @@ class BlinkTask : public TaskWithState
*/
BlinkTask(int pin) : TaskWithState()
{
Serial.println("BlinkTask created");
// Serial.println(F("BlinkTask created"));
this->pin = pin;
if (this->isActive())
{
Expand Down
6 changes: 3 additions & 3 deletions smart_bridge/src/tasks/CheckInTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void CheckInTask::tick()
lcd->write("SMART WASHING", 0, 1);
L1->switchOn(); // Turn on L1
#ifdef __LOG
Serial.println("CheckInTask::Turned on L1");
// Serial.println(F("CheckInTask::Turned on L1"));
#endif
this->resetTime(); // Reset the elapsed time
this->setState(WAITING); // Set the state to WAITING
Expand All @@ -21,11 +21,11 @@ void CheckInTask::tick()
{
L1->switchOff(); // Turn off L1
#ifdef __LOG
Serial.println("CheckInTask::Turned off L1");
// Serial.println(F("CheckInTask::Turned off L1"));
#endif
gate->write(90); // Open the gate
#ifdef __LOG
Serial.println("CheckInTask::Opened the gate");
// Serial.println(F("CheckInTask::Opened the gate"));
#endif
this->setCompleted(); // Mark the task as completed
}
Expand Down
2 changes: 1 addition & 1 deletion smart_bridge/src/tasks/CheckInTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CheckInTask : public DependantTaskWithState
this->gate = new ServoImpl(SERVO_PIN);
this->init();
this->setState(STARTED);
Serial.println("CheckInTask created");
// Serial.println(F("CheckInTask created"));
};
void tick() override;

Expand Down
6 changes: 3 additions & 3 deletions smart_bridge/src/tasks/CheckOutTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CheckOutTask::CheckOutTask()
GATE_OPEN_POSITION(90),
GATE_CLOSE_POSITION(0)
{
Serial.println("CheckOutTask created");
// Serial.println(F("CheckOutTask created"));
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);
Expand Down Expand Up @@ -39,13 +39,13 @@ void CheckOutTask::tick()
void CheckOutTask::handleTurnOnL3()
{
this->L3->switchOn();
Serial.println("CheckOutTask::L3 turned on");
// Serial.println(F("CheckOutTask::L3 turned on"));
this->setState(OPENS_GATE);
}

void CheckOutTask::handleOpensGate()
{
this->gate->write(GATE_OPEN_POSITION);
Serial.println("CheckOutTask::Gate opened");
// Serial.println(F("CheckOutTask::Gate opened"));
this->setCompleted();
}
6 changes: 3 additions & 3 deletions smart_bridge/src/tasks/CountDown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

CountDown::CountDown(int countDown) : Task()
{
Serial.println("CountDownTask created");
// Serial.println(F("CountDownTask created"));
this->setStatus(false);
this->lcd = new LCD(0x27, 16, 2);
this->resetCountDown(N3);
Expand Down Expand Up @@ -64,14 +64,14 @@ void CountDown::endsCountDown()
this->isCompleted();
this->printsEndsCountdown();
this->resetCountDown(N3);
Serial.println("CountDown::Countdown resetted");
// Serial.println(F("CountDown::Countdown resetted"));
this->setCompleted();
}
}

void CountDown::printsEndsCountdown()
{
Serial.println("CountDown::Countdown ended!");
// Serial.println(F("CountDown::Countdown ended!"));
}

bool CountDown::isCountDownActive()
Expand Down
8 changes: 4 additions & 4 deletions smart_bridge/src/tasks/ExitTransitTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ExitTransitTask::ExitTransitTask()
GATE_OPEN_POSITION(90),
GATE_CLOSE_POSITION(0)
{
Serial.println("ExitTransitTask created");
// Serial.println(F("ExitTransitTask created"));
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);
Expand Down Expand Up @@ -46,7 +46,7 @@ void ExitTransitTask::tick()
void ExitTransitTask::handleReadingDistance()
{
this->distance = sonar->detectDistance();
Serial.println("ExitTransitTask::Distance: " + String(this->distance));
// Serial.println("ExitTransitTask::Distance: " + String(this->distance));
this->setState(CHECKING_DISTANCE);
}

Expand All @@ -66,13 +66,13 @@ void ExitTransitTask::handleCheckingDistance()
void ExitTransitTask::handleClosesGate()
{
this->gate->write(GATE_CLOSE_POSITION);
Serial.println("ExitTransitTask::Gate closed");
// Serial.println(F("ExitTransitTask::Gate closed"));
this->setState(SWITCH_OFF_L3);
}

void ExitTransitTask::handleSwitchOffL3()
{
this->L3->switchOff();
Serial.println("ExitTransitTask::L3 switched off");
// Serial.println(F("ExitTransitTask::L3 switched off"));
this->setCompleted();
}
Loading

0 comments on commit 13b02cf

Please sign in to comment.