Faster (bisect) search in thermistor tables (#10883)

This commit is contained in:
Scott Lahteine 2018-05-27 23:27:51 -05:00 committed by GitHub
parent 0ca9db7051
commit 1ce97f1f6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -937,17 +937,25 @@ void Temperature::manage_heater() {
#define TEMP_AD595(RAW) ((RAW) * 5.0 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET) #define TEMP_AD595(RAW) ((RAW) * 5.0 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)
#define TEMP_AD8495(RAW) ((RAW) * 6.6 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD8495_GAIN) + TEMP_SENSOR_AD8495_OFFSET) #define TEMP_AD8495(RAW) ((RAW) * 6.6 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD8495_GAIN) + TEMP_SENSOR_AD8495_OFFSET)
#define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \ /**
for (uint8_t i = 1; i < LEN; i++) { \ * Bisect search for the range of the 'raw' value, then interpolate
const short entry10 = (short)pgm_read_word(&TBL[i][0]); \ * proportionally between the under and over values.
if (entry10 > raw) { \ */
const short entry00 = (short)pgm_read_word(&TBL[i-1][0]), \ #define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \
entry01 = (short)pgm_read_word(&TBL[i-1][1]), \ uint8_t l = 0, r = LEN, m; \
entry11 = (short)pgm_read_word(&TBL[i][1]); \ for (;;) { \
return entry01 + (raw - entry00) * float(entry11 - entry01) / float(entry10 - entry00); \ m = (l + r) >> 1; \
} \ if (m == l || m == r) return (short)pgm_read_word(&TBL[LEN-1][1]); \
} \ short v00 = pgm_read_word(&TBL[m-1][0]), \
return (short)pgm_read_word(&TBL[LEN-1][1]); \ v10 = pgm_read_word(&TBL[m-0][0]); \
if (raw < v00) r = m; \
else if (raw > v10) l = m; \
else { \
const short v01 = (short)pgm_read_word(&TBL[m-1][1]), \
v11 = (short)pgm_read_word(&TBL[m-0][1]); \
return v01 + (raw - v00) * float(v11 - v01) / float(v10 - v00); \
} \
} \
}while(0) }while(0)
// Derived from RepRap FiveD extruder::getTemperature() // Derived from RepRap FiveD extruder::getTemperature()