]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGPID.cpp
sync with JSB JSBSim CVS
[flightgear.git] / src / FDM / JSBSim / models / flight_control / FGPID.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGPID.cpp
4  Author:       Jon S. Berndt
5  Date started: 6/17/2006
6
7  ------------- Copyright (C) 2006 Jon S. Berndt (jon@jsbsim.org) -------------
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17  details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 HISTORY
27 --------------------------------------------------------------------------------
28 Initial code 6/17/2006 JSB
29
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 COMMENTS, REFERENCES,  and NOTES
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 INCLUDES
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37
38 #include "FGPID.h"
39 #include "input_output/FGXMLElement.h"
40 #include <string>
41 #include <iostream>
42
43 using namespace std;
44
45 namespace JSBSim {
46
47 static const char *IdSrc = "$Id: FGPID.cpp,v 1.20 2012/05/10 12:10:48 jberndt Exp $";
48 static const char *IdHdr = ID_PID;
49
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 CLASS IMPLEMENTATION
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
54 FGPID::FGPID(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
55 {
56   string kp_string, ki_string, kd_string;
57
58   Kp = Ki = Kd = 0.0;
59   KpPropertyNode = 0;
60   KiPropertyNode = 0;
61   KdPropertyNode = 0;
62   KpPropertySign = 1.0;
63   KiPropertySign = 1.0;
64   KdPropertySign = 1.0;
65   I_out_total = 0.0;
66   Input_prev = Input_prev2 = 0.0;
67   Trigger = 0;
68   ProcessVariableDot = 0;
69   IsStandard = false;
70   IntType = eNone;       // No integrator initially defined.
71
72   string pid_type = element->GetAttributeValue("type");
73
74   if (pid_type == "standard") IsStandard = true;
75
76   if ( element->FindElement("kp") ) {
77     kp_string = element->FindElementValue("kp");
78     if (!is_number(kp_string)) { // property
79       if (kp_string[0] == '-') {
80        KpPropertySign = -1.0;
81        kp_string.erase(0,1);
82       }
83       KpPropertyNode = PropertyManager->GetNode(kp_string);
84     } else {
85       Kp = element->FindElementValueAsNumber("kp");
86     }
87   }
88
89   if ( element->FindElement("ki") ) {
90     ki_string = element->FindElementValue("ki");
91
92     string integ_type = element->FindElement("ki")->GetAttributeValue("type");
93     if (integ_type == "rect") {            // Use rectangular integration
94       IntType = eRectEuler;
95     } else if (integ_type == "trap") {     // Use trapezoidal integration
96       IntType = eTrapezoidal;
97     } else if (integ_type == "ab2") {      // Use Adams Bashforth 2nd order integration
98       IntType = eAdamsBashforth2;
99     } else if (integ_type == "ab3") {      // Use Adams Bashforth 3rd order integration
100       IntType = eAdamsBashforth3;
101     } else {                               // Use default Adams Bashforth 2nd order integration
102       IntType = eAdamsBashforth2;
103     }
104
105     if (!is_number(ki_string)) { // property
106       if (ki_string[0] == '-') {
107        KiPropertySign = -1.0;
108        ki_string.erase(0,1);
109       }
110       KiPropertyNode = PropertyManager->GetNode(ki_string);
111     } else {
112       Ki = element->FindElementValueAsNumber("ki");
113     }
114   }
115
116   if ( element->FindElement("kd") ) {
117     kd_string = element->FindElementValue("kd");
118     if (!is_number(kd_string)) { // property
119       if (kd_string[0] == '-') {
120        KdPropertySign = -1.0;
121        kd_string.erase(0,1);
122       }
123       KdPropertyNode = PropertyManager->GetNode(kd_string);
124     } else {
125       Kd = element->FindElementValueAsNumber("kd");
126     }
127   }
128
129   if (element->FindElement("pvdot")) {
130     ProcessVariableDot =  PropertyManager->GetNode(element->FindElementValue("pvdot"));
131   }
132
133   if (element->FindElement("trigger")) {
134     Trigger =  PropertyManager->GetNode(element->FindElementValue("trigger"));
135   }
136
137   FGFCSComponent::bind();
138
139   Debug(0);
140 }
141
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143
144 FGPID::~FGPID()
145 {
146   Debug(1);
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 bool FGPID::Run(void )
152 {
153   double I_out_delta = 0.0;
154   double Dval = 0;
155
156   Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
157
158   if (KpPropertyNode != 0) Kp = KpPropertyNode->getDoubleValue() * KpPropertySign;
159   if (KiPropertyNode != 0) Ki = KiPropertyNode->getDoubleValue() * KiPropertySign;
160   if (KdPropertyNode != 0) Kd = KdPropertyNode->getDoubleValue() * KdPropertySign;
161
162   if (ProcessVariableDot) {
163     Dval = ProcessVariableDot->getDoubleValue();
164   } else {
165     Dval = (Input - Input_prev)/dt;
166   }
167
168   // Do not continue to integrate the input to the integrator if a wind-up
169   // condition is sensed - that is, if the property pointed to by the trigger
170   // element is non-zero. Reset the integrator to 0.0 if the Trigger value
171   // is negative.
172
173   double test = 0.0;
174   if (Trigger != 0) test = Trigger->getDoubleValue();
175
176   if (fabs(test) < 0.000001) {
177     switch(IntType) {
178     case eRectEuler:
179       I_out_delta = Ki * dt * Input;                         // Normal rectangular integrator
180       break;
181     case eTrapezoidal:
182       I_out_delta = (Ki/2.0) * dt * (Input + Input_prev);    // Trapezoidal integrator
183       break;
184     case eAdamsBashforth2:
185       I_out_delta = Ki * dt * (1.5*Input - 0.5*Input_prev);  // 2nd order Adams Bashforth integrator
186       break;
187     case eAdamsBashforth3:                                   // 3rd order Adams Bashforth integrator
188       I_out_delta = (Ki/12.0) * dt * (23.0*Input - 16.0*Input_prev + 5.0*Input_prev2);
189       break;
190     case eNone:
191       // No integator is defined or used.
192       I_out_delta = 0.0;
193       break;
194     }
195   }
196
197   if (test < 0.0) I_out_total = 0.0;  // Reset integrator to 0.0
198   
199   I_out_total += I_out_delta;
200
201   if (IsStandard) {
202     Output = Kp * (Input + I_out_total + Kd*Dval);
203   } else {
204     Output = Kp*Input + I_out_total + Kd*Dval;
205   }
206
207   Input_prev = Input;
208   Input_prev2 = Input_prev;
209
210   Clip();
211   if (IsOutput) SetOutput();
212
213   return true;
214 }
215
216 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 //    The bitmasked value choices are as follows:
218 //    unset: In this case (the default) JSBSim would only print
219 //       out the normally expected messages, essentially echoing
220 //       the config files as they are read. If the environment
221 //       variable is not set, debug_lvl is set to 1 internally
222 //    0: This requests JSBSim not to output any messages
223 //       whatsoever.
224 //    1: This value explicity requests the normal JSBSim
225 //       startup messages
226 //    2: This value asks for a message to be printed out when
227 //       a class is instantiated
228 //    4: When this value is set, a message is displayed when a
229 //       FGModel object executes its Run() method
230 //    8: When this value is set, various runtime state variables
231 //       are printed out periodically
232 //    16: When set various parameters are sanity checked and
233 //       a message is printed out when they go out of bounds
234
235 void FGPID::Debug(int from)
236 {
237   if (debug_lvl <= 0) return;
238
239   if (debug_lvl & 1) { // Standard console startup message output
240     if (from == 0) { // Constructor
241       if (InputSigns[0] < 0)
242         cout << "      INPUT: -" << InputNodes[0]->GetName() << endl;
243       else
244         cout << "      INPUT: " << InputNodes[0]->GetName() << endl;
245
246       if (IsOutput) {
247         for (unsigned int i=0; i<OutputNodes.size(); i++)
248           cout << "      OUTPUT: " << OutputNodes[i]->getName() << endl;
249       }
250     }
251   }
252   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
253     if (from == 0) cout << "Instantiated: FGPID" << endl;
254     if (from == 1) cout << "Destroyed:    FGPID" << endl;
255   }
256   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
257   }
258   if (debug_lvl & 8 ) { // Runtime state variables
259   }
260   if (debug_lvl & 16) { // Sanity checking
261   }
262   if (debug_lvl & 64) {
263     if (from == 0) { // Constructor
264       cout << IdSrc << endl;
265       cout << IdHdr << endl;
266     }
267   }
268 }
269 }