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