Skip to content

Commit c0a5e24

Browse files
authored
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
1 parent f99aed5 commit c0a5e24

File tree

19 files changed

+116
-108
lines changed

19 files changed

+116
-108
lines changed

source/Core/BSP/BSP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#include "BSP_Power.h"
44
#include "BSP_QC.h"
55
#include "Defines.h"
6+
#include "Types.h"
67
#include "configuration.h"
78
#include <stdbool.h>
89
#include <stdint.h>
10+
911
/*
1012
* BSP.h -- Board Support
1113
*

source/Core/BSP/MHP30/ThermoModel.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
*/
77
#include "Setup.h"
88
#include "TipThermoModel.h"
9+
#include "Types.h"
910
#include "Utils.h"
1011
#include "configuration.h"
11-
extern uint16_t tipSenseResistancex10Ohms;
12-
uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) {
12+
13+
extern uint16_t tipSenseResistancex10Ohms;
14+
TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) {
1315
// For the MHP30, we are mimicing the original code and using the resistor fitted to the base of the heater head,
1416
// this is measured at boot in pid task and in the disconnected tip check if tip is removed
1517
if (tipSenseResistancex10Ohms > 900 && tipSenseResistancex10Ohms <= 1100) {

source/Core/BSP/Miniware/ThermoModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,4 @@ const int32_t uVtoDegC[] = {
127127
#endif
128128
const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(uVtoDegC[0]));
129129

130-
uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); }
130+
TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); }

source/Core/BSP/Pinecil/ThermoModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ const int32_t uVtoDegC[] = {
6969

7070
const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(uVtoDegC[0]));
7171

72-
uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); }
72+
TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); }

source/Core/BSP/Pinecilv2/BSP.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
// These control the period's of time used for the PWM
1919
const uint16_t powerPWM = 255;
20-
const uint8_t holdoffTicks = 25; // This is the tick delay before temp measure starts (i.e. time for op-amp recovery)
21-
const uint8_t tempMeasureTicks = 25;
20+
uint8_t holdoffTicks = 25; // This is the tick delay before temp measure starts (i.e. time for op-amp recovery)
21+
uint8_t tempMeasureTicks = 25;
2222

2323
uint16_t totalPWM = 255; // Total length of the cycle's ticks
2424

@@ -162,13 +162,13 @@ uint8_t getTipResistanceX10() {
162162

163163
uint8_t getTipThermalMass() {
164164
if (lastTipResistance >= 80) {
165-
return TIP_THERMAL_MASS;
165+
return 65;
166166
}
167167
return 45;
168168
}
169169
uint8_t getTipInertia() {
170170
if (lastTipResistance >= 80) {
171-
return TIP_THERMAL_MASS;
171+
return 90;
172172
}
173173
return 10;
174174
}

source/Core/BSP/Pinecilv2/IRQ.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern "C" {
1919
}
2020
void start_PWM_output(void);
2121

22-
#define ADC_Filter_Smooth 4
22+
#define ADC_Filter_Smooth 1
2323
history<uint16_t, ADC_Filter_Smooth> ADC_Vin;
2424
history<uint16_t, ADC_Filter_Smooth> ADC_Temp;
2525
history<uint16_t, ADC_Filter_Smooth> ADC_Tip;
@@ -67,19 +67,21 @@ void adc_fifo_irq(void) {
6767
ADC_IntClr(ADC_INT_ALL);
6868
}
6969

70-
static bool fastPWM = false;
70+
volatile bool inFastPWMMode = false;
71+
7172
static void switchToFastPWM(void);
73+
static void switchToSlowPWM(void);
7274

73-
volatile uint16_t PWMSafetyTimer = 0;
74-
volatile uint8_t pendingPWM = 0;
75-
volatile bool lastPeriodWasFast = false;
75+
volatile uint16_t PWMSafetyTimer = 0;
76+
volatile uint8_t pendingPWM = 0;
77+
volatile bool pendingNextPeriodIsFast = false;
7678

7779
void start_PWM_output(void) {
7880

7981
if (PWMSafetyTimer) {
8082
PWMSafetyTimer--;
81-
if (lastPeriodWasFast != fastPWM) {
82-
if (fastPWM) {
83+
if (pendingNextPeriodIsFast != inFastPWMMode) {
84+
if (pendingNextPeriodIsFast) {
8385
switchToFastPWM();
8486
} else {
8587
switchToSlowPWM();
@@ -96,6 +98,7 @@ void start_PWM_output(void) {
9698
}
9799
} else {
98100
PWM_Channel_Disable(PWM_Channel);
101+
switchToFastPWM();
99102
}
100103
TIMER_Enable(TIMER_CH0);
101104
}
@@ -108,43 +111,47 @@ void timer0_comp0_callback(void) {
108111
void timer0_comp1_callback(void) { PWM_Channel_Disable(PWM_Channel); } // Trigged at end of output cycle; turn off the tip PWM
109112

110113
void switchToFastPWM(void) {
111-
fastPWM = true;
112-
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
114+
inFastPWMMode = true;
115+
holdoffTicks = 10;
116+
tempMeasureTicks = 10;
117+
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
113118
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM);
114119

115120
// ~10Hz
116121
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_0, powerPWM + holdoffTicks);
117-
// Set divider to 11
122+
// Set divider to 10 ~= 10.5Hz
118123

119124
uint32_t tmpVal = BL_RD_REG(TIMER_BASE, TIMER_TCDR);
120125

121-
tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 11);
126+
tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 10);
122127

123128
BL_WR_REG(TIMER_BASE, TIMER_TCDR, tmpVal);
124129
}
125130

126131
void switchToSlowPWM(void) {
127132
// 5Hz
128-
fastPWM = false;
129-
totalPWM = powerPWM + tempMeasureTicks / 2 + holdoffTicks / 2;
133+
inFastPWMMode = false;
134+
holdoffTicks = 5;
135+
tempMeasureTicks = 5;
136+
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
130137

131138
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM);
132139
// Adjust ADC
133-
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_0, powerPWM + (holdoffTicks / 2));
140+
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_0, powerPWM + holdoffTicks);
134141

