]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGAircraft.cpp
edce3e0a3fbcb5b3ea15d9f452fdda8087557f68
[flightgear.git] / src / FDM / JSBSim / models / 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 Lesser 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 Lesser General Public License for more
19  details.
20
21  You should have received a copy of the GNU Lesser 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 Lesser 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 COMMENTS, REFERENCES,  and NOTES
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41 #include <sys/stat.h>
42 #include <sys/types.h>
43
44 #ifdef FGFS
45 #  ifndef __BORLANDC__
46 #    include <simgear/compiler.h>
47 #  endif
48 #  ifdef SG_HAVE_STD_INCLUDES
49 #    include <cmath>
50 #  else
51 #    include <math.h>
52 #  endif
53 #else
54 #  if defined (sgi) && !defined(__GNUC__)
55 #    include <math.h>
56 #  else
57 #    include <cmath>
58 #  endif
59 #endif
60
61 #include "FGAircraft.h"
62 #include "FGMassBalance.h"
63 #include "FGInertial.h"
64 #include "FGGroundReactions.h"
65 #include "FGAerodynamics.h"
66 #include <FGState.h>
67 #include <FGFDMExec.h>
68 #include "FGPropagate.h"
69 #include <input_output/FGPropertyManager.h>
70
71 namespace JSBSim {
72
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 DEFINITIONS
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76
77 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 GLOBAL DATA
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
80
81 static const char *IdSrc = "$Id$";
82 static const char *IdHdr = ID_AIRCRAFT;
83
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 CLASS IMPLEMENTATION
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
87
88 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex)
89 {
90   Name = "FGAircraft";
91   WingSpan = 0.0;
92   HTailArea = VTailArea = 0.0;
93   HTailArm  = VTailArm  = 0.0;
94   lbarh = lbarv = 0.0;
95   vbarh = vbarv = 0.0;
96   WingIncidence = 0.0;
97   HoldDown = 0;
98
99   bind();
100
101   Debug(0);
102 }
103
104 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
106 FGAircraft::~FGAircraft()
107 {
108   unbind();
109   Debug(1);
110 }
111
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114 bool FGAircraft::Run(void)
115 {
116   if (FGModel::Run()) return true;
117   if (FDMExec->Holding()) return false;
118
119   vForces.InitMatrix();
120   if (!HoldDown) {
121     vForces += Aerodynamics->GetForces();
122     vForces += Propulsion->GetForces();
123     vForces += GroundReactions->GetForces();
124   }
125
126   vMoments.InitMatrix();
127   if (!HoldDown) {
128     vMoments += Aerodynamics->GetMoments();
129     vMoments += Propulsion->GetMoments();
130     vMoments += GroundReactions->GetMoments();
131   }
132
133   vBodyAccel = vForces/MassBalance->GetMass();
134
135   vNcg = vBodyAccel/Inertial->gravity();
136
137   vNwcg = State->GetTb2s() * vNcg;
138   vNwcg(3) = -1*vNwcg(3) + 1;
139
140   return false;
141 }
142
143 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144
145 double FGAircraft::GetNlf(void)
146 {
147   return -1*Aerodynamics->GetvFs(3)/MassBalance->GetWeight();
148 }
149
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 bool FGAircraft::Load(Element* el)
153 {
154   string element_name;
155   Element* element;
156
157   if (el->FindElement("wingarea"))
158     WingArea = el->FindElementValueAsNumberConvertTo("wingarea", "FT2");
159   if (el->FindElement("wingspan"))
160     WingSpan = el->FindElementValueAsNumberConvertTo("wingspan", "FT");
161   if (el->FindElement("chord"))
162     cbar = el->FindElementValueAsNumberConvertTo("chord", "FT");
163   if (el->FindElement("wing_incidence"))
164     WingIncidence = el->FindElementValueAsNumberConvertTo("wing_incidence", "DEG");
165   if (el->FindElement("htailarea"))
166     HTailArea = el->FindElementValueAsNumberConvertTo("htailarea", "FT2");
167   if (el->FindElement("htailarm"))
168     HTailArm = el->FindElementValueAsNumberConvertTo("htailarm", "FT");
169   if (el->FindElement("vtailarea"))
170     VTailArea = el->FindElementValueAsNumberConvertTo("vtailarea", "FT2");
171   if (el->FindElement("vtailarm"))
172     VTailArm = el->FindElementValueAsNumberConvertTo("vtailarm", "FT");
173
174   // Find all LOCATION elements that descend from this METRICS branch of the
175   // config file. This would be CG location, eyepoint, etc.
176
177   element = el->FindElement("location");
178   while (element) {
179     element_name = element->GetAttributeValue("name");
180
181     if (element_name == "AERORP") vXYZrp = element->FindElementTripletConvertTo("IN");
182     else if (element_name == "EYEPOINT") vXYZep = element->FindElementTripletConvertTo("IN");
183     else if (element_name == "VRP") vXYZvrp = element->FindElementTripletConvertTo("IN");
184
185     element = el->FindNextElement("location");
186   }
187
188   // calculate some derived parameters
189   if (cbar != 0.0) {
190     lbarh = HTailArm/cbar;
191     lbarv = VTailArm/cbar;
192     if (WingArea != 0.0) {
193       vbarh = HTailArm*HTailArea / (cbar*WingArea);
194       vbarv = VTailArm*VTailArea / (cbar*WingArea);
195     }
196   }
197
198   Debug(2);
199
200   return true;
201 }
202
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205 void FGAircraft::bind(void)
206 {
207   typedef double (FGAircraft::*PMF)(int) const;
208   PropertyManager->Tie("metrics/Sw-sqft", this, &FGAircraft::GetWingArea);
209   PropertyManager->Tie("metrics/bw-ft", this, &FGAircraft::GetWingSpan);
210   PropertyManager->Tie("metrics/cbarw-ft", this, &FGAircraft::Getcbar);
211   PropertyManager->Tie("metrics/iw-deg", this, &FGAircraft::GetWingIncidence);
212   PropertyManager->Tie("metrics/Sh-sqft", this, &FGAircraft::GetHTailArea);
213   PropertyManager->Tie("metrics/lh-ft", this, &FGAircraft::GetHTailArm);
214   PropertyManager->Tie("metrics/Sv-sqft", this, &FGAircraft::GetVTailArea);
215   PropertyManager->Tie("metrics/lv-ft", this, &FGAircraft::GetVTailArm);
216   PropertyManager->Tie("metrics/lh-norm", this, &FGAircraft::Getlbarh);
217   PropertyManager->Tie("metrics/lv-norm", this, &FGAircraft::Getlbarv);
218   PropertyManager->Tie("metrics/vbarh-norm", this, &FGAircraft::Getvbarh);
219   PropertyManager->Tie("metrics/vbarv-norm", this, &FGAircraft::Getvbarv);
220   PropertyManager->Tie("forces/hold-down", this, &FGAircraft::GetHoldDown, &FGAircraft::SetHoldDown);
221   PropertyManager->Tie("moments/l-total-lbsft", this, eL, (PMF)&FGAircraft::GetMoments);
222   PropertyManager->Tie("moments/m-total-lbsft", this, eM, (PMF)&FGAircraft::GetMoments);
223   PropertyManager->Tie("moments/n-total-lbsft", this, eN, (PMF)&FGAircraft::GetMoments);
224   PropertyManager->Tie("forces/fbx-total-lbs", this, eX, (PMF)&FGAircraft::GetForces);
225   PropertyManager->Tie("forces/fby-total-lbs", this, eY, (PMF)&FGAircraft::GetForces);
226   PropertyManager->Tie("forces/fbz-total-lbs", this, eZ, (PMF)&FGAircraft::GetForces);
227   PropertyManager->Tie("metrics/aero-rp-x-in", this, eX, (PMF)&FGAircraft::GetXYZrp);
228   PropertyManager->Tie("metrics/aero-rp-y-in", this, eY, (PMF)&FGAircraft::GetXYZrp);
229   PropertyManager->Tie("metrics/aero-rp-z-in", this, eZ, (PMF)&FGAircraft::GetXYZrp);
230   PropertyManager->Tie("metrics/eyepoint-x-in", this, eX, (PMF)&FGAircraft::GetXYZep);
231   PropertyManager->Tie("metrics/eyepoint-y-in", this, eY,(PMF)&FGAircraft::GetXYZep);
232   PropertyManager->Tie("metrics/eyepoint-z-in", this, eZ, (PMF)&FGAircraft::GetXYZep);
233   PropertyManager->Tie("metrics/visualrefpoint-x-in", this, eX, (PMF)&FGAircraft::GetXYZvrp);
234   PropertyManager->Tie("metrics/visualrefpoint-y-in", this, eY, (PMF)&FGAircraft::GetXYZvrp);
235   PropertyManager->Tie("metrics/visualrefpoint-z-in", this, eZ, (PMF)&FGAircraft::GetXYZvrp);
236 }
237
238 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239
240 void FGAircraft::unbind(void)
241 {
242   PropertyManager->Untie("metrics/Sw-sqft");
243   PropertyManager->Untie("metrics/bw-ft");
244   PropertyManager->Untie("metrics/cbarw-ft");
245   PropertyManager->Untie("metrics/iw-deg");
246   PropertyManager->Untie("metrics/Sh-sqft");
247   PropertyManager->Untie("metrics/lh-ft");
248   PropertyManager->Untie("metrics/Sv-sqft");
249   PropertyManager->Untie("metrics/lv-ft");
250   PropertyManager->Untie("metrics/lh-norm");
251   PropertyManager->Untie("metrics/lv-norm");
252   PropertyManager->Untie("metrics/vbarh-norm");
253   PropertyManager->Untie("metrics/vbarv-norm");
254   PropertyManager->Untie("moments/l-total-lbsft");
255   PropertyManager->Untie("moments/m-total-lbsft");
256   PropertyManager->Untie("moments/n-total-lbsft");
257   PropertyManager->Untie("forces/fbx-total-lbs");
258   PropertyManager->Untie("forces/fby-total-lbs");
259   PropertyManager->Untie("forces/fbz-total-lbs");
260   PropertyManager->Untie("forces/hold-down");
261   PropertyManager->Untie("metrics/aero-rp-x-in");
262   PropertyManager->Untie("metrics/aero-rp-y-in");
263   PropertyManager->Untie("metrics/aero-rp-z-in");
264   PropertyManager->Untie("metrics/eyepoint-x-in");
265   PropertyManager->Untie("metrics/eyepoint-y-in");
266   PropertyManager->Untie("metrics/eyepoint-z-in");
267   PropertyManager->Untie("metrics/visualrefpoint-x-in");
268   PropertyManager->Untie("metrics/visualrefpoint-y-in");
269   PropertyManager->Untie("metrics/visualrefpoint-z-in");
270 }
271
272 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 //    The bitmasked value choices are as follows:
274 //    unset: In this case (the default) JSBSim would only print
275 //       out the normally expected messages, essentially echoing
276 //       the config files as they are read. If the environment
277 //       variable is not set, debug_lvl is set to 1 internally
278 //    0: This requests JSBSim not to output any messages
279 //       whatsoever.
280 //    1: This value explicity requests the normal JSBSim
281 //       startup messages
282 //    2: This value asks for a message to be printed out when
283 //       a class is instantiated
284 //    4: When this value is set, a message is displayed when a
285 //       FGModel object executes its Run() method
286 //    8: When this value is set, various runtime state variables
287 //       are printed out periodically
288 //    16: When set various parameters are sanity checked and
289 //       a message is printed out when they go out of bounds
290
291 void FGAircraft::Debug(int from)
292 {
293   if (debug_lvl <= 0) return;
294
295   if (debug_lvl & 1) { // Standard console startup message output
296     if (from == 2) { // Loading
297       cout << endl << "  Aircraft Metrics:" << endl;
298       cout << "    WingArea: " << WingArea  << endl;
299       cout << "    WingSpan: " << WingSpan  << endl;
300       cout << "    Incidence: " << WingIncidence << endl;
301       cout << "    Chord: " << cbar << endl;
302       cout << "    H. Tail Area: " << HTailArea << endl;
303       cout << "    H. Tail Arm: " << HTailArm << endl;
304       cout << "    V. Tail Area: " << VTailArea << endl;
305       cout << "    V. Tail Arm: " << VTailArm << endl;
306       cout << "    Eyepoint (x, y, z): " << vXYZep << endl;
307       cout << "    Ref Pt (x, y, z): " << vXYZrp << endl;
308       cout << "    Visual Ref Pt (x, y, z): " << vXYZvrp << endl;
309     }
310   }
311   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
312     if (from == 0) cout << "Instantiated: FGAircraft" << endl;
313     if (from == 1) cout << "Destroyed:    FGAircraft" << endl;
314   }
315   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
316   }
317   if (debug_lvl & 8 ) { // Runtime state variables
318   }
319   if (debug_lvl & 16) { // Sanity checking
320   }
321   if (debug_lvl & 64) {
322     if (from == 0) { // Constructor
323       cout << IdSrc << endl;
324       cout << IdHdr << endl;
325     }
326   }
327 }
328
329 } // namespace JSBSim