]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAircraft.cpp
5f0573b4f278f17fdd2ff721585519105c487755
[flightgear.git] / src / FDM / JSBSim / FGAircraft.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2  
3  Module:       FGAircraft.cpp
4  Author:       Jon S. Berndt
5  Date started: 12/12/98                                   
6  Purpose:      Encapsulates an aircraft
7  Called by:    FGFDMExec
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 Models the aircraft reactions and forces. This class is instantiated by the
31 FGFDMExec class and scheduled as an FDM entry. 
32  
33 HISTORY
34 --------------------------------------------------------------------------------
35 12/12/98   JSB   Created
36 04/03/99   JSB   Changed Aero() method to correct body axis force calculation
37                  from wind vector. Fix provided by Tony Peden.
38 05/03/99   JSB   Changed (for the better?) the way configurations are read in.
39 9/17/99     TP   Combined force and moment functions. Added aero reference 
40                  point to config file. Added calculations for moments due to 
41                  difference in cg and aero reference point
42  
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 COMMENTS, REFERENCES,  and NOTES
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46  
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 INCLUDES
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 #include <sys/stat.h>
52 #include <sys/types.h>
53
54 #ifdef FGFS
55 #  ifndef __BORLANDC__
56 #    include <simgear/compiler.h>
57 #  endif
58 #  ifdef SG_HAVE_STD_INCLUDES
59 #    include <cmath>
60 #  else
61 #    include <math.h>
62 #  endif
63 #else
64 #  if defined (sgi) && !defined(__GNUC__)
65 #    include <math.h>
66 #  else
67 #    include <cmath>
68 #  endif
69 #endif
70
71 #include "FGAircraft.h"
72 #include "FGMassBalance.h"
73 #include "FGInertial.h"
74 #include "FGGroundReactions.h"
75 #include "FGAerodynamics.h"
76 #include "FGTranslation.h"
77 #include "FGRotation.h"
78 #include "FGAtmosphere.h"
79 #include "FGState.h"
80 #include "FGFDMExec.h"
81 #include "FGFCS.h"
82 #include "FGPosition.h"
83 #include "FGAuxiliary.h"
84 #include "FGOutput.h"
85
86 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 DEFINITIONS
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
89
90 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 GLOBAL DATA
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
93
94 static const char *IdSrc = "$Id$";
95 static const char *IdHdr = ID_AIRCRAFT;
96
97 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 CLASS IMPLEMENTATION
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
100
101 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex)
102 {
103   Name = "FGAircraft";
104   alphaclmin = alphaclmax = 0;
105   HTailArea = VTailArea = HTailArm = VTailArm = 0.0;
106   lbarh = lbarv = vbarh = vbarv = 0.0;
107   WingIncidence=0;
108   impending_stall = 0;
109
110   Debug(0);
111 }
112
113 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114
115 FGAircraft::~FGAircraft()
116 {
117   Debug(1);
118 }
119
120 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121
122 bool FGAircraft::Load(FGConfigFile* AC_cfg)
123 {
124   string token;
125
126   if (!ReadPrologue(AC_cfg)) return false;
127
128   while ((AC_cfg->GetNextConfigLine() != string("EOF")) &&
129          (token = AC_cfg->GetValue()) != string("/FDM_CONFIG")) {
130     if (token == "METRICS") {
131       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Metrics" << fgdef << endl;
132       if (!ReadMetrics(AC_cfg)) return false;
133     } else if (token == "AERODYNAMICS") {
134       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Aerodynamics" << fgdef << endl;
135       if (!ReadAerodynamics(AC_cfg)) return false;
136     } else if (token == "UNDERCARRIAGE") {
137       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Landing Gear" << fgdef << endl;
138       if (!ReadUndercarriage(AC_cfg)) return false;
139     } else if (token == "PROPULSION") {
140       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Propulsion" << fgdef << endl;
141       if (!ReadPropulsion(AC_cfg)) return false;
142     } else if (token == "FLIGHT_CONTROL") {
143       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Flight Control" << fgdef << endl;
144       if (!ReadFlightControls(AC_cfg)) return false;
145     } else if (token == "OUTPUT") {
146       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Output directives" << fgdef << endl;
147       if (!ReadOutput(AC_cfg)) return false;
148     }
149   }
150   
151   return true;
152 }
153
154 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156 bool FGAircraft::Run(void)
157 {
158   if (!FGModel::Run()) {                 // if false then execute this Run()
159     vForces.InitMatrix();
160     vForces += Aerodynamics->GetForces();
161     vForces += Inertial->GetForces();
162     vForces += Propulsion->GetForces();
163     vForces += GroundReactions->GetForces();
164
165     vMoments.InitMatrix();
166     vMoments += Aerodynamics->GetMoments();
167     vMoments += Propulsion->GetMoments();
168     vMoments += GroundReactions->GetMoments();
169     
170     vBodyAccel = vForces/MassBalance->GetMass();
171     
172     vNcg = vBodyAccel/Inertial->gravity();
173
174     vNwcg = State->GetTb2s() * vNcg;
175     vNwcg(3) = -1*vNwcg(3) + 1;
176     
177     if (alphaclmax != 0) {
178       if (Translation->Getalpha() > 0.85*alphaclmax) {
179         impending_stall = 10*(Translation->Getalpha()/alphaclmax - 0.85);
180       } else {
181         impending_stall = 0;
182       }
183     }      
184     
185     return false;
186   } else {                               // skip Run() execution this time
187     return true;
188   }
189 }
190
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192
193 float FGAircraft::GetNlf(void)
194 {
195   return vNwcg(3);
196 }
197
198 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199
200 bool FGAircraft::ReadPrologue(FGConfigFile* AC_cfg)
201 {
202   string token = AC_cfg->GetValue();
203   string scratch;
204   AircraftName = AC_cfg->GetValue("NAME");
205   if (debug_lvl > 0) cout << underon << "Reading Aircraft Configuration File"
206             << underoff << ": " << highint << AircraftName << normint << endl;
207   scratch = AC_cfg->GetValue("VERSION").c_str();
208
209   CFGVersion = AC_cfg->GetValue("VERSION");
210
211   if (debug_lvl > 0)
212     cout << "                            Version: " << highint << CFGVersion
213                                                              << normint << endl;
214   if (CFGVersion != needed_cfg_version) {
215     cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
216             " RESULTS WILL BE UNPREDICTABLE !!" << endl;
217     cerr << "Current version needed is: " << needed_cfg_version << endl;
218     cerr << "         You have version: " << CFGVersion << endl << fgdef << endl;
219     return false;
220   }
221   return true;
222 }
223
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225
226 bool FGAircraft::ReadMetrics(FGConfigFile* AC_cfg)
227 {
228   string token = "";
229   string parameter;
230   double EW, bixx, biyy, bizz, bixy, bixz;
231   double pmWt, pmX, pmY, pmZ;
232   FGColumnVector3 vbaseXYZcg;
233
234   AC_cfg->GetNextConfigLine();
235
236   while ((token = AC_cfg->GetValue()) != string("/METRICS")) {
237     *AC_cfg >> parameter;
238     if (parameter == "AC_WINGAREA") {
239       *AC_cfg >> WingArea;
240       if (debug_lvl > 0) cout << "    WingArea: " << WingArea  << endl;
241     } else if (parameter == "AC_WINGSPAN") {
242       *AC_cfg >> WingSpan;
243       if (debug_lvl > 0) cout << "    WingSpan: " << WingSpan  << endl;
244     } else if (parameter == "AC_WINGINCIDENCE") {
245       *AC_cfg >> WingIncidence;
246       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
247     } else if (parameter == "AC_CHORD") {
248       *AC_cfg >> cbar;
249       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
250     } else if (parameter == "AC_HTAILAREA") {
251       *AC_cfg >> HTailArea;
252       if (debug_lvl > 0) cout << "    H. Tail Area: " << HTailArea << endl;
253     } else if (parameter == "AC_HTAILARM") {
254       *AC_cfg >> HTailArm;
255       if (debug_lvl > 0) cout << "    H. Tail Arm: " << HTailArm << endl;
256     } else if (parameter == "AC_VTAILAREA") {
257       *AC_cfg >> VTailArea;
258       if (debug_lvl > 0) cout << "    V. Tail Area: " << VTailArea << endl;
259     } else if (parameter == "AC_VTAILARM") {
260       *AC_cfg >> VTailArm;
261       if (debug_lvl > 0) cout << "    V. Tail Arm: " << VTailArm << endl;
262     } else if (parameter == "AC_IXX") {
263       *AC_cfg >> bixx;
264       if (debug_lvl > 0) cout << "    baseIxx: " << bixx << endl;
265       MassBalance->SetBaseIxx(bixx);
266     } else if (parameter == "AC_IYY") {
267       *AC_cfg >> biyy;
268       if (debug_lvl > 0) cout << "    baseIyy: " << biyy << endl;
269       MassBalance->SetBaseIyy(biyy);
270     } else if (parameter == "AC_IZZ") {
271       *AC_cfg >> bizz;
272       if (debug_lvl > 0) cout << "    baseIzz: " << bizz << endl;
273       MassBalance->SetBaseIzz(bizz);
274     } else if (parameter == "AC_IXY") {
275       *AC_cfg >> bixy;
276       if (debug_lvl > 0) cout << "    baseIxy: " << bixy  << endl;
277       MassBalance->SetBaseIxy(bixy);
278     } else if (parameter == "AC_IXZ") {
279       *AC_cfg >> bixz;
280       if (debug_lvl > 0) cout << "    baseIxz: " << bixz  << endl;
281       MassBalance->SetBaseIxz(bixz);
282     } else if (parameter == "AC_EMPTYWT") {
283       *AC_cfg >> EW;
284       MassBalance->SetEmptyWeight(EW);
285       if (debug_lvl > 0) cout << "    EmptyWeight: " << EW  << endl;
286     } else if (parameter == "AC_CGLOC") {
287       *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
288       MassBalance->SetBaseCG(vbaseXYZcg);
289       if (debug_lvl > 0) cout << "    CG (x, y, z): " << vbaseXYZcg << endl;
290     } else if (parameter == "AC_EYEPTLOC") {
291       *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
292       if (debug_lvl > 0) cout << "    Eyepoint (x, y, z): " << vXYZep << endl;
293     } else if (parameter == "AC_AERORP") {
294       *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
295       if (debug_lvl > 0) cout << "    Ref Pt (x, y, z): " << vXYZrp << endl;
296     } else if (parameter == "AC_ALPHALIMITS") {
297       *AC_cfg >> alphaclmin >> alphaclmax;
298       if (debug_lvl > 0) cout << "    Maximum Alpha: " << alphaclmax
299              << "    Minimum Alpha: " << alphaclmin
300              << endl;
301     } else if (parameter == "AC_POINTMASS") {
302       *AC_cfg >> pmWt >> pmX >> pmY >> pmZ;
303       if (debug_lvl > 0) cout << "    Point Mass Object: " << pmWt << " lbs. at "
304                          << "X, Y, Z (in.): " << pmX << "  " << pmY << "  " << pmZ
305                          << endl;
306     }
307   }
308   
309   // calculate some derived parameters
310   if (cbar != 0.0) {
311     lbarh = HTailArm/cbar;
312     lbarv = VTailArm/cbar;
313     if (WingArea != 0.0) {
314       vbarh = HTailArm*HTailArea / (cbar*WingArea);
315       vbarv = VTailArm*VTailArea / (cbar*WingArea);
316     }
317   }     
318   return true;
319 }
320
321 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322
323 bool FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg)
324 {
325   if (!Propulsion->Load(AC_cfg)) {
326     cerr << "  Propulsion not successfully loaded" << endl;
327     return false;
328   }
329   return true;
330 }
331
332 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333
334 bool FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg)
335 {
336   if (!FCS->Load(AC_cfg)) {
337     cerr << "  Flight Controls not successfully loaded" << endl;
338     return false;
339   }
340   return true;
341 }
342
343 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344
345 bool FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg)
346 {
347   if (!Aerodynamics->Load(AC_cfg)) {
348     cerr << "  Aerodynamics not successfully loaded" << endl;
349     return false;
350   }
351   return true;
352 }
353
354 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355
356 bool FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg)
357 {
358   if (!GroundReactions->Load(AC_cfg)) {
359     cerr << "  Ground Reactions not successfully loaded" << endl;
360     return false;
361   }
362   return true;
363 }
364
365 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366
367 bool FGAircraft::ReadOutput(FGConfigFile* AC_cfg)
368 {
369   if (!Output->Load(AC_cfg)) {
370     cerr << "  Output not successfully loaded" << endl;
371     return false;
372   }
373   return true;
374 }
375
376 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 //    The bitmasked value choices are as follows:
378 //    unset: In this case (the default) JSBSim would only print
379 //       out the normally expected messages, essentially echoing
380 //       the config files as they are read. If the environment
381 //       variable is not set, debug_lvl is set to 1 internally
382 //    0: This requests JSBSim not to output any messages
383 //       whatsoever.
384 //    1: This value explicity requests the normal JSBSim
385 //       startup messages
386 //    2: This value asks for a message to be printed out when
387 //       a class is instantiated
388 //    4: When this value is set, a message is displayed when a
389 //       FGModel object executes its Run() method
390 //    8: When this value is set, various runtime state variables
391 //       are printed out periodically
392 //    16: When set various parameters are sanity checked and
393 //       a message is printed out when they go out of bounds
394
395 void FGAircraft::Debug(int from)
396 {
397   if (debug_lvl <= 0) return;
398
399   if (debug_lvl & 1) { // Standard console startup message output
400     if (from == 0) { // Constructor
401     }
402   }
403   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
404     if (from == 0) cout << "Instantiated: FGAircraft" << endl;
405     if (from == 1) cout << "Destroyed:    FGAircraft" << endl;
406   }
407   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
408   }
409   if (debug_lvl & 8 ) { // Runtime state variables
410   }
411   if (debug_lvl & 16) { // Sanity checking
412   }
413   if (debug_lvl & 32) { // Turbulence
414   }
415 }
416