]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGMassBalance.cpp
Add the latest version of JSBSim including support for a Visual Reference Point
[flightgear.git] / src / FDM / JSBSim / 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 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 General Public License for more
18  details.
19
20  You should have received a copy of the GNU 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 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 "FGPropertyManager.h"
43
44 namespace JSBSim {
45
46 static const char *IdSrc = "$Id$";
47 static const char *IdHdr = ID_MASSBALANCE;
48
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 CLASS IMPLEMENTATION
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52
53
54 FGMassBalance::FGMassBalance(FGFDMExec* fdmex) : FGModel(fdmex)
55 {
56   Name = "FGMassBalance";
57   Weight = EmptyWeight = Mass = 0.0;
58   Ixx = Iyy = Izz = Ixy = Ixz = 0.0;
59   baseIxx = baseIyy = baseIzz = baseIxy = baseIxz = 0.0;
60   vbaseXYZcg(eX) = 0.0;
61   vbaseXYZcg(eY) = 0.0;
62   vbaseXYZcg(eZ) = 0.0;
63   bind();
64
65   Debug(0);
66 }
67
68 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69
70 FGMassBalance::~FGMassBalance()
71 {
72   unbind();
73   Debug(1);
74 }
75
76 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77
78 bool FGMassBalance::Run(void)
79 {
80   if (!FGModel::Run()) {
81
82     Weight = EmptyWeight + Propulsion->GetTanksWeight() + GetPointMassWeight();
83
84     Mass = Weight / Inertial->gravity();
85
86 // Calculate new CG here.
87
88     vXYZcg = (Propulsion->GetTanksMoment() + EmptyWeight*vbaseXYZcg
89                                        + GetPointMassMoment() ) / Weight;
90
91 // Calculate new moments of inertia here
92
93     Ixx = baseIxx + Propulsion->GetTanksIxx(vXYZcg) + GetPMIxx();
94     Iyy = baseIyy + Propulsion->GetTanksIyy(vXYZcg) + GetPMIyy();
95     Izz = baseIzz + Propulsion->GetTanksIzz(vXYZcg) + GetPMIzz();
96     Ixy = baseIxy + Propulsion->GetTanksIxy(vXYZcg) + GetPMIxy();
97     Ixz = baseIxz + Propulsion->GetTanksIxz(vXYZcg) + GetPMIxz();
98
99     Debug(2);
100
101     return false;
102   } else {
103     return true;
104   }
105 }
106
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108
109 void FGMassBalance::AddPointMass(double weight, double X, double Y, double Z)
110 {
111   PointMassLoc.push_back(*(new FGColumnVector3(X, Y, Z)));
112   PointMassWeight.push_back(weight);
113 }
114
115 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116
117 double FGMassBalance::GetPointMassWeight(void)
118 {
119   double PM_total_weight = 0.0;
120
121   for (unsigned int i=0; i<PointMassWeight.size(); i++) {
122     PM_total_weight += PointMassWeight[i];
123   }
124   return PM_total_weight;
125 }
126
127 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128
129 FGColumnVector3& FGMassBalance::GetPointMassMoment(void)
130 {
131   PointMassCG.InitMatrix();
132
133   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
134     PointMassCG += PointMassWeight[i]*PointMassLoc[i];
135   }
136   return PointMassCG;
137 }
138
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
141 double FGMassBalance::GetPMIxx(void)
142 {
143   double I = 0.0;
144   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
145     I += (PointMassLoc[i](eX)-vXYZcg(eX))*(PointMassLoc[i](eX)-vXYZcg(eX))*PointMassWeight[i];
146   }
147   I /= (144.0*Inertial->gravity());
148   return I;
149 }
150
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152
153 double FGMassBalance::GetPMIyy(void)
154 {
155   double I = 0.0;
156   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
157     I += (PointMassLoc[i](eY)-vXYZcg(eY))*(PointMassLoc[i](eY)-vXYZcg(eY))*PointMassWeight[i];
158   }
159   I /= (144.0*Inertial->gravity());
160   return I;
161 }
162
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164
165 double FGMassBalance::GetPMIzz(void)
166 {
167   double I = 0.0;
168   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
169     I += (PointMassLoc[i](eZ)-vXYZcg(eZ))*(PointMassLoc[i](eZ)-vXYZcg(eZ))*PointMassWeight[i];
170   }
171   I /= (144.0*Inertial->gravity());
172   return I;
173 }
174
175 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176
177 double FGMassBalance::GetPMIxy(void)
178 {
179   double I = 0.0;
180   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
181     I += (PointMassLoc[i](eX)-vXYZcg(eX))*(PointMassLoc[i](eY)-vXYZcg(eY))*PointMassWeight[i];
182   }
183   I /= (144.0*Inertial->gravity());
184   return I;
185 }
186
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188
189 double FGMassBalance::GetPMIxz(void)
190 {
191   double I = 0.0;
192   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
193     I += (PointMassLoc[i](eX)-vXYZcg(eX))*(PointMassLoc[i](eZ)-vXYZcg(eZ))*PointMassWeight[i];
194   }
195   I /= (144.0*Inertial->gravity());
196   return I;
197 }
198
199 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200
201 FGColumnVector3 FGMassBalance::StructuralToBody(const FGColumnVector3& r) const
202 {
203   // Under the assumption that in the structural frame the:
204   //
205   // - X-axis is directed afterwards,
206   // - Y-axis is directed towards the right,
207   // - Z-axis is directed upwards,
208   //
209   // (as documented in http://jsbsim.sourceforge.net/JSBSimCoordinates.pdf)
210   // we have to subtract first the center of gravity of the plane which
211   // is also defined in the structural frame:
212   //
213   //   FGColumnVector3 cgOff = r - vXYZcg;
214   //
215   // Next, we do a change of units:
216   //
217   //   cgOff *= inchtoft;
218   //
219   // And then a 180 degree rotation is done about the Y axis so that the:
220   //
221   // - X-axis is directed forward,
222   // - Y-axis is directed towards the right,
223   // - Z-axis is directed downward.
224   //
225   // This is needed because the structural and body frames are 180 degrees apart.
226
227   return FGColumnVector3(inchtoft*(vXYZcg(1)-r(1)),
228                          inchtoft*(r(2)-vXYZcg(2)),
229                          inchtoft*(vXYZcg(3)-r(3)));
230 }
231
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233
234 void FGMassBalance::bind(void)
235
236   typedef double (FGMassBalance::*PMF)(int) const;
237   PropertyManager->Tie("inertia/mass-slugs", this,
238                        &FGMassBalance::GetMass);
239   PropertyManager->Tie("inertia/weight-lbs", this,
240                        &FGMassBalance::GetWeight);
241   PropertyManager->Tie("inertia/ixx-lbsft2", this,
242                        &FGMassBalance::GetIxx);
243   PropertyManager->Tie("inertia/iyy-lbsft2", this,
244                        &FGMassBalance::GetIyy);
245   PropertyManager->Tie("inertia/izz-lbsft2", this,
246                        &FGMassBalance::GetIzz);
247   PropertyManager->Tie("inertia/ixy-lbsft2", this,
248                        &FGMassBalance::GetIxy);
249   PropertyManager->Tie("inertia/ixz-lbsft2", this,
250                        &FGMassBalance::GetIxz);
251   PropertyManager->Tie("inertia/cg-x-ft", this,1,
252                        (PMF)&FGMassBalance::GetXYZcg);
253   PropertyManager->Tie("inertia/cg-y-ft", this,2,
254                        (PMF)&FGMassBalance::GetXYZcg);
255   PropertyManager->Tie("inertia/cg-z-ft", this,3,
256                        (PMF)&FGMassBalance::GetXYZcg);
257 }
258
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260
261 void FGMassBalance::unbind(void)
262 {
263   PropertyManager->Untie("inertia/mass-slugs");
264   PropertyManager->Untie("inertia/weight-lbs");
265   PropertyManager->Untie("inertia/ixx-lbsft2");
266   PropertyManager->Untie("inertia/iyy-lbsft2");
267   PropertyManager->Untie("inertia/izz-lbsft2");
268   PropertyManager->Untie("inertia/ixy-lbsft2");
269   PropertyManager->Untie("inertia/ixz-lbsft2");
270   PropertyManager->Untie("inertia/cg-x-ft");
271   PropertyManager->Untie("inertia/cg-y-ft");
272   PropertyManager->Untie("inertia/cg-z-ft");
273 }
274
275 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 //    The bitmasked value choices are as follows:
277 //    unset: In this case (the default) JSBSim would only print
278 //       out the normally expected messages, essentially echoing
279 //       the config files as they are read. If the environment
280 //       variable is not set, debug_lvl is set to 1 internally
281 //    0: This requests JSBSim not to output any messages
282 //       whatsoever.
283 //    1: This value explicity requests the normal JSBSim
284 //       startup messages
285 //    2: This value asks for a message to be printed out when
286 //       a class is instantiated
287 //    4: When this value is set, a message is displayed when a
288 //       FGModel object executes its Run() method
289 //    8: When this value is set, various runtime state variables
290 //       are printed out periodically
291 //    16: When set various parameters are sanity checked and
292 //       a message is printed out when they go out of bounds
293
294 void FGMassBalance::Debug(int from)
295 {
296   if (debug_lvl <= 0) return;
297
298   if (debug_lvl & 1) { // Standard console startup message output
299     if (from == 0) { // Constructor
300
301     }
302   }
303   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
304     if (from == 0) cout << "Instantiated: FGPiston" << endl;
305     if (from == 1) cout << "Destroyed:    FGPiston" << endl;
306   }
307   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
308   }
309   if (debug_lvl & 8 ) { // Runtime state variables
310   }
311   if (debug_lvl & 16) { // Sanity checking
312     if (from == 2) {
313       if (EmptyWeight <= 0.0 || EmptyWeight > 1e9)
314         cout << "MassBalance::EmptyWeight out of bounds: " << EmptyWeight << endl;
315       if (Weight <= 0.0 || Weight > 1e9)
316         cout << "MassBalance::Weight out of bounds: " << Weight << endl;
317       if (Mass <= 0.0 || Mass > 1e9)
318         cout << "MassBalance::Mass out of bounds: " << Mass << endl;
319     }
320   }
321   if (debug_lvl & 64) {
322     if (from == 0) { // Constructor
323       cout << IdSrc << endl;
324       cout << IdHdr << endl;
325     }
326   }
327 }
328 }