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) {
171 if (Engine != NULL) {
172 for (i=0; i<numEngines; i++)
176 for (i=0; i<numTanks; i++)
179 for (i=0; i<6; i++) {
180 for (j=0; j<Coeff[i].size(); j++) {
187 /******************************************************************************/
189 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname) {
192 string aircraftCfgFileName;
195 AircraftPath = aircraft_path;
196 EnginePath = engine_path;
199 aircraftCfgFileName = AircraftPath + "/" + fname + "/" + fname + ".xml";
201 aircraftCfgFileName = AircraftPath + ";" + fname + ";" + fname + ".xml";
204 FGConfigFile AC_cfg(aircraftCfgFileName);
205 if (!AC_cfg.IsOpen()) return false;
207 ReadPrologue(&AC_cfg);
209 while ((AC_cfg.GetNextConfigLine() != "EOF") &&
210 (token = AC_cfg.GetValue()) != "/FDM_CONFIG") {
211 if (token == "METRICS") {
212 cout << " Reading Metrics" << endl;
213 ReadMetrics(&AC_cfg);
214 } else if (token == "AERODYNAMICS") {
215 cout << " Reading Aerodynamics" << endl;
216 ReadAerodynamics(&AC_cfg);
217 } else if (token == "UNDERCARRIAGE") {
218 cout << " Reading Landing Gear" << endl;
219 ReadUndercarriage(&AC_cfg);
220 } else if (token == "PROPULSION") {
221 cout << " Reading Propulsion" << endl;
222 ReadPropulsion(&AC_cfg);
223 } else if (token == "FLIGHT_CONTROL") {
224 cout << " Reading Flight Control" << endl;
225 ReadFlightControls(&AC_cfg);
226 } else if (token == "OUTPUT") {
234 /******************************************************************************/
236 bool FGAircraft::Run(void) {
237 if (!FGModel::Run()) { // if false then execute this Run()
240 for (int i = 1; i <= 3; i++) vForces(i) = vMoments(i) = 0.0;
249 if (fabs(Position->GetGamma()) < 1.57) {
250 nlf = vFs(eZ)/(Weight*cos(Position->GetGamma()));
253 } else { // skip Run() execution this time
260 /******************************************************************************/
262 void FGAircraft::MassChange() {
263 static FGColumnVector vXYZtank(3);
265 float IXXt, IYYt, IZZt, IXZt;
267 unsigned int axis_ctr;
269 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vXYZtank(axis_ctr) = 0.0;
271 // UPDATE TANK CONTENTS
273 // For each engine, cycle through the tanks and draw an equal amount of
274 // fuel (or oxidizer) from each active tank. The needed amount of fuel is
275 // determined by the engine in the FGEngine class. If more fuel is needed
276 // than is available in the tank, then that amount is considered a shortage,
277 // and will be drawn from the next tank. If the engine cannot be fed what it
278 // needs, it will be considered to be starved, and will shut down.
280 float Oshortage, Fshortage;
282 for (unsigned int e=0; e<numEngines; e++) {
283 Fshortage = Oshortage = 0.0;
284 for (t=0; t<numTanks; t++) {
285 switch(Engine[e]->GetType()) {
286 case FGEngine::etRocket:
288 switch(Tank[t]->GetType()) {
290 if (Tank[t]->GetSelected()) {
291 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
292 numSelectedFuelTanks)*(dt*rate) + Fshortage);
295 case FGTank::ttOXIDIZER:
296 if (Tank[t]->GetSelected()) {
297 Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
298 numSelectedOxiTanks)*(dt*rate) + Oshortage);
304 case FGEngine::etPiston:
305 case FGEngine::etTurboJet:
306 case FGEngine::etTurboProp:
308 if (Tank[t]->GetSelected()) {
309 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
310 numSelectedFuelTanks)*(dt*rate) + Fshortage);
315 if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
316 else Engine[e]->SetStarved(false);
319 Weight = EmptyWeight;
320 for (t=0; t<numTanks; t++)
321 Weight += Tank[t]->GetContents();
323 Mass = Weight / GRAVITY;
324 // Calculate new CG here.
327 for (t=0; t<numTanks; t++) {
328 vXYZtank(eX) += Tank[t]->GetX()*Tank[t]->GetContents();
329 vXYZtank(eY) += Tank[t]->GetY()*Tank[t]->GetContents();
330 vXYZtank(eZ) += Tank[t]->GetZ()*Tank[t]->GetContents();
332 Tw += Tank[t]->GetContents();
335 vXYZcg = (vXYZtank + EmptyWeight*vbaseXYZcg) / (Tw + EmptyWeight);
337 // Calculate new moments of inertia here
339 IXXt = IYYt = IZZt = IXZt = 0.0;
340 for (t=0; t<numTanks; t++) {
341 IXXt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetX() - vXYZcg(eX))/12.0)*Tank[t]->GetContents()/GRAVITY;
342 IYYt += ((Tank[t]->GetY()-vXYZcg(eY))/12.0)*((Tank[t]->GetY() - vXYZcg(eY))/12.0)*Tank[t]->GetContents()/GRAVITY;
343 IZZt += ((Tank[t]->GetZ()-vXYZcg(eZ))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
344 IXZt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
347 Ixx = baseIxx + IXXt;
348 Iyy = baseIyy + IYYt;
349 Izz = baseIzz + IZZt;
350 Ixz = baseIxz + IXZt;
354 /******************************************************************************/
356 void FGAircraft::FMAero(void) {
357 static FGColumnVector vDXYZcg(3);
358 static FGColumnVector vAeroBodyForces(3);
359 unsigned int axis_ctr,ctr;
361 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vFs(axis_ctr) = 0.0;
363 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
364 for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
365 vFs(axis_ctr+1) += Coeff[axis_ctr][ctr]->TotalValue();
369 vAeroBodyForces = State->GetTs2b(alpha, beta)*vFs;
370 vForces += vAeroBodyForces;
372 // The d*cg distances below, given in inches, are the distances FROM the c.g.
373 // TO the reference point. Since the c.g. and ref point are given in inches in
374 // the structural system (X positive rearwards) and the body coordinate system
375 // is given with X positive out the nose, the dxcg and dzcg values are
376 // *rotated* 180 degrees about the Y axis.
378 vDXYZcg(eX) = -(vXYZrp(eX) - vXYZcg(eX))/12.0; //cg and rp values are in inches
379 vDXYZcg(eY) = (vXYZrp(eY) - vXYZcg(eY))/12.0;
380 vDXYZcg(eZ) = -(vXYZrp(eZ) - vXYZcg(eZ))/12.0;
382 vMoments(eL) += vAeroBodyForces(eZ)*vDXYZcg(eY) - vAeroBodyForces(eY)*vDXYZcg(eZ); // rolling moment
383 vMoments(eM) += vAeroBodyForces(eX)*vDXYZcg(eZ) - vAeroBodyForces(eZ)*vDXYZcg(eX); // pitching moment
384 vMoments(eN) += vAeroBodyForces(eY)*vDXYZcg(eX) - vAeroBodyForces(eX)*vDXYZcg(eY); // yawing moment
386 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
387 for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
388 vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->TotalValue();
393 /******************************************************************************/
395 void FGAircraft::FMGear(void) {
398 vector <FGLGear>::iterator iGear = lGear.begin();
399 while (iGear != lGear.end()) {
400 vForces += iGear->Force();
401 vMoments += iGear->Moment();
409 /******************************************************************************/
411 void FGAircraft::FMMass(void) {
412 vForces(eX) += -GRAVITY*sin(vEuler(eTht)) * Mass;
413 vForces(eY) += GRAVITY*sin(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
414 vForces(eZ) += GRAVITY*cos(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
417 /******************************************************************************/
419 void FGAircraft::FMProp(void) {
420 for (unsigned int i=0;i<numEngines;i++) {
422 // Changes required here for new engine placement parameters (i.e. location and direction)
424 vForces(eX) += Engine[i]->CalcThrust();
429 /******************************************************************************/
431 void FGAircraft::GetState(void) {
434 alpha = Translation->Getalpha();
435 beta = Translation->Getbeta();
436 vEuler = Rotation->GetEuler();
439 /******************************************************************************/
441 void FGAircraft::ReadMetrics(FGConfigFile* AC_cfg) {
445 AC_cfg->GetNextConfigLine();
447 while ((token = AC_cfg->GetValue()) != "/METRICS") {
448 *AC_cfg >> parameter;
449 if (parameter == "AC_WINGAREA") {
451 cout << " WingArea: " << WingArea << endl;
452 } else if (parameter == "AC_WINGSPAN") {
454 cout << " WingSpan: " << WingSpan << endl;
455 } else if (parameter == "AC_CHORD") {
457 cout << " Chord: " << cbar << endl;
458 } else if (parameter == "AC_IXX") {
460 cout << " baseIxx: " << baseIxx << endl;
461 } else if (parameter == "AC_IYY") {
463 cout << " baseIyy: " << baseIyy << endl;
464 } else if (parameter == "AC_IZZ") {
466 cout << " baseIzz: " << baseIzz << endl;
467 } else if (parameter == "AC_IXZ") {
469 cout << " baseIxz: " << baseIxz << endl;
470 } else if (parameter == "AC_EMPTYWT") {
471 *AC_cfg >> EmptyWeight;
472 cout << " EmptyWeight: " << EmptyWeight << endl;
473 } else if (parameter == "AC_CGLOC") {
474 *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
475 cout << " CG (x, y, z): " << vbaseXYZcg << endl;
476 } else if (parameter == "AC_EYEPTLOC") {
477 *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
478 cout << " Eyepoint (x, y, z): " << vXYZep << endl;
479 } else if (parameter == "AC_AERORP") {
480 *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
481 cout << " Ref Pt (x, y, z): " << vXYZrp << endl;
482 } else if (parameter == "AC_ALPHALIMITS") {
483 *AC_cfg >> alphaclmin >> alphaclmax;
484 cout << " Maximum Alpha: " << alphaclmax
485 << " Minimum Alpha: " << alphaclmin
491 /******************************************************************************/
493 void FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg) {
498 AC_cfg->GetNextConfigLine();
500 while ((token = AC_cfg->GetValue()) != "/PROPULSION") {
501 *AC_cfg >> parameter;
503 if (parameter == "AC_ENGINE") {
505 *AC_cfg >> engine_name;
506 Engine[numEngines] = new FGEngine(FDMExec, EnginePath, engine_name, numEngines);
509 } else if (parameter == "AC_TANK") {
511 Tank[numTanks] = new FGTank(AC_cfg);
512 switch(Tank[numTanks]->GetType()) {
514 numSelectedFuelTanks++;
516 case FGTank::ttOXIDIZER:
517 numSelectedOxiTanks++;
525 /******************************************************************************/
527 void FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg) {
530 FCS->LoadFCS(AC_cfg);
533 /******************************************************************************/
535 void FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg) {
538 AC_cfg->GetNextConfigLine();
540 while ((token = AC_cfg->GetValue()) != "/AERODYNAMICS") {
541 if (token == "AXIS") {
543 axis = AC_cfg->GetValue("NAME");
544 AC_cfg->GetNextConfigLine();
545 while ((token = AC_cfg->GetValue()) != "/AXIS") {
546 ca.push_back(new FGCoefficient(FDMExec, AC_cfg));
547 DisplayCoeffFactors(ca.back()->Getmultipliers());
549 Coeff[AxisIdx[axis]]=ca;
550 AC_cfg->GetNextConfigLine();
555 /******************************************************************************/
557 void FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg) {
560 AC_cfg->GetNextConfigLine();
562 while ((token = AC_cfg->GetValue()) != "/UNDERCARRIAGE") {
563 lGear.push_back(FGLGear(AC_cfg, FDMExec));
567 /******************************************************************************/
569 void FGAircraft::ReadOutput(FGConfigFile* AC_cfg) {
570 string token, parameter;
574 token = AC_cfg->GetValue("NAME");
575 Output->SetFilename(token);
576 token = AC_cfg->GetValue("TYPE");
577 Output->SetType(token);
578 AC_cfg->GetNextConfigLine();
580 while ((token = AC_cfg->GetValue()) != "/OUTPUT") {
581 *AC_cfg >> parameter;
582 if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
583 if (parameter == "SIMULATION") {
584 *AC_cfg >> parameter;
585 if (parameter == "ON") subsystems += ssSimulation;
587 if (parameter == "AEROSURFACES") {
588 *AC_cfg >> parameter;
589 if (parameter == "ON") subsystems += ssAerosurfaces;
591 if (parameter == "RATES") {
592 *AC_cfg >> parameter;
593 if (parameter == "ON") subsystems += ssRates;
595 if (parameter == "VELOCITIES") {
596 *AC_cfg >> parameter;
597 if (parameter == "ON") subsystems += ssVelocities;
599 if (parameter == "FORCES") {
600 *AC_cfg >> parameter;
601 if (parameter == "ON") subsystems += ssForces;
603 if (parameter == "MOMENTS") {
604 *AC_cfg >> parameter;
605 if (parameter == "ON") subsystems += ssMoments;
607 if (parameter == "ATMOSPHERE") {
608 *AC_cfg >> parameter;
609 if (parameter == "ON") subsystems += ssAtmosphere;
611 if (parameter == "MASSPROPS") {
612 *AC_cfg >> parameter;
613 if (parameter == "ON") subsystems += ssMassProps;
615 if (parameter == "POSITION") {
616 *AC_cfg >> parameter;
617 if (parameter == "ON") subsystems += ssPosition;
619 if (parameter == "COEFFICIENTS") {
620 *AC_cfg >> parameter;
621 if (parameter == "ON") subsystems += ssCoefficients;
623 if (parameter == "GROUND_REACTIONS") {
624 *AC_cfg >> parameter;
625 if (parameter == "ON") subsystems += ssGroundReactions;
629 Output->SetSubsystems(subsystems);
631 OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
632 Output->SetRate( (int)(0.5 + 1.0/(State->Getdt()*OutRate)) );
635 /******************************************************************************/
637 void FGAircraft::ReadPrologue(FGConfigFile* AC_cfg) {
638 string token = AC_cfg->GetValue();
640 AircraftName = AC_cfg->GetValue("NAME");
641 cout << "Reading Aircraft Configuration File: " << AircraftName << endl;
642 scratch=AC_cfg->GetValue("VERSION").c_str();
644 CFGVersion = AC_cfg->GetValue("VERSION");
645 cout << " Version: " << CFGVersion << endl;
646 if (CFGVersion != NEEDED_CFG_VERSION) {
647 cout << endl << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
648 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
649 cout << "Current version needed is: " << NEEDED_CFG_VERSION << endl;
650 cout << " You have version: " << CFGVersion << endl << endl;
657 /******************************************************************************/
659 void FGAircraft::DisplayCoeffFactors(vector <eParam> multipliers) {
660 cout << " Non-Dimensionalized by: ";
662 for (unsigned int i=0; i<multipliers.size();i++)
663 cout << State->paramdef[multipliers[i]];
668 /******************************************************************************/
670 string FGAircraft::GetCoefficientStrings(void) {
671 string CoeffStrings = "";
672 bool firstime = true;
674 for (unsigned int axis = 0; axis < 6; axis++) {
675 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
679 CoeffStrings += ", ";
681 CoeffStrings += Coeff[axis][sd]->Getname();
688 /******************************************************************************/
690 string FGAircraft::GetCoefficientValues(void) {
691 string SDValues = "";
693 bool firstime = true;
695 for (unsigned int axis = 0; axis < 6; axis++) {
696 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
702 sprintf(buffer, "%9.6f", Coeff[axis][sd]->GetSD());
703 SDValues += string(buffer);
711 /******************************************************************************/
713 string FGAircraft::GetGroundReactionStrings(void) {
714 string GroundReactionStrings = "";
715 bool firstime = true;
717 for (unsigned int i=0;i<lGear.size();i++) {
718 if (!firstime) GroundReactionStrings += ", ";
719 GroundReactionStrings += (lGear[i].GetName() + "_WOW, ");
720 GroundReactionStrings += (lGear[i].GetName() + "_compressLength, ");
721 GroundReactionStrings += (lGear[i].GetName() + "_compressSpeed, ");
722 GroundReactionStrings += (lGear[i].GetName() + "_Force");
727 return GroundReactionStrings;
730 /******************************************************************************/
732 string FGAircraft::GetGroundReactionValues(void) {
734 string GroundReactionValues = "";
736 bool firstime = true;
738 for (unsigned int i=0;i<lGear.size();i++) {
739 if (!firstime) GroundReactionValues += ", ";
740 GroundReactionValues += string( lGear[i].GetWOW()?"1":"0" ) + ", ";
741 GroundReactionValues += (string(gcvt(lGear[i].GetCompLen(), 5, buff)) + ", ");
742 GroundReactionValues += (string(gcvt(lGear[i].GetCompVel(), 6, buff)) + ", ");
743 GroundReactionValues += (string(gcvt(lGear[i].GetCompForce(), 10, buff)));
748 return GroundReactionValues;