hal.cpp 11 KB

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