-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorManager.cpp
More file actions
90 lines (72 loc) · 3.2 KB
/
SensorManager.cpp
File metadata and controls
90 lines (72 loc) · 3.2 KB
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
#include "esp32-hal-spi.h"
#include "esp32-hal.h"
#include <sys/_types.h>
#include "SensorManager.h"
#include <SPI.h>
// VBM Domobar Junior is an E61 machine, so after the boiler reaches its target temp, the brass needs to heat up the grouphead (takes 11 - 15 minutes usually)
const float targetGroupheadTemp = 90.0 + 14.0; // 90c target + 14 to account for calculation asymptote
const float initialGroupheadTemp = 50.0; // assumed grouphead temp when boiler is ready
// The "sluggishness" factor of the brass. The calculation used Newton's Law of Heating to estimate grouphead temp.
// calculation: 13.5 minutes to heat the grouphead after boiler is at temp. using Newton's Law of Heating, we never reach the max asymptote (91c target),
// so we calculate for tau: 91 = 105 - (105 - 50)*e^(-812/tau) -> tau = ~592.
const float tau = 592.0;
SensorManager::SensorManager(SPIClass* sharedSPI) {
maxSPI = sharedSPI;
thermo = new Adafruit_MAX31865(MAX_CS, maxSPI);
lastReadTime = 0;
currentTemp = 0.0;
isMeasuring = false;
measureStartTime = 0;
}
void SensorManager::init() {
pinMode(MAX_CS, OUTPUT);
digitalWrite(MAX_CS, HIGH); // Deselect chip
// maxSPI->begin(21, 22, 17, MAX_CS);
// ignores the startup noise
Serial.println("Initializing Sensor in Robust Mode...");
thermo->begin(MAX31865_3WIRE);
thermo->clearFault();
}
void SensorManager::update() {
unsigned long currentMillis = millis();
// Measure every 1 second.
// TODO: do we want 500ms instead? maybe later
if (!isMeasuring) {
if (currentMillis - lastReadTime > 1000) {
lastReadTime = currentMillis;
thermo->enableBias(true);
measureStartTime = currentMillis; // Start the stopwatch
isMeasuring = true;
}
} else {
// 100ms of hardware stabilization
if (currentMillis - measureStartTime >= 100) {
float temp = thermo->temperature(RNOMINAL, RREF);
uint8_t fault = thermo->readFault();
if (fault) {
Serial.print("Fault 0x"); Serial.println(fault, HEX);
thermo->clearFault();
thermo->enableBias(false);
} else {
// CALIBRATION
// 0.385ohms per 1C. 100ohms at 0C. Redundant 3WIRE mode .
// float calibratedTemp = temp - 0.25; // Account for cable length + plugs resistance.
// float calibratedTemp = temp;
Serial.print("Stable Temp: "); Serial.println(temp);
currentTemp = temp;
thermo->enableBias(false);
isMeasuring = false;
}
}
}
}
#include <math.h>
float SensorManager::getEstimatedGroupheadTemp(unsigned long timeSinceBoilerReadyMs) {
// Convert elapsed time to seconds
float timeSeconds = timeSinceBoilerReadyMs / 1000.0;
// Newton's law of heating / cooling
float estimatedTemp = targetGroupheadTemp - (targetGroupheadTemp - initialGroupheadTemp) * exp(-timeSeconds / tau);
return estimatedTemp;
}
float SensorManager::getTemp() { return currentTemp; }
bool SensorManager::checkFaults() { return (thermo->readFault() != 0); }