1 /*******************************************************************************
6 Purpose: Encapsulates an aircraft
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
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
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
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.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
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.
35 --------------------------------------------------------------------------------
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
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
50 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",
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
59 The aerodynamic coefficients used in this model are:
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
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
78 Cyb - Side force due to sideslip
79 Cyr - Side force due to yaw rate
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)
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
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
100 ********************************************************************************
102 *******************************************************************************/
104 #include <sys/stat.h>
105 #include <sys/types.h>
108 # ifndef __BORLANDC__
109 # include <simgear/compiler.h>
111 # ifdef FG_HAVE_STD_INCLUDES
120 #include "FGAircraft.h"
121 #include "FGTranslation.h"
122 #include "FGRotation.h"
123 #include "FGAtmosphere.h"
125 #include "FGFDMExec.h"
127 #include "FGPosition.h"
128 #include "FGAuxiliary.h"
129 #include "FGOutput.h"
131 /*******************************************************************************
132 ************************************ CODE **************************************
133 *******************************************************************************/
135 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex),
150 AxisIdx["PITCH"] = 4;
155 /******************************************************************************/
158 FGAircraft::~FGAircraft(void)
162 /******************************************************************************/
164 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname)
168 string aircraftCfgFileName;
171 AircraftPath = aircraft_path;
172 EnginePath = engine_path;
174 aircraftCfgFileName = AircraftPath + "/" + fname + "/" + fname + ".cfg";
176 FGConfigFile AC_cfg(aircraftCfgFileName);
178 ReadPrologue(&AC_cfg);
180 while ((AC_cfg.GetNextConfigLine() != "EOF") &&
181 (token = AC_cfg.GetValue()) != "/FDM_CONFIG")
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);
204 /******************************************************************************/
206 bool FGAircraft::Run(void)
208 if (!FGModel::Run()) { // if false then execute this Run()
211 for (int i = 1; i <= 3; i++) vForces(i) = vMoments(i) = 0.0;
219 } else { // skip Run() execution this time
224 /******************************************************************************/
226 void FGAircraft::MassChange()
228 static FGColumnVector vXYZtank(3);
230 float IXXt, IYYt, IZZt, IXZt;
232 unsigned int axis_ctr;
234 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vXYZtank(axis_ctr) = 0.0;
236 // UPDATE TANK CONTENTS
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.
245 float Oshortage, Fshortage;
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:
253 switch(Tank[t]->GetType()) {
255 if (Tank[t]->GetSelected()) {
256 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
257 numSelectedFuelTanks)*(dt*rate) + Fshortage);
260 case FGTank::ttOXIDIZER:
261 if (Tank[t]->GetSelected()) {
262 Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
263 numSelectedOxiTanks)*(dt*rate) + Oshortage);
269 case FGEngine::etPiston:
270 case FGEngine::etTurboJet:
271 case FGEngine::etTurboProp:
273 if (Tank[t]->GetSelected()) {
274 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
275 numSelectedFuelTanks)*(dt*rate) + Fshortage);
280 if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
281 else Engine[e]->SetStarved(false);
284 Weight = EmptyWeight;
285 for (t=0; t<numTanks; t++)
286 Weight += Tank[t]->GetContents();
288 Mass = Weight / GRAVITY;
290 // Calculate new CG here.
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();
298 Tw += Tank[t]->GetContents();
301 vXYZcg = (vXYZtank + EmptyWeight*vbaseXYZcg) / (Tw + EmptyWeight);
303 // Calculate new moments of inertia here
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;
313 Ixx = baseIxx + IXXt;
314 Iyy = baseIyy + IYYt;
315 Izz = baseIzz + IZZt;
316 Ixz = baseIxz + IXZt;
320 /******************************************************************************/
322 void FGAircraft::FMAero(void)
324 static FGColumnVector vFs(3);
325 static FGColumnVector vDXYZcg(3);
326 unsigned int axis_ctr,ctr;
328 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vFs(axis_ctr) = 0.0;
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();
336 vForces += State->GetTs2b(alpha, beta)*vFs;
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.
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;
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
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();
359 /******************************************************************************/
361 void FGAircraft::FMGear(void)
366 for (unsigned int i=0;i<lGear.size();i++) {
372 /******************************************************************************/
374 void FGAircraft::FMMass(void)
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;
381 /******************************************************************************/
383 void FGAircraft::FMProp(void)
385 for (int i=0;i<numEngines;i++) {
386 vForces(eX) += Engine[i]->CalcThrust();
390 /******************************************************************************/
392 void FGAircraft::GetState(void)
396 alpha = Translation->Getalpha();
397 beta = Translation->Getbeta();
398 vEuler = Rotation->GetEuler();
401 /******************************************************************************/
403 void FGAircraft::ReadMetrics(FGConfigFile* AC_cfg)
408 AC_cfg->GetNextConfigLine();
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);
426 /******************************************************************************/
428 void FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg)
434 AC_cfg->GetNextConfigLine();
436 while ((token = AC_cfg->GetValue()) != "/PROPULSION") {
437 *AC_cfg >> parameter;
439 if (parameter == "AC_ENGINE") {
441 *AC_cfg >> engine_name;
442 Engine[numEngines] = new FGEngine(FDMExec, EnginePath, engine_name, numEngines);
445 } else if (parameter == "AC_TANK") {
447 Tank[numTanks] = new FGTank(AC_cfg);
448 switch(Tank[numTanks]->GetType()) {
450 numSelectedFuelTanks++;
452 case FGTank::ttOXIDIZER:
453 numSelectedOxiTanks++;
461 /******************************************************************************/
463 void FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg)
467 FCS->LoadFCS(AC_cfg);
470 /******************************************************************************/
472 void FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg)
476 AC_cfg->GetNextConfigLine();
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()));
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());
493 AC_cfg->GetNextConfigLine();
498 /******************************************************************************/
500 void FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg)
504 AC_cfg->GetNextConfigLine();
506 while ((token = AC_cfg->GetValue()) != "/UNDERCARRIAGE") {
507 lGear.push_back(new FGLGear(AC_cfg));
511 /******************************************************************************/
513 void FGAircraft::ReadPrologue(FGConfigFile* AC_cfg)
515 string token = AC_cfg->GetValue();
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;
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;
531 /******************************************************************************/
533 void FGAircraft::DisplayCoeffFactors(int multipliers)
535 cout << " Non-Dimensionalized by: ";
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 ";
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 ";
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 ";
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) ";
571 /******************************************************************************/