Browse Source

Merge pull request #1603 from thinkyhead/issue_1523_compiler_error

Simplify manage_heater
Scott Lahteine 9 years ago
parent
commit
901c3c5392
1 changed files with 98 additions and 94 deletions
  1. 98
    94
      Marlin/temperature.cpp

+ 98
- 94
Marlin/temperature.cpp View File

@@ -75,6 +75,10 @@
75 75
 //============================= public variables ============================
76 76
 //===========================================================================
77 77
 
78
+#ifdef K1 // Defined in Configuration.h in the PID settings
79
+  #define K2 (1.0-K1)
80
+#endif
81
+
78 82
 // Sampling period of the temperature routine
79 83
 #ifdef PID_dT
80 84
   #undef PID_dT
@@ -127,8 +131,6 @@ static volatile bool temp_meas_ready = false;
127 131
   static float pid_error[EXTRUDERS];
128 132
   static float temp_iState_min[EXTRUDERS];
129 133
   static float temp_iState_max[EXTRUDERS];
130
-  // static float pid_input[EXTRUDERS];
131
-  // static float pid_output[EXTRUDERS];
132 134
   static bool pid_reset[EXTRUDERS];
133 135
 #endif //PIDTEMP
134 136
 #ifdef PIDTEMPBED
@@ -546,12 +548,102 @@ void bed_max_temp_error(void) {
546 548
   _temp_error(-1, MSG_MAXTEMP_BED_OFF, MSG_ERR_MAXTEMP_BED);
547 549
 }
548 550
 
