From 6bdee87be3525d9219301bdb90d7256d92dd9f3b Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 23 Mar 2015 00:27:29 -0700 Subject: [PATCH 1/3] Cleanup temperature code - Get rid of unused temp states in the ISR, resulting in more frequent temperature reading with fewer sensors - Shrink code slightly in min/max testing --- Marlin/temperature.cpp | 215 +++++++++++++++++++++++++---------------- 1 file changed, 132 insertions(+), 83 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 26360a5144..50fc0e3813 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1240,16 +1240,26 @@ void disable_heater() { enum TempState { PrepareTemp_0, MeasureTemp_0, - PrepareTemp_BED, - MeasureTemp_BED, - PrepareTemp_1, - MeasureTemp_1, - PrepareTemp_2, - MeasureTemp_2, - PrepareTemp_3, - MeasureTemp_3, - Prepare_FILWIDTH, - Measure_FILWIDTH, + #if HAS_TEMP_BED + PrepareTemp_BED, + MeasureTemp_BED, + #endif + #if HAS_TEMP_1 + PrepareTemp_1, + MeasureTemp_1, + #endif + #if HAS_TEMP_2 + PrepareTemp_2, + MeasureTemp_2, + #endif + #if HAS_TEMP_3 + PrepareTemp_3, + MeasureTemp_3, + #endif + #if HAS_FILAMENT_SENSOR + Prepare_FILWIDTH, + Measure_FILWIDTH, + #endif StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle }; @@ -1473,78 +1483,124 @@ ISR(TIMER0_COMPB_vect) { #if HAS_TEMP_0 raw_temp_value[0] += ADC; #endif - temp_state = PrepareTemp_BED; + temp_state = + #if HAS_TEMP_BED + PrepareTemp_BED + #elif HAS_TEMP_1 + PrepareTemp_1 + #elif HAS_TEMP_2 + PrepareTemp_2 + #elif HAS_TEMP_3 + PrepareTemp_3 + #elif HAS_FILAMENT_SENSOR + Prepare_FILWIDTH + #else + PrepareTemp_0 + #endif + ; break; - case PrepareTemp_BED: - #if HAS_TEMP_BED + + #if HAS_TEMP_BED + case PrepareTemp_BED: START_ADC(TEMP_BED_PIN); - #endif - lcd_buttons_update(); - temp_state = MeasureTemp_BED; - break; - case MeasureTemp_BED: - #if HAS_TEMP_BED + lcd_buttons_update(); + temp_state = MeasureTemp_BED; + break; + case MeasureTemp_BED: raw_temp_bed_value += ADC; - #endif - temp_state = PrepareTemp_1; - break; - case PrepareTemp_1: - #if HAS_TEMP_1 + temp_state = + #if HAS_TEMP_1 + PrepareTemp_1 + #elif HAS_TEMP_2 + PrepareTemp_2 + #elif HAS_TEMP_3 + PrepareTemp_3 + #elif HAS_FILAMENT_SENSOR + Prepare_FILWIDTH + #else + PrepareTemp_0 + #endif + ; + break; + #endif + + #if HAS_TEMP_1 + case PrepareTemp_1: START_ADC(TEMP_1_PIN); - #endif - lcd_buttons_update(); - temp_state = MeasureTemp_1; - break; - case MeasureTemp_1: - #if HAS_TEMP_1 + lcd_buttons_update(); + temp_state = MeasureTemp_1; + break; + case MeasureTemp_1: raw_temp_value[1] += ADC; - #endif - temp_state = PrepareTemp_2; - break; - case PrepareTemp_2: - #if HAS_TEMP_2 + temp_state = + #if HAS_TEMP_2 + PrepareTemp_2 + #elif HAS_TEMP_3 + PrepareTemp_3 + #elif HAS_FILAMENT_SENSOR + Prepare_FILWIDTH + #else + PrepareTemp_0 + #endif + ; + break; + #endif + + #if HAS_TEMP_2 + case PrepareTemp_2: START_ADC(TEMP_2_PIN); - #endif - lcd_buttons_update(); - temp_state = MeasureTemp_2; - break; - case MeasureTemp_2: - #if HAS_TEMP_2 + lcd_buttons_update(); + temp_state = MeasureTemp_2; + break; + case MeasureTemp_2: raw_temp_value[2] += ADC; - #endif - temp_state = PrepareTemp_3; - break; - case PrepareTemp_3: - #if HAS_TEMP_3 + temp_state = + #if HAS_TEMP_3 + PrepareTemp_3 + #elif HAS_FILAMENT_SENSOR + Prepare_FILWIDTH + #else + PrepareTemp_0 + #endif + ; + break; + #endif + + #if HAS_TEMP_3 + case PrepareTemp_3: START_ADC(TEMP_3_PIN); - #endif - lcd_buttons_update(); - temp_state = MeasureTemp_3; - break; - case MeasureTemp_3: - #if HAS_TEMP_3 + lcd_buttons_update(); + temp_state = MeasureTemp_3; + break; + case MeasureTemp_3: raw_temp_value[3] += ADC; - #endif - temp_state = Prepare_FILWIDTH; - break; - case Prepare_FILWIDTH: - #if HAS_FILAMENT_SENSOR + temp_state = + #if HAS_FILAMENT_SENSOR + Prepare_FILWIDTH + #else + PrepareTemp_0 + #endif + ; + break; + #endif + + #if HAS_FILAMENT_SENSOR + case Prepare_FILWIDTH: START_ADC(FILWIDTH_PIN); - #endif - lcd_buttons_update(); - temp_state = Measure_FILWIDTH; - break; - case Measure_FILWIDTH: - #if HAS_FILAMENT_SENSOR + lcd_buttons_update(); + temp_state = Measure_FILWIDTH; + break; + case Measure_FILWIDTH: // raw_filwidth_value += ADC; //remove to use an IIR filter approach if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data. raw_filwidth_value -= (raw_filwidth_value>>7); //multiply raw_filwidth_value by 127/128 raw_filwidth_value += ((unsigned long)ADC<<7); //add new ADC reading } - #endif - temp_state = PrepareTemp_0; - temp_count++; - break; + temp_state = PrepareTemp_0; + temp_count++; + break; + #endif + case StartupDelay: temp_state = PrepareTemp_0; break; @@ -1554,7 +1610,7 @@ ISR(TIMER0_COMPB_vect) { // SERIAL_ERRORLNPGM("Temp measurement error!"); // break; } // switch(temp_state) - + if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms. if (!temp_meas_ready) { //Only update the raw values if they have been read. Else we could be updating them during reading. #ifndef HEATER_0_USES_MAX6675 @@ -1579,7 +1635,7 @@ ISR(TIMER0_COMPB_vect) { #if HAS_FILAMENT_SENSOR current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach #endif - + temp_meas_ready = true; temp_count = 0; for (int i = 0; i < EXTRUDERS; i++) raw_temp_value[i] = 0; @@ -1587,44 +1643,39 @@ ISR(TIMER0_COMPB_vect) { #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP #define GE0 <= - #define LE0 >= #else #define GE0 >= - #define LE0 <= #endif if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0); - if (current_temperature_raw[0] LE0 minttemp_raw[0]) min_temp_error(0); + if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0); #if EXTRUDERS > 1 #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP #define GE1 <= - #define LE1 >= #else #define GE1 >= - #define LE1 <= #endif if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1); - if (current_temperature_raw[1] LE1 minttemp_raw[1]) min_temp_error(1); + if (minttemp_raw[1] GE0 current_temperature_raw[1]) min_temp_error(1); + #if EXTRUDERS > 2 #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP #define GE2 <= - #define LE2 >= #else #define GE2 >= - #define LE2 <= #endif if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2); - if (current_temperature_raw[2] LE2 minttemp_raw[2]) min_temp_error(2); + if (minttemp_raw[2] GE0 current_temperature_raw[2]) min_temp_error(2); + #if EXTRUDERS > 3 #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP #define GE3 <= - #define LE3 >= #else #define GE3 >= - #define LE3 <= #endif if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3); - if (current_temperature_raw[3] LE3 minttemp_raw[3]) min_temp_error(3); + if (minttemp_raw[3] GE0 current_temperature_raw[3]) min_temp_error(3); + #endif // EXTRUDERS > 3 #endif // EXTRUDERS > 2 #endif // EXTRUDERS > 1 @@ -1632,10 +1683,8 @@ ISR(TIMER0_COMPB_vect) { #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0) #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP #define GEBED <= - #define LEBED >= #else #define GEBED >= - #define LEBED <= #endif if (current_temperature_bed_raw GEBED bed_maxttemp_raw) { target_temperature_bed = 0; From 1fac3886640ac4aa372d7958435f3bef42c51424 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 23 Mar 2015 15:18:22 -0700 Subject: [PATCH 2/3] Revert measurement loop --- Marlin/temperature.cpp | 204 ++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 127 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 50fc0e3813..1582c437a2 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1145,28 +1145,28 @@ void disable_heater() { for (int i=0; i 1 && HAS_TEMP_1 - target_temperature[1] = 0; - soft_pwm[1] = 0; - WRITE_HEATER_1(LOW); + DISABLE_HEATER(1); #endif #if EXTRUDERS > 2 && HAS_TEMP_2 - target_temperature[2] = 0; - soft_pwm[2] = 0; - WRITE_HEATER_2(LOW); + DISABLE_HEATER(2); #endif #if EXTRUDERS > 3 && HAS_TEMP_3 - target_temperature[3] = 0; - soft_pwm[3] = 0; - WRITE_HEATER_3(LOW); + DISABLE_HEATER(3); #endif #if HAS_TEMP_BED @@ -1240,26 +1240,16 @@ void disable_heater() { enum TempState { PrepareTemp_0, MeasureTemp_0, - #if HAS_TEMP_BED - PrepareTemp_BED, - MeasureTemp_BED, - #endif - #if HAS_TEMP_1 - PrepareTemp_1, - MeasureTemp_1, - #endif - #if HAS_TEMP_2 - PrepareTemp_2, - MeasureTemp_2, - #endif - #if HAS_TEMP_3 - PrepareTemp_3, - MeasureTemp_3, - #endif - #if HAS_FILAMENT_SENSOR - Prepare_FILWIDTH, - Measure_FILWIDTH, - #endif + PrepareTemp_BED, + MeasureTemp_BED, + PrepareTemp_1, + MeasureTemp_1, + PrepareTemp_2, + MeasureTemp_2, + PrepareTemp_3, + MeasureTemp_3, + Prepare_FILWIDTH, + Measure_FILWIDTH, StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle }; @@ -1483,123 +1473,83 @@ ISR(TIMER0_COMPB_vect) { #if HAS_TEMP_0 raw_temp_value[0] += ADC; #endif - temp_state = - #if HAS_TEMP_BED - PrepareTemp_BED - #elif HAS_TEMP_1 - PrepareTemp_1 - #elif HAS_TEMP_2 - PrepareTemp_2 - #elif HAS_TEMP_3 - PrepareTemp_3 - #elif HAS_FILAMENT_SENSOR - Prepare_FILWIDTH - #else - PrepareTemp_0 - #endif - ; + temp_state = PrepareTemp_BED; break; - #if HAS_TEMP_BED - case PrepareTemp_BED: + case PrepareTemp_BED: + #if HAS_TEMP_BED START_ADC(TEMP_BED_PIN); - lcd_buttons_update(); - temp_state = MeasureTemp_BED; - break; - case MeasureTemp_BED: + #endif + lcd_buttons_update(); + temp_state = MeasureTemp_BED; + break; + case MeasureTemp_BED: + #if HAS_TEMP_BED raw_temp_bed_value += ADC; - temp_state = - #if HAS_TEMP_1 - PrepareTemp_1 - #elif HAS_TEMP_2 - PrepareTemp_2 - #elif HAS_TEMP_3 - PrepareTemp_3 - #elif HAS_FILAMENT_SENSOR - Prepare_FILWIDTH - #else - PrepareTemp_0 - #endif - ; - break; - #endif + #endif + temp_state = PrepareTemp_1; + break; - #if HAS_TEMP_1 - case PrepareTemp_1: + case PrepareTemp_1: + #if HAS_TEMP_1 START_ADC(TEMP_1_PIN); - lcd_buttons_update(); - temp_state = MeasureTemp_1; - break; - case MeasureTemp_1: + #endif + lcd_buttons_update(); + temp_state = MeasureTemp_1; + break; + case MeasureTemp_1: + #if HAS_TEMP_1 raw_temp_value[1] += ADC; - temp_state = - #if HAS_TEMP_2 - PrepareTemp_2 - #elif HAS_TEMP_3 - PrepareTemp_3 - #elif HAS_FILAMENT_SENSOR - Prepare_FILWIDTH - #else - PrepareTemp_0 - #endif - ; - break; - #endif + #endif + temp_state = PrepareTemp_2; + break; - #if HAS_TEMP_2 - case PrepareTemp_2: + case PrepareTemp_2: + #if HAS_TEMP_2 START_ADC(TEMP_2_PIN); - lcd_buttons_update(); - temp_state = MeasureTemp_2; - break; - case MeasureTemp_2: + #endif + lcd_buttons_update(); + temp_state = MeasureTemp_2; + break; + case MeasureTemp_2: + #if HAS_TEMP_2 raw_temp_value[2] += ADC; - temp_state = - #if HAS_TEMP_3 - PrepareTemp_3 - #elif HAS_FILAMENT_SENSOR - Prepare_FILWIDTH - #else - PrepareTemp_0 - #endif - ; - break; - #endif + #endif + temp_state = PrepareTemp_3; + break; - #if HAS_TEMP_3 - case PrepareTemp_3: + case PrepareTemp_3: + #if HAS_TEMP_3 START_ADC(TEMP_3_PIN); - lcd_buttons_update(); - temp_state = MeasureTemp_3; - break; - case MeasureTemp_3: + #endif + lcd_buttons_update(); + temp_state = MeasureTemp_3; + break; + case MeasureTemp_3: + #if HAS_TEMP_3 raw_temp_value[3] += ADC; - temp_state = - #if HAS_FILAMENT_SENSOR - Prepare_FILWIDTH - #else - PrepareTemp_0 - #endif - ; - break; - #endif + #endif + temp_state = Prepare_FILWIDTH; + break; - #if HAS_FILAMENT_SENSOR - case Prepare_FILWIDTH: + case Prepare_FILWIDTH: + #if HAS_FILAMENT_SENSOR START_ADC(FILWIDTH_PIN); - lcd_buttons_update(); - temp_state = Measure_FILWIDTH; - break; - case Measure_FILWIDTH: + #endif + lcd_buttons_update(); + temp_state = Measure_FILWIDTH; + break; + case Measure_FILWIDTH: + #if HAS_FILAMENT_SENSOR // raw_filwidth_value += ADC; //remove to use an IIR filter approach if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data. raw_filwidth_value -= (raw_filwidth_value>>7); //multiply raw_filwidth_value by 127/128 raw_filwidth_value += ((unsigned long)ADC<<7); //add new ADC reading } - temp_state = PrepareTemp_0; - temp_count++; - break; - #endif + #endif + temp_state = PrepareTemp_0; + temp_count++; + break; case StartupDelay: temp_state = PrepareTemp_0; From fc53e43de6e849be2001c70d604fd2a56948a491 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 23 Mar 2015 20:55:48 -0700 Subject: [PATCH 3/3] Don't test raw temp for MAX6675 - Remove test of raw temp for thermistor 0 when using MAX6675 - Longer `raw_temp_value` array for `TEMP_SENSOR_1_AS_REDUNDANT` --- Marlin/temperature.cpp | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 1582c437a2..cf431cc6f4 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -177,7 +177,7 @@ static volatile bool temp_meas_ready = false; // Init min and max temp with extreme values to prevent false errors during startup static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP); static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP); -static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0, 0 ); +static int minttemp[EXTRUDERS] = { 0 }; static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383, 16383 ); //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */ #ifdef BED_MAXTEMP @@ -197,8 +197,8 @@ static float analog2tempBed(int raw); static void updateTemperaturesFromRawValues(); #ifdef WATCH_TEMP_PERIOD - int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0); - unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0); + int watch_start_temp[EXTRUDERS] = { 0 }; + unsigned long watchmillis[EXTRUDERS] = { 0 }; #endif //WATCH_TEMP_PERIOD #ifndef SOFT_PWM_SCALE @@ -661,12 +661,6 @@ void manage_heater() { updateTemperaturesFromRawValues(); - #ifdef HEATER_0_USES_MAX6675 - float ct = current_temperature[0]; - if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0); - if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0); - #endif //HEATER_0_USES_MAX6675 - unsigned long ms = millis(); // Loop through all extruders @@ -1257,9 +1251,15 @@ enum TempState { // Timer 0 is shared with millies // ISR(TIMER0_COMPB_vect) { + #ifdef TEMP_SENSOR_1_AS_REDUNDANT + #define TEMP_SENSOR_COUNT 2 + #else + #define TEMP_SENSOR_COUNT EXTRUDERS + #endif + //these variables are only accesible from the ISR, but static, so they don't lose their value static unsigned char temp_count = 0; - static unsigned long raw_temp_value[EXTRUDERS] = { 0 }; + static unsigned long raw_temp_value[TEMP_SENSOR_COUNT] = { 0 }; static unsigned long raw_temp_bed_value = 0; static TempState temp_state = StartupDelay; static unsigned char pwm_count = BIT(SOFT_PWM_SCALE); @@ -1588,16 +1588,22 @@ ISR(TIMER0_COMPB_vect) { temp_meas_ready = true; temp_count = 0; - for (int i = 0; i < EXTRUDERS; i++) raw_temp_value[i] = 0; + for (int i = 0; i < TEMP_SENSOR_COUNT; i++) raw_temp_value[i] = 0; raw_temp_bed_value = 0; - #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP - #define GE0 <= + #ifdef HEATER_0_USES_MAX6675 + float ct = current_temperature[0]; + if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0); + if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0); #else - #define GE0 >= + #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP + #define GE0 <= + #else + #define GE0 >= + #endif + if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0); + if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0); #endif - if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0); - if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0); #if EXTRUDERS > 1 #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP