diff --git a/configtool/thermistortablefile.py b/configtool/thermistortablefile.py index e03355a..ad57ba2 100644 --- a/configtool/thermistortablefile.py +++ b/configtool/thermistortablefile.py @@ -54,6 +54,7 @@ def generateTempTables(sensors, settings): ofp.output("#define NUMTABLES %d" % len(tl)) ofp.output("#define NUMTEMPS %d" % N) + ofp.output("#define TEMPTABLE_FORMAT 1") ofp.output(""); for i in range(len(tl)): @@ -65,7 +66,7 @@ def generateTempTables(sensors, settings): ofp.close(); return True - ofp.output("const uint16_t PROGMEM temptable[NUMTABLES][NUMTEMPS][2] = {") + ofp.output("const uint16_t PROGMEM temptable[NUMTABLES][NUMTEMPS][3] = {") tcount = 0 for tn in tl: @@ -102,6 +103,7 @@ def BetaTable(ofp, params, names, settings, finalTable): samples = optimizeTempTable(thrm, N, hiadc) + prev = samples[0] for i in samples: t = thrm.temp(i) if t is None: @@ -113,14 +115,18 @@ def BetaTable(ofp, params, names, settings, finalTable): vTherm = i * vadc / 1024 ptherm = vTherm * vTherm / r + if i == max(samples): c = " " else: c = "," - ostr = (" {%4s, %5s}%s // %4d C, %6.0f ohms, %0.3f V," - " %0.2f mW") % (i, int(t * 4), c, int(t), int(round(r)), - vTherm, ptherm * 1000) + + delta = (t - thrm.temp(prev)) / (prev - i) if i != prev else 0 + ostr = (" {%4s, %5s, %5s}%s // %4d C, %6.0f ohms, %0.3f V," + " %0.2f mW, m = %6.3f") % (i, int(t * 4), int(delta * 4 * 256), c, + int(t), int(round(r)), vTherm, ptherm * 1000, delta) ofp.output(ostr) + prev = i if finalTable: ofp.output(" }") @@ -145,6 +151,7 @@ def SteinhartHartTable(ofp, params, names, settings, finalTable): samples = optimizeTempTable(thrm, N, hiadc) + prev = samples[0] for i in samples: t = thrm.temp(i) if t is None: @@ -157,8 +164,12 @@ def SteinhartHartTable(ofp, params, names, settings, finalTable): c = " " else: c = "," - ofp.output(" {%4d, %5d}%s // %4d C, %6d ohms" % - (i, int(t * 4), c, int(t), int(round(r)))) + + delta = (t - thrm.temp(prev)) / (prev - i) if i != prev else 0 + ofp.output(" {%4d, %5d, %5d}%s // %4d C, %6d ohms, m = %6.3f" % + (i, int(t * 4), int(delta * 4 * 256), c, int(t), int(round(r))), + delta) + prev = i if finalTable: ofp.output(" }") diff --git a/temp.c b/temp.c index e0162cf..2095b21 100644 --- a/temp.c +++ b/temp.c @@ -196,28 +196,40 @@ static uint16_t temp_table_lookup(uint16_t temp, uint8_t sensor) { sersendf_P(PSTR("pin:%d Raw ADC:%d table entry: %d"), temp_sensors[sensor].temp_pin, temp, j); - // Wikipedia's example linear interpolation formula. - // y = ((x - x₀)y₁ + (x₁-x)y₀) / (x₁ - x₀) - // y = temp - // x = ADC reading - // x₀= temptable[j-1][0] - // x₁= temptable[j][0] - // y₀= temptable[j-1][1] - // y₁= temptable[j][1] - temp = ( - // ((x - x₀)y₁ - ((uint32_t)temp - pgm_read_word(&(temptable[table_num][j-1][0]))) * - pgm_read_word(&(temptable[table_num][j][1])) - // + - + - // (x₁-x)y₀) - (pgm_read_word(&(temptable[table_num][j][0])) - (uint32_t)temp) * - pgm_read_word(&(temptable[table_num][j - 1][1]))) - // / - / - // (x₁ - x₀) - (pgm_read_word(&(temptable[table_num][j][0])) - - pgm_read_word(&(temptable[table_num][j - 1][0]))); + #if (TEMPTABLE_FORMAT == 0) + // Wikipedia's example linear interpolation formula. + // y = ((x - x₀)y₁ + (x₁-x)y₀) / (x₁ - x₀) + // y = temp + // x = ADC reading + // x₀= temptable[j-1][0] + // x₁= temptable[j][0] + // y₀= temptable[j-1][1] + // y₁= temptable[j][1] + temp = ( + // ((x - x₀)y₁ + ((uint32_t)temp - pgm_read_word(&(temptable[table_num][j-1][0]))) * + pgm_read_word(&(temptable[table_num][j][1])) + // + + + + // (x₁-x)y₀) + (pgm_read_word(&(temptable[table_num][j][0])) - (uint32_t)temp) * + pgm_read_word(&(temptable[table_num][j - 1][1]))) + // / + / + // (x₁ - x₀) + (pgm_read_word(&(temptable[table_num][j][0])) - + pgm_read_word(&(temptable[table_num][j - 1][0]))); + #elif (TEMPTABLE_FORMAT == 1) + // Linear interpolation using pre-computed slope. + // y = y₁ - (x - x₁) * d₁ + #define X1 pgm_read_word(&(temptable[table_num][j][0])) + #define Y1 pgm_read_word(&(temptable[table_num][j][1])) + #define D1 pgm_read_word(&(temptable[table_num][j][2])) + + temp = Y1 - ((((int32_t)temp - X1) * D1 + (1<<7)) >> 8); + #else + #error "temptable format unrecognized" + #endif if (DEBUG_PID && (debug_flags & DEBUG_PID)) sersendf_P(PSTR(" temp:%d.%d"), temp / 4, (temp % 4) * 25);