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

This commit is contained in:
Scott Lahteine 2018-05-27 23:27:34 -05:00 committed by GitHub
parent fbeea12026
commit c45bfc1c21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -918,17 +918,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)
/**
* Bisect search for the range of the 'raw' value, then interpolate
* proportionally between the under and over values.
*/
#define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \ #define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \
for (uint8_t i = 1; i < LEN; i++) { \ uint8_t l = 0, r = LEN, m; \
const short entry10 = (short)pgm_read_word(&TBL[i][0]); \ for (;;) { \
if (entry10 > raw) { \ m = (l + r) >> 1; \
const short entry00 = (short)pgm_read_word(&TBL[i-1][0]), \ if (m == l || m == r) return (short)pgm_read_word(&TBL[LEN-1][1]); \
entry01 = (short)pgm_read_word(&TBL[i-1][1]), \ short v00 = pgm_read_word(&TBL[m-1][0]), \
entry11 = (short)pgm_read_word(&TBL[i][1]); \ v10 = pgm_read_word(&TBL[m-0][0]); \
return entry01 + (raw - entry00) * float(entry11 - entry01) / float(entry10 - entry00); \ 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); \
} \ } \
} \ } \
return (short)pgm_read_word(&TBL[LEN-1][1]); \
}while(0) }while(0)
// Derived from RepRap FiveD extruder::getTemperature() // Derived from RepRap FiveD extruder::getTemperature()