Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temperature code updates #1814

Merged
merged 8 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/Core/BSP/BSP.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include "BSP_Power.h"
#include "BSP_QC.h"
#include "Defines.h"
#include "Types.h"
#include "configuration.h"
#include <stdbool.h>
#include <stdint.h>

/*
* BSP.h -- Board Support
*
Expand Down
6 changes: 4 additions & 2 deletions source/Core/BSP/MHP30/ThermoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion source/Core/BSP/Miniware/ThermoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
2 changes: 1 addition & 1 deletion source/Core/BSP/Pinecil/ThermoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
8 changes: 4 additions & 4 deletions source/Core/BSP/Pinecilv2/BSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
Expand Down
41 changes: 24 additions & 17 deletions source/Core/BSP/Pinecilv2/IRQ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern "C" {
}
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_Temp;
history<uint16_t, ADC_Filter_Smooth> ADC_Tip;
Expand Down Expand Up @@ -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();
Expand All @@ -96,6 +98,7 @@ void start_PWM_output(void) {
}
} else {
PWM_Channel_Disable(PWM_Channel);
switchToFastPWM();
}
TIMER_Enable(TIMER_CH0);
}
Expand All @@ -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;

Expand Down
1 change: 0 additions & 1 deletion source/Core/BSP/Pinecilv2/IRQ.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions source/Core/BSP/Pinecilv2/Setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
2 changes: 1 addition & 1 deletion source/Core/BSP/Pinecilv2/ThermoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
1 change: 0 additions & 1 deletion source/Core/BSP/Pinecilv2/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion source/Core/BSP/Sequre_S60/ThermoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
29 changes: 13 additions & 16 deletions source/Core/Drivers/TipThermoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
21 changes: 11 additions & 10 deletions source/Core/Drivers/TipThermoModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
10 changes: 10 additions & 0 deletions source/Core/Inc/Types.h
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions source/Core/Inc/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#define __MAIN_H
#include "OLED.hpp"
#include "Setup.h"
#include "Types.h"
#include <stdint.h>
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
Expand Down
1 change: 0 additions & 1 deletion source/Core/Inc/power.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const uint8_t wattHistoryFilter = 24; //
extern expMovingAverage<uint32_t, wattHistoryFilter> 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_ */
8 changes: 0 additions & 8 deletions source/Core/Src/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Loading
Loading