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 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 General Public License for more
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.
24 Further information about the GNU 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 "FGPropertyManager.h"
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_MASSBALANCE;
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 FGMassBalance::FGMassBalance(FGFDMExec* fdmex) : FGModel(fdmex)
54 Name = "FGMassBalance";
60 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 FGMassBalance::~FGMassBalance()
68 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 bool FGMassBalance::Run(void)
72 if (!FGModel::Run()) {
74 Weight = EmptyWeight + Propulsion->GetTanksWeight() + GetPointMassWeight();
76 Mass = Weight / Inertial->gravity();
78 // Calculate new CG here.
80 vXYZcg = (Propulsion->GetTanksCG() + EmptyWeight*vbaseXYZcg
81 + GetPointMassCG() ) / Weight;
83 // Calculate new moments of inertia here
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();
99 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 void FGMassBalance::AddPointMass(double weight, double X, double Y, double Z)
103 PointMassLoc.push_back(*(new FGColumnVector3(X, Y, Z)));
104 PointMassWeight.push_back(weight);
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 double FGMassBalance::GetPointMassWeight(void)
111 double PM_total_weight = 0.0;
113 for (unsigned int i=0; i<PointMassWeight.size(); i++) {
114 PM_total_weight += PointMassWeight[i];
116 return PM_total_weight;
119 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 FGColumnVector3& FGMassBalance::GetPointMassCG(void)
123 PointMassCG.InitMatrix();
125 for (unsigned int i=0; i<PointMassLoc.size(); i++) {
126 PointMassCG += PointMassWeight[i]*PointMassLoc[i];
131 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 double FGMassBalance::GetPMIxx(void)
136 for (unsigned int i=0; i<PointMassLoc.size(); i++) {
137 I += (PointMassLoc[i](eX)-vXYZcg(eX))*(PointMassLoc[i](eX)-vXYZcg(eX))*PointMassWeight[i];
139 I /= (144.0*Inertial->gravity());
143 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 double FGMassBalance::GetPMIyy(void)
148 for (unsigned int i=0; i<PointMassLoc.size(); i++) {
149 I += (PointMassLoc[i](eY)-vXYZcg(eY))*(PointMassLoc[i](eY)-vXYZcg(eY))*PointMassWeight[i];
151 I /= (144.0*Inertial->gravity());
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 double FGMassBalance::GetPMIzz(void)
160 for (unsigned int i=0; i<PointMassLoc.size(); i++) {
161 I += (PointMassLoc[i](eZ)-vXYZcg(eZ))*(PointMassLoc[i](eZ)-vXYZcg(eZ))*PointMassWeight[i];
163 I /= (144.0*Inertial->gravity());
167 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 double FGMassBalance::GetPMIxy(void)
172 for (unsigned int i=0; i<PointMassLoc.size(); i++) {
173 I += (PointMassLoc[i](eX)-vXYZcg(eX))*(PointMassLoc[i](eY)-vXYZcg(eY))*PointMassWeight[i];
175 I /= (144.0*Inertial->gravity());
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 double FGMassBalance::GetPMIxz(void)
184 for (unsigned int i=0; i<PointMassLoc.size(); i++) {
185 I += (PointMassLoc[i](eX)-vXYZcg(eX))*(PointMassLoc[i](eZ)-vXYZcg(eZ))*PointMassWeight[i];
187 I /= (144.0*Inertial->gravity());
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 // The bitmasked value choices are as follows:
193 // unset: In this case (the default) JSBSim would only print
194 // out the normally expected messages, essentially echoing
195 // the config files as they are read. If the environment
196 // variable is not set, debug_lvl is set to 1 internally
197 // 0: This requests JSBSim not to output any messages
199 // 1: This value explicity requests the normal JSBSim
201 // 2: This value asks for a message to be printed out when
202 // a class is instantiated
203 // 4: When this value is set, a message is displayed when a
204 // FGModel object executes its Run() method
205 // 8: When this value is set, various runtime state variables
206 // are printed out periodically
207 // 16: When set various parameters are sanity checked and
208 // a message is printed out when they go out of bounds
210 void FGMassBalance::Debug(int from)
212 if (debug_lvl <= 0) return;
214 if (debug_lvl & 1) { // Standard console startup message output
215 if (from == 0) { // Constructor
219 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
220 if (from == 0) cout << "Instantiated: FGPiston" << endl;
221 if (from == 1) cout << "Destroyed: FGPiston" << endl;
223 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
225 if (debug_lvl & 8 ) { // Runtime state variables
227 if (debug_lvl & 16) { // Sanity checking
229 if (EmptyWeight <= 0.0 || EmptyWeight > 1e9)
230 cout << "MassBalance::EmptyWeight out of bounds: " << EmptyWeight << endl;
231 if (Weight <= 0.0 || Weight > 1e9)
232 cout << "MassBalance::Weight out of bounds: " << Weight << endl;
233 if (Mass <= 0.0 || Mass > 1e9)
234 cout << "MassBalance::Mass out of bounds: " << Mass << endl;
237 if (debug_lvl & 64) {
238 if (from == 0) { // Constructor
239 cout << IdSrc << endl;
240 cout << IdHdr << endl;
245 void FGMassBalance::bind(void){
246 PropertyManager->Tie("inertia/mass-slugs", this,
247 &FGMassBalance::GetMass);
248 PropertyManager->Tie("inertia/weight-lbs", this,
249 &FGMassBalance::GetWeight);
250 PropertyManager->Tie("inertia/ixx-lbsft2", this,
251 &FGMassBalance::GetIxx);
252 PropertyManager->Tie("inertia/iyy-lbsft2", this,
253 &FGMassBalance::GetIyy);
254 PropertyManager->Tie("inertia/izz-lbsft2", this,
255 &FGMassBalance::GetIzz);
256 PropertyManager->Tie("inertia/ixy-lbsft2", this,
257 &FGMassBalance::GetIxy);
258 PropertyManager->Tie("inertia/ixz-lbsft2", this,
259 &FGMassBalance::GetIxz);
260 PropertyManager->Tie("inertia/cg-x-ft", this,1,
261 &FGMassBalance::GetXYZcg);
262 PropertyManager->Tie("inertia/cg-y-ft", this,2,
263 &FGMassBalance::GetXYZcg);
264 PropertyManager->Tie("inertia/cg-z-ft", this,3,
265 &FGMassBalance::GetXYZcg);
268 void FGMassBalance::unbind(void){
269 PropertyManager->Untie("inertia/mass-slugs");
270 PropertyManager->Untie("inertia/weight-lbs");
271 PropertyManager->Untie("inertia/ixx-lbsft2");
272 PropertyManager->Untie("inertia/iyy-lbsft2");
273 PropertyManager->Untie("inertia/izz-lbsft2");
274 PropertyManager->Untie("inertia/ixy-lbsft2");
275 PropertyManager->Untie("inertia/ixz-lbsft2");
276 PropertyManager->Untie("inertia/cg-x-ft");
277 PropertyManager->Untie("inertia/cg-y-ft");
278 PropertyManager->Untie("inertia/cg-z-ft");