From 4b1ef018ddab7f6e3be48b7430fc90bb3e8a8a6c Mon Sep 17 00:00:00 2001 From: Evgenii Meshcheriakov Date: Wed, 19 Oct 2022 09:46:55 +0300 Subject: [PATCH] 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; }