Browse Source

Merge pull request #1674 from thinkyhead/fix_temperature_minmax

Cleanup temperature code
Scott Lahteine 10 years ago
parent
commit
e2576b1ac5
1 changed files with 46 additions and 41 deletions
  1. 46
    41
      Marlin/temperature.cpp

+ 46
- 41
Marlin/temperature.cpp View File

@@ -177,7 +177,7 @@ static volatile bool temp_meas_ready = false;
177 177
 // Init min and max temp with extreme values to prevent false errors during startup
178 178
 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);
179 179
 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);
180
-static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0, 0 );
180
+static int minttemp[EXTRUDERS] = { 0 };
181 181
 static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383, 16383 );
182 182
 //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
183 183
 #ifdef BED_MAXTEMP
@@ -197,8 +197,8 @@ static float analog2tempBed(int raw);
197 197
 static void updateTemperaturesFromRawValues();
198 198
 
199 199
 #ifdef WATCH_TEMP_PERIOD
200
-  int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0);
201
-  unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0);
200
+  int watch_start_temp[EXTRUDERS] = { 0 };
201
+  unsigned long watchmillis[EXTRUDERS] = { 0 };
202 202
 #endif //WATCH_TEMP_PERIOD
203 203
 
204 204
 #ifndef SOFT_PWM_SCALE
