1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 --------- Copyright (C) 1999 Anthony K. Peden (apeden@earthlink.net) ---------
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
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
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.
23 Further information about the GNU General Public License can also be found on
24 the world wide web at http://www.gnu.org.
28 --------------------------------------------------------------------------------
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38 #include "FGFDMExec.h"
39 #include "FGAtmosphere.h"
40 #include "FGInitialCondition.h"
41 #include "FGTrimAxis.h"
42 #include "FGAircraft.h"
43 #include "FGPropulsion.h"
45 static const char *IdSrc = "$Id$";
46 static const char *IdHdr = ID_TRIMAXIS;
48 /*****************************************************************************/
50 FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, State st,
59 its_to_stable_value=0;
61 total_stability_iterations=0;
66 case tUdot: tolerance = DEFAULT_TOLERANCE; break;
67 case tVdot: tolerance = DEFAULT_TOLERANCE; break;
68 case tWdot: tolerance = DEFAULT_TOLERANCE; break;
69 case tQdot: tolerance = DEFAULT_TOLERANCE / 10; break;
70 case tPdot: tolerance = DEFAULT_TOLERANCE / 10; break;
71 case tRdot: tolerance = DEFAULT_TOLERANCE / 10; break;
72 case tHmgt: tolerance = 0.01; break;
83 control_min=-30*degtorad;
84 control_max=30*degtorad;
85 control_convert=radtodeg;
88 control_min=fdmex->GetAircraft()->GetAlphaCLMin();
89 control_max=fdmex->GetAircraft()->GetAlphaCLMax();
90 if(control_max <= control_min) {
91 control_max=20*degtorad;
92 control_min=-5*degtorad;
94 control_value= (control_min+control_max)/2;
95 control_convert=radtodeg;
96 solver_eps=tolerance/100;
106 state_convert=radtodeg;
107 solver_eps=tolerance/100;
112 control_value=fdmex->GetPosition()->GetDistanceAGL();
113 solver_eps=tolerance/100;
116 control_min=fdmex->GetRotation()->Gettht() - 5*degtorad;
117 control_max=fdmex->GetRotation()->Gettht() + 5*degtorad;
118 state_convert=radtodeg;
121 control_min=fdmex->GetRotation()->Getphi() - 30*degtorad;
122 control_max=fdmex->GetRotation()->Getphi() + 30*degtorad;
123 state_convert=radtodeg;
124 control_convert=radtodeg;
127 solver_eps=tolerance/100;
128 control_min=-80*degtorad;
129 control_max=80*degtorad;
130 control_convert=radtodeg;
133 control_min=fdmex->GetRotation()->Getpsi() - 30*degtorad;
134 control_max=fdmex->GetRotation()->Getpsi() + 30*degtorad;
135 state_convert=radtodeg;
140 if (debug_lvl & 2) cout << "Instantiated: FGTrimAxis" << endl;
143 /*****************************************************************************/
145 FGTrimAxis::~FGTrimAxis()
147 if (debug_lvl & 2) cout << "Destroyed: FGTrimAxis" << endl;
150 /*****************************************************************************/
152 void FGTrimAxis::getState(void) {
154 case tUdot: state_value=fdmex->GetAircraft()->GetBodyAccel()(1); break;
155 case tVdot: state_value=fdmex->GetAircraft()->GetBodyAccel()(2); break;
156 case tWdot: state_value=fdmex->GetAircraft()->GetBodyAccel()(3); break;
157 case tQdot: state_value=fdmex->GetRotation()->GetPQRdot(2);break;
158 case tPdot: state_value=fdmex->GetRotation()->GetPQRdot(1); break;
159 case tRdot: state_value=fdmex->GetRotation()->GetPQRdot(3); break;
160 case tHmgt: state_value=computeHmgt(); break;
164 /*****************************************************************************/
166 //States are not settable
168 void FGTrimAxis::getControl(void) {
170 case tThrottle: control_value=fdmex->GetFCS()->GetThrottleCmd(0); break;
171 case tBeta: control_value=fdmex->GetTranslation()->Getalpha(); break;
172 case tAlpha: control_value=fdmex->GetTranslation()->Getbeta(); break;
173 case tPitchTrim: control_value=fdmex->GetFCS() -> GetPitchTrimCmd(); break;
174 case tElevator: control_value=fdmex->GetFCS() -> GetDeCmd(); break;
176 case tAileron: control_value=fdmex->GetFCS() -> GetDaCmd(); break;
178 case tRudder: control_value=fdmex->GetFCS() -> GetDrCmd(); break;
179 case tAltAGL: control_value=fdmex->GetPosition()->GetDistanceAGL();break;
180 case tTheta: control_value=fdmex->GetRotation()->Gettht(); break;
181 case tPhi: control_value=fdmex->GetRotation()->Getphi(); break;
182 case tGamma: control_value=fdmex->GetPosition()->GetGamma();break;
183 case tHeading: control_value=fdmex->GetRotation()->Getpsi(); break;
187 /*****************************************************************************/
189 double FGTrimAxis::computeHmgt(void) {
192 diff = fdmex->GetRotation()->Getpsi() -
193 fdmex->GetPosition()->GetGroundTrack();
196 return (diff + 2*M_PI);
197 } else if( diff > M_PI ) {
198 return (diff - 2*M_PI);
205 /*****************************************************************************/
208 void FGTrimAxis::setControl(void) {
210 case tThrottle: setThrottlesPct(); break;
211 case tBeta: fgic->SetBetaRadIC(control_value); break;
212 case tAlpha: fgic->SetAlphaRadIC(control_value); break;
213 case tPitchTrim: fdmex->GetFCS()->SetPitchTrimCmd(control_value); break;
214 case tElevator: fdmex->GetFCS()->SetDeCmd(control_value); break;
216 case tAileron: fdmex->GetFCS()->SetDaCmd(control_value); break;
218 case tRudder: fdmex->GetFCS()->SetDrCmd(control_value); break;
219 case tAltAGL: fgic->SetAltitudeAGLFtIC(control_value); break;
220 case tTheta: fgic->SetPitchAngleRadIC(control_value); break;
221 case tPhi: fgic->SetRollAngleRadIC(control_value); break;
222 case tGamma: fgic->SetFlightPathAngleRadIC(control_value); break;
223 case tHeading: fgic->SetTrueHeadingRadIC(control_value); break;
231 /*****************************************************************************/
233 // the aircraft center of rotation is no longer the cg once the gear
234 // contact the ground so the altitude needs to be changed when pitch
235 // and roll angle are adjusted. Instead of attempting to calculate the
236 // new center of rotation, pick a gear unit as a reference and use its
237 // location vector to calculate the new height change. i.e. new altitude =
238 // earth z component of that vector (which is in body axes )
239 void FGTrimAxis::SetThetaOnGround(double ff) {
242 // favor an off-center unit so that the same one can be used for both
243 // pitch and roll. An on-center unit is used (for pitch)if that's all
244 // that's in contact with the ground.
245 i=0; ref=-1; center=-1;
246 while( (ref < 0) && (i < fdmex->GetGroundReactions()->GetNumGearUnits()) ) {
247 if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) {
248 if(fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01)
255 if((ref < 0) && (center >= 0)) {
258 cout << "SetThetaOnGround ref gear: " << ref << endl;
260 double sp=fdmex->GetRotation()->GetSinphi();
261 double cp=fdmex->GetRotation()->GetCosphi();
262 double lx=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
263 double ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
264 double lz=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
265 double hagl = -1*lx*sin(ff) +
269 fgic->SetAltitudeAGLFtIC(hagl);
270 cout << "SetThetaOnGround new alt: " << hagl << endl;
272 fgic->SetPitchAngleRadIC(ff);
273 cout << "SetThetaOnGround new theta: " << ff << endl;
276 /*****************************************************************************/
278 bool FGTrimAxis::initTheta(void) {
279 int i,N,iAft, iForward;
280 double zAft,zForward,zDiff,theta;
284 saveAlt=fgic->GetAltitudeAGLFtIC();
285 fgic->SetAltitudeAGLFtIC(100);
288 N=fdmex->GetGroundReactions()->GetNumGearUnits();
290 //find the first wheel unit forward of the cg
291 //the list is short so a simple linear search is fine
292 for( i=0; i<N; i++ ) {
293 if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) > 0 ) {
298 //now find the first wheel unit aft of the cg
299 for( i=0; i<N; i++ ) {
300 if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) < 0 ) {
306 // now adjust theta till the wheels are the same distance from the ground
307 zAft=fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear(3);
308 zForward=fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear(3);
309 zDiff = zForward - zAft;
311 theta=fgic->GetPitchAngleDegIC();
312 while(!level && (i < 100)) {
314 fgic->SetPitchAngleDegIC(theta);
316 zAft=fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear(3);
317 zForward=fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear(3);
318 zDiff = zForward - zAft;
319 //cout << endl << theta << " " << zDiff << endl;
320 //cout << "0: " << fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear() << endl;
321 //cout << "1: " << fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear() << endl;
323 if(fabs(zDiff ) < 0.1)
328 cout << " Initial Theta: " << fdmex->GetRotation()->Gettht()*radtodeg << endl;
329 control_min=(theta+5)*degtorad;
330 control_max=(theta-5)*degtorad;
331 fgic->SetAltitudeAGLFtIC(saveAlt);
338 /*****************************************************************************/
340 void FGTrimAxis::SetPhiOnGround(double ff) {
344 //must have an off-center unit here
345 while( (ref < 0) && (i < fdmex->GetGroundReactions()->GetNumGearUnits()) ) {
346 if( (fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) &&
347 (fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01))
352 double st=fdmex->GetRotation()->GetSintht();
353 double ct=fdmex->GetRotation()->GetCostht();
354 double lx=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
355 double ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
356 double lz=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
357 double hagl = -1*lx*st +
361 fgic->SetAltitudeAGLFtIC(hagl);
363 fgic->SetRollAngleRadIC(ff);
367 /*****************************************************************************/
369 void FGTrimAxis::Run(void) {
371 double last_state_value;
374 //cout << "FGTrimAxis::Run: " << control_value << endl;
379 last_state_value=state_value;
383 if((fabs(last_state_value - state_value) < tolerance) || (i >= 100) )
388 its_to_stable_value=i;
389 total_stability_iterations+=its_to_stable_value;
393 /*****************************************************************************/
395 void FGTrimAxis::setThrottlesPct(void) {
397 for(unsigned i=0;i<fdmex->GetPropulsion()->GetNumEngines();i++) {
398 tMin=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMin();
399 tMax=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMax();
400 //cout << "setThrottlespct: " << i << ", " << control_min << ", " << control_max << ", " << control_value;
401 fdmex->GetFCS()->SetThrottleCmd(i,tMin+control_value*(tMax-tMin));
402 //cout << "setThrottlespct: " << fdmex->GetFCS()->GetThrottleCmd(i) << endl;
403 fdmex->RunIC(fgic); //apply throttle change
404 fdmex->GetPropulsion()->GetSteadyState();
408 /*****************************************************************************/
410 void FGTrimAxis::AxisReport(void) {
413 sprintf(out," %20s: %6.2f %5s: %9.2e Tolerance: %3.0e\n",
414 GetControlName().c_str(), GetControl()*control_convert,
415 GetStateName().c_str(), GetState(), GetTolerance());
420 /*****************************************************************************/
422 double FGTrimAxis::GetAvgStability( void ) {
423 if(total_iterations > 0) {
424 return double(total_stability_iterations)/double(total_iterations);
429 /*****************************************************************************/
431 void FGTrimAxis::Debug(void)