Переглянути джерело

bed pid

Conflicts:

	Marlin/Configuration.h
Mark Finn 11 роки тому
джерело
коміт
9698f4ea64
4 змінених файлів з 175 додано та 52 видалено
  1. 12
    1
      Marlin/Configuration.h
  2. 30
    1
      Marlin/Marlin.pde
  3. 123
    46
      Marlin/temperature.cpp
  4. 10
    4
      Marlin/temperature.h

+ 12
- 1
Marlin/Configuration.h Переглянути файл

@@ -95,10 +95,11 @@
95 95
 // PID settings:
96 96
 // Comment the following line to disable PID and enable bang-bang.
97 97
 #define PIDTEMP
98
+#define PIDTEMPBED
98 99
 #define PID_MAX 255 // limits current to nozzle; 255=full current
99 100
 #ifdef PIDTEMP
100 101
   //#define PID_DEBUG // Sends debug data to the serial port. 
101
-  //#define PID_OPENLOOP 1 // Puts PID in open loop. M104 sets the output power in %
102
+  //#define PID_OPENLOOP 1 // Puts PID in open loop. M104/M140 sets the output power from 0 to PID_MAX
102 103
   #define PID_INTEGRAL_DRIVE_MAX 255  //limit for the integral term
103 104
   #define K1 0.95 //smoothing factor withing the PID
104 105
   #define PID_dT ((16.0 * 8.0)/(F_CPU / 64.0 / 256.0)) //sampling period of the
@@ -114,6 +115,16 @@
114 115
 //    #define  DEFAULT_Ki 0.1  
115 116
 //    #define  DEFAULT_Kd 12  
116 117
 
118
+//from FOPDT model - kp=.39 Tp=405 Tdead=66, Tc set to 79.2, argressive factor of .15 (vs .1, 1, 10)
119
+    #define  DEFAULT_bedKp 10.00
120
+    #define  DEFAULT_bedKi .023
121
+    #define  DEFAULT_bedKd 305.4
122
+
123
+//mark from pidautotune
124
+//    #define  DEFAULT_bedKp 97.1
125
+//    #define  DEFAULT_bedKi 1.41
126
+//    #define  DEFAULT_bedKd 1675.16
127
+
117 128
 // Mendel Parts V9 on 12V    
118 129
 //    #define  DEFAULT_Kp 63.0
119 130
 //    #define  DEFAULT_Ki 2.25

+ 30
- 1
Marlin/Marlin.pde Переглянути файл

@@ -113,6 +113,7 @@
113 113
 // M221 S<factor in percent>- set extrude factor override percentage
114 114
 // M240 - Trigger a camera to take a photograph
115 115
 // M301 - Set PID parameters P I and D
116
+// M304 - Set bed PID parameters P I and D
116 117
 // M302 - Allow cold extrudes
117 118
 // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
118 119
 // M400 - Finish all moves
@@ -1003,6 +1004,10 @@ void process_commands()
1003 1004
         SERIAL_PROTOCOLPGM(" @:");
1004 1005
         SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));  
1005 1006
       #endif
1007
+      #ifdef PIDTEMPBED
1008
+        SERIAL_PROTOCOLPGM(" B@:");
1009
+        SERIAL_PROTOCOL(getHeaterPower(-1));  
1010
+      #endif
1006 1011
         SERIAL_PROTOCOLLN("");
1007 1012
       return;
1008 1013
       break;
@@ -1405,6 +1410,24 @@ void process_commands()
1405 1410
       }
1406 1411
       break;
1407 1412
     #endif //PIDTEMP
1413
+    #ifdef PIDTEMPBED
1414
+    case 304: // M304
1415
+      {
1416
+        if(code_seen('P')) bedKp = code_value();
1417
+        if(code_seen('I')) bedKi = code_value()*PID_dT;
1418
+        if(code_seen('D')) bedKd = code_value()/PID_dT;
1419
+        updatePID();
1420
+        SERIAL_PROTOCOL(MSG_OK);
1421
+		SERIAL_PROTOCOL(" p:");
1422
+        SERIAL_PROTOCOL(Kp);
1423
+        SERIAL_PROTOCOL(" i:");
1424
+        SERIAL_PROTOCOL(Ki/PID_dT);
1425
+        SERIAL_PROTOCOL(" d:");
1426
+        SERIAL_PROTOCOL(Kd*PID_dT);
1427
+        SERIAL_PROTOCOLLN("");
1428
+      }
1429
+      break;
1430
+    #endif //PIDTEMP
1408 1431
     case 240: // M240  Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
