]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGPID.cpp
Update to the latest version of JSBSim which supports Lighter Than Air craft
[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 (jsb@hal-pc.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
40 namespace JSBSim {
41
42 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_PID;
44
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 CLASS IMPLEMENTATION
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48
49 FGPID::FGPID(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
50 {
51   string kp_string, ki_string, kd_string;
52   dt = fcs->GetState()->Getdt();
53
54   Kp = Ki = Kd = 0.0;
55   KpPropertyNode = 0;
56   KiPropertyNode = 0;
57   KdPropertyNode = 0;
58   KpPropertySign = 1.0;
59   KiPropertySign = 1.0;
60   KdPropertySign = 1.0;
61   I_out_total = 0.0;
62   Input_prev = Input_prev2 = 0.0;
63   Trigger = 0;
64
65   if ( element->FindElement("kp") ) {
66     kp_string = element->FindElementValue("kp");
67     if (kp_string.find_first_not_of("+-.0123456789Ee") != string::npos) { // property
68       if (kp_string[0] == '-') {
69        KpPropertySign = -1.0;
70        kp_string.erase(0,1);
71       }
72       KpPropertyNode = PropertyManager->GetNode(kp_string);
73     } else {
74       Kp = element->FindElementValueAsNumber("kp");
75     }
76   }
77
78   if ( element->FindElement("ki") ) {
79     ki_string = element->FindElementValue("ki");
80     if (ki_string.find_first_not_of("+-.0123456789Ee") != string::npos) { // property
81       if (ki_string[0] == '-') {
82        KiPropertySign = -1.0;
83        ki_string.erase(0,1);
84       }
85       KiPropertyNode = PropertyManager->GetNode(ki_string);
86     } else {
87       Ki = element->FindElementValueAsNumber("ki");
88     }
89   }
90
91   if ( element->FindElement("kd") ) {
92     kd_string = element->FindElementValue("kd");
93     if (kd_string.find_first_not_of("+-.0123456789Ee") != string::npos) { // property
94       if (kd_string[0] == '-') {
95        KdPropertySign = -1.0;
96        kd_string.erase(0,1);
97       }
98       KdPropertyNode = PropertyManager->GetNode(kd_string);
99     } else {
100       Kd = element->FindElementValueAsNumber("kd");
101     }
102   }
103
104   //if (element->FindElement("kp")) Kp = element->FindElementValueAsNumber("kp");
105   //if (element->FindElement("ki")) Ki = element->FindElementValueAsNumber("ki");
106   //if (element->FindElement("kd")) Kd = element->FindElementValueAsNumber("kd");
107   if (element->FindElement("trigger")) {
108     Trigger =  PropertyManager->GetNode(element->FindElementValue("trigger"));
109   }
110
111   FGFCSComponent::bind();
112
113   Debug(0);
114 }
115
116 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117
118 FGPID::~FGPID()
119 {
120   Debug(1);
121 }
122
123 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124
125 bool FGPID::Run(void )
126 {
127   double I_out_delta = 0.0;
128   double P_out, D_out;
129
130   Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
131
132   if (KpPropertyNode != 0) Kp = KpPropertyNode->getDoubleValue() * KpPropertySign;
133   if (KiPropertyNode != 0) Ki = KiPropertyNode->getDoubleValue() * KiPropertySign;
134   if (KdPropertyNode != 0) Kd = KdPropertyNode->getDoubleValue() * KdPropertySign;
135
136   P_out = Kp * Input;
137   D_out = (Kd / dt) * (Input - Input_prev);
138
139   // Do not continue to integrate the input to the integrator if a wind-up
140   // condition is sensed - that is, if the property pointed to by the trigger
141   // element is non-zero. Reset the integrator to 0.0 if the Trigger value
142   // is negative.
143
144   if (Trigger != 0) {
145     double test = Trigger->getDoubleValue();
146     if (fabs(test) < 0.000001) I_out_delta = Ki * dt * Input;  // Normal
147     if (test < 0.0)            I_out_total = 0.0;  // Reset integrator to 0.0
148   } else { // no anti-wind-up trigger defined
149     I_out_delta = Ki * dt * Input;
150   }
151   
152   I_out_total += I_out_delta;
153
154   Output = P_out + I_out_total + D_out;
155   
156   Input_prev = Input;
157   Input_prev2 = Input_prev;
158
159   Clip();
160   if (IsOutput) SetOutput();
161
162   return true;
163 }
164
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 //    The bitmasked value choices are as follows:
167 //    unset: In this case (the default) JSBSim would only print
168 //       out the normally expected messages, essentially echoing
169 //       the config files as they are read. If the environment
170 //       variable is not set, debug_lvl is set to 1 internally
171 //    0: This requests JSBSim not to output any messages
172 //       whatsoever.
173 //    1: This value explicity requests the normal JSBSim
174 //       startup messages
175 //    2: This value asks for a message to be printed out when
176 //       a class is instantiated
177 //    4: When this value is set, a message is displayed when a
178 //       FGModel object executes its Run() method
179 //    8: When this value is set, various runtime state variables
180 //       are printed out periodically
181 //    16: When set various parameters are sanity checked and
182 //       a message is printed out when they go out of bounds
183
184 void FGPID::Debug(int from)
185 {
186   if (debug_lvl <= 0) return;
187
188   if (debug_lvl & 1) { // Standard console startup message output
189     if (from == 0) { // Constructor
190       if (InputSigns[0] < 0)
191         cout << "      INPUT: -" << InputNodes[0]->getName() << endl;
192       else
193         cout << "      INPUT: " << InputNodes[0]->getName() << endl;
194
195       if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
196     }
197   }
198   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
199     if (from == 0) cout << "Instantiated: FGPID" << endl;
200     if (from == 1) cout << "Destroyed:    FGPID" << endl;
201   }
202   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
203   }
204   if (debug_lvl & 8 ) { // Runtime state variables
205   }
206   if (debug_lvl & 16) { // Sanity checking
207   }
208   if (debug_lvl & 64) {
209     if (from == 0) { // Constructor
210       cout << IdSrc << endl;
211       cout << IdHdr << endl;
212     }
213   }
214 }
215 }