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