]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGGroundReactions.cpp
Merge branch 'next' of git://gitorious.org/fg/flightgear into next
[flightgear.git] / src / FDM / JSBSim / models / FGGroundReactions.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGGroundReactions.cpp
4  Author:       Jon S. Berndt
5  Date started: 09/13/00
6  Purpose:      Encapsulates the ground reaction forces (gear and collision)
7
8  ------------- Copyright (C) 2000  Jon S. Berndt (jon@jsbsim.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 HISTORY
31 --------------------------------------------------------------------------------
32 09/13/00   JSB   Created
33
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 INCLUDES
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37
38 #include <sstream>
39 #include <iomanip>
40
41 #include "FGGroundReactions.h"
42 #include "FGFCS.h"
43 #include "input_output/FGPropertyManager.h"
44
45 using namespace std;
46
47 namespace JSBSim {
48
49 static const char *IdSrc = "$Id: FGGroundReactions.cpp,v 1.32 2011/05/20 03:18:36 jberndt Exp $";
50 static const char *IdHdr = ID_GROUNDREACTIONS;
51
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 CLASS IMPLEMENTATION for MultiplierIterator (See below for FGGroundReactions)
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55
56 MultiplierIterator::MultiplierIterator(FGGroundReactions* GndReactions)
57 : GroundReactions(GndReactions),
58   multiplier(NULL),
59   gearNum(0),
60   entry(0)
61 {
62   for (int i=0; i < GroundReactions->GetNumGearUnits(); i++) {
63                 FGLGear* gear = GroundReactions->GetGearUnit(i);
64
65                 if (!gear->GetWOW()) continue;
66
67     gearNum = i;
68     multiplier = gear->GetMultiplierEntry(0);
69     break;
70   }
71 }
72
73 MultiplierIterator& MultiplierIterator::operator++()
74 {
75   for (int i=gearNum; i < GroundReactions->GetNumGearUnits(); i++) {
76                 FGLGear* gear = GroundReactions->GetGearUnit(i);
77
78     if (!gear->GetWOW()) continue;
79
80     multiplier = gear->GetMultiplierEntry(++entry);
81     if (multiplier) {
82       gearNum = i;
83       break;
84     }
85     else
86       entry = -1;
87   }
88
89   return *this;
90 }
91
92 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 CLASS IMPLEMENTATION
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
95
96 FGGroundReactions::FGGroundReactions(FGFDMExec* fgex) : FGModel(fgex)
97 {
98   Name = "FGGroundReactions";
99
100   bind();
101
102   Debug(0);
103 }
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 FGGroundReactions::~FGGroundReactions(void)
108 {
109   for (unsigned int i=0; i<lGear.size();i++) delete lGear[i];
110   lGear.clear();
111
112   Debug(1);
113 }
114
115 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116
117 bool FGGroundReactions::InitModel(void)
118 {
119   return true;
120 }
121
122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123
124 bool FGGroundReactions::Run(bool Holding)
125 {
126   if (FGModel::Run(Holding)) return true;
127   if (Holding) return false;
128
129   RunPreFunctions();
130
131   vForces.InitMatrix();
132   vMoments.InitMatrix();
133
134   // Sum forces and moments for all gear, here.
135   // Some optimizations may be made here - or rather in the gear code itself.
136   // The gear ::Run() method is called several times - once for each gear.
137   // Perhaps there is some commonality for things which only need to be
138   // calculated once.
139   for (unsigned int i=0; i<lGear.size(); i++) {
140     vForces  += lGear[i]->GetBodyForces();
141     vMoments += lGear[i]->GetMoments();
142   }
143
144   RunPostFunctions();
145
146   return false;
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 bool FGGroundReactions::GetWOW(void) const
152 {
153   bool result = false;
154   for (unsigned int i=0; i<lGear.size(); i++) {
155     if (lGear[i]->IsBogey() && lGear[i]->GetWOW()) {
156       result = true;
157       break;
158     }
159   }
160   return result;
161 }
162
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 // This function must be called after friction forces are resolved in order to
165 // include them in the ground reactions total force and moment.
166 void FGGroundReactions::UpdateForcesAndMoments(void)
167 {
168   vForces.InitMatrix();
169   vMoments.InitMatrix();
170
171   for (unsigned int i=0; i<lGear.size(); i++) {
172     vForces  += lGear[i]->UpdateForces();
173     vMoments += lGear[i]->GetMoments();
174   }
175 }
176
177 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178
179 bool FGGroundReactions::Load(Element* el)
180 {
181   int num=0;
182
183   Debug(2);
184
185   Element* contact_element = el->FindElement("contact");
186   while (contact_element) {
187     lGear.push_back(new FGLGear(contact_element, FDMExec, num++));
188     FDMExec->GetFCS()->AddGear(); // make the FCS aware of the landing gear
189     contact_element = el->FindNextElement("contact");
190   }
191   
192   FGModel::Load(el); // Perform base class Load
193
194   for (unsigned int i=0; i<lGear.size();i++) lGear[i]->bind();
195
196   PostLoad(el, PropertyManager);
197
198   return true;
199 }
200
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202
203 string FGGroundReactions::GetGroundReactionStrings(string delimeter) const
204 {
205   std::ostringstream buf;
206
207   for (unsigned int i=0;i<lGear.size();i++) {
208     if (lGear[i]->IsBogey()) {
209       string name = lGear[i]->GetName();
210       buf << name << " WOW" << delimeter
211           << name << " stroke (ft)" << delimeter
212           << name << " stroke velocity (ft/sec)" << delimeter
213           << name << " compress force (lbs)" << delimeter
214           << name << " wheel side force (lbs)" << delimeter
215           << name << " wheel roll force (lbs)" << delimeter
216           << name << " body X force (lbs)" << delimeter
217           << name << " body Y force (lbs)" << delimeter
218           << name << " wheel velocity vec X (ft/sec)" << delimeter
219           << name << " wheel velocity vec Y (ft/sec)" << delimeter
220           << name << " wheel rolling velocity (ft/sec)" << delimeter
221           << name << " wheel side velocity (ft/sec)" << delimeter
222           << name << " wheel slip (deg)" << delimeter;
223     }
224   }
225
226   buf << " Total Gear Force_X (lbs)" << delimeter
227       << " Total Gear Force_Y (lbs)" << delimeter
228       << " Total Gear Force_Z (lbs)" << delimeter
229       << " Total Gear Moment_L (ft-lbs)" << delimeter
230       << " Total Gear Moment_M (ft-lbs)" << delimeter
231       << " Total Gear Moment_N (ft-lbs)";
232
233   return buf.str();
234 }
235
236 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237
238 string FGGroundReactions::GetGroundReactionValues(string delimeter) const
239 {
240   std::ostringstream buf;
241
242   for (unsigned int i=0;i<lGear.size();i++) {
243     if (lGear[i]->IsBogey()) {
244       FGLGear *gear = lGear[i];
245       buf << (gear->GetWOW() ? "1" : "0") << delimeter
246           << setprecision(5) << gear->GetCompLen() << delimeter
247           << setprecision(6) << gear->GetCompVel() << delimeter
248           << setprecision(10) << gear->GetCompForce() << delimeter
249           << gear->GetWheelSideForce() << delimeter
250           << gear->GetWheelRollForce() << delimeter
251           << gear->GetBodyXForce() << delimeter
252           << gear->GetBodyYForce() << delimeter
253           << setprecision(6) << gear->GetWheelVel(eX) << delimeter
254           << gear->GetWheelVel(eY) << delimeter
255           << gear->GetWheelRollVel() << delimeter
256           << gear->GetWheelSideVel() << delimeter
257           << gear->GetWheelSlipAngle() << delimeter;
258     }
259   }
260
261   buf << vForces(eX) << delimeter
262       << vForces(eY) << delimeter
263       << vForces(eZ) << delimeter
264       << vMoments(eX) << delimeter
265       << vMoments(eY) << delimeter
266       << vMoments(eZ);
267
268   return buf.str();
269 }
270
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272
273 void FGGroundReactions::bind(void)
274 {
275   typedef double (FGGroundReactions::*PMF)(int) const;
276   PropertyManager->Tie("gear/num-units", this, &FGGroundReactions::GetNumGearUnits);
277   PropertyManager->Tie("gear/wow", this, &FGGroundReactions::GetWOW);
278   PropertyManager->Tie("moments/l-gear-lbsft", this, eL, (PMF)&FGGroundReactions::GetMoments);
279   PropertyManager->Tie("moments/m-gear-lbsft", this, eM, (PMF)&FGGroundReactions::GetMoments);
280   PropertyManager->Tie("moments/n-gear-lbsft", this, eN, (PMF)&FGGroundReactions::GetMoments);
281   PropertyManager->Tie("forces/fbx-gear-lbs", this, eX, (PMF)&FGGroundReactions::GetForces);
282   PropertyManager->Tie("forces/fby-gear-lbs", this, eY, (PMF)&FGGroundReactions::GetForces);
283   PropertyManager->Tie("forces/fbz-gear-lbs", this, eZ, (PMF)&FGGroundReactions::GetForces);
284 }
285
286 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 //    The bitmasked value choices are as follows:
288 //    unset: In this case (the default) JSBSim would only print
289 //       out the normally expected messages, essentially echoing
290 //       the config files as they are read. If the environment
291 //       variable is not set, debug_lvl is set to 1 internally
292 //    0: This requests JSBSim not to output any messages
293 //       whatsoever.
294 //    1: This value explicity requests the normal JSBSim
295 //       startup messages
296 //    2: This value asks for a message to be printed out when
297 //       a class is instantiated
298 //    4: When this value is set, a message is displayed when a
299 //       FGModel object executes its Run() method
300 //    8: When this value is set, various runtime state variables
301 //       are printed out periodically
302 //    16: When set various parameters are sanity checked and
303 //       a message is printed out when they go out of bounds
304
305 void FGGroundReactions::Debug(int from)
306 {
307   if (debug_lvl <= 0) return;
308
309   if (debug_lvl & 1) { // Standard console startup message output
310     if (from == 2) { // Loading
311       cout << endl << "  Ground Reactions: " << endl;
312     }
313   }
314   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
315     if (from == 0) cout << "Instantiated: FGGroundReactions" << endl;
316     if (from == 1) cout << "Destroyed:    FGGroundReactions" << endl;
317   }
318   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
319   }
320   if (debug_lvl & 8 ) { // Runtime state variables
321   }
322   if (debug_lvl & 16) { // Sanity checking
323   }
324   if (debug_lvl & 64) {
325     if (from == 0) { // Constructor
326       cout << IdSrc << endl;
327       cout << IdHdr << endl;
328     }
329   }
330 }
331 }