From e937b052afbdea8b994596e6ffc3d3a762b2a178 Mon Sep 17 00:00:00 2001 From: Markus Amsler Date: Tue, 12 Apr 2011 20:00:18 +0200 Subject: [PATCH] Add support for multiple thermistor tables. DEFINE_TEMP_SENSOR takes one additional argument. For TT_THERMISOR you can specify there which thermistor table to use. --- ThermistorTable.h | 11 +++++++++- analog.c | 2 +- config.gen3.h | 6 +++--- config.gen6.h | 4 ++-- config.h.dist | 8 +++---- config.ramps.h | 6 +++--- extruder/ThermistorTable.h | 11 +++++++++- extruder/analog.c | 2 +- extruder/config.h.dist | 4 ++-- extruder/temp.c | 43 ++++++++++++++++++++------------------ extruder/temp.h | 2 +- temp.c | 43 ++++++++++++++++++++------------------ temp.h | 2 +- 13 files changed, 84 insertions(+), 60 deletions(-) diff --git a/ThermistorTable.h b/ThermistorTable.h index f21b24f..4013b1a 100644 --- a/ThermistorTable.h +++ b/ThermistorTable.h @@ -1,4 +1,11 @@ // default thermistor lookup table + +// How many thermistor tables we have +#define NUMTABLES 1 + +#define THERMISTOR_EXTRUDER 0 +// #define THERMISTOR_BED 1 + // Thermistor lookup table, generated with --num-temps=50 and trimmed in lower temperature ranges. // You may be able to improve the accuracy of this table in various ways. // 1. Measure the actual resistance of the resistor. It's "nominally" 4.7K, but that's ± 5%. @@ -17,7 +24,8 @@ // max adc: 1023 #define NUMTEMPS 20 // {ADC, temp*4 }, // temp -uint16_t temptable[NUMTEMPS][2] PROGMEM = { +uint16_t temptable[NUMTABLES][NUMTEMPS][2] PROGMEM = { +{ {1, 3364}, // 841.027617469 C {21, 1329}, // 332.486789769 C {41, 1104}, // 276.102666373 C @@ -38,4 +46,5 @@ uint16_t temptable[NUMTEMPS][2] PROGMEM = { {881, 219}, // 54.8051659223 C {981, 93}, // 23.4825243529 C {1010, 1} // 0.498606463441 C +} }; diff --git a/analog.c b/analog.c index 7dc0aca..2487e3c 100644 --- a/analog.c +++ b/analog.c @@ -11,7 +11,7 @@ /* OR-combined mask of all channels */ #undef DEFINE_TEMP_SENSOR //! automagically generate analog_mask from DEFINE_TEMP_SENSOR entries in config.h -#define DEFINE_TEMP_SENSOR(name, type, pin) | (((type == TT_THERMISTOR) || (type == TT_AD595)) ? 1 << (pin) : 0) +#define DEFINE_TEMP_SENSOR(name, type, pin, additional) | (((type == TT_THERMISTOR) || (type == TT_AD595)) ? 1 << (pin) : 0) static const uint8_t analog_mask = 0 #include "config.h" ; diff --git a/config.gen3.h b/config.gen3.h index 0f40d74..49dcdad 100644 --- a/config.gen3.h +++ b/config.gen3.h @@ -254,9 +254,9 @@ #define DEFINE_TEMP_SENSOR(...) #endif -// name type pin -DEFINE_TEMP_SENSOR(noheater, TT_INTERCOM, 0) -// DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, 1) +// name type pin additional +DEFINE_TEMP_SENSOR(noheater, TT_INTERCOM, 0, 0) +// DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, 1, 0) diff --git a/config.gen6.h b/config.gen6.h index d14e9a3..6fe6790 100644 --- a/config.gen6.h +++ b/config.gen6.h @@ -247,8 +247,8 @@ #define DEFINE_TEMP_SENSOR(...) #endif -// name type pin -DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, PINA5) +// name type pin additional +DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, PINA5, THERMISTOR_EXTRUDER) diff --git a/config.h.dist b/config.h.dist index 1765d84..623cf88 100644 --- a/config.h.dist +++ b/config.h.dist @@ -247,12 +247,12 @@ #define DEFINE_TEMP_SENSOR(...) #endif -// name type pin -DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, 0) -// DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, 1) +// name type pin additional +DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, 0, THERMISTOR_EXTRUDER) +// DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, 1, THERMISTOR_EXTRUDER) // "noheater" is a special name for a sensor which doesn't have a heater. // Use "M105 P#" to read it, where # is a zero-based index into this list. -// DEFINE_TEMP_SENSOR(noheater, TT_THERMISTOR, 1) +// DEFINE_TEMP_SENSOR(noheater, TT_THERMISTOR, 1, 0) diff --git a/config.ramps.h b/config.ramps.h index 463d71e..9ebf9e0 100644 --- a/config.ramps.h +++ b/config.ramps.h @@ -256,9 +256,9 @@ temperature is "achieved" for purposes of M109 and friends when actual temperatu #define DEFINE_TEMP_SENSOR(...) #endif -// name type pin -DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, AIO2_PIN) -DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, AIO1_PIN) +// name type pin additional +DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, AIO2_PIN, THERMISTOR_EXTRUDER) +DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, AIO1_PIN, THERMISTOR_EXTRUDER) diff --git a/extruder/ThermistorTable.h b/extruder/ThermistorTable.h index f21b24f..4013b1a 100644 --- a/extruder/ThermistorTable.h +++ b/extruder/ThermistorTable.h @@ -1,4 +1,11 @@ // default thermistor lookup table + +// How many thermistor tables we have +#define NUMTABLES 1 + +#define THERMISTOR_EXTRUDER 0 +// #define THERMISTOR_BED 1 + // Thermistor lookup table, generated with --num-temps=50 and trimmed in lower temperature ranges. // You may be able to improve the accuracy of this table in various ways. // 1. Measure the actual resistance of the resistor. It's "nominally" 4.7K, but that's ± 5%. @@ -17,7 +24,8 @@ // max adc: 1023 #define NUMTEMPS 20 // {ADC, temp*4 }, // temp -uint16_t temptable[NUMTEMPS][2] PROGMEM = { +uint16_t temptable[NUMTABLES][NUMTEMPS][2] PROGMEM = { +{ {1, 3364}, // 841.027617469 C {21, 1329}, // 332.486789769 C {41, 1104}, // 276.102666373 C @@ -38,4 +46,5 @@ uint16_t temptable[NUMTEMPS][2] PROGMEM = { {881, 219}, // 54.8051659223 C {981, 93}, // 23.4825243529 C {1010, 1} // 0.498606463441 C +} }; diff --git a/extruder/analog.c b/extruder/analog.c index 7dc0aca..2487e3c 100644 --- a/extruder/analog.c +++ b/extruder/analog.c @@ -11,7 +11,7 @@ /* OR-combined mask of all channels */ #undef DEFINE_TEMP_SENSOR //! automagically generate analog_mask from DEFINE_TEMP_SENSOR entries in config.h -#define DEFINE_TEMP_SENSOR(name, type, pin) | (((type == TT_THERMISTOR) || (type == TT_AD595)) ? 1 << (pin) : 0) +#define DEFINE_TEMP_SENSOR(name, type, pin, additional) | (((type == TT_THERMISTOR) || (type == TT_AD595)) ? 1 << (pin) : 0) static const uint8_t analog_mask = 0 #include "config.h" ; diff --git a/extruder/config.h.dist b/extruder/config.h.dist index 18251b0..cafd78c 100644 --- a/extruder/config.h.dist +++ b/extruder/config.h.dist @@ -65,8 +65,8 @@ #define TEMP_RESIDENCY_TIME 60 #ifdef DEFINE_TEMP_SENSOR -DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, TEMP_PIN_CHANNEL) -DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, TEMP_BED_PIN_CHANNEL) +DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, TEMP_PIN_CHANNEL, THERMISTOR_EXTRUDER) +DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, TEMP_BED_PIN_CHANNEL, THERMISTOR_EXTRUDER) #endif #ifdef DEFINE_HEATER diff --git a/extruder/temp.c b/extruder/temp.c index fbc20b9..1349509 100644 --- a/extruder/temp.c +++ b/extruder/temp.c @@ -21,6 +21,18 @@ #include "intercom.h" #endif +#ifdef TEMP_MAX6675 +#endif + +#ifdef TEMP_THERMISTOR +#include "analog.h" +#include "ThermistorTable.h" +#endif + +#ifdef TEMP_AD595 +#include "analog.h" +#endif + typedef enum { PRESENT, TCOPEN @@ -31,11 +43,12 @@ typedef struct { temp_type_t temp_type; ///< type of sensor uint8_t temp_pin; ///< pin that sensor is on heater_t heater; ///< associated heater if any + uint8_t additional; ///< additional, sensor type specifc config } temp_sensor_definition_t; #undef DEFINE_TEMP_SENSOR /// help build list of sensors from entries in config.h -#define DEFINE_TEMP_SENSOR(name, type, pin) { (type), (pin), (HEATER_ ## name) }, +#define DEFINE_TEMP_SENSOR(name, type, pin, additional) { (type), (pin), (HEATER_ ## name), (additional) }, static const temp_sensor_definition_t temp_sensors[NUM_TEMP_SENSORS] = { #include "config.h" @@ -54,18 +67,6 @@ struct { uint16_t next_read_time; ///< how long until we can read this sensor again? } temp_sensors_runtime[NUM_TEMP_SENSORS]; -#ifdef TEMP_MAX6675 -#endif - -#ifdef TEMP_THERMISTOR -#include "analog.h" -#include "ThermistorTable.h" -#endif - -#ifdef TEMP_AD595 -#include "analog.h" -#endif - /// set up temp sensors. Currently only the 'intercom' sensor needs initialisation. void temp_init() { temp_sensor_t i; @@ -165,13 +166,15 @@ void temp_sensor_tick() { #ifdef TEMP_THERMISTOR case TT_THERMISTOR: do { - uint8_t j; + uint8_t j, table_num; //Read current temperature temp = analog_read(temp_sensors[i].temp_pin); + // for thermistors the thermistor table number is in the additional field + table_num = temp_sensors[i].additional; //Calculate real temperature based on lookup table for (j = 1; j < NUMTEMPS; j++) { - if (pgm_read_word(&(temptable[j][0])) > temp) { + if (pgm_read_word(&(temptable[table_num][j][0])) > temp) { // Thermistor table is already in 14.2 fixed point #ifndef EXTRUDER if (debug_flags & DEBUG_PID) @@ -189,17 +192,17 @@ void temp_sensor_tick() { // Wikipedia's example linear interpolation formula. temp = ( // ((x - x₀)y₁ - ((uint32_t)temp - pgm_read_word(&(temptable[j-1][0]))) * pgm_read_word(&(temptable[j][1])) + ((uint32_t)temp - pgm_read_word(&(temptable[table_num][j-1][0]))) * pgm_read_word(&(temptable[table_num][j][1])) // + + // (x₁-x) - (pgm_read_word(&(temptable[j][0])) - (uint32_t)temp) + (pgm_read_word(&(temptable[table_num][j][0])) - (uint32_t)temp) // y₀ ) - * pgm_read_word(&(temptable[j-1][1]))) + * pgm_read_word(&(temptable[table_num][j-1][1]))) // / / // (x₁ - x₀) - (pgm_read_word(&(temptable[j][0])) - pgm_read_word(&(temptable[j-1][0]))); + (pgm_read_word(&(temptable[table_num][j][0])) - pgm_read_word(&(temptable[table_num][j-1][0]))); #ifndef EXTRUDER if (debug_flags & DEBUG_PID) sersendf_P(PSTR(" temp:%d.%d"),temp/4,(temp%4)*25); @@ -215,7 +218,7 @@ void temp_sensor_tick() { //Clamp for overflows if (j == NUMTEMPS) - temp = temptable[NUMTEMPS-1][1]; + temp = temptable[table_num][NUMTEMPS-1][1]; temp_sensors_runtime[i].next_read_time = 0; } while (0); diff --git a/extruder/temp.h b/extruder/temp.h index 6a1d5e5..68d1afe 100644 --- a/extruder/temp.h +++ b/extruder/temp.h @@ -13,7 +13,7 @@ we still need to specify which analog pins we use in machine.h for the analog se */ #undef DEFINE_TEMP_SENSOR -#define DEFINE_TEMP_SENSOR(name, type, pin) TEMP_SENSOR_ ## name, +#define DEFINE_TEMP_SENSOR(name, type, pin, additional) TEMP_SENSOR_ ## name, typedef enum { #include "config.h" NUM_TEMP_SENSORS, diff --git a/temp.c b/temp.c index fbc20b9..1349509 100644 --- a/temp.c +++ b/temp.c @@ -21,6 +21,18 @@ #include "intercom.h" #endif +#ifdef TEMP_MAX6675 +#endif + +#ifdef TEMP_THERMISTOR +#include "analog.h" +#include "ThermistorTable.h" +#endif + +#ifdef TEMP_AD595 +#include "analog.h" +#endif + typedef enum { PRESENT, TCOPEN @@ -31,11 +43,12 @@ typedef struct { temp_type_t temp_type; ///< type of sensor uint8_t temp_pin; ///< pin that sensor is on heater_t heater; ///< associated heater if any + uint8_t additional; ///< additional, sensor type specifc config } temp_sensor_definition_t; #undef DEFINE_TEMP_SENSOR /// help build list of sensors from entries in config.h -#define DEFINE_TEMP_SENSOR(name, type, pin) { (type), (pin), (HEATER_ ## name) }, +#define DEFINE_TEMP_SENSOR(name, type, pin, additional) { (type), (pin), (HEATER_ ## name), (additional) }, static const temp_sensor_definition_t temp_sensors[NUM_TEMP_SENSORS] = { #include "config.h" @@ -54,18 +67,6 @@ struct { uint16_t next_read_time; ///< how long until we can read this sensor again? } temp_sensors_runtime[NUM_TEMP_SENSORS]; -#ifdef TEMP_MAX6675 -#endif - -#ifdef TEMP_THERMISTOR -#include "analog.h" -#include "ThermistorTable.h" -#endif - -#ifdef TEMP_AD595 -#include "analog.h" -#endif - /// set up temp sensors. Currently only the 'intercom' sensor needs initialisation. void temp_init() { temp_sensor_t i; @@ -165,13 +166,15 @@ void temp_sensor_tick() { #ifdef TEMP_THERMISTOR case TT_THERMISTOR: do { - uint8_t j; + uint8_t j, table_num; //Read current temperature temp = analog_read(temp_sensors[i].temp_pin); + // for thermistors the thermistor table number is in the additional field + table_num = temp_sensors[i].additional; //Calculate real temperature based on lookup table for (j = 1; j < NUMTEMPS; j++) { - if (pgm_read_word(&(temptable[j][0])) > temp) { + if (pgm_read_word(&(temptable[table_num][j][0])) > temp) { // Thermistor table is already in 14.2 fixed point #ifndef EXTRUDER if (debug_flags & DEBUG_PID) @@ -189,17 +192,17 @@ void temp_sensor_tick() { // Wikipedia's example linear interpolation formula. temp = ( // ((x - x₀)y₁ - ((uint32_t)temp - pgm_read_word(&(temptable[j-1][0]))) * pgm_read_word(&(temptable[j][1])) + ((uint32_t)temp - pgm_read_word(&(temptable[table_num][j-1][0]))) * pgm_read_word(&(temptable[table_num][j][1])) // + + // (x₁-x) - (pgm_read_word(&(temptable[j][0])) - (uint32_t)temp) + (pgm_read_word(&(temptable[table_num][j][0])) - (uint32_t)temp) // y₀ ) - * pgm_read_word(&(temptable[j-1][1]))) + * pgm_read_word(&(temptable[table_num][j-1][1]))) // / / // (x₁ - x₀) - (pgm_read_word(&(temptable[j][0])) - pgm_read_word(&(temptable[j-1][0]))); + (pgm_read_word(&(temptable[table_num][j][0])) - pgm_read_word(&(temptable[table_num][j-1][0]))); #ifndef EXTRUDER if (debug_flags & DEBUG_PID) sersendf_P(PSTR(" temp:%d.%d"),temp/4,(temp%4)*25); @@ -215,7 +218,7 @@ void temp_sensor_tick() { //Clamp for overflows if (j == NUMTEMPS) - temp = temptable[NUMTEMPS-1][1]; + temp = temptable[table_num][NUMTEMPS-1][1]; temp_sensors_runtime[i].next_read_time = 0; } while (0); diff --git a/temp.h b/temp.h index 6a1d5e5..68d1afe 100644 --- a/temp.h +++ b/temp.h @@ -13,7 +13,7 @@ we still need to specify which analog pins we use in machine.h for the analog se */ #undef DEFINE_TEMP_SENSOR -#define DEFINE_TEMP_SENSOR(name, type, pin) TEMP_SENSOR_ ## name, +#define DEFINE_TEMP_SENSOR(name, type, pin, additional) TEMP_SENSOR_ ## name, typedef enum { #include "config.h" NUM_TEMP_SENSORS,