esp-vent-main: main cleanup and doc

- Delete unnused stuff in main
- Create common values header
- Minor fix
This commit is contained in:
Vasily Davydov 2022-10-26 23:10:20 +03:00
parent fe75d4e992
commit 4c3cd99e38
5 changed files with 95 additions and 89 deletions

View File

@ -5,12 +5,12 @@
* Author: tylen * Author: tylen
*/ */
#include "Modbus/ModbusRegister.h"
#include <assert.h>
#ifndef FAN_H_ #ifndef FAN_H_
#define FAN_H_ #define FAN_H_
#include "Modbus/ModbusRegister.h"
#include <assert.h>
class Fan class Fan
{ {
public: public:

View File

@ -22,6 +22,7 @@
#include "LiquidCrystal.h" #include "LiquidCrystal.h"
#include "PressureWrapper.h" #include "PressureWrapper.h"
#include "Timer.h" #include "Timer.h"
#include "common_control_values.h"
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"
/** Buttons enumeration /** Buttons enumeration
@ -30,8 +31,7 @@
* from main to StateHandler through * from main to StateHandler through
* a keyEvent. Enumeration determines the state * a keyEvent. Enumeration determines the state
* of the particular button. * of the particular button.
* */ */
enum _buttons enum _buttons
{ {
/** Raises the bar up */ /** Raises the bar up */
@ -93,6 +93,39 @@ public:
*/ */
unsigned int getSetSpeed (); unsigned int getSetSpeed ();
/** Handle the given event of the current state
*
* @param event event to be handled in the current state
*/
void HandleState (const Event &event);
private:
state_pointer current;
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 = { 0, 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;
Fan *_propeller;
PressureWrapper *_pressure;
bool pressure_status;
Timer *state_timer;
/* CO2 sensor object */
GMP252 co2;
/* Humidity and temperature sensor object */
HMP60 humidity;
/** Display values on LCD depending on current mode /** Display values on LCD depending on current mode
* *
* MANUAL MODE: SPEED: XX% PRESSURE: XXPa * MANUAL MODE: SPEED: XX% PRESSURE: XXPa
@ -103,47 +136,10 @@ public:
*/ */
void displaySet (size_t mode); void displaySet (size_t mode);
/** Handle the given event of the current state
*
* @param event event to be handled in the current state
*/
void HandleState (const Event &event);
private:
state_pointer current;
/** Set a new curremt state /** Set a new curremt state
* @param newstate new state to be set to current * @param newstate new state to be set to current
*/ */
void SetState (state_pointer newstate); 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 = { 0, 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;
Fan *_propeller;
PressureWrapper *_pressure;
bool pressure_status;
Timer *state_timer;
/* CO2 sensor object */
GMP252 co2;
/* Humidity and temperature sensor object */
HMP60 humidity;
/** Initialization state /** Initialization state
* *

View File

@ -0,0 +1,20 @@
/*
* common_control_values.h
*
* Created on: Oct 26, 2022
* Author: tylen
*/
#ifndef COMMON_CONTROL_VALUES_H_
#define COMMON_CONTROL_VALUES_H_
enum _global_values
{
LCD_SIZE = 16,
TIMER_GLOBAL_TIMEOUT = 15000,
TIMER_SENSORS_TIMEOUT = 5000,
TIMER_PRESSURE_TIMEOUT = 500,
TIMER_ERROR_VALUE = -255,
};
#endif /* COMMON_CONTROL_VALUES_H_ */

View File

@ -27,30 +27,32 @@ StateHandler::~StateHandler ()
void void
StateHandler::displaySet (size_t mode) StateHandler::displaySet (size_t mode)
{ {
char line_up[16] = { 0 }; char line_up[LCD_SIZE] = { 0 };
char line_down[16] = { 0 }; char line_down[LCD_SIZE] = { 0 };
switch (mode) switch (mode)
{ {
case MANUAL: case MANUAL:
snprintf (line_up, 16, "SPEED: %02d%", saved_set_value[current_mode]); snprintf (line_up, LCD_SIZE, "SPEED: %02d%",
snprintf (line_down, 16, "PRESSURE: %02dPa", saved_set_value[current_mode]);
snprintf (line_down, LCD_SIZE, "PRESSURE: %02dPa",
saved_curr_value[current_mode]); saved_curr_value[current_mode]);
break; break;
case AUTO: case AUTO:
snprintf (line_up, 16, "P. SET: %02dPa", saved_set_value[current_mode]); snprintf (line_up, LCD_SIZE, "P. SET: %02dPa",
snprintf (line_down, 16, "P. CURR: %02dPa", saved_set_value[current_mode]);
snprintf (line_down, LCD_SIZE, "P. CURR: %02dPa",
saved_curr_value[current_mode]); saved_curr_value[current_mode]);
break; break;
case SENSORS: case SENSORS:
snprintf (line_up, 16, "PRE:%02d TEM:%02d", sensors_data[PRESSUREDAT], snprintf (line_up, LCD_SIZE, "PRE:%02d TEM:%02d",
sensors_data[TEMPERATURE]); sensors_data[PRESSUREDAT], sensors_data[TEMPERATURE]);
snprintf (line_down, 16, "HUM:%02d CO2:%02d", sensors_data[HUMIDITY], snprintf (line_down, LCD_SIZE, "HUM:%02d CO2:%02d",
sensors_data[CO2]); sensors_data[HUMIDITY], sensors_data[CO2]);
break; break;
case ERROR_TIMEOUT: case ERROR_TIMEOUT:
snprintf (line_up, 16, " FORCE STOP "); snprintf (line_up, LCD_SIZE, " FORCE STOP ");
snprintf (line_down, 16, "REASON: TIMEOUT"); snprintf (line_down, LCD_SIZE, "REASON: TIMEOUT");
break; break;
default: default:
break; break;
@ -203,16 +205,16 @@ StateHandler::handleControlButtons (uint8_t button)
void void
StateHandler::handleTickValue (int value) StateHandler::handleTickValue (int value)
{ {
if (value % 5000 == 0) if (value % TIMER_SENSORS_TIMEOUT == 0)
{ {
updateSensorValues (); updateSensorValues ();
displaySet (SENSORS); displaySet (SENSORS);
} }
if (value % 500 == 0) if (value % TIMER_PRESSURE_TIMEOUT == 0)
{ {
SetState (&StateHandler::stateGetPressure); SetState (&StateHandler::stateGetPressure);
} }
if (value < 0) if (value == TIMER_ERROR_VALUE)
{ {
displaySet (ERROR_TIMEOUT); displaySet (ERROR_TIMEOUT);
this->fan_speed.setInit (0); this->fan_speed.setInit (0);

View File

@ -8,14 +8,6 @@
=============================================================================== ===============================================================================
*/ */
#if defined(__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#endif
#endif
#include "DigitalIoPin.h" #include "DigitalIoPin.h"
#include "Fan.h" #include "Fan.h"
#include "LiquidCrystal.h" #include "LiquidCrystal.h"
@ -23,32 +15,20 @@
#include "StateHandler/StateHandler.h" #include "StateHandler/StateHandler.h"
#include "SwitchController.h" #include "SwitchController.h"
#include "Timer.h" #include "Timer.h"
#include "board.h"
#include "chip.h"
#include "common_control_values.h"
#include <cr_section_macros.h> #include <cr_section_macros.h>
// TODO: insert other include files here
// TODO: insert other definitions and declarations here
int int
main (void) main (void)
{ {
/* Board init */
#if defined(__USE_LPCOPEN)
// Read clock settings and update SystemCoreClock variable
SystemCoreClockUpdate (); SystemCoreClockUpdate ();
#if !defined(NO_BOARD_LIB)
// Set b_up_state and initialize all required blocks and
// functions related to the board hardware
Board_Init (); Board_Init ();
// Set the LED to the state of "On"
Board_LED_Set (0, true); /** LCD setup */
#endif
#endif
/** Lcd & stateHandler */
Chip_RIT_Init (LPC_RITIMER); Chip_RIT_Init (LPC_RITIMER);
Timer glob_time;
Timer switch_time (false);
DigitalIoPin rs (0, 29, false, true, false); DigitalIoPin rs (0, 29, false, true, false);
DigitalIoPin en (0, 9, false, true, false); DigitalIoPin en (0, 9, false, true, false);
DigitalIoPin d4 (0, 10, false, true, false); DigitalIoPin d4 (0, 10, false, true, false);
@ -56,20 +36,26 @@ main (void)
DigitalIoPin d6 (1, 3, false, true, false); DigitalIoPin d6 (1, 3, false, true, false);
DigitalIoPin d7 (0, 0, false, true, false); DigitalIoPin d7 (0, 0, false, true, false);
LiquidCrystal lcd (&rs, &en, &d4, &d5, &d6, &d7); LiquidCrystal lcd (&rs, &en, &d4, &d5, &d6, &d7);
//
lcd.setCursor (0, 0); lcd.setCursor (0, 0);
lcd.print ("Vent-Machine"); lcd.print ("Vent-Machine");
/* FAN object */ /* Timers */
Timer glob_time;
Timer switch_time (false);
/* Modbus Fan setup */
ModbusMaster fan (1); ModbusMaster fan (1);
fan.begin (9600); fan.begin (9600);
Fan propeller (new ModbusRegister (&fan, 0)); Fan propeller (new ModbusRegister (&fan, 0));
/* Pressure sensor setup */
PressureWrapper sens; PressureWrapper sens;
glob_time.Sleep (1000); glob_time.Sleep (1000);
/* State Machine */
StateHandler ventMachine (&lcd, &propeller, &sens, &glob_time); StateHandler ventMachine (&lcd, &propeller, &sens, &glob_time);
/** Common pins */
/** Control switches */
DigitalIoPin b_up (0, 7, true, true, true); // A5 DigitalIoPin b_up (0, 7, true, true, true); // A5
SwitchController sw_up (&b_up, &ventMachine, BUTTON_CONTROL_UP); SwitchController sw_up (&b_up, &ventMachine, BUTTON_CONTROL_UP);
@ -79,14 +65,16 @@ main (void)
DigitalIoPin b_toggle (0, 5, true, true, true); // A3 DigitalIoPin b_toggle (0, 5, true, true, true); // A3
SwitchController sw_toggle (&b_toggle, &ventMachine, SwitchController sw_toggle (&b_toggle, &ventMachine,
BUTTON_CONTROL_TOG_MODE); BUTTON_CONTROL_TOG_MODE);
int getcounterValue = 0; /* Other declarations */
int getcounterValue;
while (1) while (1)
{ {
getcounterValue = glob_time.getCounter (); getcounterValue = glob_time.getCounter ();
if (getcounterValue > 15000) if (getcounterValue > TIMER_GLOBAL_TIMEOUT)
{ {
glob_time.resetCounter (); glob_time.resetCounter ();
ventMachine.HandleState (Event (Event::eTick, -1)); ventMachine.HandleState (Event (Event::eTick, TIMER_ERROR_VALUE));
} }
sw_up.listen (); sw_up.listen ();
sw_down.listen (); sw_down.listen ();