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
This commit is contained in:
Ben V. Brown
2023-09-22 10:19:50 +10:00
committed by GitHub
parent f99aed5785
commit c0a5e244b9
19 changed files with 116 additions and 108 deletions

View File

@@ -3,9 +3,11 @@
#include "BSP_Power.h" #include "BSP_Power.h"
#include "BSP_QC.h" #include "BSP_QC.h"
#include "Defines.h" #include "Defines.h"
#include "Types.h"
#include "configuration.h" #include "configuration.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
/* /*
* BSP.h -- Board Support * BSP.h -- Board Support
* *

View File

@@ -6,10 +6,12 @@
*/ */
#include "Setup.h" #include "Setup.h"
#include "TipThermoModel.h" #include "TipThermoModel.h"
#include "Types.h"
#include "Utils.h" #include "Utils.h"
#include "configuration.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, // 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 // this is measured at boot in pid task and in the disconnected tip check if tip is removed
if (tipSenseResistancex10Ohms > 900 && tipSenseResistancex10Ohms <= 1100) { if (tipSenseResistancex10Ohms > 900 && tipSenseResistancex10Ohms <= 1100) {

View File

@@ -127,4 +127,4 @@ const int32_t uVtoDegC[] = {
#endif #endif
const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(uVtoDegC[0])); 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); }

View File

@@ -69,4 +69,4 @@ const int32_t uVtoDegC[] = {
const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(uVtoDegC[0])); 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); }

View File

@@ -17,8 +17,8 @@
// These control the period's of time used for the PWM // These control the period's of time used for the PWM
const uint16_t powerPWM = 255; 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) 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 tempMeasureTicks = 25;
uint16_t totalPWM = 255; // Total length of the cycle's ticks uint16_t totalPWM = 255; // Total length of the cycle's ticks
@@ -162,13 +162,13 @@ uint8_t getTipResistanceX10() {
uint8_t getTipThermalMass() { uint8_t getTipThermalMass() {
if (lastTipResistance >= 80) { if (lastTipResistance >= 80) {
return TIP_THERMAL_MASS; return 65;
} }
return 45; return 45;
} }
uint8_t getTipInertia() { uint8_t getTipInertia() {
if (lastTipResistance >= 80) { if (lastTipResistance >= 80) {
return TIP_THERMAL_MASS; return 90;
} }
return 10; return 10;
} }

View File

@@ -19,7 +19,7 @@ extern "C" {
} }
void start_PWM_output(void); void start_PWM_output(void);
#define ADC_Filter_Smooth 4 #define ADC_Filter_Smooth 1
history<uint16_t, ADC_Filter_Smooth> ADC_Vin; history<uint16_t, ADC_Filter_Smooth> ADC_Vin;
history<uint16_t, ADC_Filter_Smooth> ADC_Temp; history<uint16_t, ADC_Filter_Smooth> ADC_Temp;
history<uint16_t, ADC_Filter_Smooth> ADC_Tip; history<uint16_t, ADC_Filter_Smooth> ADC_Tip;
@@ -67,19 +67,21 @@ void adc_fifo_irq(void) {
ADC_IntClr(ADC_INT_ALL); ADC_IntClr(ADC_INT_ALL);
} }
static bool fastPWM = false; volatile bool inFastPWMMode = false;
static void switchToFastPWM(void);
volatile uint16_t PWMSafetyTimer = 0; static void switchToFastPWM(void);
volatile uint8_t pendingPWM = 0; static void switchToSlowPWM(void);
volatile bool lastPeriodWasFast = false;
volatile uint16_t PWMSafetyTimer = 0;
volatile uint8_t pendingPWM = 0;
volatile bool pendingNextPeriodIsFast = false;
void start_PWM_output(void) { void start_PWM_output(void) {
if (PWMSafetyTimer) { if (PWMSafetyTimer) {
PWMSafetyTimer--; PWMSafetyTimer--;
if (lastPeriodWasFast != fastPWM) { if (pendingNextPeriodIsFast != inFastPWMMode) {
if (fastPWM) { if (pendingNextPeriodIsFast) {
switchToFastPWM(); switchToFastPWM();
} else { } else {
switchToSlowPWM(); switchToSlowPWM();
@@ -96,6 +98,7 @@ void start_PWM_output(void) {
} }
} else { } else {
PWM_Channel_Disable(PWM_Channel); PWM_Channel_Disable(PWM_Channel);
switchToFastPWM();
} }
TIMER_Enable(TIMER_CH0); 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 timer0_comp1_callback(void) { PWM_Channel_Disable(PWM_Channel); } // Trigged at end of output cycle; turn off the tip PWM
void switchToFastPWM(void) { void switchToFastPWM(void) {
fastPWM = true; inFastPWMMode = true;
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks; holdoffTicks = 10;
tempMeasureTicks = 10;
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM); TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM);
// ~10Hz // ~10Hz
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_0, powerPWM + holdoffTicks); 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); 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); BL_WR_REG(TIMER_BASE, TIMER_TCDR, tmpVal);
} }
void switchToSlowPWM(void) { void switchToSlowPWM(void) {
// 5Hz // 5Hz
fastPWM = false; inFastPWMMode = false;
totalPWM = powerPWM + tempMeasureTicks / 2 + holdoffTicks / 2; holdoffTicks = 5;
tempMeasureTicks = 5;
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM); TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM);
// Adjust ADC // 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 // Set divider to 22
uint32_t tmpVal = BL_RD_REG(TIMER_BASE, TIMER_TCDR); 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); BL_WR_REG(TIMER_BASE, TIMER_TCDR, tmpVal);
} }
void setTipPWM(const uint8_t pulse, const bool shouldUseFastModePWM) { 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 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. // disabled if the PID task is not scheduled often enough.
pendingPWM = pulse; pendingPWM = pulse;
fastPWM = shouldUseFastModePWM; pendingNextPeriodIsFast = shouldUseFastModePWM;
} }
extern osThreadId POWTaskHandle; extern osThreadId POWTaskHandle;

