coffee.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * coffee.cpp
  3. *
  4. * Created on: Sep 25, 2017
  5. * Author: sebastian
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <stdint.h>
  12. #include <wiringPi.h>
  13. #include <pthread.h>
  14. #include <unistd.h>
  15. #include <iostream>
  16. #include <csignal>
  17. #include <time.h>
  18. #include <ctime>
  19. #include "coffee.h"
  20. #include "hal.h"
  21. #include "logger.h"
  22. #include "timer.h"
  23. #include "database.h"
  24. coffee_status_t state;
  25. coffee_menuPage_t page;
  26. coffee_mode_t mode;
  27. int sigValue;
  28. int brewTime; //Brew time in ms
  29. timer brewTimer(&brewTimeHandler);
  30. uint64_t totalHeatingTime; //local copies of the corresponding database entries
  31. uint16_t brewCounter;
  32. bool initalHeating;
  33. bool descaling; //flag to indicate descaling and cleaning
  34. const char* PageName[] = {"SoftOff", "Kill", "Stats", "Temp", "Clean", "Demo", "Exit"};
  35. const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR", "WAITOFF"};
  36. /**
  37. * Thread for the finite state machine
  38. * It represents the current state of the machine and handles signals coming from
  39. * the pressure control, buttons, the brew switch and the proximity sensor
  40. * @param threadid the ID of the thread
  41. */
  42. void *coffeeThread(void *threadid) {
  43. logger(V_BASIC, "Initializing coffee thread...\n");
  44. //installing new Signal handler for coffeethread
  45. struct sigaction action;
  46. sigemptyset(&action.sa_mask);
  47. action.sa_flags = SA_SIGINFO;
  48. action.sa_sigaction = coffeeHandler;
  49. sigaction(SIGUSR2, &action, NULL);
  50. brewTimer.setDivider(4);
  51. brewTimer.stop();
  52. brewTime = 0;
  53. initalHeating = true;
  54. mode = MODE_STATE; //Unless we enter the menu we start in state mode
  55. page = PAGE_SOFTOFF;
  56. descaling = false;
  57. //read the database values
  58. if(!(totalHeatingTime = sqlGetConf(CFGHeatingTime))){
  59. logger_error("coffee.cpp: Couldn't read the heating time from the database");
  60. //pthread_exit(EXIT_SUCCESS);
  61. exit(EXIT_FAILURE);
  62. }
  63. if(!(brewCounter = sqlGetConf(CFGbrewcounter))){
  64. logger_error("coffee.cpp: Couldn't read the brew counter from the database");
  65. //pthread_exit(EXIT_SUCCESS);
  66. exit(EXIT_FAILURE);
  67. }
  68. event_subscribe("terminate", &coffeeTerminate);
  69. logger(V_BREW, "Determining inital state\n");
  70. //determine inital state
  71. if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
  72. && !halGetRelaisState(RELAIS_PUMP)) {
  73. //wait for heat relais to switch
  74. sleep(1);
  75. if (halIsHeating()) { //Heating is on
  76. changeState(STATE_INITALHEATING);
  77. } else {
  78. initalHeating = false;
  79. changeState(STATE_IDLE);
  80. }
  81. } else if (halGetRelaisState(RELAIS_PUMP)) {
  82. logger_error("Whoops, why is the pump running...\n");
  83. changeState(STATE_ERROR);
  84. } else {
  85. changeState(STATE_OFF);
  86. }
  87. logger(V_BREW, "Starting Coffee FSM\n");
  88. //begin FSM
  89. while (1) {
  90. /*
  91. * Menue FSM
  92. */
  93. switch (page) {
  94. case PAGE_SOFTOFF: //this page is only available when the machine is on
  95. if (SigValueEmpty() && mode == MODE_MENU)
  96. pause();
  97. switch (getSigValue(MODE_MENU)) {
  98. case SigInt0Psh:
  99. changeState(STATE_WAIT_OFF);
  100. leaveMenu();
  101. break;
  102. case SigInt1Psh:
  103. changePage(PAGE_KILL);
  104. break;
  105. }
  106. break;
  107. case PAGE_KILL: //this page is only available when the machine is on
  108. if (SigValueEmpty() && mode == MODE_MENU)
  109. pause();
  110. switch (getSigValue(MODE_MENU)) {
  111. case SigInt0Psh:
  112. if(halIsHeating()){
  113. coffeeIncreaseHeatingTime(halgetHeatingTime());
  114. }
  115. changeState(STATE_OFF);
  116. leaveMenu();
  117. break;
  118. case SigInt1Psh:
  119. if(state == STATE_IDLE || state == STATE_HEATING){
  120. changePage(PAGE_CLEAN);
  121. }
  122. else {
  123. changePage(PAGE_DEMO);
  124. }
  125. break;
  126. }
  127. break;
  128. case PAGE_CLEAN: //this page is only be available when the machine is hot
  129. if (SigValueEmpty() && mode == MODE_MENU)
  130. pause();
  131. switch (getSigValue(MODE_MENU)) {
  132. case SigInt0Psh:
  133. changeMode(MODE_STATE);
  134. if(!halProxSensorCovered()){
  135. changeState(STATE_CLEANING);
  136. leaveMenu();
  137. }
  138. else {
  139. changeState(STATE_FULLTANK);
  140. leaveMenu();
  141. }
  142. break;
  143. case SigInt1Psh:
  144. changePage(PAGE_DEMO);
  145. break;
  146. }
  147. break;
  148. case PAGE_DEMO:
  149. if (SigValueEmpty() && mode == MODE_MENU)
  150. pause();
  151. switch (getSigValue(MODE_MENU)) {
  152. case SigInt1Psh:
  153. changePage(PAGE_TEMP);
  154. break;
  155. }
  156. break;
  157. case PAGE_TEMP:
  158. if (SigValueEmpty() && mode == MODE_MENU)
  159. pause();
  160. switch (getSigValue(MODE_MENU)) {
  161. case SigInt1Psh:
  162. changePage(PAGE_STATS);
  163. break;
  164. }
  165. break;
  166. case PAGE_STATS:
  167. if (SigValueEmpty() && mode == MODE_MENU)
  168. pause();
  169. switch (getSigValue(MODE_MENU)) {
  170. case SigInt1Psh:
  171. changePage(PAGE_EXIT);
  172. break;
  173. }
  174. break;
  175. case PAGE_EXIT:
  176. if (SigValueEmpty() && mode == MODE_MENU)
  177. pause();
  178. switch (getSigValue(MODE_MENU)) {
  179. case SigInt0Psh:
  180. leaveMenu();
  181. break;
  182. case SigInt1Psh:
  183. if(state == STATE_HEATING || state == STATE_ERROR || state == STATE_IDLE || state == STATE_INITALHEATING){
  184. changePage(PAGE_SOFTOFF);
  185. }
  186. else {
  187. changePage(PAGE_DEMO);
  188. }
  189. break;
  190. }
  191. break;
  192. } //end switch (page)
  193. /*
  194. * Hardware FSM
  195. */
  196. switch (state) {
  197. /*
  198. *
  199. */
  200. case STATE_OFF:
  201. halMachineOff();
  202. writeBackCache();
  203. page = PAGE_DEMO; //machine is off, the menu starts with the demo page
  204. if (SigValueEmpty() && mode == MODE_STATE)
  205. pause();
  206. switch (getSigValue(MODE_STATE)) {
  207. case SigInt0Rls:
  208. //Check waterlevel in gray water tank
  209. //turn machine on
  210. halMachineOn();
  211. sleep(1);
  212. if (halIsHeating() && !halProxSensorCovered()) { //check if System starts to heat when turned on
  213. changeState(STATE_INITALHEATING);
  214. } else if (!halIsHeating() && !halProxSensorCovered()){
  215. changeState(STATE_IDLE);
  216. }
  217. else if (halProxSensorCovered()) {
  218. logger_error("Empty Tank please!\n");
  219. changeState(STATE_FULLTANK);
  220. }
  221. page = PAGE_SOFTOFF; //the machine is on, the menu starts with the turning off page
  222. break;
  223. case SigInt1Psh:
  224. //Enter the menu
  225. changeMode(MODE_MENU);
  226. break;
  227. }
  228. break;
  229. /*
  230. *
  231. */
  232. case STATE_WAIT_OFF:
  233. if (SigValueEmpty() && mode == MODE_STATE)
  234. pause();
  235. switch (getSigValue(MODE_STATE)) {
  236. case SigPressOpn:
  237. usleep(100000);//wait so no load will be switched
  238. coffeeIncreaseHeatingTime(halgetHeatingTime());
  239. changeState(STATE_OFF);
  240. break;
  241. case SigInt0Psh:
  242. case SigInt1Psh:
  243. if (halProxSensorCovered()) {
  244. changeState(STATE_FULLTANK);
  245. } else if (initalHeating) {
  246. changeState(STATE_INITALHEATING);
  247. } else {
  248. changeState(STATE_HEATING);
  249. }
  250. break;
  251. }
  252. break;
  253. /*
  254. *
  255. */
  256. case STATE_INITALHEATING:
  257. initalHeating = true;
  258. if (SigValueEmpty() && mode == MODE_STATE)
  259. pause();
  260. switch (getSigValue(MODE_STATE)) {
  261. // case SigInt0RlsLong:
  262. // //Turn machine off again
  263. // coffeeIncreaseHeatingTime(halgetHeatingTime());
  264. // changeState(STATE_OFF);
  265. // break;
  266. //
  267. // case SigInt0Rls:
  268. // changeState(STATE_WAIT_OFF);
  269. // break;
  270. case SigProxCvrd:
  271. changeState(STATE_FULLTANK);
  272. break;
  273. case SigPressOpn:
  274. //Inital heating finished
  275. initalHeating = false;
  276. coffeeIncreaseHeatingTime(halgetHeatingTime());
  277. changeState(STATE_IDLE);
  278. break;
  279. case SigInt1Psh:
  280. changeMode(MODE_MENU);
  281. break;
  282. }
  283. break;
  284. /*
  285. *
  286. */
  287. case STATE_HEATING:
  288. if (SigValueEmpty() && mode == MODE_STATE)
  289. pause();
  290. switch (getSigValue(MODE_STATE)) {
  291. // case SigInt1RlsLong:
  292. // //Turn machine _immediately_ off again
  293. // coffeeIncreaseHeatingTime(halgetHeatingTime());
  294. // changeState(STATE_OFF);
  295. // break;
  296. //
  297. // case SigInt1Rls:
  298. // //turn machine off when heating is finished
  299. // changeState(STATE_WAIT_OFF);
  300. // break;
  301. case SigPressOpn:
  302. coffeeIncreaseHeatingTime(halgetHeatingTime());
  303. changeState(STATE_IDLE);
  304. break;
  305. case SigInt0Psh:
  306. //start to brew a delicious coffee
  307. changeState(STATE_BREW);
  308. break;
  309. case SigProxCvrd:
  310. changeState(STATE_FULLTANK);
  311. break;
  312. case SigBrewOn:
  313. //someone brews manually
  314. changeState(STATE_BREWMANUAL);
  315. break;
  316. case SigInt1Psh:
  317. //Enter the menu
  318. changeMode(MODE_MENU);
  319. break;
  320. }
  321. break;
  322. /*
  323. *
  324. */
  325. case STATE_IDLE:
  326. if (SigValueEmpty() && mode == MODE_STATE)
  327. pause();
  328. switch (getSigValue(MODE_STATE)) {
  329. // case SigInt1RlsLong:
  330. // //turn machine _immediately_ off
  331. // changeState(STATE_OFF);
  332. // break;
  333. //
  334. // case SigInt1Rls:
  335. // changeState(STATE_OFF);
  336. // break;
  337. case SigPressCls:
  338. changeState(STATE_HEATING);
  339. break;
  340. case SigInt0Psh:
  341. changeState(STATE_BREW);
  342. break;
  343. case SigProxCvrd:
  344. changeState(STATE_FULLTANK);
  345. break;
  346. case SigBrewOn:
  347. //someone brews manually
  348. changeState(STATE_BREWMANUAL);
  349. break;
  350. case SigInt1Psh:
  351. //Enter the menu
  352. changeMode(MODE_MENU);
  353. break;
  354. }
  355. break;
  356. /*
  357. *
  358. */
  359. case STATE_BREW:
  360. //make sure the tank is not full
  361. if (halProxSensorCovered()) {
  362. changeState(STATE_FULLTANK);
  363. logger_error("coffee.cpp: Full tank detection failed..\n");
  364. } else {
  365. coffeeBrew();
  366. logger(V_BREW, "Finishing brewing\n");
  367. if (!halProxSensorCovered()) {
  368. if (halIsHeating()) {
  369. changeState(STATE_HEATING);
  370. } else {
  371. changeState(STATE_IDLE);
  372. }
  373. } else {
  374. changeState(STATE_FULLTANK);
  375. }
  376. }
  377. break;
  378. /*
  379. *
  380. */
  381. case STATE_BREWMANUAL:
  382. if (SigValueEmpty() && mode == MODE_STATE)
  383. pause();
  384. break;
  385. /*
  386. *
  387. */
  388. case STATE_CLEANING: //this can only be executed once the machine is hot!
  389. if (SigValueEmpty() && mode == MODE_STATE)
  390. pause();
  391. if (!halProxSensorCovered()) {
  392. //execute the cleaning procedure
  393. coffeeClean();
  394. if (halIsHeating()) {
  395. changeState(STATE_HEATING);
  396. } else {
  397. changeState(STATE_IDLE);
  398. }
  399. } else {
  400. changeState(STATE_FULLTANK);
  401. }
  402. break;
  403. /*
  404. * Full tank is detected at the beginning and the end of a brewing process, during
  405. * idle times, initial heating and heating
  406. */
  407. case STATE_FULLTANK:
  408. if (SigValueEmpty() && mode == MODE_STATE)
  409. pause();
  410. switch (getSigValue(MODE_STATE)) {
  411. case SigInt1Psh:
  412. case SigInt0Psh:
  413. if(halIsHeating() && initalHeating){
  414. changeState(STATE_INITALHEATING);
  415. } else if (halIsHeating() && !initalHeating){
  416. changeState(STATE_HEATING);
  417. } else {
  418. changeState(STATE_IDLE);
  419. }
  420. break;
  421. }
  422. break;
  423. /*
  424. *
  425. */
  426. case STATE_ERROR:
  427. if (SigValueEmpty() && mode == MODE_STATE)
  428. pause();
  429. switch (getSigValue(MODE_STATE)) {
  430. case SigInt1RlsLong:
  431. case SigInt0RlsLong:
  432. if(halIsHeating()){
  433. coffeeIncreaseHeatingTime(halgetHeatingTime());
  434. }
  435. changeState(STATE_OFF);
  436. break;
  437. }
  438. }
  439. }
  440. pthread_exit(EXIT_SUCCESS);
  441. }
  442. /**
  443. * Handler for the Signal send to this thread
  444. * It saves the type of signal received and tracks the time between a push and release event of up to 4 signals
  445. * The time is stored in the HalEvent variable when a release event is received
  446. * @param signum
  447. * @param siginfo
  448. * @param context
  449. */
  450. void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
  451. sigval_t sigVal = (siginfo->si_value);
  452. sigValue = sigVal.sival_int;
  453. logger(V_BREW, "coffee.cpp: CoffeeHandler called with Signal %d\n", sigValue);
  454. }
  455. /**
  456. * returns the Signal value from the last received Signal for the given mode and clears the variable
  457. * @return value sent with the last signal
  458. */
  459. int getSigValue(coffee_mode_t mode) {
  460. if(mode == MODE_MENU) {
  461. switch (sigValue){
  462. case SigInt0Psh:
  463. case SigInt0Rls:
  464. case SigInt0RlsLong:
  465. case SigInt1Psh:
  466. case SigInt1Rls:
  467. case SigInt1RlsLong:
  468. int tmp = sigValue;
  469. sigValue = 0;
  470. return tmp;
  471. break;
  472. }
  473. }
  474. else { //State Mode
  475. int tmp = sigValue;
  476. sigValue = 0;
  477. return tmp;
  478. }
  479. int tmp = sigValue;
  480. sigValue = 0;
  481. return tmp;
  482. }
  483. bool SigValueEmpty(void) {
  484. if (sigValue == 0)
  485. return true;
  486. else
  487. return false;
  488. }
  489. /**
  490. * Changes the state of the machine to newState
  491. * prints the change to the logger
  492. * @param newState
  493. */
  494. void changeState(coffee_status_t newState) {
  495. logger(V_BREW, "Changing state to %s\n", StateName[newState]);
  496. state = newState;
  497. event_trigger("statechange", &state, sizeof(state));
  498. }
  499. /*
  500. * Change Page to new menu page
  501. */
  502. void changePage(coffee_menuPage_t newPage){
  503. logger(V_BREW, "Change Page to %s", PageName[newPage]);
  504. page = newPage;
  505. }
  506. /*
  507. * Changes the mode of the machine to the given mode
  508. */
  509. void changeMode(coffee_mode_t newmode){
  510. if(mode == MODE_MENU)
  511. logger(V_BREW, "Changing to menu mode\n");
  512. else
  513. logger(V_BREW, "Changing to state mode\n");
  514. mode = newmode;
  515. }
  516. /*
  517. * leaving the menu
  518. */
  519. void leaveMenu(){
  520. logger(V_BREW, "Leaving the menu again...\n");
  521. //leave the menu again
  522. changeMode(MODE_STATE);
  523. //change page to initial page
  524. changePage(PAGE_SOFTOFF);
  525. }
  526. /**
  527. * Returns the current state of the FSM
  528. */
  529. coffee_status_t getState(void) {
  530. return state;
  531. }
  532. /**
  533. * Counter for the brew time
  534. * refresh every 200ms
  535. */
  536. void brewTimeHandler(void) {
  537. brewTime += 200;
  538. }
  539. /**
  540. * handles program termination
  541. */
  542. void coffeeTerminate(event_t *event) {
  543. logger(V_BREW, "Coffee.cpp: Thread terminating");
  544. //stop brewing
  545. halRelaisOff(RELAIS_PUMP);
  546. brewTimer.stop();
  547. writeBackCache();
  548. }
  549. /**
  550. * Function to write back the values of the local copies
  551. * brewCounter and totalHeatingTime
  552. *
  553. */
  554. void writeBackCache(void){
  555. if (sqlSetConf(CFGbrewcounter, brewCounter)) {
  556. logger_error("coffee.cpp: Couldn't write brewcounter to database");
  557. return;
  558. }
  559. if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
  560. logger_error("coffee.cpp: Couldn't write heating time to database");
  561. return;
  562. }
  563. }
  564. /*
  565. * Procedure for cleaning the machine
  566. */
  567. void coffeeClean(void){
  568. logger(V_BREW, "Cleaning...\n");
  569. for(int i = 0; i < 20; i++){
  570. halRelaisOn(RELAIS_PUMP);
  571. sleep(3);
  572. halRelaisOff(RELAIS_PUMP);
  573. sleep(15);
  574. }
  575. descaling = false;
  576. }
  577. /**
  578. * Brewing process
  579. */
  580. void coffeeBrew(void) {
  581. coffeeIncreaseBrewCounter();
  582. /*
  583. * Preinfusion
  584. */
  585. logger(V_BREW, "Starting preinfusion...\n");
  586. halResetFlow();
  587. halRelaisOn(RELAIS_PUMP);
  588. brewTime = 0;
  589. brewTimer.start();
  590. while (halGetFlow() < AMOUNT_PREINFUSION) {
  591. usleep(50000);
  592. if (getSigValue(MODE_STATE) == SigInt0Psh)
  593. stopBrewing();
  594. return;
  595. }
  596. stopBrewing();
  597. /*
  598. * Wait for coffee to soak in infused water
  599. */
  600. brewTimer.start();
  601. while (brewTime < TIME_SOAK) {
  602. usleep(100000);
  603. if (getSigValue(MODE_STATE) == SigInt0Psh)
  604. stopBrewing();
  605. return;
  606. }
  607. stopBrewing();
  608. /*
  609. * Brewing the actual espresso
  610. */
  611. logger(V_BREW, "Starting infusion...\n");
  612. halRelaisOn(RELAIS_PUMP);
  613. brewTimer.start();
  614. while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
  615. usleep(100000);
  616. if (getSigValue(MODE_STATE) == SigInt0Psh)
  617. stopBrewing();
  618. return;
  619. }
  620. stopBrewing();
  621. return;
  622. }
  623. /*
  624. * Wrapper function for the end of a brewing process
  625. * this function stops the pump, brewtimer and resets the flow and brew time to zero
  626. */
  627. void stopBrewing(){
  628. halRelaisOff(RELAIS_PUMP);
  629. brewTimer.stop();
  630. brewTime = 0;
  631. halResetFlow();
  632. }
  633. /**
  634. *
  635. */
  636. void coffeeIncreaseBrewCounter(void) {
  637. brewCounter++;
  638. if((brewCounter % DIRTY_ESPRESSO) == 0)
  639. descaling = true;
  640. }
  641. /**
  642. *
  643. */
  644. void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
  645. totalHeatingTime += heatingTime;
  646. }