]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGTrimAxis.cpp
Syncing with the very latest JSBSim development code.
[flightgear.git] / src / FDM / JSBSim / 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 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 General Public License for more
17  details.
18  
19  You should have received a copy of the GNU 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 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 #include <string>
36 #include <stdlib.h>
37
38 #include "FGFDMExec.h"
39 #include "FGAtmosphere.h"
40 #include "FGInitialCondition.h"
41 #include "FGTrimAxis.h"
42 #include "FGAircraft.h"
43 #include "FGPropulsion.h"
44
45 static const char *IdSrc = "$Id$";
46 static const char *IdHdr = ID_TRIMAXIS;
47
48 /*****************************************************************************/
49
50 FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, State st,
51                        Control ctrl) {
52
53   fdmex=fdex;
54   fgic=ic;
55   state=st;
56   control=ctrl;
57   solver_eps=tolerance;
58   max_iterations=10;
59   control_value=0;
60   its_to_stable_value=0;
61   total_iterations=0;
62   total_stability_iterations=0;
63   state_convert=1.0;
64   control_convert=1.0;
65   state_value=0;
66   switch(control) {
67   case tThrottle:
68     control_min=0;
69     control_max=1;
70     control_value=0.5;
71     break;
72   case tBeta:
73     control_min=-30*DEGTORAD;
74     control_max=30*DEGTORAD;
75     control_convert=RADTODEG;
76     break;
77   case tAlpha:
78     control_min=fdmex->GetAircraft()->GetAlphaCLMin();
79     control_max=fdmex->GetAircraft()->GetAlphaCLMax();
80     if(control_max <= control_min) {
81       control_max=20*DEGTORAD;
82       control_min=-5*DEGTORAD;
83     }
84     control_value= (control_min+control_max)/2;
85     control_convert=RADTODEG;
86     solver_eps=tolerance/100;
87     break;
88   case tPitchTrim:
89   case tElevator:
90   case tRollTrim:
91   case tAileron:
92   case tYawTrim:
93   case tRudder:
94     control_min=-1;
95     control_max=1;
96     state_convert=RADTODEG;
97     solver_eps=tolerance/100;
98     break;
99   case tAltAGL:
100     control_min=0;
101     control_max=30;
102     control_value=fdmex->GetPosition()->GetDistanceAGL();
103     solver_eps=tolerance/100;
104     break;
105   case tTheta:
106     control_min=fdmex->GetRotation()->Gettht() - 5*DEGTORAD;
107     control_max=fdmex->GetRotation()->Gettht() + 5*DEGTORAD;
108     state_convert=RADTODEG;
109     break;
110   case tPhi:
111     control_min=fdmex->GetRotation()->Getphi() - 30*DEGTORAD;
112     control_max=fdmex->GetRotation()->Getphi() + 30*DEGTORAD;
113     state_convert=RADTODEG;
114     control_convert=RADTODEG;
115     break;
116   case tGamma:
117     solver_eps=tolerance/100;
118     control_min=-80*DEGTORAD;
119     control_max=80*DEGTORAD;
120     control_convert=RADTODEG;
121     break;
122   case tHeading:
123     control_min=fdmex->GetRotation()->Getpsi() - 30*DEGTORAD;
124     control_max=fdmex->GetRotation()->Getpsi() + 30*DEGTORAD;
125     state_convert=RADTODEG;
126     break;
127   }
128   
129   switch(state) {
130     case tUdot: tolerance = DEFAULT_TOLERANCE; break;
131     case tVdot: tolerance = DEFAULT_TOLERANCE; break;
132     case tWdot: tolerance = DEFAULT_TOLERANCE; break;
133     case tQdot: tolerance = DEFAULT_TOLERANCE / 10; break;
134     case tPdot: tolerance = DEFAULT_TOLERANCE / 10; break;
135     case tRdot: tolerance = DEFAULT_TOLERANCE / 10; break;
136     case tHmgt: tolerance = 0.01; break;
137   }  
138   
139   if (debug_lvl & 2) cout << "Instantiated: FGTrimAxis" << endl;
140 }
141
142 /*****************************************************************************/
143
144 FGTrimAxis::~FGTrimAxis()
145 {
146   if (debug_lvl & 2) cout << "Destroyed:    FGTrimAxis" << endl;
147 }
148
149 /*****************************************************************************/
150
151 void FGTrimAxis::getState(void) {
152   switch(state) {
153   case tUdot: state_value=fdmex->GetTranslation()->GetUVWdot(1); break;
154   case tVdot: state_value=fdmex->GetTranslation()->GetUVWdot(2); break;
155   case tWdot: state_value=fdmex->GetTranslation()->GetUVWdot(3); break;
156   case tQdot: state_value=fdmex->GetRotation()->GetPQRdot(2);break;
157   case tPdot: state_value=fdmex->GetRotation()->GetPQRdot(1); break;
158   case tRdot: state_value=fdmex->GetRotation()->GetPQRdot(3); break;
159   case tHmgt: state_value=computeHmgt(); break;
160   }
161 }
162
163 /*****************************************************************************/
164
165 //States are not settable
166
167 void FGTrimAxis::getControl(void) {
168   switch(control) {
169   case tThrottle:  control_value=fdmex->GetFCS()->GetThrottleCmd(0); break;
170   case tBeta:      control_value=fdmex->GetTranslation()->Getalpha(); break;
171   case tAlpha:     control_value=fdmex->GetTranslation()->Getbeta();  break;
172   case tPitchTrim: control_value=fdmex->GetFCS() -> GetPitchTrimCmd(); break;
173   case tElevator:  control_value=fdmex->GetFCS() -> GetDeCmd(); break;
174   case tRollTrim:
175   case tAileron:   control_value=fdmex->GetFCS() -> GetDaCmd(); break;
176   case tYawTrim:
177   case tRudder:    control_value=fdmex->GetFCS() -> GetDrCmd(); break;
178   case tAltAGL:    control_value=fdmex->GetPosition()->GetDistanceAGL();break;
179   case tTheta:     control_value=fdmex->GetRotation()->Gettht(); break;
180   case tPhi:       control_value=fdmex->GetRotation()->Getphi(); break;
181   case tGamma:     control_value=fdmex->GetPosition()->GetGamma();break;
182   case tHeading:   control_value=fdmex->GetRotation()->Getpsi(); break;
183   }
184 }
185
186 /*****************************************************************************/
187
188 float FGTrimAxis::computeHmgt(void) {
189   float diff;
190   
191   diff   = fdmex->GetRotation()->Getpsi() - 
192              fdmex->GetPosition()->GetGroundTrack();
193   
194   if( diff < -M_PI ) {
195      return (diff + 2*M_PI);
196   } else if( diff > M_PI ) {
197      return (diff - 2*M_PI);
198   } else {
199      return diff;
200   }
201
202 }
203        
204 /*****************************************************************************/
205
206
207 void FGTrimAxis::setControl(void) {
208   switch(control) {
209   case tThrottle:  setThrottlesPct(); break;
210   case tBeta:      fgic->SetBetaRadIC(control_value); break;
211   case tAlpha:     fgic->SetAlphaRadIC(control_value);  break;
212   case tPitchTrim: fdmex->GetFCS()->SetPitchTrimCmd(control_value); break;
213   case tElevator:  fdmex->GetFCS()->SetDeCmd(control_value); break;
214   case tRollTrim:
215   case tAileron:   fdmex->GetFCS()->SetDaCmd(control_value); break;
216   case tYawTrim:
217   case tRudder:    fdmex->GetFCS()->SetDrCmd(control_value); break;
218   case tAltAGL:    fgic->SetAltitudeAGLFtIC(control_value); break;
219   case tTheta:     fgic->SetPitchAngleRadIC(control_value); break;
220   case tPhi:       fgic->SetRollAngleRadIC(control_value); break;
221   case tGamma:     fgic->SetFlightPathAngleRadIC(control_value); break;
222   case tHeading:   fgic->SetTrueHeadingRadIC(control_value); break;
223   }
224 }
225
226
227   
228
229
230 /*****************************************************************************/
231
232 // the aircraft center of rotation is no longer the cg once the gear
233 // contact the ground so the altitude needs to be changed when pitch 
234 // and roll angle are adjusted.  Instead of attempting to calculate the 
235 // new center of rotation, pick a gear unit as a reference and use its
236 // location vector to calculate the new height change. i.e. new altitude =
237 // earth z component of that vector (which is in body axes )  
238 void FGTrimAxis::SetThetaOnGround(float ff) {
239   int center,i,ref;
240
241   // favor an off-center unit so that the same one can be used for both
242   // pitch and roll.  An on-center unit is used (for pitch)if that's all 
243   // that's in contact with the ground.
244   i=0; ref=-1; center=-1;
245   while( (ref < 0) && (i < fdmex->GetAircraft()->GetNumGearUnits()) ) {
246     if(fdmex->GetAircraft()->GetGearUnit(i)->GetWOW()) {
247       if(fabs(fdmex->GetAircraft()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01)
248         ref=i;
249       else
250         center=i;
251     } 
252     i++; 
253   }
254   if((ref < 0) && (center >= 0)) {
255     ref=center;
256   }
257   cout << "SetThetaOnGround ref gear: " << ref << endl;
258   if(ref >= 0) {
259     float sp=fdmex->GetRotation()->GetSinphi();
260     float cp=fdmex->GetRotation()->GetCosphi();
261     float lx=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation(1);
262     float ly=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation(2);
263     float lz=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation(3);
264     float hagl = -1*lx*sin(ff) +
265                     ly*sp*cos(ff) +
266                     lz*cp*cos(ff);
267    
268     fgic->SetAltitudeAGLFtIC(hagl);
269     cout << "SetThetaOnGround new alt: " << hagl << endl;
270   }                   
271   fgic->SetPitchAngleRadIC(ff);  
272   cout << "SetThetaOnGround new theta: " << ff << endl;      
273 }      
274
275 /*****************************************************************************/
276
277 bool FGTrimAxis::initTheta(void) {
278   int i,N,iAft, iForward;
279   float zAft,zForward,zDiff,theta;
280   bool level;  
281   float saveAlt;
282   
283   saveAlt=fgic->GetAltitudeAGLFtIC();
284   fgic->SetAltitudeAGLFtIC(100);
285   
286   
287   N=fdmex->GetAircraft()->GetNumGearUnits();
288   
289   //find the first wheel unit forward of the cg
290   //the list is short so a simple linear search is fine
291   for( i=0; i<N; i++ ) {
292     if(fdmex->GetAircraft()->GetGearUnit(i)->GetBodyLocation(1) > 0 ) {
293         iForward=i;
294         break;
295     }
296   }
297   //now find the first wheel unit aft of the cg
298   for( i=0; i<N; i++ ) {
299     if(fdmex->GetAircraft()->GetGearUnit(i)->GetBodyLocation(1) < 0 ) {
300         iAft=i;
301         break;
302     }
303   }
304           
305   // now adjust theta till the wheels are the same distance from the ground
306   zAft=fdmex->GetAircraft()->GetGearUnit(1)->GetLocalGear(3);
307   zForward=fdmex->GetAircraft()->GetGearUnit(0)->GetLocalGear(3);
308   zDiff = zForward - zAft;
309   level=false;
310   theta=fgic->GetPitchAngleDegIC(); 
311   while(!level && (i < 100)) {
312         theta+=2.0*zDiff;
313         fgic->SetPitchAngleDegIC(theta);   
314         fdmex->RunIC(fgic);
315         zAft=fdmex->GetAircraft()->GetGearUnit(1)->GetLocalGear(3);
316         zForward=fdmex->GetAircraft()->GetGearUnit(0)->GetLocalGear(3);
317         zDiff = zForward - zAft;
318         //cout << endl << theta << "  " << zDiff << endl;
319         //cout << "0: " << fdmex->GetAircraft()->GetGearUnit(0)->GetLocalGear() << endl;
320         //cout << "1: " << fdmex->GetAircraft()->GetGearUnit(1)->GetLocalGear() << endl;
321
322         if(fabs(zDiff ) < 0.1) 
323             level=true;
324         i++;   
325   }                     
326   //cout << i << endl;
327   cout << "    Initial Theta: " << fdmex->GetRotation()->Gettht()*RADTODEG << endl;
328   control_min=(theta+5)*DEGTORAD;
329   control_max=(theta-5)*DEGTORAD;
330   fgic->SetAltitudeAGLFtIC(saveAlt);
331   if(i < 100) 
332     return true;
333   else
334     return false;  
335
336
337 /*****************************************************************************/
338
339 void FGTrimAxis::SetPhiOnGround(float ff) {
340   int i,ref;
341
342   i=0; ref=-1;
343   //must have an off-center unit here 
344   while( (ref < 0) && (i < fdmex->GetAircraft()->GetNumGearUnits()) ) {
345     if( (fdmex->GetAircraft()->GetGearUnit(i)->GetWOW()) && 
346       (fabs(fdmex->GetAircraft()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01))
347         ref=i;
348     i++; 
349   }
350   if(ref >= 0) {
351     float st=fdmex->GetRotation()->GetSintht();
352     float ct=fdmex->GetRotation()->GetCostht();
353     float lx=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation(1);
354     float ly=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation(2);
355     float lz=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation(3);
356     float hagl = -1*lx*st +
357                     ly*sin(ff)*ct +
358                     lz*cos(ff)*ct;
359    
360     fgic->SetAltitudeAGLFtIC(hagl);
361   }                   
362   fgic->SetRollAngleRadIC(ff);
363            
364 }      
365
366 /*****************************************************************************/
367
368 void FGTrimAxis::Run(void) {
369
370   float last_state_value;
371   int i;
372   setControl();
373   //cout << "FGTrimAxis::Run: " << control_value << endl;
374   i=0;
375   bool stable=false;
376   while(!stable) {
377     i++;
378     last_state_value=state_value;
379     fdmex->RunIC(fgic);
380     getState();
381     if(i > 1) {
382       if((fabs(last_state_value - state_value) < tolerance) || (i >= 100) )
383         stable=true;
384     }
385   }
386
387   its_to_stable_value=i;
388   total_stability_iterations+=its_to_stable_value;
389   total_iterations++;
390 }
391
392 /*****************************************************************************/
393
394 void FGTrimAxis::setThrottlesPct(void) {
395   float tMin,tMax;
396   for(unsigned i=0;i<fdmex->GetPropulsion()->GetNumEngines();i++) {
397       tMin=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMin();
398       tMax=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMax();
399       //cout << "setThrottlespct: " << i << ", " << control_min << ", " << control_max << ", " << control_value;
400       fdmex -> GetFCS() -> SetThrottleCmd(i,tMin+control_value*(tMax-tMin));
401   }
402 }
403
404 /*****************************************************************************/
405
406 void FGTrimAxis::AxisReport(void) {
407   
408   char out[80];
409   sprintf(out,"  %20s: %6.2f %5s: %9.2e Tolerance: %3.0e\n",
410            GetControlName().c_str(), GetControl()*control_convert,
411            GetStateName().c_str(), GetState(), GetTolerance()); 
412   cout << out;
413
414 }
415
416 /*****************************************************************************/
417
418 float FGTrimAxis::GetAvgStability( void ) {
419   if(total_iterations > 0) {
420     return float(total_stability_iterations)/float(total_iterations);
421   }
422   return 0;
423 }
424
425 /*****************************************************************************/
426
427 void FGTrimAxis::Debug(void)
428 {
429 }
430