]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFCS.cpp
Latest JSBSim changes.
[flightgear.git] / src / FDM / JSBSim / FGFCS.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2  
3  Module:       FGFCS.cpp
4  Author:       Jon Berndt
5  Date started: 12/12/98
6  Purpose:      Model the flight controls
7  Called by:    FDMExec
8  
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
10  
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15  
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  details.
20  
21  You should have received a copy of the GNU General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24  
25  Further information about the GNU General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27  
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class models the flight controls for a specific airplane
31  
32 HISTORY
33 --------------------------------------------------------------------------------
34 12/12/98   JSB   Created
35  
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include "FGFCS.h"
41 #include "FGState.h"
42 #include "FGFDMExec.h"
43 #include "FGAtmosphere.h"
44 #include "FGAircraft.h"
45 #include "FGTranslation.h"
46 #include "FGRotation.h"
47 #include "FGPosition.h"
48 #include "FGAuxiliary.h"
49 #include "FGOutput.h"
50
51 #include "filtersjb/FGFilter.h"
52 #include "filtersjb/FGDeadBand.h"
53 #include "filtersjb/FGGain.h"
54 #include "filtersjb/FGGradient.h"
55 #include "filtersjb/FGSwitch.h"
56 #include "filtersjb/FGSummer.h"
57 #include "filtersjb/FGKinemat.h"
58
59 static const char *IdSrc = "$Id$";
60 static const char *IdHdr = ID_FCS;
61
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 CLASS IMPLEMENTATION
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65
66 FGFCS::FGFCS(FGFDMExec* fdmex) : FGModel(fdmex)
67 {
68   Name = "FGFCS";
69
70   DaCmd = DeCmd = DrCmd = DfCmd = DsbCmd = DspCmd = PTrimCmd = 0.0;
71   DaPos = DePos = DrPos = DfPos = DsbPos = DspPos = 0.0;
72   GearCmd = GearPos = 1; // default to gear down
73   LeftBrake = RightBrake = CenterBrake = 0.0;
74
75   Debug(0);
76 }
77
78 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79
80 FGFCS::~FGFCS()
81 {
82   ThrottleCmd.clear();
83   ThrottlePos.clear();
84   MixtureCmd.clear();
85   MixturePos.clear();
86   PropAdvanceCmd.clear();
87   PropAdvance.clear();
88
89   unsigned int i;
90
91   for (i=0;i<Components.size();i++) delete Components[i];
92   Debug(1);
93 }
94
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96
97 bool FGFCS::Run(void)
98 {
99   unsigned int i;
100
101   if (!FGModel::Run()) {
102     for (i=0; i<ThrottlePos.size(); i++) ThrottlePos[i] = ThrottleCmd[i];
103     for (i=0; i<MixturePos.size(); i++) MixturePos[i] = MixtureCmd[i];
104     for (i=0; i<PropAdvance.size(); i++) PropAdvance[i] = PropAdvanceCmd[i];
105     for (i=0; i<Components.size(); i++)  Components[i]->Run();
106   } else {
107   }
108
109   return false;
110 }
111
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114 void FGFCS::SetThrottleCmd(int engineNum, double setting)
115 {
116   unsigned int ctr;
117
118   if (engineNum < (int)ThrottlePos.size()) {
119     if (engineNum < 0) {
120       for (ctr=0;ctr<ThrottleCmd.size();ctr++) ThrottleCmd[ctr] = setting;
121     } else {
122       ThrottleCmd[engineNum] = setting;
123     }
124   } else {
125     cerr << "Throttle " << engineNum << " does not exist! " << ThrottleCmd.size()
126          << " engines exist, but attempted throttle command is for engine "
127          << engineNum << endl;
128   }
129 }
130
131 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132
133 void FGFCS::SetThrottlePos(int engineNum, double setting)
134 {
135   unsigned int ctr;
136
137   if (engineNum < (int)ThrottlePos.size()) {
138     if (engineNum < 0) {
139       for (ctr=0;ctr<ThrottlePos.size();ctr++) ThrottlePos[ctr] = setting;
140     } else {
141       ThrottlePos[engineNum] = setting;
142     }
143   } else {
144     cerr << "Throttle " << engineNum << " does not exist! " << ThrottlePos.size()
145          << " engines exist, but attempted throttle position setting is for engine "
146          << engineNum << endl;
147   }
148 }
149
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 double FGFCS::GetThrottleCmd(int engineNum)
153 {
154   if (engineNum < (int)ThrottlePos.size()) {
155     if (engineNum < 0) {
156        cerr << "Cannot get throttle value for ALL engines" << endl;
157     } else {
158       return ThrottleCmd[engineNum];
159     }
160   } else {
161     cerr << "Throttle " << engineNum << " does not exist! " << ThrottleCmd.size()
162          << " engines exist, but throttle setting for engine " << engineNum
163          << " is selected" << endl;
164   }
165   return 0.0;
166 }
167
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169
170 double FGFCS::GetThrottlePos(int engineNum)
171 {
172   if (engineNum < (int)ThrottlePos.size()) {
173     if (engineNum < 0) {
174        cerr << "Cannot get throttle value for ALL engines" << endl;
175     } else {
176       return ThrottlePos[engineNum];
177     }
178   } else {
179     cerr << "Throttle " << engineNum << " does not exist! " << ThrottlePos.size()
180          << " engines exist, but attempted throttle position setting is for engine "
181          << engineNum << endl;
182   }
183   return 0.0; 
184 }
185
186 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187
188 void FGFCS::SetMixtureCmd(int engineNum, double setting)
189 {
190   unsigned int ctr;
191
192   if (engineNum < (int)ThrottlePos.size()) {
193     if (engineNum < 0) {
194       for (ctr=0;ctr<MixtureCmd.size();ctr++) MixtureCmd[ctr] = setting;
195     } else {
196       MixtureCmd[engineNum] = setting;
197     }
198   }
199 }
200
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202
203 void FGFCS::SetMixturePos(int engineNum, double setting)
204 {
205   unsigned int ctr;
206
207   if (engineNum < (int)ThrottlePos.size()) {
208     if (engineNum < 0) {
209       for (ctr=0;ctr<=MixtureCmd.size();ctr++) MixturePos[ctr] = MixtureCmd[ctr];
210     } else {
211       MixturePos[engineNum] = setting;
212     }
213   }
214 }
215
216 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217
218 void FGFCS::SetPropAdvanceCmd(int engineNum, double setting)
219 {
220   unsigned int ctr;
221
222   if (engineNum < (int)ThrottlePos.size()) {
223     if (engineNum < 0) {
224       for (ctr=0;ctr<PropAdvanceCmd.size();ctr++) PropAdvanceCmd[ctr] = setting;
225     } else {
226       PropAdvanceCmd[engineNum] = setting;
227     }
228   }
229 }
230
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232
233 void FGFCS::SetPropAdvance(int engineNum, double setting)
234 {
235   unsigned int ctr;
236
237   if (engineNum < (int)ThrottlePos.size()) {
238     if (engineNum < 0) {
239       for (ctr=0;ctr<=PropAdvanceCmd.size();ctr++) PropAdvance[ctr] = PropAdvanceCmd[ctr];
240     } else {
241       PropAdvance[engineNum] = setting;
242     }
243   }
244 }
245
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247
248 bool FGFCS::Load(FGConfigFile* AC_cfg)
249 {
250   string token;
251
252   Name = Name + ":" + AC_cfg->GetValue("NAME");
253   if (debug_lvl > 0) cout << "    Control System Name: " << Name << endl;
254   AC_cfg->GetNextConfigLine();
255   while ((token = AC_cfg->GetValue()) != string("/FLIGHT_CONTROL")) {
256     if (token == "COMPONENT") {
257       token = AC_cfg->GetValue("TYPE");
258       if (debug_lvl > 0) cout << "    Loading Component \""
259                               << AC_cfg->GetValue("NAME")
260                               << "\" of type: " << token << endl;
261       if ((token == "LAG_FILTER") ||
262           (token == "LEAD_LAG_FILTER") ||
263           (token == "SECOND_ORDER_FILTER") ||
264           (token == "WASHOUT_FILTER") ||
265           (token == "INTEGRATOR") ) {
266         Components.push_back(new FGFilter(this, AC_cfg));
267       } else if ((token == "PURE_GAIN") ||
268                  (token == "SCHEDULED_GAIN") ||
269                  (token == "AEROSURFACE_SCALE") ) {
270
271         Components.push_back(new FGGain(this, AC_cfg));
272
273       } else if (token == "SUMMER") {
274         Components.push_back(new FGSummer(this, AC_cfg));
275       } else if (token == "DEADBAND") {
276         Components.push_back(new FGDeadBand(this, AC_cfg));
277       } else if (token == "GRADIENT") {
278         Components.push_back(new FGGradient(this, AC_cfg));
279       } else if (token == "SWITCH") {
280         Components.push_back(new FGSwitch(this, AC_cfg));
281       } else if (token == "KINEMAT") {
282         Components.push_back(new FGKinemat(this, AC_cfg));
283       } else {
284         cerr << "Unknown token [" << token << "] in FCS portion of config file" << endl;
285         return false;
286       }
287       AC_cfg->GetNextConfigLine();
288     }
289   }
290   return true;
291 }
292
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294
295 double FGFCS::GetComponentOutput(eParam idx) {
296   return Components[idx]->GetOutput();
297 }
298
299 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300
301 string FGFCS::GetComponentName(int idx) {
302   return Components[idx]->GetName();
303 }
304
305 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306
307 double FGFCS::GetBrake(FGLGear::BrakeGroup bg)
308 {
309   switch (bg) {
310   case FGLGear::bgLeft:
311     return LeftBrake;
312   case FGLGear::bgRight:
313     return RightBrake;
314   case FGLGear::bgCenter:
315     return CenterBrake;
316   default:
317     cerr << "GetBrake asked to return a bogus brake value" << endl;
318   }
319   return 0.0;
320 }
321
322 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323
324 string FGFCS::GetComponentStrings(void)
325 {
326   unsigned int comp;
327   string CompStrings = "";
328   bool firstime = true;
329
330   for (comp = 0; comp < Components.size(); comp++) {
331     if (firstime) firstime = false;
332     else          CompStrings += ", ";
333
334     CompStrings += Components[comp]->GetName();
335   }
336
337   return CompStrings;
338 }
339
340 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341
342 string FGFCS::GetComponentValues(void)
343 {
344   unsigned int comp;
345   string CompValues = "";
346   char buffer[10];
347   bool firstime = true;
348
349   for (comp = 0; comp < Components.size(); comp++) {
350     if (firstime) firstime = false;
351     else          CompValues += ", ";
352
353     sprintf(buffer, "%9.6f", Components[comp]->GetOutput());
354     CompValues += string(buffer);
355   }
356
357   return CompValues;
358 }
359
360 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361
362 void FGFCS::AddThrottle(void)
363 {
364   ThrottleCmd.push_back(0.0);
365   ThrottlePos.push_back(0.0);
366   MixtureCmd.push_back(0.0);     // assume throttle and mixture are coupled
367   MixturePos.push_back(0.0);
368   PropAdvanceCmd.push_back(0.0); // assume throttle and prop pitch are coupled
369   PropAdvance.push_back(0.0);
370 }
371
372 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 //    The bitmasked value choices are as follows:
374 //    unset: In this case (the default) JSBSim would only print
375 //       out the normally expected messages, essentially echoing
376 //       the config files as they are read. If the environment
377 //       variable is not set, debug_lvl is set to 1 internally
378 //    0: This requests JSBSim not to output any messages
379 //       whatsoever.
380 //    1: This value explicity requests the normal JSBSim
381 //       startup messages
382 //    2: This value asks for a message to be printed out when
383 //       a class is instantiated
384 //    4: When this value is set, a message is displayed when a
385 //       FGModel object executes its Run() method
386 //    8: When this value is set, various runtime state variables
387 //       are printed out periodically
388 //    16: When set various parameters are sanity checked and
389 //       a message is printed out when they go out of bounds
390
391 void FGFCS::Debug(int from)
392 {
393   if (debug_lvl <= 0) return;
394
395   if (debug_lvl & 1) { // Standard console startup message output
396     if (from == 0) { // Constructor
397
398     }
399   }
400   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
401     if (from == 0) cout << "Instantiated: FGFCS" << endl;
402     if (from == 1) cout << "Destroyed:    FGFCS" << endl;
403   }
404   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
405   }
406   if (debug_lvl & 8 ) { // Runtime state variables
407   }
408   if (debug_lvl & 16) { // Sanity checking
409   }
410 }