]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGAircraft.cpp
sync with JSBSim CVS
[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 (jon@jsbsim.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 #include <iostream>
44 #include <cmath>
45
46 #include "FGAircraft.h"
47 #include "FGMassBalance.h"
48 #include "FGInertial.h"
49 #include "FGGroundReactions.h"
50 #include "FGExternalReactions.h"
51 #include "FGBuoyantForces.h"
52 #include "FGAerodynamics.h"
53 #include "FGFDMExec.h"
54 #include "FGPropagate.h"
55 #include "FGPropulsion.h"
56 #include "input_output/FGPropertyManager.h"
57
58 using namespace std;
59
60 namespace JSBSim {
61
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 DEFINITIONS
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 GLOBAL DATA
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
69
70 static const char *IdSrc = "$Id: FGAircraft.cpp,v 1.31 2011/05/20 03:18:36 jberndt Exp $";
71 static const char *IdHdr = ID_AIRCRAFT;
72
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 CLASS IMPLEMENTATION
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76
77 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex)
78 {
79   Name = "FGAircraft";
80   WingSpan = 0.0;
81   HTailArea = VTailArea = 0.0;
82   HTailArm  = VTailArm  = 0.0;
83   lbarh = lbarv = 0.0;
84   vbarh = vbarv = 0.0;
85   WingIncidence = 0.0;
86   HoldDown = 0;
87
88   bind();
89
90   Debug(0);
91 }
92
93 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94
95 FGAircraft::~FGAircraft()
96 {
97   Debug(1);
98 }
99
100 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101
102 bool FGAircraft::InitModel(void)
103 {
104   return true;
105 }
106
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108
109 bool FGAircraft::Run(bool Holding)
110 {
111   if (FGModel::Run(Holding)) return true;
112   if (Holding) return false;
113
114   RunPreFunctions();
115
116   vForces.InitMatrix();
117   if (!HoldDown) {
118     vForces += FDMExec->GetAerodynamics()->GetForces();
119     vForces += FDMExec->GetPropulsion()->GetForces();
120     vForces += FDMExec->GetGroundReactions()->GetForces();
121     vForces += FDMExec->GetExternalReactions()->GetForces();
122     vForces += FDMExec->GetBuoyantForces()->GetForces();
123   } else {
124     const FGMatrix33& mTl2b = FDMExec->GetPropagate()->GetTl2b();
125     vForces = mTl2b * FGColumnVector3(0,0,-FDMExec->GetMassBalance()->GetWeight());
126   }
127
128   vMoments.InitMatrix();
129   if (!HoldDown) {
130     vMoments += FDMExec->GetAerodynamics()->GetMoments();
131     vMoments += FDMExec->GetPropulsion()->GetMoments();
132     vMoments += FDMExec->GetGroundReactions()->GetMoments();
133     vMoments += FDMExec->GetExternalReactions()->GetMoments();
134     vMoments += FDMExec->GetBuoyantForces()->GetMoments();
135   }
136
137   vBodyAccel = vForces/FDMExec->GetMassBalance()->GetMass();
138
139   vNcg = vBodyAccel/FDMExec->GetInertial()->SLgravity();
140
141   vNwcg = FDMExec->GetAerodynamics()->GetTb2w() * vNcg;
142   vNwcg(3) = 1.0 - vNwcg(3);
143
144   RunPostFunctions();
145
146   return false;
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 double FGAircraft::GetNlf(void) const
152 {
153   if (FDMExec->GetMassBalance()->GetWeight() != 0)
154     return (-FDMExec->GetAerodynamics()->GetvFw(3))/FDMExec->GetMassBalance()->GetWeight();
155   else
156     return 0.;
157 }
158
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161 bool FGAircraft::Load(Element* el)
162 {
163   string element_name;
164   Element* element;
165
166   FGModel::Load(el);
167
168   if (el->FindElement("wingarea"))
169     WingArea = el->FindElementValueAsNumberConvertTo("wingarea", "FT2");
170   if (el->FindElement("wingspan"))
171     WingSpan = el->FindElementValueAsNumberConvertTo("wingspan", "FT");
172   if (el->FindElement("chord"))
173     cbar = el->FindElementValueAsNumberConvertTo("chord", "FT");
174   if (el->FindElement("wing_incidence"))
175     WingIncidence = el->FindElementValueAsNumberConvertTo("wing_incidence", "RAD");
176   if (el->FindElement("htailarea"))
177     HTailArea = el->FindElementValueAsNumberConvertTo("htailarea", "FT2");
178   if (el->FindElement("htailarm"))
179     HTailArm = el->FindElementValueAsNumberConvertTo("htailarm", "FT");
180   if (el->FindElement("vtailarea"))
181     VTailArea = el->FindElementValueAsNumberConvertTo("vtailarea", "FT2");
182   if (el->FindElement("vtailarm"))
183     VTailArm = el->FindElementValueAsNumberConvertTo("vtailarm", "FT");
184
185   // Find all LOCATION elements that descend from this METRICS branch of the
186   // config file. This would be CG location, eyepoint, etc.
187
188   element = el->FindElement("location");
189   while (element) {
190     element_name = element->GetAttributeValue("name");
191
192     if (element_name == "AERORP") vXYZrp = element->FindElementTripletConvertTo("IN");
193     else if (element_name == "EYEPOINT") vXYZep = element->FindElementTripletConvertTo("IN");
194     else if (element_name == "VRP") vXYZvrp = element->FindElementTripletConvertTo("IN");
195
196     element = el->FindNextElement("location");
197   }
198
199   // calculate some derived parameters
200   if (cbar != 0.0) {
201     lbarh = HTailArm/cbar;
202     lbarv = VTailArm/cbar;
203     if (WingArea != 0.0) {
204       vbarh = HTailArm*HTailArea / (cbar*WingArea);
205       vbarv = VTailArm*VTailArea / (WingSpan*WingArea);
206     }
207   }
208
209   PostLoad(el, PropertyManager);
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("metrics/aero-rp-x-in", this, eX, (PMF)&FGAircraft::GetXYZrp);
235   PropertyManager->Tie("metrics/aero-rp-y-in", this, eY, (PMF)&FGAircraft::GetXYZrp);
236   PropertyManager->Tie("metrics/aero-rp-z-in", this, eZ, (PMF)&FGAircraft::GetXYZrp);
237   PropertyManager->Tie("metrics/eyepoint-x-in", this, eX, (PMF)&FGAircraft::GetXYZep);
238   PropertyManager->Tie("metrics/eyepoint-y-in", this, eY,(PMF)&FGAircraft::GetXYZep);
239   PropertyManager->Tie("metrics/eyepoint-z-in", this, eZ, (PMF)&FGAircraft::GetXYZep);
240   PropertyManager->Tie("metrics/visualrefpoint-x-in", this, eX, (PMF)&FGAircraft::GetXYZvrp);
241   PropertyManager->Tie("metrics/visualrefpoint-y-in", this, eY, (PMF)&FGAircraft::GetXYZvrp);
242   PropertyManager->Tie("metrics/visualrefpoint-z-in", this, eZ, (PMF)&FGAircraft::GetXYZvrp);
243   PropertyManager->Tie("forces/fbx-total-lbs", this, eX, (PMF)&FGAircraft::GetForces);
244   PropertyManager->Tie("forces/fby-total-lbs", this, eY, (PMF)&FGAircraft::GetForces);
245   PropertyManager->Tie("forces/fbz-total-lbs", this, eZ, (PMF)&FGAircraft::GetForces);
246   PropertyManager->Tie("forces/load-factor", this, &FGAircraft::GetNlf);
247   PropertyManager->Tie("forces/hold-down", this, &FGAircraft::GetHoldDown, &FGAircraft::SetHoldDown);
248   PropertyManager->Tie("moments/l-total-lbsft", this, eL, (PMF)&FGAircraft::GetMoments);
249   PropertyManager->Tie("moments/m-total-lbsft", this, eM, (PMF)&FGAircraft::GetMoments);
250   PropertyManager->Tie("moments/n-total-lbsft", this, eN, (PMF)&FGAircraft::GetMoments);
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 //    The bitmasked value choices are as follows:
255 //    unset: In this case (the default) JSBSim would only print
256 //       out the normally expected messages, essentially echoing
257 //       the config files as they are read. If the environment
258 //       variable is not set, debug_lvl is set to 1 internally
259 //    0: This requests JSBSim not to output any messages
260 //       whatsoever.
261 //    1: This value explicity requests the normal JSBSim
262 //       startup messages
263 //    2: This value asks for a message to be printed out when
264 //       a class is instantiated
265 //    4: When this value is set, a message is displayed when a
266 //       FGModel object executes its Run() method
267 //    8: When this value is set, various runtime state variables
268 //       are printed out periodically
269 //    16: When set various parameters are sanity checked and
270 //       a message is printed out when they go out of bounds
271
272 void FGAircraft::Debug(int from)
273 {
274   if (debug_lvl <= 0) return;
275
276   if (debug_lvl & 1) { // Standard console startup message output
277     if (from == 2) { // Loading
278       cout << endl << "  Aircraft Metrics:" << endl;
279       cout << "    WingArea: " << WingArea  << endl;
280       cout << "    WingSpan: " << WingSpan  << endl;
281       cout << "    Incidence: " << WingIncidence << endl;
282       cout << "    Chord: " << cbar << endl;
283       cout << "    H. Tail Area: " << HTailArea << endl;
284       cout << "    H. Tail Arm: " << HTailArm << endl;
285       cout << "    V. Tail Area: " << VTailArea << endl;
286       cout << "    V. Tail Arm: " << VTailArm << endl;
287       cout << "    Eyepoint (x, y, z): " << vXYZep << endl;
288       cout << "    Ref Pt (x, y, z): " << vXYZrp << endl;
289       cout << "    Visual Ref Pt (x, y, z): " << vXYZvrp << endl;
290     }
291   }
292   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
293     if (from == 0) cout << "Instantiated: FGAircraft" << endl;
294     if (from == 1) cout << "Destroyed:    FGAircraft" << endl;
295   }
296   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
297   }
298   if (debug_lvl & 8 ) { // Runtime state variables
299   }
300   if (debug_lvl & 16) { // Sanity checking
301   }
302   if (debug_lvl & 64) {
303     if (from == 0) { // Constructor
304       cout << IdSrc << endl;
305       cout << IdHdr << endl;
306     }
307   }
308 }
309
310 } // namespace JSBSim