Browse Source

Invariant get_pid_output with HOTENDS < 2

Scott Lahteine 8 years ago
parent
commit
b72238f406
2 changed files with 59 additions and 52 deletions
  1. 41
    43
      Marlin/temperature.cpp
  2. 18
    9
      Marlin/temperature.h

+ 41
- 43
Marlin/temperature.cpp View File

@@ -514,88 +514,86 @@ void Temperature::min_temp_error(uint8_t e) {
514 514
 }
515 515
 
516 516
 float Temperature::get_pid_output(int e) {
517
+  #if HOTENDS == 1
518
+    UNUSED(e);
519
+    #define _HOTEND_TEST     true
520
+    #define _HOTEND_EXTRUDER active_extruder
521
+  #else
522
+    #define _HOTEND_TEST     e == active_extruder
523
+    #define _HOTEND_EXTRUDER e
524
+  #endif
517 525
   float pid_output;
518 526
   #if ENABLED(PIDTEMP)
519 527
     #if DISABLED(PID_OPENLOOP)
520
-      pid_error[e] = target_temperature[e] - current_temperature[e];
521
-      dTerm[e] = K2 * PID_PARAM(Kd, e) * (current_temperature[e] - temp_dState[e]) + K1 * dTerm[e];
522
-      temp_dState[e] = current_temperature[e];
523
-      if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
528
+      pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
529
+      dTerm[HOTEND_INDEX] = K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + K1 * dTerm[HOTEND_INDEX];
530
+      temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
531
+      if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
524 532
         pid_output = BANG_MAX;
525
-        pid_reset[e] = true;
533
+        pid_reset[HOTEND_INDEX] = true;
526 534
       }
527
-      else if (pid_error[e] < -(PID_FUNCTIONAL_RANGE) || target_temperature[e] == 0) {
535
+      else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0) {
528 536
         pid_output = 0;
529
-        pid_reset[e] = true;
537
+        pid_reset[HOTEND_INDEX] = true;
530 538
       }
531 539
       else {
532
-        if (pid_reset[e]) {
533
-          temp_iState[e] = 0.0;
534
-          pid_reset[e] = false;
540
+        if (pid_reset[HOTEND_INDEX]) {
541
+          temp_iState[HOTEND_INDEX] = 0.0;
542
+          pid_reset[HOTEND_INDEX] = false;
535 543
         }
536
-        pTerm[e] = PID_PARAM(Kp, e) * pid_error[e];
537
-        temp_iState[e] += pid_error[e];
538
-        temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
539
-        iTerm[e] = PID_PARAM(Ki, e) * temp_iState[e];
544
+        pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
545
+        temp_iState[HOTEND_INDEX] += pid_error[HOTEND_INDEX];
546
+        temp_iState[HOTEND_INDEX] = constrain(temp_iState[HOTEND_INDEX], temp_iState_min[HOTEND_INDEX], temp_iState_max[HOTEND_INDEX]);
547
+        iTerm[HOTEND_INDEX] = PID_PARAM(Ki, HOTEND_INDEX) * temp_iState[HOTEND_INDEX];
540 548
 
541
-        pid_output = pTerm[e] + iTerm[e] - dTerm[e];
542
-
543
-        #if ENABLED(SINGLENOZZLE)
544
-          #define _NOZZLE_TEST     true
545
-          #define _NOZZLE_EXTRUDER active_extruder
546
-          #define _CTERM_INDEX     0
547
-        #else
548
-          #define _NOZZLE_TEST     e == active_extruder
549
-          #define _NOZZLE_EXTRUDER e
550
-          #define _CTERM_INDEX     e
551
-        #endif
549
+        pid_output = pTerm[HOTEND_INDEX] + iTerm[HOTEND_INDEX] - dTerm[HOTEND_INDEX];
552 550
 
553 551
         #if ENABLED(PID_ADD_EXTRUSION_RATE)
554
-          cTerm[_CTERM_INDEX] = 0;
555
-          if (_NOZZLE_TEST) {
552
+          cTerm[HOTEND_INDEX] = 0;
553
+          if (_HOTEND_TEST) {
556 554
             long e_position = stepper.position(E_AXIS);
557
-            if (e_position > last_position[_NOZZLE_EXTRUDER]) {
558
-              lpq[lpq_ptr++] = e_position - last_position[_NOZZLE_EXTRUDER];
559
-              last_position[_NOZZLE_EXTRUDER] = e_position;
555
+            if (e_position > last_position[_HOTEND_EXTRUDER]) {
556
+              lpq[lpq_ptr++] = e_position - last_position[_HOTEND_EXTRUDER];
557
+              last_position[_HOTEND_EXTRUDER] = e_position;
560 558
             }
561 559
             else {
562 560
               lpq[lpq_ptr++] = 0;
563 561
             }
564 562
             if (lpq_ptr >= lpq_len) lpq_ptr = 0;
565
-            cTerm[_CTERM_INDEX] = (lpq[lpq_ptr] / planner.axis_steps_per_mm[E_AXIS]) * PID_PARAM(Kc, e);
566
-            pid_output += cTerm[e];
563
+            cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] / planner.axis_steps_per_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
564
+            pid_output += cTerm[HOTEND_INDEX];
567 565
           }
568 566
         #endif //PID_ADD_EXTRUSION_RATE
569 567
 
570 568
         if (pid_output > PID_MAX) {
571
-          if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
569
+          if (pid_error[HOTEND_INDEX] > 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
572 570
           pid_output = PID_MAX;
573 571
         }
574 572
         else if (pid_output < 0) {
575
-          if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
573
+          if (pid_error[HOTEND_INDEX] < 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
576 574
           pid_output = 0;
577 575
         }
578 576
       }
579 577
     #else
580
-      pid_output = constrain(target_temperature[e], 0, PID_MAX);
578
+      pid_output = constrain(target_temperature[HOTEND_INDEX], 0, PID_MAX);
581 579
     #endif //PID_OPENLOOP
582 580
 
583 581
     #if ENABLED(PID_DEBUG)
584 582
       SERIAL_ECHO_START;
585
-      SERIAL_ECHOPAIR(MSG_PID_DEBUG, e);
586
-      SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[e]);
583
+      SERIAL_ECHOPAIR(MSG_PID_DEBUG, HOTEND_INDEX);
584
+      SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[HOTEND_INDEX]);
587 585
       SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
588
-      SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[e]);
589
-      SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[e]);
590
-      SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[e]);
586
+      SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[HOTEND_INDEX]);
587
+      SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[HOTEND_INDEX]);
588
+      SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[HOTEND_INDEX]);
591 589
       #if ENABLED(PID_ADD_EXTRUSION_RATE)
