Brewery-Controller/boil_kettle/src/thermoControl/thermoControl.cpp
Chris Giacofei 0e74d66125 Change check for out of bounds setpoints.
Makes more sense this way, and is compatible with more boards.

ESP32 chips don't seem to like min/max with mixed types.
2024-04-29 12:47:49 -04:00

83 lines
1.7 KiB
C++

#include <Arduino.h>
#include <Adafruit_MAX31865.h>
#include "thermoControl.h"
thermoControl::thermoControl(double* current_temp, double* setpoint, double* power, modes mode) {
outMax = 100;
outMin = 10;
OpMode = mode;
SampleInterval = 100;
lastTime = millis()-SampleInterval;
output_pwm = power;
control_temp = setpoint;
actual_temp = current_temp;
hysteresis = 1.0;
max_pwr_threshold = 5.0;
Serial.println("Controller Started");
}
bool thermoControl::Compute() {
unsigned long now = millis();
unsigned long timeChange = (now - lastTime);
if(timeChange >= SampleInterval && OpMode == AUTOMATIC) {
double error = *control_temp - *actual_temp;
if (error >= max_pwr_threshold) {
*output_pwm = outMax;
} else if (error > hysteresis) {
*output_pwm = 100 * error / max_pwr_threshold;
if (*output_pwm > 100) *output_pwm = 100;
if (*output_pwm < 0) *output_pwm = 0;
} else {
*output_pwm = 0;
}
lastTime = now;
return true;
} else {
return false;
}
}
void thermoControl::SampleTime(int NewSampleTime) {
if (NewSampleTime > 0) {
SampleInterval = NewSampleTime;
}
}
void thermoControl::PowerLimits(double Max, double Min) {
if(Min >= Max) return;
outMax = Max;
outMin = Min;
}
void thermoControl::Hysteresis(double hys) {
hysteresis = hys;
}
void thermoControl::ThreshPWR(double thresh) {
max_pwr_threshold = thresh;
}
void thermoControl::Mode(modes newMode) {
Serial.println(newMode);
OpMode = newMode;
}
modes thermoControl::Mode() {
return OpMode;
}
modes thermoControl::CycleMode() {
if (OpMode + 1 == OVERFLOW) {
OpMode = (modes)(0);
} else {
OpMode = (modes)(OpMode + 1);
}
return OpMode;
}