22 |
22 |
23 #include "thermferm.h" |
23 #include "thermferm.h" |
24 #include "pid.h" |
24 #include "pid.h" |
25 |
25 |
26 |
26 |
27 double UpdatePID(pid_var *pid, double error, double position) |
27 void InitPID(pid_var *pid, int type) |
28 { |
28 { |
29 double pTerm, dTerm, iTerm; |
29 pid->Err = pid->ErrLast = pid->ErrLastLast = 0.0; |
30 |
30 pid->Input = pid->InputD = pid->OutP = pid->InputLast = pid->SetP = 0.0; |
31 pTerm = pid->pGain * error; |
31 pid->pGain = pid->iGain = pid->dGain = 0.0; |
32 |
32 pid->idleRange = 0.4; |
33 /* |
33 pid->Mode = PID_MODE_NONE; |
34 * Calculate the integral state with appopriate limiting |
34 pid->Type = type; |
35 */ |
35 pid->iMax = 100.0; |
36 pid->iState += error; |
|
37 if (pid->iState > pid->iMax) |
|
38 pid->iState = pid->iMax; |
|
39 else if (pid->iState < pid->iMin) |
|
40 pid->iState = pid->iMin; |
|
41 |
|
42 iTerm = pid->iGain * pid->iState; |
|
43 |
|
44 dTerm = pid->dGain * (pid->dState - position); |
|
45 pid->dState = position; |
|
46 |
|
47 return pTerm + dTerm + iTerm; |
|
48 } |
36 } |
49 |
37 |
50 |
38 |
|
39 |
|
40 void UpdatePID(pid_var *pid) |
|
41 { |
|
42 if (pid->Mode == PID_MODE_AUTO) { |
|
43 |
|
44 pid->InputD = pid->Input + (pid->Input - pid->InputLast) * pid->dGain * PID_TIMES; |
|
45 pid->InputLast = pid->Input; |
|
46 if (pid->Type == PID_TYPE_HEAT) |
|
47 pid->Err = pid->InputD - pid->SetP; |
|
48 else |
|
49 pid->Err = pid->SetP - pid->InputD; |
|
50 |
|
51 pid->OutP = pid->OutP + pid->pGain * (pid->Err - pid->ErrLast + pid->iGain * pid->Err + pid->dGain * (pid->Err - pid->ErrLast * 2 + pid->ErrLastLast)); |
|
52 pid->ErrLastLast = pid->ErrLast; |
|
53 pid->ErrLast = pid->Err; |
|
54 |
|
55 } if (pid->Mode == PID_MODE_BOO) { |
|
56 /* |
|
57 * Mode Bang On Off |
|
58 */ |
|
59 pid->InputLast = pid->Input; |
|
60 if (pid->Type == PID_TYPE_HEAT) |
|
61 pid->ErrLastLast = pid->ErrLast = pid->Err = pid->SetP - pid->Input; |
|
62 else |
|
63 pid->ErrLastLast = pid->ErrLast = pid->Err = pid->Input - pid->SetP; |
|
64 |
|
65 if (pid->Err > 0.0) |
|
66 pid->OutP = pid->iMax; |
|
67 else |
|
68 pid->OutP = 0.0; |
|
69 |
|
70 } else { |
|
71 /* |
|
72 * While in manual mode, stay ready for bumpless switch to |
|
73 * auto. |
|
74 */ |
|
75 pid->InputLast = pid->Input; |
|
76 pid->ErrLastLast = pid->ErrLast = pid->Err; |
|
77 } |
|
78 |
|
79 if (pid->OutP > pid->iMax) |
|
80 pid->OutP = pid->iMax; |
|
81 if (pid->OutP < 0.0) |
|
82 pid->OutP = 0.0; |
|
83 |
|
84 } |
|
85 |
|
86 |