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