Browse Source

Merge pull request #1249 from drf5n/PID_CI_v2

 temperature.cpp: Add Conditional Integration to prevent excessive integral windup
Bo Herrmannsen 10 years ago
parent
commit
9c07d28bd6
2 changed files with 18 additions and 4 deletions
  1. 2
    2
      Marlin/Configuration.h
  2. 16
    2
      Marlin/temperature.cpp

+ 2
- 2
Marlin/Configuration.h View File

@@ -144,13 +144,13 @@
144 144
 // Comment the following line to disable PID and enable bang-bang.
145 145
 #define PIDTEMP
146 146
 #define BANG_MAX 255 // limits current to nozzle while in bang-bang mode; 255=full current
147
-#define PID_MAX 255 // limits current to nozzle while PID is active (see PID_FUNCTIONAL_RANGE below); 255=full current
147
+#define PID_MAX BANG_MAX // limits current to nozzle while PID is active (see PID_FUNCTIONAL_RANGE below); 255=full current
148 148
 #ifdef PIDTEMP
149 149
   //#define PID_DEBUG // Sends debug data to the serial port.
150 150
   //#define PID_OPENLOOP 1 // Puts PID in open loop. M104/M140 sets the output power from 0 to PID_MAX
151 151
   #define PID_FUNCTIONAL_RANGE 10 // If the temperature difference between the target temperature and the actual temperature
152 152
                                   // is more then PID_FUNCTIONAL_RANGE then the PID will be shut off and the heater will be set to min/max.
153
-  #define PID_INTEGRAL_DRIVE_MAX 255  //limit for the integral term
153
+  #define PID_INTEGRAL_DRIVE_MAX PID_MAX  //limit for the integral term
154 154
   #define K1 0.95 //smoothing factor within the PID
155 155
   #define PID_dT ((OVERSAMPLENR * 10.0)/(F_CPU / 64.0 / 256.0)) //sampling period of the temperature routine
156 156
 

+ 16
- 2
Marlin/temperature.cpp View File

@@ -455,7 +455,14 @@ void manage_heater()
455 455
           //K1 defined in Configuration.h in the PID settings
456 456
           #define K2 (1.0-K1)
457 457
           dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
458
-          pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
458
+          pid_output = pTerm[e] + iTerm[e] - dTerm[e];
459
+          if (pid_output > PID_MAX) {
460
+            if (pid_error[e] > 0 )  temp_iState[e] -= pid_error[e]; // conditional un-integration
461
+            pid_output=PID_MAX;
462
+          } else if (pid_output < 0){
463
+            if (pid_error[e] < 0 )  temp_iState[e] -= pid_error[e]; // conditional un-integration
464
+            pid_output=0;
465
+          }
459 466
         }
460 467
         temp_dState[e] = pid_input;
461 468
     #else 
@@ -558,7 +565,14 @@ void manage_heater()
558 565
 		  dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
559 566
 		  temp_dState_bed = pid_input;
560 567
 
561
-		  pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER);
568
+		  pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
569
+          	  if (pid_output > MAX_BED_PID) {
570
+            	    if (pid_error_bed > 0 )  temp_iState_bed -= pid_error_bed; // conditional un-integration
571
+                    pid_output=PID_MAX;
572
+          	  } else if (pid_output < 0){
573
+            	    if (pid_error_bed < 0 )  temp_iState_bed -= pid_error_bed; // conditional un-integration
574
+                    pid_output=0;
575
+                  }
562 576
 
563 577
     #else 
564 578
       pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);

Loading…
Cancel
Save