From c0a5e244b93eba570c08bc73ecd2fe7cfc067636 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" <5425387+Ralim@users.noreply.github.com> Date: Fri, 22 Sep 2023 10:19:50 +1000 Subject: [PATCH] Temperature code updates (#1814) * Create a typedef for temperatures * Quick parse replace temp types * Fixup for fast/slow PWM on PinecilV2 * Update PIDThread.cpp * Pinecil small tips need less smoothing * Remove incorrect comment * Remove unused function * Update PinecilV2 Tune as well --- source/Core/BSP/BSP.h | 2 + source/Core/BSP/MHP30/ThermoModel.cpp | 6 +- source/Core/BSP/Miniware/ThermoModel.cpp | 2 +- source/Core/BSP/Pinecil/ThermoModel.cpp | 2 +- source/Core/BSP/Pinecilv2/BSP.cpp | 8 +- source/Core/BSP/Pinecilv2/IRQ.cpp | 41 ++++++----- source/Core/BSP/Pinecilv2/IRQ.h | 1 - source/Core/BSP/Pinecilv2/Setup.h | 6 +- source/Core/BSP/Pinecilv2/ThermoModel.cpp | 2 +- source/Core/BSP/Pinecilv2/configuration.h | 1 - source/Core/BSP/Sequre_S60/ThermoModel.cpp | 2 +- source/Core/Drivers/TipThermoModel.cpp | 29 ++++---- source/Core/Drivers/TipThermoModel.h | 21 +++--- source/Core/Inc/Types.h | 10 +++ source/Core/Inc/main.hpp | 7 +- source/Core/Inc/power.hpp | 1 - source/Core/Src/power.cpp | 8 -- .../utils/checkUndervoltage.cpp | 2 +- source/Core/Threads/PIDThread.cpp | 73 +++++++++---------- 19 files changed, 116 insertions(+), 108 deletions(-) create mode 100644 source/Core/Inc/Types.h diff --git a/source/Core/BSP/BSP.h b/source/Core/BSP/BSP.h index 626989fe49..37508597c7 100644 --- a/source/Core/BSP/BSP.h +++ b/source/Core/BSP/BSP.h @@ -3,9 +3,11 @@ #include "BSP_Power.h" #include "BSP_QC.h" #include "Defines.h" +#include "Types.h" #include "configuration.h" #include #include + /* * BSP.h -- Board Support * diff --git a/source/Core/BSP/MHP30/ThermoModel.cpp b/source/Core/BSP/MHP30/ThermoModel.cpp index f43499a0f4..9275d835c4 100644 --- a/source/Core/BSP/MHP30/ThermoModel.cpp +++ b/source/Core/BSP/MHP30/ThermoModel.cpp @@ -6,10 +6,12 @@ */ #include "Setup.h" #include "TipThermoModel.h" +#include "Types.h" #include "Utils.h" #include "configuration.h" -extern uint16_t tipSenseResistancex10Ohms; -uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { + +extern uint16_t tipSenseResistancex10Ohms; +TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { // For the MHP30, we are mimicing the original code and using the resistor fitted to the base of the heater head, // this is measured at boot in pid task and in the disconnected tip check if tip is removed if (tipSenseResistancex10Ohms > 900 && tipSenseResistancex10Ohms <= 1100) { diff --git a/source/Core/BSP/Miniware/ThermoModel.cpp b/source/Core/BSP/Miniware/ThermoModel.cpp index 25376d4ed2..da9dac4c37 100644 --- a/source/Core/BSP/Miniware/ThermoModel.cpp +++ b/source/Core/BSP/Miniware/ThermoModel.cpp @@ -127,4 +127,4 @@ const int32_t uVtoDegC[] = { #endif const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(uVtoDegC[0])); -uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); } +TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); } diff --git a/source/Core/BSP/Pinecil/ThermoModel.cpp b/source/Core/BSP/Pinecil/ThermoModel.cpp index fa2cd65d34..a20fe0aad7 100644 --- a/source/Core/BSP/Pinecil/ThermoModel.cpp +++ b/source/Core/BSP/Pinecil/ThermoModel.cpp @@ -69,4 +69,4 @@ const int32_t uVtoDegC[] = { const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(uVtoDegC[0])); -uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); } +TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); } diff --git a/source/Core/BSP/Pinecilv2/BSP.cpp b/source/Core/BSP/Pinecilv2/BSP.cpp index 358098668c..7c711a6106 100644 --- a/source/Core/BSP/Pinecilv2/BSP.cpp +++ b/source/Core/BSP/Pinecilv2/BSP.cpp @@ -17,8 +17,8 @@ // These control the period's of time used for the PWM const uint16_t powerPWM = 255; -const uint8_t holdoffTicks = 25; // This is the tick delay before temp measure starts (i.e. time for op-amp recovery) -const uint8_t tempMeasureTicks = 25; +uint8_t holdoffTicks = 25; // This is the tick delay before temp measure starts (i.e. time for op-amp recovery) +uint8_t tempMeasureTicks = 25; uint16_t totalPWM = 255; // Total length of the cycle's ticks @@ -162,13 +162,13 @@ uint8_t getTipResistanceX10() { uint8_t getTipThermalMass() { if (lastTipResistance >= 80) { - return TIP_THERMAL_MASS; + return 65; } return 45; } uint8_t getTipInertia() { if (lastTipResistance >= 80) { - return TIP_THERMAL_MASS; + return 90; } return 10; } diff --git a/source/Core/BSP/Pinecilv2/IRQ.cpp b/source/Core/BSP/Pinecilv2/IRQ.cpp index 3257ae40ba..bd64a96e00 100644 --- a/source/Core/BSP/Pinecilv2/IRQ.cpp +++ b/source/Core/BSP/Pinecilv2/IRQ.cpp @@ -19,7 +19,7 @@ extern "C" { } void start_PWM_output(void); -#define ADC_Filter_Smooth 4 +#define ADC_Filter_Smooth 1 history ADC_Vin; history ADC_Temp; history ADC_Tip; @@ -67,19 +67,21 @@ void adc_fifo_irq(void) { ADC_IntClr(ADC_INT_ALL); } -static bool fastPWM = false; +volatile bool inFastPWMMode = false; + static void switchToFastPWM(void); +static void switchToSlowPWM(void); -volatile uint16_t PWMSafetyTimer = 0; -volatile uint8_t pendingPWM = 0; -volatile bool lastPeriodWasFast = false; +volatile uint16_t PWMSafetyTimer = 0; +volatile uint8_t pendingPWM = 0; +volatile bool pendingNextPeriodIsFast = false; void start_PWM_output(void) { if (PWMSafetyTimer) { PWMSafetyTimer--; - if (lastPeriodWasFast != fastPWM) { - if (fastPWM) { + if (pendingNextPeriodIsFast != inFastPWMMode) { + if (pendingNextPeriodIsFast) { switchToFastPWM(); } else { switchToSlowPWM(); @@ -96,6 +98,7 @@ void start_PWM_output(void) { } } else { PWM_Channel_Disable(PWM_Channel); + switchToFastPWM(); } TIMER_Enable(TIMER_CH0); } @@ -108,43 +111,47 @@ void timer0_comp0_callback(void) { void timer0_comp1_callback(void) { PWM_Channel_Disable(PWM_Channel); } // Trigged at end of output cycle; turn off the tip PWM void switchToFastPWM(void) { - fastPWM = true; - totalPWM = powerPWM + tempMeasureTicks + holdoffTicks; + inFastPWMMode = true; + holdoffTicks = 10; + tempMeasureTicks = 10; + totalPWM = powerPWM + tempMeasureTicks + holdoffTicks; TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM); // ~10Hz TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_0, powerPWM + holdoffTicks); - // Set divider to 11 + // Set divider to 10 ~= 10.5Hz uint32_t tmpVal = BL_RD_REG(TIMER_BASE, TIMER_TCDR); - tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 11); + tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 10); BL_WR_REG(TIMER_BASE, TIMER_TCDR, tmpVal); } void switchToSlowPWM(void) { // 5Hz - fastPWM = false; - totalPWM = powerPWM + tempMeasureTicks / 2 + holdoffTicks / 2; + inFastPWMMode = false; + holdoffTicks = 5; + tempMeasureTicks = 5; + totalPWM = powerPWM + tempMeasureTicks + holdoffTicks; TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM); // Adjust ADC - TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_0, powerPWM + (holdoffTicks / 2)); + TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_0, powerPWM + holdoffTicks); // Set divider to 22 uint32_t tmpVal = BL_RD_REG(TIMER_BASE, TIMER_TCDR); - tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 22); + tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 20); BL_WR_REG(TIMER_BASE, TIMER_TCDR, tmpVal); } void setTipPWM(const uint8_t pulse, const bool shouldUseFastModePWM) { PWMSafetyTimer = 10; // This is decremented in the handler for PWM so that the tip pwm is // disabled if the PID task is not scheduled often enough. - pendingPWM = pulse; - fastPWM = shouldUseFastModePWM; + pendingPWM = pulse; + pendingNextPeriodIsFast = shouldUseFastModePWM; } extern osThreadId POWTaskHandle; diff --git a/source/Core/BSP/Pinecilv2/IRQ.h b/source/Core/BSP/Pinecilv2/IRQ.h index 1e66b904af..4f6d6814d2 100644 --- a/source/Core/BSP/Pinecilv2/IRQ.h +++ b/source/Core/BSP/Pinecilv2/IRQ.h @@ -21,7 +21,6 @@ void timer0_comp1_callback(void); void timer0_comp2_callback(void); void adc_fifo_irq(void); void GPIO_IRQHandler(void); -void switchToSlowPWM(void); #ifdef __cplusplus } #endif diff --git a/source/Core/BSP/Pinecilv2/Setup.h b/source/Core/BSP/Pinecilv2/Setup.h index 18591d7c1e..d2ab67cbd9 100644 --- a/source/Core/BSP/Pinecilv2/Setup.h +++ b/source/Core/BSP/Pinecilv2/Setup.h @@ -29,7 +29,7 @@ uint16_t getADCVin(uint8_t sample); #ifdef __cplusplus } #endif -void setupFUSBIRQ(); -extern const uint8_t holdoffTicks; -extern const uint8_t tempMeasureTicks; +void setupFUSBIRQ(); +extern uint8_t holdoffTicks; +extern uint8_t tempMeasureTicks; #endif /* PINE_SETUP_H_ */ diff --git a/source/Core/BSP/Pinecilv2/ThermoModel.cpp b/source/Core/BSP/Pinecilv2/ThermoModel.cpp index 4515c42f7f..028a477b7f 100644 --- a/source/Core/BSP/Pinecilv2/ThermoModel.cpp +++ b/source/Core/BSP/Pinecilv2/ThermoModel.cpp @@ -69,4 +69,4 @@ const int32_t uVtoDegC[] = { const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(int32_t)); -uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); } +TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); } diff --git a/source/Core/BSP/Pinecilv2/configuration.h b/source/Core/BSP/Pinecilv2/configuration.h index 9cc198b277..3683b085cf 100644 --- a/source/Core/BSP/Pinecilv2/configuration.h +++ b/source/Core/BSP/Pinecilv2/configuration.h @@ -161,7 +161,6 @@ #define DEBUG_UART_OUTPUT #define HAS_POWER_DEBUG_MENU #define HARDWARE_MAX_WATTAGE_X10 750 -#define TIP_THERMAL_MASS 65 // X10 watts to raise 1 deg C in 1 second #define BLE_ENABLED #define NEEDS_VBUS_PROBE 0 #define CANT_DIRECT_READ_SETTINGS diff --git a/source/Core/BSP/Sequre_S60/ThermoModel.cpp b/source/Core/BSP/Sequre_S60/ThermoModel.cpp index 550726e809..90c12ab107 100644 --- a/source/Core/BSP/Sequre_S60/ThermoModel.cpp +++ b/source/Core/BSP/Sequre_S60/ThermoModel.cpp @@ -8,4 +8,4 @@ #include "Utils.h" #include "configuration.h" -uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return (tipuVDelta * 50) / 485; } +TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return (tipuVDelta * 50) / 485; } diff --git a/source/Core/Drivers/TipThermoModel.cpp b/source/Core/Drivers/TipThermoModel.cpp index ea6a0c747d..29edc08df8 100644 --- a/source/Core/Drivers/TipThermoModel.cpp +++ b/source/Core/Drivers/TipThermoModel.cpp @@ -8,6 +8,7 @@ #include "TipThermoModel.h" #include "BSP.h" #include "Settings.h" +#include "Types.h" #include "Utils.h" #include "configuration.h" #include "main.hpp" @@ -54,45 +55,41 @@ uint32_t TipThermoModel::convertTipRawADCTouV(uint16_t rawADC, bool ski return valueuV; } -uint32_t TipThermoModel::convertTipRawADCToDegC(uint16_t rawADC) { return convertuVToDegC(convertTipRawADCTouV(rawADC)); } -uint32_t TipThermoModel::convertTipRawADCToDegF(uint16_t rawADC) { return convertuVToDegF(convertTipRawADCTouV(rawADC)); } +TemperatureType_t TipThermoModel::convertTipRawADCToDegC(uint16_t rawADC) { return convertuVToDegC(convertTipRawADCTouV(rawADC)); } +TemperatureType_t TipThermoModel::convertTipRawADCToDegF(uint16_t rawADC) { return convertuVToDegF(convertTipRawADCTouV(rawADC)); } -uint32_t TipThermoModel::convertuVToDegF(uint32_t tipuVDelta) { return convertCtoF(convertuVToDegC(tipuVDelta)); } +TemperatureType_t TipThermoModel::convertuVToDegF(uint32_t tipuVDelta) { return convertCtoF(convertuVToDegC(tipuVDelta)); } -uint32_t TipThermoModel::convertCtoF(uint32_t degC) { +TemperatureType_t TipThermoModel::convertCtoF(TemperatureType_t degC) { //(Y °C × 9/5) + 32 =Y°F return (32 + ((degC * 9) / 5)); } -uint32_t TipThermoModel::convertFtoC(uint32_t degF) { +TemperatureType_t TipThermoModel::convertFtoC(TemperatureType_t degF) { //(Y°F − 32) × 5/9 = Y°C if (degF < 32) { return 0; } return ((degF - 32) * 5) / 9; } -uint32_t TipThermoModel::getTipInC(bool sampleNow) { - int32_t currentTipTempInC = TipThermoModel::convertTipRawADCToDegC(getTipRawTemp(sampleNow)); +TemperatureType_t TipThermoModel::getTipInC(bool sampleNow) { + TemperatureType_t currentTipTempInC = TipThermoModel::convertTipRawADCToDegC(getTipRawTemp(sampleNow)); currentTipTempInC += getHandleTemperature(sampleNow) / 10; // Add handle offset - // Power usage indicates that our tip temp is lower than our thermocouple temp. - // I found a number that doesn't unbalance the existing PID, causing overshoot. - // This could be tuned in concert with PID parameters... - if (currentTipTempInC < 0) { return 0; } return currentTipTempInC; } -uint32_t TipThermoModel::getTipInF(bool sampleNow) { - uint32_t currentTipTempInF = getTipInC(sampleNow); - currentTipTempInF = convertCtoF(currentTipTempInF); +TemperatureType_t TipThermoModel::getTipInF(bool sampleNow) { + TemperatureType_t currentTipTempInF = getTipInC(sampleNow); + currentTipTempInF = convertCtoF(currentTipTempInF); return currentTipTempInF; } -uint32_t TipThermoModel::getTipMaxInC() { - uint32_t maximumTipTemp = TipThermoModel::convertTipRawADCToDegC(ADC_MAX_READING - 1); +TemperatureType_t TipThermoModel::getTipMaxInC() { + TemperatureType_t maximumTipTemp = TipThermoModel::convertTipRawADCToDegC(ADC_MAX_READING - 1); maximumTipTemp += getHandleTemperature(0) / 10; // Add handle offset return maximumTipTemp - 1; } diff --git a/source/Core/Drivers/TipThermoModel.h b/source/Core/Drivers/TipThermoModel.h index b595c7364f..03a10ff0f9 100644 --- a/source/Core/Drivers/TipThermoModel.h +++ b/source/Core/Drivers/TipThermoModel.h @@ -8,26 +8,27 @@ #ifndef SRC_TIPTHERMOMODEL_H_ #define SRC_TIPTHERMOMODEL_H_ #include "BSP.h" +#include "Types.h" #include "stdint.h" class TipThermoModel { public: // These are the main two functions - static uint32_t getTipInC(bool sampleNow = false); - static uint32_t getTipInF(bool sampleNow = false); + static TemperatureType_t getTipInC(bool sampleNow = false); + static TemperatureType_t getTipInF(bool sampleNow = false); // Calculates the maximum temperature can can be read by the ADC range - static uint32_t getTipMaxInC(); + static TemperatureType_t getTipMaxInC(); - static uint32_t convertTipRawADCToDegC(uint16_t rawADC); - static uint32_t convertTipRawADCToDegF(uint16_t rawADC); + static TemperatureType_t convertTipRawADCToDegC(uint16_t rawADC); + static TemperatureType_t convertTipRawADCToDegF(uint16_t rawADC); // Returns the uV of the tip reading before the op-amp compensating for pullups - static uint32_t convertTipRawADCTouV(uint16_t rawADC, bool skipCalOffset = false); - static uint32_t convertCtoF(uint32_t degC); - static uint32_t convertFtoC(uint32_t degF); + static uint32_t convertTipRawADCTouV(uint16_t rawADC, bool skipCalOffset = false); + static TemperatureType_t convertCtoF(TemperatureType_t degC); + static TemperatureType_t convertFtoC(TemperatureType_t degF); private: - static uint32_t convertuVToDegC(uint32_t tipuVDelta); - static uint32_t convertuVToDegF(uint32_t tipuVDelta); + static TemperatureType_t convertuVToDegC(uint32_t tipuVDelta); + static TemperatureType_t convertuVToDegF(uint32_t tipuVDelta); }; #endif /* SRC_TIPTHERMOMODEL_H_ */ diff --git a/source/Core/Inc/Types.h b/source/Core/Inc/Types.h new file mode 100644 index 0000000000..145cd6beb3 --- /dev/null +++ b/source/Core/Inc/Types.h @@ -0,0 +1,10 @@ +#ifndef TYPES_H_ +#define TYPES_H_ +#include + +// Used for temperature represented in C or x10C. +// + +typedef int32_t TemperatureType_t; + +#endif \ No newline at end of file diff --git a/source/Core/Inc/main.hpp b/source/Core/Inc/main.hpp index bd2eef417b..0ffac19dca 100644 --- a/source/Core/Inc/main.hpp +++ b/source/Core/Inc/main.hpp @@ -2,10 +2,11 @@ #define __MAIN_H #include "OLED.hpp" #include "Setup.h" +#include "Types.h" #include -extern volatile uint32_t currentTempTargetDegC; -extern bool settingsWereReset; -extern bool usb_pd_available; +extern volatile TemperatureType_t currentTempTargetDegC; +extern bool settingsWereReset; +extern bool usb_pd_available; #ifdef __cplusplus extern "C" { #endif diff --git a/source/Core/Inc/power.hpp b/source/Core/Inc/power.hpp index f8d35f67b1..432ad16695 100644 --- a/source/Core/Inc/power.hpp +++ b/source/Core/Inc/power.hpp @@ -23,7 +23,6 @@ const uint8_t wattHistoryFilter = 24; // extern expMovingAverage x10WattHistory; uint32_t availableW10(uint8_t sample); -int32_t tempToX10Watts(int32_t rawTemp); void setTipX10Watts(int32_t mw); uint8_t X10WattsToPWM(int32_t milliWatts, uint8_t sample = 0); #endif /* POWER_HPP_ */ diff --git a/source/Core/Src/power.cpp b/source/Core/Src/power.cpp index 88d0509b80..708014ed1a 100644 --- a/source/Core/Src/power.cpp +++ b/source/Core/Src/power.cpp @@ -27,14 +27,6 @@ bool shouldBeUsingFastPWMMode(const uint8_t pwmTicks) { return lastPWMWasFast; } -int32_t tempToX10Watts(int32_t rawTemp) { - // mass is in x10J/*C, rawC is raw per degree C - // returns x10Watts needed to raise/lower a mass by rawTemp - // degrees in one cycle. - int32_t x10Watts = TIP_THERMAL_MASS * rawTemp; - return x10Watts; -} - void setTipX10Watts(int32_t mw) { int32_t outputPWMLevel = X10WattsToPWM(mw, 1); const bool shouldUseFastPWM = shouldBeUsingFastPWMMode(outputPWMLevel); diff --git a/source/Core/Threads/OperatingModes/utils/checkUndervoltage.cpp b/source/Core/Threads/OperatingModes/utils/checkUndervoltage.cpp index 15d8f61329..158e88427c 100644 --- a/source/Core/Threads/OperatingModes/utils/checkUndervoltage.cpp +++ b/source/Core/Threads/OperatingModes/utils/checkUndervoltage.cpp @@ -2,7 +2,7 @@ #include "OperatingModeUtilities.h" #include "configuration.h" #ifdef POW_DC -extern volatile uint32_t currentTempTargetDegC; +extern volatile TemperatureType_t currentTempTargetDegC; // returns true if undervoltage has occured bool checkForUnderVoltage(void) { if (!getIsPoweredByDCIN()) { diff --git a/source/Core/Threads/PIDThread.cpp b/source/Core/Threads/PIDThread.cpp index 026a414c7b..7118273b1b 100644 --- a/source/Core/Threads/PIDThread.cpp +++ b/source/Core/Threads/PIDThread.cpp @@ -15,15 +15,15 @@ #include "power.hpp" #include "task.h" -static TickType_t powerPulseWaitUnit = 25 * TICKS_100MS; // 2.5 s -static TickType_t powerPulseDurationUnit = (5 * TICKS_100MS) / 2; // 250 ms -TaskHandle_t pidTaskNotification = NULL; -volatile uint32_t currentTempTargetDegC = 0; // Current temperature target in C -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 TickType_t powerPulseWaitUnit = 25 * TICKS_100MS; // 2.5 s +static TickType_t powerPulseDurationUnit = (5 * TICKS_100MS) / 2; // 250 ms +TaskHandle_t pidTaskNotification = NULL; +volatile TemperatureType_t currentTempTargetDegC = 0; // Current temperature target in C +int32_t powerSupplyWattageLimit = 0; +bool heaterThermalRunaway = false; + +static int32_t getPIDResultX10Watts(TemperatureType_t tError); +static void detectThermalRunaway(const TemperatureType_t currentTipTempInC, const TemperatureType_t tError); static void setOutputx10WattsViaFilters(int32_t x10Watts); static int32_t getX10WattageLimits(); @@ -37,8 +37,8 @@ void startPIDTask(void const *argument __unused) { currentTempTargetDegC = 0; // Force start with no output (off). If in sleep / soldering this will // be over-ridden rapidly - pidTaskNotification = xTaskGetCurrentTaskHandle(); - uint32_t PIDTempTarget = 0; + pidTaskNotification = xTaskGetCurrentTaskHandle(); + TemperatureType_t PIDTempTarget = 0; // Pre-seed the adc filters for (int i = 0; i < 32; i++) { ulTaskNotifyTake(pdTRUE, 5); @@ -58,19 +58,20 @@ void startPIDTask(void const *argument __unused) { // This is a call to block this thread until the ADC does its samples if (ulTaskNotifyTake(pdTRUE, TICKS_SECOND * 2)) { // Do the reading here to keep the temp calculations churning along - uint32_t currentTipTempInC = TipThermoModel::getTipInC(true); - PIDTempTarget = currentTempTargetDegC; + TemperatureType_t currentTipTempInC = TipThermoModel::getTipInC(true); + + PIDTempTarget = currentTempTargetDegC; if (PIDTempTarget > 0) { // Cap the max set point to 450C - if (PIDTempTarget > (450)) { + if (PIDTempTarget > 450) { // Maximum allowed output - PIDTempTarget = (450); + PIDTempTarget = 450; } // Safety check that not aiming higher than current tip can measure if (PIDTempTarget > TipThermoModel::getTipMaxInC()) { PIDTempTarget = TipThermoModel::getTipMaxInC(); } - int32_t tError = PIDTempTarget - currentTipTempInC; + TemperatureType_t tError = PIDTempTarget - currentTipTempInC; detectThermalRunaway(currentTipTempInC, tError); x10WattsOut = getPIDResultX10Watts(tError); @@ -88,7 +89,7 @@ void startPIDTask(void const *argument __unused) { } } -template struct Integrator { +template struct Integrator { T sum; T update(const T val, const int32_t inertia, const int32_t gain, const int32_t rate, const int32_t limit) { @@ -99,11 +100,12 @@ template struct Integrator { // Add the new value x integration interval ( 1 / rate) sum += (gain * val) / rate; - // limit the output - if (sum > limit) + // constrain the output between +- our max power output, this limits windup when doing the inital heatup or when solding something large + if (sum > limit) { sum = limit; - else if (sum < -limit) + } else if (sum < -limit) { sum = -limit; + } return sum; } @@ -112,15 +114,15 @@ template struct Integrator { T get(bool positiveOnly = true) const { return (positiveOnly) ? ((sum > 0) ? sum : 0) : sum; } }; -int32_t getPIDResultX10Watts(int32_t setpointDelta) { - static TickType_t lastCall = 0; - static Integrator powerStore = {0}; +int32_t getPIDResultX10Watts(TemperatureType_t setpointDelta) { + static TickType_t lastCall = 0; + static Integrator powerStore = {0}; const TickType_t rate = TICKS_SECOND / (xTaskGetTickCount() - lastCall); lastCall = xTaskGetTickCount(); // Sandman note: // PID Challenge - we have a small thermal mass that we to want heat up as fast as possible but we don't - // want to overshot excessively (if at all) the setpoint temperature. In the same time we have 'imprecise' + // want to overshot excessively (if at all) the set point temperature. In the same time we have 'imprecise' // instant temperature measurements. The nature of temperature reading imprecision is not necessarily // related to the sensor (thermocouple) or DAQ system, that otherwise are fairly decent. The real issue is // the thermal inertia. We basically read the temperature in the window between two heating sessions when @@ -130,7 +132,7 @@ int32_t getPIDResultX10Watts(int32_t setpointDelta) { // negative side effects. As a result, we can only rely on the I term but with a twist. Instead of a simple // integrator we are going to use a self decaying integrator that acts more like a dual I term / P term // rather than a plain I term. Depending on the circumstances, like when the delta temperature is large, - // it acts more like a P term whereas on closing to setpoint it acts increasingly closer to a plain I term. + // it acts more like a P term whereas on closing to set point it acts increasingly closer to a plain I term. // So in a sense, we have a bit of both. // So there we go... @@ -138,20 +140,17 @@ int32_t getPIDResultX10Watts(int32_t setpointDelta) { // delta temperature is in °C. The result is the power in X10 W needed to raise (or decrease!) the // tip temperature with (Delta Temperature ) °C in 1 second. // Note on powerStore. On update, if the value is provided in X10 (W) units then inertia shall be provided - // in X10 (J / °C) units as well. Also, powerStore is updated with a gain of 2. Where this comes from: The actual - // power CMOS is controlled by TIM3->CTR1 (that is software modulated - on/off - by TIM2-CTR4 interrupts). However, - // TIM3->CTR1 is configured with a duty cycle of 50% so, in real, we get only 50% of the presumed power output - // so we basically double the need (gain = 2) to get what we want. - return powerStore.update(getTipThermalMass() * setpointDelta, // the required power - getTipInertia(), // Inertia, smaller numbers increase dominance of the previous value - 2, // gain - rate, // PID cycle frequency + // in X10 (J / °C) units as well. + return powerStore.update(((TemperatureType_t)getTipThermalMass()) * setpointDelta, // the required power + getTipInertia(), // Inertia, smaller numbers increase dominance of the previous value + 2, // gain + rate, // PID cycle frequency getX10WattageLimits()); } -void detectThermalRunaway(const int16_t currentTipTempInC, const int tError) { - static uint16_t tipTempCRunawayTemp = 0; - static TickType_t runawaylastChangeTime = 0; +void detectThermalRunaway(const TemperatureType_t currentTipTempInC, const TemperatureType_t tError) { + static TemperatureType_t tipTempCRunawayTemp = 0; + static TickType_t runawaylastChangeTime = 0; // Check for thermal runaway, where it has been x seconds with negligible (y) temp rise // While trying to actively heat @@ -160,7 +159,7 @@ void detectThermalRunaway(const int16_t currentTipTempInC, const int tError) { if ((tError > THERMAL_RUNAWAY_TEMP_C)) { // If we have heated up by more than 20C since last sample point, snapshot time and tip temp - int16_t delta = (int16_t)currentTipTempInC - (int16_t)tipTempCRunawayTemp; + TemperatureType_t delta = currentTipTempInC - tipTempCRunawayTemp; if (delta > THERMAL_RUNAWAY_TEMP_C) { // We have heated up more than the threshold, reset the timer tipTempCRunawayTemp = currentTipTempInC;