72 lines
1.5 KiB
C++
72 lines
1.5 KiB
C++
|
|
#include <Arduino.h>
|
|
#include <EEPROM.h>
|
|
#include "thermoControl.h"
|
|
|
|
thermoControl::thermoControl(double* input_temp, uint8_t* setpoint_temp, uint8_t* output_pwm, uint8_t* max_threshold, uint8_t* hysteresis) {
|
|
output_pwm = output_pwm;
|
|
input_temp = input_temp;
|
|
setpoint_temp = setpoint_temp;
|
|
|
|
outMax = 100;
|
|
outMin = 10;
|
|
|
|
OpMode = OFF;
|
|
SampleTime = 100;
|
|
lastTime = millis()-SampleTime;
|
|
|
|
}
|
|
|
|
bool thermoControl::Compute() {
|
|
unsigned long now = millis();
|
|
unsigned long timeChange = (now - lastTime);
|
|
|
|
if(timeChange>=SampleTime && OpMode == AUTOMATIC) {
|
|
double output;
|
|
double input = *input_temp;
|
|
double error = (double)*setpoint_temp - input;
|
|
|
|
if (error >= (double)*max_threshold) {
|
|
output = outMax;
|
|
} else if (error > *hysteresis) {
|
|
output = 100 * error / *max_threshold;
|
|
output = max(output, outMin);
|
|
output = min(output, outMax);
|
|
} else {
|
|
output = 0;
|
|
}
|
|
*output_pwm = output;
|
|
lastTime = now;
|
|
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void thermoControl::SetSampleTime(int NewSampleTime) {
|
|
if (NewSampleTime > 0) {
|
|
SampleTime = (unsigned long)NewSampleTime;
|
|
}
|
|
}
|
|
|
|
void thermoControl::SetPowerLimits(uint8_t Max, uint8_t Min) {
|
|
if(Min >= Max) return;
|
|
outMax = Max;
|
|
outMin = Min;
|
|
}
|
|
|
|
void thermoControl::SetMode(modes newMode) {
|
|
OpMode = newMode;
|
|
}
|
|
|
|
modes thermoControl::GetMode() {
|
|
return OpMode;
|
|
}
|
|
|
|
modes thermoControl::CycleMode() {
|
|
OpMode = (modes)(OpMode + 1);
|
|
return OpMode;
|
|
}
|
|
|