1409 1432
      {
1410 1433
       #ifdef PHOTOGRAPH_PIN
@@ -1437,8 +1460,14 @@ void process_commands()
1437 1460
     case 303: // M303 PID autotune
1438 1461
     {
1439 1462
       float temp = 150.0;
1463
+      int e=0;
1464
+      int c=5;
1465
+      if (code_seen('E')) e=code_value();
1466
+			if (e<0)
1467
+				temp=70;
1440 1468
       if (code_seen('S')) temp=code_value();
1441
-      PID_autotune(temp);
1469
+      if (code_seen('C')) c=code_value();
1470
+      PID_autotune(temp, e, c);
1442 1471
     }
1443 1472
     break;
1444 1473
     case 400: // M400 finish all moves

+ 123
- 46
Marlin/temperature.cpp Переглянути файл

@@ -57,6 +57,15 @@ int current_raw_bed = 0;
57 57
     float Kc=DEFAULT_Kc;
58 58
   #endif
59 59
 #endif //PIDTEMP
60
+
61
+#ifdef PIDTEMPBED
62
+  // used external
63
+  float pid_setpoint_bed = { 0.0 };
64
+  
65
+  float bedKp=DEFAULT_bedKp;
66
+  float bedKi=(DEFAULT_bedKi*PID_dT);
67
+  float bedKd=(DEFAULT_bedKd/PID_dT);
68
+#endif //PIDTEMPBED
60 69
   
61 70
   
62 71
 //===========================================================================
@@ -64,9 +73,6 @@ int current_raw_bed = 0;
64 73
 //===========================================================================
65 74
 static volatile bool temp_meas_ready = false;
66 75
 
67
-static unsigned long  previous_millis_bed_heater;
68
-//static unsigned long previous_millis_heater;
69
-
70 76
 #ifdef PIDTEMP
71 77
   //static cannot be external:
72 78
   static float temp_iState[EXTRUDERS] = { 0 };
@@ -82,7 +88,20 @@ static unsigned long  previous_millis_bed_heater;
82 88
   // static float pid_output[EXTRUDERS];
83 89
   static bool pid_reset[EXTRUDERS];
84 90
 #endif //PIDTEMP
91
+#ifdef PIDTEMPBED
92
+  //static cannot be external:
93
+  static float temp_iState_bed = { 0 };
94
+  static float temp_dState_bed = { 0 };
95
+  static float pTerm_bed;
96
+  static float iTerm_bed;
97
+  static float dTerm_bed;
98
+  //int output;
99
+  static float pid_error_bed;
100
+  static float temp_iState_min_bed;
101
+  static float temp_iState_max_bed;
102
+#endif //PIDTEMPBED
85 103
   static unsigned char soft_pwm[EXTRUDERS];
104
+  static unsigned char soft_pwm_bed;
86 105
   
87 106
 #ifdef WATCHPERIOD
88 107
   int watch_raw[EXTRUDERS] = { -1000 }; // the first value used for all
@@ -122,7 +141,7 @@ static unsigned long  previous_millis_bed_heater;
122 141
 //=============================   functions      ============================
123 142
 //===========================================================================
124 143
 
125
-void PID_autotune(float temp)
144
+void PID_autotune(float temp, int extruder, int ncycles)
126 145
 {
127 146
   float input;
128 147
   int cycles=0;
@@ -139,27 +158,45 @@ void PID_autotune(float temp)
139 158
   float Ku, Tu;
140 159
   float Kp, Ki, Kd;
141 160
   float max, min;
142
-  
161
+
162
+	if ((extruder > EXTRUDERS)
163
+  #if (TEMP_BED_PIN <= -1)
164
+		||(extruder < 0)
165
+	#endif
166
+	){
167
+  	SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
168
+  	return;
169
+	}
170
+	
143 171
   SERIAL_ECHOLN("PID Autotune start");
144 172
   
145 173
   disable_heater(); // switch off all heaters.
146
-  
147
-  soft_pwm[0] = PID_MAX/2;
148
-    
149
-  for(;;) {
174
+
175
+	if (extruder<0)
176
+	  soft_pwm_bed = PID_MAX/2;
177
+	else
178
+	  soft_pwm[extruder] = PID_MAX/2;
179
+
180
+
181
+
182
+
183
+ for(;;) {
150 184
 
151 185
     if(temp_meas_ready == true) { // temp sample ready
152 186
       CRITICAL_SECTION_START;
153 187
       temp_meas_ready = false;
154 188
       CRITICAL_SECTION_END;
155
-      input = analog2temp(current_raw[0], 0);
156
-      
189
+      input = (extruder<0)?analog2tempBed(current_raw_bed):analog2temp(current_raw[extruder], extruder);
190
+
157 191
       max=max(max,input);
158 192
       min=min(min,input);
159 193
       if(heating == true && input > temp) {
160 194
         if(millis() - t2 > 5000) { 
161 195
           heating=false;
162
-          soft_pwm[0] = (bias - d) >> 1;
196
+					if (extruder<0)
197
+						soft_pwm_bed = (bias - d) >> 1;
198
+					else
199
+						soft_pwm[extruder] = (bias - d) >> 1;
163 200
           t1=millis();
164 201
           t_high=t1 - t2;
165 202
           max=temp;
@@ -210,7 +247,10 @@ void PID_autotune(float temp)
210 247
               */
211 248
             }
212 249
           }
213
-          soft_pwm[0] = (bias + d) >> 1;
250
+					if (extruder<0)
251
+						soft_pwm_bed = (bias + d) >> 1;
252
+					else
253
+						soft_pwm[extruder] = (bias + d) >> 1;
214 254
           cycles++;
215 255
           min=temp;
216 256
         }
@@ -221,17 +261,26 @@ void PID_autotune(float temp)
221 261
       return;
222 262
     }
223 263
     if(millis() - temp_millis > 2000) {
224
-      temp_millis = millis();
225
-      SERIAL_PROTOCOLPGM("ok T:");
226
-      SERIAL_PROTOCOL(degHotend(0));   
264
+			int p;
265
+			if (extruder<0){
266
+	      p=soft_pwm_bed;       
267
+	      SERIAL_PROTOCOLPGM("ok B:");
268
+			}else{
269
+	      p=soft_pwm[extruder];       
270
+	      SERIAL_PROTOCOLPGM("ok T:");
271
+			}
272
+			
273
+      SERIAL_PROTOCOL(input);   
227 274
       SERIAL_PROTOCOLPGM(" @:");
228
-      SERIAL_PROTOCOLLN(getHeaterPower(0));       
275
+      SERIAL_PROTOCOLLN(p);       
276
+
277
+      temp_millis = millis();
229 278
     }
230 279
     if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
231 280
       SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
232 281
       return;
233 282
     }
234
-    if(cycles > 5) {
283
+    if(cycles > ncycles) {
235 284
       SERIAL_PROTOCOLLNPGM("PID Autotune finished ! Place the Kp, Ki and Kd constants in the configuration.h");
236 285
       return;
237 286
     }
@@ -245,19 +294,18 @@ void updatePID()
245 294
   for(int e = 0; e < EXTRUDERS; e++) { 
246 295
      temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;  
247 296
   }
297
+  temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;  
248 298
 #endif
249 299
 }
250 300
   
251 301
 int getHeaterPower(int heater) {
302
+	if (heater<0)
303
+		return soft_pwm_bed;
252 304
   return soft_pwm[heater];
253 305
 }
254 306
 
255 307
 void manage_heater()
256 308
 {
257
-#ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
258
-  static int bed_needs_heating=0;
259
-  static int bed_is_on=0;
260
-#endif
261 309
 
262 310
   #ifdef USE_WATCHDOG
263 311
     wd_reset();
@@ -298,12 +346,16 @@ void manage_heater()
298 346
           temp_iState[e] += pid_error[e];
299 347
           temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
300 348
           iTerm[e] = Ki * temp_iState[e];
349
+
301 350
           //K1 defined in Configuration.h in the PID settings
302 351
           #define K2 (1.0-K1)
303 352
           dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
304 353
           temp_dState[e] = pid_input;
354
+
305 355
           pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
306 356
         }
357
+    #else 
358
+          pid_output = constrain(pid_setpoint[e], 0, PID_MAX);
307 359
     #endif //PID_OPENLOOP
308 360
     #ifdef PID_DEBUG
309 361
     SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]);  
