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