coffee.cpp 9.1 KB

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