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