View File

@@ -21,7 +21,6 @@ void timer0_comp1_callback(void);
void timer0_comp2_callback(void); void timer0_comp2_callback(void);
void adc_fifo_irq(void); void adc_fifo_irq(void);
void GPIO_IRQHandler(void); void GPIO_IRQHandler(void);
void switchToSlowPWM(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -29,7 +29,7 @@ uint16_t getADCVin(uint8_t sample);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
void setupFUSBIRQ(); void setupFUSBIRQ();
extern const uint8_t holdoffTicks; extern uint8_t holdoffTicks;
extern const uint8_t tempMeasureTicks; extern uint8_t tempMeasureTicks;
#endif /* PINE_SETUP_H_ */ #endif /* PINE_SETUP_H_ */

View File

@@ -69,4 +69,4 @@ const int32_t uVtoDegC[] = {
const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(int32_t)); 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); }

View File

@@ -161,7 +161,6 @@
#define DEBUG_UART_OUTPUT #define DEBUG_UART_OUTPUT
#define HAS_POWER_DEBUG_MENU #define HAS_POWER_DEBUG_MENU
#define HARDWARE_MAX_WATTAGE_X10 750 #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 BLE_ENABLED
#define NEEDS_VBUS_PROBE 0 #define NEEDS_VBUS_PROBE 0
#define CANT_DIRECT_READ_SETTINGS #define CANT_DIRECT_READ_SETTINGS

View File

@@ -8,4 +8,4 @@
#include "Utils.h" #include "Utils.h"
#include "configuration.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; }

View File

