Rewrite tip resistance measurement

This commit is contained in:
Ben V. Brown
2022-07-08 22:13:07 +10:00
parent 3279cfabfc
commit 0d8ba1dd92
2 changed files with 55 additions and 54 deletions

View File

@@ -227,7 +227,8 @@ void setStatusLED(const enum StatusLED state) {
} }
uint8_t lastTipResistance = 0; // default to unknown uint8_t lastTipResistance = 0; // default to unknown
uint32_t lastTipReadinguV = 0; uint32_t tipResistanceReadings[4] = {0, 0, 0, 0};
uint8_t tipResistanceReadingSlot = 0;
uint8_t getTipResitanceX10() { uint8_t getTipResitanceX10() {
// Return tip resistance in x10 ohms // Return tip resistance in x10 ohms
// We can measure this using the op-amp // We can measure this using the op-amp
@@ -240,7 +241,6 @@ uint8_t getTipThermalMass() {
} }
return (TIP_THERMAL_MASS * 25) / 10; return (TIP_THERMAL_MASS * 25) / 10;
} }
void startMeasureTipResistance() {
// We want to calculate lastTipResistance // We want to calculate lastTipResistance
// If tip is connected, and the tip is cold and the tip is not being heated // If tip is connected, and the tip is cold and the tip is not being heated
// We can use the GPIO to inject a small current into the tip and measure this // We can use the GPIO to inject a small current into the tip and measure this
@@ -252,58 +252,58 @@ void startMeasureTipResistance() {
// Which is definitely measureable // Which is definitely measureable
// Taking shortcuts here as we know we only really have to pick apart 6 and 8 ohm tips // Taking shortcuts here as we know we only really have to pick apart 6 and 8 ohm tips
// These are reported as 60 and 75 respectively // These are reported as 60 and 75 respectively
lastTipReadinguV = TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0)); void performTipResistanceSampleReading() {
gpio_write(TIP_RESISTANCE_SENSE, 1); // 0 = read then turn on pullup, 1 = read then turn off pullup, 2 = read then turn on pullup, 3 = final read + turn off pullup
tipResistanceReadings[tipResistanceReadingSlot] = TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0));
gpio_write(TIP_RESISTANCE_SENSE, (tipResistanceReadingSlot % 2) ? 0 : 1);
tipResistanceReadingSlot++;
} }
void FinishMeasureTipResistance() { void FinishMeasureTipResistance() {
gpio_write(TIP_RESISTANCE_SENSE, 0);
// read the tip uV with the current source on // Otherwise we now have the 4 samples;
uint32_t newReading = (TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0))); // _^_^ order, 3 delta's, combine these
if (newReading < lastTipReadinguV) {
return; int32_t steps[4] = {
} // Close samples
// newReading -= lastTipReadinguV; tipResistanceReadings[1] - tipResistanceReadings[0],
// MSG("Tip Delta %lu, %lu %lu \r\n", newReading - lastTipReadinguV, newReading, lastTipReadinguV); tipResistanceReadings[3] - tipResistanceReadings[2],
newReading -= lastTipReadinguV; tipResistanceReadings[1] - tipResistanceReadings[2],
// Longer time span samples
tipResistanceReadings[3] - tipResistanceReadings[0],
};
// The steps array now contains all 4 changes, these _should_ all be basically the same, but they may not be
// For example, a hot tip cooling down. In this case there will a difference in them proportional to the temp drop during that time
auto reading = (steps[0] + steps[1] + steps[2] + steps[3]) / 4;
// MSG("Tip reading %lu \r\n");
// As we are only detecting two resistances; we can split the difference for now // As we are only detecting two resistances; we can split the difference for now
uint8_t newRes = 0; uint8_t newRes = 0;
if (newReading > 8000) { if (reading > 8000) {
return; // Change nothing as probably disconnected tip // return; // Change nothing as probably disconnected tip
} else if (newReading < 5000) { } else if (reading < 5000) {
newRes = 62; newRes = 62;
} else { } else {
newRes = 80; newRes = 80;
} }
lastTipResistance = newRes; lastTipResistance = newRes;
} }
volatile bool tipMeasurementOccuring = false; volatile bool tipMeasurementOccuring = true;
volatile TickType_t nextTipMeasurement = 100;
void performTipMeasurementStep(bool start) { void performTipMeasurementStep() {
static TickType_t lastMeas = 0;
// Inter state that performs the steps to measure the resistor on the tip
// Return 1 if a measurement is ongoing
// We want to perform our startup measurements of the tip resistance until we detect one fitted
// Step 1; if not setup, we turn on pullup and then wait
if (tipMeasurementOccuring == false && (start || lastTipResistance == 0 || lastMeas == 0)) {
// Block starting if tip removed
if (isTipDisconnected()) {
return;
}
tipMeasurementOccuring = true;
lastTipResistance = 0;
lastMeas = xTaskGetTickCount();
startMeasureTipResistance();
return;
}
// Wait 100ms for settle time // Wait 100ms for settle time
if ((xTaskGetTickCount() - lastMeas) < (TICKS_100MS)) { if (xTaskGetTickCount() < (nextTipMeasurement)) {
return;
}
nextTipMeasurement = xTaskGetTickCount() + TICKS_100MS;
if (tipResistanceReadingSlot < 4) {
performTipResistanceSampleReading();
return; return;
} }
lastMeas = xTaskGetTickCount();
// We are sensing the resistance // We are sensing the resistance
FinishMeasureTipResistance(); FinishMeasureTipResistance();
@@ -311,10 +311,10 @@ void performTipMeasurementStep(bool start) {
} }
uint8_t preStartChecks() { uint8_t preStartChecks() {
performTipMeasurementStep(false); performTipMeasurementStep();
return tipMeasurementOccuring ? 1 : 0; return preStartChecksDone();
} }
uint8_t preStartChecksDone() { return (lastTipResistance == 0 || tipMeasurementOccuring) ? 0 : 1; } uint8_t preStartChecksDone() { return (lastTipResistance == 0 || tipResistanceReadingSlot < 4 || tipMeasurementOccuring) ? 0 : 1; }
// Return hardware unique ID if possible // Return hardware unique ID if possible
uint64_t getDeviceID() { uint64_t getDeviceID() {

View File

@@ -46,7 +46,8 @@ void startPIDTask(void const *argument __unused) {
getInputVoltageX10(getSettingValue(SettingsOptions::VoltageDiv), 1); getInputVoltageX10(getSettingValue(SettingsOptions::VoltageDiv), 1);
} }
while (preStartChecks() != 0) { while (preStartChecks() == 0) {
resetWatchdog();
ulTaskNotifyTake(pdTRUE, 2000); ulTaskNotifyTake(pdTRUE, 2000);
} }