hal.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * hal.cpp
  3. *
  4. * Created on: Aug 3, 2016
  5. * Author: Philipp Hinz, Sebastian Vendt
  6. */
  7. #include <wiringPi.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <ctime>
  14. #include <time.h>
  15. #include <unistd.h>
  16. #include "hal.h"
  17. #include "global.h"
  18. #include "logger.h"
  19. #include "timer.h"
  20. #include "database.h"
  21. typedef struct timespec timespec;
  22. volatile int flowcnt = 0;
  23. int Int0Time, Int1Time;
  24. int idleCounter;
  25. bool idle;
  26. bool flagIgnoreRlsInt0, flagIgnoreRlsInt1;
  27. //storage for the last state of the buttons, the proximity sensor and the pressure sensor
  28. int pinState[4] = {1, 1, 1, 0};
  29. //sweep counter to log every brew
  30. timer Int0Timer(&halInt0TimerHandler);
  31. timer Int1Timer(&halInt1TimerHandler);
  32. timer idleTimer(&halIdleTimerHandler);
  33. time_t heatingCycle[] = {0, 0};
  34. timespec flowTimestep[] = {{0,0},{0,0}};
  35. uint8_t flowIndex = 0;
  36. timespec pumpCycle[] = {{0,0},{0,0}};
  37. //delay of the debounce in milliseconds
  38. #define DELAY_DEBOUNCE 50
  39. //display turn off after idle time in min
  40. //minimal time is 2min
  41. #define IDLE_TIME 10
  42. /**
  43. * Initializes HAL
  44. */
  45. void halInit(void) {
  46. pinMode(RELAIS_HEAT, OUTPUT);
  47. pinMode(RELAIS_PUMP, OUTPUT);
  48. pinMode(RELAIS_POWER, OUTPUT);
  49. pinMode(PIN_PRESSURE_CTRL, INPUT);
  50. pinMode(PIN_PROXIMITY_SENSOR, INPUT);
  51. pinMode(PIN_INT0, INPUT);
  52. pinMode(PIN_INT1, INPUT);
  53. pinMode(PIN_FLOW, INPUT);
  54. pinMode(PIN_DISP, OUTPUT);
  55. idleTimer.setDivider(1200); //1 min
  56. idleCounter = 0;
  57. idle = false;
  58. clock_gettime(CLOCK_REALTIME, &flowTimestep[0]);
  59. clock_gettime(CLOCK_REALTIME, &flowTimestep[1]);
  60. halDisplayOn();
  61. if (optPower) {
  62. halMachineOn();
  63. } else {
  64. halMachineOff();
  65. }
  66. sleep(1); //wait till the machine eventually turned on when optPower
  67. pinState[3] = halIsHeating();
  68. Int0Timer.setDivider(4); //200ms
  69. Int1Timer.setDivider(4);
  70. Int0Time = 0;
  71. Int1Time = 0;
  72. flagIgnoreRlsInt0 = false;
  73. flagIgnoreRlsInt1 = false;
  74. if (wiringPiISR(PIN_INT0, INT_EDGE_BOTH, &halInt0) < 0) {
  75. logger_error("Unable to setup ISR0: %s\n", strerror(errno));
  76. return;
  77. }
  78. if (wiringPiISR(PIN_INT1, INT_EDGE_BOTH, &halInt1) < 0) {
  79. logger_error("Unable to setup ISR1: %s\n", strerror(errno));
  80. return;
  81. }
  82. if (wiringPiISR(PIN_FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
  83. logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno));
  84. return;
  85. }
  86. if (wiringPiISR(PIN_PRESSURE_CTRL, INT_EDGE_BOTH, &halIntPressure) < 0) {
  87. logger_error("Unable to setup ISRPressure: %s\n", strerror(errno));
  88. return;
  89. }
  90. if (wiringPiISR(PIN_PROXIMITY_SENSOR, INT_EDGE_BOTH, &halIntProximity) < 0) {
  91. logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
  92. return;
  93. }
  94. // in long term a manual brew detection would be nice
  95. }
  96. /**
  97. * Switches relais on
  98. * @param relais Relais ID
  99. */
  100. void halRelaisOn(int relais) {
  101. halRelaisSet(relais, LOW);
  102. }
  103. /**
  104. * Turn the display off
  105. */
  106. void halDisplayOff(){
  107. digitalWrite(PIN_DISP, LOW);
  108. }
  109. /**
  110. * Turn the display on
  111. */
  112. void halDisplayOn(){
  113. digitalWrite(PIN_DISP, HIGH);
  114. }
  115. /**
  116. * Switches relais off
  117. * @param relais Relais ID
  118. */
  119. void halRelaisOff(int relais) {
  120. halRelaisSet(relais, HIGH);
  121. }
  122. /**
  123. * Switches relais to state
  124. * @param relais Relais ID
  125. * @param state LOW(0) or HIGH(1)
  126. */
  127. void halRelaisSet(int relais, int state) {
  128. if (state != HIGH && state != LOW)
  129. return;
  130. switch (relais) {
  131. case RELAIS_POWER:
  132. case RELAIS_HEAT:
  133. case RELAIS_PUMP:
  134. digitalWrite(relais, state);
  135. break;
  136. }
  137. }
  138. /**
  139. * Returns the state of the relais relais
  140. * Returns HIGH when Relais is ON
  141. * @param relais Relais ID
  142. */
  143. int halGetRelaisState(int relais) {
  144. switch (relais) {
  145. case RELAIS_POWER:
  146. case RELAIS_HEAT:
  147. case RELAIS_PUMP:
  148. return !digitalRead(relais);
  149. break;
  150. }
  151. return -1;
  152. }
  153. /**
  154. * Interrupt routine for Int0 (Top button)
  155. */
  156. void halInt0(void) {
  157. //wait for a debounce
  158. delay(DELAY_DEBOUNCE);
  159. if (halGetInt0() && !pinState[0]) { //released
  160. logger(V_HAL, "Int0 released\n");
  161. pinState[0] = 1;
  162. if (flagIgnoreRlsInt0) {
  163. flagIgnoreRlsInt0 = false;
  164. } else {
  165. Int0Time = 0;
  166. Int0Timer.stop();
  167. halSendSignal(SigInt0Rls);
  168. }
  169. } else if(!halGetInt0() && pinState[0]) { //pressed
  170. logger(V_HAL, "Int0 pushed\n");
  171. pinState[0] = 0;
  172. halSendSignal(SigInt0Psh);
  173. Int0Time = 0;
  174. Int0Timer.start();
  175. }
  176. }
  177. /**
  178. *
  179. */
  180. void halInt0TimerHandler(void) {
  181. Int0Time += 200;
  182. if (Int0Time >= (TIME_BUTTONLONGPRESS * 1000)) {
  183. halSendSignal(SigInt0RlsLong);
  184. flagIgnoreRlsInt0 = true;
  185. Int0Time = 0;
  186. Int0Timer.stop();
  187. }
  188. }
  189. /**
  190. *
  191. */
  192. void halIdleTimerHandler(void) {
  193. if(++idleCounter == IDLE_TIME){
  194. halEnterIdle();
  195. }
  196. }
  197. /**
  198. * Interrupt routine for Int1 (Bottom button)
  199. */
  200. void halInt1(void) {
  201. delay(DELAY_DEBOUNCE);
  202. if (halGetInt1() && !pinState[1]) {
  203. logger(V_HAL, "Int1 released\n");
  204. pinState[1] = 1;
  205. if (flagIgnoreRlsInt1) {
  206. flagIgnoreRlsInt1 = false;
  207. } else {
  208. Int1Time = 0;
  209. Int1Timer.stop();
  210. halSendSignal(SigInt1Rls);
  211. }
  212. } else if(!halGetInt1() && pinState[1]) {
  213. logger(V_HAL, "Int1 pushed\n");
  214. pinState[1] = 0;
  215. halSendSignal(SigInt1Psh);
  216. Int1Time = 0;
  217. Int1Timer.start();
  218. }
  219. }
  220. /*
  221. *
  222. */
  223. void halInt1TimerHandler(void) {
  224. Int1Time += 200;
  225. if (Int1Time >= (TIME_BUTTONLONGPRESS * 1000)) {
  226. halSendSignal(SigInt1RlsLong);
  227. flagIgnoreRlsInt1 = true;
  228. Int1Time = 0;
  229. Int1Timer.stop();
  230. }
  231. }
  232. /**
  233. * Interrupt routine for the flow sensor
  234. * It counts the edgdes and stores the value in flowcnt
  235. */
  236. void halIntFlow(void) {
  237. //halRelaisOff(RELAIS_POWER);
  238. logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
  239. flowcnt++;
  240. //subroutine to log the flow to the database
  241. timespec deltaT;
  242. clock_gettime(CLOCK_REALTIME, &flowTimestep[flowIndex]);
  243. timespec_diff(&flowTimestep[((flowIndex + 1) % 2)], &flowTimestep[flowIndex], &deltaT);
  244. if (sqlLogFlow(1, halGetFlow(), deltaT.tv_sec * 1000 + deltaT.tv_nsec/1000000)) {
  245. logger_error("hal.cpp: could not log flow to database!");
  246. return;
  247. }
  248. flowIndex = (flowIndex + 1) % 2;
  249. }
  250. /**
  251. * Interrupt routine for the pressure control
  252. * It captures the time at closing point and opening point
  253. * Reading heating time via the getHeatingTime function
  254. */
  255. void halIntPressure(void) {
  256. logger(V_HAL, "IntPressure Control triggered\n");
  257. if (halIsHeating() && !pinState[3]) {
  258. pinState[3] = 1;
  259. time(&heatingCycle[0]);
  260. halSendSignal(SigPressCls);
  261. } else if(!halIsHeating() && pinState[3]) {
  262. pinState[3] = 0;
  263. time(&heatingCycle[1]);
  264. halSendSignal(SigPressOpn);
  265. }
  266. }
  267. /**
  268. * Function to read the heating time in sec
  269. * If called during a heating process, it returns the time elapsed since the heating started
  270. * If called after a heating process, it returns the total time elapsed during the heating cycle
  271. */
  272. double halgetHeatingTime(void){
  273. //TODO check return value on negative times
  274. if (halIsHeating()) {
  275. logger(V_HAL, "Hot Heating Time: %f\n", difftime(time(NULL), heatingCycle[0]));
  276. return difftime(time(0), heatingCycle[0]);
  277. }
  278. else {
  279. logger(V_HAL, "Heating time: %f\n", difftime(heatingCycle[1], heatingCycle[0]));
  280. return difftime(heatingCycle[1], heatingCycle[0]);
  281. }
  282. }
  283. /**
  284. * Method to handle toggle of the proximity sensor
  285. */
  286. void halIntProximity(void) {
  287. delay(DELAY_DEBOUNCE);
  288. if (halProxSensorCovered() && !pinState[2]) {
  289. logger(V_HAL, "IntProximity triggered\n");
  290. pinState[2] = 1;
  291. halSendSignal(SigProxCvrd);
  292. } else if(!halProxSensorCovered() && pinState[2]){
  293. logger(V_HAL, "IntProximity triggered\n");
  294. pinState[2] = 0;
  295. halSendSignal(SigProxOpn);
  296. }
  297. }
  298. /**
  299. * Returns total flow trough sensor in ml
  300. */
  301. float halGetFlow(void) {
  302. return flowcnt * FLOW_ML_PULSE;
  303. }
  304. /**
  305. * Resets the Flow counter
  306. */
  307. void halResetFlow(void) {
  308. logger(V_HAL, "Flow counter reset, amount so far: %.2f ml\n", halGetFlow());
  309. flowcnt = 0;
  310. }
  311. /**
  312. * Reads the status of the Pressure Control
  313. * @return 1 (true) for closed Pressure Control(heating) and 0 (false) for open
  314. */
  315. bool halIsHeating(void) {
  316. if (digitalRead(PIN_PRESSURE_CTRL) == 0) {
  317. return true;
  318. } else {
  319. return false;
  320. }
  321. }
  322. /**
  323. * Returns status of the proximity switch
  324. * @return 1 if the proximity switch is covered and 0 if uncovered
  325. */
  326. bool halProxSensorCovered(void) {
  327. if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
  328. return false;
  329. } else {
  330. return true;
  331. }
  332. }
  333. /**
  334. * Returns the value of the top button Int0 (low active)
  335. * @return LOW or HIGH
  336. */
  337. int halGetInt0(void) {
  338. return digitalRead(PIN_INT0);
  339. }
  340. /**
  341. * Returns the value of the bottom button Int1 (low active)
  342. * @return LOW or HIGH
  343. */
  344. int halGetInt1(void) {
  345. return digitalRead(PIN_INT1);
  346. }
  347. /**
  348. * send Signal to coffee thread
  349. * @param val Integer value assigned to signal
  350. */
  351. void halSendSignal(HalSig val) {
  352. //catch if machine is idle and drop button event
  353. switch (val) {
  354. case SigInt0Psh:
  355. case SigInt1Psh:
  356. if (idle) {
  357. return;
  358. }
  359. break;
  360. case SigInt0Rls:
  361. case SigInt0RlsLong:
  362. case SigInt1Rls:
  363. case SigInt1RlsLong:
  364. if (idle) {
  365. halLeaveIdle();
  366. return;
  367. }
  368. break;
  369. default:
  370. break;
  371. }
  372. sigval value = { 0 };
  373. value.sival_int = (int) val;
  374. try {
  375. if (pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
  376. logger_error("hal.cpp: Failed to queue signal %d %s\n", val, strerror(errno));
  377. //No Signals reach the state machine anymore...
  378. exit(EXIT_FAILURE);
  379. }
  380. } catch (int e) {
  381. logger_error("Whoops.. %d\n", e);
  382. }
  383. }
  384. /**
  385. * Turn machine on
  386. */
  387. void halMachineOn(void) {
  388. halRelaisOn(RELAIS_HEAT);
  389. halRelaisOff(RELAIS_PUMP);
  390. halRelaisOn(RELAIS_POWER);
  391. idleTimer.stop();
  392. logger(V_HAL, "Turning machine on\n");
  393. }
  394. /**
  395. * Turn machine off
  396. */
  397. void halMachineOff(void) {
  398. halRelaisOff(RELAIS_HEAT);
  399. halRelaisOff(RELAIS_PUMP);
  400. halRelaisOff(RELAIS_POWER);
  401. idleCounter = 0;
  402. idleTimer.start();
  403. logger(V_HAL, "Turning machine off\n");
  404. }
  405. /**
  406. *
  407. */
  408. void halEnterIdle(void){
  409. logger(V_HAL, "Entering Idle Mode\n");
  410. idleTimer.stop();
  411. halDisplayOff();
  412. idle = true;
  413. }
  414. /**
  415. *
  416. */
  417. void halLeaveIdle(void){
  418. idleCounter = 0;
  419. logger(V_HAL, "Leaving Idle Mode\n");
  420. halDisplayOn();
  421. idleTimer.start();
  422. idle = false;
  423. }
  424. /**
  425. * Wrapper function to turn the pump on
  426. * and to measure how long the pump is running
  427. */
  428. void halPumpOn(void){
  429. halRelaisOn(RELAIS_PUMP);
  430. clock_gettime(CLOCK_REALTIME, &pumpCycle[0]);
  431. }
  432. /**
  433. * Wrapper function to turn the pump off
  434. * and to measure how long the pump is running
  435. */
  436. void halPumpOff(void){
  437. halRelaisOff(RELAIS_PUMP);
  438. clock_gettime(CLOCK_REALTIME, &pumpCycle[1]);
  439. }
  440. /**
  441. * Function to get the elapsed time the pump is running in ms
  442. * when the pump is on, this function returns the time between turning the pump on and the call
  443. * when the pump is off, this function returns the time elapsed in the last pump cycle
  444. */
  445. double halGetPumpTime(void){
  446. timespec now;
  447. timespec diff = {0,0};
  448. if(halGetRelaisState(RELAIS_PUMP) == HIGH){//pump is on
  449. clock_gettime(CLOCK_REALTIME, &now);
  450. timespec_diff(&pumpCycle[0], &now, &diff);
  451. }
  452. else {
  453. timespec_diff(&pumpCycle[0], &pumpCycle[1], &diff);
  454. }
  455. return diff.tv_sec * 1000 + diff.tv_nsec/1000000;
  456. }
  457. /**
  458. * Function to calculate the difference between two timespecs
  459. */
  460. void timespec_diff(timespec *start, timespec *stop, timespec *result) {
  461. long int secDiff = stop->tv_sec - start->tv_sec;
  462. long int nsecDiff = stop->tv_nsec - start->tv_nsec;
  463. if (secDiff > 0) {
  464. if (nsecDiff >= 0) {
  465. result->tv_sec = secDiff;
  466. result->tv_nsec = nsecDiff;
  467. } else if (nsecDiff < 0) {
  468. result->tv_sec = --secDiff;
  469. result->tv_nsec = 1000000000 + nsecDiff;
  470. }
  471. } else if (secDiff < 0) {
  472. if (nsecDiff >= 0) {
  473. result->tv_sec = ++secDiff;
  474. result->tv_nsec = -(1000000000 - nsecDiff);
  475. } else if (nsecDiff < 0) {
  476. result->tv_sec = secDiff;
  477. result->tv_nsec = nsecDiff;
  478. }
  479. } else if (secDiff == 0){
  480. result->tv_sec = secDiff;
  481. result->tv_nsec = nsecDiff;
  482. }
  483. return;
  484. }