]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAircraft.cpp
JSBSim updates. This update changes the file format, so an update of the base
[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   HTailArea = VTailArea = 0.0;
106   HTailArm  = VTailArm  = 0.0;
107   lbarh = lbarv = 0.0;
108   vbarh = vbarv = 0.0;
109   WingIncidence = 0.0;
110
111   bind();
112
113   Debug(0);
114 }
115
116 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117
118 FGAircraft::~FGAircraft()
119 {
120   unbind();
121   Debug(1);
122 }
123
124 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125
126 bool FGAircraft::Run(void)
127 {
128   if (!FGModel::Run()) {                 // if false then execute this Run()
129     vForces.InitMatrix();
130     vForces += Aerodynamics->GetForces();
131     vForces += Inertial->GetForces();
132     vForces += Propulsion->GetForces();
133     vForces += GroundReactions->GetForces();
134
135     vMoments.InitMatrix();
136     vMoments += Aerodynamics->GetMoments();
137     vMoments += Propulsion->GetMoments();
138     vMoments += GroundReactions->GetMoments();
139     
140     vBodyAccel = vForces/MassBalance->GetMass();
141     
142     vNcg = vBodyAccel/Inertial->gravity();
143
144     vNwcg = State->GetTb2s() * vNcg;
145     vNwcg(3) = -1*vNwcg(3) + 1;
146     
147     return false;
148   } else {                               // skip Run() execution this time
149     return true;
150   }
151 }
152
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154
155 float FGAircraft::GetNlf(void)
156 {
157   return -1*Aerodynamics->GetvFs(3)/MassBalance->GetWeight();
158 }
159
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162 bool FGAircraft::Load(FGConfigFile* AC_cfg)
163 {
164   string token = "";
165   string parameter;
166   double EW, bixx, biyy, bizz, bixy, bixz;
167   double pmWt, pmX, pmY, pmZ;
168   FGColumnVector3 vbaseXYZcg;
169
170   AC_cfg->GetNextConfigLine();
171
172   while ((token = AC_cfg->GetValue()) != string("/METRICS")) {
173     *AC_cfg >> parameter;
174     if (parameter == "AC_WINGAREA") {
175       *AC_cfg >> WingArea;
176       if (debug_lvl > 0) cout << "    WingArea: " << WingArea  << endl;
177     } else if (parameter == "AC_WINGSPAN") {
178       *AC_cfg >> WingSpan;
179       if (debug_lvl > 0) cout << "    WingSpan: " << WingSpan  << endl;
180     } else if (parameter == "AC_WINGINCIDENCE") {
181       *AC_cfg >> WingIncidence;
182       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
183     } else if (parameter == "AC_CHORD") {
184       *AC_cfg >> cbar;
185       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
186     } else if (parameter == "AC_HTAILAREA") {
187       *AC_cfg >> HTailArea;
188       if (debug_lvl > 0) cout << "    H. Tail Area: " << HTailArea << endl;
189     } else if (parameter == "AC_HTAILARM") {
190       *AC_cfg >> HTailArm;
191       if (debug_lvl > 0) cout << "    H. Tail Arm: " << HTailArm << endl;
192     } else if (parameter == "AC_VTAILAREA") {
193       *AC_cfg >> VTailArea;
194       if (debug_lvl > 0) cout << "    V. Tail Area: " << VTailArea << endl;
195     } else if (parameter == "AC_VTAILARM") {
196       *AC_cfg >> VTailArm;
197       if (debug_lvl > 0) cout << "    V. Tail Arm: " << VTailArm << endl;
198     } else if (parameter == "AC_IXX") {
199       *AC_cfg >> bixx;
200       if (debug_lvl > 0) cout << "    baseIxx: " << bixx << endl;
201       MassBalance->SetBaseIxx(bixx);
202     } else if (parameter == "AC_IYY") {
203       *AC_cfg >> biyy;
204       if (debug_lvl > 0) cout << "    baseIyy: " << biyy << endl;
205       MassBalance->SetBaseIyy(biyy);
206     } else if (parameter == "AC_IZZ") {
207       *AC_cfg >> bizz;
208       if (debug_lvl > 0) cout << "    baseIzz: " << bizz << endl;
209       MassBalance->SetBaseIzz(bizz);
210     } else if (parameter == "AC_IXY") {
211       *AC_cfg >> bixy;
212       if (debug_lvl > 0) cout << "    baseIxy: " << bixy  << endl;
213       MassBalance->SetBaseIxy(bixy);
214     } else if (parameter == "AC_IXZ") {
215       *AC_cfg >> bixz;
216       if (debug_lvl > 0) cout << "    baseIxz: " << bixz  << endl;
217       MassBalance->SetBaseIxz(bixz);
218     } else if (parameter == "AC_EMPTYWT") {
219       *AC_cfg >> EW;
220       MassBalance->SetEmptyWeight(EW);
221       if (debug_lvl > 0) cout << "    EmptyWeight: " << EW  << endl;
222     } else if (parameter == "AC_CGLOC") {
223       *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
224       MassBalance->SetBaseCG(vbaseXYZcg);
225       if (debug_lvl > 0) cout << "    CG (x, y, z): " << vbaseXYZcg << endl;
226     } else if (parameter == "AC_EYEPTLOC") {
227       *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
228       if (debug_lvl > 0) cout << "    Eyepoint (x, y, z): " << vXYZep << endl;
229     } else if (parameter == "AC_AERORP") {
230       *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
231       if (debug_lvl > 0) cout << "    Ref Pt (x, y, z): " << vXYZrp << endl;
232     } else if (parameter == "AC_POINTMASS") {
233       *AC_cfg >> pmWt >> pmX >> pmY >> pmZ;
234       MassBalance->AddPointMass(pmWt, pmX, pmY, pmZ);
235       if (debug_lvl > 0) cout << "    Point Mass Object: " << pmWt << " lbs. at "
236                          << "X, Y, Z (in.): " << pmX << "  " << pmY << "  " << pmZ
237                          << endl;
238     }
239   }
240   
241   // calculate some derived parameters
242   if (cbar != 0.0) {
243     lbarh = HTailArm/cbar;
244     lbarv = VTailArm/cbar;
245     if (WingArea != 0.0) {
246       vbarh = HTailArm*HTailArea / (cbar*WingArea);
247       vbarv = VTailArm*VTailArea / (cbar*WingArea);
248     }
249   }     
250   return true;
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254
255 void FGAircraft::bind(void)
256 {
257   typedef double (FGAircraft::*PMF)(int) const;
258   PropertyManager->Tie("metrics/Sw-sqft", this,
259                        &FGAircraft::GetWingArea);
260   PropertyManager->Tie("metrics/bw-ft", this,
261                        &FGAircraft::GetWingSpan);
262   PropertyManager->Tie("metrics/cbarw-ft", this,
263                        &FGAircraft::Getcbar);
264   PropertyManager->Tie("metrics/iw-deg", this,
265                        &FGAircraft::GetWingIncidence);
266   PropertyManager->Tie("metrics/Sh-sqft", this,
267                        &FGAircraft::GetHTailArea);
268   PropertyManager->Tie("metrics/lh-ft", this,
269                        &FGAircraft::GetHTailArm);
270   PropertyManager->Tie("metrics/Sv-sqft", this,
271                        &FGAircraft::GetVTailArea);
272   PropertyManager->Tie("metrics/lv-ft", this,
273                        &FGAircraft::GetVTailArm);
274   PropertyManager->Tie("metrics/lh-norm", this,
275                        &FGAircraft::Getlbarh);
276   PropertyManager->Tie("metrics/lv-norm", this,
277                        &FGAircraft::Getlbarv);
278   PropertyManager->Tie("metrics/vbarh-norm", this,
279                        &FGAircraft::Getvbarh);
280   PropertyManager->Tie("metrics/vbarv-norm", this,
281                        &FGAircraft::Getvbarv);
282   PropertyManager->Tie("moments/l-total-lbsft", this,1,
283                        (PMF)&FGAircraft::GetMoments);
284   PropertyManager->Tie("moments/m-total-lbsft", this,2,
285                        (PMF)&FGAircraft::GetMoments);
286   PropertyManager->Tie("moments/n-total-lbsft", this,3,
287                        (PMF)&FGAircraft::GetMoments);
288   PropertyManager->Tie("forces/fbx-total-lbs", this,1,
289                        (PMF)&FGAircraft::GetForces);
290   PropertyManager->Tie("forces/fby-total-lbs", this,2,
291                        (PMF)&FGAircraft::GetForces);
292   PropertyManager->Tie("forces/fbz-total-lbs", this,3,
293                        (PMF)&FGAircraft::GetForces);
294   PropertyManager->Tie("metrics/aero-rp-x-ft", this,1,
295                        (PMF)&FGAircraft::GetXYZrp);
296   PropertyManager->Tie("metrics/aero-rp-y-ft", this,2,
297                        (PMF)&FGAircraft::GetXYZrp);
298   PropertyManager->Tie("metrics/aero-rp-z-ft", this,3,
299                        (PMF)&FGAircraft::GetXYZrp);
300   PropertyManager->Tie("metrics/eyepoint-x-ft", this,1,
301                        (PMF)&FGAircraft::GetXYZep);
302   PropertyManager->Tie("metrics/eyepoint-y-ft", this,2,
303                        (PMF)&FGAircraft::GetXYZep);
304   PropertyManager->Tie("metrics/eyepoint-z-ft", this,3,
305                        (PMF)&FGAircraft::GetXYZep);
306 }
307
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309
310 void FGAircraft::unbind(void)
311 {
312   PropertyManager->Untie("metrics/Sw-sqft");
313   PropertyManager->Untie("metrics/bw-ft");
314   PropertyManager->Untie("metrics/cbarw-ft");
315   PropertyManager->Untie("metrics/iw-deg");
316   PropertyManager->Untie("metrics/Sh-sqft");
317   PropertyManager->Untie("metrics/lh-ft");
318   PropertyManager->Untie("metrics/Sv-sqft");
319   PropertyManager->Untie("metrics/lv-ft");
320   PropertyManager->Untie("metrics/lh-norm");
321   PropertyManager->Untie("metrics/lv-norm");
322   PropertyManager->Untie("metrics/vbarh-norm");
323   PropertyManager->Untie("metrics/vbarv-norm");
324   PropertyManager->Untie("moments/l-total-lbsft");
325   PropertyManager->Untie("moments/m-total-lbsft");
326   PropertyManager->Untie("moments/n-total-lbsft");
327   PropertyManager->Untie("forces/fbx-total-lbs");
328   PropertyManager->Untie("forces/fby-total-lbs");
329   PropertyManager->Untie("forces/fbz-total-lbs");
330   PropertyManager->Untie("metrics/aero-rp-x-ft");
331   PropertyManager->Untie("metrics/aero-rp-y-ft");
332   PropertyManager->Untie("metrics/aero-rp-z-ft");
333   PropertyManager->Untie("metrics/eyepoint-x-ft");
334   PropertyManager->Untie("metrics/eyepoint-y-ft");
335   PropertyManager->Untie("metrics/eyepoint-z-ft");
336 }
337
338 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 //    The bitmasked value choices are as follows:
340 //    unset: In this case (the default) JSBSim would only print
341 //       out the normally expected messages, essentially echoing
342 //       the config files as they are read. If the environment
343 //       variable is not set, debug_lvl is set to 1 internally
344 //    0: This requests JSBSim not to output any messages
345 //       whatsoever.
346 //    1: This value explicity requests the normal JSBSim
347 //       startup messages
348 //    2: This value asks for a message to be printed out when
349 //       a class is instantiated
350 //    4: When this value is set, a message is displayed when a
351 //       FGModel object executes its Run() method
352 //    8: When this value is set, various runtime state variables
353 //       are printed out periodically
354 //    16: When set various parameters are sanity checked and
355 //       a message is printed out when they go out of bounds
356
357 void FGAircraft::Debug(int from)
358 {
359   if (debug_lvl <= 0) return;
360
361   if (debug_lvl & 1) { // Standard console startup message output
362     if (from == 0) { // Constructor
363     }
364   }
365   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
366     if (from == 0) cout << "Instantiated: FGAircraft" << endl;
367     if (from == 1) cout << "Destroyed:    FGAircraft" << endl;
368   }
369   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
370   }
371   if (debug_lvl & 8 ) { // Runtime state variables
372   }
373   if (debug_lvl & 16) { // Sanity checking
374   }
375   if (debug_lvl & 64) {
376     if (from == 0) { // Constructor
377       cout << IdSrc << endl;
378       cout << IdHdr << endl;
379     }
380   }
381 }
382