JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
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 IDENT(IdSrc,"$Id: FGPID.cpp,v 1.24 2014/01/13 10:46:09 ehofman Exp $");
48 IDENT(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  string tmp;
139  if (Name.find("/") == string::npos) {
140  tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
141  } else {
142  tmp = Name;
143  }
144  typedef double (FGPID::*PMF)(void) const;
145  PropertyManager->Tie(tmp+"/initial-integrator-value", this, (PMF)0, &FGPID::SetInitialOutput);
146 
147  Debug(0);
148 }
149 
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 
152 FGPID::~FGPID()
153 {
154  Debug(1);
155 }
156 
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 
159 void FGPID::ResetPastStates(void)
160 {
161  FGFCSComponent::ResetPastStates();
162 
163  Input_prev = Input_prev2 = Output = I_out_total = 0.0;
164 }
165 
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 
168 bool FGPID::Run(void )
169 {
170  double I_out_delta = 0.0;
171  double Dval = 0;
172 
173  Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
174 
175  if (KpPropertyNode != 0) Kp = KpPropertyNode->getDoubleValue() * KpPropertySign;
176  if (KiPropertyNode != 0) Ki = KiPropertyNode->getDoubleValue() * KiPropertySign;
177  if (KdPropertyNode != 0) Kd = KdPropertyNode->getDoubleValue() * KdPropertySign;
178 
179  if (ProcessVariableDot) {
180  Dval = ProcessVariableDot->getDoubleValue();
181  } else {
182  Dval = (Input - Input_prev)/dt;
183  }
184 
185  // Do not continue to integrate the input to the integrator if a wind-up
186  // condition is sensed - that is, if the property pointed to by the trigger
187  // element is non-zero. Reset the integrator to 0.0 if the Trigger value
188  // is negative.
189 
190  double test = 0.0;
191  if (Trigger != 0) test = Trigger->getDoubleValue();
192 
193  if (fabs(test) < 0.000001) {
194  switch(IntType) {
195  case eRectEuler:
196  I_out_delta = Ki * dt * Input; // Normal rectangular integrator
197  break;
198  case eTrapezoidal:
199  I_out_delta = (Ki/2.0) * dt * (Input + Input_prev); // Trapezoidal integrator
200  break;
201  case eAdamsBashforth2:
202  I_out_delta = Ki * dt * (1.5*Input - 0.5*Input_prev); // 2nd order Adams Bashforth integrator
203  break;
204  case eAdamsBashforth3: // 3rd order Adams Bashforth integrator
205  I_out_delta = (Ki/12.0) * dt * (23.0*Input - 16.0*Input_prev + 5.0*Input_prev2);
206  break;
207  case eNone:
208  // No integator is defined or used.
209  I_out_delta = 0.0;
210  break;
211  }
212  }
213 
214  if (test < 0.0) I_out_total = 0.0; // Reset integrator to 0.0
215 
216  I_out_total += I_out_delta;
217 
218  if (IsStandard) {
219  Output = Kp * (Input + I_out_total + Kd*Dval);
220  } else {
221  Output = Kp*Input + I_out_total + Kd*Dval;
222  }
223 
224  Input_prev = Input;
225  Input_prev2 = Input_prev;
226 
227  Clip();
228  if (IsOutput) SetOutput();
229 
230  return true;
231 }
232 
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 // The bitmasked value choices are as follows:
235 // unset: In this case (the default) JSBSim would only print
236 // out the normally expected messages, essentially echoing
237 // the config files as they are read. If the environment
238 // variable is not set, debug_lvl is set to 1 internally
239 // 0: This requests JSBSim not to output any messages
240 // whatsoever.
241 // 1: This value explicity requests the normal JSBSim
242 // startup messages
243 // 2: This value asks for a message to be printed out when
244 // a class is instantiated
245 // 4: When this value is set, a message is displayed when a
246 // FGModel object executes its Run() method
247 // 8: When this value is set, various runtime state variables
248 // are printed out periodically
249 // 16: When set various parameters are sanity checked and
250 // a message is printed out when they go out of bounds
251 
252 void FGPID::Debug(int from)
253 {
254  if (debug_lvl <= 0) return;
255 
256  if (debug_lvl & 1) { // Standard console startup message output
257  if (from == 0) { // Constructor
258  if (InputSigns[0] < 0)
259  cout << " INPUT: -" << InputNodes[0]->GetName() << endl;
260  else
261  cout << " INPUT: " << InputNodes[0]->GetName() << endl;
262 
263  if (IsOutput) {
264  for (unsigned int i=0; i<OutputNodes.size(); i++)
265  cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
266  }
267  }
268  }
269  if (debug_lvl & 2 ) { // Instantiation/Destruction notification
270  if (from == 0) cout << "Instantiated: FGPID" << endl;
271  if (from == 1) cout << "Destroyed: FGPID" << endl;
272  }
273  if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
274  }
275  if (debug_lvl & 8 ) { // Runtime state variables
276  }
277  if (debug_lvl & 16) { // Sanity checking
278  }
279  if (debug_lvl & 64) {
280  if (from == 0) { // Constructor
281  cout << IdSrc << endl;
282  cout << IdHdr << endl;
283  }
284  }
285 }
286 }
STL namespace.