Added some function to thermistor

This commit is contained in:
-help 2024-01-28 21:44:57 +02:00
parent 32ec4fa38c
commit 058d4c06a2
9 changed files with 109 additions and 42 deletions

View File

@ -1,6 +1,9 @@
#include "PidController.h" #include "PidController.h"
#include "globals.h" #include "globals.h"
#define MOSTFET_PIN 17 #define MOSTFET_PIN 17
#include "voltageReference/AnalogRef.h"
extern AnalogRef analogRef;
PidController::PidController(PidControllerData *data) PidController::PidController(PidControllerData *data)
{ {
@ -13,7 +16,7 @@ PidController::PidController(PidControllerData *data)
controller.begin(&(this->data->currentTemp), &(this->data->setPoint), &(this->data->targetTemp), kp, ki, kd); controller.begin(&(this->data->currentTemp), &(this->data->setPoint), &(this->data->targetTemp), kp, ki, kd);
controller.reverse(); controller.reverse();
controller.setOutputLimits(0, 255); controller.setOutputLimits(0, 255);
controller.setSampleTime(20); //controller.setSampleTime(20);
controller.setWindUpLimits(-100, 185); controller.setWindUpLimits(-100, 185);
} }
@ -38,6 +41,24 @@ void PidController::loop()
data->targetTemp = chosenReflowProfile.getTargetTemp(); data->targetTemp = chosenReflowProfile.getTargetTemp();
data->currentTemp = thermistor1.getTemperature(); data->currentTemp = thermistor1.getTemperature();
// debug(); // debug();
float sysVoltage = analogRef.calculateSystemVoltage();
// // Serial.print("Sys Voltage: ");
// // Serial.println(sysVoltage);
// if (sysVoltage < 10.5)
// {
// controller.setWindUpLimits(-20, 50);
// }else{
// controller.setWindUpLimits(-100, 185);
// }
compute(); compute();
analogWrite(MOSTFET_PIN, data->setPoint); analogWrite(MOSTFET_PIN, data->setPoint);
} }
@ -50,6 +71,8 @@ void PidController::stop()
analogWrite(MOSTFET_PIN, 255); // VERY IMPORTANT, DONT CHANGE! analogWrite(MOSTFET_PIN, 255); // VERY IMPORTANT, DONT CHANGE!
controller.reset(); controller.reset();
controller.stop(); controller.stop();
} }
void PidController::start() void PidController::start()

View File

