coffee.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 SigInt0Rls:
  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 SigInt0Rls:
  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 SigInt0Rls:
  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 SigInt0Rls:
  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. /*
  197. * Hardware FSM
  198. */
  199. switch (state) {
  200. /*
  201. *
  202. */
  203. case STATE_OFF:
  204. if (mode == MODE_STATE) {
  205. halMachineOff();
  206. writeBackCache();
  207. changePage(PAGE_DEMO);
  208. if (SigValueEmpty())
  209. pause();
  210. }
  211. switch (getSigValue(MODE_STATE)) {
  212. case SigInt0Rls:
  213. //Check waterlevel in gray water tank
  214. //turn machine on
  215. halMachineOn();
  216. sleep(1);
  217. if (halIsHeating() && !halProxSensorCovered()) { //check if System starts to heat when turned on
  218. changeState(STATE_INITALHEATING);
  219. } else if (!halIsHeating() && !halProxSensorCovered()) {
  220. changeState(STATE_IDLE);
  221. } else if (halProxSensorCovered()) {
  222. logger_error("Empty Tank please!\n");
  223. changeState(STATE_FULLTANK);
  224. }
  225. if (page != PAGE_SOFTOFF)
  226. changePage(PAGE_SOFTOFF); //the machine is on, the menu starts with the turning off page
  227. break;
  228. case SigInt1Psh:
  229. //Enter the menu
  230. if (page != PAGE_DEMO)
  231. changePage(PAGE_DEMO); //machine is off, the menu starts with the demo page
  232. changeMode(MODE_MENU);
  233. break;
  234. }
  235. break;
  236. /*
  237. *
  238. */
  239. case STATE_WAIT_OFF:
  240. if (SigValueEmpty() && mode == MODE_STATE)
  241. pause();
  242. switch (getSigValue(MODE_STATE)) {
  243. case SigPressOpn:
  244. usleep(100000); //wait so no load will be switched
  245. coffeeIncreaseHeatingTime(halgetHeatingTime());
  246. changeState(STATE_OFF);
  247. break;
  248. case SigInt0Psh:
  249. case SigInt1Psh:
  250. if (halProxSensorCovered()) {
  251. changeState(STATE_FULLTANK);
  252. } else if (initalHeating) {
  253. changeState(STATE_INITALHEATING);
  254. } else {
  255. changeState(STATE_HEATING);
  256. }
  257. break;
  258. }
  259. break;
  260. /*
  261. *
  262. */
  263. case STATE_INITALHEATING:
  264. initalHeating = true;
  265. if (SigValueEmpty() && mode == MODE_STATE)
  266. pause();
  267. switch (getSigValue(MODE_STATE)) {
  268. // case SigInt0RlsLong:
  269. // //Turn machine off again
  270. // coffeeIncreaseHeatingTime(halgetHeatingTime());
  271. // changeState(STATE_OFF);
  272. // break;
  273. //
  274. // case SigInt0Rls:
  275. // changeState(STATE_WAIT_OFF);
  276. // break;
  277. case SigProxCvrd:
  278. changeState(STATE_FULLTANK);
  279. break;
  280. case SigPressOpn:
  281. //Inital heating finished
  282. initalHeating = false;
  283. coffeeIncreaseHeatingTime(halgetHeatingTime());
  284. changeState(STATE_IDLE);
  285. break;
  286. case SigInt1Psh:
  287. changeMode(MODE_MENU);
  288. break;
  289. }
  290. break;
  291. /*
  292. *
  293. */
  294. case STATE_HEATING:
  295. if (SigValueEmpty() && mode == MODE_STATE)
  296. pause();
  297. switch (getSigValue(MODE_STATE)) {
  298. // case SigInt1RlsLong:
  299. // //Turn machine _immediately_ off again
  300. // coffeeIncreaseHeatingTime(halgetHeatingTime());
  301. // changeState(STATE_OFF);
  302. // break;
  303. //
  304. // case SigInt1Rls:
  305. // //turn machine off when heating is finished
  306. // changeState(STATE_WAIT_OFF);
  307. // break;
  308. case SigPressOpn:
  309. coffeeIncreaseHeatingTime(halgetHeatingTime());
  310. changeState(STATE_IDLE);
  311. break;
  312. case SigInt0Psh:
  313. //start to brew a delicious coffee
  314. changeState(STATE_BREW);
  315. break;
  316. case SigProxCvrd:
  317. changeState(STATE_FULLTANK);
  318. break;
  319. case SigBrewOn:
  320. //someone brews manually
  321. changeState(STATE_BREWMANUAL);
  322. break;
  323. case SigInt1Psh:
  324. //Enter the menu
  325. changeMode(MODE_MENU);
  326. break;
  327. }
  328. break;
  329. /*
  330. *
  331. */
  332. case STATE_IDLE:
  333. if (SigValueEmpty() && mode == MODE_STATE)
  334. pause();
  335. switch (getSigValue(MODE_STATE)) {
  336. // case SigInt1RlsLong:
  337. // //turn machine _immediately_ off
  338. // changeState(STATE_OFF);
  339. // break;
  340. //
  341. // case SigInt1Rls:
  342. // changeState(STATE_OFF);
  343. // break;
  344. case SigPressCls:
  345. changeState(STATE_HEATING);
  346. break;
  347. case SigInt0Psh:
  348. changeState(STATE_BREW);
  349. break;
  350. case SigProxCvrd:
  351. changeState(STATE_FULLTANK);
  352. break;
  353. case SigBrewOn:
  354. //someone brews manually
  355. changeState(STATE_BREWMANUAL);
  356. break;
  357. case SigInt1Psh:
  358. //Enter the menu
  359. changeMode(MODE_MENU);
  360. break;
  361. }
  362. break;
  363. /*
  364. *
  365. */
  366. case STATE_BREW:
  367. //make sure the tank is not full
  368. if (halProxSensorCovered()) {
  369. changeState(STATE_FULLTANK);
  370. logger_error("coffee.cpp: Full tank detection failed..\n");
  371. } else {
  372. coffeeBrew();
  373. logger(V_BREW, "Finishing brewing\n");
  374. if (!halProxSensorCovered()) {
  375. if (halIsHeating()) {
  376. changeState(STATE_HEATING);
  377. } else {
  378. changeState(STATE_IDLE);
  379. }
  380. } else {
  381. changeState(STATE_FULLTANK);
  382. }
  383. }
  384. break;
  385. /*
  386. *
  387. */
  388. case STATE_BREWMANUAL:
  389. if (SigValueEmpty() && mode == MODE_STATE)
  390. pause();
  391. break;
  392. /*
  393. *
  394. */
  395. case STATE_CLEANING: //this can only be executed once the machine is hot!
  396. if (SigValueEmpty() && mode == MODE_STATE)
  397. pause();
  398. if (!halProxSensorCovered()) {
  399. //execute the cleaning procedure
  400. coffeeClean();
  401. if (halIsHeating()) {
  402. changeState(STATE_HEATING);
  403. } else {
  404. changeState(STATE_IDLE);
  405. }
  406. } else {
  407. changeState(STATE_FULLTANK);
  408. }
  409. break;
  410. /*
  411. * Full tank is detected at the beginning and the end of a brewing process, during
  412. * idle times, initial heating and heating
  413. */
  414. case STATE_FULLTANK:
  415. if (SigValueEmpty() && mode == MODE_STATE)
  416. pause();
  417. switch (getSigValue(MODE_STATE)) {
  418. case SigInt1Psh:
  419. case SigInt0Psh:
  420. if (halIsHeating() && initalHeating) {
  421. changeState(STATE_INITALHEATING);
  422. } else if (halIsHeating() && !initalHeating) {
  423. changeState(STATE_HEATING);
  424. } else {
  425. changeState(STATE_IDLE);
  426. }
  427. break;
  428. }
  429. break;
  430. /*
  431. *
  432. */
  433. case STATE_ERROR:
  434. if (SigValueEmpty() && mode == MODE_STATE)
  435. pause();
  436. switch (getSigValue(MODE_STATE)) {
  437. case SigInt1RlsLong:
  438. case SigInt0RlsLong:
  439. if (halIsHeating()) {
  440. coffeeIncreaseHeatingTime(halgetHeatingTime());
  441. }
  442. changeState(STATE_OFF);
  443. break;
  444. }
  445. }
  446. }
  447. pthread_exit(EXIT_SUCCESS);
  448. }
  449. /**
  450. * Handler for the Signal send to this thread
  451. * It saves the type of signal received and tracks the time between a push and release event of up to 4 signals
  452. * The time is stored in the HalEvent variable when a release event is received
  453. * @param signum
  454. * @param siginfo
  455. * @param context
  456. */
  457. void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
  458. sigval_t sigVal = (siginfo->si_value);
  459. sigValue = sigVal.sival_int;
  460. logger(V_BREW, "coffee.cpp: CoffeeHandler called with Signal %d\n",
  461. sigValue);
  462. }
  463. /**
  464. * returns the Signal value from the last received Signal for the given mode and clears the variable
  465. * @return value sent with the last signal
  466. */
  467. int getSigValue(coffee_mode_t mode) {
  468. int tmp = sigValue;
  469. if (mode == MODE_MENU) {
  470. switch (sigValue) {
  471. case SigInt0Psh:
  472. case SigInt0Rls:
  473. case SigInt0RlsLong:
  474. case SigInt1Psh:
  475. case SigInt1Rls:
  476. case SigInt1RlsLong:
  477. sigValue = 0;
  478. return tmp;
  479. break;
  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. * sets the start page for the next menu call to softoff
  530. */
  531. void leaveMenu() {
  532. logger(V_BREW, "Leaving the menu again...\n");
  533. //leave the menu again
  534. changeMode(MODE_STATE);
  535. //change page to initial page
  536. changePage(PAGE_SOFTOFF);
  537. }
  538. /**
  539. * Returns the current state of the FSM
  540. */
  541. coffee_status_t getState(void) {
  542. return state;
  543. }
  544. /**
  545. * Returns the local up-to-date brewcounter
  546. */
  547. uint16_t getBrewCounter(void) {
  548. return brewCounter;
  549. }
  550. /**
  551. * Counter for the brew time
  552. * refresh every 200ms
  553. */
  554. void brewTimeHandler(void) {
  555. brewTime += 200;
  556. }
  557. /**
  558. * handles program termination
  559. */
  560. void coffeeTerminate(event_t *event) {
  561. logger(V_BREW, "Coffee.cpp: Thread terminating");
  562. //stop brewing
  563. halRelaisOff(RELAIS_PUMP);
  564. brewTimer.stop();
  565. writeBackCache();
  566. }
  567. /**
  568. * Function to write back the values of the local copies
  569. * brewCounter and totalHeatingTime
  570. *
  571. */
  572. void writeBackCache(void) {
  573. if (sqlSetConf(CFGbrewcounter, brewCounter)) {
  574. logger_error("coffee.cpp: Couldn't write brewcounter to database");
  575. return;
  576. }
  577. if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
  578. logger_error("coffee.cpp: Couldn't write heating time to database");
  579. return;
  580. }
  581. }
  582. /*
  583. * Procedure for cleaning the machine
  584. */
  585. void coffeeClean(void) {
  586. logger(V_BREW, "Cleaning...\n");
  587. for (int i = 0; i < 20; i++) {
  588. halRelaisOn(RELAIS_PUMP);
  589. sleep(3);
  590. halRelaisOff(RELAIS_PUMP);
  591. sleep(15);
  592. }
  593. descaling = false;
  594. }
  595. /**
  596. * Brewing process
  597. */
  598. void coffeeBrew(void) {
  599. coffeeIncreaseBrewCounter();
  600. /*
  601. * Preinfusion
  602. */
  603. logger(V_BREW, "Starting preinfusion...\n");
  604. halResetFlow();
  605. halRelaisOn(RELAIS_PUMP);
  606. brewTime = 0;
  607. brewTimer.start();
  608. while (halGetFlow() < AMOUNT_PREINFUSION) {
  609. usleep(50000);
  610. if (getSigValue(MODE_STATE) == SigInt0Psh){
  611. stopBrewing();
  612. return;
  613. }
  614. }
  615. stopBrewing();
  616. /*
  617. * Wait for coffee to soak in infused water
  618. */
  619. brewTimer.start();
  620. while (brewTime < TIME_SOAK) {
  621. usleep(100000);
  622. if (getSigValue(MODE_STATE) == SigInt0Psh) {
  623. stopBrewing();
  624. return;
  625. }
  626. }
  627. stopBrewing();
  628. /*
  629. * Brewing the actual espresso
  630. */
  631. logger(V_BREW, "Starting infusion...\n");
  632. halRelaisOn(RELAIS_PUMP);
  633. brewTimer.start();
  634. while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
  635. usleep(100000);
  636. if (getSigValue(MODE_STATE) == SigInt0Psh){
  637. stopBrewing();
  638. break;
  639. }
  640. }
  641. stopBrewing();
  642. return;
  643. }
  644. /*
  645. * Wrapper function for the end of a brewing process
  646. * this function stops the pump, brewtimer and resets the flow and brew time to zero
  647. */
  648. void stopBrewing() {
  649. halRelaisOff(RELAIS_PUMP);
  650. brewTimer.stop();
  651. brewTime = 0;
  652. halResetFlow();
  653. }
  654. /**
  655. *
  656. */
  657. void coffeeIncreaseBrewCounter(void) {
  658. brewCounter++;
  659. if ((brewCounter % DIRTY_ESPRESSO) == 0)
  660. descaling = true;
  661. }
  662. /**
  663. *
  664. */
  665. void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
  666. totalHeatingTime += heatingTime;
  667. }