@@ -338,42 +390,58 @@ void manage_heater()
338 390
     }
339 391
   #endif
340 392
   
341
-#ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
342
-  if (bed_needs_heating){
343
-    if (bed_is_on==0)
344
-        WRITE(HEATER_BED_PIN,HIGH);
345
-    if (bed_is_on==1)
346
-        WRITE(HEATER_BED_PIN,LOW);
347
-    bed_is_on=(bed_is_on+1) % HEATER_BED_DUTY_CYCLE_DIVIDER;
348
-  }
349
-#endif
350 393
 
394
+		#ifndef PIDTEMPBED
351 395
   if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
352 396
     return;
353 397
   previous_millis_bed_heater = millis();
354
-  
398
+    #endif
399
+
355 400
   #if TEMP_BED_PIN > -1
356 401
   
357
-    #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
358
-    bed_needs_heating=0;
359
-    #endif
402
+		#ifdef PIDTEMPBED
403
+    pid_input = analog2tempBed(current_raw_bed);
404
+
405
+    #ifndef PID_OPENLOOP
406
+		  pid_error_bed = pid_setpoint_bed - pid_input;
407
+		  pTerm_bed = bedKp * pid_error_bed;
408
+		  temp_iState_bed += pid_error_bed;
409
+		  temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
410
+		  iTerm_bed = bedKi * temp_iState_bed;
411
+
412
+		  //K1 defined in Configuration.h in the PID settings
413
+		  #define K2 (1.0-K1)
414
+		  dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
415
+		  temp_dState_bed = pid_input;
416
+
417
+		  pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, PID_MAX);
418
+
419
+    #else 
420
+      pid_output = constrain(pid_setpoint_bed, 0, PID_MAX);
421
+    #endif //PID_OPENLOOP
360 422
 