@@ -8,6 +8,7 @@
#include "TipThermoModel.h" #include "TipThermoModel.h"
#include "BSP.h" #include "BSP.h"
#include "Settings.h" #include "Settings.h"
#include "Types.h"
#include "Utils.h" #include "Utils.h"
#include "configuration.h" #include "configuration.h"
#include "main.hpp" #include "main.hpp"
@@ -54,45 +55,41 @@ uint32_t TipThermoModel::convertTipRawADCTouV(uint16_t rawADC, bool ski
return valueuV; return valueuV;
} }
uint32_t TipThermoModel::convertTipRawADCToDegC(uint16_t rawADC) { return convertuVToDegC(convertTipRawADCTouV(rawADC)); } TemperatureType_t TipThermoModel::convertTipRawADCToDegC(uint16_t rawADC) { return convertuVToDegC(convertTipRawADCTouV(rawADC)); }
uint32_t TipThermoModel::convertTipRawADCToDegF(uint16_t rawADC) { return convertuVToDegF(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 //(Y °C × 9/5) + 32 =Y°F
return (32 + ((degC * 9) / 5)); 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 //(Y°F 32) × 5/9 = Y°C
if (degF < 32) { if (degF < 32) {
return 0; return 0;
} }
return ((degF - 32) * 5) / 9; return ((degF - 32) * 5) / 9;
} }
uint32_t TipThermoModel::getTipInC(bool sampleNow) { TemperatureType_t TipThermoModel::getTipInC(bool sampleNow) {
int32_t currentTipTempInC = TipThermoModel::convertTipRawADCToDegC(getTipRawTemp(sampleNow)); TemperatureType_t currentTipTempInC = TipThermoModel::convertTipRawADCToDegC(getTipRawTemp(sampleNow));
currentTipTempInC += getHandleTemperature(sampleNow) / 10; // Add handle offset 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) { if (currentTipTempInC < 0) {
return 0; return 0;
} }
return currentTipTempInC; return currentTipTempInC;
} }
uint32_t TipThermoModel::getTipInF(bool sampleNow) { TemperatureType_t TipThermoModel::getTipInF(bool sampleNow) {
uint32_t currentTipTempInF = getTipInC(sampleNow); TemperatureType_t currentTipTempInF = getTipInC(sampleNow);
currentTipTempInF = convertCtoF(currentTipTempInF); currentTipTempInF = convertCtoF(currentTipTempInF);
return currentTipTempInF; return currentTipTempInF;
} }
uint32_t TipThermoModel::getTipMaxInC() { TemperatureType_t TipThermoModel::getTipMaxInC() {
uint32_t maximumTipTemp = TipThermoModel::convertTipRawADCToDegC(ADC_MAX_READING - 1); TemperatureType_t maximumTipTemp = TipThermoModel::convertTipRawADCToDegC(ADC_MAX_READING - 1);
maximumTipTemp += getHandleTemperature(0) / 10; // Add handle offset maximumTipTemp += getHandleTemperature(0) / 10; // Add handle offset
return maximumTipTemp - 1; return maximumTipTemp - 1;
} }

View File

@@ -8,26 +8,27 @@
#ifndef SRC_TIPTHERMOMODEL_H_ #ifndef SRC_TIPTHERMOMODEL_H_
#define SRC_TIPTHERMOMODEL_H_ #define SRC_TIPTHERMOMODEL_H_
#include "BSP.h" #include "BSP.h"
#include "Types.h"
#include "stdint.h" #include "stdint.h"
class TipThermoModel { class TipThermoModel {
public: public:
// These are the main two functions // These are the main two functions
static uint32_t getTipInC(bool sampleNow = false); static TemperatureType_t getTipInC(bool sampleNow = false);
static uint32_t getTipInF(bool sampleNow = false); static TemperatureType_t getTipInF(bool sampleNow = false);
// Calculates the maximum temperature can can be read by the ADC range // 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 TemperatureType_t convertTipRawADCToDegC(uint16_t rawADC);
static uint32_t convertTipRawADCToDegF(uint16_t rawADC); static TemperatureType_t convertTipRawADCToDegF(uint16_t rawADC);
// Returns the uV of the tip reading before the op-amp compensating for pullups // 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 convertTipRawADCTouV(uint16_t rawADC, bool skipCalOffset = false);
static uint32_t convertCtoF(uint32_t degC); static TemperatureType_t convertCtoF(TemperatureType_t degC);
static uint32_t convertFtoC(uint32_t degF); static TemperatureType_t convertFtoC(TemperatureType_t degF);
private: private:
static uint32_t convertuVToDegC(uint32_t tipuVDelta); static TemperatureType_t convertuVToDegC(uint32_t tipuVDelta);
static uint32_t convertuVToDegF(uint32_t tipuVDelta); static TemperatureType_t convertuVToDegF(uint32_t tipuVDelta);
}; };
#endif /* SRC_TIPTHERMOMODEL_H_ */ #endif /* SRC_TIPTHERMOMODEL_H_ */

10
source/Core/Inc/Types.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef TYPES_H_
#define TYPES_H_
#include <stddef.h>
// Used for temperature represented in C or x10C.
//
typedef int32_t TemperatureType_t;
#endif

View File

@@ -2,10 +2,11 @@
#define __MAIN_H #define __MAIN_H
#include "OLED.hpp" #include "OLED.hpp"
#include "Setup.h" #include "Setup.h"
#include "Types.h"
#include <stdint.h> #include <stdint.h>
extern volatile uint32_t currentTempTargetDegC; extern volatile TemperatureType_t currentTempTargetDegC;
extern bool settingsWereReset; extern bool settingsWereReset;
extern bool usb_pd_available; extern bool usb_pd_available;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@@ -23,7 +23,6 @@ const uint8_t wattHistoryFilter = 24; //
extern expMovingAverage<uint32_t, wattHistoryFilter> x10WattHistory; extern expMovingAverage<uint32_t, wattHistoryFilter> x10WattHistory;
uint32_t availableW10(uint8_t sample); uint32_t availableW10(uint8_t sample);
int32_t tempToX10Watts(int32_t rawTemp);
void setTipX10Watts(int32_t mw); void setTipX10Watts(int32_t mw);
uint8_t X10WattsToPWM(int32_t milliWatts, uint8_t sample = 0); uint8_t X10WattsToPWM(int32_t milliWatts, uint8_t sample = 0);
#endif /* POWER_HPP_ */ #endif /* POWER_HPP_ */

