coffee.cpp 7.3 KB

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