Explorar o código

removed pumptime in hal, added tracking of flowtime in hal, changed trigger of brewManual signal, removed tracking of brewtime in coffee.cpp

Sebastian Vendt %!s(int64=5) %!d(string=hai) anos
pai
achega
d666dcc3a4
Modificáronse 6 ficheiros con 70 adicións e 110 borrados
  1. 1 1
      CoffeeCode/buildno
  2. 17 64
      CoffeeCode/coffee.cpp
  3. 2 2
      CoffeeCode/coffee.h
  4. 2 2
      CoffeeCode/display2.cpp
  5. 44 39
      CoffeeCode/hal.cpp
  6. 4 2
      CoffeeCode/hal.h

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-343
+346

+ 17 - 64
CoffeeCode/coffee.cpp

@@ -29,13 +29,12 @@ coffee_mode_t mode;
 
 int sigValue;
 int menuTimeout;
-uint16_t brewTime; //Brew time in ms
-uint16_t lastBrewTime;
-timer brewTimer(&brewTimeHandler);
-timer menuTimer(&menuTimeHandler);
+timer menuTimer(&menuTimerHandler);
+timer brewTimer(&brewTimerHandler);
 
 uint64_t totalHeatingTime; //local copies of the corresponding database entries
 uint16_t brewCounter;
+uint16_t brewTime;
 uint16_t currentCleanCycle;
 bool initalHeating;
 bool descaling; //flag to indicate descaling and cleaning
