commit
995773577e
@ -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;
|
||||
}
|
||||
@ -19,6 +19,11 @@
|
||||
#include "DigitalIoPin.h"
|
||||
#include "Event.h"
|
||||
#include "LiquidCrystal.h"
|
||||
#include "ModbusMaster.h"
|
||||
#include "ModbusRegister.h"
|
||||
#include "GMP252.h"
|
||||
#include "HMP60.h"
|
||||
#include "PressureWrapper.h"
|
||||
|
||||
/** Buttons enumeration
|
||||
*
|
||||
@ -57,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);
|
||||
StateHandler (LiquidCrystal *lcd, ModbusRegister *A01, PressureWrapper *pressure);
|
||||
virtual ~StateHandler ();
|
||||
|
||||
/** Get currently set pressure
|
||||
@ -103,9 +116,25 @@ private:
|
||||
void SetState (state_pointer newstate);
|
||||
bool current_mode;
|
||||
Counter value[2] = { { 0, 100 }, { 0, 120 } };
|
||||
/* 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 = {20, 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 };
|
||||
int sensors_data[4] = {0};
|
||||
LiquidCrystal *_lcd;
|
||||
ModbusRegister *A01;
|
||||
PressureWrapper * pressure;
|
||||
/* CO2 sensor object */
|
||||
GMP252 co2;
|
||||
|
||||
/* Humidity and temperature sensor object */
|
||||
HMP60 humidity;
|
||||
|
||||
/** Initialization state
|
||||
*
|
||||
@ -132,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
|
||||
*/
|
||||
@ -144,7 +181,12 @@ 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
|
||||
*
|
||||
*/
|
||||
void pid ();
|
||||
};
|
||||
|
||||
#endif /* STATE_HANDLER_H_ */
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,10 +6,13 @@
|
||||
*/
|
||||
|
||||
#include <StateHandler.h>
|
||||
#define PID 0
|
||||
|
||||
StateHandler::StateHandler (LiquidCrystal *lcd)
|
||||
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;
|
||||
@ -76,6 +79,7 @@ StateHandler::stateInit (const Event &event)
|
||||
switch (event.type)
|
||||
{
|
||||
case Event::eEnter:
|
||||
SetState(&StateHandler::stateSensors);
|
||||
break;
|
||||
case Event::eExit:
|
||||
_lcd->clear ();
|
||||
@ -103,15 +107,17 @@ 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 ();
|
||||
break;
|
||||
case Event::eKey:
|
||||
handleControlButtons (event.value);
|
||||
this->A01->write(value[MANUAL].getCurrent() * 10);
|
||||
break;
|
||||
case Event::eTick:
|
||||
save (event.value, value[MANUAL].getCurrent (), MANUAL);
|
||||
save (event.value, MANUAL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -131,7 +137,53 @@ StateHandler::stateAuto (const Event &event)
|
||||
handleControlButtons (event.value);
|
||||
break;
|
||||
case Event::eTick:
|
||||
save (event.value, value[AUTO].getCurrent (), AUTO);
|
||||
save (event.value, AUTO);
|
||||
#if PID
|
||||
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());
|
||||
} else if(saved_curr_value[AUTO] > saved_set_value[AUTO]){
|
||||
fan_speed.dec();
|
||||
this->A01->write(fan_speed.getCurrent());
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
#if 1
|
||||
char line_up[16] = { 0 };
|
||||
char line_down[16] = { 0 };
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -157,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<720) i++;
|
||||
i = 0;
|
||||
eventValue = pressure->getPressure();
|
||||
}
|
||||
int counterValue = value[mode].getCurrent ();
|
||||
if (saved_curr_value[mode] != eventValue
|
||||
|| saved_set_value[mode] != counterValue)
|
||||
{
|
||||
@ -167,3 +228,14 @@ StateHandler::save (int eventValue, int counterValue, size_t mode)
|
||||
displaySet (saved_set_value[mode], saved_curr_value[mode]);
|
||||
}
|
||||
}
|
||||
|
||||
void StateHandler::pid () {
|
||||
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;
|
||||
integral += error;
|
||||
derivative = error - last_error;
|
||||
fan_speed.setInit((kP*error) + (kI*integral) + (kD * derivative));
|
||||
}
|
||||
|
||||
|
||||
@ -55,11 +55,20 @@ 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);
|
||||
|
||||
PressureWrapper sens;
|
||||
|
||||
|
||||
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);
|
||||
@ -73,10 +82,7 @@ main (void)
|
||||
BUTTON_CONTROL_TOG_MODE);
|
||||
|
||||
|
||||
PressureWrapper sens();
|
||||
|
||||
|
||||
|
||||
int pressure = 0, pressure_time = 0;
|
||||
while (1)
|
||||
{
|
||||
|
||||
@ -87,7 +93,14 @@ main (void)
|
||||
* TODO:
|
||||
* - Update current pressure to eTick
|
||||
*/
|
||||
ventMachine.HandleState (Event (Event::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);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user