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