From 20d6df10db420a46d76d90eab460e0250d96fcd5 Mon Sep 17 00:00:00 2001 From: Nathan Jesudason Date: Wed, 9 Feb 2022 22:54:17 -0800 Subject: [PATCH] added comments to flow sensor code and added flow rate to SD data (#31) --- src/Application/App.hpp | 20 ++++++++++++-------- src/Components/Sensor.hpp | 3 +++ src/Components/Sensors/TurbineFlowSensor.cpp | 2 ++ src/Components/Sensors/TurbineFlowSensor.hpp | 9 ++++++++- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Application/App.hpp b/src/Application/App.hpp index 9e57dc8..581a250 100755 --- a/src/Application/App.hpp +++ b/src/Application/App.hpp @@ -216,10 +216,10 @@ class App : public KPController, public KPSerialInputObserver, public TaskObserv // Regular log header if (!SD.exists(config.logFile)) { File file = SD.open(config.logFile, FILE_WRITE); - KPStringBuilder<384> header{"UTC, Formatted Time, Task Name, Valve Number, Current " + KPStringBuilder<404> header{"UTC, Formatted Time, Task Name, Valve Number, Current " "State, Config Sample Time, Config Sample " "Pressure, Config Sample Volume, Temperature Recorded," - "Max Pressure Recorded, Volume Recorded\n"}; + "Max Pressure Recorded, Volume Recorded, Flow Rate\n"}; file.println(header); file.close(); } @@ -227,10 +227,10 @@ class App : public KPController, public KPSerialInputObserver, public TaskObserv // Detail log header if (!SD.exists("detail.csv")) { File file = SD.open("detail.csv", FILE_WRITE); - KPStringBuilder<384> header{"UTC, Formatted Time, Task Name, Valve Number, Current " + KPStringBuilder<404> header{"UTC, Formatted Time, Task Name, Valve Number, Current " "State, Config Sample Time, Config Sample " "Pressure, Config Sample Volume, Temperature Recorded," - "Pressure Recorded, Volume Recorded\n"}; + "Pressure Recorded, Volume Recorded, Flow Rate\n"}; file.println(header); file.close(); } @@ -333,7 +333,7 @@ class App : public KPController, public KPSerialInputObserver, public TaskObserv formattedTime, "%u/%u/%u %02u:%02u:%02u GMT+0", year(utc), month(utc), day(utc), hour(utc), minute(utc), second(utc)); - KPStringBuilder<512> data{ + KPStringBuilder<544> data{ utc, ",", formattedTime, @@ -354,7 +354,9 @@ class App : public KPController, public KPSerialInputObserver, public TaskObserv ",", status.pressure, ",", - status.waterVolume}; + status.waterVolume, + ",", + status.waterFlow}; log.println(data); log.flush(); log.close(); @@ -372,7 +374,7 @@ class App : public KPController, public KPSerialInputObserver, public TaskObserv formattedTime, "%u/%u/%u %02u:%02u:%02u GMT+0", year(utc), month(utc), day(utc), hour(utc), minute(utc), second(utc)); - KPStringBuilder<512> data{ + KPStringBuilder<544> data{ utc, ",", formattedTime, @@ -393,7 +395,9 @@ class App : public KPController, public KPSerialInputObserver, public TaskObserv ",", status.maxPressure, ",", - status.waterVolume}; + status.waterVolume, + ",", + status.waterFlow}; log.println(data); log.flush(); log.close(); diff --git a/src/Components/Sensor.hpp b/src/Components/Sensor.hpp index fde3c26..591429b 100644 --- a/src/Components/Sensor.hpp +++ b/src/Components/Sensor.hpp @@ -108,6 +108,9 @@ class Sensor { } // Lazy evaluation: last_update will never be initialized if the sensor is not enabled; + // last_update is initialized to millis() here in the first call, and then set below + // see link for another examples of static vars in functions: + // https://www.tutorialspoint.com/what-is-the-lifetime-of-a-static-variable-in-a-cplusplus-function static long last_update = millis(); if ((millis() - last_update) < updateInterval) { return ErrorCode::notReady; diff --git a/src/Components/Sensors/TurbineFlowSensor.cpp b/src/Components/Sensors/TurbineFlowSensor.cpp index d9657f9..ea4e796 100644 --- a/src/Components/Sensors/TurbineFlowSensor.cpp +++ b/src/Components/Sensors/TurbineFlowSensor.cpp @@ -5,7 +5,9 @@ volatile unsigned long flowIntervalMicros; volatile bool flowUpdated; void flowTick() { + //This is the time interval since the last flowTick flowIntervalMicros = micros() - lastFlowTick; lastFlowTick = micros(); + //make sure to read data flowUpdated = true; } \ No newline at end of file diff --git a/src/Components/Sensors/TurbineFlowSensor.hpp b/src/Components/Sensors/TurbineFlowSensor.hpp index 1e41920..3ac3150 100644 --- a/src/Components/Sensors/TurbineFlowSensor.hpp +++ b/src/Components/Sensors/TurbineFlowSensor.hpp @@ -21,6 +21,7 @@ class TurbineFlowSensor : public Sensor { void begin() override { lastFlowTick = micros(); pinMode(A3, INPUT); + //sensor is updated once a second setUpdateFreq(1000); } @@ -43,9 +44,15 @@ class TurbineFlowSensor : public Sensor { SensorData read() override { if (flowUpdated) { flowUpdated = false; - + //micro is 1e-6, so we divide the micros in a second with the flow interval + //to get the frequency auto hz = 1000000.0 / double(flowIntervalMicros); + //The spec sheet says that the output frequency is between 36.6 to 917 Hz + //So if hz is less than 37/36.6, then the flow is zero. Otherwise, interporlate + //between the frequency into the flow rate. + //flow that the sensor can record is between 0.1LPM and 2.5LPM lpm = hz < 37 ? 0 : interpolate(hz, 37, 917, 0.110, 2.476); + //LPM * change in minute gives volume in liters volume += lpm * (flowIntervalMicros / 60000000.0); println("Volume: ", volume, ", LPM: ", lpm); } else {