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),
152 AxisIdx["PITCH"] = 4;
155 Coeff = new CoeffArray[6];
159 alphaclmin = alphaclmax = 0;
161 numTanks = numEngines = numSelectedFuelTanks = numSelectedOxiTanks = 0;
165 /******************************************************************************/
168 FGAircraft::~FGAircraft(void) {
170 cout << " ~FGAircraft" << endl;
172 for(i=0;i<numEngines; i++)
175 cout << " Engine" << endl;
177 for(i=0;i<numTanks; i++)
180 cout << " Tank" << endl;
181 cout << " NumAxes: " << 6 << endl;
183 cout << " NumCoeffs: " << Coeff[i].size() << " " << &Coeff[i] << endl;
184 for(j=0;j<Coeff[i].size();j++) {
186 cout << " Coeff[" << i << "][" << j << "]: " << Coeff[i][j] << endl;
191 cout << " Coeffs" << endl;
192 for(i=0;i<lGear.size();i++) {
197 /******************************************************************************/
199 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname) {
202 string aircraftCfgFileName;
205 AircraftPath = aircraft_path;
206 EnginePath = engine_path;
209 aircraftCfgFileName = AircraftPath + "/" + fname + "/" + fname + ".xml";
211 aircraftCfgFileName = AircraftPath + ";" + fname + ";" + fname + ".xml";
214 FGConfigFile AC_cfg(aircraftCfgFileName);
215 if (!AC_cfg.IsOpen()) return false;
217 ReadPrologue(&AC_cfg);
219 while ((AC_cfg.GetNextConfigLine() != "EOF") &&
220 (token = AC_cfg.GetValue()) != "/FDM_CONFIG") {
221 if (token == "METRICS") {
222 cout << " Reading Metrics" << endl;
223 ReadMetrics(&AC_cfg);
224 } else if (token == "AERODYNAMICS") {
225 cout << " Reading Aerodynamics" << endl;
226 ReadAerodynamics(&AC_cfg);
227 } else if (token == "UNDERCARRIAGE") {
228 cout << " Reading Landing Gear" << endl;
229 ReadUndercarriage(&AC_cfg);
230 } else if (token == "PROPULSION") {
231 cout << " Reading Propulsion" << endl;
232 ReadPropulsion(&AC_cfg);
233 } else if (token == "FLIGHT_CONTROL") {
234 cout << " Reading Flight Control" << endl;
235 ReadFlightControls(&AC_cfg);
236 } else if (token == "OUTPUT") {
244 /******************************************************************************/
246 bool FGAircraft::Run(void) {
247 if (!FGModel::Run()) { // if false then execute this Run()
250 for (int i = 1; i <= 3; i++) vForces(i) = vMoments(i) = 0.0;
259 if (fabs(Position->GetGamma()) < 1.57) {
260 nlf = vFs(eZ)/(Weight*cos(Position->GetGamma()));
263 } else { // skip Run() execution this time
270 /******************************************************************************/
272 void FGAircraft::MassChange() {
273 static FGColumnVector vXYZtank(3);
275 float IXXt, IYYt, IZZt, IXZt;
277 unsigned int axis_ctr;
279 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vXYZtank(axis_ctr) = 0.0;
281 // UPDATE TANK CONTENTS
283 // For each engine, cycle through the tanks and draw an equal amount of
284 // fuel (or oxidizer) from each active tank. The needed amount of fuel is
285 // determined by the engine in the FGEngine class. If more fuel is needed
286 // than is available in the tank, then that amount is considered a shortage,
287 // and will be drawn from the next tank. If the engine cannot be fed what it
288 // needs, it will be considered to be starved, and will shut down.
290 float Oshortage, Fshortage;
292 for (unsigned int e=0; e<numEngines; e++) {
293 Fshortage = Oshortage = 0.0;
294 for (t=0; t<numTanks; t++) {
295 switch(Engine[e]->GetType()) {
296 case FGEngine::etRocket:
298 switch(Tank[t]->GetType()) {
300 if (Tank[t]->GetSelected()) {
301 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
302 numSelectedFuelTanks)*(dt*rate) + Fshortage);
305 case FGTank::ttOXIDIZER:
306 if (Tank[t]->GetSelected()) {
307 Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
308 numSelectedOxiTanks)*(dt*rate) + Oshortage);
314 case FGEngine::etPiston:
315 case FGEngine::etTurboJet:
316 case FGEngine::etTurboProp:
318 if (Tank[t]->GetSelected()) {
319 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
320 numSelectedFuelTanks)*(dt*rate) + Fshortage);
325 if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
326 else Engine[e]->SetStarved(false);
329 Weight = EmptyWeight;
330 for (t=0; t<numTanks; t++)
331 Weight += Tank[t]->GetContents();
333 Mass = Weight / GRAVITY;
334 // Calculate new CG here.
337 for (t=0; t<numTanks; t++) {
338 vXYZtank(eX) += Tank[t]->GetX()*Tank[t]->GetContents();
339 vXYZtank(eY) += Tank[t]->GetY()*Tank[t]->GetContents();
340 vXYZtank(eZ) += Tank[t]->GetZ()*Tank[t]->GetContents();
342 Tw += Tank[t]->GetContents();
345 vXYZcg = (vXYZtank + EmptyWeight*vbaseXYZcg) / (Tw + EmptyWeight);
347 // Calculate new moments of inertia here
349 IXXt = IYYt = IZZt = IXZt = 0.0;
350 for (t=0; t<numTanks; t++) {
351 IXXt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetX() - vXYZcg(eX))/12.0)*Tank[t]->GetContents()/GRAVITY;
352 IYYt += ((Tank[t]->GetY()-vXYZcg(eY))/12.0)*((Tank[t]->GetY() - vXYZcg(eY))/12.0)*Tank[t]->GetContents()/GRAVITY;
353 IZZt += ((Tank[t]->GetZ()-vXYZcg(eZ))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
354 IXZt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
357 Ixx = baseIxx + IXXt;
358 Iyy = baseIyy + IYYt;
359 Izz = baseIzz + IZZt;
360 Ixz = baseIxz + IXZt;
364 /******************************************************************************/
366 void FGAircraft::FMAero(void) {
367 static FGColumnVector vDXYZcg(3);
368 static FGColumnVector vAeroBodyForces(3);
369 unsigned int axis_ctr,ctr;
371 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vFs(axis_ctr) = 0.0;
373 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
374 for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
375 vFs(axis_ctr+1) += Coeff[axis_ctr][ctr]->TotalValue();
379 vAeroBodyForces = State->GetTs2b(alpha, beta)*vFs;
380 vForces += vAeroBodyForces;
382 // The d*cg distances below, given in inches, are the distances FROM the c.g.
383 // TO the reference point. Since the c.g. and ref point are given in inches in
384 // the structural system (X positive rearwards) and the body coordinate system
385 // is given with X positive out the nose, the dxcg and dzcg values are
386 // *rotated* 180 degrees about the Y axis.
388 vDXYZcg(eX) = -(vXYZrp(eX) - vXYZcg(eX))/12.0; //cg and rp values are in inches
389 vDXYZcg(eY) = (vXYZrp(eY) - vXYZcg(eY))/12.0;
390 vDXYZcg(eZ) = -(vXYZrp(eZ) - vXYZcg(eZ))/12.0;
392 vMoments(eL) += vAeroBodyForces(eZ)*vDXYZcg(eY) - vAeroBodyForces(eY)*vDXYZcg(eZ); // rolling moment
393 vMoments(eM) += vAeroBodyForces(eX)*vDXYZcg(eZ) - vAeroBodyForces(eZ)*vDXYZcg(eX); // pitching moment
394 vMoments(eN) += vAeroBodyForces(eY)*vDXYZcg(eX) - vAeroBodyForces(eX)*vDXYZcg(eY); // yawing moment
396 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
397 for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
398 vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->TotalValue();
403 /******************************************************************************/
405 void FGAircraft::FMGear(void) {
410 for (unsigned int i=0;i<lGear.size();i++) {
411 vForces += lGear[i]->Force();
412 vMoments += lGear[i]->Moment();
417 /******************************************************************************/
419 void FGAircraft::FMMass(void) {
420 vForces(eX) += -GRAVITY*sin(vEuler(eTht)) * Mass;
421 vForces(eY) += GRAVITY*sin(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
422 vForces(eZ) += GRAVITY*cos(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
425 /******************************************************************************/
427 void FGAircraft::FMProp(void) {
428 for (unsigned int i=0;i<numEngines;i++) {
430 // Changes required here for new engine placement parameters (i.e. location and direction)
432 vForces(eX) += Engine[i]->CalcThrust();
437 /******************************************************************************/
439 void FGAircraft::GetState(void) {
442 alpha = Translation->Getalpha();
443 beta = Translation->Getbeta();
444 vEuler = Rotation->GetEuler();
447 /******************************************************************************/
449 void FGAircraft::ReadMetrics(FGConfigFile* AC_cfg) {
453 AC_cfg->GetNextConfigLine();
455 while ((token = AC_cfg->GetValue()) != "/METRICS") {
456 *AC_cfg >> parameter;
457 if (parameter == "AC_WINGAREA") {
459 cout << " WingArea: " << WingArea << endl;
460 } else if (parameter == "AC_WINGSPAN") {
462 cout << " WingSpan: " << WingSpan << endl;
463 } else if (parameter == "AC_CHORD") {
465 cout << " Chord: " << cbar << endl;
466 } else if (parameter == "AC_IXX") {
468 cout << " baseIxx: " << baseIxx << endl;
469 } else if (parameter == "AC_IYY") {
471 cout << " baseIyy: " << baseIyy << endl;
472 } else if (parameter == "AC_IZZ") {
474 cout << " baseIzz: " << baseIzz << endl;
475 } else if (parameter == "AC_IXZ") {
477 cout << " baseIxz: " << baseIxz << endl;
478 } else if (parameter == "AC_EMPTYWT") {
479 *AC_cfg >> EmptyWeight;
480 cout << " EmptyWeight: " << EmptyWeight << endl;
481 } else if (parameter == "AC_CGLOC") {
482 *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
483 cout << " CG (x, y, z): " << vbaseXYZcg << endl;
484 } else if (parameter == "AC_EYEPTLOC") {
485 *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
486 cout << " Eyepoint (x, y, z): " << vXYZep << endl;
487 } else if (parameter == "AC_AERORP") {
488 *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
489 cout << " Ref Pt (x, y, z): " << vXYZrp << endl;
490 } else if (parameter == "AC_ALPHALIMITS") {
491 *AC_cfg >> alphaclmin >> alphaclmax;
492 cout << " Maximum Alpha: " << alphaclmax
493 << " Minimum Alpha: " << alphaclmin
499 /******************************************************************************/
501 void FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg) {
506 AC_cfg->GetNextConfigLine();
508 while ((token = AC_cfg->GetValue()) != "/PROPULSION") {
509 *AC_cfg >> parameter;
511 if (parameter == "AC_ENGINE") {
513 *AC_cfg >> engine_name;
514 Engine[numEngines] = new FGEngine(FDMExec, EnginePath, engine_name, numEngines);
517 } else if (parameter == "AC_TANK") {
519 Tank[numTanks] = new FGTank(AC_cfg);
520 switch(Tank[numTanks]->GetType()) {
522 numSelectedFuelTanks++;
524 case FGTank::ttOXIDIZER:
525 numSelectedOxiTanks++;
533 /******************************************************************************/
535 void FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg) {
538 FCS->LoadFCS(AC_cfg);
541 /******************************************************************************/
543 void FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg) {
546 AC_cfg->GetNextConfigLine();
548 while ((token = AC_cfg->GetValue()) != "/AERODYNAMICS") {
549 if (token == "AXIS") {
551 axis = AC_cfg->GetValue("NAME");
552 AC_cfg->GetNextConfigLine();
553 while ((token = AC_cfg->GetValue()) != "/AXIS") {
554 ca.push_back(new FGCoefficient(FDMExec, AC_cfg));
555 DisplayCoeffFactors(ca.back()->Getmultipliers());
557 Coeff[AxisIdx[axis]]=ca;
558 AC_cfg->GetNextConfigLine();
563 /******************************************************************************/
565 void FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg) {
568 AC_cfg->GetNextConfigLine();
570 while ((token = AC_cfg->GetValue()) != "/UNDERCARRIAGE") {
571 lGear.push_back(new FGLGear(AC_cfg, FDMExec));
575 /******************************************************************************/
577 void FGAircraft::ReadOutput(FGConfigFile* AC_cfg) {
578 string token, parameter;
582 token = AC_cfg->GetValue("NAME");
583 Output->SetFilename(token);
584 token = AC_cfg->GetValue("TYPE");
585 Output->SetType(token);
586 AC_cfg->GetNextConfigLine();
588 while ((token = AC_cfg->GetValue()) != "/OUTPUT") {
589 *AC_cfg >> parameter;
590 if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
591 if (parameter == "SIMULATION") {
592 *AC_cfg >> parameter;
593 if (parameter == "ON") subsystems += ssSimulation;
595 if (parameter == "AEROSURFACES") {
596 *AC_cfg >> parameter;
597 if (parameter == "ON") subsystems += ssAerosurfaces;
599 if (parameter == "RATES") {
600 *AC_cfg >> parameter;
601 if (parameter == "ON") subsystems += ssRates;
603 if (parameter == "VELOCITIES") {
604 *AC_cfg >> parameter;
605 if (parameter == "ON") subsystems += ssVelocities;
607 if (parameter == "FORCES") {
608 *AC_cfg >> parameter;
609 if (parameter == "ON") subsystems += ssForces;
611 if (parameter == "MOMENTS") {
612 *AC_cfg >> parameter;
613 if (parameter == "ON") subsystems += ssMoments;
615 if (parameter == "ATMOSPHERE") {
616 *AC_cfg >> parameter;
617 if (parameter == "ON") subsystems += ssAtmosphere;
619 if (parameter == "MASSPROPS") {
620 *AC_cfg >> parameter;
621 if (parameter == "ON") subsystems += ssMassProps;
623 if (parameter == "POSITION") {
624 *AC_cfg >> parameter;
625 if (parameter == "ON") subsystems += ssPosition;
627 if (parameter == "COEFFICIENTS") {
628 *AC_cfg >> parameter;
629 if (parameter == "ON") subsystems += ssCoefficients;
631 if (parameter == "GROUND_REACTIONS") {
632 *AC_cfg >> parameter;
633 if (parameter == "ON") subsystems += ssGroundReactions;
637 Output->SetSubsystems(subsystems);
639 OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
640 Output->SetRate( (int)(0.5 + 1.0/(State->Getdt()*OutRate)) );
643 /******************************************************************************/
645 void FGAircraft::ReadPrologue(FGConfigFile* AC_cfg) {
646 string token = AC_cfg->GetValue();
648 AircraftName = AC_cfg->GetValue("NAME");
649 cout << "Reading Aircraft Configuration File: " << AircraftName << endl;
650 scratch=AC_cfg->GetValue("VERSION").c_str();
652 CFGVersion = AC_cfg->GetValue("VERSION");
653 cout << " Version: " << CFGVersion << endl;
654 if (CFGVersion != NEEDED_CFG_VERSION) {
655 cout << endl << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
656 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
657 cout << "Current version needed is: " << NEEDED_CFG_VERSION << endl;
658 cout << " You have version: " << CFGVersion << endl << endl;
665 /******************************************************************************/
667 void FGAircraft::DisplayCoeffFactors(vector <eParam> multipliers) {
668 cout << " Non-Dimensionalized by: ";
670 for (unsigned int i=0; i<multipliers.size();i++)
671 cout << State->paramdef[multipliers[i]];
676 /******************************************************************************/
678 string FGAircraft::GetCoefficientStrings(void) {
679 string CoeffStrings = "";
680 bool firstime = true;
682 for (unsigned int axis = 0; axis < 6; axis++) {
683 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
687 CoeffStrings += ", ";
689 CoeffStrings += Coeff[axis][sd]->Getname();
696 /******************************************************************************/
698 string FGAircraft::GetCoefficientValues(void) {
699 string SDValues = "";
701 bool firstime = true;
703 for (unsigned int axis = 0; axis < 6; axis++) {
704 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
710 sprintf(buffer, "%9.6f", Coeff[axis][sd]->GetSD());
711 SDValues += string(buffer);
719 /******************************************************************************/
721 string FGAircraft::GetGroundReactionStrings(void) {
722 string GroundReactionStrings = "";
723 bool firstime = true;
725 for (unsigned int i=0;i<lGear.size();i++) {
726 if (!firstime) GroundReactionStrings += ", ";
727 GroundReactionStrings += (lGear[i]->GetName() + "_WOW, ");
728 GroundReactionStrings += (lGear[i]->GetName() + "_compressLength, ");
729 GroundReactionStrings += (lGear[i]->GetName() + "_compressSpeed, ");
730 GroundReactionStrings += (lGear[i]->GetName() + "_Force");
735 return GroundReactionStrings;
738 /******************************************************************************/
740 string FGAircraft::GetGroundReactionValues(void) {
742 string GroundReactionValues = "";
744 bool firstime = true;
746 for (unsigned int i=0;i<lGear.size();i++) {
747 if (!firstime) GroundReactionValues += ", ";
748 GroundReactionValues += string( lGear[i]->GetWOW()?"1":"0" ) + ", ";
749 GroundReactionValues += (string(gcvt(lGear[i]->GetCompLen(), 5, buff)) + ", ");
750 GroundReactionValues += (string(gcvt(lGear[i]->GetCompVel(), 6, buff)) + ", ");
751 GroundReactionValues += (string(gcvt(lGear[i]->GetCompForce(), 10, buff)));
756 return GroundReactionValues;