Prechádzať zdrojové kódy

fixed the trigger of multiple signals when button was pushed, moved the heating time counter into hal, added source for communication with the max31855 temperature sensor, added todo for the events.

Sebastian 7 rokov pred
rodič
commit
fa25883f7a

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-176
+181

+ 13 - 25
CoffeeCode/coffee.cpp

@@ -28,9 +28,9 @@ coffee_status_t state;
 int sigValue;
 int brewTime; //Brew time in ms
 timer brewTimer(&brewTimeHandler);
-clock_t beginHeating;
-clock_t endHeating;
-double heatingTime;
+
+
+const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR"};
 
 /**
  * Thread for the finite state machine
@@ -52,8 +52,6 @@ void *coffeeThread(void *threadid) {
 	brewTimer.setDivider(4);
 	brewTimer.stop();
 	brewTime = 0;
-	heatingTime = 0;
-	beginHeating = endHeating = clock();
 
 	logger(V_BREW, "Determining inital state\n");
 	//determine inital state
@@ -104,40 +102,38 @@ void *coffeeThread(void *threadid) {
 			 *
 			 */
 		case STATE_INITALHEATING:
-			//beginHeating = clock();
 			if (SigValueEmpty())
 				pause();
 			switch (getSigValue()) {
 			case SigInt1RlsLong:
 				//Turn machine off again
 				halMachineOff();
+				coffeeIncreaseHeatingTime(halgetHeatingTime());
 				changeState(STATE_OFF);
 				break;
 			case SigPressOpn:
 				//Inital heating finished
+				coffeeIncreaseHeatingTime(halgetHeatingTime());
 				changeState(STATE_IDLE);
 				break;
 			}
-			/* FIXME See notes below
-			endHeating = clock();
-			heatingTime = double(endHeating - beginHeating) / CLOCKS_PER_SEC;
-			coffeeIncreaseHeatingTime((uint64_t) heatingTime);*/
 			break;
 
 			/*
 			 *
 			 */
 		case STATE_HEATING:
