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