551
+float get_pid_output(int e) {
552
+  float pid_output;
553
+  #ifdef PIDTEMP
554
+    #ifndef PID_OPENLOOP
555
+      pid_error[e] = target_temperature[e] - current_temperature[e];
556
+      if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
557
+        pid_output = BANG_MAX;
558
+        pid_reset[e] = true;
559
+      }
560
+      else if (pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
561
+        pid_output = 0;
562
+        pid_reset[e] = true;
563
+      }
564
+      else {
565
+        if (pid_reset[e]) {
566
+          temp_iState[e] = 0.0;
567
+          pid_reset[e] = false;
568
+        }
569
+        pTerm[e] = PID_PARAM(Kp,e) * pid_error[e];
570
+        temp_iState[e] += pid_error[e];
571
+        temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
572
+        iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e];
573
+
574
+        dTerm[e] = K2 * PID_PARAM(Kd,e) * (current_temperature[e] - temp_dState[e]) + K1 * dTerm[e];
575
+        pid_output = pTerm[e] + iTerm[e] - dTerm[e];
576
+        if (pid_output > PID_MAX) {
577
+          if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
578
+          pid_output = PID_MAX;
579
+        }
580
+        else if (pid_output < 0) {
581
+          if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
582
+          pid_output = 0;
583
+        }
584
+      }
585
+      temp_dState[e] = current_temperature[e];
586
+    #else
587
+      pid_output = constrain(target_temperature[e], 0, PID_MAX);
588
+    #endif //PID_OPENLOOP
589
+
590
+    #ifdef PID_DEBUG
591
+      SERIAL_ECHO_START;
592
+      SERIAL_ECHO(MSG_PID_DEBUG);
593
+      SERIAL_ECHO(e);
594
+      SERIAL_ECHO(MSG_PID_DEBUG_INPUT);
595
+      SERIAL_ECHO(current_temperature[e]);
596
+      SERIAL_ECHO(MSG_PID_DEBUG_OUTPUT);
597
+      SERIAL_ECHO(pid_output);
598
+      SERIAL_ECHO(MSG_PID_DEBUG_PTERM);
599
+      SERIAL_ECHO(pTerm[e]);
600
+      SERIAL_ECHO(MSG_PID_DEBUG_ITERM);
601
+      SERIAL_ECHO(iTerm[e]);
602
+      SERIAL_ECHO(MSG_PID_DEBUG_DTERM);
603
+      SERIAL_ECHOLN(dTerm[e]);
604
+    #endif //PID_DEBUG
605
+
606
+  #else /* PID off */
607
+    pid_output = (current_temperature[e] < target_temperature[e]) ? PID_MAX : 0;
608
+  #endif
609
+
610
+  return pid_output;
611
+}
612
+
613
+#ifdef PIDTEMPBED
614
+  float get_pid_output_bed() {
615
+    float pid_output;
616
+    #ifndef PID_OPENLOOP
617
+      pid_error_bed = target_temperature_bed - current_temperature_bed;
618
+      pTerm_bed = bedKp * pid_error_bed;
619
+      temp_iState_bed += pid_error_bed;
620
+      temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
621
+      iTerm_bed = bedKi * temp_iState_bed;
622
+
623
+      dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
624
+      temp_dState_bed = current_temperature_bed;
625
+
626
+      pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
627
+      if (pid_output > MAX_BED_POWER) {
628
+        if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
629
+        pid_output = MAX_BED_POWER;
630
+      }
631
+      else if (pid_output < 0) {
632
+        if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
633
+        pid_output = 0;
634
+      }
635
+    #else
636
+      pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
637
+    #endif // PID_OPENLOOP
638
+
639
+    return pid_output;
640
+  }
641
+#endif
642
+
549 643
 void manage_heater() {
550 644
 
551 645
   if (!temp_meas_ready) return;
552 646
 
553
-  float pid_input, pid_output;
554
-
555 647
   updateTemperaturesFromRawValues();
556 648
 
557 649
   #ifdef HEATER_0_USES_MAX6675
@@ -569,69 +661,7 @@ void manage_heater() {
569 661
       thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_RUNAWAY_PROTECTION_PERIOD, THERMAL_RUNAWAY_PROTECTION_HYSTERESIS);
570 662
     #endif
571 663
 
572
-    #ifdef PIDTEMP
573
-      pid_input = current_temperature[e];
574
-
575
-      #ifndef PID_OPENLOOP
576
-        pid_error[e] = target_temperature[e] - pid_input;
577
-        if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
578
-          pid_output = BANG_MAX;
579
-          pid_reset[e] = true;
580
-        }
581
-        else if (pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
582
-          pid_output = 0;
583
-          pid_reset[e] = true;
584
-        }
585
-        else {
586
-          if (pid_reset[e] == true) {
587
-            temp_iState[e] = 0.0;
588
-            pid_reset[e] = false;
589
-          }
590
-          pTerm[e] = PID_PARAM(Kp,e) * pid_error[e];
591
-          temp_iState[e] += pid_error[e];
592
-          temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
593
-          iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e];
594
-
595
-          //K1 defined in Configuration.h in the PID settings
596
-          #define K2 (1.0-K1)
597
-          dTerm[e] = (PID_PARAM(Kd,e) * (pid_input - temp_dState[e])) * K2 + (K1 * dTerm[e]);
598
-          pid_output = pTerm[e] + iTerm[e] - dTerm[e];
599
-          if (pid_output > PID_MAX) {
600
-            if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
601
-            pid_output = PID_MAX;
602
-          }
603
-          else if (pid_output < 0) {
604
-            if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
605
-            pid_output = 0;
606
-          }
607
-        }
608
-        temp_dState[e] = pid_input;
609
-      #else
610
-        pid_output = constrain(target_temperature[e], 0, PID_MAX);
611
-      #endif //PID_OPENLOOP
612
-
613
-      #ifdef PID_DEBUG
614
-        SERIAL_ECHO_START;
615
-        SERIAL_ECHO(MSG_PID_DEBUG);
616
-        SERIAL_ECHO(e);
617
-        SERIAL_ECHO(MSG_PID_DEBUG_INPUT);
618
-        SERIAL_ECHO(pid_input);
619
-        SERIAL_ECHO(MSG_PID_DEBUG_OUTPUT);
620
-        SERIAL_ECHO(pid_output);
621
-        SERIAL_ECHO(MSG_PID_DEBUG_PTERM);
622
-        SERIAL_ECHO(pTerm[e]);
623
-        SERIAL_ECHO(MSG_PID_DEBUG_ITERM);
624
-        SERIAL_ECHO(iTerm[e]);
625
-        SERIAL_ECHO(MSG_PID_DEBUG_DTERM);
626
-        SERIAL_ECHOLN(dTerm[e]);
627
-      #endif //PID_DEBUG
628
-
629
-    #else /* PID off */
630
-
631
-      pid_output = 0;
632
-      if (current_temperature[e] < target_temperature[e]) pid_output = PID_MAX;
633
-
634
-    #endif
664
+    float pid_output = get_pid_output(e);
635 665
 
636 666
     // Check if temperature is within the correct range
637 667
     soft_pwm[e] = current_temperature[e] > minttemp[e] && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
@@ -678,33 +708,7 @@ void manage_heater() {
678 708
     #endif
679 709
 
680 710
     #ifdef PIDTEMPBED
681
-      pid_input = current_temperature_bed;
682
-
683
-      #ifndef PID_OPENLOOP
684
-        pid_error_bed = target_temperature_bed - pid_input;
685
-        pTerm_bed = bedKp * pid_error_bed;
686
-        temp_iState_bed += pid_error_bed;
687
-        temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
688
-        iTerm_bed = bedKi * temp_iState_bed;
689
-
690
-        //K1 defined in Configuration.h in the PID settings
691
-  		  #define K2 (1.0-K1)
692
-  		  dTerm_bed = (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
693
-        temp_dState_bed = pid_input;
694
-
695
-        pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
696
-        if (pid_output > MAX_BED_POWER) {
697
-          if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
698
-          pid_output = MAX_BED_POWER;
699
-        }
700
-        else if (pid_output < 0) {
701
-          if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
702
-          pid_output = 0;
703
-        }
704
-
705
-      #else
706
-        pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
707
-      #endif //PID_OPENLOOP
711
+      float pid_output = get_pid_output_bed();
708 712
 
709 713
       soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
710 714
 

Loading…
Cancel
Save