1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGMassBalance.cpp
5 Date started: 09/12/2000
6 Purpose: This module models weight and balance
8 ------------- Copyright (C) 2000 Jon S. Berndt (jsb@hal-pc.org) --------------
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
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
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.
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.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
30 This class models the change in weight and balance of the aircraft due to fuel
34 --------------------------------------------------------------------------------
35 09/12/2000 JSB Created
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41 #include "FGMassBalance.h"
42 #include "FGPropulsion.h"
43 #include <input_output/FGPropertyManager.h>
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_MASSBALANCE;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 FGMassBalance::FGMassBalance(FGFDMExec* fdmex) : FGModel(fdmex)
57 Name = "FGMassBalance";
58 Weight = EmptyWeight = Mass = 0.0;
60 vbaseXYZcg.InitMatrix(0.0);
71 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 FGMassBalance::~FGMassBalance()
80 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 bool FGMassBalance::Load(Element* el)
85 string element_name = "";
86 double bixx, biyy, bizz, bixy, bixz, biyz;
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,
103 -bixz, -biyz, bizz ));
104 EmptyWeight = el->FindElementValueAsNumberConvertTo("emptywt", "LBS");
106 element = el->FindElement("location");
108 element_name = element->GetAttributeValue("name");
109 if (element_name == "CG") vbaseXYZcg = element->FindElementTripletConvertTo("IN");
110 element = el->FindNextElement("location");
113 // Find all POINTMASS elements that descend from this METRICS branch of the
116 element = el->FindElement("pointmass");
118 AddPointMass(element);
119 element = el->FindNextElement("pointmass");
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 bool FGMassBalance::Run(void)
130 double denom, k1, k2, k3, k4, k5, k6;
131 double Ixx, Iyy, Izz, Ixy, Ixz, Iyz;
133 if (FGModel::Run()) return true;
134 if (FDMExec->Holding()) return false;
136 Weight = EmptyWeight + Propulsion->GetTanksWeight() + GetPointMassWeight();
138 Mass = lbtoslug*Weight;
142 vXYZcg = (Propulsion->GetTanksMoment() + EmptyWeight*vbaseXYZcg
143 + GetPointMassMoment() ) / Weight;
145 // Calculate new total moments of inertia
147 // At first it is the base configuration inertia matrix ...
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();
162 // Calculate inertia matrix inverse (ref. Stevens and Lewis, "Flight Control & Simulation")
164 k1 = (Iyy*Izz - Iyz*Iyz);
165 k2 = (Iyz*Ixz + Ixy*Izz);
166 k3 = (Ixy*Iyz + Iyy*Ixz);
168 denom = 1.0/(Ixx*k1 - Ixy*k2 - Ixz*k3 );
172 k4 = (Izz*Ixx - Ixz*Ixz)*denom;
173 k5 = (Ixy*Ixz + Iyz*Ixx)*denom;
174 k6 = (Ixx*Iyy - Ixy*Ixy)*denom;
176 mJinv.InitMatrix( k1, k2, k3,
185 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 void FGMassBalance::AddPointMass(Element* el)
189 Element* loc_element = el->FindElement("location");
190 string pointmass_name = el->GetAttributeValue("name");
192 cerr << "Pointmass " << pointmass_name << " has no location." << endl;
196 double w = el->FindElementValueAsNumberConvertTo("weight", "LBS");
197 FGColumnVector3 vXYZ = loc_element->FindElementTripletConvertTo("IN");
198 PointMasses.push_back(PointMass(w, vXYZ));
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 double FGMassBalance::GetPointMassWeight(void)
205 double PM_total_weight = 0.0;
207 for (unsigned int i=0; i<PointMasses.size(); i++) {
208 PM_total_weight += PointMasses[i].Weight;
210 return PM_total_weight;
213 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 FGColumnVector3& FGMassBalance::GetPointMassMoment(void)
217 PointMassCG.InitMatrix();
219 for (unsigned int i=0; i<PointMasses.size(); i++) {
220 PointMassCG += PointMasses[i].Weight*PointMasses[i].Location;
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 FGMatrix33& FGMassBalance::CalculatePMInertias(void)
231 size = PointMasses.size();
232 if (size == 0) return pmJ;
236 for (unsigned int i=0; i<size; i++)
237 pmJ += GetPointmassInertia( lbtoslug * PointMasses[i].Weight, PointMasses[i].Location );
242 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 FGColumnVector3 FGMassBalance::StructuralToBody(const FGColumnVector3& r) const
246 // Under the assumption that in the structural frame the:
248 // - X-axis is directed afterwards,
249 // - Y-axis is directed towards the right,
250 // - Z-axis is directed upwards,
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:
256 // FGColumnVector3 cgOff = r - vXYZcg;
258 // Next, we do a change of units:
260 // cgOff *= inchtoft;
262 // And then a 180 degree rotation is done about the Y axis so that the:
264 // - X-axis is directed forward,
265 // - Y-axis is directed towards the right,
266 // - Z-axis is directed downward.
268 // This is needed because the structural and body frames are 180 degrees apart.
270 return FGColumnVector3(inchtoft*(vXYZcg(1)-r(1)),
271 inchtoft*(r(2)-vXYZcg(2)),
272 inchtoft*(vXYZcg(3)-r(3)));
275 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 void FGMassBalance::bind(void)
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);
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 void FGMassBalance::unbind(void)
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");
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
311 // 1: This value explicity requests the normal JSBSim
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
322 void FGMassBalance::Debug(int from)
324 if (debug_lvl <= 0) return;
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;
346 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
347 if (from == 0) cout << "Instantiated: FGMassBalance" << endl;
348 if (from == 1) cout << "Destroyed: FGMassBalance" << endl;
350 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
352 if (debug_lvl & 8 ) { // Runtime state variables
354 if (debug_lvl & 16) { // Sanity checking
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;
364 if (debug_lvl & 64) {
365 if (from == 0) { // Constructor
366 cout << IdSrc << endl;
367 cout << IdHdr << endl;