thermferm/pid.c

changeset 316
73cd31dc6ce1
child 362
c92651a54969
equal deleted inserted replaced
315:198f3b4bd0d8 316:73cd31dc6ce1
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 "thermferm.h"
24 #include "pid.h"
25
26
27 double UpdatePID(pid_var *pid, double error, double position)
28 {
29 double pTerm, dTerm, iTerm;
30
31 pTerm = pid->pGain * error;
32
33 /*
34 * Calculate the integral state with appopriate limiting
35 */
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 }
49
50

mercurial