]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAircraft.cpp
Syncing with the very latest JSBSim development code.
[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     vMoments(3),
103     vForces(3),
104     vXYZrp(3),
105     vXYZep(3),
106     vDXYZcg(3),
107     vBodyAccel(3)
108 {
109   Name = "FGAircraft";
110   GearUp = false;
111   alphaclmin = alphaclmax = 0;
112   HTailArea = VTailArea = HTailArm = VTailArm = 0.0;
113   lbarh = lbarv = vbarh = vbarv = 0.0;
114   WingIncidence=0;
115
116   if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
117 }
118
119
120 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121
122
123 FGAircraft::~FGAircraft()
124 {
125   if (debug_lvl & 2) cout << "Destroyed:    FGAircraft" << endl;
126 }
127
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129
130 bool FGAircraft::Load(FGConfigFile* AC_cfg)
131 {
132   string token;
133
134   ReadPrologue(AC_cfg);
135
136   while ((AC_cfg->GetNextConfigLine() != "EOF") &&
137          (token = AC_cfg->GetValue()) != "/FDM_CONFIG") {
138     if (token == "METRICS") {
139       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Metrics" << fgdef << endl;
140       ReadMetrics(AC_cfg);
141     } else if (token == "AERODYNAMICS") {
142       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Aerodynamics" << fgdef << endl;
143       ReadAerodynamics(AC_cfg);
144     } else if (token == "UNDERCARRIAGE") {
145       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Landing Gear" << fgdef << endl;
146       ReadUndercarriage(AC_cfg);
147     } else if (token == "PROPULSION") {
148       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Propulsion" << fgdef << endl;
149       ReadPropulsion(AC_cfg);
150     } else if (token == "FLIGHT_CONTROL") {
151       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Flight Control" << fgdef << endl;
152       ReadFlightControls(AC_cfg);
153     } else if (token == "OUTPUT") {
154       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Output directives" << fgdef << endl;
155       ReadOutput(AC_cfg);
156     }
157   }
158   
159   return true;
160 }
161
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163
164 bool FGAircraft::Run(void)
165 {
166   if (!FGModel::Run()) {                 // if false then execute this Run()
167     vForces.InitMatrix();
168     vForces += Aerodynamics->GetForces();
169     vForces += Inertial->GetForces();
170     vForces += Propulsion->GetForces();
171     vForces += GroundReactions->GetForces();
172
173     vMoments.InitMatrix();
174     vMoments += Aerodynamics->GetMoments();
175     vMoments += Propulsion->GetMoments();
176     vMoments += GroundReactions->GetMoments();
177     
178     vBodyAccel = vForces/MassBalance->GetMass();
179     
180     return false;
181   } else {                               // skip Run() execution this time
182     return true;
183   }
184 }
185
186 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187
188 void FGAircraft::ReadMetrics(FGConfigFile* AC_cfg)
189 {
190   string token = "";
191   string parameter;
192   float EW, bixx, biyy, bizz, bixz, biyz;
193   FGColumnVector3 vbaseXYZcg(3);
194
195   AC_cfg->GetNextConfigLine();
196
197   while ((token = AC_cfg->GetValue()) != "/METRICS") {
198     *AC_cfg >> parameter;
199     if (parameter == "AC_WINGAREA") {
200       *AC_cfg >> WingArea;
201       if (debug_lvl > 0) cout << "    WingArea: " << WingArea  << endl;
202     } else if (parameter == "AC_WINGSPAN") {
203       *AC_cfg >> WingSpan;
204       if (debug_lvl > 0) cout << "    WingSpan: " << WingSpan  << endl;
205     } else if (parameter == "AC_WINGINCIDENCE") {
206       *AC_cfg >> WingIncidence;
207       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
208     } else if (parameter == "AC_CHORD") {
209       *AC_cfg >> cbar;
210       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
211     } else if (parameter == "AC_HTAILAREA") {
212       *AC_cfg >> HTailArea;
213       if (debug_lvl > 0) cout << "    H. Tail Area: " << HTailArea << endl;
214     } else if (parameter == "AC_HTAILARM") {
215       *AC_cfg >> HTailArm;
216       if (debug_lvl > 0) cout << "    H. Tail Arm: " << HTailArm << endl;
217     } else if (parameter == "AC_VTAILAREA") {
218       *AC_cfg >> VTailArea;
219       if (debug_lvl > 0) cout << "    V. Tail Area: " << VTailArea << endl;
220     } else if (parameter == "AC_VTAILARM") {
221       *AC_cfg >> VTailArm;
222       if (debug_lvl > 0) cout << "    V. Tail Arm: " << VTailArm << endl;
223     } else if (parameter == "AC_IXX") {
224       *AC_cfg >> bixx;
225       if (debug_lvl > 0) cout << "    baseIxx: " << bixx << endl;
226       MassBalance->SetBaseIxx(bixx);
227     } else if (parameter == "AC_IYY") {
228       *AC_cfg >> biyy;
229       if (debug_lvl > 0) cout << "    baseIyy: " << biyy << endl;
230       MassBalance->SetBaseIyy(biyy);
231     } else if (parameter == "AC_IZZ") {
232       *AC_cfg >> bizz;
233       if (debug_lvl > 0) cout << "    baseIzz: " << bizz << endl;
234       MassBalance->SetBaseIzz(bizz);
235     } else if (parameter == "AC_IXZ") {
236       *AC_cfg >> bixz;
237       if (debug_lvl > 0) cout << "    baseIxz: " << bixz  << endl;
238       MassBalance->SetBaseIxz(bixz);
239     } else if (parameter == "AC_IYZ") {
240       *AC_cfg >> biyz;
241       if (debug_lvl > 0) cout << "    baseIyz: " << biyz  << endl;
242       MassBalance->SetBaseIyz(biyz);
243     } else if (parameter == "AC_EMPTYWT") {
244       *AC_cfg >> EW;
245       MassBalance->SetEmptyWeight(EW);
246       if (debug_lvl > 0) cout << "    EmptyWeight: " << EW  << endl;
247     } else if (parameter == "AC_CGLOC") {
248       *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
249       MassBalance->SetBaseCG(vbaseXYZcg);
250       if (debug_lvl > 0) cout << "    CG (x, y, z): " << vbaseXYZcg << endl;
251     } else if (parameter == "AC_EYEPTLOC") {
252       *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
253       if (debug_lvl > 0) cout << "    Eyepoint (x, y, z): " << vXYZep << endl;
254     } else if (parameter == "AC_AERORP") {
255       *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
256       if (debug_lvl > 0) cout << "    Ref Pt (x, y, z): " << vXYZrp << endl;
257     } else if (parameter == "AC_ALPHALIMITS") {
258       *AC_cfg >> alphaclmin >> alphaclmax;
259       if (debug_lvl > 0) cout << "    Maximum Alpha: " << alphaclmax
260              << "    Minimum Alpha: " << alphaclmin
261              << endl;
262     }
263   }
264   
265   // calculate some derived parameters
266   if (cbar != 0.0) {
267     lbarh = HTailArm/cbar;
268     lbarv = VTailArm/cbar;
269     if (WingArea != 0.0) {
270       vbarh = HTailArm*HTailArea / (cbar*WingArea);
271       vbarv = VTailArm*VTailArea / (cbar*WingArea);
272     }
273   }     
274
275 }
276
277 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278
279 void FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg)
280 {
281   if (!Propulsion->Load(AC_cfg)) {
282     cerr << "Propulsion not successfully loaded" << endl;
283   }
284 }
285
286 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287
288 void FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg)
289 {
290   if (!FCS->Load(AC_cfg)) {
291     cerr << "Flight Controls not successfully loaded" << endl;
292   }
293 }
294
295 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296
297 void FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg)
298 {
299   if (!Aerodynamics->Load(AC_cfg)) {
300     cerr << "Aerodynamics not successfully loaded" << endl;
301   }
302 }
303
304 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305
306 void FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg)
307 {
308   if (!GroundReactions->Load(AC_cfg)) {
309     cerr << "Ground Reactions not successfully loaded" << endl;
310   }
311 }
312
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314
315 void FGAircraft::ReadOutput(FGConfigFile* AC_cfg)
316 {
317   string token, parameter;
318   int OutRate = 0;
319   int subsystems = 0;
320
321   token = AC_cfg->GetValue("NAME");
322   Output->SetFilename(token);
323   token = AC_cfg->GetValue("TYPE");
324   Output->SetType(token);
325   AC_cfg->GetNextConfigLine();
326
327   while ((token = AC_cfg->GetValue()) != "/OUTPUT") {
328     *AC_cfg >> parameter;
329     if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
330     if (parameter == "SIMULATION") {
331       *AC_cfg >> parameter;
332       if (parameter == "ON") subsystems += ssSimulation;
333     }
334     if (parameter == "AEROSURFACES") {
335       *AC_cfg >> parameter;
336       if (parameter == "ON") subsystems += ssAerosurfaces;
337     }
338     if (parameter == "RATES") {
339       *AC_cfg >> parameter;
340       if (parameter == "ON") subsystems += ssRates;
341     }
342     if (parameter == "VELOCITIES") {
343       *AC_cfg >> parameter;
344       if (parameter == "ON") subsystems += ssVelocities;
345     }
346     if (parameter == "FORCES") {
347       *AC_cfg >> parameter;
348       if (parameter == "ON") subsystems += ssForces;
349     }
350     if (parameter == "MOMENTS") {
351       *AC_cfg >> parameter;
352       if (parameter == "ON") subsystems += ssMoments;
353     }
354     if (parameter == "ATMOSPHERE") {
355       *AC_cfg >> parameter;
356       if (parameter == "ON") subsystems += ssAtmosphere;
357     }
358     if (parameter == "MASSPROPS") {
359       *AC_cfg >> parameter;
360       if (parameter == "ON") subsystems += ssMassProps;
361     }
362     if (parameter == "POSITION") {
363       *AC_cfg >> parameter;
364       if (parameter == "ON") subsystems += ssPosition;
365     }
366     if (parameter == "COEFFICIENTS") {
367       *AC_cfg >> parameter;
368       if (parameter == "ON") subsystems += ssCoefficients;
369     }
370     if (parameter == "GROUND_REACTIONS") {
371       *AC_cfg >> parameter;
372       if (parameter == "ON") subsystems += ssGroundReactions;
373     }
374     if (parameter == "FCS") {
375       *AC_cfg >> parameter;
376       if (parameter == "ON") subsystems += ssFCS;
377     }
378     if (parameter == "PROPULSION") {
379       *AC_cfg >> parameter;
380       if (parameter == "ON") subsystems += ssPropulsion;
381     }
382   }
383
384   Output->SetSubsystems(subsystems);
385
386   OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
387   Output->SetRate( (int)(0.5 + 1.0/(State->Getdt()*OutRate)) );
388 }
389
390 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391
392 void FGAircraft::ReadPrologue(FGConfigFile* AC_cfg)
393 {
394   string token = AC_cfg->GetValue();
395   string scratch;
396   AircraftName = AC_cfg->GetValue("NAME");
397   if (debug_lvl > 0) cout << underon << "Reading Aircraft Configuration File"
398             << underoff << ": " << highint << AircraftName << normint << endl;
399   scratch = AC_cfg->GetValue("VERSION").c_str();
400
401   CFGVersion = AC_cfg->GetValue("VERSION");
402
403   if (debug_lvl > 0)
404     cout << "                            Version: " << highint << CFGVersion
405                                                              << normint << endl;
406   if (CFGVersion != NEEDED_CFG_VERSION) {
407     cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
408             " RESULTS WILL BE UNPREDICTABLE !!" << endl;
409     cerr << "Current version needed is: " << NEEDED_CFG_VERSION << endl;
410     cerr << "         You have version: " << CFGVersion << endl << fgdef << endl;
411   }
412 }
413
414 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
415
416 void FGAircraft::Debug(void)
417 {
418     //TODO: Add your source code here
419 }
420