From 4a414ea70f5ede4aa97d5193ca3c2cef1ff4c5e9 Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Tue, 18 Oct 2022 15:21:34 +0300 Subject: [PATCH 01/10] StateHandler: add functionality to control fan speed in Manual mode. Main: Modbus object + pressure display --- StateHandler/inc/StateHandler.h | 5 ++++- StateHandler/src/StateHandler.cpp | 7 ++++++- esp-vent-main/src/esp-vent-main.cpp | 19 +++++++++++++++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/StateHandler/inc/StateHandler.h b/StateHandler/inc/StateHandler.h index e95b025..174d53f 100644 --- a/StateHandler/inc/StateHandler.h +++ b/StateHandler/inc/StateHandler.h @@ -19,6 +19,8 @@ #include "DigitalIoPin.h" #include "Event.h" #include "LiquidCrystal.h" +#include "ModbusMaster.h" +#include "ModbusRegister.h" /** Buttons enumeration * @@ -63,7 +65,7 @@ typedef void (StateHandler::*state_pointer) (const Event &); class StateHandler { public: - StateHandler (LiquidCrystal *lcd); + StateHandler (LiquidCrystal *lcd, ModbusRegister *A01); virtual ~StateHandler (); /** Get currently set pressure @@ -106,6 +108,7 @@ private: int saved_set_value[2] = { 0, 0 }; int saved_curr_value[2] = { 0, 0 }; LiquidCrystal *_lcd; + ModbusRegister *A01; /** Initialization state * diff --git a/StateHandler/src/StateHandler.cpp b/StateHandler/src/StateHandler.cpp index e56c2c8..535f1d1 100644 --- a/StateHandler/src/StateHandler.cpp +++ b/StateHandler/src/StateHandler.cpp @@ -7,9 +7,10 @@ #include -StateHandler::StateHandler (LiquidCrystal *lcd) +StateHandler::StateHandler (LiquidCrystal *lcd, ModbusRegister *A01) { this->_lcd = lcd; + this->A01 = A01; current = &StateHandler::stateInit; (this->*current) (Event (Event::eEnter)); current_mode = MANUAL; @@ -143,9 +144,13 @@ StateHandler::handleControlButtons (uint8_t button) { case BUTTON_CONTROL_DOWN: this->value[(current_mode) ? AUTO : MANUAL].dec (); + if(current_mode == MANUAL) + this->A01->write(value[(current_mode) ? AUTO : MANUAL].getCurrent() * 10); break; case BUTTON_CONTROL_UP: this->value[(current_mode) ? AUTO : MANUAL].inc (); + if(current_mode == MANUAL) + this->A01->write(value[(current_mode) ? AUTO : MANUAL].getCurrent() * 10); break; case BUTTON_CONTROL_TOG_MODE: current_mode = !current_mode; diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp index 2ed9296..ff404df 100644 --- a/esp-vent-main/src/esp-vent-main.cpp +++ b/esp-vent-main/src/esp-vent-main.cpp @@ -55,11 +55,17 @@ main (void) DigitalIoPin d6 (1, 3, false, true, false); DigitalIoPin d7 (0, 0, false, true, false); LiquidCrystal lcd (&rs, &en, &d4, &d5, &d6, &d7); - StateHandler ventMachine (&lcd); // lcd.setCursor (0, 0); lcd.print ("Test"); + /* FAN object */ + ModbusMaster fan(1); + fan.begin(9600); + ModbusRegister A01(&fan, 0); + ModbusRegister DI1(&fan, 4, false); + + StateHandler ventMachine (&lcd, &A01); /** Common pins */ DigitalIoPin b_up (0, 7, true, true, true); // A5 SwitchController sw_up (&b_up, &glob_time, &ventMachine, BUTTON_CONTROL_UP); @@ -73,9 +79,9 @@ main (void) BUTTON_CONTROL_TOG_MODE); - PressureWrapper sens(); - + PressureWrapper sens; + int pressure = 0, pressure_time = 0; while (1) { @@ -87,8 +93,13 @@ main (void) * TODO: * - Update current pressure to eTick */ - ventMachine.HandleState (Event (Event::eTick)); + if(pressure_time == 100) { + pressure = sens.getPressure(); + pressure_time = 0; + } + ventMachine.HandleState (Event (Event::eTick, pressure)); glob_time.tickCounter (1); + ++pressure_time; } return 0; From 2de40ba7c086d54eabf935f68495e41fa16792d0 Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Tue, 18 Oct 2022 17:43:13 +0300 Subject: [PATCH 02/10] StateHandler: stateAuto: fan speed control by changing pressure value. Without PID regulation. --- StateHandler/inc/StateHandler.h | 6 ++++++ StateHandler/src/StateHandler.cpp | 15 +++++++++++++++ esp-vent-main/src/PressureWrapper.cpp | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/StateHandler/inc/StateHandler.h b/StateHandler/inc/StateHandler.h index 174d53f..97288d5 100644 --- a/StateHandler/inc/StateHandler.h +++ b/StateHandler/inc/StateHandler.h @@ -105,6 +105,7 @@ private: void SetState (state_pointer newstate); bool current_mode; Counter value[2] = { { 0, 100 }, { 0, 120 } }; + Counter fan_speed = {80, 1000}; int saved_set_value[2] = { 0, 0 }; int saved_curr_value[2] = { 0, 0 }; LiquidCrystal *_lcd; @@ -148,6 +149,11 @@ private: * @param mode current mode */ void save (int eventValue, int counterValue, size_t mode); + + /** Calculates pid for fan control value + * + */ + void pid (); }; #endif /* STATE_HANDLER_H_ */ diff --git a/StateHandler/src/StateHandler.cpp b/StateHandler/src/StateHandler.cpp index 535f1d1..53c104a 100644 --- a/StateHandler/src/StateHandler.cpp +++ b/StateHandler/src/StateHandler.cpp @@ -104,6 +104,7 @@ StateHandler::stateManual (const Event &event) { case Event::eEnter: displaySet (saved_set_value[MANUAL], saved_curr_value[MANUAL]); + this->A01->write(this->value[FAN_SPEED].getCurrent ()); break; case Event::eExit: _lcd->clear (); @@ -133,6 +134,13 @@ StateHandler::stateAuto (const Event &event) break; case Event::eTick: save (event.value, value[AUTO].getCurrent (), AUTO); + if(saved_curr_value[AUTO] < saved_set_value[AUTO]) { + fan_speed.inc(); + this->A01->write(fan_speed.getCurrent()); + } else if(saved_curr_value[AUTO] > saved_set_value[AUTO]){ + fan_speed.dec(); + this->A01->write(fan_speed.getCurrent()); + } break; } } @@ -172,3 +180,10 @@ StateHandler::save (int eventValue, int counterValue, size_t mode) displaySet (saved_set_value[mode], saved_curr_value[mode]); } } + +void StateHandler::pid () { + int integral = 0, error = 0; + error = saved_curr_value[AUTO] - saved_set_value[AUTO]; + +} + diff --git a/esp-vent-main/src/PressureWrapper.cpp b/esp-vent-main/src/PressureWrapper.cpp index 1bebb57..e5cd2e6 100644 --- a/esp-vent-main/src/PressureWrapper.cpp +++ b/esp-vent-main/src/PressureWrapper.cpp @@ -39,7 +39,7 @@ int PressureWrapper::getPressure() { int16_t pressure = 0; if(!getRawPressure ()) { unsigned int i = 0; - while(i<7200000) i++; + while(i<7200) i++; getRawPressure (); i = 0; } From 4b1ef018ddab7f6e3be48b7430fc90bb3e8a8a6c Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Wed, 19 Oct 2022 09:46:55 +0300 Subject: [PATCH 03/10] StateHandler: first version of pid() for fan --- StateHandler/inc/StateHandler.h | 9 ++++++++- StateHandler/src/StateHandler.cpp | 17 ++++++++--------- esp-vent-main/src/esp-vent-main.cpp | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/StateHandler/inc/StateHandler.h b/StateHandler/inc/StateHandler.h index 97288d5..9c17622 100644 --- a/StateHandler/inc/StateHandler.h +++ b/StateHandler/inc/StateHandler.h @@ -105,7 +105,14 @@ private: void SetState (state_pointer newstate); bool current_mode; Counter value[2] = { { 0, 100 }, { 0, 120 } }; - Counter fan_speed = {80, 1000}; + /* motor of fan starts at value 90. probably because of some + * weigh of fan, so voltage within range of 0-89 is not + * sufficient to start motor. + * TODO: Value 89 should be scaled to 0 at some point */ + Counter fan_speed = {89, 1000}; + /*integral controller for PID. should be global, since it + * accumulates error signals encountered since startup*/ + int integral = 0; int saved_set_value[2] = { 0, 0 }; int saved_curr_value[2] = { 0, 0 }; LiquidCrystal *_lcd; diff --git a/StateHandler/src/StateHandler.cpp b/StateHandler/src/StateHandler.cpp index 53c104a..7ff451f 100644 --- a/StateHandler/src/StateHandler.cpp +++ b/StateHandler/src/StateHandler.cpp @@ -134,13 +134,8 @@ StateHandler::stateAuto (const Event &event) break; case Event::eTick: save (event.value, value[AUTO].getCurrent (), AUTO); - if(saved_curr_value[AUTO] < saved_set_value[AUTO]) { - fan_speed.inc(); - this->A01->write(fan_speed.getCurrent()); - } else if(saved_curr_value[AUTO] > saved_set_value[AUTO]){ - fan_speed.dec(); - this->A01->write(fan_speed.getCurrent()); - } + pid(); + this->A01->write(fan_speed.getCurrent()); break; } } @@ -182,8 +177,12 @@ StateHandler::save (int eventValue, int counterValue, size_t mode) } void StateHandler::pid () { - int integral = 0, error = 0; + float kP = 0.7, kI = 0.7, kD = 0.7; + int error = 0, last_error = 0, derivative = 0; error = saved_curr_value[AUTO] - saved_set_value[AUTO]; - + last_error = error; + integral += error; + derivative = error - last_error; + fan_speed.setInit((kP*error) + (kI*integral) + (kD * derivative)); } diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp index ff404df..5ebf321 100644 --- a/esp-vent-main/src/esp-vent-main.cpp +++ b/esp-vent-main/src/esp-vent-main.cpp @@ -93,7 +93,7 @@ main (void) * TODO: * - Update current pressure to eTick */ - if(pressure_time == 100) { + if(pressure_time == 80) { pressure = sens.getPressure(); pressure_time = 0; } From 290c73f358c8a1f310dacaf9fc7ab260d6e65f0b Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Wed, 19 Oct 2022 09:58:59 +0300 Subject: [PATCH 04/10] Counter: setInit(): fix. Init can't be set to lower of down_lim or higher of up_lim --- StateHandler/src/Counter.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/StateHandler/src/Counter.cpp b/StateHandler/src/Counter.cpp index 571e6c8..39cec29 100644 --- a/StateHandler/src/Counter.cpp +++ b/StateHandler/src/Counter.cpp @@ -50,7 +50,15 @@ Counter::Counter (unsigned int down, unsigned int up) } void -Counter::setInit (unsigned int i) +Counter::setInit (unsigned int newInit) { - init = i; + if(newInit > up_lim){ + init = up_lim; + } + else if(newInit < down_lim){ + init = down_lim; + } + else{ + init = newInit; + } } From 0d70f8b464694657fbe67133190b73fcc2ffef60 Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Wed, 19 Oct 2022 16:46:32 +0300 Subject: [PATCH 05/10] StateHandler: simple fan speed adjustment. pid() needs improvments --- StateHandler/inc/StateHandler.h | 2 +- StateHandler/src/StateHandler.cpp | 18 ++++++++++++++---- esp-vent-main/src/esp-vent-main.cpp | 7 +++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/StateHandler/inc/StateHandler.h b/StateHandler/inc/StateHandler.h index 9c17622..50e2a9c 100644 --- a/StateHandler/inc/StateHandler.h +++ b/StateHandler/inc/StateHandler.h @@ -109,7 +109,7 @@ private: * weigh of fan, so voltage within range of 0-89 is not * sufficient to start motor. * TODO: Value 89 should be scaled to 0 at some point */ - Counter fan_speed = {89, 1000}; + Counter fan_speed = {20, 1000}; /*integral controller for PID. should be global, since it * accumulates error signals encountered since startup*/ int integral = 0; diff --git a/StateHandler/src/StateHandler.cpp b/StateHandler/src/StateHandler.cpp index 7ff451f..9fc2258 100644 --- a/StateHandler/src/StateHandler.cpp +++ b/StateHandler/src/StateHandler.cpp @@ -134,8 +134,18 @@ StateHandler::stateAuto (const Event &event) break; case Event::eTick: save (event.value, value[AUTO].getCurrent (), AUTO); - pid(); - this->A01->write(fan_speed.getCurrent()); + int i = 0; +// pid(); +// this->A01->write(fan_speed.getCurrent()); + if(saved_curr_value[AUTO] < saved_set_value[AUTO]) { + fan_speed.inc(); + this->A01->write(fan_speed.getCurrent()); + } else if(saved_curr_value[AUTO] > saved_set_value[AUTO]){ + fan_speed.dec(); + while(i<720) i++; + i = 0; + this->A01->write(fan_speed.getCurrent()); + } break; } } @@ -177,9 +187,9 @@ StateHandler::save (int eventValue, int counterValue, size_t mode) } void StateHandler::pid () { - float kP = 0.7, kI = 0.7, kD = 0.7; + float kP = 0.001, kI = 0.001, kD = 0.001; int error = 0, last_error = 0, derivative = 0; - error = saved_curr_value[AUTO] - saved_set_value[AUTO]; + error = saved_set_value[AUTO] - saved_curr_value[AUTO]; last_error = error; integral += error; derivative = error - last_error; diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp index 5ebf321..be77b29 100644 --- a/esp-vent-main/src/esp-vent-main.cpp +++ b/esp-vent-main/src/esp-vent-main.cpp @@ -63,7 +63,8 @@ main (void) ModbusMaster fan(1); fan.begin(9600); ModbusRegister A01(&fan, 0); - ModbusRegister DI1(&fan, 4, false); +// ModbusRegister DI1(&fan, 4, false); + StateHandler ventMachine (&lcd, &A01); /** Common pins */ @@ -78,8 +79,6 @@ main (void) SwitchController sw_toggle (&b_toggle, &glob_time, &ventMachine, BUTTON_CONTROL_TOG_MODE); - - PressureWrapper sens; int pressure = 0, pressure_time = 0; @@ -93,7 +92,7 @@ main (void) * TODO: * - Update current pressure to eTick */ - if(pressure_time == 80) { + if(pressure_time == 5) { pressure = sens.getPressure(); pressure_time = 0; } From ab184498e19e9753bdb925532615d7447aecd96c Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Thu, 20 Oct 2022 14:48:40 +0300 Subject: [PATCH 06/10] root: init sensors: humidity+temp, CO2. Pressure sensor is now static class --- SDP600Sensor/inc/PressureWrapper.h | 0 .../src/PressureWrapper.cpp | 0 StateHandler/inc/StateHandler.h | 8 ++++ StateHandler/src/StateHandler.cpp | 3 ++ esp-vent-main/src/PressureWrapper.h | 48 ------------------- 5 files changed, 11 insertions(+), 48 deletions(-) create mode 100644 SDP600Sensor/inc/PressureWrapper.h rename {esp-vent-main => SDP600Sensor}/src/PressureWrapper.cpp (100%) delete mode 100644 esp-vent-main/src/PressureWrapper.h diff --git a/SDP600Sensor/inc/PressureWrapper.h b/SDP600Sensor/inc/PressureWrapper.h new file mode 100644 index 0000000..e69de29 diff --git a/esp-vent-main/src/PressureWrapper.cpp b/SDP600Sensor/src/PressureWrapper.cpp similarity index 100% rename from esp-vent-main/src/PressureWrapper.cpp rename to SDP600Sensor/src/PressureWrapper.cpp diff --git a/StateHandler/inc/StateHandler.h b/StateHandler/inc/StateHandler.h index 50e2a9c..7d014f9 100644 --- a/StateHandler/inc/StateHandler.h +++ b/StateHandler/inc/StateHandler.h @@ -21,6 +21,8 @@ #include "LiquidCrystal.h" #include "ModbusMaster.h" #include "ModbusRegister.h" +#include "GMP252.h" +#include "HMP60.h" /** Buttons enumeration * @@ -118,6 +120,12 @@ private: LiquidCrystal *_lcd; ModbusRegister *A01; + /* CO2 sensor object */ + GMP252 co2; + + /* Humidity and temperature sensor object */ + HMP60 humidity; + /** Initialization state * * @param event event of the state diff --git a/StateHandler/src/StateHandler.cpp b/StateHandler/src/StateHandler.cpp index 9fc2258..884d2bd 100644 --- a/StateHandler/src/StateHandler.cpp +++ b/StateHandler/src/StateHandler.cpp @@ -139,6 +139,8 @@ StateHandler::stateAuto (const Event &event) // this->A01->write(fan_speed.getCurrent()); if(saved_curr_value[AUTO] < saved_set_value[AUTO]) { fan_speed.inc(); + while(i<720) i++; + i = 0; this->A01->write(fan_speed.getCurrent()); } else if(saved_curr_value[AUTO] > saved_set_value[AUTO]){ fan_speed.dec(); @@ -150,6 +152,7 @@ StateHandler::stateAuto (const Event &event) } } + void StateHandler::handleControlButtons (uint8_t button) { diff --git a/esp-vent-main/src/PressureWrapper.h b/esp-vent-main/src/PressureWrapper.h deleted file mode 100644 index 6ee2936..0000000 --- a/esp-vent-main/src/PressureWrapper.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * PressureWrapper.h - * - * Created on: 5 Oct 2022 - * Author: evgenymeshcheryakov - */ - -#ifndef PRESSUREWRAPPER_H_ -#define PRESSUREWRAPPER_H_ - -#include "I2C.h" -#include - -#define ADDRESS 0x40 - - -/** - * @brief structure to hold a raw data from - * the pressure sensor - */ - -typedef struct _PRESSURE{ - uint8_t rBuffer[2]; - uint8_t crc; -}PRESSURE_DATA; - -class PressureWrapper -{ -public: - PressureWrapper (); - /* - * @return pressure in Pascal - */ - int getPressure (); - - virtual ~PressureWrapper (); - -private: - I2C *i2c; - PRESSURE_DATA data = {{0, 0}, 0}; - /* - * @return struct with pressure data in - * rBuffer and CRC check in crc - */ - bool getRawPressure (); -}; - -#endif /* PRESSUREWRAPPER_H_ */ From 45ae1d0e3ea888473968d084f8ef32b3dd7c6781 Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Thu, 20 Oct 2022 17:25:54 +0300 Subject: [PATCH 07/10] StateHandler: new state - stateSensors which pools data from sensors and displays it --- SDP600Sensor/inc/PressureWrapper.h | 48 +++++++++++++++++++++++++++++ StateHandler/inc/StateHandler.h | 24 +++++++++++++-- StateHandler/src/StateHandler.cpp | 35 ++++++++++++++++++++- esp-vent-main/src/esp-vent-main.cpp | 5 +-- 4 files changed, 106 insertions(+), 6 deletions(-) diff --git a/SDP600Sensor/inc/PressureWrapper.h b/SDP600Sensor/inc/PressureWrapper.h index e69de29..6ee2936 100644 --- a/SDP600Sensor/inc/PressureWrapper.h +++ b/SDP600Sensor/inc/PressureWrapper.h @@ -0,0 +1,48 @@ +/* + * PressureWrapper.h + * + * Created on: 5 Oct 2022 + * Author: evgenymeshcheryakov + */ + +#ifndef PRESSUREWRAPPER_H_ +#define PRESSUREWRAPPER_H_ + +#include "I2C.h" +#include + +#define ADDRESS 0x40 + + +/** + * @brief structure to hold a raw data from + * the pressure sensor + */ + +typedef struct _PRESSURE{ + uint8_t rBuffer[2]; + uint8_t crc; +}PRESSURE_DATA; + +class PressureWrapper +{ +public: + PressureWrapper (); + /* + * @return pressure in Pascal + */ + int getPressure (); + + virtual ~PressureWrapper (); + +private: + I2C *i2c; + PRESSURE_DATA data = {{0, 0}, 0}; + /* + * @return struct with pressure data in + * rBuffer and CRC check in crc + */ + bool getRawPressure (); +}; + +#endif /* PRESSUREWRAPPER_H_ */ diff --git a/StateHandler/inc/StateHandler.h b/StateHandler/inc/StateHandler.h index 7d014f9..70a244c 100644 --- a/StateHandler/inc/StateHandler.h +++ b/StateHandler/inc/StateHandler.h @@ -23,6 +23,7 @@ #include "ModbusRegister.h" #include "GMP252.h" #include "HMP60.h" +#include "PressureWrapper.h" /** Buttons enumeration * @@ -61,13 +62,21 @@ enum _mode AUTO }; +enum _sensors +{ + PRESSUREDAT, + TEMPERATURE, + HUMIDITY, + CO2 +}; + class StateHandler; typedef void (StateHandler::*state_pointer) (const Event &); class StateHandler { public: - StateHandler (LiquidCrystal *lcd, ModbusRegister *A01); + StateHandler (LiquidCrystal *lcd, ModbusRegister *A01, PressureWrapper *pressure); virtual ~StateHandler (); /** Get currently set pressure @@ -117,9 +126,10 @@ private: int integral = 0; int saved_set_value[2] = { 0, 0 }; int saved_curr_value[2] = { 0, 0 }; + int sensors_data[4] = {0}; LiquidCrystal *_lcd; ModbusRegister *A01; - + PressureWrapper * pressure; /* CO2 sensor object */ GMP252 co2; @@ -151,7 +161,15 @@ private: */ void stateAuto (const Event &event); - /** Hnadle button presses + /** Sensors state + * + * - print current sensrs readings + * + * @param event + */ + void stateSensors (const Event &event); + + /** Handle button presses * * @param button current button */ diff --git a/StateHandler/src/StateHandler.cpp b/StateHandler/src/StateHandler.cpp index 884d2bd..0a90e24 100644 --- a/StateHandler/src/StateHandler.cpp +++ b/StateHandler/src/StateHandler.cpp @@ -7,10 +7,11 @@ #include -StateHandler::StateHandler (LiquidCrystal *lcd, ModbusRegister *A01) +StateHandler::StateHandler (LiquidCrystal *lcd, ModbusRegister *A01, PressureWrapper *pressure) { this->_lcd = lcd; this->A01 = A01; + this->pressure = pressure; current = &StateHandler::stateInit; (this->*current) (Event (Event::eEnter)); current_mode = MANUAL; @@ -77,6 +78,7 @@ StateHandler::stateInit (const Event &event) switch (event.type) { case Event::eEnter: + SetState(&StateHandler::stateSensors); break; case Event::eExit: _lcd->clear (); @@ -152,6 +154,37 @@ StateHandler::stateAuto (const Event &event) } } +void +StateHandler::stateSensors (const Event &event) +{ + switch (event.type) + { + case Event::eEnter: + break; + case Event::eExit: + break; + case Event::eKey: + break; + case Event::eTick: + sensors_data[PRESSUREDAT] = pressure->getPressure(); + sensors_data[TEMPERATURE] = humidity.readT(); + sensors_data[HUMIDITY] = humidity.readRH(); + sensors_data[CO2] = co2.read(); + char line_up[16] = { 0 }; + char line_down[16] = { 0 }; + + snprintf (line_up, 16, "PRE:%02dPa TEM:%02dC", sensors_data[PRESSUREDAT],sensors_data[TEMPERATURE]); + snprintf (line_down, 16, "HUM:%02d CO2:%02d", sensors_data[HUMIDITY], sensors_data[CO2]); + + _lcd->clear (); + _lcd->setCursor (0, 0); + _lcd->print (line_up); + _lcd->setCursor (0, 1); + _lcd->print (line_down); + SetState (&StateHandler::stateManual); + break; + } +} void StateHandler::handleControlButtons (uint8_t button) diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp index be77b29..9bb2230 100644 --- a/esp-vent-main/src/esp-vent-main.cpp +++ b/esp-vent-main/src/esp-vent-main.cpp @@ -65,8 +65,10 @@ main (void) ModbusRegister A01(&fan, 0); // ModbusRegister DI1(&fan, 4, false); + PressureWrapper sens; - StateHandler ventMachine (&lcd, &A01); + + StateHandler ventMachine (&lcd, &A01, &sens); /** Common pins */ DigitalIoPin b_up (0, 7, true, true, true); // A5 SwitchController sw_up (&b_up, &glob_time, &ventMachine, BUTTON_CONTROL_UP); @@ -79,7 +81,6 @@ main (void) SwitchController sw_toggle (&b_toggle, &glob_time, &ventMachine, BUTTON_CONTROL_TOG_MODE); - PressureWrapper sens; int pressure = 0, pressure_time = 0; while (1) From 611ae1336260739928940bf3de13011f19acd1b6 Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Fri, 21 Oct 2022 10:37:11 +0300 Subject: [PATCH 08/10] StateHandler: save() now takes only two params. is able to check if pressure provided from main or inside of state machine. --- StateHandler/inc/StateHandler.h | 2 +- StateHandler/src/StateHandler.cpp | 29 ++++++++++++++++++----------- esp-vent-main/src/esp-vent-main.cpp | 6 ++++-- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/StateHandler/inc/StateHandler.h b/StateHandler/inc/StateHandler.h index 70a244c..20ca0e6 100644 --- a/StateHandler/inc/StateHandler.h +++ b/StateHandler/inc/StateHandler.h @@ -181,7 +181,7 @@ private: * @param counterValue value of the inner Counter * @param mode current mode */ - void save (int eventValue, int counterValue, size_t mode); + void save (int eventValue, size_t mode); /** Calculates pid for fan control value * diff --git a/StateHandler/src/StateHandler.cpp b/StateHandler/src/StateHandler.cpp index 0a90e24..bbbcdb8 100644 --- a/StateHandler/src/StateHandler.cpp +++ b/StateHandler/src/StateHandler.cpp @@ -6,6 +6,7 @@ */ #include +#define PID 0 StateHandler::StateHandler (LiquidCrystal *lcd, ModbusRegister *A01, PressureWrapper *pressure) { @@ -115,7 +116,7 @@ StateHandler::stateManual (const Event &event) handleControlButtons (event.value); break; case Event::eTick: - save (event.value, value[MANUAL].getCurrent (), MANUAL); + save (event.value, MANUAL); break; } } @@ -135,19 +136,16 @@ StateHandler::stateAuto (const Event &event) handleControlButtons (event.value); break; case Event::eTick: - save (event.value, value[AUTO].getCurrent (), AUTO); - int i = 0; -// pid(); -// this->A01->write(fan_speed.getCurrent()); + save (event.value, AUTO); +#if PID + pid(); + this->A01->write(fan_speed.getCurrent()); +#endif if(saved_curr_value[AUTO] < saved_set_value[AUTO]) { fan_speed.inc(); - while(i<720) i++; - i = 0; this->A01->write(fan_speed.getCurrent()); } else if(saved_curr_value[AUTO] > saved_set_value[AUTO]){ fan_speed.dec(); - while(i<720) i++; - i = 0; this->A01->write(fan_speed.getCurrent()); } break; @@ -181,7 +179,7 @@ StateHandler::stateSensors (const Event &event) _lcd->print (line_up); _lcd->setCursor (0, 1); _lcd->print (line_down); - SetState (&StateHandler::stateManual); + SetState (current_mode? &StateHandler::stateAuto : &StateHandler::stateManual); break; } } @@ -211,8 +209,17 @@ StateHandler::handleControlButtons (uint8_t button) } void -StateHandler::save (int eventValue, int counterValue, size_t mode) +StateHandler::save (int eventValue, size_t mode) { + /* if pressure is not provided from main it checks it in following if{}*/ + if(!eventValue) { + /* Small delay for modbus communications with pressure sensor */ + int i = 0; + while(i<7200) i++; + i = 0; + eventValue = pressure->getPressure(); + } + int counterValue = value[mode].getCurrent (); if (saved_curr_value[mode] != eventValue || saved_set_value[mode] != counterValue) { diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp index 9bb2230..c8fda08 100644 --- a/esp-vent-main/src/esp-vent-main.cpp +++ b/esp-vent-main/src/esp-vent-main.cpp @@ -81,8 +81,8 @@ main (void) SwitchController sw_toggle (&b_toggle, &glob_time, &ventMachine, BUTTON_CONTROL_TOG_MODE); - int pressure = 0, pressure_time = 0; + int pressure = 0, pressure_time = 0; while (1) { @@ -93,13 +93,15 @@ main (void) * TODO: * - Update current pressure to eTick */ +#if 0 if(pressure_time == 5) { pressure = sens.getPressure(); pressure_time = 0; } + ++pressure_time; +#endif ventMachine.HandleState (Event (Event::eTick, pressure)); glob_time.tickCounter (1); - ++pressure_time; } return 0; From 95ae3cd4a88e39db94652539e9c3e3d8933ed7ba Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Fri, 21 Oct 2022 11:51:30 +0300 Subject: [PATCH 09/10] StateHandler: minor improvments --- StateHandler/src/StateHandler.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/StateHandler/src/StateHandler.cpp b/StateHandler/src/StateHandler.cpp index bbbcdb8..b26ea5e 100644 --- a/StateHandler/src/StateHandler.cpp +++ b/StateHandler/src/StateHandler.cpp @@ -141,6 +141,7 @@ StateHandler::stateAuto (const Event &event) pid(); this->A01->write(fan_speed.getCurrent()); #endif +#if !PID if(saved_curr_value[AUTO] < saved_set_value[AUTO]) { fan_speed.inc(); this->A01->write(fan_speed.getCurrent()); @@ -148,6 +149,7 @@ StateHandler::stateAuto (const Event &event) fan_speed.dec(); this->A01->write(fan_speed.getCurrent()); } +#endif break; } } @@ -168,17 +170,18 @@ StateHandler::stateSensors (const Event &event) sensors_data[TEMPERATURE] = humidity.readT(); sensors_data[HUMIDITY] = humidity.readRH(); sensors_data[CO2] = co2.read(); +#if 1 char line_up[16] = { 0 }; char line_down[16] = { 0 }; - - snprintf (line_up, 16, "PRE:%02dPa TEM:%02dC", sensors_data[PRESSUREDAT],sensors_data[TEMPERATURE]); - snprintf (line_down, 16, "HUM:%02d CO2:%02d", sensors_data[HUMIDITY], sensors_data[CO2]); + snprintf (line_up, 16, "PRE:%02d TEM:%02d", sensors_data[PRESSUREDAT],sensors_data[TEMPERATURE]); + snprintf (line_down, 16, "HUM:%02d CO2:%02d", sensors_data[HUMIDITY], sensors_data[CO2]); _lcd->clear (); _lcd->setCursor (0, 0); _lcd->print (line_up); _lcd->setCursor (0, 1); _lcd->print (line_down); +#endif SetState (current_mode? &StateHandler::stateAuto : &StateHandler::stateManual); break; } @@ -215,7 +218,7 @@ StateHandler::save (int eventValue, size_t mode) if(!eventValue) { /* Small delay for modbus communications with pressure sensor */ int i = 0; - while(i<7200) i++; + while(i<720) i++; i = 0; eventValue = pressure->getPressure(); } @@ -230,7 +233,7 @@ StateHandler::save (int eventValue, size_t mode) } void StateHandler::pid () { - float kP = 0.001, kI = 0.001, kD = 0.001; + float kP = 0.1, kI = 0.01, kD = 0.01; int error = 0, last_error = 0, derivative = 0; error = saved_set_value[AUTO] - saved_curr_value[AUTO]; last_error = error; From 0be8c9a3f0cb34263156a3844009536856381e2b Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Fri, 21 Oct 2022 20:16:53 +0300 Subject: [PATCH 10/10] StateHandler: redundant code removal --- StateHandler/src/StateHandler.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/StateHandler/src/StateHandler.cpp b/StateHandler/src/StateHandler.cpp index b26ea5e..40cd323 100644 --- a/StateHandler/src/StateHandler.cpp +++ b/StateHandler/src/StateHandler.cpp @@ -114,6 +114,7 @@ StateHandler::stateManual (const Event &event) break; case Event::eKey: handleControlButtons (event.value); + this->A01->write(value[MANUAL].getCurrent() * 10); break; case Event::eTick: save (event.value, MANUAL); @@ -194,13 +195,9 @@ StateHandler::handleControlButtons (uint8_t button) { case BUTTON_CONTROL_DOWN: this->value[(current_mode) ? AUTO : MANUAL].dec (); - if(current_mode == MANUAL) - this->A01->write(value[(current_mode) ? AUTO : MANUAL].getCurrent() * 10); break; case BUTTON_CONTROL_UP: this->value[(current_mode) ? AUTO : MANUAL].inc (); - if(current_mode == MANUAL) - this->A01->write(value[(current_mode) ? AUTO : MANUAL].getCurrent() * 10); break; case BUTTON_CONTROL_TOG_MODE: current_mode = !current_mode;