]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAircraft.cpp
Updated JSBsim 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. LoadAircraft() is supplied with a
32 name of a valid, registered aircraft, and the data file is parsed.
33
34 HISTORY
35 --------------------------------------------------------------------------------
36 12/12/98   JSB   Created
37 04/03/99   JSB   Changed Aero() method to correct body axis force calculation
38                  from wind vector. Fix provided by Tony Peden.
39 05/03/99   JSB   Changed (for the better?) the way configurations are read in.
40 9/17/99     TP   Combined force and moment functions. Added aero reference 
41                  point to config file. Added calculations for moments due to 
42                  difference in cg and aero reference point
43
44 ********************************************************************************
45 COMMENTS, REFERENCES,  and NOTES
46 ********************************************************************************
47 [1] Cooke, Zyda, Pratt, and McGhee, "NPSNET: Flight Simulation Dynamic Modeling
48       Using Quaternions", Presence, Vol. 1, No. 4, pp. 404-420  Naval Postgraduate
49       School, January 1994
50 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",
51       JSC 12960, July 1977
52 [3] Richard E. McFarland, "A Standard Kinematic Model for Flight Simulation at
53       NASA-Ames", NASA CR-2497, January 1975
54 [4] Barnes W. McCormick, "Aerodynamics, Aeronautics, and Flight Mechanics",
55       Wiley & Sons, 1979 ISBN 0-471-03032-5
56 [5] Bernard Etkin, "Dynamics of Flight, Stability and Control", Wiley & Sons,
57       1982 ISBN 0-471-08936-2
58
59 The aerodynamic coefficients used in this model are:
60
61 Longitudinal
62   CL0 - Reference lift at zero alpha
63   CD0 - Reference drag at zero alpha
64   CDM - Drag due to Mach
65   CLa - Lift curve slope (w.r.t. alpha)
66   CDa - Drag curve slope (w.r.t. alpha)
67   CLq - Lift due to pitch rate
68   CLM - Lift due to Mach
69   CLadt - Lift due to alpha rate
70
71   Cmadt - Pitching Moment due to alpha rate
72   Cm0 - Reference Pitching moment at zero alpha
73   Cma - Pitching moment slope (w.r.t. alpha)
74   Cmq - Pitch damping (pitch moment due to pitch rate)
75   CmM - Pitch Moment due to Mach
76
77 Lateral
78   Cyb - Side force due to sideslip
79   Cyr - Side force due to yaw rate
80
81   Clb - Dihedral effect (roll moment due to sideslip)
82   Clp - Roll damping (roll moment due to roll rate)
83   Clr - Roll moment due to yaw rate
84   Cnb - Weathercocking stability (yaw moment due to sideslip)
85   Cnp - Rudder adverse yaw (yaw moment due to roll rate)
86   Cnr - Yaw damping (yaw moment due to yaw rate)
87
88 Control
89   CLDe - Lift due to elevator
90   CDDe - Drag due to elevator
91   CyDr - Side force due to rudder
92   CyDa - Side force due to aileron
93
94   CmDe - Pitch moment due to elevator
95   ClDa - Roll moment due to aileron
96   ClDr - Roll moment due to rudder
97   CnDr - Yaw moment due to rudder
98   CnDa - Yaw moment due to aileron
99
100 ********************************************************************************
101 INCLUDES
102 *******************************************************************************/
103
104 #include <sys/stat.h>
105 #include <sys/types.h>
106
107 #ifdef FGFS
108 # ifndef __BORLANDC__
109 #  include <simgear/compiler.h>
110 # endif
111 #  ifdef FG_HAVE_STD_INCLUDES
112 #    include <cmath>
113 #  else
114 #    include <math.h>
115 #  endif
116 #else
117 #  include <cmath>
118 #endif
119
120 #include "FGAircraft.h"
121 #include "FGTranslation.h"
122 #include "FGRotation.h"
123 #include "FGAtmosphere.h"
124 #include "FGState.h"
125 #include "FGFDMExec.h"
126 #include "FGFCS.h"
127 #include "FGPosition.h"
128 #include "FGAuxiliary.h"
129 #include "FGOutput.h"
130
131 /*******************************************************************************
132 ************************************ CODE **************************************
133 *******************************************************************************/
134
135 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex),
136                                            vMoments(3),
137                                            vForces(3),
138                                            vXYZrp(3),
139                                            vbaseXYZcg(3),
140                                            vXYZcg(3),
141                                            vXYZep(3),
142                                            vEuler(3)
143 {
144   Name = "FGAircraft";
145
146   AxisIdx["LIFT"]  = 0;
147   AxisIdx["SIDE"]  = 1;
148   AxisIdx["DRAG"]  = 2;
149   AxisIdx["ROLL"]  = 3;
150   AxisIdx["PITCH"] = 4;
151   AxisIdx["YAW"]   = 5;
152 }
153
154
155 /******************************************************************************/
156
157
158 FGAircraft::~FGAircraft(void)
159 {
160 }
161
162 /******************************************************************************/
163
164 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname)
165 {
166   string path;
167   string filename;
168   string aircraftCfgFileName;
169   string token;
170
171   AircraftPath = aircraft_path;
172   EnginePath = engine_path;
173
174   aircraftCfgFileName = AircraftPath + "/" + fname + "/" + fname + ".cfg";
175
176   FGConfigFile AC_cfg(aircraftCfgFileName);
177
178   ReadPrologue(&AC_cfg);
179
180   while ((AC_cfg.GetNextConfigLine() != "EOF") &&
181                                    (token = AC_cfg.GetValue()) != "/FDM_CONFIG")
182   {
183     if (token == "METRICS") {
184       cout << "  Reading Metrics" << endl;
185       ReadMetrics(&AC_cfg);
186     } else if (token == "AERODYNAMICS") {
187       cout << "  Reading Aerodynamics" << endl;
188       ReadAerodynamics(&AC_cfg);
189     } else if (token == "UNDERCARRIAGE") {
190       cout << "  Reading Landing Gear" << endl;
191       ReadUndercarriage(&AC_cfg);
192     } else if (token == "PROPULSION") {
193       cout << "  Reading Propulsion" << endl;
194       ReadPropulsion(&AC_cfg);
195     } else if (token == "FLIGHT_CONTROL") {
196       cout << "  Reading Flight Control" << endl;
197       ReadFlightControls(&AC_cfg);
198     }
199   }
200
201   return true;
202 }
203
204 /******************************************************************************/
205
206 bool FGAircraft::Run(void)
207 {
208   if (!FGModel::Run()) {                 // if false then execute this Run()
209     GetState();
210
211     for (int i = 1; i <= 3; i++)  vForces(i) = vMoments(i) = 0.0;
212
213     MassChange();
214
215     FMProp();
216     FMAero();
217     FMGear();
218     FMMass();
219   } else {                               // skip Run() execution this time
220   }
221   return false;
222 }
223
224 /******************************************************************************/
225
226 void FGAircraft::MassChange()
227 {
228   static FGColumnVector vXYZtank(3);
229   float Tw;
230   float IXXt, IYYt, IZZt, IXZt;
231   int t;
232   unsigned int axis_ctr;
233
234   for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vXYZtank(axis_ctr) = 0.0;
235
236   // UPDATE TANK CONTENTS
237   //
238   // For each engine, cycle through the tanks and draw an equal amount of
239   // fuel (or oxidizer) from each active tank. The needed amount of fuel is
240   // determined by the engine in the FGEngine class. If more fuel is needed
241   // than is available in the tank, then that amount is considered a shortage,
242   // and will be drawn from the next tank. If the engine cannot be fed what it
243   // needs, it will be considered to be starved, and will shut down.
244
245   float Oshortage, Fshortage;
246
247   for (int e=0; e<numEngines; e++) {
248     Fshortage = Oshortage = 0.0;
249     for (t=0; t<numTanks; t++) {
250       switch(Engine[e]->GetType()) {
251       case FGEngine::etRocket:
252
253         switch(Tank[t]->GetType()) {
254         case FGTank::ttFUEL:
255           if (Tank[t]->GetSelected()) {
256             Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
257                                          numSelectedFuelTanks)*(dt*rate) + Fshortage);
258           }
259           break;
260         case FGTank::ttOXIDIZER:
261           if (Tank[t]->GetSelected()) {
262             Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
263                                          numSelectedOxiTanks)*(dt*rate) + Oshortage);
264           }
265           break;
266         }
267         break;
268
269       case FGEngine::etPiston:
270       case FGEngine::etTurboJet:
271       case FGEngine::etTurboProp:
272
273         if (Tank[t]->GetSelected()) {
274           Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
275                                        numSelectedFuelTanks)*(dt*rate) + Fshortage);
276         }
277         break;
278       }
279     }
280     if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
281     else Engine[e]->SetStarved(false);
282   }
283
284   Weight = EmptyWeight;
285   for (t=0; t<numTanks; t++)
286     Weight += Tank[t]->GetContents();
287
288   Mass = Weight / GRAVITY;
289
290   // Calculate new CG here.
291
292   Tw = 0;
293   for (t=0; t<numTanks; t++) {
294     vXYZtank(eX) += Tank[t]->GetX()*Tank[t]->GetContents();
295     vXYZtank(eY) += Tank[t]->GetY()*Tank[t]->GetContents();
296     vXYZtank(eZ) += Tank[t]->GetZ()*Tank[t]->GetContents();
297
298     Tw += Tank[t]->GetContents();
299   }
300
301   vXYZcg = (vXYZtank + EmptyWeight*vbaseXYZcg) / (Tw + EmptyWeight);
302
303   // Calculate new moments of inertia here
304
305   IXXt = IYYt = IZZt = IXZt = 0.0;
306   for (t=0; t<numTanks; t++) {
307     IXXt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetX() - vXYZcg(eX))/12.0)*Tank[t]->GetContents()/GRAVITY;
308     IYYt += ((Tank[t]->GetY()-vXYZcg(eY))/12.0)*((Tank[t]->GetY() - vXYZcg(eY))/12.0)*Tank[t]->GetContents()/GRAVITY;
309     IZZt += ((Tank[t]->GetZ()-vXYZcg(eZ))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
310     IXZt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
311   }
312
313   Ixx = baseIxx + IXXt;
314   Iyy = baseIyy + IYYt;
315   Izz = baseIzz + IZZt;
316   Ixz = baseIxz + IXZt;
317
318 }
319
320 /******************************************************************************/
321
322 void FGAircraft::FMAero(void)
323 {
324   static FGColumnVector vFs(3);
325   static FGColumnVector vDXYZcg(3);
326   unsigned int axis_ctr,ctr;
327
328   for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vFs(axis_ctr) = 0.0;
329
330   for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
331     for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
332       vFs(axis_ctr+1) += Coeff[axis_ctr][ctr].TotalValue();
333     }
334   }
335
336   vForces += State->GetTs2b(alpha, beta)*vFs;
337
338   // The d*cg distances below, given in inches, are the distances FROM the c.g.
339   // TO the reference point. Since the c.g. and ref point are given in inches in
340   // the structural system (X positive rearwards) and the body coordinate system
341   // is given with X positive out the nose, the dxcg and dzcg values are
342   // *rotated* 180 degrees about the Y axis.
343
344   vDXYZcg(eX) = -(vXYZrp(eX) - vXYZcg(eX))/12.0;  //cg and rp values are in inches
345   vDXYZcg(eY) =  (vXYZrp(eY) - vXYZcg(eY))/12.0;
346   vDXYZcg(eZ) = -(vXYZrp(eZ) - vXYZcg(eZ))/12.0;
347
348   vMoments(eL) += vForces(eZ)*vDXYZcg(eY) - vForces(eY)*vDXYZcg(eZ); // rolling moment
349   vMoments(eM) += vForces(eX)*vDXYZcg(eZ) - vForces(eZ)*vDXYZcg(eX); // pitching moment
350   vMoments(eN) += vForces(eX)*vDXYZcg(eY) - vForces(eY)*vDXYZcg(eX); // yawing moment
351
352   for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
353     for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
354       vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr].TotalValue();
355     }
356   }
357 }
358
359 /******************************************************************************/
360
361 void FGAircraft::FMGear(void)
362 {
363   if (GearUp) {
364     // crash routine
365   } else {
366     for (unsigned int i=0;i<lGear.size();i++) {
367       //      lGear[i].
368     }
369   }
370 }
371
372 /******************************************************************************/
373
374 void FGAircraft::FMMass(void)
375 {
376   vForces(eX) += -GRAVITY*sin(vEuler(eTht)) * Mass;
377   vForces(eY) +=  GRAVITY*sin(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
378   vForces(eZ) +=  GRAVITY*cos(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
379 }
380
381 /******************************************************************************/
382
383 void FGAircraft::FMProp(void)
384 {
385   for (int i=0;i<numEngines;i++) {
386     vForces(eX) += Engine[i]->CalcThrust();
387   }
388 }
389
390 /******************************************************************************/
391
392 void FGAircraft::GetState(void)
393 {
394   dt = State->Getdt();
395
396   alpha = Translation->Getalpha();
397   beta = Translation->Getbeta();
398   vEuler = Rotation->GetEuler();
399 }
400
401 /******************************************************************************/
402
403 void FGAircraft::ReadMetrics(FGConfigFile* AC_cfg)
404 {
405   string token = "";
406   string parameter;
407
408   AC_cfg->GetNextConfigLine();
409
410   while ((token = AC_cfg->GetValue()) != "/METRICS") {
411     *AC_cfg >> parameter;
412     if (parameter == "AC_WINGAREA") *AC_cfg >> WingArea;
413     else if (parameter == "AC_WINGSPAN") *AC_cfg >> WingSpan;
414     else if (parameter == "AC_CHORD") *AC_cfg >> cbar;
415     else if (parameter == "AC_IXX") *AC_cfg >> baseIxx;
416     else if (parameter == "AC_IYY") *AC_cfg >> baseIyy;
417     else if (parameter == "AC_IZZ") *AC_cfg >> baseIzz;
418     else if (parameter == "AC_IXZ") *AC_cfg >> baseIxz;
419     else if (parameter == "AC_EMPTYWT") *AC_cfg >> EmptyWeight;
420     else if (parameter == "AC_CGLOC") *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
421     else if (parameter == "AC_EYEPTLOC") *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
422     else if (parameter == "AC_AERORP") *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
423   }
424 }
425
426 /******************************************************************************/
427
428 void FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg)
429 {
430   string token;
431   string engine_name;
432   string parameter;
433
434   AC_cfg->GetNextConfigLine();
435
436   while ((token = AC_cfg->GetValue()) != "/PROPULSION") {
437     *AC_cfg >> parameter;
438
439     if (parameter == "AC_ENGINE") {
440
441       *AC_cfg >> engine_name;
442       Engine[numEngines] = new FGEngine(FDMExec, EnginePath, engine_name, numEngines);
443       numEngines++;
444
445     } else if (parameter == "AC_TANK") {
446
447       Tank[numTanks] = new FGTank(AC_cfg);
448       switch(Tank[numTanks]->GetType()) {
449       case FGTank::ttFUEL:
450         numSelectedFuelTanks++;
451         break;
452       case FGTank::ttOXIDIZER:
453         numSelectedOxiTanks++;
454         break;
455       }
456       numTanks++;
457     }
458   }
459 }
460
461 /******************************************************************************/
462
463 void FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg)
464 {
465   string token;
466
467   FCS->LoadFCS(AC_cfg);
468 }
469
470 /******************************************************************************/
471
472 void FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg)
473 {
474   string token, axis;
475
476   AC_cfg->GetNextConfigLine();
477
478   Coeff.push_back(*(new CoeffArray()));
479   Coeff.push_back(*(new CoeffArray()));
480   Coeff.push_back(*(new CoeffArray()));
481   Coeff.push_back(*(new CoeffArray()));
482   Coeff.push_back(*(new CoeffArray()));
483   Coeff.push_back(*(new CoeffArray()));
484
485   while ((token = AC_cfg->GetValue()) != "/AERODYNAMICS") {
486     if (token == "AXIS") {
487       axis = AC_cfg->GetValue("NAME");
488       AC_cfg->GetNextConfigLine();
489       while ((token = AC_cfg->GetValue()) != "/AXIS") {
490         Coeff[AxisIdx[axis]].push_back(*(new FGCoefficient(FDMExec, AC_cfg)));
491         DisplayCoeffFactors(Coeff[AxisIdx[axis]].back().Getmultipliers());
492       }
493       AC_cfg->GetNextConfigLine();
494     }
495   }
496 }
497
498 /******************************************************************************/
499
500 void FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg)
501 {
502   string token;
503
504   AC_cfg->GetNextConfigLine();
505
506   while ((token = AC_cfg->GetValue()) != "/UNDERCARRIAGE") {
507     lGear.push_back(new FGLGear(AC_cfg));
508   }
509 }
510
511 /******************************************************************************/
512
513 void FGAircraft::ReadPrologue(FGConfigFile* AC_cfg)
514 {
515   string token = AC_cfg->GetValue();
516
517   AircraftName = AC_cfg->GetValue("NAME");
518   cout << "Reading Aircraft Configuration File: " << AircraftName << endl;
519   CFGVersion = strtod(AC_cfg->GetValue("VERSION").c_str(),NULL);
520   cout << "                            Version: " << CFGVersion << endl;
521
522   if (CFGVersion < NEEDED_CFG_VERSION) {
523     cout << endl << "YOU HAVE AN OLD CFG FILE FOR THIS AIRCRAFT."
524                     " RESULTS WILL BE UNPREDICTABLE !!" << endl;
525     cout << "Current version needed is: " << NEEDED_CFG_VERSION << endl;
526     cout << "         You have version: " << CFGVersion << endl << endl;
527     exit(-1);
528   }
529 }
530
531 /******************************************************************************/
532
533 void FGAircraft::DisplayCoeffFactors(int multipliers)
534 {
535   cout << "   Non-Dimensionalized by: ";
536
537   if (multipliers & FG_QBAR)      cout << "qbar ";
538   if (multipliers & FG_WINGAREA)  cout << "S ";
539   if (multipliers & FG_WINGSPAN)  cout << "b ";
540   if (multipliers & FG_CBAR)      cout << "c ";
541   if (multipliers & FG_ALPHA)     cout << "alpha ";
542   if (multipliers & FG_ALPHADOT)  cout << "alphadot ";
543   if (multipliers & FG_BETA)      cout << "beta ";
544   if (multipliers & FG_BETADOT)   cout << "betadot ";
545   if (multipliers & FG_PITCHRATE) cout << "q ";
546   if (multipliers & FG_ROLLRATE)  cout << "p ";
547   if (multipliers & FG_YAWRATE)   cout << "r ";
548
549   if (multipliers & FG_ELEVATOR_CMD)  cout << "De cmd ";
550   if (multipliers & FG_AILERON_CMD)   cout << "Da cmd ";
551   if (multipliers & FG_RUDDER_CMD)    cout << "Dr cmd ";
552   if (multipliers & FG_FLAPS_CMD)     cout << "Df cmd ";
553   if (multipliers & FG_SPOILERS_CMD)  cout << "Dsp cmd ";
554   if (multipliers & FG_SPDBRAKE_CMD)   cout << "Dsb cmd ";
555
556   if (multipliers & FG_ELEVATOR_POS)  cout << "De ";
557   if (multipliers & FG_AILERON_POS)   cout << "Da ";
558   if (multipliers & FG_RUDDER_POS)    cout << "Dr ";
559   if (multipliers & FG_FLAPS_POS)     cout << "Df ";
560   if (multipliers & FG_SPOILERS_POS)  cout << "Dsp ";
561   if (multipliers & FG_SPDBRAKE_POS)  cout << "Dsb ";
562
563   if (multipliers & FG_MACH)      cout << "Mach ";
564   if (multipliers & FG_ALTITUDE)  cout << "h ";
565   if (multipliers & FG_BI2VEL)    cout << "b /(2*Vt) ";
566   if (multipliers & FG_CI2VEL)    cout << "c /(2*Vt) ";
567
568   cout << endl;
569 }
570
571 /******************************************************************************/
572
573