]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGLGear.cpp
e3cfeadf21f44c6eed4f0fa465332458e9b7c55b
[flightgear.git] / src / FDM / JSBSim / models / FGLGear.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGLGear.cpp
4  Author:       Jon S. Berndt
5                Norman H. Princen
6                Bertrand Coconnier
7  Date started: 11/18/99
8  Purpose:      Encapsulates the landing gear elements
9  Called by:    FGAircraft
10
11  ------------- Copyright (C) 1999  Jon S. Berndt (jon@jsbsim.org) -------------
12
13  This program is free software; you can redistribute it and/or modify it under
14  the terms of the GNU Lesser General Public License as published by the Free Software
15  Foundation; either version 2 of the License, or (at your option) any later
16  version.
17
18  This program is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
21  details.
22
23  You should have received a copy of the GNU Lesser General Public License along with
24  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
25  Place - Suite 330, Boston, MA  02111-1307, USA.
26
27  Further information about the GNU Lesser General Public License can also be found on
28  the world wide web at http://www.gnu.org.
29
30 FUNCTIONAL DESCRIPTION
31 --------------------------------------------------------------------------------
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 11/18/99   JSB   Created
36 01/30/01   NHP   Extended gear model to properly simulate steering and braking
37 07/08/09   BC    Modified gear model to support large angles between aircraft and ground
38
39 /%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 INCLUDES
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42
43 #include "FGLGear.h"
44
45 namespace JSBSim {
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 DEFINITIONS
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 GLOBAL DATA
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55 static const char *IdSrc = "$Id$";
56 static const char *IdHdr = ID_LGEAR;
57
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 CLASS IMPLEMENTATION
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61
62 FGLGear::FGLGear(Element* el, FGFDMExec* fdmex, int number) :
63   GearNumber(number),
64   Exec(fdmex)
65 {
66   Element *force_table=0;
67   Element *dampCoeff=0;
68   Element *dampCoeffRebound=0;
69   string force_type="";
70
71   kSpring = bDamp = bDampRebound = dynamicFCoeff = staticFCoeff = rollingFCoeff = maxSteerAngle = 0;
72   sSteerType = sBrakeGroup = sSteerType = "";
73   isRetractable = 0;
74   eDampType = dtLinear;
75   eDampTypeRebound = dtLinear;
76
77   name = el->GetAttributeValue("name");
78   sContactType = el->GetAttributeValue("type");
79   if (sContactType == "BOGEY") {
80     eContactType = ctBOGEY;
81   } else if (sContactType == "STRUCTURE") {
82     eContactType = ctSTRUCTURE;
83   } else {
84     eContactType = ctUNKNOWN;
85   }
86
87   if (el->FindElement("spring_coeff"))
88     kSpring = el->FindElementValueAsNumberConvertTo("spring_coeff", "LBS/FT");
89   if (el->FindElement("damping_coeff")) {
90     dampCoeff = el->FindElement("damping_coeff");
91     if (dampCoeff->GetAttributeValue("type") == "SQUARE") {
92       eDampType = dtSquare;
93       bDamp   = el->FindElementValueAsNumberConvertTo("damping_coeff", "LBS/FT2/SEC2");
94     } else {
95       bDamp   = el->FindElementValueAsNumberConvertTo("damping_coeff", "LBS/FT/SEC");
96     }
97   }
98
99   if (el->FindElement("damping_coeff_rebound")) {
100     dampCoeffRebound = el->FindElement("damping_coeff_rebound");
101     if (dampCoeffRebound->GetAttributeValue("type") == "SQUARE") {
102       eDampTypeRebound = dtSquare;
103       bDampRebound   = el->FindElementValueAsNumberConvertTo("damping_coeff_rebound", "LBS/FT2/SEC2");
104     } else {
105       bDampRebound   = el->FindElementValueAsNumberConvertTo("damping_coeff_rebound", "LBS/FT/SEC");
106     }
107   } else {
108     bDampRebound   = bDamp;
109     eDampTypeRebound = eDampType;
110   }
111
112   if (el->FindElement("dynamic_friction"))
113     dynamicFCoeff = el->FindElementValueAsNumber("dynamic_friction");
114   if (el->FindElement("static_friction"))
115     staticFCoeff = el->FindElementValueAsNumber("static_friction");
116   if (el->FindElement("rolling_friction"))
117     rollingFCoeff = el->FindElementValueAsNumber("rolling_friction");
118   if (el->FindElement("max_steer"))
119     maxSteerAngle = el->FindElementValueAsNumberConvertTo("max_steer", "DEG");
120   if (el->FindElement("retractable"))
121     isRetractable = ((unsigned int)el->FindElementValueAsNumber("retractable"))>0.0?true:false;
122
123   ForceY_Table = 0;
124   force_table = el->FindElement("table");
125   while (force_table) {
126     force_type = force_table->GetAttributeValue("type");
127     if (force_type == "CORNERING_COEFF") {
128       ForceY_Table = new FGTable(Exec->GetPropertyManager(), force_table);
129     } else {
130       cerr << "Undefined force table for " << name << " contact point" << endl;
131     }
132     force_table = el->FindNextElement("table");
133   }
134
135   sBrakeGroup = el->FindElementValue("brake_group");
136
137   if (maxSteerAngle == 360) sSteerType = "CASTERED";
138   else if (maxSteerAngle == 0.0) sSteerType = "FIXED";
139   else sSteerType = "STEERABLE";
140
141   Element* element = el->FindElement("location");
142   if (element) vXYZ = element->FindElementTripletConvertTo("IN");
143   else {cerr << "No location given for contact " << name << endl; exit(-1);}
144
145   if      (sBrakeGroup == "LEFT"  ) eBrakeGrp = bgLeft;
146   else if (sBrakeGroup == "RIGHT" ) eBrakeGrp = bgRight;
147   else if (sBrakeGroup == "CENTER") eBrakeGrp = bgCenter;
148   else if (sBrakeGroup == "NOSE"  ) eBrakeGrp = bgNose;
149   else if (sBrakeGroup == "TAIL"  ) eBrakeGrp = bgTail;
150   else if (sBrakeGroup == "NONE"  ) eBrakeGrp = bgNone;
151   else if (sBrakeGroup.empty()    ) {eBrakeGrp = bgNone;
152                                      sBrakeGroup = "NONE (defaulted)";}
153   else {
154     cerr << "Improper braking group specification in config file: "
155          << sBrakeGroup << " is undefined." << endl;
156   }
157
158   if      (sSteerType == "STEERABLE") eSteerType = stSteer;
159   else if (sSteerType == "FIXED"    ) eSteerType = stFixed;
160   else if (sSteerType == "CASTERED" ) eSteerType = stCaster;
161   else if (sSteerType.empty()       ) {eSteerType = stFixed;
162                                        sSteerType = "FIXED (defaulted)";}
163   else {
164     cerr << "Improper steering type specification in config file: "
165          << sSteerType << " is undefined." << endl;
166   }
167
168   RFRV = 0.7;  // Rolling force relaxation velocity, default value
169   SFRV = 0.7;  // Side force relaxation velocity, default value
170
171   Element* relax_vel = el->FindElement("relaxation_velocity");
172   if (relax_vel) {
173     if (relax_vel->FindElement("rolling")) {
174       RFRV = relax_vel->FindElementValueAsNumberConvertTo("rolling", "FT/SEC");
175     }
176     if (relax_vel->FindElement("side")) {
177       SFRV = relax_vel->FindElementValueAsNumberConvertTo("side", "FT/SEC");
178     }
179   }
180
181   State = Exec->GetState();
182   LongForceLagFilterCoeff = 1/State->Getdt(); // default longitudinal force filter coefficient
183   LatForceLagFilterCoeff  = 1/State->Getdt(); // default lateral force filter coefficient
184
185   Element* force_lag_filter_elem = el->FindElement("force_lag_filter");
186   if (force_lag_filter_elem) {
187     if (force_lag_filter_elem->FindElement("rolling")) {
188       LongForceLagFilterCoeff = force_lag_filter_elem->FindElementValueAsNumber("rolling");
189     }
190     if (force_lag_filter_elem->FindElement("side")) {
191       LatForceLagFilterCoeff = force_lag_filter_elem->FindElementValueAsNumber("side");
192     }
193   }
194
195   LongForceFilter = Filter(LongForceLagFilterCoeff, State->Getdt());
196   LatForceFilter = Filter(LatForceLagFilterCoeff, State->Getdt());
197
198   WheelSlipLagFilterCoeff = 1/State->Getdt();
199
200   Element *wheel_slip_angle_lag_elem = el->FindElement("wheel_slip_filter");
201   if (wheel_slip_angle_lag_elem) {
202     WheelSlipLagFilterCoeff = wheel_slip_angle_lag_elem->GetDataAsNumber();
203   }
204   
205   WheelSlipFilter = Filter(WheelSlipLagFilterCoeff, State->Getdt());
206
207   GearUp = false;
208   GearDown = true;
209   GearPos  = 1.0;
210   useFCSGearPos = false;
211   Servicable = true;
212
213 // Add some AI here to determine if gear is located properly according to its
214 // brake group type ??
215
216   State       = Exec->GetState();
217   Aircraft    = Exec->GetAircraft();
218   Propagate   = Exec->GetPropagate();
219   Auxiliary   = Exec->GetAuxiliary();
220   FCS         = Exec->GetFCS();
221   MassBalance = Exec->GetMassBalance();
222
223   WOW = lastWOW = false;
224   ReportEnable = true;
225   FirstContact = false;
226   StartedGroundRun = false;
227   TakeoffReported = LandingReported = false;
228   LandingDistanceTraveled = TakeoffDistanceTraveled = TakeoffDistanceTraveled50ft = 0.0;
229   MaximumStrutForce = MaximumStrutTravel = 0.0;
230   SinkRate = GroundSpeed = 0.0;
231
232   vWhlBodyVec = MassBalance->StructuralToBody(vXYZ);
233
234   vLocalGear = Propagate->GetTb2l() * vWhlBodyVec;
235
236   vLocalWhlVel.InitMatrix();
237
238   compressLength  = 0.0;
239   compressSpeed   = 0.0;
240   brakePct        = 0.0;
241   maxCompLen      = 0.0;
242
243   WheelSlip = 0.0;
244   TirePressureNorm = 1.0;
245
246   // Set Pacejka terms
247
248   Stiffness = 0.06;
249   Shape = 2.8;
250   Peak = staticFCoeff;
251   Curvature = 1.03;
252
253   Debug(0);
254 }
255
256 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257
258 FGLGear::~FGLGear()
259 {
260   delete ForceY_Table;
261   Debug(1);
262 }
263
264 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265
266 FGColumnVector3& FGLGear::Force(void)
267 {
268   double t = Exec->GetState()->Getsim_time();
269   dT = State->Getdt()*Exec->GetGroundReactions()->GetRate();
270
271   vForce.InitMatrix();
272   vLocalForce.InitMatrix();
273   vMoment.InitMatrix();
274
275   if (isRetractable) ComputeRetractionState();
276
277   if (GearDown) {
278
279     vWhlBodyVec = MassBalance->StructuralToBody(vXYZ); // Get wheel in body frame
280     vLocalGear = Propagate->GetTb2l() * vWhlBodyVec; // Get local frame wheel location
281
282     gearLoc = Propagate->GetLocation().LocalToLocation(vLocalGear);
283     // Compute the height of the theoretical location of the wheel (if strut is not compressed) with
284     // respect to the ground level
285     double height = Exec->GetGroundCallback()->GetAGLevel(t, gearLoc, contact, normal, cvel);
286     vGroundNormal = -1. * Propagate->GetTec2b() * normal;
287
288     switch (eContactType) {
289     case ctBOGEY:
290       // Project the height in the local coordinate frame of the strut to compute the actual compression
291       // length. The strut is assumed to be parallel to Z in the body frame.
292       compressLength = vGroundNormal(eZ) < 0.0 ? height / vGroundNormal(eZ) : 0.0;
293       break;
294     case ctSTRUCTURE:
295       compressLength = -height;
296       break;
297     }
298
299     if (compressLength > 0.00) {
300
301       WOW = true;
302
303       // [The next equation should really use the vector to the contact patch of
304       // the tire including the strut compression and not the original vWhlBodyVec.]
305
306       FGColumnVector3 vWhlContactVec = vWhlBodyVec - FGColumnVector3(0., 0., compressLength);
307       vWhlVelVec      =  Propagate->GetPQR() * vWhlContactVec;
308       vWhlVelVec     +=  Propagate->GetUVW() - Propagate->GetTec2b() * cvel;
309
310       InitializeReporting();
311       ComputeSteeringAngle();
312       ComputeGroundCoordSys();
313
314       vLocalWhlVel = Tb2g * vWhlVelVec;
315
316       compressSpeed = -vLocalWhlVel(eZ);
317       if (eContactType == ctBOGEY)
318         // Project the compression speed in the local coordinate frame of the strut
319         compressSpeed /= -vGroundNormal(eZ);
320
321       ComputeVerticalStrutForce();
322
323       // Compute the forces in the wheel ground plane.
324       if (eContactType == ctBOGEY) {
325         ComputeSlipAngle();
326         ComputeBrakeForceCoefficient();
327         ComputeSideForceCoefficient();
328         double sign = vLocalWhlVel(eX)>0?1.0:(vLocalWhlVel(eX)<0?-1.0:0.0);
329         vLocalForce(eX) = - ((1.0 - TirePressureNorm) * 30 + vLocalForce(eZ) * BrakeFCoeff) * sign;
330         vLocalForce(eY) = vLocalForce(eZ) * FCoeff;
331       }
332       else if (eContactType == ctSTRUCTURE) {
333         FGColumnVector3 vSlipVec = vLocalWhlVel;
334         vSlipVec(eZ) = 0.;
335         vSlipVec.Normalize();
336         vLocalForce -= staticFCoeff * vLocalForce(eZ) * vSlipVec;
337       }
338
339       // Lag and attenuate the XY-plane forces dependent on velocity. This code
340       // uses a lag filter, C/(s + C) where "C" is the filter coefficient. When
341       // "C" is chosen at the frame rate (in Hz), the jittering is significantly
342       // reduced. This is because the jitter is present *at* the execution rate.
343       // If a coefficient is set to something equal to or less than zero, the
344       // filter is bypassed.
345
346       if (LongForceLagFilterCoeff > 0) vLocalForce(eX) = LongForceFilter.execute(vLocalForce(eX));
347       if (LatForceLagFilterCoeff > 0)  vLocalForce(eY) = LatForceFilter.execute(vLocalForce(eY));
348
349       if ((fabs(vLocalWhlVel(eX)) <= RFRV) && RFRV > 0) vLocalForce(eX) *= fabs(vLocalWhlVel(eX))/RFRV;
350       if ((fabs(vLocalWhlVel(eY)) <= SFRV) && SFRV > 0) vLocalForce(eY) *= fabs(vLocalWhlVel(eY))/SFRV;
351
352       // End section for attenuating gear jitter
353
354       // Transform the forces back to the body frame and compute the moment.
355
356       vForce  = Tg2b * vLocalForce;
357       vMoment = vWhlContactVec * vForce;
358
359     } else { // Gear is NOT compressed
360
361       WOW = false;
362       compressLength = 0.0;
363       compressSpeed = 0.0;
364
365       // Let wheel spin down slowly
366       vLocalWhlVel(eX) -= 13.0*dT;
367       if (vLocalWhlVel(eX) < 0.0) vLocalWhlVel(eX) = 0.0;
368
369       // Return to neutral position between 1.0 and 0.8 gear pos.
370       SteerAngle *= max(GetGearUnitPos()-0.8, 0.0)/0.2;
371
372       ResetReporting();
373     }
374   }
375
376   ReportTakeoffOrLanding();
377
378   // Require both WOW and LastWOW to be true before checking crash conditions
379   // to allow the WOW flag to be used in terminating a scripted run.
380   if (WOW && lastWOW) CrashDetect();
381
382   lastWOW = WOW;
383
384   return vForce;
385 }
386
387 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 // Build a local "ground" coordinate system defined by
389 //  eX : projection of the rolling direction on the ground
390 //  eY : projection of the sliping direction on the ground
391 //  eZ : normal to the ground
392
393 void FGLGear::ComputeGroundCoordSys(void)
394 {
395   FGColumnVector3 vRollingGroundVec;
396
397   if (eContactType == ctBOGEY) {
398     // Compute the rolling direction projected on the ground
399     // It consists in finding a vector 'r' such that 'r' lies in the plane (w,z) and r.n = 0 (scalar
400     // product) where:
401     // 'n' is the normal to the ground,
402     // (x,y,z) are the directions defined in the body coord system
403     // and 'w' is 'x' rotated by the steering angle (SteerAngle) in the plane (x,y). 
404     // r = u * w + v * z and r.n = 0 => v/u = -w.n/z.n = a
405     // We also want u**2+v**2=1 and u > 0 (i.e. r orientated in the same 'direction' than w)
406     // after some arithmetic, one finds that :
407     double a = -(vGroundNormal(eX)*cos(SteerAngle)+vGroundNormal(eY)*sin(SteerAngle)) / vGroundNormal(eZ);
408     double u = 1. / sqrt(1. + a*a);
409     double v = a * u;
410     vRollingGroundVec = FGColumnVector3(u * cos(SteerAngle), u * sin(SteerAngle), v);
411   }
412   else {
413     // Here the only significant direction is the normal to the ground "vGroundNormal". Since there is
414     // no wheel the 2 other vectors of the orthonormal basis are not meaningful and are only used to
415     // create the transformation matrix Tg2b. So we are building vRollingGroundVec as an arbitrary
416     // vector normal to vGroundNormal
417     if (fabs(vGroundNormal(eX)) > 0.)
418       vRollingGroundVec = FGColumnVector3(-vGroundNormal(eZ)/vGroundNormal(eX), 0., 1.);
419     else if (fabs(vGroundNormal(eY)) > 0.)
420       vRollingGroundVec = FGColumnVector3(0., -vGroundNormal(eZ)/vGroundNormal(eY), 1.);
421     else
422       vRollingGroundVec = FGColumnVector3(1., 0., -vGroundNormal(eX)/vGroundNormal(eZ));
423
424     vRollingGroundVec.Normalize();
425   }
426
427   // The sliping direction is the cross product multiplication of the ground normal and rolling
428   // directions
429   FGColumnVector3 vSlipGroundVec = vGroundNormal * vRollingGroundVec;
430
431   Tg2b(1,1) = vRollingGroundVec(eX);
432   Tg2b(2,1) = vRollingGroundVec(eY);
433   Tg2b(3,1) = vRollingGroundVec(eZ);
434   Tg2b(1,2) = vSlipGroundVec(eX);
435   Tg2b(2,2) = vSlipGroundVec(eY);
436   Tg2b(3,2) = vSlipGroundVec(eZ);
437   Tg2b(1,3) = vGroundNormal(eX);
438   Tg2b(2,3) = vGroundNormal(eY);
439   Tg2b(3,3) = vGroundNormal(eZ);
440
441   Tb2g = Tg2b.Transposed();
442 }
443
444 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445
446 void FGLGear::ComputeRetractionState(void)
447 {
448   double gearPos = GetGearUnitPos();
449   if (gearPos < 0.01) {
450     GearUp   = true;
451     WOW      = false;
452     GearDown = false;
453     vLocalWhlVel.InitMatrix();
454   } else if (gearPos > 0.99) {
455     GearDown = true;
456     GearUp   = false;
457   } else {
458     GearUp   = false;
459     GearDown = false;
460   }
461 }
462
463 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464
465 void FGLGear::ComputeSlipAngle(void)
466 {
467   // Calculate tire slip angle.
468   WheelSlip = -atan2(vLocalWhlVel(eY), fabs(vLocalWhlVel(eX)))*radtodeg;
469
470   // Filter the wheel slip angle
471   if (WheelSlipLagFilterCoeff > 0) WheelSlip = WheelSlipFilter.execute(WheelSlip);
472 }
473
474 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475 // Compute the steering angle in any case.
476 // This will also make sure that animations will look right.
477
478 void FGLGear::ComputeSteeringAngle(void)
479 {
480   switch (eSteerType) {
481   case stSteer:
482     SteerAngle = degtorad * FCS->GetSteerPosDeg(GearNumber);
483     break;
484   case stFixed:
485     SteerAngle = 0.0;
486     break;
487   case stCaster:
488     SteerAngle = atan2(fabs(vWhlVelVec(eX)), vWhlVelVec(eY));
489     break;
490   default:
491     cerr << "Improper steering type membership detected for this gear." << endl;
492     break;
493   }
494 }
495
496 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
497 // Reset reporting functionality after takeoff
498
499 void FGLGear::ResetReporting(void)
500 {
501   if (Propagate->GetDistanceAGL() > 200.0) {
502     FirstContact = false;
503     StartedGroundRun = false;
504     LandingReported = false;
505     TakeoffReported = true;
506     LandingDistanceTraveled = 0.0;
507     MaximumStrutForce = MaximumStrutTravel = 0.0;
508   }
509 }
510
511 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512
513 void FGLGear::InitializeReporting(void)
514 {
515   // If this is the first time the wheel has made contact, remember some values
516   // for later printout.
517
518   if (!FirstContact) {
519     FirstContact  = true;
520     SinkRate      =  compressSpeed;
521     GroundSpeed   =  Propagate->GetVel().Magnitude();
522     TakeoffReported = false;
523   }
524
525   // If the takeoff run is starting, initialize.
526
527   if ((Propagate->GetVel().Magnitude() > 0.1) &&
528       (FCS->GetBrake(bgLeft) == 0) &&
529       (FCS->GetBrake(bgRight) == 0) &&
530       (FCS->GetThrottlePos(0) > 0.90) && !StartedGroundRun)
531   {
532     TakeoffDistanceTraveled = 0;
533     TakeoffDistanceTraveled50ft = 0;
534     StartedGroundRun = true;
535   }
536 }
537
538 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
539 // Takeoff and landing reporting functionality
540
541 void FGLGear::ReportTakeoffOrLanding(void)
542 {
543   double deltaT = State->Getdt()*Exec->GetGroundReactions()->GetRate();
544
545   if (FirstContact)
546     LandingDistanceTraveled += Auxiliary->GetVground()*deltaT;
547
548   if (StartedGroundRun) {
549     TakeoffDistanceTraveled50ft += Auxiliary->GetVground()*deltaT;
550     if (WOW) TakeoffDistanceTraveled += Auxiliary->GetVground()*deltaT;
551   }
552
553   if ( ReportEnable
554        && Auxiliary->GetVground() <= 0.05
555        && !LandingReported
556        && Exec->GetGroundReactions()->GetWOW())
557   {
558     if (debug_lvl > 0) Report(erLand);
559   }
560
561   if ( ReportEnable
562        && !TakeoffReported
563        && (Propagate->GetDistanceAGL() - vLocalGear(eZ)) > 50.0
564        && !Exec->GetGroundReactions()->GetWOW())
565   {
566     if (debug_lvl > 0) Report(erTakeoff);
567   }
568
569   if (lastWOW != WOW) PutMessage("GEAR_CONTACT: " + name, WOW);
570 }
571
572 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573 // Crash detection logic (really out-of-bounds detection)
574
575 void FGLGear::CrashDetect(void)
576 {
577   if ( (compressLength > 500.0 ||
578       vForce.Magnitude() > 100000000.0 ||
579       vMoment.Magnitude() > 5000000000.0 ||
580       SinkRate > 1.4666*30 ) && !State->IntegrationSuspended())
581   {
582     PutMessage("Crash Detected: Simulation FREEZE.");
583     State->SuspendIntegration();
584   }
585 }
586
587 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588 // The following needs work regarding friction coefficients and braking and
589 // steering The BrakeFCoeff formula assumes that an anti-skid system is used.
590 // It also assumes that we won't be turning and braking at the same time.
591 // Will fix this later.
592 // [JSB] The braking force coefficients include normal rolling coefficient +
593 // a percentage of the static friction coefficient based on braking applied.
594
595 void FGLGear::ComputeBrakeForceCoefficient(void)
596 {
597   switch (eBrakeGrp) {
598   case bgLeft:
599     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgLeft)) +
600                      staticFCoeff*FCS->GetBrake(bgLeft) );
601     break;
602   case bgRight:
603     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgRight)) +
604                      staticFCoeff*FCS->GetBrake(bgRight) );
605     break;
606   case bgCenter:
607     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgCenter)) +
608                      staticFCoeff*FCS->GetBrake(bgCenter) );
609     break;
610   case bgNose:
611     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgCenter)) +
612                      staticFCoeff*FCS->GetBrake(bgCenter) );
613     break;
614   case bgTail:
615     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgCenter)) +
616                      staticFCoeff*FCS->GetBrake(bgCenter) );
617     break;
618   case bgNone:
619     BrakeFCoeff =  rollingFCoeff;
620     break;
621   default:
622     cerr << "Improper brake group membership detected for this gear." << endl;
623     break;
624   }
625 }
626
627 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 // Compute the sideforce coefficients using Pacejka's Magic Formula.
629 //
630 //   y(x) = D sin {C arctan [Bx - E(Bx - arctan Bx)]}
631 //
632 // Where: B = Stiffness Factor (0.06, here)
633 //        C = Shape Factor (2.8, here)
634 //        D = Peak Factor (0.8, here)
635 //        E = Curvature Factor (1.03, here)
636
637 void FGLGear::ComputeSideForceCoefficient(void)
638 {
639   if (ForceY_Table) {
640     FCoeff = ForceY_Table->GetValue(WheelSlip);
641   } else {
642     double StiffSlip = Stiffness*WheelSlip;
643     FCoeff = Peak * sin(Shape*atan(StiffSlip - Curvature*(StiffSlip - atan(StiffSlip))));
644   }
645 }
646
647 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648 // Compute the vertical force on the wheel using square-law damping (per comment
649 // in paper AIAA-2000-4303 - see header prologue comments). We might consider
650 // allowing for both square and linear damping force calculation. Also need to
651 // possibly give a "rebound damping factor" that differs from the compression
652 // case.
653
654 void FGLGear::ComputeVerticalStrutForce(void)
655 {
656   double springForce = 0;
657   double dampForce = 0;
658
659   springForce = -compressLength * kSpring;
660
661   if (compressSpeed >= 0.0) {
662
663     if (eDampType == dtLinear)   dampForce = -compressSpeed * bDamp;
664     else         dampForce = -compressSpeed * compressSpeed * bDamp;
665
666   } else {
667
668     if (eDampTypeRebound == dtLinear)
669       dampForce   = -compressSpeed * bDampRebound;
670     else
671       dampForce   =  compressSpeed * compressSpeed * bDampRebound;
672
673   }
674
675   StrutForce = min(springForce + dampForce, (double)0.0);
676
677   // The reaction force of the wheel is always normal to the ground
678   switch (eContactType) {
679   case ctBOGEY:
680     // Project back the strut force in the local coordinate frame of the ground
681     vLocalForce(eZ) =  StrutForce / vGroundNormal(eZ);
682     break;
683   case ctSTRUCTURE:
684     vLocalForce(eZ) = -StrutForce;
685     break;
686   }
687
688   // Remember these values for reporting
689   MaximumStrutForce = max(MaximumStrutForce, fabs(StrutForce));
690   MaximumStrutTravel = max(MaximumStrutTravel, fabs(compressLength));
691 }
692
693 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
694
695 double FGLGear::GetGearUnitPos(void)
696 {
697   // hack to provide backward compatibility to gear/gear-pos-norm property
698   if( useFCSGearPos || FCS->GetGearPos() != 1.0 ) {
699     useFCSGearPos = true;
700     return FCS->GetGearPos();
701   }
702   return GearPos;
703 }
704
705 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706
707 void FGLGear::bind(void)
708 {
709   string property_name;
710   string base_property_name;
711   base_property_name = CreateIndexedPropertyName("gear/unit", GearNumber);
712   if (eContactType == ctBOGEY) {
713     property_name = base_property_name + "/slip-angle-deg";
714     Exec->GetPropertyManager()->Tie( property_name.c_str(), &WheelSlip );
715     property_name = base_property_name + "/WOW";
716     Exec->GetPropertyManager()->Tie( property_name.c_str(), &WOW );
717     property_name = base_property_name + "/wheel-speed-fps";
718     Exec->GetPropertyManager()->Tie( property_name.c_str(), (FGLGear*)this,
719                           &FGLGear::GetWheelRollVel);
720     property_name = base_property_name + "/z-position";
721     Exec->GetPropertyManager()->Tie( property_name.c_str(), (FGLGear*)this,
722                           &FGLGear::GetZPosition, &FGLGear::SetZPosition);
723     property_name = base_property_name + "/compression-ft";
724     Exec->GetPropertyManager()->Tie( property_name.c_str(), &compressLength );
725     property_name = base_property_name + "/side_friction_coeff";
726     Exec->GetPropertyManager()->Tie( property_name.c_str(), &FCoeff );
727
728     property_name = base_property_name + "/static_friction_coeff";
729     Exec->GetPropertyManager()->Tie( property_name.c_str(), &staticFCoeff );
730
731   }
732
733   if( isRetractable ) {
734     property_name = base_property_name + "/pos-norm";
735     Exec->GetPropertyManager()->Tie( property_name.c_str(), &GearPos );
736   }
737
738 }
739
740 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
741
742 void FGLGear::Report(ReportType repType)
743 {
744   if (fabs(TakeoffDistanceTraveled) < 0.001) return; // Don't print superfluous reports
745
746   switch(repType) {
747   case erLand:
748     cout << endl << "Touchdown report for " << name << " (WOW at time: "
749          << Exec->GetState()->Getsim_time() << " seconds)" << endl;
750     cout << "  Sink rate at contact:  " << SinkRate                << " fps,    "
751                                 << SinkRate*0.3048          << " mps"     << endl;
752     cout << "  Contact ground speed:  " << GroundSpeed*.5925       << " knots,  "
753                                 << GroundSpeed*0.3048       << " mps"     << endl;
754     cout << "  Maximum contact force: " << MaximumStrutForce       << " lbs,    "
755                                 << MaximumStrutForce*4.448  << " Newtons" << endl;
756     cout << "  Maximum strut travel:  " << MaximumStrutTravel*12.0 << " inches, "
757                                 << MaximumStrutTravel*30.48 << " cm"      << endl;
758     cout << "  Distance traveled:     " << LandingDistanceTraveled        << " ft,     "
759                                 << LandingDistanceTraveled*0.3048  << " meters"  << endl;
760     LandingReported = true;
761     break;
762   case erTakeoff:
763     cout << endl << "Takeoff report for " << name << " (Liftoff at time: "
764          << Exec->GetState()->Getsim_time() << " seconds)" << endl;
765     cout << "  Distance traveled:                " << TakeoffDistanceTraveled
766          << " ft,     " << TakeoffDistanceTraveled*0.3048  << " meters"  << endl;
767     cout << "  Distance traveled (over 50'):     " << TakeoffDistanceTraveled50ft
768          << " ft,     " << TakeoffDistanceTraveled50ft*0.3048 << " meters" << endl;
769     cout << "  [Altitude (ASL): " << Exec->GetPropagate()->GetAltitudeASL() << " ft. / "
770          << Exec->GetPropagate()->GetAltitudeASLmeters() << " m  | Temperature: "
771          << Exec->GetAtmosphere()->GetTemperature() - 459.67 << " F / "
772          << RankineToCelsius(Exec->GetAtmosphere()->GetTemperature()) << " C]" << endl;
773     cout << "  [Velocity (KCAS): " << Exec->GetAuxiliary()->GetVcalibratedKTS() << "]" << endl;
774     TakeoffReported = true;
775     break;
776   }
777 }
778
779 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 //    The bitmasked value choices are as follows:
781 //    unset: In this case (the default) JSBSim would only print
782 //       out the normally expected messages, essentially echoing
783 //       the config files as they are read. If the environment
784 //       variable is not set, debug_lvl is set to 1 internally
785 //    0: This requests JSBSim not to output any messages
786 //       whatsoever.
787 //    1: This value explicity requests the normal JSBSim
788 //       startup messages
789 //    2: This value asks for a message to be printed out when
790 //       a class is instantiated
791 //    4: When this value is set, a message is displayed when a
792 //       FGModel object executes its Run() method
793 //    8: When this value is set, various runtime state variables
794 //       are printed out periodically
795 //    16: When set various parameters are sanity checked and
796 //       a message is printed out when they go out of bounds
797
798 void FGLGear::Debug(int from)
799 {
800   if (debug_lvl <= 0) return;
801
802   if (debug_lvl & 1) { // Standard console startup message output
803     if (from == 0) { // Constructor - loading and initialization
804       cout << "    " << sContactType << " " << name          << endl;
805       cout << "      Location: "         << vXYZ          << endl;
806       cout << "      Spring Constant:  " << kSpring       << endl;
807
808       if (eDampType == dtLinear)
809         cout << "      Damping Constant: " << bDamp << " (linear)" << endl;
810       else
811         cout << "      Damping Constant: " << bDamp << " (square law)" << endl;
812
813       if (eDampTypeRebound == dtLinear)
814         cout << "      Rebound Damping Constant: " << bDampRebound << " (linear)" << endl;
815       else 
816         cout << "      Rebound Damping Constant: " << bDampRebound << " (square law)" << endl;
817
818       cout << "      Dynamic Friction: " << dynamicFCoeff << endl;
819       cout << "      Static Friction:  " << staticFCoeff  << endl;
820       if (eContactType == ctBOGEY) {
821         cout << "      Rolling Friction: " << rollingFCoeff << endl;
822         cout << "      Steering Type:    " << sSteerType    << endl;
823         cout << "      Grouping:         " << sBrakeGroup   << endl;
824         cout << "      Max Steer Angle:  " << maxSteerAngle << endl;
825         cout << "      Retractable:      " << isRetractable  << endl;
826         cout << "      Relaxation Velocities:" << endl;
827         cout << "        Rolling:          " << RFRV << endl;
828         cout << "        Side:             " << SFRV << endl;
829       }
830     }
831   }
832   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
833     if (from == 0) cout << "Instantiated: FGLGear" << endl;
834     if (from == 1) cout << "Destroyed:    FGLGear" << endl;
835   }
836   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
837   }
838   if (debug_lvl & 8 ) { // Runtime state variables
839   }
840   if (debug_lvl & 16) { // Sanity checking
841   }
842   if (debug_lvl & 64) {
843     if (from == 0) { // Constructor
844       cout << IdSrc << endl;
845       cout << IdHdr << endl;
846     }
847   }
848 }
849
850 } // namespace JSBSim
851