View File

@@ -27,14 +27,6 @@ bool shouldBeUsingFastPWMMode(const uint8_t pwmTicks) {
return lastPWMWasFast; 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) { void setTipX10Watts(int32_t mw) {
int32_t outputPWMLevel = X10WattsToPWM(mw, 1); int32_t outputPWMLevel = X10WattsToPWM(mw, 1);
const bool shouldUseFastPWM = shouldBeUsingFastPWMMode(outputPWMLevel); const bool shouldUseFastPWM = shouldBeUsingFastPWMMode(outputPWMLevel);

View File

@@ -2,7 +2,7 @@
#include "OperatingModeUtilities.h" #include "OperatingModeUtilities.h"
#include "configuration.h" #include "configuration.h"
#ifdef POW_DC #ifdef POW_DC
extern volatile uint32_t currentTempTargetDegC; extern volatile TemperatureType_t currentTempTargetDegC;
// returns true if undervoltage has occured // returns true if undervoltage has occured
bool checkForUnderVoltage(void) { bool checkForUnderVoltage(void) {
if (!getIsPoweredByDCIN()) { if (!getIsPoweredByDCIN()) {

View File

@@ -15,15 +15,15 @@
#include "power.hpp" #include "power.hpp"
#include "task.h" #include "task.h"
static TickType_t powerPulseWaitUnit = 25 * TICKS_100MS; // 2.5 s static TickType_t powerPulseWaitUnit = 25 * TICKS_100MS; // 2.5 s
static TickType_t powerPulseDurationUnit = (5 * TICKS_100MS) / 2; // 250 ms static TickType_t powerPulseDurationUnit = (5 * TICKS_100MS) / 2; // 250 ms
TaskHandle_t pidTaskNotification = NULL; TaskHandle_t pidTaskNotification = NULL;
volatile uint32_t currentTempTargetDegC = 0; // Current temperature target in C volatile TemperatureType_t currentTempTargetDegC = 0; // Current temperature target in C
int32_t powerSupplyWattageLimit = 0; int32_t powerSupplyWattageLimit = 0;
bool heaterThermalRunaway = false; bool heaterThermalRunaway = false;
static int32_t getPIDResultX10Watts(int32_t tError); static int32_t getPIDResultX10Watts(TemperatureType_t tError);
static void detectThermalRunaway(const int16_t currentTipTempInC, const int tError); static void detectThermalRunaway(const TemperatureType_t currentTipTempInC, const TemperatureType_t tError);
static void setOutputx10WattsViaFilters(int32_t x10Watts); static void setOutputx10WattsViaFilters(int32_t x10Watts);
static int32_t getX10WattageLimits(); 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 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();
uint32_t PIDTempTarget = 0; TemperatureType_t PIDTempTarget = 0;
// Pre-seed the adc filters // Pre-seed the adc filters
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
ulTaskNotifyTake(pdTRUE, 5); 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 // This is a call to block this thread until the ADC does its samples
if (ulTaskNotifyTake(pdTRUE, TICKS_SECOND * 2)) { if (ulTaskNotifyTake(pdTRUE, TICKS_SECOND * 2)) {
// 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); TemperatureType_t currentTipTempInC = TipThermoModel::getTipInC(true);
PIDTempTarget = currentTempTargetDegC;
PIDTempTarget = currentTempTargetDegC;
if (PIDTempTarget > 0) { if (PIDTempTarget > 0) {
// Cap the max set point to 450C // Cap the max set point to 450C
if (PIDTempTarget > (450)) { if (PIDTempTarget > 450) {
// Maximum allowed output // Maximum allowed output
PIDTempTarget = (450); PIDTempTarget = 450;
} }
// Safety check that not aiming higher than current tip can measure // Safety check that not aiming higher than current tip can measure
if (PIDTempTarget > TipThermoModel::getTipMaxInC()) { if (PIDTempTarget > TipThermoModel::getTipMaxInC()) {
PIDTempTarget = TipThermoModel::getTipMaxInC(); PIDTempTarget = TipThermoModel::getTipMaxInC();
} }
int32_t tError = PIDTempTarget - currentTipTempInC; TemperatureType_t tError = PIDTempTarget - currentTipTempInC;
detectThermalRunaway(currentTipTempInC, tError); detectThermalRunaway(currentTipTempInC, tError);
x10WattsOut = getPIDResultX10Watts(tError); x10WattsOut = getPIDResultX10Watts(tError);
@@ -88,7 +89,7 @@ void startPIDTask(void const *argument __unused) {
} }
} }
template <class T = int32_t> struct Integrator { template <class T = TemperatureType_t> struct Integrator {
T sum; T sum;
T update(const T val, const int32_t inertia, const int32_t gain, const int32_t rate, const int32_t limit) { 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 <class T = int32_t> struct Integrator {
// Add the new value x integration interval ( 1 / rate) // Add the new value x integration interval ( 1 / rate)
sum += (gain * val) / rate; sum += (gain * val) / rate;
// limit the output // constrain the output between +- our max power output, this limits windup when doing the inital heatup or when solding something large
if (sum > limit) if (sum > limit) {
sum = limit; sum = limit;
else if (sum < -limit) } else if (sum < -limit) {
sum = -limit; sum = -limit;
}
return sum; return sum;
} }
@@ -112,15 +114,15 @@ template <class T = int32_t> struct Integrator {
T get(bool positiveOnly = true) const { return (positiveOnly) ? ((sum > 0) ? sum : 0) : sum; } T get(bool positiveOnly = true) const { return (positiveOnly) ? ((sum > 0) ? sum : 0) : sum; }
}; };
int32_t getPIDResultX10Watts(int32_t setpointDelta) { int32_t getPIDResultX10Watts(TemperatureType_t setpointDelta) {
static TickType_t lastCall = 0; static TickType_t lastCall = 0;
static Integrator<int32_t> powerStore = {0}; static Integrator<TemperatureType_t> powerStore = {0};
const TickType_t rate = TICKS_SECOND / (xTaskGetTickCount() - lastCall); const TickType_t rate = TICKS_SECOND / (xTaskGetTickCount() - lastCall);
lastCall = xTaskGetTickCount(); lastCall = xTaskGetTickCount();
// Sandman note: // Sandman note:
// PID Challenge - we have a small thermal mass that we to want heat up as fast as possible but we don't // 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 // 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 // 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 // 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 // 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 // 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, // 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 in a sense, we have a bit of both.
// So there we go... // 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 // 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. // 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 // 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 // in X10 (J / °C) units as well.
// power CMOS is controlled by TIM3->CTR1 (that is software modulated - on/off - by TIM2-CTR4 interrupts). However, return powerStore.update(((TemperatureType_t)getTipThermalMass()) * setpointDelta, // the required power
// TIM3->CTR1 is configured with a duty cycle of 50% so, in real, we get only 50% of the presumed power output getTipInertia(), // Inertia, smaller numbers increase dominance of the previous value
// so we basically double the need (gain = 2) to get what we want. 2, // gain
return powerStore.update(getTipThermalMass() * setpointDelta, // the required power rate, // PID cycle frequency
getTipInertia(), // Inertia, smaller numbers increase dominance of the previous value
2, // gain
rate, // PID cycle frequency
getX10WattageLimits()); getX10WattageLimits());
} }
void detectThermalRunaway(const int16_t currentTipTempInC, const int tError) { void detectThermalRunaway(const TemperatureType_t currentTipTempInC, const TemperatureType_t tError) {
static uint16_t tipTempCRunawayTemp = 0; static TemperatureType_t tipTempCRunawayTemp = 0;
static TickType_t runawaylastChangeTime = 0; static TickType_t runawaylastChangeTime = 0;
// Check for thermal runaway, where it has been x seconds with negligible (y) temp rise // Check for thermal runaway, where it has been x seconds with negligible (y) temp rise
// While trying to actively heat // 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 ((tError > THERMAL_RUNAWAY_TEMP_C)) {
// If we have heated up by more than 20C since last sample point, snapshot time and tip temp // 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) { if (delta > THERMAL_RUNAWAY_TEMP_C) {
// We have heated up more than the threshold, reset the timer // We have heated up more than the threshold, reset the timer
tipTempCRunawayTemp = currentTipTempInC; tipTempCRunawayTemp = currentTipTempInC;