libnxter  0.1
PID.nxc
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  PID.nxc
4  Copyright (C) 2008 Naba Kumar <naba@gnome.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #ifndef _PID_CONTROLLER_H_
22 #define _PID_CONTROLLER_H_
23 
32 struct PIDControl
33 {
34  long PIDScale;
35  long PValue;
36  long IValue;
37  long DValue;
38  long setPoint;
43  /* Output parameters */
44  long outputGain;
45  long absMaxOutput;
48  /* State */
49  long integral;
50  long lastError;
51  long steadyStateCount;
52 };
53 
71 void PIDControlInit(PIDControl &pidControl, long PIDScale,
72  long PValue, long IValue, long DValue,
73  int steadyStateCountThreshold,
74  long outputGain, long absMaxOutput)
75 {
76  pidControl.PIDScale = PIDScale;
77  pidControl.PValue = PValue;
78  pidControl.IValue = IValue;
79  pidControl.DValue = DValue;
80  pidControl.steadyStateCountThreshold = steadyStateCountThreshold;
81  pidControl.outputGain = outputGain;
82  pidControl.absMaxOutput = Abs(absMaxOutput);
83  pidControl.absIntegralLimit = pidControl.absMaxOutput;
84  pidControl.setPoint = 0;
85  pidControl.integral = 0;
86  pidControl.lastError = 0;
87  pidControl.steadyStateCount = 0;
88 }
89 
94 void PIDControlSetIntegralLimit(PIDControl &pidControl, long absIntegralLimit)
95 {
96  pidControl.absIntegralLimit = Abs(absIntegralLimit);
97 }
98 
103 void PIDControlSetPoint(PIDControl &pidControl, long setPoint)
104 {
105  pidControl.setPoint = setPoint;
106  pidControl.integral = 0;
107  pidControl.steadyStateCount = 0;
108 }
109 
121 {
122  if (pidControl.steadyStateCount > pidControl.steadyStateCountThreshold)
123  return true;
124  else
125  return false;
126 }
127 
132 {
133  return pidControl.lastError;
134 }
135 
142 long PIDControlStep(PIDControl &pidControl, long currentPoint)
143 {
144  long P, I, D, error, output;
145 
146  if (PIDControlCheckEnd(pidControl))
147  return 0;
148 
149  error = pidControl.setPoint - currentPoint;
150 
151  /* If error hasn't changed for last steadyStateCountThreshold samples, end PID control */
152  if (pidControl.steadyStateCountThreshold > 0 && (error - pidControl.lastError) == 0)
153  pidControl.steadyStateCount++;
154 
155  P = ((pidControl.PValue * error) / pidControl.PIDScale);
156  I = pidControl.integral + ((pidControl.IValue * error) / pidControl.PIDScale);
157  D = ((pidControl.DValue) * (error - pidControl.lastError)) / pidControl.PIDScale;
158 
159  /* Limit integral output */
160  if (I > (pidControl.absIntegralLimit/pidControl.outputGain))
161  I = pidControl.absIntegralLimit;
162  if (I < -(pidControl.absIntegralLimit/pidControl.outputGain))
163  I = -pidControl.absIntegralLimit;
164 
165  output = pidControl.outputGain * (P + I + D);
166  if (output > pidControl.absMaxOutput) output = pidControl.absMaxOutput;
167  if (output < -pidControl.absMaxOutput) output = -pidControl.absMaxOutput;
168 
169  pidControl.integral = I;
170  pidControl.lastError = error;
171 
172  return output;
173 }
174 
175 #endif
176 
long PIDControlStep(PIDControl &pidControl, long currentPoint)
Steps the PID controller. currentPoint is the current state of the system. The return value is the ou...
Definition: PID.nxc:142
long PIDScale
Definition: PID.nxc:34
long setPoint
Definition: PID.nxc:38
long absIntegralLimit
Definition: PID.nxc:46
PID controller class.
Definition: PID.nxc:32
long PValue
Definition: PID.nxc:35
int steadyStateCountThreshold
Definition: PID.nxc:39
long integral
Definition: PID.nxc:49
long DValue
Definition: PID.nxc:37
void PIDControlInit(PIDControl &pidControl, long PIDScale, long PValue, long IValue, long DValue, int steadyStateCountThreshold, long outputGain, long absMaxOutput)
Initializes a PID controller with the given P, I and D values, output parameters. ...
Definition: PID.nxc:71
bool PIDControlCheckEnd(PIDControl &pidControl)
Checks if the PID controller has ended.
Definition: PID.nxc:120
long lastError
Definition: PID.nxc:50
void PIDControlSetIntegralLimit(PIDControl &pidControl, long absIntegralLimit)
Sets integral output absolute maximum used to prevent integral windup. The default is set to absMaxOu...
Definition: PID.nxc:94
void PIDControlSetPoint(PIDControl &pidControl, long setPoint)
Sets a new set point for the controller. The controller state is reset and the new target is pursued...
Definition: PID.nxc:103
long outputGain
Definition: PID.nxc:44
long absMaxOutput
Definition: PID.nxc:45
long PIDControlGetLastError(PIDControl &pidControl)
Gets the error from the last step.
Definition: PID.nxc:131
long IValue
Definition: PID.nxc:36