123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808 |
- /*
- * coffee.cpp
- *
- * Created on: Sep 25, 2017
- * Author: sebastian
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <stdint.h>
- #include <wiringPi.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <iostream>
- #include <csignal>
- #include <time.h>
- #include <ctime>
- #include "coffee.h"
- #include "hal.h"
- #include "logger.h"
- #include "timer.h"
- #include "database.h"
- coffee_status_t state;
- coffee_menuPage_t page;
- coffee_mode_t mode;
- int sigValue;
- int brewTime; //Brew time in ms
- timer brewTimer(&brewTimeHandler);
- uint64_t totalHeatingTime; //local copies of the corresponding database entries
- uint16_t brewCounter;
- bool initalHeating;
- bool descaling; //flag to indicate descaling and cleaning
- uint16_t descBrewcount;
- time_t descRawTimestamp;
- const char* PageName[] = { "SoftOff", "Kill", "Stats", "Temp", "Clean", "Demo",
- "Exit" };
- const char* StateName[] = { "OFF", "HEATING", "INITHEAT", "IDLE", "BREW",
- "BREWMAN", "CLEAN", "ERROR", "WAITOFF" };
- /**
- * Thread for the finite state machine
- * It represents the current state of the machine and handles signals coming from
- * the pressure control, buttons, the brew switch and the proximity sensor
- * @param threadid the ID of the thread
- */
- void *coffeeThread(void *threadid) {
- logger(V_BASIC, "Initializing coffee thread...\n");
- //installing new Signal handler for coffeethread
- struct sigaction action;
- sigemptyset(&action.sa_mask);
- action.sa_flags = SA_SIGINFO;
- action.sa_sigaction = coffeeHandler;
- sigaction(SIGUSR2, &action, NULL);
- brewTimer.setDivider(4);
- brewTimer.stop();
- brewTime = 0;
- initalHeating = true;
- mode = MODE_STATE; //Unless we enter the menu we start in state mode
- page = PAGE_SOFTOFF;
- descaling = false;
- //read the database values
- if (!(totalHeatingTime = sqlGetConf(CFGHeatingTime))) {
- logger_error("coffee.cpp: Couldn't read the heating time from the database");
- //pthread_exit(EXIT_SUCCESS);
- exit(EXIT_FAILURE);
- }
- if (!(brewCounter = sqlGetConf(CFGbrewcounter))) {
- logger_error("coffee.cpp: Couldn't read the brew counter from the database");
- //pthread_exit(EXIT_SUCCESS);
- exit(EXIT_FAILURE);
- }
- if (!(descRawTimestamp = (time_t) sqlGetConf(CFGDescTimestamp))) {
- logger_error("coffee.cpp: Couldn't read the descaling time from the database");
- //pthread_exit(EXIT_SUCCESS);
- exit(EXIT_FAILURE);
- }
- if (!(descBrewcount = sqlGetConf(CFGDescBrewCount))) {
- logger_error("coffee.cpp: Couldn't read the descaling brewcount time from the database");
- //pthread_exit(EXIT_SUCCESS);
- exit(EXIT_FAILURE);
- }
- checkDescaling();
- event_subscribe("terminate", &coffeeTerminate);
- logger(V_BREW, "Determining inital state\n");
- //determine inital state
- if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
- && !halGetRelaisState(RELAIS_PUMP)) {
- //wait for heat relais to switch
- sleep(1);
- if (halIsHeating()) { //Heating is on
- changeState(STATE_INITALHEATING);
- } else {
- initalHeating = false;
- changeState(STATE_IDLE);
- }
- } else if (halGetRelaisState(RELAIS_PUMP)) {
- logger_error("Whoops, why is the pump running...\n");
- changeState(STATE_ERROR);
- } else {
- changeState(STATE_OFF);
- }
- logger(V_BREW, "Starting Coffee FSM\n");
- //begin FSM
- while (1) {
- /*
- * Menue FSM
- */
- switch (page) {
- case PAGE_SOFTOFF: //this page is only available when the machine is on
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt0Rls:
- changeState(STATE_WAIT_OFF);
- leaveMenu();
- break;
- case SigInt1Psh:
- changePage(PAGE_KILL);
- break;
- }
- break;
- case PAGE_KILL: //this page is only available when the machine is on
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt0Rls:
- if (halIsHeating()) {
- coffeeIncreaseHeatingTime(halgetHeatingTime());
- }
- changeState(STATE_OFF);
- leaveMenu();
- break;
- case SigInt1Psh:
- if (state == STATE_IDLE || state == STATE_HEATING) {
- changePage(PAGE_CLEAN);
- } else {
- changePage(PAGE_DEMO);
- }
- break;
- }
- break;
- case PAGE_CLEAN: //this page is only be available when the machine is hot
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt0Rls:
- changeMode(MODE_STATE);
- if (!halProxSensorCovered()) {
- changeState(STATE_CLEANING);
- leaveMenu();
- } else {
- changeState(STATE_FULLTANK);
- leaveMenu();
- }
- break;
- case SigInt1Psh:
- changePage(PAGE_DEMO);
- break;
- }
- break;
- case PAGE_DEMO:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt1Psh:
- changePage(PAGE_TEMP);
- break;
- }
- break;
- case PAGE_TEMP:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt1Psh:
- changePage(PAGE_STATS);
- break;
- }
- break;
- case PAGE_STATS:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt1Psh:
- changePage(PAGE_EXIT);
- break;
- }
- break;
- case PAGE_EXIT:
- if (SigValueEmpty() && mode == MODE_MENU)
- pause();
- switch (getSigValue(MODE_MENU)) {
- case SigInt0Rls:
- leaveMenu();
- break;
- case SigInt1Psh:
- if (state == STATE_HEATING || state == STATE_ERROR
- || state == STATE_IDLE
- || state == STATE_INITALHEATING) {
- changePage(PAGE_SOFTOFF);
- } else {
- changePage(PAGE_DEMO);
- }
- break;
- }
- break;
- } //end switch (page)
- /*
- * Hardware FSM
- */
- switch (state) {
- /*
- *
- */
- case STATE_OFF:
- if (mode == MODE_STATE) {
- halMachineOff();
- writeBackCache();
- changePage(PAGE_DEMO);
- if (SigValueEmpty())
- pause();
- }
- switch (getSigValue(MODE_STATE)) {
- case SigInt0Rls:
- //Check waterlevel in gray water tank
- //turn machine on
- halMachineOn();
- sleep(1);
- if (halIsHeating() && !halProxSensorCovered()) { //check if System starts to heat when turned on
- changeState(STATE_INITALHEATING);
- } else if (!halIsHeating() && !halProxSensorCovered()) {
- changeState(STATE_IDLE);
- } else if (halProxSensorCovered()) {
- logger_error("Empty Tank please!\n");
- changeState(STATE_FULLTANK);
- }
- if (page != PAGE_SOFTOFF)
- changePage(PAGE_SOFTOFF); //the machine is on, the menu starts with the turning off page
- break;
- case SigInt1Psh:
- //Enter the menu
- if (page != PAGE_DEMO)
- changePage(PAGE_DEMO); //machine is off, the menu starts with the demo page
- changeMode(MODE_MENU);
- break;
- }
- break;
- /*
- *
- */
- case STATE_WAIT_OFF:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- case SigPressOpn:
- usleep(100000); //wait so no load will be switched
- coffeeIncreaseHeatingTime(halgetHeatingTime());
- changeState(STATE_OFF);
- break;
- case SigInt0Psh:
- case SigInt1Psh:
- if (halProxSensorCovered()) {
- changeState(STATE_FULLTANK);
- } else if (initalHeating) {
- changeState(STATE_INITALHEATING);
- } else {
- changeState(STATE_HEATING);
- }
- break;
- }
- break;
- /*
- *
- */
- case STATE_INITALHEATING:
- initalHeating = true;
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- // case SigInt0RlsLong:
- // //Turn machine off again
- // coffeeIncreaseHeatingTime(halgetHeatingTime());
- // changeState(STATE_OFF);
- // break;
- //
- // case SigInt0Rls:
- // changeState(STATE_WAIT_OFF);
- // break;
- case SigProxCvrd:
- changeState(STATE_FULLTANK);
- break;
- case SigPressOpn:
- //Inital heating finished
- initalHeating = false;
- coffeeIncreaseHeatingTime(halgetHeatingTime());
- changeState(STATE_IDLE);
- break;
- case SigInt1Psh:
- changeMode(MODE_MENU);
- break;
- }
- break;
- /*
- *
- */
- case STATE_HEATING:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- // case SigInt1RlsLong:
- // //Turn machine _immediately_ off again
- // coffeeIncreaseHeatingTime(halgetHeatingTime());
- // changeState(STATE_OFF);
- // break;
- //
- // case SigInt1Rls:
- // //turn machine off when heating is finished
- // changeState(STATE_WAIT_OFF);
- // break;
- case SigPressOpn:
- coffeeIncreaseHeatingTime(halgetHeatingTime());
- changeState(STATE_IDLE);
- break;
- case SigInt0Psh:
- //start to brew a delicious coffee
- changeState(STATE_BREW);
- break;
- case SigProxCvrd:
- changeState(STATE_FULLTANK);
- break;
- case SigBrewOn:
- //someone brews manually
- changeState(STATE_BREWMANUAL);
- break;
- case SigInt1Psh:
- //Enter the menu
- changeMode(MODE_MENU);
- break;
- }
- break;
- /*
- *
- */
- case STATE_IDLE:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- // case SigInt1RlsLong:
- // //turn machine _immediately_ off
- // changeState(STATE_OFF);
- // break;
- //
- // case SigInt1Rls:
- // changeState(STATE_OFF);
- // break;
- case SigPressCls:
- changeState(STATE_HEATING);
- break;
- case SigInt0Psh:
- changeState(STATE_BREW);
- break;
- case SigProxCvrd:
- changeState(STATE_FULLTANK);
- break;
- case SigBrewOn:
- //someone brews manually
- changeState(STATE_BREWMANUAL);
- break;
- case SigInt1Psh:
- //Enter the menu
- changeMode(MODE_MENU);
- break;
- }
- break;
- /*
- *
- */
- case STATE_BREW:
- //make sure the tank is not full
- if (halProxSensorCovered()) {
- changeState(STATE_FULLTANK);
- logger_error("coffee.cpp: Full tank detection failed..\n");
- } else {
- coffeeBrew();
- checkDescaling();
- logger(V_BREW, "Finishing brewing\n");
- if (!halProxSensorCovered()) {
- if (halIsHeating()) {
- changeState(STATE_HEATING);
- } else {
- changeState(STATE_IDLE);
- }
- } else {
- changeState(STATE_FULLTANK);
- }
- }
- break;
- /*
- *
- */
- case STATE_BREWMANUAL:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- break;
- /*
- *
- */
- case STATE_CLEANING: //this can only be executed once the machine is hot!
- if (!halProxSensorCovered()) {
- //execute the cleaning procedure
- coffeeClean();
- if (halIsHeating()) {
- changeState(STATE_HEATING);
- } else {
- changeState(STATE_IDLE);
- }
- } else {
- changeState(STATE_FULLTANK);
- }
- break;
- /*
- * Full tank is detected at the beginning and the end of a brewing process, during
- * idle times, initial heating and heating
- */
- case STATE_FULLTANK:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- case SigInt1Psh:
- case SigInt0Psh:
- if (halIsHeating() && initalHeating) {
- changeState(STATE_INITALHEATING);
- } else if (halIsHeating() && !initalHeating) {
- changeState(STATE_HEATING);
- } else {
- changeState(STATE_IDLE);
- }
- break;
- }
- break;
- /*
- *
- */
- case STATE_ERROR:
- if (SigValueEmpty() && mode == MODE_STATE)
- pause();
- switch (getSigValue(MODE_STATE)) {
- case SigInt1RlsLong:
- case SigInt0RlsLong:
- if (halIsHeating()) {
- coffeeIncreaseHeatingTime(halgetHeatingTime());
- }
- changeState(STATE_OFF);
- break;
- }
- }
- }
- pthread_exit(EXIT_SUCCESS);
- }
- /**
- * Handler for the Signal send to this thread
- * It saves the type of signal received and tracks the time between a push and release event of up to 4 signals
- * The time is stored in the HalEvent variable when a release event is received
- * @param signum
- * @param siginfo
- * @param context
- */
- void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
- sigval_t sigVal = (siginfo->si_value);
- sigValue = sigVal.sival_int;
- logger(V_BREW, "coffee.cpp: CoffeeHandler called with Signal %d\n",
- sigValue);
- }
- /**
- * returns the Signal value from the last received Signal for the given mode and clears the variable
- * @return value sent with the last signal
- */
- int getSigValue(coffee_mode_t mode) {
- int tmp = sigValue;
- if (mode == MODE_MENU) {
- switch (sigValue) {
- case SigInt0Psh:
- case SigInt0Rls:
- case SigInt0RlsLong:
- case SigInt1Psh:
- case SigInt1Rls:
- case SigInt1RlsLong:
- sigValue = 0;
- return tmp;
- break;
- default:
- break;
- }
- } else { //State Mode
- sigValue = 0;
- return tmp;
- }
- //int tmp = sigValue;
- //sigValue = 0;
- //return tmp;
- return 0;
- }
- bool SigValueEmpty(void) {
- if (sigValue == 0)
- return true;
- else
- return false;
- }
- /**
- * Changes the state of the machine to newState
- * prints the change to the logger
- * @param newState
- */
- void changeState(coffee_status_t newState) {
- logger(V_BREW, "Changing state to %s\n", StateName[newState]);
- state = newState;
- event_trigger("statechange", &state, sizeof(state));
- }
- /*
- * Change Page to new menu page
- */
- void changePage(coffee_menuPage_t newPage) {
- logger(V_BREW, "Change Page to %s\n", PageName[newPage]);
- event_trigger("pagechange", &newPage, sizeof(newPage));
- page = newPage;
- }
- /*
- * Changes the mode of the machine to the given mode
- */
- void changeMode(coffee_mode_t newMode) {
- if (newMode == MODE_MENU)
- logger(V_BREW, "Changing to menu mode\n");
- else
- logger(V_BREW, "Changing to state mode\n");
- event_trigger("modechange", &newMode, sizeof(newMode));
- mode = newMode;
- }
- /*
- * leaving the menu
- * sets the start page for the next menu call to softoff
- */
- void leaveMenu() {
- logger(V_BREW, "Leaving the menu again...\n");
- //leave the menu again
- changeMode(MODE_STATE);
- //change page to initial page
- changePage(PAGE_SOFTOFF);
- }
- /**
- * Returns the current state of the FSM
- */
- coffee_status_t getState(void) {
- return state;
- }
- /**
- * Returns the local up-to-date brewcounter
- */
- uint16_t getBrewCounter(void) {
- return brewCounter;
- }
- /**
- * Counter for the brew time
- * refresh every 200ms
- */
- void brewTimeHandler(void) {
- brewTime += 200;
- }
- /**
- * handles program termination
- */
- void coffeeTerminate(event_t *event) {
- logger(V_BREW, "Coffee.cpp: Terminating\n");
- //stop brewing
- halRelaisOff(RELAIS_PUMP);
- brewTimer.stop();
- writeBackCache();
- }
- /**
- * Function to write back the values of the local copies
- * brewCounter and totalHeatingTime
- *
- */
- void writeBackCache(void) {
- if (sqlSetConf(CFGbrewcounter, brewCounter)) {
- logger_error("coffee.cpp: Couldn't write brewcounter to database");
- return;
- }
- if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
- logger_error("coffee.cpp: Couldn't write heating time to database");
- return;
- }
- if (sqlSetConf(CFGDescBrewCount, descBrewcount)) {
- logger_error("coffee.cpp: Couldn't write descaling brewcount to database");
- return;
- }
- if (sqlSetConf(CFGDescTimestamp, (uint64_t)descRawTimestamp)) {
- logger_error("coffee.cpp: Couldn't write descaling timestamp to database");
- return;
- }
- }
- /*
- * Procedure for cleaning the machine
- */
- void coffeeClean(void) {
- logger(V_BREW, "Cleaning...\n");
- for (int i = 0; i < 20; i++) {
- halRelaisOn(RELAIS_PUMP);
- //TODO the sleep function returns when a signal is delivered
- //this causes the cleaning to not work properly when the pressure is triggered
- sleep(3);
- halRelaisOff(RELAIS_PUMP);
- sleep(15);
- }
- updateDescaling();
- descaling = false;
- event_trigger("descaling", &descaling, sizeof(bool));
- }
- /**
- * Brewing process
- */
- void coffeeBrew(void) {
- coffeeIncreaseBrewCounter();
- /*
- * Preinfusion
- */
- logger(V_BREW, "Starting preinfusion...\n");
- halResetFlow();
- halRelaisOn(RELAIS_PUMP);
- brewTime = 0;
- brewTimer.start();
- while (halGetFlow() < AMOUNT_PREINFUSION) {
- usleep(50000);
- if (getSigValue(MODE_STATE) == SigInt0Psh){
- stopBrewing();
- return;
- }
- }
- stopBrewing();
- /*
- * Wait for coffee to soak in infused water
- */
- brewTimer.start();
- while (brewTime < TIME_SOAK) {
- usleep(100000);
- if (getSigValue(MODE_STATE) == SigInt0Psh) {
- stopBrewing();
- return;
- }
- }
- stopBrewing();
- /*
- * Brewing the actual espresso
- */
- logger(V_BREW, "Starting infusion...\n");
- halRelaisOn(RELAIS_PUMP);
- brewTimer.start();
- while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
- usleep(100000);
- if (getSigValue(MODE_STATE) == SigInt0Psh){
- stopBrewing();
- break;
- }
- }
- stopBrewing();
- return;
- }
- /*
- * Wrapper function for the end of a brewing process
- * this function stops the pump, brewtimer and resets the flow and brew time to zero
- */
- void stopBrewing() {
- halRelaisOff(RELAIS_PUMP);
- brewTimer.stop();
- brewTime = 0;
- halResetFlow();
- }
- /**
- *
- */
- void coffeeIncreaseBrewCounter(void) {
- brewCounter++;
- }
- /**
- *
- */
- void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
- totalHeatingTime += heatingTime;
- }
- /**
- * Checks if the descaling is necessary
- * uses descBrewcount and descTimestamp
- */
- void checkDescaling(){
- time_t rawtime;
- time(&rawtime);
- double diffseconds = difftime(rawtime, descRawTimestamp);
- diffseconds /= 24*60*60;
- if((brewCounter - descBrewcount) >= DIRTY_ESPRESSO) {
- logger(V_BREW, "Descaling necessary due to quantity: %d\n", brewCounter - descBrewcount);
- descaling = true;
- event_trigger("descaling", &descaling, sizeof(bool));
- }
- if(diffseconds >= DIRTY_TIME) {
- logger(V_BREW, "Descaling necessary due to time in days: %d\n", diffseconds);
- descaling = true;
- event_trigger("descaling", &descaling, sizeof(bool));
- }
- }
- /**
- * updates the corresponding variables after a descaling process
- */
- void updateDescaling(){
- descBrewcount = brewCounter;
- time_t newDesTimestamp;
- time(&newDesTimestamp);
- if(newDesTimestamp == -1){
- logger(V_BREW, "Whoops, couldn't retrieve new descaling timestamp\n");
- }
- else {
- descRawTimestamp = newDesTimestamp;
- }
- }
|