Skip to content

Commit

Permalink
added comments to flow sensor code and added flow rate to SD data (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanJesudason authored Feb 10, 2022
1 parent 1edf3ab commit 20d6df1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/Application/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,21 @@ 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();
}

// 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();
}
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/Components/Sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Components/Sensors/TurbineFlowSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 8 additions & 1 deletion src/Components/Sensors/TurbineFlowSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TurbineFlowSensor : public Sensor<TurbineFlowSensorData> {
void begin() override {
lastFlowTick = micros();
pinMode(A3, INPUT);
//sensor is updated once a second
setUpdateFreq(1000);
}

Expand All @@ -43,9 +44,15 @@ class TurbineFlowSensor : public Sensor<TurbineFlowSensorData> {
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 {
Expand Down

0 comments on commit 20d6df1

Please sign in to comment.