coffee.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #include "events.h"
  25. coffee_status_t state;
  26. int sigValue;
  27. int brewTime; //Brew time in ms
  28. timer brewTimer(&brewTimeHandler);
  29. clock_t beginHeating;
  30. clock_t endHeating;
  31. double heatingTime;
  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. heatingTime = 0;
  50. beginHeating = endHeating = clock();
  51. logger(V_BREW, "Determining inital state\n");
  52. //determine inital state
  53. if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
  54. && !halGetRelaisState(RELAIS_PUMP)) {
  55. //wait for heat relais to switch
  56. sleep(1);
  57. if (halIsHeating()) { //Heating is on
  58. changeState(STATE_INITALHEATING);
  59. } else {
  60. changeState(STATE_IDLE);
  61. }
  62. } else if (halGetRelaisState(RELAIS_PUMP)) {
  63. logger_error("Whoops, why is the pump running...\n");
  64. changeState(STATE_ERROR);
  65. } else {
  66. changeState(STATE_OFF);
  67. }
  68. logger(V_BREW, "Starting Coffee FSM\n");
  69. //begin FSM
  70. while (1) {
  71. switch (state) {
  72. /*
  73. *
  74. */
  75. case STATE_OFF:
  76. if (SigValueEmpty())
  77. pause();
  78. if (getSigValue() == SigInt0Rls) {
  79. if (halProxSensorCovered()) { //Check Waterlevel
  80. //turn machine on
  81. halMachineOn();
  82. sleep(1);
  83. if (halIsHeating()) { //check if System starts to heat when turned on
  84. changeState(STATE_INITALHEATING);
  85. } else {
  86. changeState(STATE_IDLE);
  87. }
  88. } else {
  89. changeState(STATE_ERROR);
  90. }
  91. break;
  92. }
  93. break;
  94. /*
  95. *
  96. */
  97. case STATE_INITALHEATING:
  98. //beginHeating = clock();
  99. if (SigValueEmpty())
  100. pause();
  101. switch (getSigValue()) {
  102. case SigInt1RlsLong:
  103. //Turn machine off again
  104. halMachineOff();
  105. changeState(STATE_OFF);
  106. break;
  107. case SigPressOpn:
  108. //Inital heating finished
  109. changeState(STATE_IDLE);
  110. break;
  111. }
  112. /* FIXME See notes below
  113. endHeating = clock();
  114. heatingTime = double(endHeating - beginHeating) / CLOCKS_PER_SEC;
  115. coffeeIncreaseHeatingTime((uint64_t) heatingTime);*/
  116. break;
  117. /*
  118. *
  119. */
  120. case STATE_HEATING:
  121. //beginHeating = clock();
  122. if (SigValueEmpty())
  123. pause();
  124. switch (getSigValue()) {
  125. case SigInt1RlsLong:
  126. //Turn machine off again
  127. halMachineOff();
  128. changeState(STATE_OFF);
  129. break;
  130. case SigPressOpn:
  131. changeState(STATE_IDLE);
  132. break;
  133. case SigInt0Psh:
  134. //start to brew a delicious coffee
  135. changeState(STATE_BREW);
  136. break;
  137. case SigBrewOn:
  138. //someone brews manually
  139. changeState(STATE_BREWMANUAL);
  140. break;
  141. }
  142. /*
  143. * @TODO FIXME:
  144. * The time measurement makes no sense, since the FSM loops and there are a lot of interrupts that
  145. * should be considered (like buttons or proximity sensor)
  146. * Suggestion: capture time snapshot once at change to heating. If a change to another state occurs,
  147. * capture again (like before, this capture is interrupted quite often)
  148. *
  149. * Another con: coffeeIncreaseHeatingTime() takes long for accessing the database, maybe store
  150. * values at termination or periodically
  151. endHeating = clock();
  152. heatingTime = double(endHeating - beginHeating) / CLOCKS_PER_SEC;
  153. coffeeIncreaseHeatingTime((uint64_t) heatingTime);*/
  154. break;
  155. /*
  156. *
  157. */
  158. case STATE_IDLE:
  159. if (SigValueEmpty())
  160. pause();
  161. switch (getSigValue()) {
  162. case SigInt1RlsLong:
  163. //Turn machine off again
  164. halMachineOff();
  165. changeState(STATE_OFF);
  166. break;
  167. case SigPressCls:
  168. changeState(STATE_HEATING);
  169. break;
  170. case SigInt0Psh:
  171. //start to brew a delicious coffee
  172. changeState(STATE_BREW);
  173. break;
  174. case SigBrewOn:
  175. //someone brews manually
  176. changeState(STATE_BREWMANUAL);
  177. break;
  178. }
  179. break;
  180. /*
  181. *
  182. */
  183. case STATE_BREW:
  184. coffeeBrew();
  185. logger(V_BREW, "Finishing brewing\n");
  186. changeState(STATE_IDLE);
  187. break;
  188. /*
  189. *
  190. */
  191. case STATE_BREWMANUAL:
  192. if (SigValueEmpty())
  193. pause();
  194. break;
  195. /*
  196. *
  197. */
  198. case STATE_CLEANING:
  199. if (SigValueEmpty())
  200. pause();
  201. break;
  202. /*
  203. *
  204. */
  205. case STATE_ERROR:
  206. if (SigValueEmpty())
  207. pause();
  208. break;
  209. }
  210. }
  211. pthread_exit(EXIT_SUCCESS);
  212. }
  213. /**
  214. * Handler for the Signal send to this thread
  215. * It saves the type of signal received and tracks the time between a push and release event of up to 4 signals
  216. * The time is stored in the HalEvent variable when a release event is received
  217. * @param signum
  218. * @param siginfo
  219. * @param context
  220. */
  221. void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
  222. sigval_t sigVal = (siginfo->si_value);
  223. sigValue = sigVal.sival_int;
  224. logger(V_BREW, "CoffeeHandler called with %d\n", sigValue);
  225. }
  226. /**
  227. * returns the Signal value from the last received Signal and clears the variable
  228. * @return value sent with the last signal
  229. */
  230. int getSigValue(void) {
  231. int tmp = sigValue;
  232. sigValue = 0;
  233. return tmp;
  234. }
  235. bool SigValueEmpty(void) {
  236. if (sigValue == 0)
  237. return true;
  238. else
  239. return false;
  240. }
  241. /**
  242. * Changes the state of the machine to newState
  243. * prints the change to the logger
  244. * @param newState
  245. */
  246. void changeState(coffee_status_t newState) {
  247. logger(V_BREW, "Changing state to %d\n", newState);
  248. state = newState;
  249. event_trigger("statechange", &state, sizeof(state));
  250. }
  251. /**
  252. * Returns the current state of the FSM
  253. */
  254. coffee_status_t getState(void) {
  255. return state;
  256. }
  257. /**
  258. * Counter for the brew time
  259. * refresh every 200ms
  260. */
  261. void brewTimeHandler(void) {
  262. brewTime += 200;
  263. }
  264. /**
  265. * handles program termination
  266. */
  267. void coffeeTerminate(void) {
  268. logger_error("Coffee thread terminated");
  269. //stop brewing
  270. halRelaisOff(RELAIS_PUMP);
  271. brewTimer.stop();
  272. }
  273. /**
  274. * Brewing process
  275. */
  276. void coffeeBrew(void) {
  277. coffeeIncreaseBrewCounter();
  278. /*
  279. * Preinfusion
  280. */
  281. logger(V_BREW, "Starting preinfusion...\n");
  282. halResetFlow();
  283. halRelaisOn(RELAIS_PUMP);
  284. brewTime = 0;
  285. brewTimer.start();
  286. while (halGetFlow() < AMOUNT_PREINFUSION) {
  287. usleep(50000);
  288. if (getSigValue() == SigInt1Psh)
  289. return;
  290. }
  291. brewTimer.stop();
  292. brewTime = 0;
  293. halRelaisOff(RELAIS_PUMP);
  294. /*
  295. * Wait for coffee to soak in infused water
  296. */
  297. brewTimer.start();
  298. while (brewTime < TIME_SOAK) {
  299. usleep(100000);
  300. if (getSigValue() == SigInt1Psh)
  301. return;
  302. }
  303. brewTimer.stop();
  304. brewTime = 0;
  305. halResetFlow();
  306. /*
  307. * Brewing the actual espresso
  308. */
  309. logger(V_BREW, "Starting infusion...\n");
  310. halRelaisOn(RELAIS_PUMP);
  311. brewTimer.start();
  312. while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
  313. usleep(100000);
  314. if (getSigValue() == SigInt1Psh)
  315. return;
  316. }
  317. halRelaisOff(RELAIS_PUMP);
  318. halResetFlow();
  319. brewTimer.stop();
  320. brewTime = 0;
  321. changeState(STATE_IDLE);
  322. return;
  323. }
  324. /**
  325. *
  326. */
  327. void coffeeIncreaseBrewCounter(void) {
  328. uint64_t brewcounter = sqlGetConf(CFGbrewcounter);
  329. if (sqlSetConf(CFGbrewcounter, ++brewcounter)) {
  330. logger_error("Couldn't write brewcounter to database");
  331. }
  332. }
  333. /**
  334. *
  335. */
  336. void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
  337. uint64_t totalHeatingTime = sqlGetConf(CFGHeatingTime);
  338. if (sqlSetConf(CFGHeatingTime, (totalHeatingTime + heatingTime))) {
  339. logger_error("Couldn't write heating time to database");
  340. }
  341. }