]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGLGear.cpp
e8df4370ad99c828950097f6721c05ae415d02cb
[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 (jsb@hal-pc.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 theoritical location of the wheel (if struct was 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   if( fabs(vGroundNormal(eZ)) < 1e-3 ) {
396     cout << "BOMB: ";
397     cout << vGroundNormal(eX) << "/" << vGroundNormal(eY) << "/" << 
398          vGroundNormal(eZ) << " - " << SteerAngle << endl;
399     return;
400   }
401
402   // Compute the rolling direction projected on the ground
403   // It consists in finding a vector 'r' such that 'r' lies in the plane (w,z) and r.n = 0 (scalar
404   // product) where:
405   // 'n' is the normal to the ground,
406   // (x,y,z) are the directions defined in the body coord system
407   // and 'w' is 'x' rotated by the steering angle (SteerAngle) in the plane (x,y). 
408   // r = u * w + v * z and r.n = 0 => v/u = -w.n/z.n = a
409   // We also want u**2+v**2=1 and u > 0 (i.e. r orientated in the same 'direction' than w)
410   // after some arithmetic, one finds that :
411   double a = -(vGroundNormal(eX)*cos(SteerAngle)+vGroundNormal(eY)*sin(SteerAngle)) / vGroundNormal(eZ);
412   double u = 1. / sqrt(1. + a*a);
413   double v = a * u;
414   FGColumnVector3 vRollingGroundVec = FGColumnVector3(u * cos(SteerAngle), u * sin(SteerAngle), v);
415
416   // The sliping direction is the cross product multiplication of the ground normal and rolling
417   // directions
418   FGColumnVector3 vSlipGroundVec = vGroundNormal * vRollingGroundVec;
419
420   Tg2b(1,1) = vRollingGroundVec(eX);
421   Tg2b(2,1) = vRollingGroundVec(eY);
422   Tg2b(3,1) = vRollingGroundVec(eZ);
423   Tg2b(1,2) = vSlipGroundVec(eX);
424   Tg2b(2,2) = vSlipGroundVec(eY);
425   Tg2b(3,2) = vSlipGroundVec(eZ);
426   Tg2b(1,3) = vGroundNormal(eX);
427   Tg2b(2,3) = vGroundNormal(eY);
428   Tg2b(3,3) = vGroundNormal(eZ);
429
430   Tb2g = Tg2b.Transposed();
431 }
432
433 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434
435 void FGLGear::ComputeRetractionState(void)
436 {
437   double gearPos = GetGearUnitPos();
438   if (gearPos < 0.01) {
439     GearUp   = true;
440     WOW      = false;
441     GearDown = false;
442     vLocalWhlVel.InitMatrix();
443   } else if (gearPos > 0.99) {
444     GearDown = true;
445     GearUp   = false;
446   } else {
447     GearUp   = false;
448     GearDown = false;
449   }
450 }
451
452 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453
454 void FGLGear::ComputeSlipAngle(void)
455 {
456   // Calculate tire slip angle.
457   WheelSlip = -atan2(vLocalWhlVel(eY), fabs(vLocalWhlVel(eX)))*radtodeg;
458
459   // Filter the wheel slip angle
460   if (WheelSlipLagFilterCoeff > 0) WheelSlip = WheelSlipFilter.execute(WheelSlip);
461 }
462
463 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464 // Compute the steering angle in any case.
465 // This will also make sure that animations will look right.
466
467 void FGLGear::ComputeSteeringAngle(void)
468 {
469   switch (eSteerType) {
470   case stSteer:
471     SteerAngle = degtorad * FCS->GetSteerPosDeg(GearNumber);
472     break;
473   case stFixed:
474     SteerAngle = 0.0;
475     break;
476   case stCaster:
477     SteerAngle = atan2(fabs(vWhlVelVec(eX)), vWhlVelVec(eY));
478     break;
479   default:
480     cerr << "Improper steering type membership detected for this gear." << endl;
481     break;
482   }
483 }
484
485 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 // Reset reporting functionality after takeoff
487
488 void FGLGear::ResetReporting(void)
489 {
490   if (Propagate->GetDistanceAGL() > 200.0) {
491     FirstContact = false;
492     StartedGroundRun = false;
493     LandingReported = false;
494     TakeoffReported = true;
495     LandingDistanceTraveled = 0.0;
496     MaximumStrutForce = MaximumStrutTravel = 0.0;
497   }
498 }
499
500 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501
502 void FGLGear::InitializeReporting(void)
503 {
504   // If this is the first time the wheel has made contact, remember some values
505   // for later printout.
506
507   if (!FirstContact) {
508     FirstContact  = true;
509     SinkRate      =  compressSpeed;
510     GroundSpeed   =  Propagate->GetVel().Magnitude();
511     TakeoffReported = false;
512   }
513
514   // If the takeoff run is starting, initialize.
515
516   if ((Propagate->GetVel().Magnitude() > 0.1) &&
517       (FCS->GetBrake(bgLeft) == 0) &&
518       (FCS->GetBrake(bgRight) == 0) &&
519       (FCS->GetThrottlePos(0) > 0.90) && !StartedGroundRun)
520   {
521     TakeoffDistanceTraveled = 0;
522     TakeoffDistanceTraveled50ft = 0;
523     StartedGroundRun = true;
524   }
525 }
526
527 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528 // Takeoff and landing reporting functionality
529
530 void FGLGear::ReportTakeoffOrLanding(void)
531 {
532   double deltaT = State->Getdt()*Exec->GetGroundReactions()->GetRate();
533
534   if (FirstContact)
535     LandingDistanceTraveled += Auxiliary->GetVground()*deltaT;
536
537   if (StartedGroundRun) {
538     TakeoffDistanceTraveled50ft += Auxiliary->GetVground()*deltaT;
539     if (WOW) TakeoffDistanceTraveled += Auxiliary->GetVground()*deltaT;
540   }
541
542   if ( ReportEnable
543        && Auxiliary->GetVground() <= 0.05
544        && !LandingReported
545        && Exec->GetGroundReactions()->GetWOW())
546   {
547     if (debug_lvl > 0) Report(erLand);
548   }
549
550   if ( ReportEnable
551        && !TakeoffReported
552        && (Propagate->GetDistanceAGL() - vLocalGear(eZ)) > 50.0
553        && !Exec->GetGroundReactions()->GetWOW())
554   {
555     if (debug_lvl > 0) Report(erTakeoff);
556   }
557
558   if (lastWOW != WOW) PutMessage("GEAR_CONTACT: " + name, WOW);
559 }
560
561 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 // Crash detection logic (really out-of-bounds detection)
563
564 void FGLGear::CrashDetect(void)
565 {
566   if ( (compressLength > 500.0 ||
567       vForce.Magnitude() > 100000000.0 ||
568       vMoment.Magnitude() > 5000000000.0 ||
569       SinkRate > 1.4666*30 ) && !State->IntegrationSuspended())
570   {
571     PutMessage("Crash Detected: Simulation FREEZE.");
572     State->SuspendIntegration();
573   }
574 }
575
576 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 // The following needs work regarding friction coefficients and braking and
578 // steering The BrakeFCoeff formula assumes that an anti-skid system is used.
579 // It also assumes that we won't be turning and braking at the same time.
580 // Will fix this later.
581 // [JSB] The braking force coefficients include normal rolling coefficient +
582 // a percentage of the static friction coefficient based on braking applied.
583
584 void FGLGear::ComputeBrakeForceCoefficient(void)
585 {
586   switch (eBrakeGrp) {
587   case bgLeft:
588     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgLeft)) +
589                      staticFCoeff*FCS->GetBrake(bgLeft) );
590     break;
591   case bgRight:
592     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgRight)) +
593                      staticFCoeff*FCS->GetBrake(bgRight) );
594     break;
595   case bgCenter:
596     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgCenter)) +
597                      staticFCoeff*FCS->GetBrake(bgCenter) );
598     break;
599   case bgNose:
600     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgCenter)) +
601                      staticFCoeff*FCS->GetBrake(bgCenter) );
602     break;
603   case bgTail:
604     BrakeFCoeff =  ( rollingFCoeff*(1.0 - FCS->GetBrake(bgCenter)) +
605                      staticFCoeff*FCS->GetBrake(bgCenter) );
606     break;
607   case bgNone:
608     BrakeFCoeff =  rollingFCoeff;
609     break;
610   default:
611     cerr << "Improper brake group membership detected for this gear." << endl;
612     break;
613   }
614 }
615
616 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617 // Compute the sideforce coefficients using Pacejka's Magic Formula.
618 //
619 //   y(x) = D sin {C arctan [Bx - E(Bx - arctan Bx)]}
620 //
621 // Where: B = Stiffness Factor (0.06, here)
622 //        C = Shape Factor (2.8, here)
623 //        D = Peak Factor (0.8, here)
624 //        E = Curvature Factor (1.03, here)
625
626 void FGLGear::ComputeSideForceCoefficient(void)
627 {
628   if (ForceY_Table) {
629     FCoeff = ForceY_Table->GetValue(WheelSlip);
630   } else {
631     double StiffSlip = Stiffness*WheelSlip;
632     FCoeff = Peak * sin(Shape*atan(StiffSlip - Curvature*(StiffSlip - atan(StiffSlip))));
633   }
634 }
635
636 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 // Compute the vertical force on the wheel using square-law damping (per comment
638 // in paper AIAA-2000-4303 - see header prologue comments). We might consider
639 // allowing for both square and linear damping force calculation. Also need to
640 // possibly give a "rebound damping factor" that differs from the compression
641 // case.
642
643 void FGLGear::ComputeVerticalStrutForce(void)
644 {
645   double springForce = 0;
646   double dampForce = 0;
647
648   springForce = -compressLength * kSpring;
649
650   if (compressSpeed >= 0.0) {
651
652     if (eDampType == dtLinear)   dampForce = -compressSpeed * bDamp;
653     else         dampForce = -compressSpeed * compressSpeed * bDamp;
654
655   } else {
656
657     if (eDampTypeRebound == dtLinear)
658       dampForce   = -compressSpeed * bDampRebound;
659     else
660       dampForce   =  compressSpeed * compressSpeed * bDampRebound;
661
662   }
663
664   StrutForce = min(springForce + dampForce, (double)0.0);
665
666   // The reaction force of the wheel is always normal to the ground
667   switch (eContactType) {
668   case ctBOGEY:
669     // Project back the strut force in the local coordinate frame of the ground
670     vLocalForce(eZ) =  StrutForce / vGroundNormal(eZ);
671     break;
672   case ctSTRUCTURE:
673     vLocalForce(eZ) = -StrutForce;
674     break;
675   }
676
677   // Remember these values for reporting
678   MaximumStrutForce = max(MaximumStrutForce, fabs(StrutForce));
679   MaximumStrutTravel = max(MaximumStrutTravel, fabs(compressLength));
680 }
681
682 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683
684 double FGLGear::GetGearUnitPos(void)
685 {
686   // hack to provide backward compatibility to gear/gear-pos-norm property
687   if( useFCSGearPos || FCS->GetGearPos() != 1.0 ) {
688     useFCSGearPos = true;
689     return FCS->GetGearPos();
690   }
691   return GearPos;
692 }
693
694 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695
696 void FGLGear::bind(void)
697 {
698   string property_name;
699   string base_property_name;
700   base_property_name = CreateIndexedPropertyName("gear/unit", GearNumber);
701   if (eContactType == ctBOGEY) {
702     property_name = base_property_name + "/slip-angle-deg";
703     Exec->GetPropertyManager()->Tie( property_name.c_str(), &WheelSlip );
704     property_name = base_property_name + "/WOW";
705     Exec->GetPropertyManager()->Tie( property_name.c_str(), &WOW );
706     property_name = base_property_name + "/wheel-speed-fps";
707     Exec->GetPropertyManager()->Tie( property_name.c_str(), (FGLGear*)this,
708                           &FGLGear::GetWheelRollVel);
709     property_name = base_property_name + "/z-position";
710     Exec->GetPropertyManager()->Tie( property_name.c_str(), (FGLGear*)this,
711                           &FGLGear::GetZPosition, &FGLGear::SetZPosition);
712     property_name = base_property_name + "/compression-ft";
713     Exec->GetPropertyManager()->Tie( property_name.c_str(), &compressLength );
714     property_name = base_property_name + "/side_friction_coeff";
715     Exec->GetPropertyManager()->Tie( property_name.c_str(), &FCoeff );
716
717     property_name = base_property_name + "/static_friction_coeff";
718     Exec->GetPropertyManager()->Tie( property_name.c_str(), &staticFCoeff );
719
720   }
721
722   if( isRetractable ) {
723     property_name = base_property_name + "/pos-norm";
724     Exec->GetPropertyManager()->Tie( property_name.c_str(), &GearPos );
725   }
726
727 }
728
729 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730
731 void FGLGear::Report(ReportType repType)
732 {
733   if (fabs(TakeoffDistanceTraveled) < 0.001) return; // Don't print superfluous reports
734
735   switch(repType) {
736   case erLand:
737     cout << endl << "Touchdown report for " << name << " (WOW at time: "
738          << Exec->GetState()->Getsim_time() << " seconds)" << endl;
739     cout << "  Sink rate at contact:  " << SinkRate                << " fps,    "
740                                 << SinkRate*0.3048          << " mps"     << endl;
741     cout << "  Contact ground speed:  " << GroundSpeed*.5925       << " knots,  "
742                                 << GroundSpeed*0.3048       << " mps"     << endl;
743     cout << "  Maximum contact force: " << MaximumStrutForce       << " lbs,    "
744                                 << MaximumStrutForce*4.448  << " Newtons" << endl;
745     cout << "  Maximum strut travel:  " << MaximumStrutTravel*12.0 << " inches, "
746                                 << MaximumStrutTravel*30.48 << " cm"      << endl;
747     cout << "  Distance traveled:     " << LandingDistanceTraveled        << " ft,     "
748                                 << LandingDistanceTraveled*0.3048  << " meters"  << endl;
749     LandingReported = true;
750     break;
751   case erTakeoff:
752     cout << endl << "Takeoff report for " << name << " (Liftoff at time: "
753          << Exec->GetState()->Getsim_time() << " seconds)" << endl;
754     cout << "  Distance traveled:                " << TakeoffDistanceTraveled
755          << " ft,     " << TakeoffDistanceTraveled*0.3048  << " meters"  << endl;
756     cout << "  Distance traveled (over 50'):     " << TakeoffDistanceTraveled50ft
757          << " ft,     " << TakeoffDistanceTraveled50ft*0.3048 << " meters" << endl;
758     cout << "  [Altitude (ASL): " << Exec->GetPropagate()->GetAltitudeASL() << " ft. / "
759          << Exec->GetPropagate()->GetAltitudeASLmeters() << " m  | Temperature: "
760          << Exec->GetAtmosphere()->GetTemperature() - 459.67 << " F / "
761          << RankineToCelsius(Exec->GetAtmosphere()->GetTemperature()) << " C]" << endl;
762     cout << "  [Velocity (KCAS): " << Exec->GetAuxiliary()->GetVcalibratedKTS() << "]" << endl;
763     TakeoffReported = true;
764     break;
765   }
766 }
767
768 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769 //    The bitmasked value choices are as follows:
770 //    unset: In this case (the default) JSBSim would only print
771 //       out the normally expected messages, essentially echoing
772 //       the config files as they are read. If the environment
773 //       variable is not set, debug_lvl is set to 1 internally
774 //    0: This requests JSBSim not to output any messages
775 //       whatsoever.
776 //    1: This value explicity requests the normal JSBSim
777 //       startup messages
778 //    2: This value asks for a message to be printed out when
779 //       a class is instantiated
780 //    4: When this value is set, a message is displayed when a
781 //       FGModel object executes its Run() method
782 //    8: When this value is set, various runtime state variables
783 //       are printed out periodically
784 //    16: When set various parameters are sanity checked and
785 //       a message is printed out when they go out of bounds
786
787 void FGLGear::Debug(int from)
788 {
789   if (debug_lvl <= 0) return;
790
791   if (debug_lvl & 1) { // Standard console startup message output
792     if (from == 0) { // Constructor - loading and initialization
793       cout << "    " << sContactType << " " << name          << endl;
794       cout << "      Location: "         << vXYZ          << endl;
795       cout << "      Spring Constant:  " << kSpring       << endl;
796
797       if (eDampType == dtLinear)
798         cout << "      Damping Constant: " << bDamp << " (linear)" << endl;
799       else
800         cout << "      Damping Constant: " << bDamp << " (square law)" << endl;
801
802       if (eDampTypeRebound == dtLinear)
803         cout << "      Rebound Damping Constant: " << bDampRebound << " (linear)" << endl;
804       else 
805         cout << "      Rebound Damping Constant: " << bDampRebound << " (square law)" << endl;
806
807       cout << "      Dynamic Friction: " << dynamicFCoeff << endl;
808       cout << "      Static Friction:  " << staticFCoeff  << endl;
809       if (eContactType == ctBOGEY) {
810         cout << "      Rolling Friction: " << rollingFCoeff << endl;
811         cout << "      Steering Type:    " << sSteerType    << endl;
812         cout << "      Grouping:         " << sBrakeGroup   << endl;
813         cout << "      Max Steer Angle:  " << maxSteerAngle << endl;
814         cout << "      Retractable:      " << isRetractable  << endl;
815         cout << "      Relaxation Velocities:" << endl;
816         cout << "        Rolling:          " << RFRV << endl;
817         cout << "        Side:             " << SFRV << endl;
818       }
819     }
820   }
821   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
822     if (from == 0) cout << "Instantiated: FGLGear" << endl;
823     if (from == 1) cout << "Destroyed:    FGLGear" << endl;
824   }
825   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
826   }
827   if (debug_lvl & 8 ) { // Runtime state variables
828   }
829   if (debug_lvl & 16) { // Sanity checking
830   }
831   if (debug_lvl & 64) {
832     if (from == 0) { // Constructor
833       cout << IdSrc << endl;
834       cout << IdHdr << endl;
835     }
836   }
837 }
838
839 } // namespace JSBSim
840