Refactor PID inner core out
This commit is contained in:
@@ -21,6 +21,7 @@ uint32_t currentTempTargetDegC = 0; // Current temperature target in
|
||||
int32_t powerSupplyWattageLimit = 0;
|
||||
bool heaterThermalRunaway = false;
|
||||
|
||||
static int32_t getPIDResultX10Watts(int32_t tError);
|
||||
static void detectThermalRunaway(const int16_t currentTipTempInC, const int tError);
|
||||
static void setOutputx10WattsViaFilters(int32_t x10Watts);
|
||||
|
||||
@@ -32,7 +33,6 @@ void startPIDTask(void const *argument __unused) {
|
||||
*/
|
||||
setTipX10Watts(0); // disable the output at startup
|
||||
|
||||
history<int32_t, PID_TIM_HZ> tempError = {{0}, 0, 0};
|
||||
currentTempTargetDegC = 0; // Force start with no output (off). If in sleep / soldering this will
|
||||
// be over-ridden rapidly
|
||||
pidTaskNotification = xTaskGetCurrentTaskHandle();
|
||||
@@ -42,12 +42,12 @@ void startPIDTask(void const *argument __unused) {
|
||||
vTaskDelay(2);
|
||||
TipThermoModel::getTipInC(true);
|
||||
}
|
||||
int32_t x10WattsOut = 0;
|
||||
for (;;) {
|
||||
|
||||
x10WattsOut = 0;
|
||||
// This is a call to block this thread until the ADC does its samples
|
||||
if (ulTaskNotifyTake(pdTRUE, 2000)) {
|
||||
|
||||
int32_t x10WattsOut = 0;
|
||||
// Do the reading here to keep the temp calculations churning along
|
||||
uint32_t currentTipTempInC = TipThermoModel::getTipInC(true);
|
||||
PIDTempTarget = currentTempTargetDegC;
|
||||
@@ -61,15 +61,24 @@ void startPIDTask(void const *argument __unused) {
|
||||
if (PIDTempTarget > TipThermoModel::getTipMaxInC()) {
|
||||
PIDTempTarget = TipThermoModel::getTipMaxInC();
|
||||
}
|
||||
|
||||
// As we get close to our target, temp noise causes the system
|
||||
// to be unstable. Use a rolling average to dampen it.
|
||||
// We overshoot by roughly 1 degree C.
|
||||
// This helps stabilize the display.
|
||||
int32_t tError = PIDTempTarget - currentTipTempInC;
|
||||
tError = tError > INT16_MAX ? INT16_MAX : tError;
|
||||
tError = tError < INT16_MIN ? INT16_MIN : tError;
|
||||
tempError.update(tError);
|
||||
|
||||
detectThermalRunaway(currentTipTempInC, tError);
|
||||
x10WattsOut = getPIDResultX10Watts(tError);
|
||||
} else {
|
||||
detectThermalRunaway(currentTipTempInC, 0);
|
||||
}
|
||||
setOutputx10WattsViaFilters(x10WattsOut);
|
||||
} else {
|
||||
// ADC interrupt timeout
|
||||
setTipPWM(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int32_t getPIDResultX10Watts(int32_t tError) {
|
||||
|
||||
// Now for the PID!
|
||||
|
||||
@@ -83,7 +92,7 @@ void startPIDTask(void const *argument __unused) {
|
||||
int32_t x10WattsNeeded = tempToX10Watts(tError);
|
||||
// note that milliWattsNeeded is sometimes negative, this counters overshoot
|
||||
// from I term's inertia.
|
||||
x10WattsOut += x10WattsNeeded;
|
||||
int32_t x10WattsOut = x10WattsNeeded;
|
||||
|
||||
// I term - energy needed to compensate for heat loss.
|
||||
// We track energy put into the system over some window.
|
||||
@@ -96,17 +105,7 @@ void startPIDTask(void const *argument __unused) {
|
||||
// and counters extra power if the iron is no longer losing temp.
|
||||
// basically: temp - lastTemp
|
||||
// Unfortunately, our temp signal is too noisy to really help.
|
||||
|
||||
detectThermalRunaway(currentTipTempInC, tError);
|
||||
} else {
|
||||
detectThermalRunaway(currentTipTempInC, 0);
|
||||
}
|
||||
setOutputx10WattsViaFilters(x10WattsOut);
|
||||
} else {
|
||||
// ADC interrupt timeout
|
||||
setTipPWM(0);
|
||||
}
|
||||
}
|
||||
return x10WattsOut;
|
||||
}
|
||||
|
||||
void detectThermalRunaway(const int16_t currentTipTempInC, const int tError) {
|
||||
|
||||
Reference in New Issue
Block a user