-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpid_window.h
104 lines (84 loc) · 2.51 KB
/
pid_window.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
#ifndef pid_window_h
#define pid_window_h
#include <max31855.h>
#include <pidtune.h>
// #include <FastPID.h>
#include <PID_v1.h> // https://github.com/br3ttb/Arduino-PID-Library
uint16_t fullPowerPeriod = 15000; // full power startup pulse period
bool fullPowerStartup = true; // enable full power period
int long pidTimerStart = 0;
bool pidRunning = false; // flag is pid running, used for init start etc.
float wantedTemp = -1;
float currentDelta = 0;
bool isCuttoff = false;
bool isFanOn = false;
float lastWantedTemp = -1;
bool DEBUG_pid = false;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void pid_reset_I(){
myPID.SetTunings(Kp, 0, Kd);
}
void pid_preset_I(){
myPID.SetTunings(Kp, Ki, Kd);
}
void init_PID(){
// float Kp=97.403, Ki=3.142, Kd=754.9, Hz=10;
Serial.println("[PID] init");
Serial.println("[PID] PID : " + (String)Kp + " " + (String)Ki + " " + (String)Kd);
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(0,250); // depending on the ssr freq, low values do nothing < 5%, test this, even 1% will slowly add thermal over time
// myPID.SetSampleTime(120);
// if(!res) Serial.println("[ERROR] init FAILED (outputrange)");
// if(myPID.err()) Serial.println("[ERROR] init FAILED (construct)");
}
void pidStart(){
pidTimerStart = millis();
if(fullPowerStartup){
myPID.SetMode(MANUAL);
Output = 250; // output never returns to normal !!!!
}
pidRunning = true;
}
void run_PID(){
if(!pidRunning) pidStart();
// updateTemps(); // this is too close to the PID freq
// make sure temps are updated before calling run pid, but do not run close to the same frequency
// this will cause oscillations due to noise induced inthe TC for example
Setpoint = (double)wantedTemp;
Input = (double)currentTempAvg;
// reset integrator at 50%
if(Setpoint > (wantedTemp*0.50)){
pid_reset_I();
}
else pid_preset_I();
myPID.Compute();
// why is PID doing effort after setpoint
if(Input > Setpoint && (Output > 1)){
Serial.println("[PID] WTF PID");
}
// Serial.print("-");
// Serial.print(Output);
if(fullPowerStartup){
if(millis()-pidTimerStart < fullPowerPeriod){
}
else if(myPID.GetMode() == MANUAL){
Output = 0;
myPID.SetMode(AUTOMATIC);
}
}
setSSR(Output);
}
void stop_PID(){
wantedTemp = 0;
setSSR(0);
pidRunning = false;
}
void MatchTemp()
{
// Serial.print(".");
if(wantedTemp > 0) run_PID();
return;
}
#endif