]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGMassBalance.cpp
Merge branch 'ehofman/jsbsim'
[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 (jon@jsbsim.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 #include <iostream>
46 #include <cstdlib>
47
48 using namespace std;
49
50 namespace JSBSim {
51
52 static const char *IdSrc = "$Id$";
53 static const char *IdHdr = ID_MASSBALANCE;
54
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 CLASS IMPLEMENTATION
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58
59
60 FGMassBalance::FGMassBalance(FGFDMExec* fdmex) : FGModel(fdmex)
61 {
62   Name = "FGMassBalance";
63   Weight = EmptyWeight = Mass = 0.0;
64
65   vbaseXYZcg.InitMatrix(0.0);
66   vXYZcg.InitMatrix(0.0);
67   vLastXYZcg.InitMatrix(0.0);
68   vDeltaXYZcg.InitMatrix(0.0);
69   baseJ.InitMatrix();
70   mJ.InitMatrix();
71   mJinv.InitMatrix();
72   pmJ.InitMatrix();
73
74   bind();
75
76   Debug(0);
77 }
78
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80
81 FGMassBalance::~FGMassBalance()
82 {
83   for (unsigned int i=0; i<PointMasses.size(); i++) delete PointMasses[i];
84   PointMasses.clear();
85
86   Debug(1);
87 }
88
89 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90
91 bool FGMassBalance::InitModel(void)
92 {
93   if (!FGModel::InitModel()) return false;
94
95   vLastXYZcg.InitMatrix(0.0);
96   vDeltaXYZcg.InitMatrix(0.0);
97
98   return true;
99 }
100
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103 bool FGMassBalance::Load(Element* el)
104 {
105   Element *element;
106   string element_name = "";
107   double bixx, biyy, bizz, bixy, bixz, biyz;
108
109   FGModel::Load(el); // Perform base class Load.
110
111   bixx = biyy = bizz = bixy = bixz = biyz = 0.0;
112   if (el->FindElement("ixx"))
113     bixx = el->FindElementValueAsNumberConvertTo("ixx", "SLUG*FT2");
114   if (el->FindElement("iyy"))
115     biyy = el->FindElementValueAsNumberConvertTo("iyy", "SLUG*FT2");
116   if (el->FindElement("izz"))
117     bizz = el->FindElementValueAsNumberConvertTo("izz", "SLUG*FT2");
118   if (el->FindElement("ixy"))
119     bixy = el->FindElementValueAsNumberConvertTo("ixy", "SLUG*FT2");
120   if (el->FindElement("ixz"))
121     bixz = el->FindElementValueAsNumberConvertTo("ixz", "SLUG*FT2");
122   if (el->FindElement("iyz"))
123     biyz = el->FindElementValueAsNumberConvertTo("iyz", "SLUG*FT2");
124   SetAircraftBaseInertias(FGMatrix33(  bixx,  -bixy,  bixz,
125                                       -bixy,  biyy,  -biyz,
126                                        bixz,  -biyz,  bizz ));
127   EmptyWeight = el->FindElementValueAsNumberConvertTo("emptywt", "LBS");
128
129   element = el->FindElement("location");
130   while (element) {
131     element_name = element->GetAttributeValue("name");
132     if (element_name == "CG") vbaseXYZcg = element->FindElementTripletConvertTo("IN");
133     element = el->FindNextElement("location");
134   }
135
136 // Find all POINTMASS elements that descend from this METRICS branch of the
137 // config file.
138
139   element = el->FindElement("pointmass");
140   while (element) {
141     AddPointMass(element);
142     element = el->FindNextElement("pointmass");
143   }
144
145   double ChildFDMWeight = 0.0;
146   for (int fdm=0; fdm<FDMExec->GetFDMCount(); fdm++) {
147     if (FDMExec->GetChildFDM(fdm)->mated) ChildFDMWeight += FDMExec->GetChildFDM(fdm)->exec->GetMassBalance()->GetWeight();
148   }
149
150   Weight = EmptyWeight + Propulsion->GetTanksWeight() + GetTotalPointMassWeight()
151     + BuoyantForces->GetGasMass()*slugtolb + ChildFDMWeight;
152
153   Mass = lbtoslug*Weight;
154
155   Debug(2);
156   return true;
157 }
158
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161 bool FGMassBalance::Run(void)
162 {
163   double denom, k1, k2, k3, k4, k5, k6;
164   double Ixx, Iyy, Izz, Ixy, Ixz, Iyz;
165
166   if (FGModel::Run()) return true;
167   if (FDMExec->Holding()) return false;
168
169   double ChildFDMWeight = 0.0;
170   for (int fdm=0; fdm<FDMExec->GetFDMCount(); fdm++) {
171     if (FDMExec->GetChildFDM(fdm)->mated) ChildFDMWeight += FDMExec->GetChildFDM(fdm)->exec->GetMassBalance()->GetWeight();
172   }
173
174   Weight = EmptyWeight + Propulsion->GetTanksWeight() + GetTotalPointMassWeight()
175     + BuoyantForces->GetGasMass()*slugtolb + ChildFDMWeight;
176
177   Mass = lbtoslug*Weight;
178
179 // Calculate new CG
180
181   vXYZcg = (Propulsion->GetTanksMoment() + EmptyWeight*vbaseXYZcg
182             + GetPointMassMoment()
183             + BuoyantForces->GetGasMassMoment()) / Weight;
184
185   // Track frame-by-frame delta CG, and move the EOM-tracked location
186   // by this amount.
187   if (vLastXYZcg.Magnitude() == 0.0) vLastXYZcg = vXYZcg;
188   vDeltaXYZcg = vXYZcg - vLastXYZcg;
189   vDeltaXYZcgBody = StructuralToBody(vLastXYZcg) - StructuralToBody(vXYZcg);
190   vLastXYZcg = vXYZcg;
191   Propagate->NudgeBodyLocation(vDeltaXYZcgBody);
192
193 // Calculate new total moments of inertia
194
195   // At first it is the base configuration inertia matrix ...
196   mJ = baseJ;
197   // ... with the additional term originating from the parallel axis theorem.
198   mJ += GetPointmassInertia( lbtoslug * EmptyWeight, vbaseXYZcg );
199   // Then add the contributions from the additional pointmasses.
200   mJ += CalculatePMInertias();
201   mJ += Propulsion->CalculateTankInertias();
202   mJ += BuoyantForces->GetGasMassInertia();
203
204   Ixx = mJ(1,1);
205   Iyy = mJ(2,2);
206   Izz = mJ(3,3);
207   Ixy = -mJ(1,2);
208   Ixz = -mJ(1,3);
209   Iyz = -mJ(2,3);
210
211 // Calculate inertia matrix inverse (ref. Stevens and Lewis, "Flight Control & Simulation")
212
213   k1 = (Iyy*Izz - Iyz*Iyz);
214   k2 = (Iyz*Ixz + Ixy*Izz);
215   k3 = (Ixy*Iyz + Iyy*Ixz);
216
217   denom = 1.0/(Ixx*k1 - Ixy*k2 - Ixz*k3 );
218   k1 = k1*denom;
219   k2 = k2*denom;
220   k3 = k3*denom;
221   k4 = (Izz*Ixx - Ixz*Ixz)*denom;
222   k5 = (Ixy*Ixz + Iyz*Ixx)*denom;
223   k6 = (Ixx*Iyy - Ixy*Ixy)*denom;
224
225   mJinv.InitMatrix( k1, k2, k3,
226                     k2, k4, k5,
227                     k3, k5, k6 );
228
229   Debug(0);
230
231   return false;
232 }
233
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235
236 void FGMassBalance::AddPointMass(Element* el)
237 {
238   Element* loc_element = el->FindElement("location");
239   string pointmass_name = el->GetAttributeValue("name");
240   if (!loc_element) {
241     cerr << "Pointmass " << pointmass_name << " has no location." << endl;
242     exit(-1);
243   }
244
245   double w = el->FindElementValueAsNumberConvertTo("weight", "LBS");
246   FGColumnVector3 vXYZ = loc_element->FindElementTripletConvertTo("IN");
247
248   PointMasses.push_back(new PointMass(w, vXYZ));
249   PointMasses.back()->bind(PropertyManager, PointMasses.size()-1);
250 }
251
252 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253
254 double FGMassBalance::GetTotalPointMassWeight(void)
255 {
256   double PM_total_weight = 0.0;
257
258   for (unsigned int i=0; i<PointMasses.size(); i++) {
259     PM_total_weight += PointMasses[i]->Weight;
260   }
261   return PM_total_weight;
262 }
263
264 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265
266 FGColumnVector3& FGMassBalance::GetPointMassMoment(void)
267 {
268   PointMassCG.InitMatrix();
269
270   for (unsigned int i=0; i<PointMasses.size(); i++) {
271     PointMassCG += PointMasses[i]->Weight*PointMasses[i]->Location;
272   }
273   return PointMassCG;
274 }
275
276 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277
278 FGMatrix33& FGMassBalance::CalculatePMInertias(void)
279 {
280   unsigned int size;
281
282   size = PointMasses.size();
283   if (size == 0) return pmJ;
284
285   pmJ = FGMatrix33();
286
287   for (unsigned int i=0; i<size; i++)
288     pmJ += GetPointmassInertia( lbtoslug * PointMasses[i]->Weight, PointMasses[i]->Location );
289
290   return pmJ;
291 }
292
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294
295 FGColumnVector3 FGMassBalance::StructuralToBody(const FGColumnVector3& r) const
296 {
297   // Under the assumption that in the structural frame the:
298   //
299   // - X-axis is directed afterwards,
300   // - Y-axis is directed towards the right,
301   // - Z-axis is directed upwards,
302   //
303   // (as documented in http://jsbsim.sourceforge.net/JSBSimCoordinates.pdf)
304   // we have to subtract first the center of gravity of the plane which
305   // is also defined in the structural frame:
306   //
307   //   FGColumnVector3 cgOff = r - vXYZcg;
308   //
309   // Next, we do a change of units:
310   //
311   //   cgOff *= inchtoft;
312   //
313   // And then a 180 degree rotation is done about the Y axis so that the:
314   //
315   // - X-axis is directed forward,
316   // - Y-axis is directed towards the right,
317   // - Z-axis is directed downward.
318   //
319   // This is needed because the structural and body frames are 180 degrees apart.
320
321   return FGColumnVector3(inchtoft*(vXYZcg(1)-r(1)),
322                          inchtoft*(r(2)-vXYZcg(2)),
323                          inchtoft*(vXYZcg(3)-r(3)));
324 }
325
326 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327
328 void FGMassBalance::bind(void)
329 {
330   typedef double (FGMassBalance::*PMF)(int) const;
331   PropertyManager->Tie("inertia/mass-slugs", this,
332                        &FGMassBalance::GetMass);
333   PropertyManager->Tie("inertia/weight-lbs", this,
334                        &FGMassBalance::GetWeight);
335   PropertyManager->Tie("inertia/empty-weight-lbs", this,
336     &FGMassBalance::GetWeight, &FGMassBalance::SetEmptyWeight);
337   PropertyManager->Tie("inertia/cg-x-in", this,1,
338                        (PMF)&FGMassBalance::GetXYZcg);
339   PropertyManager->Tie("inertia/cg-y-in", this,2,
340                        (PMF)&FGMassBalance::GetXYZcg);
341   PropertyManager->Tie("inertia/cg-z-in", this,3,
342                        (PMF)&FGMassBalance::GetXYZcg);
343 }
344
345 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346
347 void FGMassBalance::PointMass::bind(FGPropertyManager* PropertyManager, int num) {
348   string tmp = CreateIndexedPropertyName("inertia/pointmass-weight-lbs", num);
349   PropertyManager->Tie( tmp.c_str(), this, &PointMass::GetPointMassWeight,
350                                        &PointMass::SetPointMassWeight);
351
352   tmp = CreateIndexedPropertyName("inertia/pointmass-location-X-inches", num);
353   PropertyManager->Tie( tmp.c_str(), this, eX, &PointMass::GetPointMassLocation,
354                                            &PointMass::SetPointMassLocation);
355   tmp = CreateIndexedPropertyName("inertia/pointmass-location-Y-inches", num);
356   PropertyManager->Tie( tmp.c_str(), this, eY, &PointMass::GetPointMassLocation,
357                                            &PointMass::SetPointMassLocation);
358   tmp = CreateIndexedPropertyName("inertia/pointmass-location-Z-inches", num);
359   PropertyManager->Tie( tmp.c_str(), this, eZ, &PointMass::GetPointMassLocation,
360                                            &PointMass::SetPointMassLocation);
361 }
362
363 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 //    The bitmasked value choices are as follows:
365 //    unset: In this case (the default) JSBSim would only print
366 //       out the normally expected messages, essentially echoing
367 //       the config files as they are read. If the environment
368 //       variable is not set, debug_lvl is set to 1 internally
369 //    0: This requests JSBSim not to output any messages
370 //       whatsoever.
371 //    1: This value explicity requests the normal JSBSim
372 //       startup messages
373 //    2: This value asks for a message to be printed out when
374 //       a class is instantiated
375 //    4: When this value is set, a message is displayed when a
376 //       FGModel object executes its Run() method
377 //    8: When this value is set, various runtime state variables
378 //       are printed out periodically
379 //    16: When set various parameters are sanity checked and
380 //       a message is printed out when they go out of bounds
381
382 void FGMassBalance::Debug(int from)
383 {
384   if (debug_lvl <= 0) return;
385
386   if (debug_lvl & 1) { // Standard console startup message output
387     if (from == 2) { // Loading
388       cout << endl << "  Mass and Balance:" << endl;
389       cout << "    baseIxx: " << baseJ(1,1) << " slug-ft2" << endl;
390       cout << "    baseIyy: " << baseJ(2,2) << " slug-ft2" << endl;
391       cout << "    baseIzz: " << baseJ(3,3) << " slug-ft2" << endl;
392       cout << "    baseIxy: " << baseJ(1,2) << " slug-ft2" << endl;
393       cout << "    baseIxz: " << baseJ(1,3) << " slug-ft2" << endl;
394       cout << "    baseIyz: " << baseJ(2,3) << " slug-ft2" << endl;
395       cout << "    EmptyWeight: " << EmptyWeight << " lbm" << endl;
396       cout << "    CG (x, y, z): " << vbaseXYZcg << endl;
397       // ToDo: Need to add point mass outputs here
398       for (unsigned int i=0; i<PointMasses.size(); i++) {
399         cout << "    Point Mass Object: " << PointMasses[i]->Weight << " lbs. at "
400                    << "X, Y, Z (in.): " << PointMasses[i]->Location(eX) << "  "
401                    << PointMasses[i]->Location(eY) << "  "
402                    << PointMasses[i]->Location(eZ) << endl;
403       }
404     }
405   }
406   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
407     if (from == 0) cout << "Instantiated: FGMassBalance" << endl;
408     if (from == 1) cout << "Destroyed:    FGMassBalance" << endl;
409   }
410   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
411   }
412   if (debug_lvl & 8 ) { // Runtime state variables
413   }
414   if (debug_lvl & 16) { // Sanity checking
415     if (from == 2) {
416       if (EmptyWeight <= 0.0 || EmptyWeight > 1e9)
417         cout << "MassBalance::EmptyWeight out of bounds: " << EmptyWeight << endl;
418       if (Weight <= 0.0 || Weight > 1e9)
419         cout << "MassBalance::Weight out of bounds: " << Weight << endl;
420       if (Mass <= 0.0 || Mass > 1e9)
421         cout << "MassBalance::Mass out of bounds: " << Mass << endl;
422     }
423   }
424   if (debug_lvl & 64) {
425     if (from == 0) { // Constructor
426       cout << IdSrc << endl;
427       cout << IdHdr << endl;
428     }
429   }
430 }
431 }