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