1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 6/17/2006
7 ------------- Copyright (C) 2006 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
27 --------------------------------------------------------------------------------
28 Initial code 6/17/2006 JSB
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 COMMENTS, REFERENCES, and NOTES
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 #include "input_output/FGXMLElement.h"
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;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 FGPID::FGPID(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
56 string kp_string, ki_string, kd_string;
66 Input_prev = Input_prev2 = 0.0;
68 ProcessVariableDot = 0;
70 IntType = eNone; // No integrator initially defined.
72 string pid_type = element->GetAttributeValue("type");
74 if (pid_type == "standard") IsStandard = true;
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;
83 KpPropertyNode = PropertyManager->GetNode(kp_string);
85 Kp = element->FindElementValueAsNumber("kp");
89 if ( element->FindElement("ki") ) {
90 ki_string = element->FindElementValue("ki");
92 string integ_type = element->FindElement("ki")->GetAttributeValue("type");
93 if (integ_type == "rect") { // Use rectangular integration
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;
105 if (!is_number(ki_string)) { // property
106 if (ki_string[0] == '-') {
107 KiPropertySign = -1.0;
108 ki_string.erase(0,1);
110 KiPropertyNode = PropertyManager->GetNode(ki_string);
112 Ki = element->FindElementValueAsNumber("ki");
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);
123 KdPropertyNode = PropertyManager->GetNode(kd_string);
125 Kd = element->FindElementValueAsNumber("kd");
129 if (element->FindElement("pvdot")) {
130 ProcessVariableDot = PropertyManager->GetNode(element->FindElementValue("pvdot"));
133 if (element->FindElement("trigger")) {
134 Trigger = PropertyManager->GetNode(element->FindElementValue("trigger"));
137 FGFCSComponent::bind();
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 bool FGPID::Run(void )
153 double I_out_delta = 0.0;
156 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
158 if (KpPropertyNode != 0) Kp = KpPropertyNode->getDoubleValue() * KpPropertySign;
159 if (KiPropertyNode != 0) Ki = KiPropertyNode->getDoubleValue() * KiPropertySign;
160 if (KdPropertyNode != 0) Kd = KdPropertyNode->getDoubleValue() * KdPropertySign;
162 if (ProcessVariableDot) {
163 Dval = ProcessVariableDot->getDoubleValue();
165 Dval = (Input - Input_prev)/dt;
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
174 if (Trigger != 0) test = Trigger->getDoubleValue();
176 if (fabs(test) < 0.000001) {
179 I_out_delta = Ki * dt * Input; // Normal rectangular integrator
182 I_out_delta = (Ki/2.0) * dt * (Input + Input_prev); // Trapezoidal integrator
184 case eAdamsBashforth2:
185 I_out_delta = Ki * dt * (1.5*Input - 0.5*Input_prev); // 2nd order Adams Bashforth integrator
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);
191 // No integator is defined or used.
197 if (test < 0.0) I_out_total = 0.0; // Reset integrator to 0.0
199 I_out_total += I_out_delta;
202 Output = Kp * (Input + I_out_total + Kd*Dval);
204 Output = Kp*Input + I_out_total + Kd*Dval;
208 Input_prev2 = Input_prev;
211 if (IsOutput) SetOutput();
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
224 // 1: This value explicity requests the normal JSBSim
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
235 void FGPID::Debug(int from)
237 if (debug_lvl <= 0) return;
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;
244 cout << " INPUT: " << InputNodes[0]->GetName() << endl;
247 for (unsigned int i=0; i<OutputNodes.size(); i++)
248 cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
252 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
253 if (from == 0) cout << "Instantiated: FGPID" << endl;
254 if (from == 1) cout << "Destroyed: FGPID" << endl;
256 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
258 if (debug_lvl & 8 ) { // Runtime state variables
260 if (debug_lvl & 16) { // Sanity checking
262 if (debug_lvl & 64) {
263 if (from == 0) { // Constructor
264 cout << IdSrc << endl;
265 cout << IdHdr << endl;