forked from WLANThermo-nano/WLANThermo_ESP82XX_Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_pitmaster.h
700 lines (551 loc) · 21.7 KB
/
c_pitmaster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/***************************************************
Copyright (C) 2016 Steffen Ochs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The AutotunePID elements of this program based on the work of
AUTHOR: Repetier
PURPOSE: Repetier-Firmware/extruder.cpp
HISTORY: Please refer Github History
****************************************************/
int pidMax = 100; // Maximum (PWM) value, the heater should be set
//#define PM_DEBUG // ENABLE SERIAL AUTOTUNE DEBUG MESSAGES
#ifdef PM_DEBUG
#define PMPRINT(...) Serial.print(__VA_ARGS__)
#define PMPRINTLN(...) Serial.println(__VA_ARGS__)
#define PMPRINTP(...) Serial.print(F(__VA_ARGS__))
#define PMPRINTPLN(...) Serial.println(F(__VA_ARGS__))
#define PMPRINTF(...) Serial.printf(__VA_ARGS__)
#else
#define PMPRINT(...) //blank line
#define PMPRINTLN(...) //blank line
#define PMPRINTP(...) //blank line
#define PMPRINTPLN(...) //blank line
#define PMPRINTF(...) //blank line
#endif
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Clear Pitmaster Settings
void init_pitmaster(bool init, byte id) {
if (init) {
pitMaster[id].pid = 0;
pitMaster[id].channel = 0;
pitMaster[id].set = PITMASTERSETMIN;
pitMaster[id].active = PITOFF;
//pitMaster[id].resume = 0;
}
pitMaster[id].resume = 1; // später wieder raus
if (!pitMaster[id].resume) pitMaster[id].active = PITOFF;
if (pitMaster[id].active != MANUAL) pitMaster[id].value = 0;
pitMaster[id].event = false;
pitMaster[id].msec = 0;
pitMaster[id].pause = 1000;
pitMaster[id].esum = 0;
pitMaster[id].elast = 0;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set Pitmaster Pin
void set_pitmaster(bool init) {
pitMaster[0].io = PITMASTER1;
pinMode(PITMASTER1, OUTPUT);
digitalWrite(PITMASTER1, LOW);
init_pitmaster(init, 0);
pitMaster[1].io = PITMASTER2;
pinMode(PITMASTER2, OUTPUT);
digitalWrite(PITMASTER2, LOW);
init_pitmaster(init, 1);
if (sys.hwversion > 1) {
pinMode(PITSUPPLY, OUTPUT);
digitalWrite(PITSUPPLY, LOW);
}
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set Default PID-Settings
void set_pid(byte index) {
// Name, Nr, Aktor, Kp, Ki, Kd, Kp_a, Ki_a, Kp_a, Ki_min, Ki_max, Switch, DCmin, DCmax, ...
pidsize = 3; //3;
pid[0] = {"SSR SousVide", 0, 0, 104, 0.2, 0, 20, 0, 0, 0, 95, 0.9, 0, 100};
pid[1] = {"TITAN 50x50", 1, 1, 3.8, 0.01, 128, 6.2, 0.001, 5, 0, 95, 0.9, 25, 100};
pid[2] = {"Kamado 50x50", 2, 1, 7.0, 0.019, 630, 6.2, 0.001, 5, 0, 95, 0.9, 25, 100};
if (index)
pid[2] = {"Servo", 2, 2, 12.0, 0.09, 0, 15, 0, 0, 0, 95, 0.9, 20, 80};
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// PID
float PID_Regler(byte id){
// see: http://rn-wissen.de/wiki/index.php/Regelungstechnik
float x = ch[pitMaster[id].channel].temp; // IST
float w = pitMaster[id].set; // SOLL
byte ii = pitMaster[id].pid;
// PID Parameter
float kp, ki, kd;
if (x > (pid[ii].pswitch * w)) {
kp = pid[ii].Kp;
ki = pid[ii].Ki;
kd = pid[ii].Kd;
} else {
kp = pid[ii].Kp_a;
ki = pid[ii].Ki_a;
kd = pid[ii].Kd_a;
}
int diff;
float e;
// Abweichung bestimmen
switch (pid[pitMaster[id].pid].aktor) {
case SSR: // SSR
diff = (w -x)*100;
e = diff/100.0;
break;
default:
diff = (w -x)*10;
e = diff/10.0; // nur Temperaturunterschiede von >0.1°C beachten
}
//float e = w - x;
// Proportional-Anteil
float p_out = kp * e;
// Differential-Anteil
float edif = (e - pitMaster[id].elast)/(pitMaster[id].pause/1000.0);
pitMaster[id].elast = e;
float d_out = kd * edif;
// Integral-Anteil
// Anteil nur erweitert, falls Begrenzung nicht bereits erreicht
if ((p_out + d_out) < PITMAX) {
pitMaster[id].esum += e * (pitMaster[id].pause/1000.0);
}
// ANTI-WIND-UP (sonst Verzögerung)
// Limits an Ki anpassen: Ki*limit muss y_limit ergeben können
if (pitMaster[id].esum * ki > pid[ii].Ki_max) pitMaster[id].esum = pid[ii].Ki_max/ki;
else if (pitMaster[id].esum * ki < pid[ii].Ki_min) pitMaster[id].esum = pid[ii].Ki_min/ki;
float i_out = ki * pitMaster[id].esum;
// PID-Regler berechnen
float y = p_out + i_out + d_out;
y = constrain(y,PITMIN,PITMAX); // Auflösung am Ausgang ist begrenzt
PMPRINTLN("[PM]\tPID:" + String(y,1) + "\tp:" + String(p_out,1) + "\ti:" + String(i_out,2) + "\td:" + String(d_out,1));
return y;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Clear PID
void clear_PID_Regler(byte id) {
// PID zurücksetzen
pitMaster[id].esum = 0;
pitMaster[id].elast = 0;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Control - Pitmaster 12V Supply
void pitsupply(bool out, byte id) {
if (pitMaster[id].io == PITMASTER1 && sys.hwversion > 1) {
//if () out = HIGH; // SSR || FAN
//Serial.println(out);
digitalWrite(PITSUPPLY, out);
}
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// TURN PITMASTER OFF
void disableHeater(byte id) {
// Anschlüsse ausschalten
if (pid[pitMaster[id].pid].aktor == FAN || pid[pitMaster[id].pid].aktor == DAMPER) { // FAN
analogWrite(pitMaster[id].io, LOW);
} else {
digitalWrite(pitMaster[id].io, LOW); // SSR
}
//digitalWrite(PITMASTER2, LOW);
pitsupply(0, id); // 12V Supply abschalten, falls nötig
pitMaster[id].active = PITOFF;
pitMaster[id].value = 0;
pitMaster[id].event = false;
pitMaster[id].msec = 0;
clear_PID_Regler(id);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void disableAllHeater() {
disableHeater(0);
disableHeater(1);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
static inline float min(float a,float b) {
if(a < b) return a;
return b;
}
static inline float max(float a,float b) {
if(a < b) return b;
return a;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// STOP AUTOTUNE
void stopautotune(byte id) {
autotune.value = 0;
autotune.initialized = false;
if ((autotune.stop == 1) && autotune.storeValues) { // sauber beendet
setconfig(ePIT,{});
if (autotune.keepup) pitMaster[id].active = AUTO; // Pitmaster in AUTO fortsetzen
else pitMaster[id].active = PITOFF;
} else pitMaster[id].active = PITOFF;
question.typ = TUNE;
drawQuestion(autotune.stop);
autotune.stop = 0;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// START AUTOTUNE
void startautotunePID(int maxCyc, bool store, int over, long tlimit, byte id) {
autotune.cycles = 0; // Durchläufe
autotune.heating = true; // Flag
autotune.temp = pitMaster[id].set;
autotune.maxCycles = constrain(maxCyc, 5, 20);
autotune.storeValues = store;
autotune.keepup = false;
uint32_t tmi = millis();
float tem = ch[pitMaster[id].channel].temp;
// t0, t1, t2, t_high, bias, d, Kp, Ki, Kd, maxTemp, minTemp, pTemp, maxTP, tWP, TWP
autotune.t0 = tmi; // Zeitpunkt t0
autotune.t1 = tmi; // Zeitpunkt t1
autotune.t2 = tmi; // Zeitpunkt t2
autotune.t_high = 0;
autotune.bias = pidMax/2; // Startwert = halber Wert
autotune.d = pidMax/2; // Startwert = halber Wert
autotune.Kp = 0;
autotune.Ki = 0;
autotune.Kd = 0;
autotune.maxTemp = tem;
autotune.minTemp = tem;
autotune.pTemp = tem; // Startwert
autotune.maxTP = 0.0;
autotune.tWP = tmi;
autotune.TWP = tem;
autotune.overtemp = over; //40
autotune.timelimit = tlimit; //(10L*60L*1000L*4L)
PMPRINTPLN("[AT]\t Start!");
disableHeater(id); // switch off heaters.
autotune.value = pidMax; // Aktor einschalten
autotune.initialized = true;
pitMaster[id].active = AUTOTUNE;
autotune.start = tem;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// AUTOTUNE
float autotunePID(byte id) {
/*
Relay Feedback Test [Astrom & Hagglund, 1984
Autotune Variation (ATV)
see: https://www.google.de/?gws_rd=ssl#q=Relay+Feedback+PID
t_high = t1-t2*
+d ____ ____ _
bias--__| |____| |____|
-d t2* t1 t2
/ t_low = t2-t1
*/
// http://www.aaabbb.de/ControlTheory/ControlLoopTuningStepResponse.php
// Show start on OLED
if (autotune.start) {
question.typ = TUNE;
drawQuestion(0);
autotune.start = false;
}
float currentTemp = ch[pitMaster[id].channel].temp;
unsigned long time = millis();
if (autotune.cycles == 0) {
float TP = (currentTemp - autotune.pTemp)/ (float)pitMaster[id].pause;
if (autotune.maxTP < TP) {
autotune.maxTP = TP;
autotune.tWP = time;
autotune.TWP = (currentTemp + autotune.pTemp)/2.0;
PMPRINTP("[AT]\tWendepunktbestimmung: ");
PMPRINTLN(TP*1000);
}
autotune.pTemp = currentTemp;
}
autotune.maxTemp = max(autotune.maxTemp,currentTemp);
autotune.minTemp = min(autotune.minTemp,currentTemp);
// Soll während aufheizen ueberschritten --> switch heating off
if (autotune.heating == true && currentTemp > autotune.temp) {
if(time - autotune.t2 > 3000) { // warum Wartezeit und wieviel
autotune.heating = false;
autotune.value = (autotune.bias - autotune.d); // Aktorwert
autotune.t1 = time;
autotune.t_high = autotune.t1 - autotune.t2;
autotune.maxTemp = autotune.temp;
PMPRINTPLN("[AT]\toverrun!");
}
}
// Soll während abkühlen unterschritten --> switch heating on
else if (autotune.heating == false && currentTemp < autotune.temp) {
if(time - autotune.t1 > 3000) {
autotune.heating = true;
autotune.t2 = time;
autotune.t_low = autotune.t2 - autotune.t1; // half wave length
PMPRINTPLN("[AT]\tfall below");
if (autotune.cycles == 0) {
uint32_t tWP1 = (autotune.TWP-autotune.minTemp)/autotune.maxTP; // Zeitabschn. (ms) unter Wendetangente bis Ausgangstemperatur
uint32_t tWP2 = (autotune.temp-autotune.TWP)/autotune.maxTP; // Zeitabschn. (ms) oberhalb Wendetangente bis Zieltemperatur
uint32_t Tt = (autotune.tWP - autotune.t0 - tWP1)/1000.0; // Totzeit in sec
uint32_t Tg = (tWP1 + tWP2)/1000.0; // Ausgleichszeit in sec
float Ks = (autotune.temp - autotune.minTemp)/(autotune.bias + autotune.d);
float Tn = 2 * Tt; // Tn = 3.33 * Tt;
float Tv = 0.5 * Tt;
PMPRINTP("[AT]\tTt: ");
PMPRINT(Tt);
PMPRINTP(" s, Tg: ");
PMPRINT(Tg);
PMPRINTPLN(" s");
autotune.Kp_a = (0.1*autotune.temp);
autotune.Ki_a = 0;
autotune.Kd_a = 0;
PMPRINTP("[AT]\tKp_a: ");
PMPRINT(autotune.Kp_a);
PMPRINTP(" Ki_a: ");
PMPRINT(autotune.Ki_a);
PMPRINTP(" Kd_a: ");
PMPRINTLN(autotune.Kd_a);
} else {
// Anpassung Bias
autotune.bias += (autotune.d*(autotune.t_high - autotune.t_low))/(autotune.t_low + autotune.t_high);
autotune.bias = constrain(autotune.bias, 5 ,pidMax - 5); // Arbeitsbereich zwischen 5% und 95%
if (autotune.bias > pidMax/2) autotune.d = pidMax - autotune.bias;
else autotune.d = autotune.bias;
PMPRINTP("[AT]\tbias: ");
PMPRINT(autotune.bias);
PMPRINTP(" d: ");
PMPRINTLN(autotune.d);
if(autotune.cycles > 2) {
// Parameter according Ziegler & Nichols method: http://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method
// Ku = kritische Verstaerkung = 4*d / (pi*A)
// Tu = kritische Periodendauer = t_low + t_high = (t2 - t1) + (t1 - t2*) = t2 - t2*
// A = Amplitude der Schwingung = maxTemp-minTemp/2 // Division fehlt in der Quelle
float A = (autotune.maxTemp - autotune.minTemp)/2.0;
float Ku = (4.0 * autotune.d) / (3.14159 * A);
float Tu = ((float)(autotune.t_low + autotune.t_high)/1000.0);
PMPRINTP("[AT]\tKu: ");
PMPRINT(Ku);
PMPRINTP(" Tu: ");
PMPRINT(Tu);
PMPRINTP(" A: ");
PMPRINT(A);
PMPRINTP(" t_max: ");
PMPRINT(autotune.maxTemp);
PMPRINTP(" t_min: ");
PMPRINTLN(autotune.minTemp);
float Tn = 0.5*Tu;
float Tv = 0.125*Tu;
autotune.Kp = 0.6*Ku;
autotune.Ki = autotune.Kp/Tn; // Ki = 1.2*Ku/Tu = Kp/Tn = Kp/(0.5*Tu) = 2*Kp/Tu
autotune.Kd = autotune.Kp*Tv; // Kd = 0.075*Ku*Tu = Kp*Tv = Kp*0.125*Tu
PMPRINTP("[AT]\tKp: ");
PMPRINT(autotune.Kp);
PMPRINTP(" Ki: ");
PMPRINT(autotune.Ki);
PMPRINTP(" Kd: ");
PMPRINTLN(autotune.Kd);
}
}
autotune.value = (autotune.bias + autotune.d);
PMPRINTPLN("[AT]\tNext cycle");
autotune.cycles++;
autotune.minTemp = autotune.temp;
}
}
if (currentTemp > (autotune.temp + autotune.overtemp)) { // FEHLER
IPRINTPLN("f:AT OVERTEMP");
disableHeater(id);
autotune.stop = 2;
return 0;
}
if (((time - autotune.t1) + (time - autotune.t2)) > autotune.timelimit) { // 20 Minutes
IPRINTPLN("f:AT TIMEOUT");
disableHeater(id);
autotune.stop = 2;
return 0;
}
if (autotune.cycles > autotune.maxCycles) { // FINISH
PMPRINTPLN("[AT]\tFinished!");
disableHeater(id);
autotune.stop = 1;
if (autotune.storeValues) {
pid[pitMaster[id].pid].Kp = autotune.Kp;
pid[pitMaster[id].pid].Ki = autotune.Ki;
pid[pitMaster[id].pid].Kd = autotune.Kd;
pid[pitMaster[id].pid].Kp_a = autotune.Kp_a;
pid[pitMaster[id].pid].Ki_a = autotune.Ki_a;
pid[pitMaster[id].pid].Kd_a = autotune.Kd_a;
DPRINTLN("[AT]\tParameters saved!");
}
return 0;
}
PMPRINTLN("AT: " + String(autotune.value,1) + " %");
return autotune.value;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Start Duty Cycle Test
void DC_start(bool dc, byte aktor, int val, byte id) {
if (pitMaster[id].active != DUTYCYCLE) {
dutyCycle[id].dc = dc;
dutyCycle[id].aktor = aktor;
dutyCycle[id].value = val;
dutyCycle[id].timer = millis();
if (aktor == SERVO && sys.hwversion > 1) pitMaster[0].io = PITMASTER2; // Servo-Spezial
switch (pitMaster[id].active) {
case PITOFF: dutyCycle[id].saved = PITOFF; break;
case MANUAL: dutyCycle[id].saved = pitMaster[id].value; break;
case AUTOTUNE:
dutyCycle[id].saved = PITOFF; // autotune abbrechen
autotune.stop = 2;
break;
case AUTO: dutyCycle[id].saved = -1; break;
}
pitMaster[id].active = DUTYCYCLE;
}
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Stop Duty Cycle Test
void DC_stop(byte id) {
if (pitMaster[id].active == DUTYCYCLE && (millis() - dutyCycle[id].timer > 10000)) {
if (dutyCycle[id].saved == 0) { // off
pitMaster[id].active = PITOFF;
} else if (dutyCycle[id].saved > 0) {
pitMaster[id].value = dutyCycle[id].saved; // manual
pitMaster[id].active = MANUAL;
} else pitMaster[id].active = AUTO; // auto
PMPRINTLN("[DC]\tTest finished");
}
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Control - Pitmaster Pause
void check_pit_pause(byte id) {
int aktor;
uint16_t dcmin, dcmax;
if (pitMaster[id].active == DUTYCYCLE) {
aktor = dutyCycle[id].aktor;
dcmin = 0;
dcmax = 1000; // 1. Nachkommastelle
}
else {
aktor = pid[pitMaster[id].pid].aktor;
dcmin = pid[pitMaster[id].pid].DCmin*10; // 1. Nachkommastelle
dcmax = pid[pitMaster[id].pid].DCmax*10; // 1. Nachkommastelle
}
int pause;
switch (aktor) {
case SSR:
pause = 2000; // 1/2 Hz, Netzsynchron // myPitmaster-Anpassung
pitMaster[id].dcmin = map(dcmin,0,1000,0,pitMaster[id].pause);
pitMaster[id].dcmax = map(dcmax,0,1000,0,pitMaster[id].pause);
break;
case DAMPER:
case FAN:
pause = 1000; // 1 Hz
pitMaster[id].dcmin = map(dcmin,0,1000,0,1024);
pitMaster[id].dcmax = map(dcmax,0,1000,0,1024);
break;
case SERVO:
pause = 20; // 50 Hz
pitMaster[id].dcmin = map(dcmin,0,1000,SERVOPULSMIN,SERVOPULSMAX);
pitMaster[id].dcmax = map(dcmax,0,1000,SERVOPULSMIN,SERVOPULSMAX);
break;
}
pitMaster[id].pause = pause;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// myPitmaster
int myPitmaster(Pitmaster pitmaster) {
// veränderte Taktzeit durch Anpassung der PitmasterPause
// Puffertemp < Ofentemp und Ofentemp > Grenze
if (ch[pitmaster.channel].temp < ch[3].temp && ch[3].temp > pitmaster.set) // ch[3] = OFEN
return 100;
else
return 0;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Control - Manuell PWM
void pitmaster_control(byte id) {
// Stop Autotune
if (autotune.stop > 0) stopautotune(id);
// Stop Duty Cylce Test
DC_stop(id);
// ESP PWM funktioniert nur bis 10 Hz Trägerfrequenz stabil, daher eigene Taktung
if (pitMaster[id].active > 0) {
// Check Pitmaster Pause
check_pit_pause(id);
// SSR: Ende eines HIGH-Intervalls, wird durch pit_event nur einmal pro Intervall durchlaufen
if ((millis() - pitMaster[id].last > pitMaster[id].msec) && pitMaster[id].event) {
digitalWrite(pitMaster[id].io, LOW);
pitMaster[id].event = false;
}
// neuen Stellwert bestimmen und ggf HIGH-Intervall einleiten
if (millis() - pitMaster[id].last > pitMaster[id].pause) {
pitMaster[id].last = millis();
byte aktor;
// PITMASTER TYP
switch (pitMaster[id].active) {
case DUTYCYCLE:
aktor = dutyCycle[id].aktor;
// Startanlauf: bei Servo beide male zuerst in die Mitte, bei Fan nur unten
if (millis() - dutyCycle[id].timer < 1000) {
if ((aktor == FAN && !dutyCycle[id].dc) || aktor == SERVO) pitMaster[id].value = 50;
} else pitMaster[id].value = dutyCycle[id].value/10.0;
pitMaster[id].timer0 = 0; // Überbrückung Anlauf-Prozess
break;
case AUTOTUNE:
pitMaster[id].value = autotunePID(id);
aktor = pid[pitMaster[id].pid].aktor;
break;
case AUTO:
pitMaster[id].value = PID_Regler(id); //myPitmaster();
aktor = pid[pitMaster[id].pid].aktor;
break;
case MANUAL: // falls manual wird value vorgegeben
aktor = pid[pitMaster[id].pid].aktor;
break;
}
// PITMASTER AKTOR
switch (aktor) {
case SSR:
pitsupply(1, id); // immer 12V Supply
pitMaster[id].msec = map(pitMaster[id].value,0,100,pitMaster[id].dcmin,pitMaster[id].dcmax);
if (pitMaster[id].msec > 0) digitalWrite(pitMaster[id].io, HIGH);
if (pitMaster[id].msec < pitMaster[id].pause) pitMaster[id].event = true; // außer bei 100%
break;
case DAMPER:
// PITMASTER 1 = FAN
// PITMASTER 2 = SERVO
case FAN:
pitsupply(sys.pitsupply, id); // 12V Supply nur falls aktiviert
if (pitMaster[id].value == 0) {
analogWrite(pitMaster[id].io,0); // bei 0 soll der Lüfter auch stehen
pitMaster[id].timer0 = millis();
} else {
// Anlaufhilfe: ein Zyklus auf 30% wenn von 0% kommend
if (millis() - pitMaster[id].timer0 < pitMaster[id].pause*2 && pitMaster[id].value < 30) {
analogWrite(pitMaster[id].io,map(30,0,100,pitMaster[id].dcmin,pitMaster[id].dcmax)); // BOOST
} else {
analogWrite(pitMaster[id].io,map(pitMaster[id].value,0,100,pitMaster[id].dcmin,pitMaster[id].dcmax));
}
}
break;
case SERVO: // Achtung bei V2 mit den 12V bei Anschluss an Stromversorgung
pitsupply(0, id); // keine 12V Supply
pitMaster[id].msec = mapfloat(pitMaster[id].value,0,100,pitMaster[id].dcmin,pitMaster[id].dcmax);
//Serial.println(pitMaster[id].msec);
if (pid[pitMaster[0].pid].aktor == DAMPER) { // Einfacher Damper
pitMaster[id].msec = pitMaster[id].dcmin;
if (pitMaster[0].value > 0) pitMaster[id].msec = pitMaster[id].dcmax;
}
pitMaster[id].timer0 = micros();
noInterrupts();
digitalWrite(pitMaster[id].io, HIGH);
while (micros() - pitMaster[id].timer0 < pitMaster[id].msec) {} //delayMicroseconds() ist zu ungenau
digitalWrite(pitMaster[id].io, LOW);
interrupts();
break;
}
}
} else { // TURN OFF PITMASTER
disableHeater(id);
}
}