From 058d4c06a27625dae20bce4fcc3d8060bc854d2c Mon Sep 17 00:00:00 2001 From: -help Date: Sun, 28 Jan 2024 21:44:57 +0200 Subject: [PATCH] Added some function to thermistor --- src/PID/PidController.cpp | 25 +++++++++++- src/displays/oled.cpp | 46 ++++++++++++++--------- src/globals.cpp | 6 +-- src/main.cpp | 4 +- src/thermistors/TemperatureController.cpp | 8 ++++ src/thermistors/TemperatureController.h | 2 + src/thermistors/Thermistor.cpp | 46 ++++++++++++++++++----- src/thermistors/Thermistor.h | 12 +++--- src/voltageReference/AnalogRef.h | 2 +- 9 files changed, 109 insertions(+), 42 deletions(-) diff --git a/src/PID/PidController.cpp b/src/PID/PidController.cpp index c317068..ac652de 100644 --- a/src/PID/PidController.cpp +++ b/src/PID/PidController.cpp @@ -1,6 +1,9 @@ #include "PidController.h" #include "globals.h" #define MOSTFET_PIN 17 +#include "voltageReference/AnalogRef.h" + +extern AnalogRef analogRef; 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.reverse(); controller.setOutputLimits(0, 255); - controller.setSampleTime(20); + //controller.setSampleTime(20); controller.setWindUpLimits(-100, 185); } @@ -38,6 +41,24 @@ void PidController::loop() data->targetTemp = chosenReflowProfile.getTargetTemp(); data->currentTemp = thermistor1.getTemperature(); // 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(); analogWrite(MOSTFET_PIN, data->setPoint); } @@ -50,6 +71,8 @@ void PidController::stop() analogWrite(MOSTFET_PIN, 255); // VERY IMPORTANT, DONT CHANGE! controller.reset(); controller.stop(); + + } void PidController::start() diff --git a/src/displays/oled.cpp b/src/displays/oled.cpp index 12938a6..282ce71 100644 --- a/src/displays/oled.cpp +++ b/src/displays/oled.cpp @@ -67,9 +67,12 @@ void OledDisplay::handleButtonStateChange(PairgoNextItem(); } } - } else if (state == DONE) { + } + else if (state == DONE) + { // GO back to iniital state - if (change.second.to == ButtonState::PRESSED) { + if (change.second.to == ButtonState::PRESSED) + { reflowProcessState.set(USER_INPUT); } } @@ -97,7 +100,7 @@ void OledDisplay::handleDrawThermistorMenu(OledMenuItem menuItem) display.setTextSize(1, 2); 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.println(String(i + 1) + ":" + String(thermR)); } @@ -186,9 +189,12 @@ void OledDisplay::loop() else if (state >= ReflowProcessState::PREHEAT && state < ReflowProcessState::DONE) { handleReflowState(); - } else if (state == ReflowProcessState::DONE) { + } + else if (state == ReflowProcessState::DONE) + { // Traverse back to root menu - while (curMenu->parent != NULL) { + while (curMenu->parent != NULL) + { curMenu = curMenu->parent; } display.clearDisplay(); @@ -196,9 +202,9 @@ void OledDisplay::loop() display.setTextSize(2); drawPositionedText("DONE :)", DisplayTextAlignment::CENTER, DisplayTextAlignment::START); uint8_t curTemp = thermistor1.getTemperature(); - display.setTextSize(1,2); + display.setTextSize(1, 2); 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(); @@ -278,9 +284,12 @@ void OledDisplay::handleUserInputState() else { // 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); - } else { + } + else + { display.setTextSize(2); } centerText(menuItem.title); @@ -297,15 +306,10 @@ void OledDisplay::handleReflowState() ReflowProcessState state = reflowProcessState.get(); // Title topleft drawPositionedText(STATE_STR(state), DisplayTextAlignment::CENTER, DisplayTextAlignment::START); - + 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 uint32_t elapsedStep = chosenReflowProfile.getCurrentStepRelativeTime(); drawPositionedText("Remaining", DisplayTextAlignment::START, DisplayTextAlignment::CENTER); @@ -317,8 +321,14 @@ void OledDisplay::handleReflowState() drawPositionedText(("Curr.: " + String(curTemp)).c_str(), DisplayTextAlignment::END, DisplayTextAlignment::CENTER); drawPositionedText(("Target: " + String(targetTemp)).c_str(), DisplayTextAlignment::END, DisplayTextAlignment::END); - // 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(); } diff --git a/src/globals.cpp b/src/globals.cpp index 82e1b88..08e50b3 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -6,8 +6,8 @@ WrappedState reflowProcessState = WrappedState(INITIALIZING); 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 -TempCalibration calibration_100K_3950 = {25, 100000, 86, 10324, 169, 1148}; +// 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, 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 // 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 // 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 thermistor3(THERMISTOR3_PIN, 2500, ThermistorZ_Placement::TOP, ThermistorXY_Placement::MIDDLE); // 1k reference = Best accuracy around 173c diff --git a/src/main.cpp b/src/main.cpp index e9fb4e8..e19f834 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -110,9 +110,9 @@ void loop() { pidController.loop(); - #ifdef DEBUG + // #ifdef DEBUG pidController.debug(); - #endif + // #endif tftDisplay.drawRealTemp(pidController.getInput(), chosenReflowProfile.getPercentage()); ReflowStep step = chosenReflowProfile.reflowStep(); // Here we draw the actual temp vs time to the display diff --git a/src/thermistors/TemperatureController.cpp b/src/thermistors/TemperatureController.cpp index 69be6b9..ef1b3c7 100644 --- a/src/thermistors/TemperatureController.cpp +++ b/src/thermistors/TemperatureController.cpp @@ -40,6 +40,14 @@ void TemperatureController::checkPluggedInThermistors() 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. * The average temperature is calculated by summing the temperature of each active thermistor and dividing by the number of active thermistors. diff --git a/src/thermistors/TemperatureController.h b/src/thermistors/TemperatureController.h index bb8d102..becf408 100644 --- a/src/thermistors/TemperatureController.h +++ b/src/thermistors/TemperatureController.h @@ -14,10 +14,12 @@ public: float getPlateTemperature(); void checkPluggedInThermistors(); + float static getThermistorTempFast(uint8_t thermistorIndex); private: bool thermistorIsActive[6]; uint8_t activeThermistorCount =0 ; + }; #endif // TEMPERATURE_CONTROLLER_H diff --git a/src/thermistors/Thermistor.cpp b/src/thermistors/Thermistor.cpp index d2f497b..a9a6994 100644 --- a/src/thermistors/Thermistor.cpp +++ b/src/thermistors/Thermistor.cpp @@ -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 // 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) { temp = temp * scalingFactor; @@ -67,7 +69,7 @@ void Thermistor::calculateCoefficents(TempCalibration calibration) 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) { return false; @@ -76,7 +78,6 @@ bool Thermistor::isPluggedIn() { 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 systemVoltage = analogRef.calculateSystemVoltage(); - int raw = analogRead(thermistorPin); + // int raw = analogRead(thermistorPin); - // Get resistance value - float buffer = raw * systemVoltage; - float vOut = (buffer) / 1023; + // // Get resistance value + // float buffer = raw * systemVoltage; + // float vOut = (buffer) / 1023; - // Calculate the resistance of the thermistor with the system voltage accounted for - buffer = (systemVoltage / vOut) - 1; + // // Calculate the resistance of the thermistor with the system voltage accounted for + // buffer = (systemVoltage / vOut) - 1; - // return the resistence - sensorResistance = setRes * buffer; + // // return the resistence + // 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; } diff --git a/src/thermistors/Thermistor.h b/src/thermistors/Thermistor.h index f0941e9..503aec8 100644 --- a/src/thermistors/Thermistor.h +++ b/src/thermistors/Thermistor.h @@ -32,7 +32,6 @@ extern TempCalibration calibration_100K_3950; extern AnalogRef analogRef; - enum ThermistorZ_Placement { TOP, @@ -52,13 +51,13 @@ public: // Constructor 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); 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); calculateScalingFactor(); @@ -74,6 +73,7 @@ public: float getScalingFactor() { return scalingFactor; }; bool isPluggedIn(); + float getTemperatureFast(); private: ThermistorZ_Placement zPlacement; @@ -81,14 +81,12 @@ private: const double K = 273.15; float sensorResistance; uint8_t thermistorPin; - uint16_t setRes; + float setRes; Coefficents coefficents; float referenceResistance; TempCalibration calibration; - float scalingFactor ; + float scalingFactor; void calculateScalingFactor(); - - }; #endif // THERMISTOR_H diff --git a/src/voltageReference/AnalogRef.h b/src/voltageReference/AnalogRef.h index 4bf93e2..3377855 100644 --- a/src/voltageReference/AnalogRef.h +++ b/src/voltageReference/AnalogRef.h @@ -64,7 +64,7 @@ public: float value; - uint8_t sampleCount = 20; + uint8_t sampleCount = 10; value = 0; for (size_t i = 0; i < sampleCount; ++i)