]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAircraft.cpp
0429 updates from Jon.
[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   numTanks = numEngines = numSelectedFuelTanks = numSelectedOxiTanks = 0;
154 }
155
156
157 /******************************************************************************/
158
159
160 FGAircraft::~FGAircraft(void)
161 {
162 }
163
164 /******************************************************************************/
165
166 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname)
167 {
168   string path;
169   string filename;
170   string aircraftCfgFileName;
171   string token;
172
173   AircraftPath = aircraft_path;
174   EnginePath = engine_path;
175
176   aircraftCfgFileName = AircraftPath + "/" + fname + "/" + fname + ".cfg";
177
178   FGConfigFile AC_cfg(aircraftCfgFileName);
179
180   ReadPrologue(&AC_cfg);
181
182   while ((AC_cfg.GetNextConfigLine() != "EOF") &&
183                                    (token = AC_cfg.GetValue()) != "/FDM_CONFIG")
184   {
185     if (token == "METRICS") {
186       cout << "  Reading Metrics" << endl;
187       ReadMetrics(&AC_cfg);
188     } else if (token == "AERODYNAMICS") {
189       cout << "  Reading Aerodynamics" << endl;
190       ReadAerodynamics(&AC_cfg);
191     } else if (token == "UNDERCARRIAGE") {
192       cout << "  Reading Landing Gear" << endl;
193       ReadUndercarriage(&AC_cfg);
194     } else if (token == "PROPULSION") {
195       cout << "  Reading Propulsion" << endl;
196       ReadPropulsion(&AC_cfg);
197     } else if (token == "FLIGHT_CONTROL") {
198       cout << "  Reading Flight Control" << endl;
199       ReadFlightControls(&AC_cfg);
200     }
201   }
202
203   return true;
204 }
205
206 /******************************************************************************/
207
208 bool FGAircraft::Run(void)
209 {
210   if (!FGModel::Run()) {                 // if false then execute this Run()
211     GetState();
212
213     for (int i = 1; i <= 3; i++)  vForces(i) = vMoments(i) = 0.0;
214
215     MassChange();
216
217     FMProp();
218     FMAero();
219     FMGear();
220     FMMass();
221   } else {                               // skip Run() execution this time
222   }
223   return false;
224 }
225
226 /******************************************************************************/
227
228 void FGAircraft::MassChange()
229 {
230   static FGColumnVector vXYZtank(3);
231   float Tw;
232   float IXXt, IYYt, IZZt, IXZt;
233   int t;
234   unsigned int axis_ctr;
235
236   for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vXYZtank(axis_ctr) = 0.0;
237
238   // UPDATE TANK CONTENTS
239   //
240   // For each engine, cycle through the tanks and draw an equal amount of
241   // fuel (or oxidizer) from each active tank. The needed amount of fuel is
242   // determined by the engine in the FGEngine class. If more fuel is needed
243   // than is available in the tank, then that amount is considered a shortage,
244   // and will be drawn from the next tank. If the engine cannot be fed what it
245   // needs, it will be considered to be starved, and will shut down.
246
247   float Oshortage, Fshortage;
248
249   for (int e=0; e<numEngines; e++) {
250     Fshortage = Oshortage = 0.0;
251     for (t=0; t<numTanks; t++) {
252       switch(Engine[e]->GetType()) {
253       case FGEngine::etRocket:
254
255         switch(Tank[t]->GetType()) {
256         case FGTank::ttFUEL:
257           if (Tank[t]->GetSelected()) {
258             Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
259                                          numSelectedFuelTanks)*(dt*rate) + Fshortage);
260           }
261           break;
262         case FGTank::ttOXIDIZER:
263           if (Tank[t]->GetSelected()) {
264             Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
265                                          numSelectedOxiTanks)*(dt*rate) + Oshortage);
266           }
267           break;
268         }
269         break;
270
271       case FGEngine::etPiston:
272       case FGEngine::etTurboJet:
273       case FGEngine::etTurboProp:
274
275         if (Tank[t]->GetSelected()) {
276           Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
277                                        numSelectedFuelTanks)*(dt*rate) + Fshortage);
278         }
279         break;
280       }
281     }
282     if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
283     else Engine[e]->SetStarved(false);
284   }
285
286   Weight = EmptyWeight;
287   for (t=0; t<numTanks; t++)
288     Weight += Tank[t]->GetContents();
289
290   Mass = Weight / GRAVITY;
291   // Calculate new CG here.
292
293   Tw = 0;
294   for (t=0; t<numTanks; t++) {
295     vXYZtank(eX) += Tank[t]->GetX()*Tank[t]->GetContents();
296     vXYZtank(eY) += Tank[t]->GetY()*Tank[t]->GetContents();
297     vXYZtank(eZ) += Tank[t]->GetZ()*Tank[t]->GetContents();
298
299     Tw += Tank[t]->GetContents();
300   }
301
302   vXYZcg = (vXYZtank + EmptyWeight*vbaseXYZcg) / (Tw + EmptyWeight);
303
304   // Calculate new moments of inertia here
305
306   IXXt = IYYt = IZZt = IXZt = 0.0;
307   for (t=0; t<numTanks; t++) {
308     IXXt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetX() - vXYZcg(eX))/12.0)*Tank[t]->GetContents()/GRAVITY;
309     IYYt += ((Tank[t]->GetY()-vXYZcg(eY))/12.0)*((Tank[t]->GetY() - vXYZcg(eY))/12.0)*Tank[t]->GetContents()/GRAVITY;
310     IZZt += ((Tank[t]->GetZ()-vXYZcg(eZ))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
311     IXZt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
312   }
313
314   Ixx = baseIxx + IXXt;
315   Iyy = baseIyy + IYYt;
316   Izz = baseIzz + IZZt;
317   Ixz = baseIxz + IXZt;
318
319 }
320
321 /******************************************************************************/
322
323 void FGAircraft::FMAero(void)
324 {
325   static FGColumnVector vFs(3);
326   static FGColumnVector vDXYZcg(3);
327   unsigned int axis_ctr,ctr;
328
329   for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vFs(axis_ctr) = 0.0;
330
331   for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
332     for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
333       vFs(axis_ctr+1) += Coeff[axis_ctr][ctr].TotalValue();
334     }
335   }
336
337   vForces += State->GetTs2b(alpha, beta)*vFs;
338
339   // The d*cg distances below, given in inches, are the distances FROM the c.g.
340   // TO the reference point. Since the c.g. and ref point are given in inches in
341   // the structural system (X positive rearwards) and the body coordinate system
342   // is given with X positive out the nose, the dxcg and dzcg values are
343   // *rotated* 180 degrees about the Y axis.
344
345   vDXYZcg(eX) = -(vXYZrp(eX) - vXYZcg(eX))/12.0;  //cg and rp values are in inches
346   vDXYZcg(eY) =  (vXYZrp(eY) - vXYZcg(eY))/12.0;
347   vDXYZcg(eZ) = -(vXYZrp(eZ) - vXYZcg(eZ))/12.0;
348
349   vMoments(eL) += vForces(eZ)*vDXYZcg(eY) - vForces(eY)*vDXYZcg(eZ); // rolling moment
350   vMoments(eM) += vForces(eX)*vDXYZcg(eZ) - vForces(eZ)*vDXYZcg(eX); // pitching moment
351   vMoments(eN) += vForces(eX)*vDXYZcg(eY) - vForces(eY)*vDXYZcg(eX); // yawing moment
352
353   for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
354     for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
355       vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr].TotalValue();
356     }
357   }
358 }
359
360 /******************************************************************************/
361
362 void FGAircraft::FMGear(void)
363 {
364   if (GearUp) {
365     // crash routine
366   } else {
367     for (unsigned int i=0;i<lGear.size();i++) {
368       //      lGear[i].
369     }
370   }
371 }
372
373 /******************************************************************************/
374
375 void FGAircraft::FMMass(void)
376 {
377   vForces(eX) += -GRAVITY*sin(vEuler(eTht)) * Mass;
378   vForces(eY) +=  GRAVITY*sin(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
379   vForces(eZ) +=  GRAVITY*cos(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
380 }
381
382 /******************************************************************************/
383
384 void FGAircraft::FMProp(void)
385 {
386   for (int i=0;i<numEngines;i++) {
387     vForces(eX) += Engine[i]->CalcThrust();
388   }
389 }
390
391 /******************************************************************************/
392
393 void FGAircraft::GetState(void)
394 {
395   dt = State->Getdt();
396
397   alpha = Translation->Getalpha();
398   beta = Translation->Getbeta();
399   vEuler = Rotation->GetEuler();
400 }
401
402 /******************************************************************************/
403
404 void FGAircraft::ReadMetrics(FGConfigFile* AC_cfg)
405 {
406   string token = "";
407   string parameter;
408
409   AC_cfg->GetNextConfigLine();
410
411   while ((token = AC_cfg->GetValue()) != "/METRICS") {
412     *AC_cfg >> parameter;
413     if (parameter == "AC_WINGAREA") *AC_cfg >> WingArea;
414     else if (parameter == "AC_WINGSPAN") *AC_cfg >> WingSpan;
415     else if (parameter == "AC_CHORD") *AC_cfg >> cbar;
416     else if (parameter == "AC_IXX") *AC_cfg >> baseIxx;
417     else if (parameter == "AC_IYY") *AC_cfg >> baseIyy;
418     else if (parameter == "AC_IZZ") *AC_cfg >> baseIzz;
419     else if (parameter == "AC_IXZ") *AC_cfg >> baseIxz;
420     else if (parameter == "AC_EMPTYWT") *AC_cfg >> EmptyWeight;
421     else if (parameter == "AC_CGLOC") *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
422     else if (parameter == "AC_EYEPTLOC") *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
423     else if (parameter == "AC_AERORP") *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
424   }
425 }
426
427 /******************************************************************************/
428
429 void FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg)
430 {
431   string token;
432   string engine_name;
433   string parameter;
434
435   AC_cfg->GetNextConfigLine();
436
437   while ((token = AC_cfg->GetValue()) != "/PROPULSION") {
438     *AC_cfg >> parameter;
439
440     if (parameter == "AC_ENGINE") {
441
442       *AC_cfg >> engine_name;
443       Engine[numEngines] = new FGEngine(FDMExec, EnginePath, engine_name, numEngines);
444       numEngines++;
445
446     } else if (parameter == "AC_TANK") {
447
448       Tank[numTanks] = new FGTank(AC_cfg);
449       switch(Tank[numTanks]->GetType()) {
450       case FGTank::ttFUEL:
451         numSelectedFuelTanks++;
452         break;
453       case FGTank::ttOXIDIZER:
454         numSelectedOxiTanks++;
455         break;
456       }
457       numTanks++;
458     }
459   }
460 }
461
462 /******************************************************************************/
463
464 void FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg)
465 {
466   string token;
467
468   FCS->LoadFCS(AC_cfg);
469 }
470
471 /******************************************************************************/
472
473 void FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg)
474 {
475   string token, axis;
476
477   AC_cfg->GetNextConfigLine();
478
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   Coeff.push_back(*(new CoeffArray()));
485
486   while ((token = AC_cfg->GetValue()) != "/AERODYNAMICS") {
487     if (token == "AXIS") {
488       axis = AC_cfg->GetValue("NAME");
489       AC_cfg->GetNextConfigLine();
490       while ((token = AC_cfg->GetValue()) != "/AXIS") {
491         Coeff[AxisIdx[axis]].push_back(*(new FGCoefficient(FDMExec, AC_cfg)));
492         DisplayCoeffFactors(Coeff[AxisIdx[axis]].back().Getmultipliers());
493       }
494       AC_cfg->GetNextConfigLine();
495     }
496   }
497 }
498
499 /******************************************************************************/
500
501 void FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg)
502 {
503   string token;
504
505   AC_cfg->GetNextConfigLine();
506
507   while ((token = AC_cfg->GetValue()) != "/UNDERCARRIAGE") {
508     lGear.push_back(new FGLGear(AC_cfg));
509   }
510 }
511
512 /******************************************************************************/
513
514 void FGAircraft::ReadPrologue(FGConfigFile* AC_cfg)
515 {
516   string token = AC_cfg->GetValue();
517
518   AircraftName = AC_cfg->GetValue("NAME");
519   cout << "Reading Aircraft Configuration File: " << AircraftName << endl;
520   CFGVersion = strtod(AC_cfg->GetValue("VERSION").c_str(),NULL);
521   cout << "                            Version: " << CFGVersion << endl;
522
523   if (CFGVersion < NEEDED_CFG_VERSION) {
524     cout << endl << "YOU HAVE AN OLD CFG FILE FOR THIS AIRCRAFT."
525                     " RESULTS WILL BE UNPREDICTABLE !!" << endl;
526     cout << "Current version needed is: " << NEEDED_CFG_VERSION << endl;
527     cout << "         You have version: " << CFGVersion << endl << endl;
528     exit(-1);
529   }
530 }
531
532 /******************************************************************************/
533
534 void FGAircraft::DisplayCoeffFactors(int multipliers)
535 {
536   cout << "   Non-Dimensionalized by: ";
537
538   if (multipliers & FG_QBAR)      cout << "qbar ";
539   if (multipliers & FG_WINGAREA)  cout << "S ";
540   if (multipliers & FG_WINGSPAN)  cout << "b ";
541   if (multipliers & FG_CBAR)      cout << "c ";
542   if (multipliers & FG_ALPHA)     cout << "alpha ";
543   if (multipliers & FG_ALPHADOT)  cout << "alphadot ";
544   if (multipliers & FG_BETA)      cout << "beta ";
545   if (multipliers & FG_BETADOT)   cout << "betadot ";
546   if (multipliers & FG_PITCHRATE) cout << "q ";
547   if (multipliers & FG_ROLLRATE)  cout << "p ";
548   if (multipliers & FG_YAWRATE)   cout << "r ";
549
550   if (multipliers & FG_ELEVATOR_CMD)  cout << "De cmd ";
551   if (multipliers & FG_AILERON_CMD)   cout << "Da cmd ";
552   if (multipliers & FG_RUDDER_CMD)    cout << "Dr cmd ";
553   if (multipliers & FG_FLAPS_CMD)     cout << "Df cmd ";
554   if (multipliers & FG_SPOILERS_CMD)  cout << "Dsp cmd ";
555   if (multipliers & FG_SPDBRAKE_CMD)   cout << "Dsb cmd ";
556
557   if (multipliers & FG_ELEVATOR_POS)  cout << "De ";
558   if (multipliers & FG_AILERON_POS)   cout << "Da ";
559   if (multipliers & FG_RUDDER_POS)    cout << "Dr ";
560   if (multipliers & FG_FLAPS_POS)     cout << "Df ";
561   if (multipliers & FG_SPOILERS_POS)  cout << "Dsp ";
562   if (multipliers & FG_SPDBRAKE_POS)  cout << "Dsb ";
563
564   if (multipliers & FG_MACH)      cout << "Mach ";
565   if (multipliers & FG_ALTITUDE)  cout << "h ";
566   if (multipliers & FG_BI2VEL)    cout << "b /(2*Vt) ";
567   if (multipliers & FG_CI2VEL)    cout << "c /(2*Vt) ";
568
569   cout << endl;
570 }
571
572 /******************************************************************************/
573
574