From c57906b627bf7245578522ab50e9d35e9c6bc96b Mon Sep 17 00:00:00 2001
From: Bernhard Kubicek <kubicek@gmx.at>
Date: Sun, 13 Nov 2011 21:43:26 +0100
Subject: [PATCH] fixed bug with adjusting ki in realtime.

---
 Marlin/Configuration.h |  8 ++++----
 Marlin/Marlin.pde      | 40 ++++++++++++++++++++++------------------
 Marlin/temperature.cpp |  7 +++++++
 Marlin/temperature.h   |  1 +
 4 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h
index 8661ae438e..5b15dd0a47 100644
--- a/Marlin/Configuration.h
+++ b/Marlin/Configuration.h
@@ -95,11 +95,11 @@
   // PID_SWING_AT_CRITIAL is the time for a full period of the oscillations at the critical Gain
   // usually further manual tunine is necessary.
 
-  #define PID_CRITIAL_GAIN 3000
-  #define PID_SWING_AT_CRITIAL 45 //seconds
+  #define PID_CRITIAL_GAIN 50
+  #define PID_SWING_AT_CRITIAL 47 //seconds
   
-  #define PID_PI    //no differentail term
-  //#define PID_PID //normal PID
+  //#define PID_PI    //no differentail term
+  #define PID_PID //normal PID
 
   #ifdef PID_PID
     //PID according to Ziegler-Nichols method
diff --git a/Marlin/Marlin.pde b/Marlin/Marlin.pde
index e15f025514..84f07b3e68 100644
--- a/Marlin/Marlin.pde
+++ b/Marlin/Marlin.pde
@@ -898,24 +898,28 @@ inline void process_commands()
 
     #ifdef PIDTEMP
     case 301: // M301
-      if(code_seen('P')) Kp = code_value();
-      if(code_seen('I')) Ki = code_value()*PID_dT;
-      if(code_seen('D')) Kd = code_value()/PID_dT;
-      #ifdef PID_ADD_EXTRUSION_RATE
-      if(code_seen('C')) Kc = code_value();
-      #endif
-      SERIAL_PROTOCOL("ok p:");
-      SERIAL_PROTOCOL(Kp);
-      SERIAL_PROTOCOL(" i:");
-      SERIAL_PROTOCOL(Ki/PID_dT);
-      SERIAL_PROTOCOL(" d:");
-      SERIAL_PROTOCOL(Kd*PID_dT);
-      #ifdef PID_ADD_EXTRUSION_RATE
-      SERIAL_PROTOCOL(" c:");
-      SERIAL_PROTOCOL(Kc*PID_dT);
-      #endif
-      SERIAL_PROTOCOLLN("");
-      
+      {
+        
+       
+        if(code_seen('P')) Kp = code_value();
+        if(code_seen('I')) Ki = code_value()*PID_dT;
+        if(code_seen('D')) Kd = code_value()/PID_dT;
+        #ifdef PID_ADD_EXTRUSION_RATE
+        if(code_seen('C')) Kc = code_value();
+        #endif
+        updatePID();
+        SERIAL_PROTOCOL("ok p:");
+        SERIAL_PROTOCOL(Kp);
+        SERIAL_PROTOCOL(" i:");
+        SERIAL_PROTOCOL(Ki/PID_dT);
+        SERIAL_PROTOCOL(" d:");
+        SERIAL_PROTOCOL(Kd*PID_dT);
+        #ifdef PID_ADD_EXTRUSION_RATE
+        SERIAL_PROTOCOL(" c:");
+        SERIAL_PROTOCOL(Kc*PID_dT);
+        #endif
+        SERIAL_PROTOCOLLN("");
+      }
       break;
     #endif //PIDTEMP
     case 400: // finish all moves
diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 8ea8ee008f..ac299399dc 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -111,6 +111,13 @@ static unsigned long previous_millis_heater, previous_millis_bed_heater;
 //=============================functions         ============================
 //===========================================================================
   
+void updatePID()
+{
+#ifdef PIDTEMP
+  temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki;
+#endif
+}
+  
 void manage_heater()
 {
   #ifdef USE_WATCHDOG
diff --git a/Marlin/temperature.h b/Marlin/temperature.h
index 9c4b59a241..0f0de985f4 100644
--- a/Marlin/temperature.h
+++ b/Marlin/temperature.h
@@ -87,6 +87,7 @@ inline bool isCoolingBed() {return target_raw[TEMPSENSOR_BED] < current_raw[TEMP
 
 void disable_heater();
 void setWatch();
+void updatePID();
 
 #endif
 
-- 
GitLab