361
-    #ifndef BED_LIMIT_SWITCHING
423
+	  if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) 
424
+	  {
425
+	    soft_pwm_bed = (int)pid_output >> 1;
426
+	  }
427
+	  else {
428
+	    soft_pwm_bed = 0;
429
+	  }
430
+
431
+    #elif not defined BED_LIMIT_SWITCHING
362 432
       // Check if temperature is within the correct range
363 433
       if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
364 434
         if(current_raw_bed >= target_raw_bed)
365 435
         {
366
-          WRITE(HEATER_BED_PIN,LOW);
436
+					soft_pwm_bed = 0;
367 437
         }
368 438
         else 
369 439
         {
370
-          #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
371
-          bed_needs_heating=1;
372
-          #endif
373
-          WRITE(HEATER_BED_PIN,HIGH);
440
+					soft_pwm_bed = 100;
374 441
         }
375 442
       }
376 443
       else {
444
+					soft_pwm_bed = 0;
377 445
         WRITE(HEATER_BED_PIN,LOW);
378 446
       }
379 447
     #else //#ifdef BED_LIMIT_SWITCHING
@@ -381,18 +449,16 @@ void manage_heater()
381 449
       if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
382 450
         if(current_raw_bed > target_bed_high_temp)
383 451
         {
384
-          WRITE(HEATER_BED_PIN,LOW);
452
+					soft_pwm_bed = 0;
385 453
         }
386 454
         else 
387 455
           if(current_raw_bed <= target_bed_low_temp)
388 456
         {
389
-          #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
390
-          bed_needs_heating=1;
391
-          #endif
392
-          WRITE(HEATER_BED_PIN,HIGH);
457
+					soft_pwm_bed = 100;
393 458
         }