@@ -661,12 +661,6 @@ void manage_heater() {
661 661
 
662 662
   updateTemperaturesFromRawValues();
663 663
 
664
-  #ifdef HEATER_0_USES_MAX6675
665
-    float ct = current_temperature[0];
666
-    if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
667
-    if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
668
-  #endif //HEATER_0_USES_MAX6675
669
-
670 664
   unsigned long ms = millis();
671 665
 
672 666
   // Loop through all extruders
@@ -1145,28 +1139,28 @@ void disable_heater() {
1145 1139
   for (int i=0; i<EXTRUDERS; i++) setTargetHotend(0, i);
1146 1140
   setTargetBed(0);
1147 1141
 
1142
+  #define DISABLE_HEATER(NR) { \
1143
+    target_temperature[NR] = 0; \
1144
+    soft_pwm[NR] = 0; \
1145
+    WRITE_HEATER_ ## NR (LOW); \
1146
+  }
1147
+
1148 1148
   #if HAS_TEMP_0
1149 1149
     target_temperature[0] = 0;
1150 1150
     soft_pwm[0] = 0;
1151
-    WRITE_HEATER_0P(LOW); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
1151
+    WRITE_HEATER_0P(LOW); // Should HEATERS_PARALLEL apply here? Then change to DISABLE_HEATER(0)
1152 1152
   #endif
1153 1153
 
1154 1154
   #if EXTRUDERS > 1 && HAS_TEMP_1
1155
-    target_temperature[1] = 0;
1156
-    soft_pwm[1] = 0;
1157
-    WRITE_HEATER_1(LOW);
1155
+    DISABLE_HEATER(1);
1158 1156
   #endif
1159 1157
 
1160 1158
   #if EXTRUDERS > 2 && HAS_TEMP_2
1161
-    target_temperature[2] = 0;
1162
-    soft_pwm[2] = 0;
1163
-    WRITE_HEATER_2(LOW);
1159
+    DISABLE_HEATER(2);
1164 1160
   #endif
1165 1161
 
1166 1162
   #if EXTRUDERS > 3 && HAS_TEMP_3
1167
-    target_temperature[3] = 0;
1168
-    soft_pwm[3] = 0;
1169
-    WRITE_HEATER_3(LOW);
1163
+    DISABLE_HEATER(3);
1170 1164
   #endif
1171 1165
 
1172 1166
   #if HAS_TEMP_BED
@@ -1257,9 +1251,15 @@ enum TempState {
1257 1251
 // Timer 0 is shared with millies
1258 1252
 //
1259 1253
 ISR(TIMER0_COMPB_vect) {
1254
+  #ifdef TEMP_SENSOR_1_AS_REDUNDANT
1255
+    #define TEMP_SENSOR_COUNT 2
1256
+  #else 
1257
+    #define TEMP_SENSOR_COUNT EXTRUDERS
1258
+  #endif
1259
+
1260 1260
   //these variables are only accesible from the ISR, but static, so they don't lose their value
1261 1261
   static unsigned char temp_count = 0;
1262
-  static unsigned long raw_temp_value[EXTRUDERS] = { 0 };
1262
+  static unsigned long raw_temp_value[TEMP_SENSOR_COUNT] = { 0 };
1263 1263
   static unsigned long raw_temp_bed_value = 0;
1264 1264
   static TempState temp_state = StartupDelay;
1265 1265
   static unsigned char pwm_count = BIT(SOFT_PWM_SCALE);
@@ -1475,6 +1475,7 @@ ISR(TIMER0_COMPB_vect) {
1475 1475
       #endif
1476 1476
       temp_state = PrepareTemp_BED;
1477 1477
       break;
1478
+
1478 1479
     case PrepareTemp_BED:
1479 1480
       #if HAS_TEMP_BED
1480 1481
         START_ADC(TEMP_BED_PIN);
@@ -1488,6 +1489,7 @@ ISR(TIMER0_COMPB_vect) {
1488 1489
       #endif
1489 1490
       temp_state = PrepareTemp_1;
1490 1491
       break;
1492
+
1491 1493
     case PrepareTemp_1:
1492 1494
       #if HAS_TEMP_1
1493 1495
         START_ADC(TEMP_1_PIN);
@@ -1501,6 +1503,7 @@ ISR(TIMER0_COMPB_vect) {
1501 1503
       #endif
1502 1504
       temp_state = PrepareTemp_2;
1503 1505
       break;
1506
+
1504 1507
     case PrepareTemp_2:
1505 1508
       #if HAS_TEMP_2
1506 1509
         START_ADC(TEMP_2_PIN);
@@ -1514,6 +1517,7 @@ ISR(TIMER0_COMPB_vect) {
1514 1517
       #endif
1515 1518
       temp_state = PrepareTemp_3;
1516 1519
       break;
1520
+
1517 1521
     case PrepareTemp_3:
1518 1522
       #if HAS_TEMP_3
1519 1523
         START_ADC(TEMP_3_PIN);
@@ -1527,6 +1531,7 @@ ISR(TIMER0_COMPB_vect) {
1527 1531
       #endif
1528 1532
       temp_state = Prepare_FILWIDTH;
1529 1533
       break;
1534
+
1530 1535
     case Prepare_FILWIDTH:
1531 1536
       #if HAS_FILAMENT_SENSOR
1532 1537
         START_ADC(FILWIDTH_PIN);
@@ -1545,6 +1550,7 @@ ISR(TIMER0_COMPB_vect) {
1545 1550
       temp_state = PrepareTemp_0;
1546 1551
       temp_count++;
1547 1552
       break;
1553
+
1548 1554
     case StartupDelay:
1549 1555
       temp_state = PrepareTemp_0;
1550 1556
       break;
@@ -1554,7 +1560,7 @@ ISR(TIMER0_COMPB_vect) {
1554 1560
     //   SERIAL_ERRORLNPGM("Temp measurement error!");
1555 1561
     //   break;
1556 1562
   } // switch(temp_state)
1557
-    
1563
+
1558 1564
   if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256)  = 164ms.
1559 1565
     if (!temp_meas_ready) { //Only update the raw values if they have been read. Else we could be updating them during reading.
1560 1566
       #ifndef HEATER_0_USES_MAX6675
@@ -1579,52 +1585,53 @@ ISR(TIMER0_COMPB_vect) {
1579 1585
     #if HAS_FILAMENT_SENSOR
1580 1586
       current_raw_filwidth = raw_filwidth_value >> 10;  // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
1581 1587
     #endif
1582
-    
1588
+
1583 1589
     temp_meas_ready = true;
1584 1590
     temp_count = 0;
1585
-    for (int i = 0; i < EXTRUDERS; i++) raw_temp_value[i] = 0;
1591
+    for (int i = 0; i < TEMP_SENSOR_COUNT; i++) raw_temp_value[i] = 0;
1586 1592
     raw_temp_bed_value = 0;
1587 1593
 
1588
-    #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
1589
-      #define GE0 <=
1590
-      #define LE0 >=
1594
+    #ifdef HEATER_0_USES_MAX6675
1595
+      float ct = current_temperature[0];
1596
+      if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
1597
+      if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
1591 1598
     #else
1592
-      #define GE0 >=
1593
-      #define LE0 <=
1599
+      #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
1600
+        #define GE0 <=
1601
+      #else
1602
+        #define GE0 >=
1603
+      #endif
1604
+      if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
1605
+      if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0);
1594 1606
     #endif
1595
-    if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
1596
-    if (current_temperature_raw[0] LE0 minttemp_raw[0]) min_temp_error(0);
1597 1607
 
1598 1608
     #if EXTRUDERS > 1
1599 1609
       #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
1600 1610
         #define GE1 <=
1601
-        #define LE1 >=
1602 1611
       #else
1603 1612
         #define GE1 >=
1604
-        #define LE1 <=
1605 1613
       #endif
1606 1614
       if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
1607
-      if (current_temperature_raw[1] LE1 minttemp_raw[1]) min_temp_error(1);
1615
+      if (minttemp_raw[1] GE0 current_temperature_raw[1]) min_temp_error(1);
1616
+
1608 1617
       #if EXTRUDERS > 2
1609 1618
         #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
1610 1619
           #define GE2 <=
1611
-          #define LE2 >=
1612 1620
         #else
1613 1621
           #define GE2 >=
1614
-          #define LE2 <=
1615 1622
         #endif
1616 1623
         if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
1617
-        if (current_temperature_raw[2] LE2 minttemp_raw[2]) min_temp_error(2);
1624
+        if (minttemp_raw[2] GE0 current_temperature_raw[2]) min_temp_error(2);
1625
+
1618 1626
         #if EXTRUDERS > 3
1619 1627
           #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
1620 1628
             #define GE3 <=
1621
-            #define LE3 >=
1622 1629
           #else
1623 1630
             #define GE3 >=
1624
-            #define LE3 <=
1625 1631
           #endif
1626 1632
           if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
1627
-          if (current_temperature_raw[3] LE3 minttemp_raw[3]) min_temp_error(3);
1633
+          if (minttemp_raw[3] GE0 current_temperature_raw[3]) min_temp_error(3);
1634
+
1628 1635
         #endif // EXTRUDERS > 3
1629 1636
       #endif // EXTRUDERS > 2
1630 1637
     #endif // EXTRUDERS > 1
@@ -1632,10 +1639,8 @@ ISR(TIMER0_COMPB_vect) {
1632 1639
     #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
1633 1640
       #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
1634 1641
         #define GEBED <=
1635
-        #define LEBED >=
1636 1642
       #else
1637 1643
         #define GEBED >=
1638
-        #define LEBED <=
1639 1644
       #endif
1640 1645
       if (current_temperature_bed_raw GEBED bed_maxttemp_raw) {
1641 1646
         target_temperature_bed = 0;

Loading…
Cancel
Save