-
Notifications
You must be signed in to change notification settings - Fork 0
/
rheem.ino
238 lines (196 loc) · 5.54 KB
/
rheem.ino
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// Arduino sketch to control Rheem Heat Pump Water Heater
//
// Hardware requirements:
// * Uno board
// * Dallas 1-Wire temperature sensors
// * relays
// * LCD
//
// Arduino libraries
#include <LiquidCrystal.h>
// third party libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PrintEx.h>
#include <Timer.h>
#include <PID_v1.h>
// project files
#include "constants.h"
#include "selftest.h"
#include "temperature.h"
#include "duty.h"
#define REVISION 1
PrintEx exSerial = Serial;
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
OneWire ow(2);
DallasTemperature sensors(&ow);
Timer timer;
unsigned int pulseMsec = 0;
double hexInTemp;
double hexOutTemp;
double hwcTopTemp;
double hwcBottomTemp;
bool pumpOn;
bool heaterOn;
// helpers to implement recycle time for compressor
unsigned long compressorStopTime = millis();
int recycleEvent = NO_TIMER_AVAILABLE;
enum State {
stopped,
starting,
started
};
State compressorState = stopped;
// PID variables
double dutyCycle = dutyMinimum;
double targetTemp = targetTemperature;
double kP = 0.1; // 0.0 disables the component
double kI = 0.01;
double kD = 0.5;
PID pid(&hexOutTemp, // input
&dutyCycle, // output
&targetTemp, // setpoint
kP, kI, kD,
P_ON_E,
REVERSE);
void onEvery(void* context);
void recycleCallback(void* context);
void setup() {
lcd.begin(16, 2);
Serial.begin(115200);
pinMode(pumpPin, OUTPUT);
pinMode(compressorPin, OUTPUT);
pinMode(selectorPin, OUTPUT);
// uncomment following line to find sensor addresses and test outputs
//selfTest();
sensors.begin();
sensors.setResolution(HeatExInAddr, 10);
sensors.setResolution(HeatExOutAddr, 10);
sensors.setResolution(HWC_TopAddr, 10);
sensors.setResolution(HWC_BottomAddr, 10);
sensors.setWaitForConversion(false);
timer.every(periodMsec, onEvery, NULL);
pid.SetOutputLimits(dutyMinimum, dutyMaximum);
pid.SetSampleTime(periodMsec);
pid.SetMode(AUTOMATIC);
stopPump();
stopCompressor();
selectCompressor();
}
void startCompressor() {
if (stopped == compressorState) {
if (compressorStopTime + recycleTimeMsec < millis() ) {
digitalWrite(compressorPin, LOW);
exSerial.printf("Compressor started\n");
compressorState = started;
}
else {
unsigned long holdoff = recycleTimeMsec - (millis() - compressorStopTime);
recycleEvent = timer.after(holdoff, recycleCallback, NULL);
compressorState = starting;
exSerial.printf("Compressor starting\n");
}
}
else {
// nothing to do
}
}
void recycleCallback(void* context) {
digitalWrite(compressorPin, LOW);
compressorState = started;
exSerial.printf("Compressor started after recycle delay\n");
}
void stopCompressor() {
digitalWrite(compressorPin, HIGH);
if (stopped != compressorState) {
compressorState = stopped;
compressorStopTime = millis();
if (recycleEvent >= 0) {
recycleEvent = timer.stop(recycleEvent);
}
exSerial.printf("Compressor stopped\n");
}
}
void selectCompressor() {digitalWrite(selectorPin, LOW); heaterOn = false;}
void selectHeater() {digitalWrite(selectorPin, HIGH); heaterOn = true;}
void startPump() {setDutyCycle(dutyMinimum); pumpOn = true;}
void stopPump() {digitalWrite(pumpPin, HIGH); pumpOn = false; pulseMsec = 0;}
void stopAllHeaters() {
exSerial.printf("Stopping all heaters\n");
stopCompressor();
stopPump();
}
void calculateOutputs() {
if (hexInTemp >= hexInTempMax || hwcBottomTemp >= hwcBottomTempMax ) {
exSerial.printf("Cylinder water too hot\n");
if ( compressorState != stopped || heaterOn) {
stopAllHeaters();
}
// too hot, nothing to do
return;
}
// safety override
if (hexOutTemp > hexOutTempMax ) {
exSerial.printf("Heat exchanger output too hot\n");
if (compressorState != stopped || heaterOn) {
stopAllHeaters();
}
// too hot, nothing to do
return;
}
if (hwcBottomTemp < hwcBottomTempMin && stopped == compressorState) {
exSerial.printf("Hot-water cylinder bottom too cold, starting compressor\n");
startCompressor(); // will take a while to start heating...
//TODO: start heater if ambient temperature low
startPump();
return;
}
pid.Compute(); // where all the magic happens...
if (started == compressorState) {
setDutyCycle(dutyCycle);
}
}
void onEvery(void* context) {
// first start the pulse, to avoid being affected
// by time taken by temperature measurement, printing, etc.
if (pumpOn) {
timer.pulseImmediate(pumpPin, pulseMsec, LOW);
}
measureTemperatures();
displayTemperatures();
#if 0
if ( hexInTemp < 0 || hexOutTemp < 0 || hwcBottomTemp < 0 || hwcTopTemp < 0) {
// likely sensor error (-127C), so stop everything
// TODO: display error code
stopAllHeaters();
return;
}
#endif
calculateOutputs();
displayDuty();
}
void getParameters() {
char type = Serial.read();
float value = Serial.parseFloat();
exSerial.printf("%c = %.3f\n", type, value);
switch (type) {
case 'p': kP = (double)value; break;
case 'i': kI = (double)value; break;
case 'd': kD = (double)value; break;
case 't': targetTemp = (double)value; break;
case '?': break; // print current values
case 'h': break; // print current values
default:
exSerial.printf("Error: %c is not a valid parameter\n", type);
return;
}
pid.SetTunings(kP, kI, kD);
exSerial.printf("kP = %.3f kI = %.3f kD = %.3f target: %.1f C\n", kP, kI, kD, targetTemp);
}
void loop() {
timer.update();
if (Serial.available() > 0) {
getParameters();
}
}