]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/initialization/FGTrimAxis.cpp
std namespace fix
[flightgear.git] / src / FDM / JSBSim / initialization / FGTrimAxis.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Header:       FGTrimAxis.cpp
4  Author:       Tony Peden
5  Date started: 7/3/00
6
7  --------- Copyright (C) 1999  Anthony K. Peden (apeden@earthlink.net) ---------
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17  details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26
27  HISTORY
28 --------------------------------------------------------------------------------
29 7/3/00   TP   Created
30
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 INCLUDES
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
34
35 #ifdef _MSC_VER
36 #  pragma warning (disable : 4786)
37 #endif
38
39 #include <string>
40 #include <cstdlib>
41 #include <iomanip>
42 #include "FGFDMExec.h"
43 #include "models/FGAtmosphere.h"
44 #include "FGInitialCondition.h"
45 #include "FGTrimAxis.h"
46 #include "models/FGPropulsion.h"
47 #include "models/FGAerodynamics.h"
48 #include "models/FGFCS.h"
49 #include "models/propulsion/FGEngine.h"
50 #include "models/FGAuxiliary.h"
51 #include "models/FGGroundReactions.h"
52 #include "models/FGPropagate.h"
53 #include "models/FGAccelerations.h"
54
55 using namespace std;
56
57 namespace JSBSim {
58
59 static const char *IdSrc = "$Id: FGTrimAxis.cpp,v 1.13 2011/07/28 12:48:19 jberndt Exp $";
60 static const char *IdHdr = ID_TRIMAXIS;
61
62 /*****************************************************************************/
63
64 FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, State st,
65                        Control ctrl) {
66
67   fdmex=fdex;
68   fgic=ic;
69   state=st;
70   control=ctrl;
71   max_iterations=10;
72   control_value=0;
73   its_to_stable_value=0;
74   total_iterations=0;
75   total_stability_iterations=0;
76   state_convert=1.0;
77   control_convert=1.0;
78   state_value=0;
79   state_target=0;
80   switch(state) {
81     case tUdot: tolerance = DEFAULT_TOLERANCE; break;
82     case tVdot: tolerance = DEFAULT_TOLERANCE; break;
83     case tWdot: tolerance = DEFAULT_TOLERANCE; break;
84     case tQdot: tolerance = DEFAULT_TOLERANCE / 10; break;
85     case tPdot: tolerance = DEFAULT_TOLERANCE / 10; break;
86     case tRdot: tolerance = DEFAULT_TOLERANCE / 10; break;
87     case tHmgt: tolerance = 0.01; break;
88     case  tNlf: state_target=1.0; tolerance = 1E-5; break;
89     case tAll: break;
90   }
91
92   solver_eps=tolerance;
93   switch(control) {
94   case tThrottle:
95     control_min=0;
96     control_max=1;
97     control_value=0.5;
98     break;
99   case tBeta:
100     control_min=-30*degtorad;
101     control_max=30*degtorad;
102     control_convert=radtodeg;
103     break;
104   case tAlpha:
105     control_min=fdmex->GetAerodynamics()->GetAlphaCLMin();
106     control_max=fdmex->GetAerodynamics()->GetAlphaCLMax();
107     if(control_max <= control_min) {
108       control_max=20*degtorad;
109       control_min=-5*degtorad;
110     }
111     control_value= (control_min+control_max)/2;
112     control_convert=radtodeg;
113     solver_eps=tolerance/100;
114     break;
115   case tPitchTrim:
116   case tElevator:
117   case tRollTrim:
118   case tAileron:
119   case tYawTrim:
120   case tRudder:
121     control_min=-1;
122     control_max=1;
123     state_convert=radtodeg;
124     solver_eps=tolerance/100;
125     break;
126   case tAltAGL:
127     control_min=0;
128     control_max=30;
129     control_value=fdmex->GetPropagate()->GetDistanceAGL();
130     solver_eps=tolerance/100;
131     break;
132   case tTheta:
133     control_min=fdmex->GetPropagate()->GetEuler(eTht) - 5*degtorad;
134     control_max=fdmex->GetPropagate()->GetEuler(eTht) + 5*degtorad;
135     state_convert=radtodeg;
136     break;
137   case tPhi:
138     control_min=fdmex->GetPropagate()->GetEuler(ePhi) - 30*degtorad;
139     control_max=fdmex->GetPropagate()->GetEuler(ePhi) + 30*degtorad;
140     state_convert=radtodeg;
141     control_convert=radtodeg;
142     break;
143   case tGamma:
144     solver_eps=tolerance/100;
145     control_min=-80*degtorad;
146     control_max=80*degtorad;
147     control_convert=radtodeg;
148     break;
149   case tHeading:
150     control_min=fdmex->GetPropagate()->GetEuler(ePsi) - 30*degtorad;
151     control_max=fdmex->GetPropagate()->GetEuler(ePsi) + 30*degtorad;
152     state_convert=radtodeg;
153     break;
154   }
155
156
157   Debug(0);
158 }
159
160 /*****************************************************************************/
161
162 FGTrimAxis::~FGTrimAxis(void)
163 {
164   Debug(1);
165 }
166
167 /*****************************************************************************/
168
169 void FGTrimAxis::getState(void) {
170   switch(state) {
171   case tUdot: state_value=fdmex->GetAccelerations()->GetUVWdot(1)-state_target; break;
172   case tVdot: state_value=fdmex->GetAccelerations()->GetUVWdot(2)-state_target; break;
173   case tWdot: state_value=fdmex->GetAccelerations()->GetUVWdot(3)-state_target; break;
174   case tQdot: state_value=fdmex->GetAccelerations()->GetPQRdot(2)-state_target;break;
175   case tPdot: state_value=fdmex->GetAccelerations()->GetPQRdot(1)-state_target; break;
176   case tRdot: state_value=fdmex->GetAccelerations()->GetPQRdot(3)-state_target; break;
177   case tHmgt: state_value=computeHmgt()-state_target; break;
178   case tNlf:  state_value=fdmex->GetAuxiliary()->GetNlf()-state_target; break;
179   case tAll: break;
180   }
181 }
182
183 /*****************************************************************************/
184
185 //States are not settable
186
187 void FGTrimAxis::getControl(void) {
188   switch(control) {
189   case tThrottle:  control_value=fdmex->GetFCS()->GetThrottleCmd(0); break;
190   case tBeta:      control_value=fdmex->GetAuxiliary()->Getbeta(); break;
191   case tAlpha:     control_value=fdmex->GetAuxiliary()->Getalpha();  break;
192   case tPitchTrim: control_value=fdmex->GetFCS() -> GetPitchTrimCmd(); break;
193   case tElevator:  control_value=fdmex->GetFCS() -> GetDeCmd(); break;
194   case tRollTrim:
195   case tAileron:   control_value=fdmex->GetFCS() -> GetDaCmd(); break;
196   case tYawTrim:
197   case tRudder:    control_value=fdmex->GetFCS() -> GetDrCmd(); break;
198   case tAltAGL:    control_value=fdmex->GetPropagate()->GetDistanceAGL();break;
199   case tTheta:     control_value=fdmex->GetPropagate()->GetEuler(eTht); break;
200   case tPhi:       control_value=fdmex->GetPropagate()->GetEuler(ePhi); break;
201   case tGamma:     control_value=fdmex->GetAuxiliary()->GetGamma();break;
202   case tHeading:   control_value=fdmex->GetPropagate()->GetEuler(ePsi); break;
203   }
204 }
205
206 /*****************************************************************************/
207
208 double FGTrimAxis::computeHmgt(void) {
209   double diff;
210
211   diff   = fdmex->GetPropagate()->GetEuler(ePsi) -
212              fdmex->GetAuxiliary()->GetGroundTrack();
213
214   if( diff < -M_PI ) {
215      return (diff + 2*M_PI);
216   } else if( diff > M_PI ) {
217      return (diff - 2*M_PI);
218   } else {
219      return diff;
220   }
221
222 }
223
224 /*****************************************************************************/
225
226
227 void FGTrimAxis::setControl(void) {
228   switch(control) {
229   case tThrottle:  setThrottlesPct(); break;
230   case tBeta:      fgic->SetBetaRadIC(control_value); break;
231   case tAlpha:     fgic->SetAlphaRadIC(control_value);  break;
232   case tPitchTrim: fdmex->GetFCS()->SetPitchTrimCmd(control_value); break;
233   case tElevator:  fdmex->GetFCS()->SetDeCmd(control_value); break;
234   case tRollTrim:
235   case tAileron:   fdmex->GetFCS()->SetDaCmd(control_value); break;
236   case tYawTrim:
237   case tRudder:    fdmex->GetFCS()->SetDrCmd(control_value); break;
238   case tAltAGL:    fgic->SetAltitudeAGLFtIC(control_value); break;
239   case tTheta:     fgic->SetThetaRadIC(control_value); break;
240   case tPhi:       fgic->SetPhiRadIC(control_value); break;
241   case tGamma:     fgic->SetFlightPathAngleRadIC(control_value); break;
242   case tHeading:   fgic->SetPsiRadIC(control_value); break;
243   }
244 }
245
246
247
248
249
250 /*****************************************************************************/
251
252 // the aircraft center of rotation is no longer the cg once the gear
253 // contact the ground so the altitude needs to be changed when pitch
254 // and roll angle are adjusted.  Instead of attempting to calculate the
255 // new center of rotation, pick a gear unit as a reference and use its
256 // location vector to calculate the new height change. i.e. new altitude =
257 // earth z component of that vector (which is in body axes )
258 void FGTrimAxis::SetThetaOnGround(double ff) {
259   int center,i,ref;
260
261   // favor an off-center unit so that the same one can be used for both
262   // pitch and roll.  An on-center unit is used (for pitch)if that's all
263   // that's in contact with the ground.
264   i=0; ref=-1; center=-1;
265   while( (ref < 0) && (i < fdmex->GetGroundReactions()->GetNumGearUnits()) ) {
266     if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) {
267       if(fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01)
268         ref=i;
269       else
270         center=i;
271     }
272     i++;
273   }
274   if((ref < 0) && (center >= 0)) {
275     ref=center;
276   }
277   cout << "SetThetaOnGround ref gear: " << ref << endl;
278   if(ref >= 0) {
279     double sp = fdmex->GetPropagate()->GetSinEuler(ePhi);
280     double cp = fdmex->GetPropagate()->GetCosEuler(ePhi);
281     double lx = fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
282     double ly = fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
283     double lz = fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
284     double hagl = -1*lx*sin(ff) +
285                     ly*sp*cos(ff) +
286                     lz*cp*cos(ff);
287
288     fgic->SetAltitudeAGLFtIC(hagl);
289     cout << "SetThetaOnGround new alt: " << hagl << endl;
290   }
291   fgic->SetThetaRadIC(ff);
292   cout << "SetThetaOnGround new theta: " << ff << endl;
293 }
294
295 /*****************************************************************************/
296
297 bool FGTrimAxis::initTheta(void) {
298   int i,N;
299   int iForward = 0;
300   int iAft = 1;
301   double zAft,zForward,zDiff,theta;
302   double xAft,xForward,xDiff;
303   bool level;
304   double saveAlt;
305
306   saveAlt=fgic->GetAltitudeAGLFtIC();
307   fgic->SetAltitudeAGLFtIC(100);
308
309
310   N=fdmex->GetGroundReactions()->GetNumGearUnits();
311
312   //find the first wheel unit forward of the cg
313   //the list is short so a simple linear search is fine
314   for( i=0; i<N; i++ ) {
315     if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) > 0 ) {
316         iForward=i;
317         break;
318     }
319   }
320   //now find the first wheel unit aft of the cg
321   for( i=0; i<N; i++ ) {
322     if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) < 0 ) {
323         iAft=i;
324         break;
325     }
326   }
327
328   // now adjust theta till the wheels are the same distance from the ground
329   xAft=fdmex->GetGroundReactions()->GetGearUnit(iAft)->GetBodyLocation(1);
330   xForward=fdmex->GetGroundReactions()->GetGearUnit(iForward)->GetBodyLocation(1);
331   xDiff = xForward - xAft;
332   zAft=fdmex->GetGroundReactions()->GetGearUnit(iAft)->GetLocalGear(3);
333   zForward=fdmex->GetGroundReactions()->GetGearUnit(iForward)->GetLocalGear(3);
334   zDiff = zForward - zAft;
335   level=false;
336   theta=fgic->GetThetaDegIC();
337   while(!level && (i < 100)) {
338     theta+=radtodeg*atan(zDiff/xDiff);
339     fgic->SetThetaDegIC(theta);
340     fdmex->RunIC();
341     zAft=fdmex->GetGroundReactions()->GetGearUnit(iAft)->GetLocalGear(3);
342     zForward=fdmex->GetGroundReactions()->GetGearUnit(iForward)->GetLocalGear(3);
343     zDiff = zForward - zAft;
344     //cout << endl << theta << "  " << zDiff << endl;
345     //cout << "0: " << fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear() << endl;
346     //cout << "1: " << fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear() << endl;
347     if(fabs(zDiff ) < 0.1)
348         level=true;
349     i++;
350   }
351   //cout << i << endl;
352   if (debug_lvl > 0) {
353       cout << "    Initial Theta: " << fdmex->GetPropagate()->GetEuler(eTht)*radtodeg << endl;
354       cout << "    Used gear unit " << iAft << " as aft and " << iForward << " as forward" << endl;
355   }
356   control_min=(theta+5)*degtorad;
357   control_max=(theta-5)*degtorad;
358   fgic->SetAltitudeAGLFtIC(saveAlt);
359   if(i < 100)
360     return true;
361   else
362     return false;
363 }
364
365 /*****************************************************************************/
366
367 void FGTrimAxis::SetPhiOnGround(double ff) {
368   int i,ref;
369
370   i=0; ref=-1;
371   //must have an off-center unit here
372   while ( (ref < 0) && (i < fdmex->GetGroundReactions()->GetNumGearUnits()) ) {
373     if ( (fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) &&
374       (fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01))
375         ref=i;
376     i++;
377   }
378   if (ref >= 0) {
379     double st = fdmex->GetPropagate()->GetSinEuler(eTht);
380     double ct = fdmex->GetPropagate()->GetCosEuler(eTht);
381     double lx = fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
382     double ly = fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
383     double lz = fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
384     double hagl = -1*lx*st +
385                     ly*sin(ff)*ct +
386                     lz*cos(ff)*ct;
387
388     fgic->SetAltitudeAGLFtIC(hagl);
389   }
390   fgic->SetPhiRadIC(ff);
391
392 }
393
394 /*****************************************************************************/
395
396 void FGTrimAxis::Run(void) {
397
398   double last_state_value;
399   int i;
400   setControl();
401   //cout << "FGTrimAxis::Run: " << control_value << endl;
402   i=0;
403   bool stable=false;
404   while(!stable) {
405     i++;
406     last_state_value=state_value;
407     fdmex->RunIC();
408     getState();
409     if(i > 1) {
410       if((fabs(last_state_value - state_value) < tolerance) || (i >= 100) )
411         stable=true;
412     }
413   }
414
415   its_to_stable_value=i;
416   total_stability_iterations+=its_to_stable_value;
417   total_iterations++;
418 }
419
420 /*****************************************************************************/
421
422 void FGTrimAxis::setThrottlesPct(void) {
423   double tMin,tMax;
424   for(unsigned i=0;i<fdmex->GetPropulsion()->GetNumEngines();i++) {
425       tMin=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMin();
426       tMax=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMax();
427
428       // Both the main throttle setting in FGFCS and the copy of the position
429       // in the Propulsion::Inputs structure need to be set at this time.
430       fdmex->GetFCS()->SetThrottleCmd(i,tMin+control_value*(tMax-tMin));
431       fdmex->GetPropulsion()->in.ThrottlePos[i] = tMin +control_value*(tMax - tMin);
432
433       fdmex->RunIC(); //apply throttle change
434       fdmex->GetPropulsion()->GetSteadyState();
435   }
436 }
437
438 /*****************************************************************************/
439
440 void FGTrimAxis::AxisReport(void) {
441   // Save original cout format characteristics
442   std::ios_base::fmtflags originalFormat = cout.flags();
443   std::streamsize originalPrecision = cout.precision();
444   std::streamsize originalWidth = cout.width();
445   cout << "  " << setw(20) << GetControlName() << ": ";
446   cout << setw(6) << setprecision(2) << GetControl()*control_convert << ' ';
447   cout << setw(5) << GetStateName() << ": ";
448   cout << setw(9) << setprecision(2) << scientific << GetState()+state_target;
449   cout << " Tolerance: " << setw(3) << setprecision(0) << scientific << GetTolerance();
450
451   if( fabs(GetState()+state_target) < fabs(GetTolerance()) )
452      cout << "  Passed" << endl;
453   else
454      cout << "  Failed" << endl;
455   // Restore original cout format characteristics
456   cout.flags(originalFormat);
457   cout.precision(originalPrecision);
458   cout.width(originalWidth);
459 }
460
461 /*****************************************************************************/
462
463 double FGTrimAxis::GetAvgStability( void ) {
464   if(total_iterations > 0) {
465     return double(total_stability_iterations)/double(total_iterations);
466   }
467   return 0;
468 }
469
470 /*****************************************************************************/
471 //    The bitmasked value choices are as follows:
472 //    unset: In this case (the default) JSBSim would only print
473 //       out the normally expected messages, essentially echoing
474 //       the config files as they are read. If the environment
475 //       variable is not set, debug_lvl is set to 1 internally
476 //    0: This requests JSBSim not to output any messages
477 //       whatsoever.
478 //    1: This value explicity requests the normal JSBSim
479 //       startup messages
480 //    2: This value asks for a message to be printed out when
481 //       a class is instantiated
482 //    4: When this value is set, a message is displayed when a
483 //       FGModel object executes its Run() method
484 //    8: When this value is set, various runtime state variables
485 //       are printed out periodically
486 //    16: When set various parameters are sanity checked and
487 //       a message is printed out when they go out of bounds
488
489 void FGTrimAxis::Debug(int from)
490 {
491
492   if (debug_lvl <= 0) return;
493   if (debug_lvl & 1 ) { // Standard console startup message output
494     if (from == 0) { // Constructor
495
496     }
497   }
498   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
499     if (from == 0) cout << "Instantiated: FGTrimAxis" << endl;
500     if (from == 1) cout << "Destroyed:    FGTrimAxis" << endl;
501   }
502   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
503   }
504   if (debug_lvl & 8 ) { // Runtime state variables
505   }
506   if (debug_lvl & 16) { // Sanity checking
507   }
508   if (debug_lvl & 64) {
509     if (from == 0) { // Constructor
510       cout << IdSrc << endl;
511       cout << IdHdr << endl;
512     }
513   }
514 }
515 }