ソースを参照

added code to log the flow with timesteps into the database

Sebastian 6 年 前
コミット
94c81cbb91

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-263
+273

+ 5 - 5
CoffeeCode/coffee.cpp

@@ -766,9 +766,9 @@ void writeBackCache(void) {
 void coffeeClean(void) {
 	logger(V_BREW, "Cleaning...\n");
 	for (int i = 0; i < 20; i++) {
-		halRelaisOn(RELAIS_PUMP);
+		halPumpOn();
 		coffeeNap(3,0);
-		halRelaisOff(RELAIS_PUMP);
+		halPumpOff();
 		coffeeNap(15,0);
 	}
 	updateDescaling();
@@ -786,7 +786,7 @@ void coffeeBrew(void) {
 	 */
 	logger(V_BREW, "Starting preinfusion...\n");
 	halResetFlow();
-	halRelaisOn(RELAIS_PUMP);
+	halPumpOn();
 	brewTime = 0;
 	brewTimer.start();
 	while (halGetFlow() < AMOUNT_PREINFUSION && brewTime < TIME_PREINFUSION) {
@@ -815,7 +815,7 @@ void coffeeBrew(void) {
 	 * Brewing the actual espresso
 	 */
 	logger(V_BREW, "Starting infusion...\n");
-	halRelaisOn(RELAIS_PUMP);
+	halPumpOn();
 	brewTimer.start();
 	while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
 		coffeeNap(1, 100000000);
@@ -834,7 +834,7 @@ void coffeeBrew(void) {
  * this function stops the pump, brewtimer and resets the flow and brew time to zero
  */
 void stopBrewing() {
-	halRelaisOff(RELAIS_PUMP);
+	halPumpOff();
 	brewTimer.stop();
 	brewTime = 0;
 	halResetFlow();

+ 15 - 0
CoffeeCode/database.cpp

@@ -203,6 +203,21 @@ int sqlSetConf(config_key_t id, uint64_t value) {
 	return EXIT_SUCCESS;
 }
 
+/**
+ * Logs the flow curve
+ * @param sweep identification for the curve
+ * @param flow the cumulative measured amount
+ * @param deltaT time between the last flow interrupt and the current one
+ */
+
+int sqlLogFlow(uint64_t sweep, uint64_t flow, uint64_t deltaT) {
+	int rc = sqlExecute(string_sprintf("REPLACE INTO flowLog (sweep, flow, timestamp) "
+			"VALUES (%lld, %lld, %lld);", sweep, flow, deltaT));
+	if (rc != SQLITE_OK)
+		return EXIT_FAILURE;
+	return EXIT_SUCCESS;
+}
+
 /**
  * Converts the GUID from int to char array
  * @param *guid Pointer to the uint8_t GUID

+ 3 - 1
CoffeeCode/database.h

@@ -23,7 +23,8 @@ typedef enum {
 	CFGHeatingTime = 2,
 	CFGdisplaylang = 3,
 	CFGDescTimestamp = 4,
-	CFGDescBrewCount = 5
+	CFGDescBrewCount = 5,
+	CFGSweepCounter = 6
 } config_key_t;
 
 int sqlOpen();
@@ -38,5 +39,6 @@ void sqltest();
 std::string string_sprintf(const char* format, ...);
 uint64_t sqlGetConf(config_key_t id);
 int sqlSetConf(config_key_t id, uint64_t value);
+int sqlLogFlow(uint64_t sweep, uint64_t flow, uint64_t deltaT);
 
 #endif /* DATABASE_H_ */

+ 3 - 7
CoffeeCode/deploy

@@ -1,13 +1,9 @@
 #!/bin/bash
-echo "Deploying source and executable from $2 ..."
-#echo "Deploying source of $1"
-#rsync -av --delete "$2/" "/mnt/flippi/Pi code/"
+#echo "Deploying source and executable from $2 ..."
 echo "Deploying executable $1"
-rsync -av --delete "$2/Debug/$1" "/mnt/coffeepi/Debug/"
-#echo "Deploying on Janztecpi.."
-#rsync -av --delete "$2/Debug/$1" "/mnt/janztecpi/Pi code/Debug/"
-COUNT=$(cat "$2/buildno")
+rsync -av --delete -e ssh $2/Debug/$1 pi@192.168.0.7:/opt/coffeepi/Debug
 
+COUNT=$(cat "$2/buildno")
 if [ "$1" == "coffeecode" ]; then
 echo "Increasing build No.."
 echo $(($COUNT+1)) > "$2/buildno"

+ 17 - 0
CoffeeCode/deploy.old

@@ -0,0 +1,17 @@
+#!/bin/bash
+echo "Deploying source and executable from $2 ..."
+#echo "Deploying source of $1"
+#rsync -av --delete "$2/" "/mnt/flippi/Pi code/"
+echo "Deploying executable $1"
+rsync -av --delete "$2/Debug/$1" "/mnt/coffeepi/Debug/"
+#echo "Deploying on Janztecpi.."
+#rsync -av --delete "$2/Debug/$1" "/mnt/janztecpi/Pi code/Debug/"
+COUNT=$(cat "$2/buildno")
+
+if [ "$1" == "coffeecode" ]; then
+echo "Increasing build No.."
+echo $(($COUNT+1)) > "$2/buildno"
+echo "Current build number is $(($COUNT+1))"
+else
+echo "Current build number is $COUNT"
+fi

+ 23 - 3
CoffeeCode/hal.cpp

@@ -6,6 +6,7 @@
  */
 #include <wiringPi.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
@@ -16,6 +17,7 @@
 #include "global.h"
 #include "logger.h"
 #include "timer.h"
+#include "database.h"
 
 
 typedef struct timespec timespec;
@@ -31,11 +33,16 @@ bool flagIgnoreRlsInt0, flagIgnoreRlsInt1;
 //storage for the last state of the buttons, the proximity sensor and the pressure sensor
 int pinState[4] = {1, 1, 1, 0};
 
+//sweep counter to log every brew
+
 timer Int0Timer(&halInt0TimerHandler);
 timer Int1Timer(&halInt1TimerHandler);
 timer idleTimer(&halIdleTimerHandler);
 
 time_t heatingCycle[] = {0, 0};
+timespec flowTimestep[] = {{0,0},{0,0}};
+uint8_t flowIndex = 0;
+
 timespec pumpCycle[] = {{0,0},{0,0}};
 
 //delay of the debounce in milliseconds
@@ -64,6 +71,8 @@ void halInit(void) {
 	idleTimer.setDivider(1200); //1 min
 	idleCounter = 0;
 	idle = false;
+	clock_gettime(CLOCK_REALTIME, &flowTimestep[0]);
+	clock_gettime(CLOCK_REALTIME, &flowTimestep[1]);
 	halDisplayOn();
 
 	if (optPower) {
@@ -103,6 +112,7 @@ void halInit(void) {
 		logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
 		return;
 	}
+	// in long term a manual brew detection would be nice
 }
 
 /**
@@ -260,6 +270,16 @@ void halIntFlow(void) {
 	//halRelaisOff(RELAIS_POWER);
 	logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
 	flowcnt++;
+
+	//subroutine to log the flow to the database
+	timespec deltaT;
+	clock_gettime(CLOCK_REALTIME, &flowTimestep[flowIndex]);
+	timespec_diff(&flowTimestep[((flowIndex + 1) % 2)], &flowTimestep[flowIndex], &deltaT);
+	if (sqlLogFlow(0, halGetFlow(), deltaT.tv_sec * 1000 + deltaT.tv_nsec/1000000)) {
+		logger_error("coffee.cpp: Couldn't write brewcounter to database");
+		return;
+	}
+	flowIndex = (flowIndex + 1) % 2;
 }
 
 /**
@@ -458,7 +478,7 @@ void halLeaveIdle(void){
 
 /**
  * Wrapper function to turn the pump on
- * and is used for time measurements on how long the pump is running
+ * and to measure how long the pump is running
  */
 void halPumpOn(void){
 	halRelaisOn(RELAIS_PUMP);
@@ -467,7 +487,7 @@ void halPumpOn(void){
 
 /**
  * Wrapper function to turn the pump off
- * and is used for time measurements on how long the pump is running
+ * and to measure how long the pump is running
  */
 void halPumpOff(void){
 	halRelaisOff(RELAIS_PUMP);
@@ -477,7 +497,7 @@ void halPumpOff(void){
 /**
  * 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 last
+ * when the pump is off, this function returns the time elapsed in the last pump cycle
  */
 double halGetPumpTime(void){
 	timespec now;