13 #define DEGTORAD ((double)3.1415926535/180.0)
15 CStdPID::CStdPID(
void)
20 CStdPID::CStdPID(
float fltSetpoint,
float fltGain,
float fltIntegralAct,
float fltDerivativeAct,
21 bool bComplexError,
bool bAntiResetWindup,
bool bRampLimit,
22 float fltRangeMax,
float fltRangeMin,
float fltARWBound,
float fltRampGradient)
26 Setpoint(fltSetpoint);
28 IntegralAct(fltIntegralAct);
29 DerivativeAct(fltDerivativeAct);
30 ComplexError(bComplexError);
31 AntiResetWindup(bAntiResetWindup);
32 RampLimit(bRampLimit);
33 RangeMax(fltRangeMax);
34 RangeMin(fltRangeMin);
40 CStdPID::~CStdPID(
void)
45 void CStdPID::FullReset()
49 m_bComplexError =
true;
50 m_bAntiResetWindup =
true;
57 m_fltProportional = 0;
61 m_fltDerivativeAct = 0;
70 m_fltRampGradient = 0;
73 m_aryOldErrors.Clear();
74 m_aryOldErrors.Add(0);
75 m_aryOldErrors.Add(0);
76 m_aryOldErrors.Add(0);
79 void CStdPID::ResetVars()
85 m_fltProportional = 0;
94 m_aryOldErrors.Clear();
95 m_aryOldErrors.Add(0);
96 m_aryOldErrors.Add(0);
97 m_aryOldErrors.Add(0);
100 float CStdPID::Error() {
return m_fltError;}
102 float CStdPID::ErrorChange() {
return m_fltErrorChange;}
104 void CStdPID::Setpoint(
float fltVal) {m_fltSetpoint = fltVal;}
106 float CStdPID::Setpoint() {
return m_fltSetpoint;}
108 void CStdPID::Gain(
float fltVal)
114 float CStdPID::Gain() {
return m_fltGain;}
116 void CStdPID::IntegralAct(
float fltVal)
119 m_fltIntegralAct = fltVal;
122 float CStdPID::IntegralAct() {
return m_fltIntegralAct;}
124 void CStdPID::DerivativeAct(
float fltVal)
127 m_fltDerivativeAct = fltVal;
130 float CStdPID::DerivativeAct() {
return m_fltDerivativeAct;}
132 float CStdPID::Proportional() {
return m_fltProportional;}
134 float CStdPID::Integral() {
return m_fltIntegral;}
136 float CStdPID::OldIntegral() {
return m_fltOldIntegral;}
138 float CStdPID::Derivative() {
return m_fltDerivative;}
140 float CStdPID::Output() {
return m_fltOutput;}
142 float CStdPID::OldOutput() {
return m_fltOldOutput;}
144 void CStdPID::ComplexError(
bool bVal) {m_bComplexError = bVal;}
146 bool CStdPID::ComplexError() {
return m_bComplexError;}
148 void CStdPID::AntiResetWindup(
bool bVal) {m_bAntiResetWindup = bVal;}
150 bool CStdPID::AntiResetWindup() {
return m_bAntiResetWindup;}
152 void CStdPID::RampLimit(
bool bVal) {m_bRampLimit = bVal;}
154 bool CStdPID::RampLimit() {
return m_bRampLimit;}
156 void CStdPID::RangeMax(
float fltVal)
158 if(fltVal >= m_fltRangeMin)
160 m_fltRangeMax = fltVal;
161 m_fltRange = m_fltRangeMax - m_fltRangeMin;
165 float CStdPID::RangeMax() {
return m_fltRangeMax;}
167 void CStdPID::RangeMin(
float fltVal)
169 if(fltVal <= m_fltRangeMax)
171 m_fltRangeMin = fltVal;
172 m_fltRange = m_fltRangeMax - m_fltRangeMin;
176 float CStdPID::RangeMin() {
return m_fltRangeMin;}
178 float CStdPID::Range() {
return m_fltRange;}
190 void CStdPID::ARWBound(
float fltVal)
192 if(fltVal >= 0 && fltVal <= 1)
193 m_fltARWBound = fltVal;
196 float CStdPID::ARWBound() {
return m_fltARWBound;}
206 void CStdPID::RampGradient(
float fltVal)
208 if(fltVal >= 0 && fltVal <= 90)
209 m_fltRampGradient = fltVal;
212 float CStdPID::RampGradient() {
return m_fltRampGradient;}
216 float CStdPID::Calculate(
float fltDt,
float fltInput)
225 m_fltError = m_fltSetpoint-fltInput;
230 fltError1 = m_aryOldErrors.GetAt(2);
231 m_fltErrorChange = m_fltError-fltError1;
235 fltError1 = m_aryOldErrors.GetAt(2);
236 fltError2 = m_aryOldErrors.GetAt(1);
237 fltError3 = m_aryOldErrors.GetAt(0);
238 m_fltErrorChange = (m_fltError+(3*fltError1)-(3*fltError2)-fltError3)/6;
242 m_fltProportional = m_fltGain * m_fltError;
245 m_fltIntegral = m_fltIntegral + (m_fltGain * (m_fltIntegralAct*fltDt) * m_fltError);
248 m_fltDerivative = m_fltGain * (m_fltDerivativeAct/fltDt) * m_fltErrorChange;
251 m_fltOutput = (m_fltProportional + m_fltIntegral + m_fltDerivative);
254 if(m_bAntiResetWindup)
256 if(m_fltOutput >= m_fltRangeMax-(m_fltARWBound*m_fltRange))
258 m_fltOutput = m_fltRangeMax-(m_fltARWBound*m_fltRange);
259 m_fltIntegral = m_fltOldIntegral;
261 else if(m_fltOutput <= m_fltRangeMin+(m_fltARWBound*m_fltRange))
263 m_fltOutput = m_fltRangeMin+(m_fltARWBound*m_fltRange);
264 m_fltIntegral = m_fltOldIntegral;
271 fltMaxChange = (float)tan((((
double)m_fltRampGradient)*DEGTORAD)/fltDt);
272 fltChange = m_fltOutput - m_fltOldOutput;
273 if(fltChange > fltMaxChange)
275 m_fltOutput = m_fltOldOutput + fltMaxChange;
278 fltChange = (float)fabs(m_fltIntegral - m_fltOldIntegral);
279 if(fltChange > fltMaxChange)
281 m_fltIntegral = m_fltOldIntegral + fltMaxChange;
284 else if(fltChange < (0-fltMaxChange))
286 m_fltOutput = m_fltOldOutput - fltMaxChange;
289 fltChange = (float)fabs(m_fltIntegral - m_fltOldIntegral);
290 if(fltChange > fltMaxChange)
292 m_fltIntegral = m_fltOldIntegral - fltMaxChange;
297 m_aryOldErrors.AddEnd(m_fltError);
299 m_fltOldIntegral = m_fltIntegral;
301 m_fltOldOutput = m_fltOutput;
virtual void ARWBound(float fltVal)
Sets the anti-reset boundary. This is specified as a percentage of the entire output range...
virtual void RampGradient(float fltVal)
Ramp gradient.
Namespace for the standard utility objects.