135142
// Set divider to 22
136143

137144
uint32_t tmpVal = BL_RD_REG(TIMER_BASE, TIMER_TCDR);
138145

139-
tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 22);
146+
tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 20);
140147

141148
BL_WR_REG(TIMER_BASE, TIMER_TCDR, tmpVal);
142149
}
143150
void setTipPWM(const uint8_t pulse, const bool shouldUseFastModePWM) {
144151
PWMSafetyTimer = 10; // This is decremented in the handler for PWM so that the tip pwm is
145152
// disabled if the PID task is not scheduled often enough.
146-
pendingPWM = pulse;
147-
fastPWM = shouldUseFastModePWM;
153+
pendingPWM = pulse;
154+
pendingNextPeriodIsFast = shouldUseFastModePWM;
148155
}
149156
extern osThreadId POWTaskHandle;
150157

source/Core/BSP/Pinecilv2/IRQ.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ void timer0_comp1_callback(void);
2121
void timer0_comp2_callback(void);
2222
void adc_fifo_irq(void);
2323
void GPIO_IRQHandler(void);
24-
void switchToSlowPWM(void);
2524
#ifdef __cplusplus
2625
}
2726
#endif

source/Core/BSP/Pinecilv2/Setup.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ uint16_t getADCVin(uint8_t sample);
2929
#ifdef __cplusplus
3030
}
3131
#endif
32-
void setupFUSBIRQ();
33-
extern const uint8_t holdoffTicks;
34-
extern const uint8_t tempMeasureTicks;
32+
void setupFUSBIRQ();
33+
extern uint8_t holdoffTicks;
34+
extern uint8_t tempMeasureTicks;
3535
#endif /* PINE_SETUP_H_ */

source/Core/BSP/Pinecilv2/ThermoModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ const int32_t uVtoDegC[] = {
6969

7070
const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(int32_t));
7171

72-
uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); }
72+
TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); }

source/Core/BSP/Pinecilv2/configuration.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@
161161
#define DEBUG_UART_OUTPUT
162162
#define HAS_POWER_DEBUG_MENU
163163
#define HARDWARE_MAX_WATTAGE_X10 750
164-
#define TIP_THERMAL_MASS 65 // X10 watts to raise 1 deg C in 1 second
165164
#define BLE_ENABLED
166165
#define NEEDS_VBUS_PROBE 0
167166
#define CANT_DIRECT_READ_SETTINGS

source/Core/BSP/Sequre_S60/ThermoModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
#include "Utils.h"
99
#include "configuration.h"
1010

11-
uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return (tipuVDelta * 50) / 485; }
11+
TemperatureType_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return (tipuVDelta * 50) / 485; }

