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