]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAircraft.cpp
Patch from Melchior Franz:
[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. 
32  
33 HISTORY
34 --------------------------------------------------------------------------------
35 12/12/98   JSB   Created
36 04/03/99   JSB   Changed Aero() method to correct body axis force calculation
37                  from wind vector. Fix provided by Tony Peden.
38 05/03/99   JSB   Changed (for the better?) the way configurations are read in.
39 9/17/99     TP   Combined force and moment functions. Added aero reference 
40                  point to config file. Added calculations for moments due to 
41                  difference in cg and aero reference point
42  
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 COMMENTS, REFERENCES,  and NOTES
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46  
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 INCLUDES
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 #include <sys/stat.h>
52 #include <sys/types.h>
53
54 #ifdef FGFS
55 #  ifndef __BORLANDC__
56 #    include <simgear/compiler.h>
57 #  endif
58 #  ifdef SG_HAVE_STD_INCLUDES
59 #    include <cmath>
60 #  else
61 #    include <math.h>
62 #  endif
63 #else
64 #  if defined (sgi) && !defined(__GNUC__)
65 #    include <math.h>
66 #  else
67 #    include <cmath>
68 #  endif
69 #endif
70
71 #include "FGAircraft.h"
72 #include "FGMassBalance.h"
73 #include "FGInertial.h"
74 #include "FGGroundReactions.h"
75 #include "FGAerodynamics.h"
76 #include "FGTranslation.h"
77 #include "FGRotation.h"
78 #include "FGAtmosphere.h"
79 #include "FGState.h"
80 #include "FGFDMExec.h"
81 #include "FGFCS.h"
82 #include "FGPosition.h"
83 #include "FGAuxiliary.h"
84 #include "FGOutput.h"
85 #include "FGPropertyManager.h"
86
87 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 DEFINITIONS
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
90
91 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 GLOBAL DATA
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
94
95 static const char *IdSrc = "$Id$";
96 static const char *IdHdr = ID_AIRCRAFT;
97
98 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 CLASS IMPLEMENTATION
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
101
102 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex)
103 {
104   Name = "FGAircraft";
105   WingIncidence   = 0.0;
106   impending_stall = stall_hyst = 0.0;
107   alphaclmin = alphaclmax = 0.0;
108   alphahystmin = alphahystmax = 0.0;
109   HTailArea = VTailArea = 0.0;
110   HTailArm  = VTailArm  = 0.0;
111   lbarh = lbarv = 0.0;
112   vbarh = vbarv = 0.0;
113   bi2vel = ci2vel = 0.0;
114   alphaw = 0.0;
115
116   bind();
117
118   Debug(0);
119 }
120
121 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122
123 FGAircraft::~FGAircraft()
124 {
125   unbind();
126   Debug(1);
127 }
128
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
131 bool FGAircraft::Run(void)
132 {
133   double twovel;
134   double alpha;
135   
136   if (!FGModel::Run()) {                 // if false then execute this Run()
137     vForces.InitMatrix();
138     vForces += Aerodynamics->GetForces();
139     vForces += Inertial->GetForces();
140     vForces += Propulsion->GetForces();
141     vForces += GroundReactions->GetForces();
142
143     vMoments.InitMatrix();
144     vMoments += Aerodynamics->GetMoments();
145     vMoments += Propulsion->GetMoments();
146     vMoments += GroundReactions->GetMoments();
147     
148     vBodyAccel = vForces/MassBalance->GetMass();
149     
150     vNcg = vBodyAccel/Inertial->gravity();
151
152     vNwcg = State->GetTb2s() * vNcg;
153     vNwcg(3) = -1*vNwcg(3) + 1;
154     
155     twovel = 2*Translation->GetVt();
156     if (twovel > 0) {
157       bi2vel = WingSpan / twovel;
158       ci2vel = cbar / twovel;
159     }  
160     
161     alphaw = Translation->Getalpha() + WingIncidence;
162     
163     alpha=Translation->Getalpha();
164     
165     if (alphaclmax != 0) {
166       if (alpha > 0.85*alphaclmax) {
167         impending_stall = 10*(alpha/alphaclmax - 0.85);
168       } else {
169         impending_stall = 0;
170       }
171           
172     }   
173     if(alphahystmax != 0.0 && alphahystmin != 0.0) {
174           if( alpha > alphahystmax ) {
175             stall_hyst = 1;
176           } else if(alpha < alphahystmin) {
177             stall_hyst = 0;
178           }    
179     }
180
181      
182     
183     return false;
184   } else {                               // skip Run() execution this time
185     return true;
186   }
187 }
188
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190
191 float FGAircraft::GetNlf(void)
192 {
193   return -1*Aerodynamics->GetvFs(3)/MassBalance->GetWeight();
194 }
195
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197
198 bool FGAircraft::Load(FGConfigFile* AC_cfg)
199 {
200   string token = "";
201   string parameter;
202   double EW, bixx, biyy, bizz, bixy, bixz;
203   double pmWt, pmX, pmY, pmZ;
204   FGColumnVector3 vbaseXYZcg;
205
206   AC_cfg->GetNextConfigLine();
207
208   while ((token = AC_cfg->GetValue()) != string("/METRICS")) {
209     *AC_cfg >> parameter;
210     if (parameter == "AC_WINGAREA") {
211       *AC_cfg >> WingArea;
212       if (debug_lvl > 0) cout << "    WingArea: " << WingArea  << endl;
213     } else if (parameter == "AC_WINGSPAN") {
214       *AC_cfg >> WingSpan;
215       if (debug_lvl > 0) cout << "    WingSpan: " << WingSpan  << endl;
216     } else if (parameter == "AC_WINGINCIDENCE") {
217       *AC_cfg >> WingIncidence;
218       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
219     } else if (parameter == "AC_CHORD") {
220       *AC_cfg >> cbar;
221       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
222     } else if (parameter == "AC_HTAILAREA") {
223       *AC_cfg >> HTailArea;
224       if (debug_lvl > 0) cout << "    H. Tail Area: " << HTailArea << endl;
225     } else if (parameter == "AC_HTAILARM") {
226       *AC_cfg >> HTailArm;
227       if (debug_lvl > 0) cout << "    H. Tail Arm: " << HTailArm << endl;
228     } else if (parameter == "AC_VTAILAREA") {
229       *AC_cfg >> VTailArea;
230       if (debug_lvl > 0) cout << "    V. Tail Area: " << VTailArea << endl;
231     } else if (parameter == "AC_VTAILARM") {
232       *AC_cfg >> VTailArm;
233       if (debug_lvl > 0) cout << "    V. Tail Arm: " << VTailArm << endl;
234     } else if (parameter == "AC_IXX") {
235       *AC_cfg >> bixx;
236       if (debug_lvl > 0) cout << "    baseIxx: " << bixx << endl;
237       MassBalance->SetBaseIxx(bixx);
238     } else if (parameter == "AC_IYY") {
239       *AC_cfg >> biyy;
240       if (debug_lvl > 0) cout << "    baseIyy: " << biyy << endl;
241       MassBalance->SetBaseIyy(biyy);
242     } else if (parameter == "AC_IZZ") {
243       *AC_cfg >> bizz;
244       if (debug_lvl > 0) cout << "    baseIzz: " << bizz << endl;
245       MassBalance->SetBaseIzz(bizz);
246     } else if (parameter == "AC_IXY") {
247       *AC_cfg >> bixy;
248       if (debug_lvl > 0) cout << "    baseIxy: " << bixy  << endl;
249       MassBalance->SetBaseIxy(bixy);
250     } else if (parameter == "AC_IXZ") {
251       *AC_cfg >> bixz;
252       if (debug_lvl > 0) cout << "    baseIxz: " << bixz  << endl;
253       MassBalance->SetBaseIxz(bixz);
254     } else if (parameter == "AC_EMPTYWT") {
255       *AC_cfg >> EW;
256       MassBalance->SetEmptyWeight(EW);
257       if (debug_lvl > 0) cout << "    EmptyWeight: " << EW  << endl;
258     } else if (parameter == "AC_CGLOC") {
259       *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
260       MassBalance->SetBaseCG(vbaseXYZcg);
261       if (debug_lvl > 0) cout << "    CG (x, y, z): " << vbaseXYZcg << endl;
262     } else if (parameter == "AC_EYEPTLOC") {
263       *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
264       if (debug_lvl > 0) cout << "    Eyepoint (x, y, z): " << vXYZep << endl;
265     } else if (parameter == "AC_AERORP") {
266       *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
267       if (debug_lvl > 0) cout << "    Ref Pt (x, y, z): " << vXYZrp << endl;
268     } else if (parameter == "AC_ALPHALIMITS") {
269       *AC_cfg >> alphaclmin >> alphaclmax;
270       if (debug_lvl > 0) cout << "    Maximum Alpha: " << alphaclmax
271              << "    Minimum Alpha: " << alphaclmin
272              << endl;
273     } else if (parameter == "AC_HYSTLIMITS") {
274       *AC_cfg >> alphahystmin >> alphahystmax;
275       if (debug_lvl > 0) cout << "    Hysteresis Start: " << alphahystmax
276              << "    Hysteresis End: " << alphahystmin
277              << endl;
278     } else if (parameter == "AC_POINTMASS") {
279       *AC_cfg >> pmWt >> pmX >> pmY >> pmZ;
280       MassBalance->AddPointMass(pmWt, pmX, pmY, pmZ);
281       if (debug_lvl > 0) cout << "    Point Mass Object: " << pmWt << " lbs. at "
282                          << "X, Y, Z (in.): " << pmX << "  " << pmY << "  " << pmZ
283                          << endl;
284     }
285   }
286   
287   // calculate some derived parameters
288   if (cbar != 0.0) {
289     lbarh = HTailArm/cbar;
290     lbarv = VTailArm/cbar;
291     if (WingArea != 0.0) {
292       vbarh = HTailArm*HTailArea / (cbar*WingArea);
293       vbarv = VTailArm*VTailArea / (cbar*WingArea);
294     }
295   }     
296   return true;
297 }
298
299 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300
301 void FGAircraft::bind(void)
302 {
303   typedef double (FGAircraft::*PMF)(int) const;
304   PropertyManager->Tie("metrics/Sw-sqft", this,
305                        &FGAircraft::GetWingArea);
306   PropertyManager->Tie("metrics/bw-ft", this,
307                        &FGAircraft::GetWingSpan);
308   PropertyManager->Tie("metrics/cbarw-ft", this,
309                        &FGAircraft::Getcbar);
310   PropertyManager->Tie("metrics/iw-deg", this,
311                        &FGAircraft::GetWingIncidence);
312   PropertyManager->Tie("metrics/Sh-sqft", this,
313                        &FGAircraft::GetHTailArea);
314   PropertyManager->Tie("metrics/lh-ft", this,
315                        &FGAircraft::GetHTailArm);
316   PropertyManager->Tie("metrics/Sv-sqft", this,
317                        &FGAircraft::GetVTailArea);
318   PropertyManager->Tie("metrics/lv-ft", this,
319                        &FGAircraft::GetVTailArm);
320   PropertyManager->Tie("metrics/lh-norm", this,
321                        &FGAircraft::Getlbarh);
322   PropertyManager->Tie("metrics/lv-norm", this,
323                        &FGAircraft::Getlbarv);
324   PropertyManager->Tie("metrics/vbarh-norm", this,
325                        &FGAircraft::Getvbarh);
326   PropertyManager->Tie("metrics/vbarv-norm", this,
327                        &FGAircraft::Getvbarv);
328   PropertyManager->Tie("moments/l-total-lbsft", this,1,
329                        (PMF)&FGAircraft::GetMoments);
330   PropertyManager->Tie("moments/m-total-lbsft", this,2,
331                        (PMF)&FGAircraft::GetMoments);
332   PropertyManager->Tie("moments/n-total-lbsft", this,3,
333                        (PMF)&FGAircraft::GetMoments);
334   PropertyManager->Tie("forces/fbx-total-lbs", this,1,
335                        (PMF)&FGAircraft::GetForces);
336   PropertyManager->Tie("forces/fby-total-lbs", this,2,
337                        (PMF)&FGAircraft::GetForces);
338   PropertyManager->Tie("forces/fbz-total-lbs", this,3,
339                        (PMF)&FGAircraft::GetForces);
340   PropertyManager->Tie("metrics/aero-rp-x-ft", this,1,
341                        (PMF)&FGAircraft::GetXYZrp);
342   PropertyManager->Tie("metrics/aero-rp-y-ft", this,2,
343                        (PMF)&FGAircraft::GetXYZrp);
344   PropertyManager->Tie("metrics/aero-rp-z-ft", this,3,
345                        (PMF)&FGAircraft::GetXYZrp);
346   PropertyManager->Tie("metrics/eyepoint-x-ft", this,1,
347                        (PMF)&FGAircraft::GetXYZep);
348   PropertyManager->Tie("metrics/eyepoint-y-ft", this,2,
349                        (PMF)&FGAircraft::GetXYZep);
350   PropertyManager->Tie("metrics/eyepoint-z-ft", this,3,
351                        (PMF)&FGAircraft::GetXYZep);
352   PropertyManager->Tie("metrics/alpha-max-deg", this,
353                        &FGAircraft::GetAlphaCLMax,
354                        &FGAircraft::SetAlphaCLMax,
355                        true);
356   PropertyManager->Tie("metrics/alpha-min-deg", this,
357                        &FGAircraft::GetAlphaCLMin,
358                        &FGAircraft::SetAlphaCLMin,
359                        true);
360   PropertyManager->Tie("aero/bi2vel", this,
361                        &FGAircraft::GetBI2Vel);
362   PropertyManager->Tie("aero/ci2vel", this,
363                        &FGAircraft::GetCI2Vel);
364   PropertyManager->Tie("aero/alpha-wing-rad", this,
365                        &FGAircraft::GetAlphaW);
366   PropertyManager->Tie("systems/stall-warn-norm", this,
367                         &FGAircraft::GetStallWarn);
368   PropertyManager->Tie("aero/stall-hyst-norm", this,
369                         &FGAircraft::GetHysteresisParm);
370                         
371 }
372
373 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374
375 void FGAircraft::unbind(void)
376 {
377   PropertyManager->Untie("metrics/Sw-sqft");
378   PropertyManager->Untie("metrics/bw-ft");
379   PropertyManager->Untie("metrics/cbarw-ft");
380   PropertyManager->Untie("metrics/iw-deg");
381   PropertyManager->Untie("metrics/Sh-sqft");
382   PropertyManager->Untie("metrics/lh-ft");
383   PropertyManager->Untie("metrics/Sv-sqft");
384   PropertyManager->Untie("metrics/lv-ft");
385   PropertyManager->Untie("metrics/lh-norm");
386   PropertyManager->Untie("metrics/lv-norm");
387   PropertyManager->Untie("metrics/vbarh-norm");
388   PropertyManager->Untie("metrics/vbarv-norm");
389   PropertyManager->Untie("moments/l-total-lbsft");
390   PropertyManager->Untie("moments/m-total-lbsft");
391   PropertyManager->Untie("moments/n-total-lbsft");
392   PropertyManager->Untie("forces/fbx-total-lbs");
393   PropertyManager->Untie("forces/fby-total-lbs");
394   PropertyManager->Untie("forces/fbz-total-lbs");
395   PropertyManager->Untie("metrics/aero-rp-x-ft");
396   PropertyManager->Untie("metrics/aero-rp-y-ft");
397   PropertyManager->Untie("metrics/aero-rp-z-ft");
398   PropertyManager->Untie("metrics/eyepoint-x-ft");
399   PropertyManager->Untie("metrics/eyepoint-y-ft");
400   PropertyManager->Untie("metrics/eyepoint-z-ft");
401   PropertyManager->Untie("metrics/alpha-max-deg");
402   PropertyManager->Untie("metrics/alpha-min-deg");
403   PropertyManager->Untie("aero/bi2vel");
404   PropertyManager->Untie("aero/ci2vel");
405   PropertyManager->Untie("aero/alpha-wing-rad");
406   PropertyManager->Untie("systems/stall-warn-norm");
407 }
408
409 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 //    The bitmasked value choices are as follows:
411 //    unset: In this case (the default) JSBSim would only print
412 //       out the normally expected messages, essentially echoing
413 //       the config files as they are read. If the environment
414 //       variable is not set, debug_lvl is set to 1 internally
415 //    0: This requests JSBSim not to output any messages
416 //       whatsoever.
417 //    1: This value explicity requests the normal JSBSim
418 //       startup messages
419 //    2: This value asks for a message to be printed out when
420 //       a class is instantiated
421 //    4: When this value is set, a message is displayed when a
422 //       FGModel object executes its Run() method
423 //    8: When this value is set, various runtime state variables
424 //       are printed out periodically
425 //    16: When set various parameters are sanity checked and
426 //       a message is printed out when they go out of bounds
427
428 void FGAircraft::Debug(int from)
429 {
430   if (debug_lvl <= 0) return;
431
432   if (debug_lvl & 1) { // Standard console startup message output
433     if (from == 0) { // Constructor
434     }
435   }
436   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
437     if (from == 0) cout << "Instantiated: FGAircraft" << endl;
438     if (from == 1) cout << "Destroyed:    FGAircraft" << endl;
439   }
440   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
441   }
442   if (debug_lvl & 8 ) { // Runtime state variables
443   }
444   if (debug_lvl & 16) { // Sanity checking
445   }
446   if (debug_lvl & 64) {
447     if (from == 0) { // Constructor
448       cout << IdSrc << endl;
449       cout << IdHdr << endl;
450     }
451   }
452 }
453