source/Core/Drivers/TipThermoModel.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "TipThermoModel.h"
99
#include "BSP.h"
1010
#include "Settings.h"
11+
#include "Types.h"
1112
#include "Utils.h"
1213
#include "configuration.h"
1314
#include "main.hpp"
@@ -54,45 +55,41 @@ uint32_t TipThermoModel::convertTipRawADCTouV(uint16_t rawADC, bool ski
5455
return valueuV;
5556
}
5657

57-
uint32_t TipThermoModel::convertTipRawADCToDegC(uint16_t rawADC) { return convertuVToDegC(convertTipRawADCTouV(rawADC)); }
58-
uint32_t TipThermoModel::convertTipRawADCToDegF(uint16_t rawADC) { return convertuVToDegF(convertTipRawADCTouV(rawADC)); }
58+
TemperatureType_t TipThermoModel::convertTipRawADCToDegC(uint16_t rawADC) { return convertuVToDegC(convertTipRawADCTouV(rawADC)); }
59+
TemperatureType_t TipThermoModel::convertTipRawADCToDegF(uint16_t rawADC) { return convertuVToDegF(convertTipRawADCTouV(rawADC)); }
5960

60-
uint32_t TipThermoModel::convertuVToDegF(uint32_t tipuVDelta) { return convertCtoF(convertuVToDegC(tipuVDelta)); }
61+
TemperatureType_t TipThermoModel::convertuVToDegF(uint32_t tipuVDelta) { return convertCtoF(convertuVToDegC(tipuVDelta)); }
6162

