]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAircraft.cpp
Latest JSBSim changes.
[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   if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
111 }
112
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116
117 FGAircraft::~FGAircraft()
118 {
119   if (debug_lvl & 2) cout << "Destroyed:    FGAircraft" << endl;
120 }
121
122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123
124 bool FGAircraft::Load(FGConfigFile* AC_cfg)
125 {
126   string token;
127
128   if (!ReadPrologue(AC_cfg)) return false;
129
130   while ((AC_cfg->GetNextConfigLine() != string("EOF")) &&
131          (token = AC_cfg->GetValue()) != string("/FDM_CONFIG")) {
132     if (token == "METRICS") {
133       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Metrics" << fgdef << endl;
134       if (!ReadMetrics(AC_cfg)) return false;
135     } else if (token == "AERODYNAMICS") {
136       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Aerodynamics" << fgdef << endl;
137       if (!ReadAerodynamics(AC_cfg)) return false;
138     } else if (token == "UNDERCARRIAGE") {
139       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Landing Gear" << fgdef << endl;
140       if (!ReadUndercarriage(AC_cfg)) return false;
141     } else if (token == "PROPULSION") {
142       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Propulsion" << fgdef << endl;
143       if (!ReadPropulsion(AC_cfg)) return false;
144     } else if (token == "FLIGHT_CONTROL") {
145       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Flight Control" << fgdef << endl;
146       if (!ReadFlightControls(AC_cfg)) return false;
147     } else if (token == "OUTPUT") {
148       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Output directives" << fgdef << endl;
149       if (!ReadOutput(AC_cfg)) return false;
150     }
151   }
152   
153   return true;
154 }
155
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157
158 bool FGAircraft::Run(void)
159 {
160   if (!FGModel::Run()) {                 // if false then execute this Run()
161     vForces.InitMatrix();
162     vForces += Aerodynamics->GetForces();
163     vForces += Inertial->GetForces();
164     vForces += Propulsion->GetForces();
165     vForces += GroundReactions->GetForces();
166
167     vMoments.InitMatrix();
168     vMoments += Aerodynamics->GetMoments();
169     vMoments += Propulsion->GetMoments();
170     vMoments += GroundReactions->GetMoments();
171     
172     vBodyAccel = vForces/MassBalance->GetMass();
173     
174     vNcg = vBodyAccel/Inertial->gravity();
175
176     vNwcg = State->GetTb2s() * vNcg;
177     vNwcg(3) = -1*vNwcg(3) + 1;
178     
179     if (alphaclmax != 0) {
180       if (Translation->Getalpha() > 0.85*alphaclmax) {
181         impending_stall = 10*(Translation->Getalpha()/alphaclmax - 0.85);
182       } else {
183         impending_stall = 0;
184       }
185     }      
186     
187     return false;
188   } else {                               // skip Run() execution this time
189     return true;
190   }
191 }
192
193 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194
195 float FGAircraft::GetNlf(void)
196 {
197   return vNwcg(3);
198 }
199
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
201
202 bool FGAircraft::ReadPrologue(FGConfigFile* AC_cfg)
203 {
204   string token = AC_cfg->GetValue();
205   string scratch;
206   AircraftName = AC_cfg->GetValue("NAME");
207   if (debug_lvl > 0) cout << underon << "Reading Aircraft Configuration File"
208             << underoff << ": " << highint << AircraftName << normint << endl;
209   scratch = AC_cfg->GetValue("VERSION").c_str();
210
211   CFGVersion = AC_cfg->GetValue("VERSION");
212
213   if (debug_lvl > 0)
214     cout << "                            Version: " << highint << CFGVersion
215                                                              << normint << endl;
216   if (CFGVersion != needed_cfg_version) {
217     cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
218             " RESULTS WILL BE UNPREDICTABLE !!" << endl;
219     cerr << "Current version needed is: " << needed_cfg_version << endl;
220     cerr << "         You have version: " << CFGVersion << endl << fgdef << endl;
221     return false;
222   }
223   return true;
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
228 bool FGAircraft::ReadMetrics(FGConfigFile* AC_cfg)
229 {
230   string token = "";
231   string parameter;
232   double EW, bixx, biyy, bizz, bixy, bixz;
233   double pmWt, pmX, pmY, pmZ;
234   FGColumnVector3 vbaseXYZcg;
235
236   AC_cfg->GetNextConfigLine();
237
238   while ((token = AC_cfg->GetValue()) != string("/METRICS")) {
239     *AC_cfg >> parameter;
240     if (parameter == "AC_WINGAREA") {
241       *AC_cfg >> WingArea;
242       if (debug_lvl > 0) cout << "    WingArea: " << WingArea  << endl;
243     } else if (parameter == "AC_WINGSPAN") {
244       *AC_cfg >> WingSpan;
245       if (debug_lvl > 0) cout << "    WingSpan: " << WingSpan  << endl;
246     } else if (parameter == "AC_WINGINCIDENCE") {
247       *AC_cfg >> WingIncidence;
248       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
249     } else if (parameter == "AC_CHORD") {
250       *AC_cfg >> cbar;
251       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
252     } else if (parameter == "AC_HTAILAREA") {
253       *AC_cfg >> HTailArea;
254       if (debug_lvl > 0) cout << "    H. Tail Area: " << HTailArea << endl;
255     } else if (parameter == "AC_HTAILARM") {
256       *AC_cfg >> HTailArm;
257       if (debug_lvl > 0) cout << "    H. Tail Arm: " << HTailArm << endl;
258     } else if (parameter == "AC_VTAILAREA") {
259       *AC_cfg >> VTailArea;
260       if (debug_lvl > 0) cout << "    V. Tail Area: " << VTailArea << endl;
261     } else if (parameter == "AC_VTAILARM") {
262       *AC_cfg >> VTailArm;
263       if (debug_lvl > 0) cout << "    V. Tail Arm: " << VTailArm << endl;
264     } else if (parameter == "AC_IXX") {
265       *AC_cfg >> bixx;
266       if (debug_lvl > 0) cout << "    baseIxx: " << bixx << endl;
267       MassBalance->SetBaseIxx(bixx);
268     } else if (parameter == "AC_IYY") {
269       *AC_cfg >> biyy;
270       if (debug_lvl > 0) cout << "    baseIyy: " << biyy << endl;
271       MassBalance->SetBaseIyy(biyy);
272     } else if (parameter == "AC_IZZ") {
273       *AC_cfg >> bizz;
274       if (debug_lvl > 0) cout << "    baseIzz: " << bizz << endl;
275       MassBalance->SetBaseIzz(bizz);
276     } else if (parameter == "AC_IXY") {
277       *AC_cfg >> bixy;
278       if (debug_lvl > 0) cout << "    baseIxy: " << bixy  << endl;
279       MassBalance->SetBaseIxy(bixy);
280     } else if (parameter == "AC_IXZ") {
281       *AC_cfg >> bixz;
282       if (debug_lvl > 0) cout << "    baseIxz: " << bixz  << endl;
283       MassBalance->SetBaseIxz(bixz);
284     } else if (parameter == "AC_EMPTYWT") {
285       *AC_cfg >> EW;
286       MassBalance->SetEmptyWeight(EW);
287       if (debug_lvl > 0) cout << "    EmptyWeight: " << EW  << endl;
288     } else if (parameter == "AC_CGLOC") {
289       *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
290       MassBalance->SetBaseCG(vbaseXYZcg);
291       if (debug_lvl > 0) cout << "    CG (x, y, z): " << vbaseXYZcg << endl;
292     } else if (parameter == "AC_EYEPTLOC") {
293       *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
294       if (debug_lvl > 0) cout << "    Eyepoint (x, y, z): " << vXYZep << endl;
295     } else if (parameter == "AC_AERORP") {
296       *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
297       if (debug_lvl > 0) cout << "    Ref Pt (x, y, z): " << vXYZrp << endl;
298     } else if (parameter == "AC_ALPHALIMITS") {
299       *AC_cfg >> alphaclmin >> alphaclmax;
300       if (debug_lvl > 0) cout << "    Maximum Alpha: " << alphaclmax
301              << "    Minimum Alpha: " << alphaclmin
302              << endl;
303     } else if (parameter == "AC_POINTMASS") {
304       *AC_cfg >> pmWt >> pmX >> pmY >> pmZ;
305       if (debug_lvl > 0) cout << "    Point Mass Object: " << pmWt << " lbs. at "
306                          << "X, Y, Z (in.): " << pmX << "  " << pmY << "  " << pmZ
307                          << endl;
308     }
309   }
310   
311   // calculate some derived parameters
312   if (cbar != 0.0) {
313     lbarh = HTailArm/cbar;
314     lbarv = VTailArm/cbar;
315     if (WingArea != 0.0) {
316       vbarh = HTailArm*HTailArea / (cbar*WingArea);
317       vbarv = VTailArm*VTailArea / (cbar*WingArea);
318     }
319   }     
320   return true;
321 }
322
323 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324
325 bool FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg)
326 {
327   if (!Propulsion->Load(AC_cfg)) {
328     cerr << "  Propulsion not successfully loaded" << endl;
329     return false;
330   }
331   return true;
332 }
333
334 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335
336 bool FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg)
337 {
338   if (!FCS->Load(AC_cfg)) {
339     cerr << "  Flight Controls not successfully loaded" << endl;
340     return false;
341   }
342   return true;
343 }
344
345 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346
347 bool FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg)
348 {
349   if (!Aerodynamics->Load(AC_cfg)) {
350     cerr << "  Aerodynamics not successfully loaded" << endl;
351     return false;
352   }
353   return true;
354 }
355
356 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357
358 bool FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg)
359 {
360   if (!GroundReactions->Load(AC_cfg)) {
361     cerr << "  Ground Reactions not successfully loaded" << endl;
362     return false;
363   }
364   return true;
365 }
366
367 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368
369 bool FGAircraft::ReadOutput(FGConfigFile* AC_cfg)
370 {
371   if (!Output->Load(AC_cfg)) {
372     cerr << "  Output not successfully loaded" << endl;
373     return false;
374   }
375   return true;
376 }
377
378 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379
380 void FGAircraft::Debug(int from)
381 {
382     //TODO: Add your source code here
383 }
384