@ -67,9 +67,12 @@ void OledDisplay::handleButtonStateChange(Pair<ButtonKind, StateChangeEvent<Butt
curMenu->goNextItem(); curMenu->goNextItem();
} }
} }
} else if (state == DONE) { }
else if (state == DONE)
{
// GO back to iniital state // GO back to iniital state
if (change.second.to == ButtonState::PRESSED) { if (change.second.to == ButtonState::PRESSED)
{
reflowProcessState.set(USER_INPUT); reflowProcessState.set(USER_INPUT);
} }
} }
@ -97,7 +100,7 @@ void OledDisplay::handleDrawThermistorMenu(OledMenuItem menuItem)
display.setTextSize(1, 2); display.setTextSize(1, 2);
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
float thermR = thermistors[i].getResistance()/1000; float thermR = thermistors[i].getResistance() / 1000;
display.setCursor(i < 3 ? 0 : (SCREEN_WIDTH / 2 + 20), 20 * (i % 3)); display.setCursor(i < 3 ? 0 : (SCREEN_WIDTH / 2 + 20), 20 * (i % 3));
display.println(String(i + 1) + ":" + String(thermR)); display.println(String(i + 1) + ":" + String(thermR));
} }
@ -186,9 +189,12 @@ void OledDisplay::loop()
else if (state >= ReflowProcessState::PREHEAT && state < ReflowProcessState::DONE) else if (state >= ReflowProcessState::PREHEAT && state < ReflowProcessState::DONE)
{ {
handleReflowState(); handleReflowState();
} else if (state == ReflowProcessState::DONE) { }
else if (state == ReflowProcessState::DONE)
{
// Traverse back to root menu // Traverse back to root menu
while (curMenu->parent != NULL) { while (curMenu->parent != NULL)
{
curMenu = curMenu->parent; curMenu = curMenu->parent;
} }
display.clearDisplay(); display.clearDisplay();
@ -196,9 +202,9 @@ void OledDisplay::loop()
display.setTextSize(2); display.setTextSize(2);
drawPositionedText("DONE :)", DisplayTextAlignment::CENTER, DisplayTextAlignment::START); drawPositionedText("DONE :)", DisplayTextAlignment::CENTER, DisplayTextAlignment::START);
uint8_t curTemp = thermistor1.getTemperature(); uint8_t curTemp = thermistor1.getTemperature();
display.setTextSize(1,2); display.setTextSize(1, 2);
drawPositionedText("Temperature", DisplayTextAlignment::START, DisplayTextAlignment::CENTER); drawPositionedText("Temperature", DisplayTextAlignment::START, DisplayTextAlignment::CENTER);
drawPositionedText((String(curTemp)+" C").c_str(), DisplayTextAlignment::END, DisplayTextAlignment::CENTER); drawPositionedText((String(curTemp) + " C").c_str(), DisplayTextAlignment::END, DisplayTextAlignment::CENTER);
display.display(); display.display();
@ -278,9 +284,12 @@ void OledDisplay::handleUserInputState()
else else
{ {
// Default menu handling. Just display the title and the indicators to go back and forth // Default menu handling. Just display the title and the indicators to go back and forth
if (strlen(menuItem.title) > 8) { if (strlen(menuItem.title) > 8)
{
display.setTextSize(1, 2); display.setTextSize(1, 2);
} else { }
else
{
display.setTextSize(2); display.setTextSize(2);
} }
centerText(menuItem.title); centerText(menuItem.title);
@ -297,15 +306,10 @@ void OledDisplay::handleReflowState()
ReflowProcessState state = reflowProcessState.get(); ReflowProcessState state = reflowProcessState.get();
// Title topleft // Title topleft
drawPositionedText(STATE_STR(state), DisplayTextAlignment::CENTER, DisplayTextAlignment::START); drawPositionedText(STATE_STR(state), DisplayTextAlignment::CENTER, DisplayTextAlignment::START);
display.setTextSize(1, 2); display.setTextSize(1, 2);
// SysV topright
// #ifdef DEBUG
drawPositionedText((String(analogRef.calculateInputVoltage())+"V").c_str(), DisplayTextAlignment::END, DisplayTextAlignment::START);
// #endif
// Remaining time center left + bottom left // Remaining time center left + bottom left
uint32_t elapsedStep = chosenReflowProfile.getCurrentStepRelativeTime(); uint32_t elapsedStep = chosenReflowProfile.getCurrentStepRelativeTime();
drawPositionedText("Remaining", DisplayTextAlignment::START, DisplayTextAlignment::CENTER); drawPositionedText("Remaining", DisplayTextAlignment::START, DisplayTextAlignment::CENTER);
@ -317,8 +321,14 @@ void OledDisplay::handleReflowState()
drawPositionedText(("Curr.: " + String(curTemp)).c_str(), DisplayTextAlignment::END, DisplayTextAlignment::CENTER); drawPositionedText(("Curr.: " + String(curTemp)).c_str(), DisplayTextAlignment::END, DisplayTextAlignment::CENTER);
drawPositionedText(("Target: " + String(targetTemp)).c_str(), DisplayTextAlignment::END, DisplayTextAlignment::END); drawPositionedText(("Target: " + String(targetTemp)).c_str(), DisplayTextAlignment::END, DisplayTextAlignment::END);
// display.println("In Voltage:"+String(systemVoltage)); // display.println("In Voltage:"+String(systemVoltage));
// SysV topright
// #ifdef DEBUG
display.setCursor(25, 15);
display.setTextSize(1, 1);
display.print(analogRef.calculateInputVoltage());
// #endif
display.display(); display.display();
} }

View File

@ -6,8 +6,8 @@
WrappedState<ReflowProcessState> reflowProcessState = WrappedState<ReflowProcessState>(INITIALIZING); WrappedState<ReflowProcessState> reflowProcessState = WrappedState<ReflowProcessState>(INITIALIZING);
AnalogRef analogRef(5.0); AnalogRef analogRef(5.0);
// Calibration data for 100K thermistors ->https://fab.cba.mit.edu/classes/863.18/CBA/people/erik/reference/11_NTC-3950-100K.pdf ->Glass thermistor NTC 3950 100K // Calibration data for 100K thermistors ->https://datasheet.lcsc.com/lcsc/1810161311_Nanjing-Shiheng-Elec-MF58-104F3950_C123399.pdf ->Glass thermistor NTC 3950 100K
TempCalibration calibration_100K_3950 = {25, 100000, 86, 10324, 169, 1148}; TempCalibration calibration_100K_3950 = {25, 100000, 107, 4957, 167, 1000};
// Initalize the 3950 100K thermistors with ACTUAL reference resistor measurnment(Measured between Left pin and GND when the board is powered off) using the default calibration data for 100K thermistor // Initalize the 3950 100K thermistors with ACTUAL reference resistor measurnment(Measured between Left pin and GND when the board is powered off) using the default calibration data for 100K thermistor
// You can also make a custom calibration data for your thermistor and use that instead of the default one pass it as shown below --> keep the naming of the thermistor the same as the one you are replacing // You can also make a custom calibration data for your thermistor and use that instead of the default one pass it as shown below --> keep the naming of the thermistor the same as the one you are replacing
@ -23,7 +23,7 @@ TempCalibration calibration_100K_3950 = {25, 100000, 86, 10324, 169, 1148};
// To measure the resistence turn off the controller completley and measure between GND and the left pin of the connector with the thermistor unplugged // To measure the resistence turn off the controller completley and measure between GND and the left pin of the connector with the thermistor unplugged
// 2.5k reference = Best accuracy around 138C // 2.5k reference = Best accuracy around 138C
Thermistor thermistor1(THERMISTOR1_PIN, 2500, ThermistorZ_Placement::BOTTOM, ThermistorXY_Placement::RIGHT); Thermistor thermistor1(THERMISTOR1_PIN, 2500, ThermistorZ_Placement::TOP, ThermistorXY_Placement::MIDDLE);
Thermistor thermistor2(THERMISTOR2_PIN, 2500, ThermistorZ_Placement::TOP, ThermistorXY_Placement::MIDDLE); Thermistor thermistor2(THERMISTOR2_PIN, 2500, ThermistorZ_Placement::TOP, ThermistorXY_Placement::MIDDLE);
Thermistor thermistor3(THERMISTOR3_PIN, 2500, ThermistorZ_Placement::TOP, ThermistorXY_Placement::MIDDLE); Thermistor thermistor3(THERMISTOR3_PIN, 2500, ThermistorZ_Placement::TOP, ThermistorXY_Placement::MIDDLE);
// 1k reference = Best accuracy around 173c // 1k reference = Best accuracy around 173c

View File

@ -110,9 +110,9 @@ void loop()
{ {
pidController.loop(); pidController.loop();
#ifdef DEBUG // #ifdef DEBUG
pidController.debug(); pidController.debug();
#endif // #endif
tftDisplay.drawRealTemp(pidController.getInput(), chosenReflowProfile.getPercentage()); tftDisplay.drawRealTemp(pidController.getInput(), chosenReflowProfile.getPercentage());
ReflowStep step = chosenReflowProfile.reflowStep(); ReflowStep step = chosenReflowProfile.reflowStep();
// Here we draw the actual temp vs time to the display // Here we draw the actual temp vs time to the display

View File

@ -40,6 +40,14 @@ void TemperatureController::checkPluggedInThermistors()
debugLine(activeThermistorCount); debugLine(activeThermistorCount);
} }
float TemperatureController::getThermistorTempFast(uint8_t thermistorIndex)
{
return thermistors[thermistorIndex].getTemperatureFast();
}
/** /**
* Calculates and returns the average temperature of the solder plate based on the active thermistors. * Calculates and returns the average temperature of the solder plate based on the active thermistors.
* The average temperature is calculated by summing the temperature of each active thermistor and dividing by the number of active thermistors. * The average temperature is calculated by summing the temperature of each active thermistor and dividing by the number of active thermistors.

View File

@ -14,10 +14,12 @@ public:
float getPlateTemperature(); float getPlateTemperature();
void checkPluggedInThermistors(); void checkPluggedInThermistors();
float static getThermistorTempFast(uint8_t thermistorIndex);
private: private:
bool thermistorIsActive[6]; bool thermistorIsActive[6];
uint8_t activeThermistorCount =0 ; uint8_t activeThermistorCount =0 ;
}; };
#endif // TEMPERATURE_CONTROLLER_H #endif // TEMPERATURE_CONTROLLER_H

View File

@ -27,6 +27,8 @@ float Thermistor::getTemperature()
// The scaling factor should only be applied when the plate is being heated up -> 60C seems like a good threshold unless you live in the sahara desert with no AC // The scaling factor should only be applied when the plate is being heated up -> 60C seems like a good threshold unless you live in the sahara desert with no AC
// Its non-linear so it will be more accurate so we will probably need to impliment a refrence table for the scaling factor this is just a rough estimate it will be based on a sensor calibrated on the top middle of the plate // Its non-linear so it will be more accurate so we will probably need to impliment a refrence table for the scaling factor this is just a rough estimate it will be based on a sensor calibrated on the top middle of the plate
//TODO: Impliment a method to calculate the scaling factor based on the placement of the thermistor by measurnment sof refrence center thermistor to other positions
if (temp > 60) if (temp > 60)
{ {
temp = temp * scalingFactor; temp = temp * scalingFactor;
@ -67,7 +69,7 @@ void Thermistor::calculateCoefficents(TempCalibration calibration)
bool Thermistor::isPluggedIn() bool Thermistor::isPluggedIn()
{ {
//check if the resistnece is INF is so then the thermistor is not plugged in // check if the resistnece is INF is so then the thermistor is not plugged in
if (getResistance() == INFINITY) if (getResistance() == INFINITY)
{ {
return false; return false;
@ -76,7 +78,6 @@ bool Thermistor::isPluggedIn()
{ {
return true; return true;
} }
} }
/** /**
@ -120,22 +121,47 @@ void Thermistor::calculateScalingFactor()
} }
} }
float Thermistor::getTemperatureFast()
{
// Get only one reading
getResistance();
float temp = (int)1 / (coefficents.a + coefficents.b * log(sensorResistance) + coefficents.c * (pow(log(sensorResistance), 3))) - K;
// The scaling factor should only be applied when the plate is being heated up -> 60C seems like a good threshold unless you live in the sahara desert with no AC
// Its non-linear so it will be more accurate so we will probably need to impliment a refrence table for the scaling factor this is just a rough estimate it will be based on a sensor calibrated on the top middle of the plate
if (temp > 60)
{
temp = temp * scalingFactor;
}
return temp;
}
float Thermistor::getResistance() float Thermistor::getResistance()
{ {
float systemVoltage = analogRef.calculateSystemVoltage(); float systemVoltage = analogRef.calculateSystemVoltage();
int raw = analogRead(thermistorPin); // int raw = analogRead(thermistorPin);
// Get resistance value // // Get resistance value
float buffer = raw * systemVoltage; // float buffer = raw * systemVoltage;
float vOut = (buffer) / 1023; // float vOut = (buffer) / 1023;
// Calculate the resistance of the thermistor with the system voltage accounted for // // Calculate the resistance of the thermistor with the system voltage accounted for
buffer = (systemVoltage / vOut) - 1; // buffer = (systemVoltage / vOut) - 1;
// return the resistence // // return the resistence
sensorResistance = setRes * buffer; // sensorResistance = setRes * buffer;
int sensorValue = analogRead(thermistorPin); // Read the analog value (0-1023)
float voltage = sensorValue * (systemVoltage / 1023.0); // Convert to voltage
float R_unknown = (setRes * (systemVoltage - voltage)) / voltage; // Calculate the unknown resistor's value
sensorResistance = R_unknown;
return sensorResistance; return sensorResistance;
} }

View File

@ -32,7 +32,6 @@ extern TempCalibration calibration_100K_3950;
extern AnalogRef analogRef; extern AnalogRef analogRef;
enum ThermistorZ_Placement enum ThermistorZ_Placement
{ {
TOP, TOP,
@ -52,13 +51,13 @@ public:
// Constructor // Constructor
Thermistor(); Thermistor();
Thermistor(uint8_t pin, uint16_t resistance, TempCalibration calibration,ThermistorZ_Placement zPlacement,ThermistorXY_Placement xyPlacment) : thermistorPin(pin), setRes(resistance), calibration(calibration),zPlacement(zPlacement),xyPlacment(xyPlacment) Thermistor(uint8_t pin, uint16_t resistance, TempCalibration calibration, ThermistorZ_Placement zPlacement, ThermistorXY_Placement xyPlacment) : thermistorPin(pin), setRes(resistance), calibration(calibration), zPlacement(zPlacement), xyPlacment(xyPlacment)
{ {
calculateCoefficents(calibration); calculateCoefficents(calibration);
calculateScalingFactor(); calculateScalingFactor();
} }
Thermistor(uint8_t pin, uint16_t resistance,ThermistorZ_Placement zPlacement,ThermistorXY_Placement xyPlacment) : thermistorPin(pin), setRes(resistance), calibration(calibration_100K_3950),zPlacement(zPlacement),xyPlacment(xyPlacment) Thermistor(uint8_t pin, uint16_t resistance, ThermistorZ_Placement zPlacement, ThermistorXY_Placement xyPlacment) : thermistorPin(pin), setRes(resistance), calibration(calibration_100K_3950), zPlacement(zPlacement), xyPlacment(xyPlacment)
{ {
calculateCoefficents(calibration); calculateCoefficents(calibration);
calculateScalingFactor(); calculateScalingFactor();
@ -74,6 +73,7 @@ public:
float getScalingFactor() { return scalingFactor; }; float getScalingFactor() { return scalingFactor; };
bool isPluggedIn(); bool isPluggedIn();
float getTemperatureFast();
private: private:
ThermistorZ_Placement zPlacement; ThermistorZ_Placement zPlacement;
@ -81,14 +81,12 @@ private:
const double K = 273.15; const double K = 273.15;
float sensorResistance; float sensorResistance;
uint8_t thermistorPin; uint8_t thermistorPin;
uint16_t setRes; float setRes;
Coefficents coefficents; Coefficents coefficents;
float referenceResistance; float referenceResistance;
TempCalibration calibration; TempCalibration calibration;
float scalingFactor ; float scalingFactor;
void calculateScalingFactor(); void calculateScalingFactor();
}; };
#endif // THERMISTOR_H #endif // THERMISTOR_H

View File

@ -64,7 +64,7 @@ public:
float value; float value;
uint8_t sampleCount = 20; uint8_t sampleCount = 10;
value = 0; value = 0;
for (size_t i = 0; i < sampleCount; ++i) for (size_t i = 0; i < sampleCount; ++i)