From 1b0c3bcb67dd56f8aa574fdeac7a9244c8eba0d2 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Tue, 14 Sep 2021 21:48:58 +1000 Subject: [PATCH] Cleaning up fast/slow PWM change --- source/Core/Inc/configuration.h | 25 +++++++++++-------- source/Core/Src/power.cpp | 43 ++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/source/Core/Inc/configuration.h b/source/Core/Inc/configuration.h index 0f85e170..d4864556 100644 --- a/source/Core/Inc/configuration.h +++ b/source/Core/Inc/configuration.h @@ -227,28 +227,33 @@ #endif #ifdef MODEL_TS100 -const int32_t tipMass = 65; // X10 watts to raise 1 deg C in 1 second -const uint8_t tipResistance = 75; // x10 ohms, 7.5 typical for ts100 tips +const int32_t HARDWARE_MAX_WATTAGE_X10 = 750; +const int32_t TIP_THERMAL_MASS = 65; // X10 watts to raise 1 deg C in 1 second +const uint8_t tipResistance = 75; // x10 ohms, 7.5 typical for ts100 tips #endif #ifdef MODEL_Pinecil -const int32_t tipMass = 45; // X10 watts to raise 1 deg C in 1 second -const uint8_t tipResistance = 75; // x10 ohms, 7.5 typical for ts100 tips +const int32_t HARDWARE_MAX_WATTAGE_X10 = 750; +const int32_t TIP_THERMAL_MASS = 65; // X10 watts to raise 1 deg C in 1 second +const uint8_t tipResistance = 75; // x10 ohms, 7.5 typical for ts100 tips #endif #ifdef MODEL_TS80 -const uint32_t tipMass = 40; -const uint8_t tipResistance = 45; // x10 ohms, 4.5 typical for ts80 tips +const int32_t HARDWARE_MAX_WATTAGE_X10 = 180; +const uint32_t TIP_THERMAL_MASS = 40; +const uint8_t tipResistance = 45; // x10 ohms, 4.5 typical for ts80 tips #endif #ifdef MODEL_TS80P -const uint32_t tipMass = 40; -const uint8_t tipResistance = 45; // x10 ohms, 4.5 typical for ts80 tips +const int32_t HARDWARE_MAX_WATTAGE_X10 = 300; +const uint32_t TIP_THERMAL_MASS = 40; +const uint8_t tipResistance = 45; // x10 ohms, 4.5 typical for ts80 tips #endif #ifdef MODEL_MHP30 -const uint32_t tipMass = 45; // TODO -const uint8_t tipResistance = 60; // x10 ohms, ~6 typical +const int32_t HARDWARE_MAX_WATTAGE_X10 = 650; +const uint32_t TIP_THERMAL_MASS = 65; // TODO, needs refinement +const uint8_t tipResistance = 60; // x10 ohms, ~6 typical #endif #ifdef POW_QC_20V diff --git a/source/Core/Src/power.cpp b/source/Core/Src/power.cpp index 97e4472a..dec27147 100644 --- a/source/Core/Src/power.cpp +++ b/source/Core/Src/power.cpp @@ -13,18 +13,31 @@ static int32_t PWMToX10Watts(uint8_t pwm, uint8_t sample); expMovingAverage x10WattHistory = {0}; +bool shouldBeUsingFastPWMMode(const uint8_t pwmTicks) { + // Determine if we should use slow or fast PWM mode + // Crossover between modes set around the midpoint of the PWM control point + static bool lastPWMWasFast = true; + if (pwmTicks > (128 + 8) && lastPWMWasFast) { + lastPWMWasFast = false; + } else if (pwmTicks < (128 - 8) && !lastPWMWasFast) { + lastPWMWasFast = true; + } + return lastPWMWasFast; +} + int32_t tempToX10Watts(int32_t rawTemp) { - // mass is in milliJ/*C, rawC is raw per degree C - // returns milliWatts needed to raise/lower a mass by 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 milliJoules = tipMass * rawTemp; - return milliJoules; + int32_t x10Watts = TIP_THERMAL_MASS * rawTemp; + return x10Watts; } void setTipX10Watts(int32_t mw) { - int32_t output = X10WattsToPWM(mw, 1); - setTipPWM(output); - uint32_t actualMilliWatts = PWMToX10Watts(output, 0); + int32_t outputPWMLevel = X10WattsToPWM(mw, 1); + const bool shouldUseFastPWM = shouldBeUsingFastPWMMode(outputPWMLevel); + setTipPWM(outputPWMLevel, shouldUseFastPWM); + uint32_t actualMilliWatts = PWMToX10Watts(outputPWMLevel, 0); x10WattHistory.update(actualMilliWatts); } @@ -45,7 +58,6 @@ static uint32_t availableW10(uint8_t sample) { // availableMilliWattsX10 is now an accurate representation return availableWattsX10; } - uint8_t X10WattsToPWM(int32_t milliWatts, uint8_t sample) { // Scale input milliWatts to the pwm range available if (milliWatts < 1) { @@ -56,15 +68,12 @@ uint8_t X10WattsToPWM(int32_t milliWatts, uint8_t sample) { // Calculate desired milliwatts as a percentage of availableW10 uint32_t pwm; - do { - pwm = (powerPWM * milliWatts) / availableW10(sample); - if (pwm > powerPWM) { - // constrain to max PWM counter, shouldn't be possible, - // but small cost for safety to avoid wraps - pwm = powerPWM; - } - } while (tryBetterPWM(pwm)); - + pwm = (powerPWM * milliWatts) / availableW10(sample); + if (pwm > powerPWM) { + // constrain to max PWM counter, shouldn't be possible, + // but small cost for safety to avoid wraps + pwm = powerPWM; + } return pwm; }