coffee.cpp 8.7 KB

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