]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGLGear.cpp
Moved JSBSim.hxx to src/FDM/JSBSim/
[flightgear.git] / src / FDM / JSBSim / FGLGear.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGLGear.cpp
4  Author:       Jon S. Berndt
5                Norman H. Princen
6  Date started: 11/18/99
7  Purpose:      Encapsulates the landing gear elements
8  Called by:    FGAircraft
9
10  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
11
12  This program is free software; you can redistribute it and/or modify it under
13  the terms of the GNU General Public License as published by the Free Software
14  Foundation; either version 2 of the License, or (at your option) any later
15  version.
16
17  This program is distributed in the hope that it will be useful, but WITHOUT
18  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  details.
21
22  You should have received a copy of the GNU General Public License along with
23  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
24  Place - Suite 330, Boston, MA  02111-1307, USA.
25
26  Further information about the GNU General Public License can also be found on
27  the world wide web at http://www.gnu.org.
28
29 FUNCTIONAL DESCRIPTION
30 --------------------------------------------------------------------------------
31
32 HISTORY
33 --------------------------------------------------------------------------------
34 11/18/99   JSB   Created
35 01/30/01   NHP   Extended gear model to properly simulate steering and braking
36
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41 #include "FGLGear.h"
42 #include <algorithm>
43
44 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 DEFINITIONS
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 GLOBAL DATA
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51
52
53 static const char *IdSrc = "$Id$";
54 static const char *IdHdr = ID_LGEAR;
55
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 CLASS IMPLEMENTATION
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59
60 FGLGear::FGLGear(FGConfigFile* AC_cfg, FGFDMExec* fdmex) : Exec(fdmex)
61 {
62   string tmp;
63   
64   *AC_cfg >> tmp >> name >> vXYZ(1) >> vXYZ(2) >> vXYZ(3)
65             >> kSpring >> bDamp>> dynamicFCoeff >> staticFCoeff
66                   >> rollingFCoeff >> sSteerType >> sBrakeGroup 
67                      >> maxSteerAngle >> sRetractable;
68
69   if      (sBrakeGroup == "LEFT"  ) eBrakeGrp = bgLeft;
70   else if (sBrakeGroup == "RIGHT" ) eBrakeGrp = bgRight;
71   else if (sBrakeGroup == "CENTER") eBrakeGrp = bgCenter;
72   else if (sBrakeGroup == "NOSE"  ) eBrakeGrp = bgNose;
73   else if (sBrakeGroup == "TAIL"  ) eBrakeGrp = bgTail;
74   else if (sBrakeGroup == "NONE"  ) eBrakeGrp = bgNone;
75   else {
76     cerr << "Improper braking group specification in config file: "
77          << sBrakeGroup << " is undefined." << endl;
78   }
79
80   if      (sSteerType == "STEERABLE") eSteerType = stSteer;
81   else if (sSteerType == "FIXED"    ) eSteerType = stFixed;
82   else if (sSteerType == "CASTERED" ) eSteerType = stCaster;
83   else {
84     cerr << "Improper steering type specification in config file: "
85          << sSteerType << " is undefined." << endl;
86   }
87   
88   if ( sRetractable == "RETRACT" ) {
89     isRetractable = true;
90   } else  {
91     isRetractable = false;
92   }  
93   
94   GearUp = false;
95   GearDown = true;
96
97 // Add some AI here to determine if gear is located properly according to its
98 // brake group type ??
99
100   State       = Exec->GetState();
101   Aircraft    = Exec->GetAircraft();
102   Position    = Exec->GetPosition();
103   Rotation    = Exec->GetRotation();
104   FCS         = Exec->GetFCS();
105   MassBalance = Exec->GetMassBalance();
106
107   WOW = lastWOW = false;
108   ReportEnable = true;
109   FirstContact = false;
110   Reported = false;
111   DistanceTraveled = 0.0;
112   MaximumStrutForce = MaximumStrutTravel = 0.0;
113   SinkRate = GroundSpeed = 0.0;
114
115   vWhlBodyVec     = (vXYZ - MassBalance->GetXYZcg()) / 12.0;
116   vWhlBodyVec(eX) = -vWhlBodyVec(eX);
117   vWhlBodyVec(eZ) = -vWhlBodyVec(eZ);
118
119   vLocalGear = State->GetTb2l() * vWhlBodyVec;
120
121   Debug(0);
122 }
123
124 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125
126 FGLGear::FGLGear(const FGLGear& lgear)
127 {
128   State    = lgear.State;
129   Aircraft = lgear.Aircraft;
130   Position = lgear.Position;
131   Rotation = lgear.Rotation;
132   Exec     = lgear.Exec;
133   FCS      = lgear.FCS;
134   MassBalance = lgear.MassBalance;
135
136   vXYZ = lgear.vXYZ;
137   vMoment = lgear.vMoment;
138   vWhlBodyVec = lgear.vWhlBodyVec;
139   vLocalGear = lgear.vLocalGear;
140
141   WOW                = lgear.WOW;
142   lastWOW            = lgear.lastWOW;
143   ReportEnable       = lgear.ReportEnable;
144   FirstContact       = lgear.FirstContact;
145   DistanceTraveled   = lgear.DistanceTraveled;
146   MaximumStrutForce  = lgear.MaximumStrutForce;
147   MaximumStrutTravel = lgear.MaximumStrutTravel;
148
149   kSpring         = lgear.kSpring;
150   bDamp           = lgear.bDamp;
151   compressLength  = lgear.compressLength;
152   compressSpeed   = lgear.compressSpeed;
153   staticFCoeff    = lgear.staticFCoeff;
154   dynamicFCoeff   = lgear.dynamicFCoeff;
155   rollingFCoeff   = lgear.rollingFCoeff;
156   brakePct        = lgear.brakePct;
157   maxCompLen      = lgear.maxCompLen;
158   SinkRate        = lgear.SinkRate;
159   GroundSpeed     = lgear.GroundSpeed;
160   Reported        = lgear.Reported;
161   name            = lgear.name;
162   sSteerType      = lgear.sSteerType;
163   sRetractable    = lgear.sRetractable;
164   eSteerType      = lgear.eSteerType;
165   sBrakeGroup     = lgear.sBrakeGroup;
166   eBrakeGrp       = lgear.eBrakeGrp;
167   maxSteerAngle   = lgear.maxSteerAngle;
168   isRetractable   = lgear.isRetractable;
169   GearUp          = lgear.GearUp;
170   GearDown        = lgear.GearDown;
171 }
172
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174
175 FGLGear::~FGLGear()
176 {
177   Debug(1);
178 }
179
180 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181
182 FGColumnVector3& FGLGear::Force(void)
183 {
184   double SteerGain = 0;
185   double SinWheel, CosWheel;
186
187   vForce.InitMatrix();
188   vMoment.InitMatrix();
189
190   if (isRetractable) {
191     if (FCS->GetGearPos() < 0.01) {
192       GearUp   = true;
193       GearDown = false;
194      } else if (FCS->GetGearPos() > 0.99) {
195       GearDown = true;
196       GearUp   = false;
197      } else {
198       GearUp   = false;
199       GearDown = false;
200      }
201   } else {
202       GearUp   = false;
203       GearDown = true;
204   }         
205       
206   if (GearDown) {
207
208     vWhlBodyVec     = (vXYZ - MassBalance->GetXYZcg()) / 12.0;
209     vWhlBodyVec(eX) = -vWhlBodyVec(eX);
210     vWhlBodyVec(eZ) = -vWhlBodyVec(eZ);
211
212 // vWhlBodyVec now stores the vector from the cg to this wheel
213
214     vLocalGear = State->GetTb2l() * vWhlBodyVec;
215
216 // vLocalGear now stores the vector from the cg to the wheel in local coords.
217
218     compressLength = vLocalGear(eZ) - Position->GetDistanceAGL();
219
220 // The compression length is currently measured in the Z-axis, only, at this time.
221 // It should be measured along the strut axis. If the local-frame gear position
222 // "hangs down" below the CG greater than the altitude, then the compressLength
223 // will be positive - i.e. the gear will have made contact.
224
225     if (compressLength > 0.00) {
226
227       WOW = true;// Weight-On-Wheels is true
228
229 // The next equation should really use the vector to the contact patch of the tire
230 // including the strut compression and not vWhlBodyVec.  Will fix this later.
231 // As it stands, now, the following equation takes the aircraft body-frame
232 // rotational rate and calculates the cross-product with the vector from the CG
233 // to the wheel, thus producing the instantaneous velocity vector of the tire
234 // in Body coords. The frame is also converted to local coordinates. When the
235 // aircraft local-frame velocity is added to this quantity, the total velocity of
236 // the wheel in local frame is then known. Subsequently, the compression speed
237 // (used for calculating damping force) is found by taking the Z-component of the
238 // wheel velocity.
239
240       vWhlVelVec      =  State->GetTb2l() * (Rotation->GetPQR() * vWhlBodyVec);
241
242       vWhlVelVec     +=  Position->GetVel();
243
244       compressSpeed   =  vWhlVelVec(eZ);
245
246 // If this is the first time the wheel has made contact, remember some values
247 // for later printout.
248
249       if (!FirstContact) {
250         FirstContact  = true;
251         SinkRate      =  compressSpeed;
252         GroundSpeed   =  Position->GetVel().Magnitude();
253       }
254
255 // The following needs work regarding friction coefficients and braking and
256 // steering The BrakeFCoeff formula assumes that an anti-skid system is used.
257 // It also assumes that we won't be turning and braking at the same time.
258 // Will fix this later.
259 // [JSB] The braking force coefficients include normal rolling coefficient +
260 // a percentage of the static friction coefficient based on braking applied.
261
262       switch (eBrakeGrp) {
263       case bgLeft:
264         SteerGain = -0.10;
265         BrakeFCoeff = rollingFCoeff*(1.0 - FCS->GetBrake(bgLeft)) +
266                                               staticFCoeff*FCS->GetBrake(bgLeft);
267         break;
268       case bgRight:
269         SteerGain = -0.10;
270         BrakeFCoeff = rollingFCoeff*(1.0 - FCS->GetBrake(bgRight)) +
271                                              staticFCoeff*FCS->GetBrake(bgRight);
272         break;
273       case bgCenter:
274         SteerGain = -0.10;
275         BrakeFCoeff = rollingFCoeff*(1.0 - FCS->GetBrake(bgCenter)) +
276                                              staticFCoeff*FCS->GetBrake(bgCenter);
277         break;
278       case bgNose:
279         SteerGain = 0.10;
280         BrakeFCoeff = rollingFCoeff;
281         break;
282       case bgTail:
283         SteerGain = -0.10;
284         BrakeFCoeff = rollingFCoeff;
285         break;
286       case bgNone:
287         SteerGain = -0.10;
288         BrakeFCoeff = rollingFCoeff;
289         break;
290       default:
291         cerr << "Improper brake group membership detected for this gear." << endl;
292         break;
293       }
294
295       switch (eSteerType) {
296       case stSteer:
297         SteerAngle = SteerGain*FCS->GetDrPos();
298         break;
299       case stFixed:
300         SteerAngle = 0.0;
301         break;
302       case stCaster:
303 // Note to Jon: This is not correct for castering gear.  I'll fix it later.
304         SteerAngle = 0.0;
305         break;
306       default:
307         cerr << "Improper steering type membership detected for this gear." << endl;
308         break;
309       }
310
311 // Transform the wheel velocities from the local axis system to the wheel axis system.
312 // For now, steering angle is assumed to happen in the Local Z axis,
313 // not the strut axis as it should be.  Will fix this later.
314
315       SinWheel      = sin(Rotation->Getpsi() + SteerAngle);
316       CosWheel      = cos(Rotation->Getpsi() + SteerAngle);
317       RollingWhlVel = vWhlVelVec(eX)*CosWheel + vWhlVelVec(eY)*SinWheel;
318       SideWhlVel    = vWhlVelVec(eY)*CosWheel - vWhlVelVec(eX)*SinWheel;
319
320 // Calculate tire slip angle.
321
322       if (RollingWhlVel == 0.0 && SideWhlVel == 0.0) {
323         WheelSlip = 0.0;
324       } else {
325         WheelSlip = radtodeg*atan2(SideWhlVel, RollingWhlVel);
326       }
327
328 // Compute the sideforce coefficients using similar assumptions to LaRCSim for now.
329 // Allow a maximum of 10 degrees tire slip angle before wheel slides.  At that point,
330 // transition from static to dynamic friction.  There are more complicated formulations
331 // of this that avoid the discrete jump.  Will fix this later.
332
333       if (fabs(WheelSlip) <= 10.0) {
334         FCoeff = staticFCoeff*WheelSlip/10.0;
335       } else {
336         FCoeff = dynamicFCoeff*fabs(WheelSlip)/WheelSlip;
337       }
338
339 // Compute the vertical force on the wheel using square-law damping (per comment
340 // in paper AIAA-2000-4303 - see header prologue comments). We might consider
341 // allowing for both square and linear damping force calculation. Also need to
342 // possibly give a "rebound damping factor" that differs from the compression
343 // case.
344
345       vLocalForce(eZ) =  min(-compressLength * kSpring
346                              - compressSpeed * bDamp, (double)0.0);
347
348       MaximumStrutForce = max(MaximumStrutForce, fabs(vLocalForce(eZ)));
349       MaximumStrutTravel = max(MaximumStrutTravel, fabs(compressLength));
350
351 // Compute the forces in the wheel ground plane.
352
353       RollingForce = 0;
354       if (fabs(RollingWhlVel) > 1E-3) {
355         RollingForce = vLocalForce(eZ) * BrakeFCoeff * fabs(RollingWhlVel)/RollingWhlVel;
356       }
357       SideForce    = vLocalForce(eZ) * FCoeff;
358
359 // Transform these forces back to the local reference frame.
360
361       vLocalForce(eX) = RollingForce*CosWheel - SideForce*SinWheel;
362       vLocalForce(eY) = SideForce*CosWheel    + RollingForce*SinWheel;
363
364 // Note to Jon: At this point the forces will be too big when the airplane is
365 // stopped or rolling to a stop.  We need to make sure that the gear forces just
366 // balance out the non-gear forces when the airplane is stopped.  That way the
367 // airplane won't start to accelerate until the non-gear/ forces are larger than
368 // the gear forces.  I think that the proper fix should go into FGAircraft::FMGear.
369 // This routine would only compute the local strut forces and return them to
370 // FMGear. All of the gear forces would get adjusted in FMGear using the total
371 // non-gear forces. Then the gear moments would be calculated. If strange things
372 // start happening to the airplane during testing as it rolls to a stop, then we
373 // need to implement this change.  I ran out of time to do it now but have the
374 // equations.
375
376 // Transform the forces back to the body frame and compute the moment.
377
378       vForce  = State->GetTl2b() * vLocalForce;
379       vMoment = vWhlBodyVec * vForce;
380
381     } else {
382
383       WOW = false;
384
385       if (Position->GetDistanceAGL() > 200.0) {
386         FirstContact = false;
387         Reported = false;
388         DistanceTraveled = 0.0;
389         MaximumStrutForce = MaximumStrutTravel = 0.0;
390       }
391
392       compressLength = 0.0; // reset compressLength to zero for data output validity
393     }
394
395     if (FirstContact) {
396       DistanceTraveled += Position->GetVel().Magnitude()*State->Getdt()*Aircraft->GetRate();
397     }
398   
399     if (ReportEnable && Position->GetVel().Magnitude() <= 0.05 && !Reported) {
400       if (debug_lvl > 0) Report();
401     }
402
403     if (lastWOW != WOW) {
404       PutMessage("GEAR_CONTACT", WOW);
405     }
406
407     lastWOW = WOW;
408
409 // Crash detection logic (really out-of-bounds detection)
410
411     if (compressLength > 500.0 ||
412         vForce.Magnitude() > 100000000.0 ||
413         vMoment.Magnitude() > 5000000000.0 ||
414         SinkRate > 1.4666*30)
415     {
416       PutMessage("Crash Detected");
417       Exec->Freeze();
418     }
419   } 
420   return vForce; 
421 }
422
423 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424
425 void FGLGear::Report(void)
426 {
427   cout << endl << "Touchdown report for " << name << endl;
428   cout << "  Sink rate at contact:  " << SinkRate                << " fps,    "
429                               << SinkRate*0.3408          << " mps"     << endl;
430   cout << "  Contact ground speed:  " << GroundSpeed*.5925       << " knots,  "
431                               << GroundSpeed*0.3408       << " mps"     << endl;
432   cout << "  Maximum contact force: " << MaximumStrutForce       << " lbs,    "
433                               << MaximumStrutForce*4.448  << " Newtons" << endl;
434   cout << "  Maximum strut travel:  " << MaximumStrutTravel*12.0 << " inches, "
435                               << MaximumStrutTravel*30.48 << " cm"      << endl;
436   cout << "  Distance traveled:     " << DistanceTraveled        << " ft,     "
437                               << DistanceTraveled*0.3408  << " meters"  << endl;
438   Reported = true;
439 }
440
441 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 //    The bitmasked value choices are as follows:
443 //    unset: In this case (the default) JSBSim would only print
444 //       out the normally expected messages, essentially echoing
445 //       the config files as they are read. If the environment
446 //       variable is not set, debug_lvl is set to 1 internally
447 //    0: This requests JSBSim not to output any messages
448 //       whatsoever.
449 //    1: This value explicity requests the normal JSBSim
450 //       startup messages
451 //    2: This value asks for a message to be printed out when
452 //       a class is instantiated
453 //    4: When this value is set, a message is displayed when a
454 //       FGModel object executes its Run() method
455 //    8: When this value is set, various runtime state variables
456 //       are printed out periodically
457 //    16: When set various parameters are sanity checked and
458 //       a message is printed out when they go out of bounds
459
460 void FGLGear::Debug(int from)
461 {
462   if (debug_lvl <= 0) return;
463
464   if (debug_lvl & 1) { // Standard console startup message output
465     if (from == 0) { // Constructor
466       cout << "    Name: "               << name          << endl;
467       cout << "      Location: "         << vXYZ          << endl;
468       cout << "      Spring Constant:  " << kSpring       << endl;
469       cout << "      Damping Constant: " << bDamp         << endl;
470       cout << "      Dynamic Friction: " << dynamicFCoeff << endl;
471       cout << "      Static Friction:  " << staticFCoeff  << endl;
472       cout << "      Rolling Friction: " << rollingFCoeff << endl;
473       cout << "      Steering Type:    " << sSteerType    << endl;
474       cout << "      Grouping:         " << sBrakeGroup   << endl;
475       cout << "      Max Steer Angle:  " << maxSteerAngle << endl;
476       cout << "      Retractable:      " << sRetractable  << endl;
477     }
478   }
479   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
480     if (from == 0) cout << "Instantiated: FGLGear" << endl;
481     if (from == 1) cout << "Destroyed:    FGLGear" << endl;
482   }
483   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
484   }
485   if (debug_lvl & 8 ) { // Runtime state variables
486   }
487   if (debug_lvl & 16) { // Sanity checking
488   }
489   if (debug_lvl & 64) {
490     if (from == 0) { // Constructor
491       cout << IdSrc << endl;
492       cout << IdHdr << endl;
493     }
494   }
495 }
496