62-
uint32_t TipThermoModel::convertCtoF(uint32_t degC) {
63+
TemperatureType_t TipThermoModel::convertCtoF(TemperatureType_t degC) {
6364
//(Y °C × 9/5) + 32 =Y°F
6465
return (32 + ((degC * 9) / 5));
6566
}
6667

67-
uint32_t TipThermoModel::convertFtoC(uint32_t degF) {
68+
TemperatureType_t TipThermoModel::convertFtoC(TemperatureType_t degF) {
6869
//(Y°F − 32) × 5/9 = Y°C
6970
if (degF < 32) {
7071
return 0;
7172
}
7273
return ((degF - 32) * 5) / 9;
7374
}
74-
uint32_t TipThermoModel::getTipInC(bool sampleNow) {
75-
int32_t currentTipTempInC = TipThermoModel::convertTipRawADCToDegC(getTipRawTemp(sampleNow));
75+
TemperatureType_t TipThermoModel::getTipInC(bool sampleNow) {
76+
TemperatureType_t currentTipTempInC = TipThermoModel::convertTipRawADCToDegC(getTipRawTemp(sampleNow));
7677
currentTipTempInC += getHandleTemperature(sampleNow) / 10; // Add handle offset
7778

78-
// Power usage indicates that our tip temp is lower than our thermocouple temp.
79-
// I found a number that doesn't unbalance the existing PID, causing overshoot.
80-
// This could be tuned in concert with PID parameters...
81-
8279
if (currentTipTempInC < 0) {
8380
return 0;
8481
}
8582
return currentTipTempInC;
8683
}
8784

88-
uint32_t TipThermoModel::getTipInF(bool sampleNow) {
89-
uint32_t currentTipTempInF = getTipInC(sampleNow);
90-
currentTipTempInF = convertCtoF(currentTipTempInF);
85+
TemperatureType_t TipThermoModel::getTipInF(bool sampleNow) {
86+
TemperatureType_t currentTipTempInF = getTipInC(sampleNow);
87+
currentTipTempInF = convertCtoF(currentTipTempInF);
9188
return currentTipTempInF;
9289
}
9390

94-
uint32_t TipThermoModel::getTipMaxInC() {
95-
uint32_t maximumTipTemp = TipThermoModel::convertTipRawADCToDegC(ADC_MAX_READING - 1);
91+
TemperatureType_t TipThermoModel::getTipMaxInC() {
92+
TemperatureType_t maximumTipTemp = TipThermoModel::convertTipRawADCToDegC(ADC_MAX_READING - 1);
9693
maximumTipTemp += getHandleTemperature(0) / 10; // Add handle offset
9794
return maximumTipTemp - 1;
9895
}

source/Core/Drivers/TipThermoModel.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,27 @@
88
#ifndef SRC_TIPTHERMOMODEL_H_
99
#define SRC_TIPTHERMOMODEL_H_
1010
#include "BSP.h"
11+
#include "Types.h"
1112
#include "stdint.h"
1213
class TipThermoModel {
1314
public:
1415
// These are the main two functions
15-
static uint32_t getTipInC(bool sampleNow = false);
16-
static uint32_t getTipInF(bool sampleNow = false);
16+
static TemperatureType_t getTipInC(bool sampleNow = false);
17+
static TemperatureType_t getTipInF(bool sampleNow = false);
1718

1819
// Calculates the maximum temperature can can be read by the ADC range
19-
static uint32_t getTipMaxInC();
20+
static TemperatureType_t getTipMaxInC();
2021

21-
static uint32_t convertTipRawADCToDegC(uint16_t rawADC);
22-
static uint32_t convertTipRawADCToDegF(uint16_t rawADC);
22+
static TemperatureType_t convertTipRawADCToDegC(uint16_t rawADC);
23+
static TemperatureType_t convertTipRawADCToDegF(uint16_t rawADC);
2324
// Returns the uV of the tip reading before the op-amp compensating for pullups
24-
static uint32_t convertTipRawADCTouV(uint16_t rawADC, bool skipCalOffset = false);
25-
static uint32_t convertCtoF(uint32_t degC);
26-
static uint32_t convertFtoC(uint32_t degF);
25+
static uint32_t convertTipRawADCTouV(uint16_t rawADC, bool skipCalOffset = false);
26+
static TemperatureType_t convertCtoF(TemperatureType_t degC);
27+
static TemperatureType_t convertFtoC(TemperatureType_t degF);
2728

2829
private:
29-
static uint32_t convertuVToDegC(uint32_t tipuVDelta);
30-
static uint32_t convertuVToDegF(uint32_t tipuVDelta);
30+
static TemperatureType_t convertuVToDegC(uint32_t tipuVDelta);
31+
static TemperatureType_t convertuVToDegF(uint32_t tipuVDelta);
3132
};
3233

3334
#endif /* SRC_TIPTHERMOMODEL_H_ */

source/Core/Inc/Types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef TYPES_H_
2+
#define TYPES_H_
3+
#include <stddef.h>
4+
5+
// Used for temperature represented in C or x10C.
6+
//
7+
8+
typedef int32_t TemperatureType_t;
9+
10+
#endif

source/Core/Inc/main.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#define __MAIN_H
33
#include "OLED.hpp"
44
#include "Setup.h"
5+
#include "Types.h"
56
#include <stdint.h>
6-
extern volatile uint32_t currentTempTargetDegC;
7-
extern bool settingsWereReset;
8-
extern bool usb_pd_available;
7+
extern volatile TemperatureType_t currentTempTargetDegC;
8+
extern bool settingsWereReset;
9+
extern bool usb_pd_available;
910
#ifdef __cplusplus
1011
extern "C" {
1112
#endif

source/Core/Inc/power.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const uint8_t wattHistoryFilter = 24; //
2323
extern expMovingAverage<uint32_t, wattHistoryFilter> x10WattHistory;
2424

2525
uint32_t availableW10(uint8_t sample);
26-
int32_t tempToX10Watts(int32_t rawTemp);
2726
void setTipX10Watts(int32_t mw);
2827
uint8_t X10WattsToPWM(int32_t milliWatts, uint8_t sample = 0);
2928
#endif /* POWER_HPP_ */

source/Core/Src/power.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ bool shouldBeUsingFastPWMMode(const uint8_t pwmTicks) {
2727
return lastPWMWasFast;
2828
}
2929

30-
int32_t tempToX10Watts(int32_t rawTemp) {
31-
// mass is in x10J/*C, rawC is raw per degree C
32-
// returns x10Watts needed to raise/lower a mass by rawTemp
33-
// degrees in one cycle.
34-
int32_t x10Watts = TIP_THERMAL_MASS * rawTemp;
35-
return x10Watts;
36-
}
37-
3830
void setTipX10Watts(int32_t mw) {
3931
int32_t outputPWMLevel = X10WattsToPWM(mw, 1);
4032
const bool shouldUseFastPWM = shouldBeUsingFastPWMMode(outputPWMLevel);

source/Core/Threads/OperatingModes/utils/checkUndervoltage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "OperatingModeUtilities.h"
33
#include "configuration.h"
44
#ifdef POW_DC
5-
extern volatile uint32_t currentTempTargetDegC;
5+
extern volatile TemperatureType_t currentTempTargetDegC;
66
// returns true if undervoltage has occured
77
bool checkForUnderVoltage(void) {
88
if (!getIsPoweredByDCIN()) {

0 commit comments

Comments
 (0)