hal.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. flowcnt++;
  232. }
  233. /**
  234. * Interrupt routine for the pressure control
  235. * It captures the time at closing point and opening point
  236. * Reading heating time via the getHeatingTime function
  237. */
  238. void halIntPressure(void) {
  239. logger(V_HAL, "IntPressure Control triggered\n");
  240. if (halIsHeating() && !pinState[3]) {
  241. pinState[3] = 1;
  242. time(&heatingCycle[0]);
  243. halSendSignal(SigPressCls);
  244. } else if(!halIsHeating() && pinState[3]) {
  245. pinState[3] = 0;
  246. time(&heatingCycle[1]);
  247. halSendSignal(SigPressOpn);
  248. }
  249. }
  250. /**
  251. * Function to read the heating time in sec
  252. * If called during a heating process, it returns the time elapsed since the heating started
  253. * If called after a heating process, it returns the total time elapsed during the heating cycle
  254. */
  255. double halgetHeatingTime(void){
  256. //TODO check return value on negative times
  257. if (halIsHeating()) {
  258. logger(V_HAL, "Hot Heating Time: %f\n", difftime(time(NULL), heatingCycle[0]));
  259. return difftime(time(0), heatingCycle[0]);
  260. }
  261. else {
  262. logger(V_HAL, "Heating time: %f\n", difftime(heatingCycle[1], heatingCycle[0]));
  263. return difftime(heatingCycle[1], heatingCycle[0]);
  264. }
  265. }
  266. /**
  267. * Method to handle toggle of the proximity sensor
  268. */
  269. void halIntProximity(void) {
  270. delay(DELAY_DEBOUNCE);
  271. if (halProxSensorCovered() && !pinState[2]) {
  272. logger(V_HAL, "IntProximity triggered\n");
  273. pinState[2] = 1;
  274. halSendSignal(SigProxCvrd);
  275. } else if(!halProxSensorCovered() && pinState[2]){
  276. logger(V_HAL, "IntProximity triggered\n");
  277. pinState[2] = 0;
  278. halSendSignal(SigProxOpn);
  279. }
  280. }
  281. /**
  282. * Returns total flow trough sensor in ml
  283. */
  284. float halGetFlow(void) {
  285. return flowcnt * FLOW_ML_PULSE;
  286. }
  287. /**
  288. * Resets the Flow counter
  289. */
  290. void halResetFlow(void) {
  291. logger(V_HAL, "Flow counter reset, amount so far: %.2f ml\n", halGetFlow());
  292. flowcnt = 0;
  293. }
  294. /**
  295. * Reads the status of the Pressure Control
  296. * @return 1 (true) for closed Pressure Control(heating) and 0 (false) for open
  297. */
  298. bool halIsHeating(void) {
  299. if (digitalRead(PIN_PRESSURE_CTRL) == 0) {
  300. return true;
  301. } else {
  302. return false;
  303. }
  304. }
  305. /**
  306. * Returns status of the proximity switch
  307. * @return 1 if the proximity switch is covered and 0 if uncovered
  308. */
  309. bool halProxSensorCovered(void) {
  310. if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
  311. return false;
  312. } else {
  313. return true;
  314. }
  315. }
  316. /**
  317. * Returns the value of the top button Int0 (low active)
  318. * @return LOW or HIGH
  319. */
  320. int halGetInt0(void) {
  321. return digitalRead(PIN_INT0);
  322. }
  323. /**
  324. * Returns the value of the bottom button Int1 (low active)
  325. * @return LOW or HIGH
  326. */
  327. int halGetInt1(void) {
  328. return digitalRead(PIN_INT1);
  329. }
  330. /**
  331. * send Signal to coffee thread
  332. * @param val Integer value assigned to signal
  333. */
  334. void halSendSignal(HalSig val) {
  335. //catch if machine is idle and drop button event
  336. switch (val) {
  337. case SigInt0Psh:
  338. case SigInt1Psh:
  339. if (idle) {
  340. return;
  341. }
  342. break;
  343. case SigInt0Rls:
  344. case SigInt0RlsLong:
  345. case SigInt1Rls:
  346. case SigInt1RlsLong:
  347. if (idle) {
  348. halLeaveIdle();
  349. return;
  350. }
  351. break;
  352. default:
  353. break;
  354. }
  355. sigval value = { 0 };
  356. value.sival_int = (int) val;
  357. try {
  358. if (pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
  359. logger_error("hal.cpp: Failed to queue signal %d %s\n", val, strerror(errno));
  360. //No Signals reach the state machine anymore...
  361. exit(EXIT_FAILURE);
  362. }
  363. } catch (int e) {
  364. logger_error("Whoops.. %d\n", e);
  365. }
  366. }
  367. /**
  368. * Turn machine on
  369. */
  370. void halMachineOn(void) {
  371. halRelaisOn(RELAIS_HEAT);
  372. halRelaisOff(RELAIS_PUMP);
  373. halRelaisOn(RELAIS_POWER);
  374. idleTimer.stop();
  375. logger(V_HAL, "Turning machine on\n");
  376. }
  377. /**
  378. * Turn machine off
  379. */
  380. void halMachineOff(void) {
  381. halRelaisOff(RELAIS_HEAT);
  382. halRelaisOff(RELAIS_PUMP);
  383. halRelaisOff(RELAIS_POWER);
  384. idleCounter = 0;
  385. idleTimer.start();
  386. logger(V_HAL, "Turning machine off\n");
  387. }
  388. /**
  389. *
  390. */
  391. void halEnterIdle(void){
  392. logger(V_HAL, "Entering Idle Mode\n");
  393. idleTimer.stop();
  394. halDisplayOff();
  395. idle = true;
  396. }
  397. /**
  398. *
  399. */
  400. void halLeaveIdle(void){
  401. idleCounter = 0;
  402. logger(V_HAL, "Leaving Idle Mode\n");
  403. halDisplayOn();
  404. idleTimer.start();
  405. idle = false;
  406. }
  407. /**
  408. * Wrapper function to turn the pump on
  409. * and is used for time measurements on how long the pump is running
  410. */
  411. void halPumpOn(void){
  412. halRelaisOn(RELAIS_PUMP);
  413. clock_gettime(CLOCK_REALTIME, &pumpCycle[0]);
  414. }
  415. /**
  416. * Wrapper function to turn the pump off
  417. * and is used for time measurements on how long the pump is running
  418. */
  419. void halPumpOff(void){
  420. halRelaisOff(RELAIS_PUMP);
  421. clock_gettime(CLOCK_REALTIME, &pumpCycle[1]);
  422. }
  423. /**
  424. * Function to get the elapsed time the pump is running in ms
  425. * when the pump is on, this function returns the time between turning the pump on and the call
  426. * when the pump is off, this function returns the last
  427. */
  428. double halGetPumpTime(void){
  429. timespec now;
  430. timespec diff = {0,0};
  431. if(halGetRelaisState(RELAIS_PUMP) == HIGH){//pump is on
  432. clock_gettime(CLOCK_REALTIME, &now);
  433. timespec_diff(&pumpCycle[0], &now, &diff);
  434. }
  435. else {
  436. timespec_diff(&pumpCycle[0], &pumpCycle[1], &diff);
  437. }
  438. return diff.tv_sec * 1000 + diff.tv_nsec/1000000;
  439. }
  440. /**
  441. * Function to calculate the difference between two timespecs
  442. */
  443. void timespec_diff(timespec *start, timespec *stop, timespec *result) {
  444. long int secDiff = stop->tv_sec - start->tv_sec;
  445. long int nsecDiff = stop->tv_nsec - start->tv_nsec;
  446. if (secDiff > 0) {
  447. if (nsecDiff >= 0) {
  448. result->tv_sec = secDiff;
  449. result->tv_nsec = nsecDiff;
  450. } else if (nsecDiff < 0) {
  451. result->tv_sec = --secDiff;
  452. result->tv_nsec = 1000000000 + nsecDiff;
  453. }
  454. } else if (secDiff < 0) {
  455. if (nsecDiff >= 0) {
  456. result->tv_sec = ++secDiff;
  457. result->tv_nsec = -(1000000000 - nsecDiff);
  458. } else if (nsecDiff < 0) {
  459. result->tv_sec = secDiff;
  460. result->tv_nsec = nsecDiff;
  461. }
  462. } else if (secDiff == 0){
  463. result->tv_sec = secDiff;
  464. result->tv_nsec = nsecDiff;
  465. }
  466. return;
  467. }