@@ -64,10 +63,6 @@ void *coffeeThread(void *threadid) {
 	action.sa_sigaction = coffeeHandler;
 	sigaction(SIGUSR2, &action, NULL);
 
-	brewTimer.setDivider(4);
-	brewTimer.stop();
-	brewTime = 0;
-
 	currentCleanCycle = 0;
 
 	menuTimer.setDivider(4);
@@ -572,7 +567,6 @@ void *coffeeThread(void *threadid) {
 				pause();
 			switch (getSigValue(MODE_STATE)) {
 				case SigBrewOff:
-					coffeeManualBrewStop();
 					if (halIsHeating()) {
 						changeState(STATE_HEATING);
 					} else {
@@ -767,17 +761,6 @@ coffee_status_t getState(void) {
 	return state;
 }
 
-uint16_t getBrewTime(void) {
-	return brewTime;
-}
-
-/**
- * Returns the last elapsed brew time in ms
- */
-uint16_t getLastBrewTime(void) {
-	return lastBrewTime;
-}
-
 /**
  * Returns the local up-to-date brewcounter
  */
@@ -806,19 +789,12 @@ time_t * getDescTimestamp (void) {
 	return &descRawTimestamp;
 }
 
-/**
- * Counter for the brew time
- * refresh every 200ms
- */
-void brewTimeHandler(void) {
-	brewTime += 200;
-}
 
 /**
  * Counter for the menu timeout
  * When no input is coming from the user the machine leaves the menu automatically after MENUTIMEOUT seconds
  */
-void menuTimeHandler(void){
+void menuTimerHandler(void){
 	menuTimeout += 200;
 	if((menuTimeout/1000) >= MENUTIMEOUT)  {
 		leaveMenu();
@@ -832,7 +808,6 @@ void coffeeTerminate(event_t *event) {
 	logger(V_BREW, "Coffee.cpp: Terminating\n");
 	//stop brewing
 	halRelaisOff(RELAIS_PUMP);
-	brewTimer.stop();
 	writeBackCache();
 }
 
@@ -914,17 +889,6 @@ void coffeeClean(void) {
 void coffeeManualBrewStart (void) {
 	logger(V_BREW, "Starting manual brewing...\n");
 	coffeeIncreaseBrewCounter();
-	brewTime = 0;
-	brewTimer.start();
-}
-
-/*
- *
- */
-void coffeeManualBrewStop (void) {
-	lastBrewTime = brewTime;
-	brewTime = 0;
-	brewTimer.stop();
 }
 
 /**
@@ -932,24 +896,20 @@ void coffeeManualBrewStop (void) {
  */
 void coffeeBrew(void) {
 	coffeeIncreaseBrewCounter();
-	halIncreaseLogCycleCounter();
 	/*
 	 * Preinfusion
 	 */
 	logger(V_BREW, "Starting preinfusion...\n");
-	halResetFlow();
 	halPumpOn();
-	brewTime = 0;
-	brewTimer.start();
-	while (halGetFlow() < AMOUNT_PREINFUSION && brewTime < TIME_PREINFUSION) {
+	while (halGetFlow() < AMOUNT_PREINFUSION && halGetFlowTime() < TIME_PREINFUSION) {
 		//TODO don't use coffeeNap here since we don't want to resume to sleep after a signal got caught...
 		coffeeNap(0, 50000000);
 		if (getSigValue(MODE_STATE) == SigInt0Rls){
-			stopBrewing();
+			halPumpOff();
 			return;
 		}
 	}
-	stopBrewing();
+	halPumpOff();
 
 	/*
 	 * Wait for coffee to soak in infused water
@@ -958,40 +918,33 @@ void coffeeBrew(void) {
 	while (brewTime < TIME_SOAK) {
 		coffeeNap(1, 100000000);
 		if (getSigValue(MODE_STATE) == SigInt0Rls) {
-			stopBrewing();
+			brewTimer.stop();
+			brewTime = 0;
 			return;
 		}
 	}
-	stopBrewing();
+	brewTimer.stop();
+	brewTime = 0;
 
 	/*
 	 * Brewing the actual espresso
 	 */
 	logger(V_BREW, "Starting infusion...\n");
 	halPumpOn();
-	brewTimer.start();
 	while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
 		coffeeNap(1, 100000000);
 		if (getSigValue(MODE_STATE) == SigInt0Rls){
-			stopBrewing();
-			break;
+			halPumpOff();
+			return;
 		}
 	}
-	stopBrewing();
-	halIncreaseLogCycleCounter();
+	halPumpOff();
 	return;
 }
 
-/*
- * Wrapper function for the end of a brewing process
- * this function stops the pump, brewtimer and resets the flow and brew time to zero
- */
-void stopBrewing() {
-	halPumpOff();
-	brewTimer.stop();
-	lastBrewTime = brewTime;
-	brewTime = 0;
-	halResetFlow();
+
+void brewTimerHandler(){
+	brewTime += 200;
 }
 
 /**

+ 2 - 2
CoffeeCode/coffee.h

@@ -75,8 +75,8 @@ uint16_t getBrewCounter(void);
 uint64_t getTotalHeatingTime(void);
 uint16_t getDescBrewCounter (void);
 time_t * getDescTimestamp (void);
-void brewTimeHandler(void);
-void menuTimeHandler(void);
+void brewTimerHandler(void);
+void menuTimerHandler(void);
 void writeBackCache(void);
 void coffeeTerminate(event_t *event);
 void coffeeNap (uint64_t sec, uint64_t nanosec);

+ 2 - 2
CoffeeCode/display2.cpp

@@ -133,7 +133,7 @@ void displayPrintPostBrew(int line){
 	char buffer[17];
 	if (line > DISPLAY_ROWS) line = 0;
 
-	float brewTime = (float)getLastBrewTime();
+	float brewTime = (float)halGetLastFlowTime();
 
 	sprintf(buffer, "%.1f ml / %.1f s", halGetLastFlow(), brewTime / 1000);
 	displayPrintLn(line, buffer, true);
@@ -143,7 +143,7 @@ void displayPrintBrewManual(int line) {
 	char buffer[17];
 	if (line > DISPLAY_ROWS) line = 0;
 
-	float brewTime = (float)getBrewTime();
+	float brewTime = (float)halGetFlowTime();
 
 	sprintf(buffer, "%.1f ml / %.1f s", halGetFlow(), brewTime / 1000);
 	displayPrintLn(line, buffer, true);

+ 44 - 39
CoffeeCode/hal.cpp

@@ -66,14 +66,15 @@ timer Int0Timer(&halInt0TimerHandler);
 timer Int1Timer(&halInt1TimerHandler);
 timer idleTimer(&halIdleTimerHandler);
 timer flowResetTimer (&flowResetTimerHandler);
+timer flowTimer(&halFlowTimerHandler);
 
 time_t heatingCycle[] = {0, 0};
 timespec flowTimestep[] = {{0,0},{0,0}};
 uint8_t flowIndex = 0;
 int16_t tickCounter = 0;
-
-
-timespec pumpCycle[] = {{0,0},{0,0}};
+uint16_t flowtime = 0;
+uint16_t lastFlowTime = 0;
+bool brewSigFired = false;
 
 //delay of the debounce in milliseconds
 #define DELAY_DEBOUNCE	50
@@ -126,6 +127,8 @@ void halInit(void) {
 
 	flowResetTimer.setDivider(20); //1000ms
 
+	flowTimer.setDivider(4); // 100ms
+
 	flagIgnoreRlsInt0 = false;
 	flagIgnoreRlsInt1 = false;
 
@@ -263,10 +266,6 @@ void halIntRotary(void) {
 }
 
 
-
-
-
-
 /**
  * Interrupt routine for Int0 (Top button)
  */
@@ -356,7 +355,7 @@ void halInt1TimerHandler(void) {
 /**
  * Timer handler to auto-reset the flow counter.
  * The timer is started when the flow interrupt is triggered.
- * It compares the last value (flowResetValue) with the current flow value (flowcnt)
+ * It compares the last value (flowResetValue) with the current flow value
  * If they match (e.g no flow within 1000ms) the flow counter is reseted.
  * Setting the last value to -1 ensures that when the timer gets called immediately after it is started the comparison will fail.
  * This implementation is read-only and so it doesn't need semaphores.
@@ -369,6 +368,13 @@ void flowResetTimerHandler() {
 	flowResetValue = flowcnt;
 }
 
+/**
+ *
+ */
+void halFlowTimerHandler() {
+	flowtime += 200;
+}
+
 /**
  * Interrupt routine for the flow sensor
  * It counts the edges and stores the value in flowcnt
@@ -376,15 +382,17 @@ void flowResetTimerHandler() {
 void halIntFlow(void) {
 	//halRelaisOff(RELAIS_POWER);
 	if(!flowResetTimer.isActive()) flowResetTimer.start();
-	logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
 	flowcnt++;
+	logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
 
-	if(flowcnt >= BREW_MANUAL_TRIGGER && !brewmanual) { //send signal once
+	//tracking of flowtime
+	if(!flowTimer.isActive()) flowTimer.start();
+
+	if (halGetFlow() >= BREW_MANUAL_TRIGGER && !brewSigFired) {
 		halSendSignal(SigBrewOn);
-		brewmanual = true;
+		brewSigFired = true;
 	}
 
-
 	//subroutine to log the flow to the database
 	/*timespec deltaT;
 	clock_gettime(CLOCK_REALTIME, &flowTimestep[flowIndex]);
@@ -457,24 +465,44 @@ float halGetFlow(void) {
 	return flowcnt * FLOW_ML_PULSE;
 }
 
+/**
+ * Returns the total flow time in ms
+ */
+uint16_t halGetFlowTime(void){
+	return flowtime;
+}
+
 /*
  * Returns the last total flow through the sensor in ml after reset
  */
 float halGetLastFlow(void) {
 	return lastFlowcnt * FLOW_ML_PULSE;
 }
+
 /**
- * Resets the Flow counter
+ * Returns the total flow time in ms before the last reset.
+ */
+uint16_t halGetLastFlowTime(void){
+	return lastFlowTime;
+}
+/**
+ * Resets the Flow counter. Do not call this function manually. It will be automatically triggered
+ * from the flow reset timer routine!
  */
 void halResetFlow(void) {
 	logger(V_HAL, "Flow counter reset, amount so far: %.2f ml\n", halGetFlow());
 	lastFlowcnt = flowcnt;
 	flowcnt = 0;
 
+	lastFlowTime = flowtime;
+	flowtime = 0;
+
 	flowResetTimer.stop();
+	flowTimer.stop();
+
 	flowResetValue = -1;
-	if(brewmanual) {
-		brewmanual = false;
+	if(brewSigFired) {
+		brewSigFired = false;
 		halSendSignal(SigBrewOff);
 	}
 }
@@ -619,19 +647,15 @@ void halLeaveIdle(void){
 }
 
 /**
- * Wrapper function to turn the pump on
- * and to measure how long the pump is running
- * @param cycle the number of the sweep in the database
+ * Wrapper function to turn the pump on.
  */
 void halPumpOn(){
 	halRelaisOn(RELAIS_PUMP);
-	clock_gettime(CLOCK_REALTIME, &pumpCycle[0]);
 }
 
 /**
  *
  */
-
 void halIncreaseLogCycleCounter(void){
 	logcycle = logcycle + 1;
 }
@@ -642,25 +666,6 @@ void halIncreaseLogCycleCounter(void){
  */
 void halPumpOff(void){
 	halRelaisOff(RELAIS_PUMP);
-	clock_gettime(CLOCK_REALTIME, &pumpCycle[1]);
-}
-
-/**
- * Function to get the elapsed time the pump is running in ms
- * when the pump is on, this function returns the time between turning the pump on and the call
- * when the pump is off, this function returns the time elapsed in the last pump cycle
- */
-double halGetPumpTime(void){
-	timespec now;
-	timespec diff = {0,0};
-	if(halGetRelaisState(RELAIS_PUMP) == HIGH){//pump is on
-		clock_gettime(CLOCK_REALTIME, &now);
-		timespec_diff(&pumpCycle[0], &now, &diff);
-	}
-	else {
-		timespec_diff(&pumpCycle[0], &pumpCycle[1], &diff);
-	}
-	return diff.tv_sec * 1000 + diff.tv_nsec/1000000;
 }
 
 /**

+ 4 - 2
CoffeeCode/hal.h

@@ -31,7 +31,7 @@
 
 
 #define TIME_BUTTONLONGPRESS	3	//Time in s until a Signal for a long pressed button is sent
-#define BREW_MANUAL_TRIGGER		25	//specifies the flow ticks after which a brew_manual signal is triggered (25 eq. approx. 11ml)
+#define BREW_MANUAL_TRIGGER		26	//specifies the ml after which a brew_manual signal is triggered
 /*
  * Explanation for the signal levels
  * SigInt_Psh 			Button is pushed
@@ -82,12 +82,15 @@ void halIntRotary(void);
 void halInt0(void);
 void halInt1(void);
 void flowResetTimerHandler(void);
+void halFlowTimerHandler(void);
 void halIntFlow(void);
 void halIntPressure(void);
 double halgetHeatingTime(void);
 void halIntProximity(void);
 float halGetFlow(void);
+uint16_t halGetFlowTime(void);
 float halGetLastFlow(void);
+uint16_t halGetLastFlowTime(void);
 void halResetFlow(void);
 bool halIsHeating(void);
 bool halProxSensorCovered(void);
@@ -104,7 +107,6 @@ void halLeaveIdle (void);
 void halPumpOn();
 void halPumpOff(void);
 void halIncreaseLogCycleCounter(void);
-double halGetPumpTime(void);
 void timespec_diff(timespec *start, timespec *stop, timespec *result);
 void halTerminate(event_t *event);
 void halWriteBackCache(void);