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;
153 numTanks = numEngines = numSelectedFuelTanks = numSelectedOxiTanks = 0;
157 /******************************************************************************/
160 FGAircraft::~FGAircraft(void)
164 /******************************************************************************/
166 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname)
170 string aircraftCfgFileName;
173 AircraftPath = aircraft_path;
174 EnginePath = engine_path;
176 aircraftCfgFileName = AircraftPath + "/" + fname + "/" + fname + ".cfg";
178 FGConfigFile AC_cfg(aircraftCfgFileName);
180 ReadPrologue(&AC_cfg);
182 while ((AC_cfg.GetNextConfigLine() != "EOF") &&
183 (token = AC_cfg.GetValue()) != "/FDM_CONFIG")
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);
206 /******************************************************************************/
208 bool FGAircraft::Run(void)
210 if (!FGModel::Run()) { // if false then execute this Run()
213 for (int i = 1; i <= 3; i++) vForces(i) = vMoments(i) = 0.0;
221 } else { // skip Run() execution this time
226 /******************************************************************************/
228 void FGAircraft::MassChange()
230 static FGColumnVector vXYZtank(3);
232 float IXXt, IYYt, IZZt, IXZt;
234 unsigned int axis_ctr;
236 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vXYZtank(axis_ctr) = 0.0;
238 // UPDATE TANK CONTENTS
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.
247 float Oshortage, Fshortage;
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:
255 switch(Tank[t]->GetType()) {
257 if (Tank[t]->GetSelected()) {
258 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
259 numSelectedFuelTanks)*(dt*rate) + Fshortage);
262 case FGTank::ttOXIDIZER:
263 if (Tank[t]->GetSelected()) {
264 Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
265 numSelectedOxiTanks)*(dt*rate) + Oshortage);
271 case FGEngine::etPiston:
272 case FGEngine::etTurboJet:
273 case FGEngine::etTurboProp:
275 if (Tank[t]->GetSelected()) {
276 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
277 numSelectedFuelTanks)*(dt*rate) + Fshortage);
282 if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
283 else Engine[e]->SetStarved(false);
286 Weight = EmptyWeight;
287 for (t=0; t<numTanks; t++)
288 Weight += Tank[t]->GetContents();
290 Mass = Weight / GRAVITY;
291 // Calculate new CG here.
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();
299 Tw += Tank[t]->GetContents();
302 vXYZcg = (vXYZtank + EmptyWeight*vbaseXYZcg) / (Tw + EmptyWeight);
304 // Calculate new moments of inertia here
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;
314 Ixx = baseIxx + IXXt;
315 Iyy = baseIyy + IYYt;
316 Izz = baseIzz + IZZt;
317 Ixz = baseIxz + IXZt;
321 /******************************************************************************/
323 void FGAircraft::FMAero(void)
325 static FGColumnVector vFs(3);
326 static FGColumnVector vDXYZcg(3);
327 unsigned int axis_ctr,ctr;
329 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vFs(axis_ctr) = 0.0;
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();
337 vForces += State->GetTs2b(alpha, beta)*vFs;
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.
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;
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
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();
360 /******************************************************************************/
362 void FGAircraft::FMGear(void)
367 for (unsigned int i=0;i<lGear.size();i++) {
373 /******************************************************************************/
375 void FGAircraft::FMMass(void)
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;
382 /******************************************************************************/
384 void FGAircraft::FMProp(void)
386 for (int i=0;i<numEngines;i++) {
387 vForces(eX) += Engine[i]->CalcThrust();
391 /******************************************************************************/
393 void FGAircraft::GetState(void)
397 alpha = Translation->Getalpha();
398 beta = Translation->Getbeta();
399 vEuler = Rotation->GetEuler();
402 /******************************************************************************/
404 void FGAircraft::ReadMetrics(FGConfigFile* AC_cfg)
409 AC_cfg->GetNextConfigLine();
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);
427 /******************************************************************************/
429 void FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg)
435 AC_cfg->GetNextConfigLine();
437 while ((token = AC_cfg->GetValue()) != "/PROPULSION") {
438 *AC_cfg >> parameter;
440 if (parameter == "AC_ENGINE") {
442 *AC_cfg >> engine_name;
443 Engine[numEngines] = new FGEngine(FDMExec, EnginePath, engine_name, numEngines);
446 } else if (parameter == "AC_TANK") {
448 Tank[numTanks] = new FGTank(AC_cfg);
449 switch(Tank[numTanks]->GetType()) {
451 numSelectedFuelTanks++;
453 case FGTank::ttOXIDIZER:
454 numSelectedOxiTanks++;
462 /******************************************************************************/
464 void FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg)
468 FCS->LoadFCS(AC_cfg);
471 /******************************************************************************/
473 void FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg)
477 AC_cfg->GetNextConfigLine();
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()));
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());
494 AC_cfg->GetNextConfigLine();
499 /******************************************************************************/
501 void FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg)
505 AC_cfg->GetNextConfigLine();
507 while ((token = AC_cfg->GetValue()) != "/UNDERCARRIAGE") {
508 lGear.push_back(new FGLGear(AC_cfg));
512 /******************************************************************************/
514 void FGAircraft::ReadPrologue(FGConfigFile* AC_cfg)
516 string token = AC_cfg->GetValue();
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;
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;
532 /******************************************************************************/
534 void FGAircraft::DisplayCoeffFactors(int multipliers)
536 cout << " Non-Dimensionalized by: ";
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 ";
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 ";
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 ";
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) ";
572 /******************************************************************************/