592
-        SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[e]);
590
+        SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[HOTEND_INDEX]);
593 591
       #endif
594 592
       SERIAL_EOL;
595 593
     #endif //PID_DEBUG
596 594
 
597 595
   #else /* PID off */
598
-    pid_output = (current_temperature[e] < target_temperature[e]) ? PID_MAX : 0;
596
+    pid_output = (current_temperature[HOTEND_INDEX] < target_temperature[HOTEND_INDEX]) ? PID_MAX : 0;
599 597
   #endif
600 598
 
601 599
   return pid_output;
@@ -672,7 +670,7 @@ void Temperature::manage_heater() {
672 670
   #endif
673 671
 
674 672
   // Loop through all hotends
675
-  for (int e = 0; e < HOTENDS; e++) {
673
+  for (uint8_t e = 0; e < HOTENDS; e++) {
676 674
 
677 675
     #if ENABLED(THERMAL_PROTECTION_HOTENDS)
678 676
       thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);

+ 18
- 9
Marlin/temperature.h View File

@@ -38,6 +38,16 @@
38 38
   #define SOFT_PWM_SCALE 0
39 39
 #endif
40 40
 
41
+#if HOTENDS == 1
42
+  #define HOTEND_ARG 0
43
+  #define HOTEND_INDEX 0
44
+  #define EXTRUDER_ARG 0
45
+#else
46
+  #define HOTEND_ARG hotend
47
+  #define HOTEND_INDEX e
48
+  #define EXTRUDER_ARG active_extruder
49
+#endif
50
+
41 51
 class Temperature {
42 52
 
43 53
   public:
@@ -112,7 +122,12 @@ class Temperature {
112 122
 
113 123
     #if ENABLED(PREVENT_DANGEROUS_EXTRUDE)
114 124
       static float extrude_min_temp;
115
-      static bool tooColdToExtrude(uint8_t e) { return degHotend(e) < extrude_min_temp; }
125
+      static bool tooColdToExtrude(uint8_t e) {
126
+        #if HOTENDS == 1
127
+          UNUSED(e);
128
+        #endif
129
+        return degHotend(HOTEND_INDEX) < extrude_min_temp;
130
+      }
116 131
     #else
117 132
       static bool tooColdToExtrude(uint8_t e) { UNUSED(e); return false; }
118 133
     #endif
@@ -230,12 +245,6 @@ class Temperature {
230 245
     //inline so that there is no performance decrease.
231 246
     //deg=degreeCelsius
232 247
 
233
-    #if HOTENDS == 1
234
-      #define HOTEND_ARG 0
235
-    #else
236
-      #define HOTEND_ARG hotend
237
-    #endif
238
-
239 248
     static float degHotend(uint8_t hotend) {
240 249
       #if HOTENDS == 1
241 250
         UNUSED(hotend);
@@ -329,8 +338,8 @@ class Temperature {
329 338
       #if ENABLED(AUTOTEMP)
330 339
         if (planner.autotemp_enabled) {
331 340
           planner.autotemp_enabled = false;
332
-          if (degTargetHotend(active_extruder) > planner.autotemp_min)
333
-            setTargetHotend(0, active_extruder);
341
+          if (degTargetHotend(EXTRUDER_ARG) > planner.autotemp_min)
342
+            setTargetHotend(0, EXTRUDER_ARG);
334 343
         }
335 344
       #endif
336 345
     }

Loading…
Cancel
Save