394 459
       }
395 460
       else {
461
+					soft_pwm_bed = 0;
396 462
         WRITE(HEATER_BED_PIN,LOW);
397 463
       }
398 464
     #endif
@@ -567,6 +633,8 @@ void tp_init()
567 633
 #ifdef PIDTEMP
568 634
     temp_iState_min[e] = 0.0;
569 635
     temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
636
+    temp_iState_min_bed = 0.0;
637
+    temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
570 638
 #endif //PIDTEMP
571 639
   }
572 640
 
@@ -728,6 +796,7 @@ void disable_heater()
728 796
 
729 797
   #if TEMP_BED_PIN > -1
730 798
     target_raw_bed=0;
799
+    soft_pwm_bed=0;
731 800
     #if HEATER_BED_PIN > -1  
732 801
       WRITE(HEATER_BED_PIN,LOW);
733 802
     #endif
@@ -832,6 +901,7 @@ ISR(TIMER0_COMPB_vect)
832 901
   static unsigned char soft_pwm_0;
833 902
   static unsigned char soft_pwm_1;
834 903
   static unsigned char soft_pwm_2;
904
+  static unsigned char soft_pwm_b;
835 905
   
836 906
   if(pwm_count == 0){
837 907
     soft_pwm_0 = soft_pwm[0];
@@ -844,6 +914,10 @@ ISR(TIMER0_COMPB_vect)
844 914
     soft_pwm_2 = soft_pwm[2];
845 915
     if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
846 916
     #endif
917
+    #if HEATER_BED_PIN > -1
918
+    soft_pwm_b = soft_pwm_bed;
919
+    if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1);
920
+    #endif
847 921
   }
848 922
   if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
849 923
   #if EXTRUDERS > 1
@@ -852,6 +926,9 @@ ISR(TIMER0_COMPB_vect)
852 926
   #if EXTRUDERS > 2
853 927
   if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
854 928
   #endif
929
+  #if HEATER_BED_PIN > -1
930
+  if(soft_pwm_b <= pwm_count) WRITE(HEATER_BED_PIN,0);
931
+  #endif
855 932
   
856 933
   pwm_count++;
857 934
   pwm_count &= 0x7f;

+ 10
- 4
Marlin/temperature.h Переглянути файл

@@ -46,11 +46,15 @@ extern int current_raw_bed;
46 46
   extern int target_bed_low_temp ;  
47 47
   extern int target_bed_high_temp ;
48 48
 #endif
49
-extern float Kp,Ki,Kd,Kc;
50 49
 
51 50
 #ifdef PIDTEMP
51
+  extern float Kp,Ki,Kd,Kc;
52 52
   extern float pid_setpoint[EXTRUDERS];
53 53
 #endif
54
+#ifdef PIDTEMPBED
55
+  extern float bedKp,bedKi,bedKd;
56
+  extern float pid_setpoint_bed;
57
+#endif
54 58
   
55 59
 // #ifdef WATCHPERIOD
56 60
   extern int watch_raw[EXTRUDERS] ;
@@ -88,7 +92,9 @@ FORCE_INLINE void setTargetHotend(const float &celsius, uint8_t extruder) {
88 92
 FORCE_INLINE void setTargetBed(const float &celsius) {  
89 93
   
90 94
   target_raw_bed = temp2analogBed(celsius);
91
-  #ifdef BED_LIMIT_SWITCHING
95
+	#ifdef PIDTEMPBED
96
+  pid_setpoint_bed = celsius;
97
+  #elif defined BED_LIMIT_SWITCHING
92 98
     if(celsius>BED_HYSTERESIS)
93 99
     {
94 100
     target_bed_low_temp= temp2analogBed(celsius-BED_HYSTERESIS);
@@ -163,7 +169,7 @@ FORCE_INLINE void autotempShutdown(){
163 169
  #endif
164 170
 }
165 171
 
166
-void PID_autotune(float temp);
172
+void PID_autotune(float temp, int extruder, int ncycles);
167 173
 
168 174
 #endif
169
-
175
+

Завантаження…
Відмінити
Зберегти