]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGAircraft.cpp
0d8c3ed7910228f40fc254b7f9fa2b593de6d85f
[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 #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 <input_output/FGPropertyManager.h>
56
57 namespace JSBSim {
58
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 DEFINITIONS
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 GLOBAL DATA
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
66
67 static const char *IdSrc = "$Id$";
68 static const char *IdHdr = ID_AIRCRAFT;
69
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 CLASS IMPLEMENTATION
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73
74 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex)
75 {
76   Name = "FGAircraft";
77   WingSpan = 0.0;
78   HTailArea = VTailArea = 0.0;
79   HTailArm  = VTailArm  = 0.0;
80   lbarh = lbarv = 0.0;
81   vbarh = vbarv = 0.0;
82   WingIncidence = 0.0;
83   HoldDown = 0;
84
85   bind();
86
87   Debug(0);
88 }
89
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91
92 FGAircraft::~FGAircraft()
93 {
94   Debug(1);
95 }
96
97 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98
99 bool FGAircraft::InitModel(void)
100 {
101   if (!FGModel::InitModel()) return false;
102
103   return true;
104 }
105
106 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107
108 bool FGAircraft::Run(void)
109 {
110   if (FGModel::Run()) return true;
111   if (FDMExec->Holding()) return false;
112
113   vForces.InitMatrix();
114   if (!HoldDown) {
115     vForces += Aerodynamics->GetForces();
116     vForces += Propulsion->GetForces();
117     vForces += GroundReactions->GetForces();
118     vForces += ExternalReactions->GetForces();
119     vForces += BuoyantForces->GetForces();
120   }
121
122   vMoments.InitMatrix();
123   if (!HoldDown) {
124     vMoments += Aerodynamics->GetMoments();
125     vMoments += Propulsion->GetMoments();
126     vMoments += GroundReactions->GetMoments();
127     vMoments += ExternalReactions->GetMoments();
128     vMoments += BuoyantForces->GetMoments();
129   }
130
131   vBodyAccel = vForces/MassBalance->GetMass();
132
133   vNcg = vBodyAccel/Inertial->gravity();
134
135   vNwcg = Aerodynamics->GetTb2w() * vNcg;
136   vNwcg(3) = -1*vNwcg(3) + 1;
137
138   return false;
139 }
140
141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142
143 double FGAircraft::GetNlf(void) const
144 {
145   return -1*Aerodynamics->GetvFw(3)/MassBalance->GetWeight();
146 }
147
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149
150 bool FGAircraft::Load(Element* el)
151 {
152   string element_name;
153   Element* element;
154
155   FGModel::Load(el);
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", "RAD");
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 / (WingSpan*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, &FGAircraft::SetWingArea);
209   PropertyManager->Tie("metrics/bw-ft", this, &FGAircraft::GetWingSpan);
210   PropertyManager->Tie("metrics/cbarw-ft", this, &FGAircraft::Getcbar);
211   PropertyManager->Tie("metrics/iw-rad", this, &FGAircraft::GetWingIncidence);
212   PropertyManager->Tie("metrics/iw-deg", this, &FGAircraft::GetWingIncidenceDeg);
213   PropertyManager->Tie("metrics/Sh-sqft", this, &FGAircraft::GetHTailArea);
214   PropertyManager->Tie("metrics/lh-ft", this, &FGAircraft::GetHTailArm);
215   PropertyManager->Tie("metrics/Sv-sqft", this, &FGAircraft::GetVTailArea);
216   PropertyManager->Tie("metrics/lv-ft", this, &FGAircraft::GetVTailArm);
217   PropertyManager->Tie("metrics/lh-norm", this, &FGAircraft::Getlbarh);
218   PropertyManager->Tie("metrics/lv-norm", this, &FGAircraft::Getlbarv);
219   PropertyManager->Tie("metrics/vbarh-norm", this, &FGAircraft::Getvbarh);
220   PropertyManager->Tie("metrics/vbarv-norm", this, &FGAircraft::Getvbarv);
221   PropertyManager->Tie("metrics/aero-rp-x-in", this, eX, (PMF)&FGAircraft::GetXYZrp);
222   PropertyManager->Tie("metrics/aero-rp-y-in", this, eY, (PMF)&FGAircraft::GetXYZrp);
223   PropertyManager->Tie("metrics/aero-rp-z-in", this, eZ, (PMF)&FGAircraft::GetXYZrp);
224   PropertyManager->Tie("metrics/eyepoint-x-in", this, eX, (PMF)&FGAircraft::GetXYZep);
225   PropertyManager->Tie("metrics/eyepoint-y-in", this, eY,(PMF)&FGAircraft::GetXYZep);
226   PropertyManager->Tie("metrics/eyepoint-z-in", this, eZ, (PMF)&FGAircraft::GetXYZep);
227   PropertyManager->Tie("metrics/visualrefpoint-x-in", this, eX, (PMF)&FGAircraft::GetXYZvrp);
228   PropertyManager->Tie("metrics/visualrefpoint-y-in", this, eY, (PMF)&FGAircraft::GetXYZvrp);
229   PropertyManager->Tie("metrics/visualrefpoint-z-in", this, eZ, (PMF)&FGAircraft::GetXYZvrp);
230   PropertyManager->Tie("forces/fbx-total-lbs", this, eX, (PMF)&FGAircraft::GetForces);
231   PropertyManager->Tie("forces/fby-total-lbs", this, eY, (PMF)&FGAircraft::GetForces);
232   PropertyManager->Tie("forces/fbz-total-lbs", this, eZ, (PMF)&FGAircraft::GetForces);
233   PropertyManager->Tie("forces/load-factor", this, &FGAircraft::GetNlf);
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 }
239
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 //    The bitmasked value choices are as follows:
242 //    unset: In this case (the default) JSBSim would only print
243 //       out the normally expected messages, essentially echoing
244 //       the config files as they are read. If the environment
245 //       variable is not set, debug_lvl is set to 1 internally
246 //    0: This requests JSBSim not to output any messages
247 //       whatsoever.
248 //    1: This value explicity requests the normal JSBSim
249 //       startup messages
250 //    2: This value asks for a message to be printed out when
251 //       a class is instantiated
252 //    4: When this value is set, a message is displayed when a
253 //       FGModel object executes its Run() method
254 //    8: When this value is set, various runtime state variables
255 //       are printed out periodically
256 //    16: When set various parameters are sanity checked and
257 //       a message is printed out when they go out of bounds
258
259 void FGAircraft::Debug(int from)
260 {
261   if (debug_lvl <= 0) return;
262
263   if (debug_lvl & 1) { // Standard console startup message output
264     if (from == 2) { // Loading
265       cout << endl << "  Aircraft Metrics:" << endl;
266       cout << "    WingArea: " << WingArea  << endl;
267       cout << "    WingSpan: " << WingSpan  << endl;
268       cout << "    Incidence: " << WingIncidence << endl;
269       cout << "    Chord: " << cbar << endl;
270       cout << "    H. Tail Area: " << HTailArea << endl;
271       cout << "    H. Tail Arm: " << HTailArm << endl;
272       cout << "    V. Tail Area: " << VTailArea << endl;
273       cout << "    V. Tail Arm: " << VTailArm << endl;
274       cout << "    Eyepoint (x, y, z): " << vXYZep << endl;
275       cout << "    Ref Pt (x, y, z): " << vXYZrp << endl;
276       cout << "    Visual Ref Pt (x, y, z): " << vXYZvrp << endl;
277     }
278   }
279   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
280     if (from == 0) cout << "Instantiated: FGAircraft" << endl;
281     if (from == 1) cout << "Destroyed:    FGAircraft" << endl;
282   }
283   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
284   }
285   if (debug_lvl & 8 ) { // Runtime state variables
286   }
287   if (debug_lvl & 16) { // Sanity checking
288   }
289   if (debug_lvl & 64) {
290     if (from == 0) { // Constructor
291       cout << IdSrc << endl;
292       cout << IdHdr << endl;
293     }
294   }
295 }
296
297 } // namespace JSBSim