-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRackTempController.cpp
317 lines (263 loc) · 8.19 KB
/
RackTempController.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "RackTempController.h"
#include <ArduinoLog.h>
/**
* Process temperatures, modify fan speed, check for errors, update trends
*/
void RackTempController::process(RackState_t& rs) {
// read temperatures
readTempStates(rs.thermos);
// adjust fan speeds based on temps
adjustFanSpeeds(rs);
// read fan tach/rpms - delay of 750ms per fan
readFanSpeeds(rs.fans);
// verify fan PWMs matches RPMs
verifyFanStates(rs.fans);
// analyse trends
analyseTrends(rs);
};
void RackTempController::analyseTrends(RackState_t& rs) /* const */ {
// cache RackState for trend analysis
cache(rs);
float acc = 0.0;
uint16_t samples = 0;
// iterate through history to accumulate temps, count samples
for(auto it = _rsHistory.begin(); it != _rsHistory.end(); it++) {
for(auto tt = it->thermos.begin(); tt != it->thermos.end(); tt++) {
acc += tt->second.tempCelsuis;
samples++;
}
}
float movingAve = (float)acc/samples;
Log.notice(F("Moving average temp - %F"), movingAve);
rs.aveTempCelsius = movingAve;
//rs.trend.movingAveTempCelsuis = movingAve;
//rs.trend.accFanError =
}
/**
* Maintains FIFO queue. Not using queue STL
* as it doesn't provide iterator.
*/
void RackTempController::cache(const RackState_t& rs) {
_rsHistory.push_back(rs);
if (_rsHistory.size() > _historyDepth) {
_rsHistory.pop_front();
}
Log.notice(F("history queue size %d"), _rsHistory.size());
}
/**
* Confirm for the fan, that
* - RPMs are above minimum (if minRpm is not zero), i.e. the fan is detected as spinning
* - PWM setting and RPM reading is within expectation, i.e. +/- 10%
*/
RESULT RackTempController::checkRpm(FanState_t& fs) const {
// is fan spinning at all?
if (fs.minRpm > 0 && fs.rpm < fs.minRpm) {
fs.result = ERR_FAN_NOT_OPERATIONAL;
Log.error(F("Fan %s is not operational"), fs.position.c_str());
return ERR_FAN_NOT_OPERATIONAL;
}
// is the fan within expected RPM variance given dutyCycle?
uint16_t expectedRpm = round( fs.maxRpm * (float)fs.pwm/100 );
int16_t r = round(expectedRpm - fs.maxRpm * _rpmVariance);
uint16_t minExpectedRpm = (r<0) ? 0 : r;
uint16_t maxExpectedRpm = round(expectedRpm + fs.maxRpm * _rpmVariance);
// if rpm is out of range of expectated rpm
if (fs.rpm < minExpectedRpm || fs.rpm > maxExpectedRpm)
{
/* if (fs.rpm > fs.maxRpm) {
// assume this is NOT an error condition - perhaps tach noise or manufacture issue?
Log.warning(F("Fan %s is spinning at %d rpm, which is faster than maxRpm: %d"),
fs.position.c_str(),
fs.rpm,
fs.maxRpm);
}
else */
{
// we are out of range
fs.result = ERR_FAN_TACH;
Log.error(F("Rpm of fan %s is out of range: %d is not between %d to %d"),
fs.position.c_str(),
fs.rpm,
minExpectedRpm,
maxExpectedRpm);
return ERR_FAN_TACH;
}
}
return RES_OK;
}
/**
* Verify fan state: speed
*/
void RackTempController::verifyFanStates(Fans_t& fans) const {
for (auto it = fans.begin(); it != fans.end(); it++) {
checkRpm(it->second);
}
}
void RackTempController::adjustFanSpeeds(RackState_t& rs) {
// Basic rule: any thermo above threshold temp, set it to full spin
// else run it at half speed.
uint8_t dutyCycle = 50;
for (auto it = rs.thermos.begin(); it != rs.thermos.end(); it++) {
Temperature_t& thermo = it->second;
if (thermo.tempCelsuis > _TEMP_THRESHOLD) {
dutyCycle = 100;
}
}
// update fan speed and state
_fanControl.setPWMForAll(dutyCycle);
for (auto it = rs.fans.begin(); it != rs.fans.end(); it++) {
it->second.pwm = dutyCycle;
}
Log.notice(F("Fan's pwm set at - %d"), dutyCycle);
}
/**
* Get tach/rpm for all fans
* Delay of 750ms per fan within getRPM()
*/
void RackTempController::readFanSpeeds(Fans_t& fans) {
uint16_t rpm = 0;
Log.notice(F("Reading fan rpms"));
for (auto it = fans.begin(); it != fans.end(); it++) {
_fanControl.getRPM(it->first, rpm);
it->second.rpm = rpm;
Log.notice(F("Fan %s rpm - %d"), it->second.position.c_str(), rpm);
}
};
void RackTempController::readTempStates(Thermos_t& thermos) {
// Initialise sensors each read incase new sensors are added/removed.
_tempSensors.begin();
// useful for new thermo's to get deviceAddress
// searchAndPrintAddresses();
// Iterate through all devices ensuring they are still connected
for (auto it = thermos.begin(); it != thermos.end(); it++) {
Temperature_t& thermo = it->second;
if (!_tempSensors.isConnected(thermo.addr)) {
Log.warning(F("Unable to find thermometer %s"), it->first.c_str());
thermo.result = ERR_FAILED_TO_FIND_DEVICE;
}
}
// Send command to all DS18* for temperature conversion
Log.notice(F("Requesting temperatures"));
_tempSensors.requestTemperatures();
// Get temperature for each thermometer
for (auto it = thermos.begin(); it != thermos.end(); it++) {
Temperature_t& thermo = it->second;
// if device found, then read temp
if (thermo.result != ERR_FAILED_TO_FIND_DEVICE) {
if ((thermo.tempCelsuis = _tempSensors.getTempC(thermo.addr)) == DEVICE_DISCONNECTED_C) {
thermo.result = ERR_FAILED_TO_READ_TEMP;
Log.warning(F("Failed to read %s temperature"), it->first.c_str());
}
else {
Log.notice(F("%s.tempCelsuis - %F"), it->first.c_str(), thermo.tempCelsuis);
}
}
}
}
/**
* Search for all devices and printout addresses
*/
void RackTempController::searchAndPrintAddresses() {
DeviceAddress deviceAddr;
// locate devices on the bus
Serial.println("Locating devices...");
Serial.print("Found ");
int deviceCount = _tempSensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");
Serial.println("Printing addresses...");
for (int i = 0; i < deviceCount; i++)
{
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" : ");
_tempSensors.getAddress(deviceAddr, i);
printAddress(deviceAddr);
}
}
// function to print a device address for DS18*
void RackTempController::printAddress(const DeviceAddress deviceAddress) const
{
for (uint8_t i = 0; i < 8; i++)
{
Serial.print("0x");
if (deviceAddress[i] < 0x10)
Serial.print("0");
Serial.print(deviceAddress[i], HEX);
if (i < 7)
Serial.print(", ");
}
Serial.println("");
}
RackState_t RackTempController::build() const {
RackState_t rs;
rs.thermos.insert({
"topRack", {
{ 0x28, 0xAA, 0x48, 0x66, 0x53, 0x14, 0x01, 0xD5 },
0.0,
RES_OK
}
});
rs.thermos.insert({
"baseRack", {
{ 0x28, 0xAA, 0x51, 0x59, 0x53, 0x14, 0x01, 0x88 },
0.0,
RES_OK
}
});
rs.aveTempCelsius = 0.0;
rs.fans.insert({
1, {
"TL",
0, 0,
400, 1200,
RES_OK
}
});
rs.fans.insert({
2, {
"TR",
0, 0,
400, 1200,
RES_OK
}
});
rs.fans.insert({
3, {
"BL",
0, 0,
400, 1200,
RES_OK
}
});
rs.fans.insert({
4, {
"BR",
0, 0,
400, 1200,
RES_OK
}
});
return rs;
}
RackState_t RackTempController::build_debug() const {
RackState_t rs;
rs.thermos.insert({
"topRack", {
{ 0x28, 0xFF, 0xBF, 0xDC, 0x51, 0x17, 0x04, 0x48 }, //keyes
0.0,
RES_OK
}
});
rs.aveTempCelsius = 0.0;
rs.fans.insert({
1,{
"TL",
0, 0,
400, 1200,
RES_OK
}
});
return rs;
}