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