1 // pidcontroller.cxx - implementation of PID controller
3 // Written by Torsten Dreyer
4 // Based heavily on work created by Curtis Olson, started January 2004.
6 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7 // Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "pidcontroller.hxx"
26 using namespace FGXMLAutopilot;
31 PIDController::PIDController():
48 * Ok! Here is the PID controller algorithm that I would like to see
51 * delta_u_n = Kp * [ (ep_n - ep_n-1) + ((Ts/Ti)*e_n)
52 * + (Td/Ts)*(edf_n - 2*edf_n-1 + edf_n-2) ]
54 * u_n = u_n-1 + delta_u_n
58 * delta_u : The incremental output
59 * Kp : Proportional gain
60 * ep : Proportional error with reference weighing
63 * beta : Weighing factor
64 * r : Reference (setpoint)
65 * y : Process value, measured
68 * Ts : Sampling interval
69 * Ti : Integrator time
71 * edf : Derivate error with reference weighing and filtering
72 * edf_n = edf_n-1 / ((Ts/Tf) + 1) + ed_n * (Ts/Tf) / ((Ts/Tf) + 1)
75 * Tf = alpha * Td , where alpha usually is set to 0.1
76 * ed : Unfiltered derivate error with reference weighing
79 * gamma : Weighing factor
83 * Index n means the n'th value.
88 * y_n , r_n , beta=1 , gamma=0 , alpha=0.1 ,
89 * Kp , Ti , Td , Ts (is the sampling time available?)
96 void PIDController::update( bool firstTime, double dt )
99 double u_n = 0.0; // absolute output
101 double u_min = _minInput.get_value();
102 double u_max = _maxInput.get_value();
105 if( elapsedTime <= desiredTs ) {
106 // do nothing if time step is not positive (i.e. no time has
110 double Ts = elapsedTime; // sampling interval (sec)
114 // first time being enabled, seed u_n with current
115 // property tree value
117 edf_n_2 = edf_n_1 = edf_n = 0.0;
118 u_n = get_output_value();
122 if( Ts > SGLimitsd::min()) {
123 if( _debug ) cout << "Updating " << get_name()
124 << " Ts " << Ts << endl;
126 double y_n = _valueInput.get_value();
127 double r_n = _referenceInput.get_value();
129 if ( _debug ) cout << " input = " << y_n << " ref = " << r_n << endl;
131 // Calculates proportional error:
132 double ep_n = beta * r_n - y_n;
133 if ( _debug ) cout << " ep_n = " << ep_n;
134 if ( _debug ) cout << " ep_n_1 = " << ep_n_1;
137 double e_n = r_n - y_n;
138 if ( _debug ) cout << " e_n = " << e_n;
140 double td = Td.get_value();
141 if ( td > 0.0 ) { // do we need to calcluate derivative error?
143 // Calculates derivate error:
144 double ed_n = gamma * r_n - y_n;
145 if ( _debug ) cout << " ed_n = " << ed_n;
147 // Calculates filter time:
148 double Tf = alpha * td;
149 if ( _debug ) cout << " Tf = " << Tf;
151 // Filters the derivate error:
152 edf_n = edf_n_1 / (Ts/Tf + 1)
153 + ed_n * (Ts/Tf) / (Ts/Tf + 1);
154 if ( _debug ) cout << " edf_n = " << edf_n;
156 edf_n_2 = edf_n_1 = edf_n = 0.0;
159 // Calculates the incremental output:
160 double ti = Ti.get_value();
161 double delta_u_n = 0.0; // incremental output
163 delta_u_n = Kp.get_value() * ( (ep_n - ep_n_1)
165 + ((td/Ts) * (edf_n - 2*edf_n_1 + edf_n_2)) );
168 cout << " delta_u_n = " << delta_u_n << endl;
169 cout << "P:" << Kp.get_value() * (ep_n - ep_n_1)
170 << " I:" << Kp.get_value() * ((Ts/ti) * e_n)
171 << " D:" << Kp.get_value() * ((td/Ts) * (edf_n - 2*edf_n_1 + edf_n_2))
176 // Integrator anti-windup logic:
177 if ( delta_u_n > (u_max - u_n_1) ) {
178 delta_u_n = u_max - u_n_1;
179 if ( _debug ) cout << " max saturation " << endl;
180 } else if ( delta_u_n < (u_min - u_n_1) ) {
181 delta_u_n = u_min - u_n_1;
182 if ( _debug ) cout << " min saturation " << endl;
185 // Calculates absolute output:
186 u_n = u_n_1 + delta_u_n;
187 if ( _debug ) cout << " output = " << u_n << endl;
189 // Updates indexed values;
195 set_output_value( u_n );
199 bool PIDController::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
201 SG_LOG( SG_AUTOPILOT, SG_BULK, "PIDController::configure(" << nodeName << ")" << endl );
203 if( AnalogComponent::configure( nodeName, configNode ) )
206 if( nodeName == "config" ) {
207 Component::configure( configNode );
211 if (nodeName == "Ts") {
212 desiredTs = configNode->getDoubleValue();
216 if (nodeName == "Kp") {
217 Kp.push_back( new InputValue(configNode) );
221 if (nodeName == "Ti") {
222 Ti.push_back( new InputValue(configNode) );
226 if (nodeName == "Td") {
227 Td.push_back( new InputValue(configNode) );
231 if (nodeName == "beta") {
232 beta = configNode->getDoubleValue();
236 if (nodeName == "alpha") {
237 alpha = configNode->getDoubleValue();
241 if (nodeName == "gamma") {
242 gamma = configNode->getDoubleValue();
246 SG_LOG( SG_AUTOPILOT, SG_BULK, "PIDController::configure(" << nodeName << ") [unhandled]" << endl );