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