coffee.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. int sigValue;
  26. int brewTime; //Brew time in ms
  27. timer brewTimer(&brewTimeHandler);
  28. uint64_t totalHeatingTime; //local copies of the corresponding database entries
  29. uint16_t brewCounter;
  30. bool initalHeating;
  31. const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR", "WAITOFF"};
  32. /**
  33. * Thread for the finite state machine
  34. * It represents the current state of the machine and handles signals coming from
  35. * the pressure control, buttons, the brew switch and the proximity sensor
  36. * @param threadid the ID of the thread
  37. */
  38. void *coffeeThread(void *threadid) {
  39. logger(V_BASIC, "Initializing coffee thread...\n");
  40. //installing new Signal handler for coffeethread
  41. struct sigaction action;
  42. sigemptyset(&action.sa_mask);
  43. action.sa_flags = SA_SIGINFO;
  44. action.sa_sigaction = coffeeHandler;
  45. sigaction(SIGUSR2, &action, NULL);
  46. brewTimer.setDivider(4);
  47. brewTimer.stop();
  48. brewTime = 0;
  49. lastState = STATE_OFF;
  50. initalHeating = true;
  51. //read the database values
  52. if(!(totalHeatingTime = sqlGetConf(CFGHeatingTime))){
  53. logger_error("coffee.cpp: Couldn't read the heating time from the database");
  54. //pthread_exit(EXIT_SUCCESS);
  55. exit(EXIT_FAILURE);
  56. }
  57. if(!(brewCounter = sqlGetConf(CFGbrewcounter))){
  58. logger_error("coffee.cpp: Couldn't read the brew counter from the database");
  59. //pthread_exit(EXIT_SUCCESS);
  60. exit(EXIT_FAILURE);
  61. }
  62. event_subscribe("terminate", &coffeeTerminate);
  63. logger(V_BREW, "Determining inital state\n");
  64. //determine inital state
  65. if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
  66. && !halGetRelaisState(RELAIS_PUMP)) {
  67. //wait for heat relais to switch
  68. sleep(1);
  69. if (halIsHeating()) { //Heating is on
  70. changeState(STATE_INITALHEATING);
  71. } else {
  72. initalHeating = false;
  73. changeState(STATE_IDLE);
  74. }
  75. } else if (halGetRelaisState(RELAIS_PUMP)) {
  76. logger_error("Whoops, why is the pump running...\n");
  77. changeState(STATE_ERROR);
  78. } else {
  79. changeState(STATE_OFF);
  80. }
  81. logger(V_BREW, "Starting Coffee FSM\n");
  82. //begin FSM
  83. while (1) {
  84. switch (state) {
  85. /*
  86. *
  87. */
  88. case STATE_OFF:
  89. halMachineOff();
  90. writeBackCache();
  91. if (SigValueEmpty())
  92. pause();
  93. if (getSigValue() == SigInt0Rls) {
  94. //Check waterlevel in gray water tank
  95. //turn machine on
  96. halMachineOn();
  97. sleep(1);
  98. if (halIsHeating()) { //check if System starts to heat when turned on
  99. initalHeating = true;
  100. changeState(STATE_INITALHEATING);
  101. } else {
  102. changeState(STATE_IDLE);
  103. }
  104. if (halProxSensorCovered()) {
  105. logger_error("Empty Tank please!\n");//change the state again to go back to the last state afterwards
  106. changeState(STATE_FULLTANK);
  107. }
  108. break;
  109. }
  110. break;
  111. /*
  112. *
  113. */
  114. case STATE_WAIT_OFF:
  115. if (SigValueEmpty())
  116. pause();
  117. switch (getSigValue()) {
  118. case SigPressOpn:
  119. usleep(100000);//wait so no load will be switched
  120. coffeeIncreaseHeatingTime(halgetHeatingTime());
  121. changeState(STATE_OFF);
  122. break;
  123. case SigInt0Psh:
  124. case SigInt1Psh:
  125. if (halProxSensorCovered()) {
  126. changeState(STATE_FULLTANK);
  127. } else if (initalHeating) {
  128. changeState(STATE_INITALHEATING);
  129. } else {
  130. changeState(STATE_HEATING);
  131. }
  132. break;
  133. }
  134. break;
  135. /*
  136. *
  137. */
  138. case STATE_INITALHEATING:
  139. initalHeating = true;
  140. if (SigValueEmpty())
  141. pause();
  142. switch (getSigValue()) {
  143. case SigInt1RlsLong:
  144. //Turn machine off again
  145. coffeeIncreaseHeatingTime(halgetHeatingTime());
  146. changeState(STATE_OFF);
  147. break;
  148. case SigInt1Rls:
  149. changeState(STATE_WAIT_OFF);
  150. break;
  151. case SigProxCvrd:
  152. changeState(STATE_FULLTANK);
  153. break;
  154. case SigPressOpn:
  155. //Inital heating finished
  156. initalHeating = false;
  157. coffeeIncreaseHeatingTime(halgetHeatingTime());
  158. changeState(STATE_IDLE);
  159. break;
  160. }
  161. break;
  162. /*
  163. *
  164. */
  165. case STATE_HEATING:
  166. if (SigValueEmpty())
  167. pause();
  168. switch (getSigValue()) {
  169. case SigInt1RlsLong:
  170. //Turn machine _immediately_ off again
  171. coffeeIncreaseHeatingTime(halgetHeatingTime());
  172. changeState(STATE_OFF);
  173. break;
  174. case SigInt1Rls:
  175. //turn machine off when heating is finished
  176. changeState(STATE_WAIT_OFF);
  177. break;
  178. case SigPressOpn:
  179. coffeeIncreaseHeatingTime(halgetHeatingTime());
  180. changeState(STATE_IDLE);
  181. break;
  182. case SigInt0Psh:
  183. //start to brew a delicious coffee
  184. changeState(STATE_BREW);
  185. break;
  186. case SigProxCvrd:
  187. changeState(STATE_FULLTANK);
  188. break;
  189. case SigBrewOn:
  190. //someone brews manually
  191. changeState(STATE_BREWMANUAL);
  192. break;
  193. }
  194. break;
  195. /*
  196. *
  197. */
  198. case STATE_IDLE:
  199. if (SigValueEmpty())
  200. pause();
  201. switch (getSigValue()) {
  202. case SigInt1RlsLong:
  203. //turn machine _immediately_ off
  204. changeState(STATE_OFF);
  205. break;
  206. case SigInt1Rls:
  207. changeState(STATE_OFF);
  208. break;
  209. case SigPressCls:
  210. changeState(STATE_HEATING);
  211. break;
  212. case SigInt0Psh:
  213. changeState(STATE_BREW);
  214. break;
  215. case SigProxCvrd:
  216. changeState(STATE_FULLTANK);
  217. break;
  218. case SigBrewOn:
  219. //someone brews manually
  220. changeState(STATE_BREWMANUAL);
  221. break;
  222. }
  223. break;
  224. /*
  225. *
  226. */
  227. case STATE_BREW:
  228. //make sure the tank is not full
  229. if (halProxSensorCovered()) {
  230. changeState(STATE_FULLTANK);
  231. logger_error("coffee.cpp: Full tank detection failed..\n");
  232. } else {
  233. coffeeBrew();
  234. logger(V_BREW, "Finishing brewing\n");
  235. if (!halProxSensorCovered()) {
  236. if (halIsHeating()) {
  237. changeState(STATE_HEATING);
  238. } else {
  239. changeState(STATE_IDLE);
  240. }
  241. } else {
  242. changeState(STATE_FULLTANK);
  243. }
  244. }
  245. break;
  246. /*
  247. *
  248. */
  249. case STATE_BREWMANUAL:
  250. if (SigValueEmpty())
  251. pause();
  252. break;
  253. /*
  254. *
  255. */
  256. case STATE_CLEANING:
  257. if (SigValueEmpty())
  258. pause();
  259. break;
  260. /*
  261. * Full tank is detected at the beginning and the end of a brewing process, during
  262. * idle times, initial heating and heating
  263. */
  264. case STATE_FULLTANK:
  265. if (SigValueEmpty())
  266. pause();
  267. switch (getSigValue()) {
  268. case SigInt1Psh:
  269. case SigInt0Psh:
  270. if(halIsHeating() && initalHeating){
  271. changeState(STATE_INITALHEATING);
  272. } else if (halIsHeating() && !initalHeating){
  273. changeState(STATE_HEATING);
  274. } else {
  275. changeState(STATE_IDLE);
  276. }
  277. break;
  278. }
  279. break;
  280. /*
  281. *
  282. */
  283. case STATE_ERROR:
  284. if (SigValueEmpty())
  285. pause();
  286. switch (getSigValue()) {
  287. case SigInt1RlsLong:
  288. case SigInt0RlsLong:
  289. if(halIsHeating()){
  290. coffeeIncreaseHeatingTime(halgetHeatingTime());
  291. }
  292. changeState(STATE_OFF);
  293. break;
  294. }
  295. }
  296. }
  297. pthread_exit(EXIT_SUCCESS);
  298. }
  299. /**
  300. * Handler for the Signal send to this thread
  301. * It saves the type of signal received and tracks the time between a push and release event of up to 4 signals
  302. * The time is stored in the HalEvent variable when a release event is received
  303. * @param signum
  304. * @param siginfo
  305. * @param context
  306. */
  307. void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
  308. sigval_t sigVal = (siginfo->si_value);
  309. sigValue = sigVal.sival_int;
  310. logger(V_BREW, "coffee.cpp: CoffeeHandler called with Signal %d\n", sigValue);
  311. }
  312. /**
  313. * returns the Signal value from the last received Signal and clears the variable
  314. * @return value sent with the last signal
  315. */
  316. int getSigValue(void) {
  317. int tmp = sigValue;
  318. sigValue = 0;
  319. return tmp;
  320. }
  321. bool SigValueEmpty(void) {
  322. if (sigValue == 0)
  323. return true;
  324. else
  325. return false;
  326. }
  327. /**
  328. * Changes the state of the machine to newState
  329. * prints the change to the logger
  330. * @param newState
  331. */
  332. void changeState(coffee_status_t newState) {
  333. logger(V_BREW, "Changing state to %s\n", StateName[newState]);
  334. state = newState;
  335. event_trigger("statechange", &state, sizeof(state));
  336. }
  337. /**
  338. * Returns the current state of the FSM
  339. */
  340. coffee_status_t getState(void) {
  341. return state;
  342. }
  343. /**
  344. * Counter for the brew time
  345. * refresh every 200ms
  346. */
  347. void brewTimeHandler(void) {
  348. brewTime += 200;
  349. }
  350. /**
  351. * handles program termination
  352. */
  353. void coffeeTerminate(event_t *event) {
  354. logger(V_BREW, "Coffee.cpp: Thread terminating");
  355. //stop brewing
  356. halRelaisOff(RELAIS_PUMP);
  357. brewTimer.stop();
  358. writeBackCache();
  359. }
  360. /**
  361. * Function to write back the values of the local copies
  362. * brewCounter and totalHeatingTime
  363. *
  364. */
  365. void writeBackCache(void){
  366. if (sqlSetConf(CFGbrewcounter, brewCounter)) {
  367. logger_error("coffee.cpp: Couldn't write brewcounter to database");
  368. return;
  369. }
  370. if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
  371. logger_error("coffee.cpp: Couldn't write heating time to database");
  372. return;
  373. }
  374. }
  375. /**
  376. * Brewing process
  377. */
  378. void coffeeBrew(void) {
  379. coffeeIncreaseBrewCounter();
  380. /*
  381. * Preinfusion
  382. */
  383. logger(V_BREW, "Starting preinfusion...\n");
  384. halResetFlow();
  385. halRelaisOn(RELAIS_PUMP);
  386. brewTime = 0;
  387. brewTimer.start();
  388. while (halGetFlow() < AMOUNT_PREINFUSION) {
  389. usleep(50000);
  390. if (getSigValue() == SigInt1Psh)
  391. return;
  392. }
  393. brewTimer.stop();
  394. brewTime = 0;
  395. halRelaisOff(RELAIS_PUMP);
  396. /*
  397. * Wait for coffee to soak in infused water
  398. */
  399. brewTimer.start();
  400. while (brewTime < TIME_SOAK) {
  401. usleep(100000);
  402. if (getSigValue() == SigInt1Psh)
  403. return;
  404. }
  405. brewTimer.stop();
  406. brewTime = 0;
  407. halResetFlow();
  408. /*
  409. * Brewing the actual espresso
  410. */
  411. logger(V_BREW, "Starting infusion...\n");
  412. halRelaisOn(RELAIS_PUMP);
  413. brewTimer.start();
  414. while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
  415. usleep(100000);
  416. if (getSigValue() == SigInt1Psh)
  417. return;
  418. }
  419. halRelaisOff(RELAIS_PUMP);
  420. halResetFlow();
  421. brewTimer.stop();
  422. brewTime = 0;
  423. return;
  424. }
  425. /**
  426. *
  427. */
  428. void coffeeIncreaseBrewCounter(void) {
  429. brewCounter++;
  430. }
  431. /**
  432. *
  433. */
  434. void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
  435. totalHeatingTime += heatingTime;
  436. }