-			//beginHeating = clock();
 			if (SigValueEmpty())
 				pause();
 			switch (getSigValue()) {
 			case SigInt1RlsLong:
 				//Turn machine off again
 				halMachineOff();
+				coffeeIncreaseHeatingTime(halgetHeatingTime());
 				changeState(STATE_OFF);
 				break;
 			case SigPressOpn:
+				coffeeIncreaseHeatingTime(halgetHeatingTime());
 				changeState(STATE_IDLE);
 				break;
 			case SigInt0Psh:
@@ -149,20 +145,6 @@ void *coffeeThread(void *threadid) {
 				changeState(STATE_BREWMANUAL);
 				break;
 			}
-			/*
-			 * @TODO FIXME:
-			 * Put in HAL
-			 * The time measurement makes no sense, since the FSM loops and there are a lot of interrupts that
-			 * should be considered (like buttons or proximity sensor)
-			 * Suggestion: capture time snapshot once at change to heating. If a change to another state occurs,
-			 * 	capture again (like before, this capture is interrupted quite often)
-			 *
-			 * Another con: coffeeIncreaseHeatingTime() takes long for accessing the database, maybe store
-			 *  values at termination or periodically
-
-			endHeating = clock();
-			heatingTime = double(endHeating - beginHeating) / CLOCKS_PER_SEC;
-			coffeeIncreaseHeatingTime((uint64_t) heatingTime);*/
 			break;
 			/*
 			 *
@@ -357,6 +339,12 @@ void coffeeIncreaseBrewCounter(void) {
 		logger_error("Couldn't write brewcounter to database");
 	}
 }
+/**
+ *
+ */
+void coffeeIncreaseHeatingTime(clock_t begin, clock_t end){
+
+}
 
 /**
  *

+ 2 - 1
CoffeeCode/coffee.h

@@ -20,7 +20,8 @@ typedef enum {
 	STATE_CLEANING,
 	STATE_ERROR
 } coffee_status_t;
-static const char StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR"};
+
+extern const char* StateName[];
 
 #define AMOUNT_PREINFUSION	25 //Preinfusion amount in ml
 #define TIME_SOAK			5000 //Time between preinfusion and infusion in ms

+ 0 - 14
CoffeeCode/display.cpp

@@ -4,21 +4,7 @@
  *  Created on: Sep 26, 2017
  *      Author: Philipp Hinz
  */
-#include <stdlib.h>
-#include <pthread.h>
-#include <time.h>
-#include <unistd.h>
-#include <string.h>
-
-#include "global.h"
 #include "display.h"
-#include "logger.h"
-#include "database.h"
-#include "timer.h"
-#include "lcd.h"
-#include "coffee.h"
-#include "hal.h"
-#include "events.h"
 
 display_lang_t displayLang;
 timer displayTimer(displayTimerHandler);

+ 12 - 0
CoffeeCode/display.h

@@ -8,7 +8,19 @@
 #ifndef DISPLAY_H_
 #define DISPLAY_H_
 #include <string>
+#include <stdlib.h>
+#include <pthread.h>
+#include <time.h>
+#include <unistd.h>
+#include <string.h>
+#include "global.h"
+#include "logger.h"
+#include "database.h"
+#include "timer.h"
+#include "lcd.h"
 #include "coffee.h"
+#include "hal.h"
+#include "events.h"
 
 typedef enum {
 	lang_de,

+ 1 - 0
CoffeeCode/events.cpp

@@ -99,6 +99,7 @@ void addSubscriber(events_t *event, void (*handler)(event_t *)) {
  */
 
 void event_subscribe(const char *event, void (*handler)(event_t *)) {
+	//TODO change event name to a typedef enum or similar
 	events_t *newEvent = (events_t *) malloc(sizeof(events_t));
 	if (!newEvent)
 		logger_error("Not enough memory\n");

+ 45 - 22
CoffeeCode/hal.cpp

@@ -9,6 +9,7 @@
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
+#include <ctime>
 #include "hal.h"
 #include "global.h"
 #include "logger.h"
@@ -16,23 +17,19 @@
 
 volatile int flowcnt = 0;
 
-int Int0Time;
-int Int1Time;
-bool flagIgnoreRlsInt0;
-bool flagIgnoreRlsInt1;
+int Int0Time, Int1Time;
+bool flagIgnoreRlsInt0, flagIgnoreRlsInt1;
+
 timer Int0Timer(&halInt0TimerHandler);
 timer Int1Timer(&halInt1TimerHandler);
 
+clock_t heatingCycle[2] = {0, 0};
+
 /**
  * Initializes HAL
  */
 
 void halInit(void) {
-	if (optPower) {
-		halMachineOn();
-	} else {
-		halMachineOff();
-	}
 	pinMode(RELAIS_HEAT, OUTPUT);
 	pinMode(RELAIS_PUMP, OUTPUT);
 	pinMode(RELAIS_POWER, OUTPUT);
@@ -42,6 +39,12 @@ void halInit(void) {
 	pinMode(PIN_INT1, INPUT);
 	pinMode(PIN_FLOW, INPUT);
 
+	if (optPower) {
+		halMachineOn();
+	} else {
+		halMachineOff();
+	}
+
 	Int0Timer.setDivider(4); //200ms
 	Int1Timer.setDivider(4);
 	Int0Time = 0;
@@ -66,8 +69,7 @@ void halInit(void) {
 		logger_error("Unable to setup ISRPressure: %s\n", strerror(errno));
 		return;
 	}
-	if (wiringPiISR(PIN_PROXIMITY_SENSOR, INT_EDGE_BOTH, &halIntProximity)
-			< 0) {
+	if (wiringPiISR(PIN_PROXIMITY_SENSOR, INT_EDGE_BOTH, &halIntProximity) < 0) {
 		logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
 		return;
 	}
@@ -126,14 +128,17 @@ int halGetRelaisState(int relais) {
  * Interrupt routine for Int0 (Top button)
  */
 void halInt0(void) {
-	logger(V_HAL, "Int0 triggered\n");
-	if (halGetInt0()) {
+	if (halGetInt0()) { //released
+		logger(V_HAL, "Int0 released\n");
 		if (flagIgnoreRlsInt0) {
 			flagIgnoreRlsInt0 = false;
 		} else {
+			Int0Time = 0;
+			Int0Timer.stop();
 			halSendSignal(SigInt0Rls);
 		}
-	} else {
+	} else { //pressed
+		logger(V_HAL, "Int0 pushed\n");
 		halSendSignal(SigInt0Psh);
 		Int0Time = 0;
 		Int0Timer.start();
@@ -157,14 +162,17 @@ void halInt0TimerHandler(void) {
  * Interrupt routine for Int1 (Bottom button)
  */
 void halInt1(void) {
-	logger(V_HAL, "Int1 triggered\n");
 	if (halGetInt1()) {
+		logger(V_HAL, "Int1 released\n");
 		if (flagIgnoreRlsInt1) {
 			flagIgnoreRlsInt1 = false;
 		} else {
+			Int0Time = 0;
+			Int0Timer.stop();
 			halSendSignal(SigInt1Rls);
 		}
 	} else {
+		logger(V_HAL, "Int1 pushed\n");
 		halSendSignal(SigInt1Psh);
 		Int1Time = 0;
 		Int1Timer.start();
@@ -190,8 +198,7 @@ void halInt1TimerHandler(void) {
  */
 void halIntFlow(void) {
 	//halRelaisOff(RELAIS_POWER);
-	logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt,
-			halGetFlow());
+	//logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
 	if (flowcnt == 99) {
 		halRelaisOff(RELAIS_PUMP);
 	}
@@ -200,16 +207,34 @@ void halIntFlow(void) {
 
 /**
  * Interrupt routine for the pressure control
+ * It captures the time at closing point and opening point
+ * Reading heating time via the getHeatingTime function
  */
 void halIntPressure(void) {
 	logger(V_HAL, "IntPressure Control triggered\n");
 	if (halIsHeating()) {
+		heatingCycle[0] = clock();
 		halSendSignal(SigPressCls);
 	} else {
+		heatingCycle[1] = clock();
 		halSendSignal(SigPressOpn);
 	}
 }
 
+/**
+ * Function to read the heating time in sec
+ * If called during a heating process, it returns the time elapsed since the heating started
+ * If called after a heating process, it returns the total time elapes during the heating cycle
+ */
+uint64_t halgetHeatingTime(void){
+	if (halIsHeating()) {
+		return (uint64_t)(clock() - heatingCycle[0]) / CLOCKS_PER_SEC;
+	}
+	else {
+		return (uint64_t)(heatingCycle[1] - heatingCycle[0]) / CLOCKS_PER_SEC;
+	}
+}
+
 /**
  * Method to handle toggle of the proximity sensor
  */
@@ -240,7 +265,7 @@ void halResetFlow(void) {
 
 /**
  * Reads the status of the Pressure Control
- * @return 0 for closed Pressure Control(heating) and 1 for open
+ * @return 1 (true) for closed Pressure Control(heating) and 0 (false) for open
  */
 bool halIsHeating(void) {
 	if (digitalRead(PIN_PRESSURE_CTRL) == 0) {
@@ -255,13 +280,11 @@ bool halIsHeating(void) {
  * @return 1 if the proximity switch is covered and 0 if uncovered
  */
 bool halProxSensorCovered(void) {
-	//for legacy till sensor is installed
-	/*if(digitalRead(PROXIMITY_SENSOR) == 0){
+	if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
 	 return false;
 	 } else {
 	 return true;
-	 }*/
-	return true;
+	 }
 }
 
 /**

+ 1 - 0
CoffeeCode/hal.h

@@ -61,6 +61,7 @@ void halInt0(void);
 void halInt1(void);
 void halIntFlow(void);
 void halIntPressure(void);
+uint64_t halgetHeatingTime(void);
 void halIntProximity(void);
 float halGetFlow(void);
 void halResetFlow(void);

+ 7 - 0
CoffeeCode/main.cpp

@@ -26,6 +26,7 @@
 #include "display.h"
 #include "server.h"
 #include "events.h"
+#include "temperatur.h"
 
 const int buildno = (1+(int)(
 #include "buildno"
@@ -125,6 +126,7 @@ int main(int argc, char *argv[]) {
 	logger_reset();
 	initTimers();
 	displayInit();
+	temperatur_init();
 
 	// http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
 
@@ -237,6 +239,11 @@ void *mainLoop(void *threadid) {
 	sqlSetup();
 
 	// Do more stuff here
+//	while (1){
+//		sleep(4);
+//		int16_t temp = get_temperatur();
+//		logger(V_BASIC, "Temperatur: %d\n", temp);
+//	}
 
 	logger(V_BASIC, "Thread goes Sleeping..\n");
 	while (1) {

+ 41 - 0
CoffeeCode/temperatur.cpp

@@ -0,0 +1,41 @@
+/*
+ * temperatur.cpp
+ *
+ *  Created on: Feb 23, 2018
+ *      Author: sebastian
+ */
+#include <wiringPi.h>
+//#include <wiringPiSPI.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <unistd.h>
+
+#include "global.h"
+#include "logger.h"
+#include "temperatur.h"
+#include "spi.h"
+
+uint8_t rawData[4] = {0,0,0,0};
+
+void temperatur_init(){
+	pinMode(TEMP_CS, OUTPUT);
+	digitalWrite(TEMP_CS, HIGH);
+}
+
+int16_t get_temperatur(){
+	//active low
+	digitalWrite(TEMP_CS, LOW);
+	//do one read, Temperatur will only be reevaluated on CS High
+	for(int i = 0; i < 4; i++){
+		rawData[i] = spi_readwrite(0);
+	}
+	digitalWrite(TEMP_CS, HIGH);
+
+	//extracting the temperature data without the lowest two bit
+	int16_t temp = (((uint16_t)rawData[0]) << 4) | (((uint16_t)rawData[1] >> 4) & 0x0F);
+	logger(V_BASIC, "%x, %x, %x, %x\n", rawData[0], rawData[1], rawData[2], rawData[3]);
+	return temp;
+}
+

+ 17 - 0
CoffeeCode/temperatur.h

@@ -0,0 +1,17 @@
+/*
+ * temperatur.h
+ *
+ *  Created on: Feb 23, 2018
+ *      Author: sebastian
+ */
+
+#ifndef TEMPERATUR_H_
+#define TEMPERATUR_H_
+
+
+#define TEMP_CS	10 // CS for the MAX31855
+
+void temperatur_init(void);
+int16_t get_temperatur (void);
+
+#endif /* TEMPERATUR_H_ */