]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGMassBalance.cpp
eae63733523674de850efd750ec44bc1c52cde52
[flightgear.git] / src / FDM / JSBSim / models / FGMassBalance.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGMassBalance.cpp
4  Author:       Jon S. Berndt
5  Date started: 09/12/2000
6  Purpose:      This module models weight and balance
7
8  ------------- Copyright (C) 2000  Jon S. Berndt (jsb@hal-pc.org) --------------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
18  details.
19
20  You should have received a copy of the GNU Lesser General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU Lesser General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29
30 This class models the change in weight and balance of the aircraft due to fuel
31 burnoff, etc.
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 09/12/2000  JSB  Created
36
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41 #include "FGMassBalance.h"
42 #include "FGPropulsion.h"
43 #include "FGBuoyantForces.h"
44 #include <input_output/FGPropertyManager.h>
45
46 namespace JSBSim {
47
48 static const char *IdSrc = "$Id$";
49 static const char *IdHdr = ID_MASSBALANCE;
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 CLASS IMPLEMENTATION
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55
56 FGMassBalance::FGMassBalance(FGFDMExec* fdmex) : FGModel(fdmex)
57 {
58   Name = "FGMassBalance";
59   Weight = EmptyWeight = Mass = 0.0;
60
61   vbaseXYZcg.InitMatrix(0.0);
62   baseJ.InitMatrix();
63   mJ.InitMatrix();
64   mJinv.InitMatrix();
65   pmJ.InitMatrix();
66
67   bind();
68
69   Debug(0);
70 }
71
72 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73
74 FGMassBalance::~FGMassBalance()
75 {
76   for (unsigned int i=0; i<PointMasses.size(); i++) delete PointMasses[i];
77   PointMasses.clear();
78
79   Debug(1);
80 }
81
82 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83
84 bool FGMassBalance::InitModel(void)
85 {
86   if (!FGModel::InitModel()) return false;
87
88   return true;
89 }
90
91 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92
93 bool FGMassBalance::Load(Element* el)
94 {
95   Element *element;
96   string element_name = "";
97   double bixx, biyy, bizz, bixy, bixz, biyz;
98
99   FGModel::Load(el); // Perform base class Load.
100
101   bixx = biyy = bizz = bixy = bixz = biyz = 0.0;
102   if (el->FindElement("ixx"))
103     bixx = el->FindElementValueAsNumberConvertTo("ixx", "SLUG*FT2");
104   if (el->FindElement("iyy"))
105     biyy = el->FindElementValueAsNumberConvertTo("iyy", "SLUG*FT2");
106   if (el->FindElement("izz"))
107     bizz = el->FindElementValueAsNumberConvertTo("izz", "SLUG*FT2");
108   if (el->FindElement("ixy"))
109     bixy = el->FindElementValueAsNumberConvertTo("ixy", "SLUG*FT2");
110   if (el->FindElement("ixz"))
111     bixz = el->FindElementValueAsNumberConvertTo("ixz", "SLUG*FT2");
112   if (el->FindElement("iyz"))
113     biyz = el->FindElementValueAsNumberConvertTo("iyz", "SLUG*FT2");
114   SetAircraftBaseInertias(FGMatrix33(  bixx,  -bixy,  bixz,
115                                       -bixy,  biyy,  -biyz,
116                                        bixz,  -biyz,  bizz ));
117   EmptyWeight = el->FindElementValueAsNumberConvertTo("emptywt", "LBS");
118
119   element = el->FindElement("location");
120   while (element) {
121     element_name = element->GetAttributeValue("name");
122     if (element_name == "CG") vbaseXYZcg = element->FindElementTripletConvertTo("IN");
123     element = el->FindNextElement("location");
124   }
125
126 // Find all POINTMASS elements that descend from this METRICS branch of the
127 // config file.
128
129   element = el->FindElement("pointmass");
130   while (element) {
131     AddPointMass(element);
132     element = el->FindNextElement("pointmass");
133   }
134
135   double ChildFDMWeight = 0.0;
136   for (int fdm=0; fdm<FDMExec->GetFDMCount(); fdm++) {
137     if (FDMExec->GetChildFDM(fdm)->mated) ChildFDMWeight += FDMExec->GetChildFDM(fdm)->exec->GetMassBalance()->GetWeight();
138   }
139
140   Weight = EmptyWeight + Propulsion->GetTanksWeight() + GetTotalPointMassWeight()
141     + BuoyantForces->GetGasMass()*slugtolb + ChildFDMWeight;
142
143   Mass = lbtoslug*Weight;
144
145   Debug(2);
146   return true;
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 bool FGMassBalance::Run(void)
152 {
153   double denom, k1, k2, k3, k4, k5, k6;
154   double Ixx, Iyy, Izz, Ixy, Ixz, Iyz;
155
156   if (FGModel::Run()) return true;
157   if (FDMExec->Holding()) return false;
158
159   double ChildFDMWeight = 0.0;
160   for (int fdm=0; fdm<FDMExec->GetFDMCount(); fdm++) {
161     if (FDMExec->GetChildFDM(fdm)->mated) ChildFDMWeight += FDMExec->GetChildFDM(fdm)->exec->GetMassBalance()->GetWeight();
162   }
163
164   Weight = EmptyWeight + Propulsion->GetTanksWeight() + GetTotalPointMassWeight()
165     + BuoyantForces->GetGasMass()*slugtolb + ChildFDMWeight;
166
167   Mass = lbtoslug*Weight;
168
169 // Calculate new CG
170
171   vXYZcg = (Propulsion->GetTanksMoment() + EmptyWeight*vbaseXYZcg
172             + GetPointMassMoment()
173             + BuoyantForces->GetGasMassMoment()) / Weight;
174
175 // Calculate new total moments of inertia
176
177   // At first it is the base configuration inertia matrix ...
178   mJ = baseJ;
179   // ... with the additional term originating from the parallel axis theorem.
180   mJ += GetPointmassInertia( lbtoslug * EmptyWeight, vbaseXYZcg );
181   // Then add the contributions from the additional pointmasses.
182   mJ += CalculatePMInertias();
183   mJ += Propulsion->CalculateTankInertias();
184   mJ += BuoyantForces->GetGasMassInertia();
185
186   Ixx = mJ(1,1);
187   Iyy = mJ(2,2);
188   Izz = mJ(3,3);
189   Ixy = -mJ(1,2);
190   Ixz = -mJ(1,3);
191   Iyz = -mJ(2,3);
192
193 // Calculate inertia matrix inverse (ref. Stevens and Lewis, "Flight Control & Simulation")
194
195   k1 = (Iyy*Izz - Iyz*Iyz);
196   k2 = (Iyz*Ixz + Ixy*Izz);
197   k3 = (Ixy*Iyz + Iyy*Ixz);
198
199   denom = 1.0/(Ixx*k1 - Ixy*k2 - Ixz*k3 );
200   k1 = k1*denom;
201   k2 = k2*denom;
202   k3 = k3*denom;
203   k4 = (Izz*Ixx - Ixz*Ixz)*denom;
204   k5 = (Ixy*Ixz + Iyz*Ixx)*denom;
205   k6 = (Ixx*Iyy - Ixy*Ixy)*denom;
206
207   mJinv.InitMatrix( k1, k2, k3,
208                     k2, k4, k5,
209                     k3, k5, k6 );
210
211   Debug(0);
212
213   return false;
214 }
215
216 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217
218 void FGMassBalance::AddPointMass(Element* el)
219 {
220   Element* loc_element = el->FindElement("location");
221   string pointmass_name = el->GetAttributeValue("name");
222   if (!loc_element) {
223     cerr << "Pointmass " << pointmass_name << " has no location." << endl;
224     exit(-1);
225   }
226
227   double w = el->FindElementValueAsNumberConvertTo("weight", "LBS");
228   FGColumnVector3 vXYZ = loc_element->FindElementTripletConvertTo("IN");
229
230   PointMasses.push_back(new PointMass(w, vXYZ));
231   PointMasses.back()->bind(PropertyManager, PointMasses.size()-1);
232 }
233
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235
236 double FGMassBalance::GetTotalPointMassWeight(void)
237 {
238   double PM_total_weight = 0.0;
239
240   for (unsigned int i=0; i<PointMasses.size(); i++) {
241     PM_total_weight += PointMasses[i]->Weight;
242   }
243   return PM_total_weight;
244 }
245
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247
248 FGColumnVector3& FGMassBalance::GetPointMassMoment(void)
249 {
250   PointMassCG.InitMatrix();
251
252   for (unsigned int i=0; i<PointMasses.size(); i++) {
253     PointMassCG += PointMasses[i]->Weight*PointMasses[i]->Location;
254   }
255   return PointMassCG;
256 }
257
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259
260 FGMatrix33& FGMassBalance::CalculatePMInertias(void)
261 {
262   unsigned int size;
263
264   size = PointMasses.size();
265   if (size == 0) return pmJ;
266
267   pmJ = FGMatrix33();
268
269   for (unsigned int i=0; i<size; i++)
270     pmJ += GetPointmassInertia( lbtoslug * PointMasses[i]->Weight, PointMasses[i]->Location );
271
272   return pmJ;
273 }
274
275 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276
277 FGColumnVector3 FGMassBalance::StructuralToBody(const FGColumnVector3& r) const
278 {
279   // Under the assumption that in the structural frame the:
280   //
281   // - X-axis is directed afterwards,
282   // - Y-axis is directed towards the right,
283   // - Z-axis is directed upwards,
284   //
285   // (as documented in http://jsbsim.sourceforge.net/JSBSimCoordinates.pdf)
286   // we have to subtract first the center of gravity of the plane which
287   // is also defined in the structural frame:
288   //
289   //   FGColumnVector3 cgOff = r - vXYZcg;
290   //
291   // Next, we do a change of units:
292   //
293   //   cgOff *= inchtoft;
294   //
295   // And then a 180 degree rotation is done about the Y axis so that the:
296   //
297   // - X-axis is directed forward,
298   // - Y-axis is directed towards the right,
299   // - Z-axis is directed downward.
300   //
301   // This is needed because the structural and body frames are 180 degrees apart.
302
303   return FGColumnVector3(inchtoft*(vXYZcg(1)-r(1)),
304                          inchtoft*(r(2)-vXYZcg(2)),
305                          inchtoft*(vXYZcg(3)-r(3)));
306 }
307
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309
310 void FGMassBalance::bind(void)
311 {
312   typedef double (FGMassBalance::*PMF)(int) const;
313   PropertyManager->Tie("inertia/mass-slugs", this,
314                        &FGMassBalance::GetMass);
315   PropertyManager->Tie("inertia/weight-lbs", this,
316                        &FGMassBalance::GetWeight);
317   PropertyManager->Tie("inertia/empty-weight-lbs", this,
318     &FGMassBalance::GetWeight, &FGMassBalance::SetEmptyWeight);
319   PropertyManager->Tie("inertia/cg-x-in", this,1,
320                        (PMF)&FGMassBalance::GetXYZcg);
321   PropertyManager->Tie("inertia/cg-y-in", this,2,
322                        (PMF)&FGMassBalance::GetXYZcg);
323   PropertyManager->Tie("inertia/cg-z-in", this,3,
324                        (PMF)&FGMassBalance::GetXYZcg);
325 }
326
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 //    The bitmasked value choices are as follows:
329 //    unset: In this case (the default) JSBSim would only print
330 //       out the normally expected messages, essentially echoing
331 //       the config files as they are read. If the environment
332 //       variable is not set, debug_lvl is set to 1 internally
333 //    0: This requests JSBSim not to output any messages
334 //       whatsoever.
335 //    1: This value explicity requests the normal JSBSim
336 //       startup messages
337 //    2: This value asks for a message to be printed out when
338 //       a class is instantiated
339 //    4: When this value is set, a message is displayed when a
340 //       FGModel object executes its Run() method
341 //    8: When this value is set, various runtime state variables
342 //       are printed out periodically
343 //    16: When set various parameters are sanity checked and
344 //       a message is printed out when they go out of bounds
345
346 void FGMassBalance::Debug(int from)
347 {
348   if (debug_lvl <= 0) return;
349
350   if (debug_lvl & 1) { // Standard console startup message output
351     if (from == 2) { // Loading
352       cout << endl << "  Mass and Balance:" << endl;
353       cout << "    baseIxx: " << baseJ(1,1) << " slug-ft2" << endl;
354       cout << "    baseIyy: " << baseJ(2,2) << " slug-ft2" << endl;
355       cout << "    baseIzz: " << baseJ(3,3) << " slug-ft2" << endl;
356       cout << "    baseIxy: " << baseJ(1,2) << " slug-ft2" << endl;
357       cout << "    baseIxz: " << baseJ(1,3) << " slug-ft2" << endl;
358       cout << "    baseIyz: " << baseJ(2,3) << " slug-ft2" << endl;
359       cout << "    EmptyWeight: " << EmptyWeight << " lbm" << endl;
360       cout << "    CG (x, y, z): " << vbaseXYZcg << endl;
361       // ToDo: Need to add point mass outputs here
362       for (unsigned int i=0; i<PointMasses.size(); i++) {
363         cout << "    Point Mass Object: " << PointMasses[i]->Weight << " lbs. at "
364                    << "X, Y, Z (in.): " << PointMasses[i]->Location(eX) << "  "
365                    << PointMasses[i]->Location(eY) << "  "
366                    << PointMasses[i]->Location(eZ) << endl;
367       }
368     }
369   }
370   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
371     if (from == 0) cout << "Instantiated: FGMassBalance" << endl;
372     if (from == 1) cout << "Destroyed:    FGMassBalance" << endl;
373   }
374   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
375   }
376   if (debug_lvl & 8 ) { // Runtime state variables
377   }
378   if (debug_lvl & 16) { // Sanity checking
379     if (from == 2) {
380       if (EmptyWeight <= 0.0 || EmptyWeight > 1e9)
381         cout << "MassBalance::EmptyWeight out of bounds: " << EmptyWeight << endl;
382       if (Weight <= 0.0 || Weight > 1e9)
383         cout << "MassBalance::Weight out of bounds: " << Weight << endl;
384       if (Mass <= 0.0 || Mass > 1e9)
385         cout << "MassBalance::Mass out of bounds: " << Mass << endl;
386     }
387   }
388   if (debug_lvl & 64) {
389     if (from == 0) { // Constructor
390       cout << IdSrc << endl;
391       cout << IdHdr << endl;
392     }
393   }
394 }
395 }