-
Notifications
You must be signed in to change notification settings - Fork 1
/
WaterController.cpp
135 lines (113 loc) · 3.93 KB
/
WaterController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* IIRR -- Intelligent Irrigator Based on ESP8266
Copyright (C) 2016--2018 Sergio Queiroz <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "WaterController.h"
#include "ConfParams.h"
#include "sensor_calibration.h"
#include "SensorTask.h"
#include "global_funcs.h"
static unsigned long pulseCount;
static void ICACHE_RAM_ATTR handleInterrupt() {
pulseCount++;
}
WaterStartStatus WellPumpWaterController::startWater(bool ignoreNoConf) {
if(!ignoreNoConf && this->noConfStatus()) return WATER_STARTNOCONF;
if(this->emptyTriggered) return WATER_STARTEMPTY;
if(pumpIsOn) return WATER_STARTNOACTION;
digitalWrite(PUMP_PIN, HIGH);
this->pumpIsOn = true;
return WATER_STARTOK;
}
void WellPumpWaterController::stopWater() {
digitalWrite(PUMP_PIN, LOW);
this->pumpIsOn = false;
}
WellPumpWaterController::~WellPumpWaterController() {
if (this->currStatus() == WATER_CURRFLOWING)
this->stopWater();
}
WaterCurrSensorStatus WellPumpWaterController::currStatus() {
if (this->emptyTriggered) return WATER_CURREMPTY;
if (this->noConfStatus()) return WATER_CURRNOCONF;
double avgPulsesSec;
turnOnSensor();
avgSensorPulsesPerSec();
avgPulsesSec = 0.5*avgSensorPulsesPerSec();
avgPulsesSec += 0.5*avgSensorPulsesPerSec(); //we call three times, ignoring the first and taking the avg of the last two.
turnOffSensor();
bool noFlow = (avgPulsesSec <= (acceptedMinFlowPerc*mainConfParams.normalPulsesPerSec));
if (!noFlow) {
return WATER_CURRFLOWING;
}
//here we do not have flow
if (this->pumpIsOn) {
//trigger EMPTY
stopWater();
this->emptyTriggered = true;
return WATER_CURREMPTY;
} else {
//pump is off, we dont have flow
return WATER_CURRSTOP;
}
}
void WellPumpWaterController::resetEmpty() {
this->emptyTriggered = false;
}
bool WellPumpWaterController::configureFlow() {
double avgPulsesSec;
bool pumpWasOn = this->pumpIsOn;
if (!pumpWasOn) {
if (this->startWater(true) != WATER_STARTOK) {
return false;
}
}
delayHandler(30000);
this->turnOnSensor();
delayHandler(1000);
avgSensorPulsesPerSec();
avgPulsesSec = 0.5*avgSensorPulsesPerSec();
avgPulsesSec += 0.5*avgSensorPulsesPerSec(); //we call three times, ignoring the first and taking the avg of the last two.
this->turnOffSensor();
if (!pumpWasOn) {
this->stopWater();
}
mainConfParams.normalPulsesPerSec = (unsigned long)avgPulsesSec;
return SensorTask::updateConfParams(mainConfParams);
}
void WellPumpWaterController::turnOnSensor() {
digitalWrite(DECOD_A0_PIN, LOW);
digitalWrite(DECOD_A1_PIN, HIGH);
digitalWrite(DECOD_A2_PIN, HIGH);
enableMulAndDecod();
}
void WellPumpWaterController::turnOffSensor() {
disableMulAndDecod();
}
double WellPumpWaterController::avgSensorPulsesPerSec() {
pulseCount = 0;
//XXX chamar codigo de ligar o sensor antes de chamar essa funcao
attachInterrupt(digitalPinToInterrupt(FLOWSIGNAL_PIN), handleInterrupt, FALLING);
unsigned long startTime = millis();
do {
delayHandler(50);
} while ((millis() - startTime) < 1000);
detachInterrupt(digitalPinToInterrupt(FLOWSIGNAL_PIN));
double pulsesPerSec = (1000.0/(millis() - startTime))*pulseCount;
return pulsesPerSec;
}
WellPumpWaterController::WellPumpWaterController() : WaterController(),
pumpIsOn(false),
emptyTriggered(false){
digitalWrite(PUMP_PIN, LOW);
}