|
1 /***************************************************************************** |
|
2 * Copyright (C) 2015 |
|
3 * |
|
4 * Michiel Broek <mbroek at mbse dot eu> |
|
5 * |
|
6 * This file is part of the mbsePi-apps |
|
7 * |
|
8 * This is free software; you can redistribute it and/or modify it |
|
9 * under the terms of the GNU General Public License as published by the |
|
10 * Free Software Foundation; either version 2, or (at your option) any |
|
11 * later version. |
|
12 * |
|
13 * mbsePi-apps is distributed in the hope that it will be useful, but |
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with ThermFerm; see the file COPYING. If not, write to the Free |
|
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
21 *****************************************************************************/ |
|
22 |
|
23 #include "brewco.h" |
|
24 #include "pid.h" |
|
25 |
|
26 |
|
27 void InitPID(pid_var *pid) |
|
28 { |
|
29 pid->Err = pid->ErrLast = pid->iState = 0.0; |
|
30 pid->Input = pid->OutP = pid->SetP = 0.0; |
|
31 pid->pGain = pid->iGain = pid->dGain = 0.0; |
|
32 pid->Mode = PID_MODE_NONE; |
|
33 pid->iMax = 100.0; |
|
34 } |
|
35 |
|
36 |
|
37 |
|
38 void UpdatePID(pid_var *pid) |
|
39 { |
|
40 if (pid->Mode == PID_MODE_AUTO) { |
|
41 |
|
42 double pTerm, dTerm, iTerm; |
|
43 |
|
44 pid->Err = pid->SetP - pid->Input; |
|
45 |
|
46 /* |
|
47 * Calculate the integral state with appopriate limiting. |
|
48 * Use ErrLastLast as iState |
|
49 */ |
|
50 pid->iState += pid->Err; |
|
51 if (pid->iState > PID_WINDUP_GUARD) |
|
52 pid->iState = PID_WINDUP_GUARD; |
|
53 else if (pid->iState < -PID_WINDUP_GUARD) |
|
54 pid->iState = -PID_WINDUP_GUARD; |
|
55 |
|
56 pTerm = pid->pGain * pid->Err; |
|
57 iTerm = pid->iGain * pid->iState; |
|
58 dTerm = pid->dGain * (pid->Err - pid->ErrLast); |
|
59 |
|
60 pid->OutP = pTerm + dTerm + iTerm; |
|
61 pid->ErrLast = pid->Err; |
|
62 |
|
63 } else if (pid->Mode == PID_MODE_BOO) { |
|
64 /* |
|
65 * Mode Bang On Off |
|
66 */ |
|
67 pid->ErrLast = pid->Err; |
|
68 pid->Err = pid->SetP - pid->Input; |
|
69 |
|
70 if (pid->OutP && (pid->Err <= 0.0)) |
|
71 pid->OutP = 0.0; |
|
72 else if ((pid->OutP == 0.0) && (pid->Err > 0.0)) |
|
73 pid->OutP = pid->iMax; |
|
74 |
|
75 pid->iState = 0.0; |
|
76 |
|
77 } else { |
|
78 /* |
|
79 * While in manual mode, stay ready for bumpless switch to |
|
80 * auto. |
|
81 */ |
|
82 pid->ErrLast = pid->Err = 0.0; |
|
83 pid->OutP = pid->iState = 0.0; |
|
84 } |
|
85 |
|
86 if (pid->OutP > pid->iMax) |
|
87 pid->OutP = pid->iMax; |
|
88 if (pid->OutP < 0.0) |
|
89 pid->OutP = 0.0; |
|
90 |
|
91 } |
|
92 |
|
93 |