coffee.cpp 7.2 KB

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