]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGGroundReactions.cpp
Andreas Gaeb: fix #222 (JSBSIm reset problems)
[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.31 2010/11/18 12:38:06 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   if (!FGModel::InitModel()) return false;
120
121   return true;
122 }
123
124 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125
126 bool FGGroundReactions::Run(void)
127 {
128   if (FGModel::Run()) return true;
129   if (FDMExec->Holding()) return false;
130
131   RunPreFunctions();
132
133   vForces.InitMatrix();
134   vMoments.InitMatrix();
135
136   // Sum forces and moments for all gear, here.
137   // Some optimizations may be made here - or rather in the gear code itself.
138   // The gear ::Run() method is called several times - once for each gear.
139   // Perhaps there is some commonality for things which only need to be
140   // calculated once.
141   for (unsigned int i=0; i<lGear.size(); i++) {
142     vForces  += lGear[i]->GetBodyForces();
143     vMoments += lGear[i]->GetMoments();
144   }
145
146   RunPostFunctions();
147
148   return false;
149 }
150
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152
153 bool FGGroundReactions::GetWOW(void) const
154 {
155   bool result = false;
156   for (unsigned int i=0; i<lGear.size(); i++) {
157     if (lGear[i]->IsBogey() && lGear[i]->GetWOW()) {
158       result = true;
159       break;
160     }
161   }
162   return result;
163 }
164
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 // This function must be called after friction forces are resolved in order to
167 // include them in the ground reactions total force and moment.
168 void FGGroundReactions::UpdateForcesAndMoments(void)
169 {
170   vForces.InitMatrix();
171   vMoments.InitMatrix();
172
173   for (unsigned int i=0; i<lGear.size(); i++) {
174     vForces  += lGear[i]->UpdateForces();
175     vMoments += lGear[i]->GetMoments();
176   }
177 }
178
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180
181 bool FGGroundReactions::Load(Element* el)
182 {
183   int num=0;
184
185   Debug(2);
186
187   Element* contact_element = el->FindElement("contact");
188   while (contact_element) {
189     lGear.push_back(new FGLGear(contact_element, FDMExec, num++));
190     FDMExec->GetFCS()->AddGear(); // make the FCS aware of the landing gear
191     contact_element = el->FindNextElement("contact");
192   }
193   
194   FGModel::Load(el); // Perform base class Load
195
196   for (unsigned int i=0; i<lGear.size();i++) lGear[i]->bind();
197
198   PostLoad(el, PropertyManager);
199
200   return true;
201 }
202
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205 string FGGroundReactions::GetGroundReactionStrings(string delimeter) const
206 {
207   std::ostringstream buf;
208
209   for (unsigned int i=0;i<lGear.size();i++) {
210     if (lGear[i]->IsBogey()) {
211       string name = lGear[i]->GetName();
212       buf << name << " WOW" << delimeter
213           << name << " stroke (ft)" << delimeter
214           << name << " stroke velocity (ft/sec)" << delimeter
215           << name << " compress force (lbs)" << delimeter
216           << name << " wheel side force (lbs)" << delimeter
217           << name << " wheel roll force (lbs)" << delimeter
218           << name << " body X force (lbs)" << delimeter
219           << name << " body Y force (lbs)" << delimeter
220           << name << " wheel velocity vec X (ft/sec)" << delimeter
221           << name << " wheel velocity vec Y (ft/sec)" << delimeter
222           << name << " wheel rolling velocity (ft/sec)" << delimeter
223           << name << " wheel side velocity (ft/sec)" << delimeter
224           << name << " wheel slip (deg)" << delimeter;
225     }
226   }
227
228   buf << " Total Gear Force_X (lbs)" << delimeter
229       << " Total Gear Force_Y (lbs)" << delimeter
230       << " Total Gear Force_Z (lbs)" << delimeter
231       << " Total Gear Moment_L (ft-lbs)" << delimeter
232       << " Total Gear Moment_M (ft-lbs)" << delimeter
233       << " Total Gear Moment_N (ft-lbs)";
234
235   return buf.str();
236 }
237
238 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239
240 string FGGroundReactions::GetGroundReactionValues(string delimeter) const
241 {
242   std::ostringstream buf;
243
244   for (unsigned int i=0;i<lGear.size();i++) {
245     if (lGear[i]->IsBogey()) {
246       FGLGear *gear = lGear[i];
247       buf << (gear->GetWOW() ? "1" : "0") << delimeter
248           << setprecision(5) << gear->GetCompLen() << delimeter
249           << setprecision(6) << gear->GetCompVel() << delimeter
250           << setprecision(10) << gear->GetCompForce() << delimeter
251           << gear->GetWheelSideForce() << delimeter
252           << gear->GetWheelRollForce() << delimeter
253           << gear->GetBodyXForce() << delimeter
254           << gear->GetBodyYForce() << delimeter
255           << setprecision(6) << gear->GetWheelVel(eX) << delimeter
256           << gear->GetWheelVel(eY) << delimeter
257           << gear->GetWheelRollVel() << delimeter
258           << gear->GetWheelSideVel() << delimeter
259           << gear->GetWheelSlipAngle() << delimeter;
260     }
261   }
262
263   buf << vForces(eX) << delimeter
264       << vForces(eY) << delimeter
265       << vForces(eZ) << delimeter
266       << vMoments(eX) << delimeter
267       << vMoments(eY) << delimeter
268       << vMoments(eZ);
269
270   return buf.str();
271 }
272
273 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274
275 void FGGroundReactions::bind(void)
276 {
277   typedef double (FGGroundReactions::*PMF)(int) const;
278   PropertyManager->Tie("gear/num-units", this, &FGGroundReactions::GetNumGearUnits);
279   PropertyManager->Tie("gear/wow", this, &FGGroundReactions::GetWOW);
280   PropertyManager->Tie("moments/l-gear-lbsft", this, eL, (PMF)&FGGroundReactions::GetMoments);
281   PropertyManager->Tie("moments/m-gear-lbsft", this, eM, (PMF)&FGGroundReactions::GetMoments);
282   PropertyManager->Tie("moments/n-gear-lbsft", this, eN, (PMF)&FGGroundReactions::GetMoments);
283   PropertyManager->Tie("forces/fbx-gear-lbs", this, eX, (PMF)&FGGroundReactions::GetForces);
284   PropertyManager->Tie("forces/fby-gear-lbs", this, eY, (PMF)&FGGroundReactions::GetForces);
285   PropertyManager->Tie("forces/fbz-gear-lbs", this, eZ, (PMF)&FGGroundReactions::GetForces);
286 }
287
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 //    The bitmasked value choices are as follows:
290 //    unset: In this case (the default) JSBSim would only print
291 //       out the normally expected messages, essentially echoing
292 //       the config files as they are read. If the environment
293 //       variable is not set, debug_lvl is set to 1 internally
294 //    0: This requests JSBSim not to output any messages
295 //       whatsoever.
296 //    1: This value explicity requests the normal JSBSim
297 //       startup messages
298 //    2: This value asks for a message to be printed out when
299 //       a class is instantiated
300 //    4: When this value is set, a message is displayed when a
301 //       FGModel object executes its Run() method
302 //    8: When this value is set, various runtime state variables
303 //       are printed out periodically
304 //    16: When set various parameters are sanity checked and
305 //       a message is printed out when they go out of bounds
306
307 void FGGroundReactions::Debug(int from)
308 {
309   if (debug_lvl <= 0) return;
310
311   if (debug_lvl & 1) { // Standard console startup message output
312     if (from == 2) { // Loading
313       cout << endl << "  Ground Reactions: " << endl;
314     }
315   }
316   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
317     if (from == 0) cout << "Instantiated: FGGroundReactions" << endl;
318     if (from == 1) cout << "Destroyed:    FGGroundReactions" << endl;
319   }
320   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
321   }
322   if (debug_lvl & 8 ) { // Runtime state variables
323   }
324   if (debug_lvl & 16) { // Sanity checking
325   }
326   if (debug_lvl & 64) {
327     if (from == 0) { // Constructor
328       cout << IdSrc << endl;
329       cout << IdHdr << endl;
330     }
331   }
332 }
333 }