coffee.cpp 15 KB

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