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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_PID;
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 FGPID::FGPID(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
51 string kp_string, ki_string, kd_string;
61 Input_prev = Input_prev2 = 0.0;
64 if ( element->FindElement("kp") ) {
65 kp_string = element->FindElementValue("kp");
66 if (!is_number(kp_string)) { // property
67 if (kp_string[0] == '-') {
68 KpPropertySign = -1.0;
71 KpPropertyNode = PropertyManager->GetNode(kp_string);
73 Kp = element->FindElementValueAsNumber("kp");
77 if ( element->FindElement("ki") ) {
78 ki_string = element->FindElementValue("ki");
79 if (!is_number(ki_string)) { // property
80 if (ki_string[0] == '-') {
81 KiPropertySign = -1.0;
84 KiPropertyNode = PropertyManager->GetNode(ki_string);
86 Ki = element->FindElementValueAsNumber("ki");
90 if ( element->FindElement("kd") ) {
91 kd_string = element->FindElementValue("kd");
92 if (!is_number(kd_string)) { // property
93 if (kd_string[0] == '-') {
94 KdPropertySign = -1.0;
97 KdPropertyNode = PropertyManager->GetNode(kd_string);
99 Kd = element->FindElementValueAsNumber("kd");
103 if (element->FindElement("trigger")) {
104 Trigger = PropertyManager->GetNode(element->FindElementValue("trigger"));
107 FGFCSComponent::bind();
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 bool FGPID::Run(void )
123 double I_out_delta = 0.0;
126 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
128 if (KpPropertyNode != 0) Kp = KpPropertyNode->getDoubleValue() * KpPropertySign;
129 if (KiPropertyNode != 0) Ki = KiPropertyNode->getDoubleValue() * KiPropertySign;
130 if (KdPropertyNode != 0) Kd = KdPropertyNode->getDoubleValue() * KdPropertySign;
133 D_out = (Kd / dt) * (Input - Input_prev);
135 // Do not continue to integrate the input to the integrator if a wind-up
136 // condition is sensed - that is, if the property pointed to by the trigger
137 // element is non-zero. Reset the integrator to 0.0 if the Trigger value
141 double test = Trigger->getDoubleValue();
142 if (fabs(test) < 0.000001) I_out_delta = Ki * dt * Input; // Normal
143 if (test < 0.0) I_out_total = 0.0; // Reset integrator to 0.0
144 } else { // no anti-wind-up trigger defined
145 I_out_delta = Ki * dt * Input;
148 I_out_total += I_out_delta;
150 Output = P_out + I_out_total + D_out;
153 Input_prev2 = Input_prev;
156 if (IsOutput) SetOutput();
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 // The bitmasked value choices are as follows:
163 // unset: In this case (the default) JSBSim would only print
164 // out the normally expected messages, essentially echoing
165 // the config files as they are read. If the environment
166 // variable is not set, debug_lvl is set to 1 internally
167 // 0: This requests JSBSim not to output any messages
169 // 1: This value explicity requests the normal JSBSim
171 // 2: This value asks for a message to be printed out when
172 // a class is instantiated
173 // 4: When this value is set, a message is displayed when a
174 // FGModel object executes its Run() method
175 // 8: When this value is set, various runtime state variables
176 // are printed out periodically
177 // 16: When set various parameters are sanity checked and
178 // a message is printed out when they go out of bounds
180 void FGPID::Debug(int from)
182 if (debug_lvl <= 0) return;
184 if (debug_lvl & 1) { // Standard console startup message output
185 if (from == 0) { // Constructor
186 if (InputSigns[0] < 0)
187 cout << " INPUT: -" << InputNodes[0]->getName() << endl;
189 cout << " INPUT: " << InputNodes[0]->getName() << endl;
192 for (unsigned int i=0; i<OutputNodes.size(); i++)
193 cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
197 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
198 if (from == 0) cout << "Instantiated: FGPID" << endl;
199 if (from == 1) cout << "Destroyed: FGPID" << endl;
201 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
203 if (debug_lvl & 8 ) { // Runtime state variables
205 if (debug_lvl & 16) { // Sanity checking
207 if (debug_lvl & 64) {
208 if (from == 0) { // Constructor
209 cout << IdSrc << endl;
210 cout << IdHdr << endl;