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,
60 its_to_stable_value=0;
62 total_stability_iterations=0;
73 control_min=-30*DEGTORAD;
74 control_max=30*DEGTORAD;
75 control_convert=RADTODEG;
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;
84 control_value= (control_min+control_max)/2;
85 control_convert=RADTODEG;
86 solver_eps=tolerance/100;
96 state_convert=RADTODEG;
97 solver_eps=tolerance/100;
102 control_value=fdmex->GetPosition()->GetDistanceAGL();
103 solver_eps=tolerance/100;
106 control_min=fdmex->GetRotation()->Gettht() - 5*DEGTORAD;
107 control_max=fdmex->GetRotation()->Gettht() + 5*DEGTORAD;
108 state_convert=RADTODEG;
111 control_min=fdmex->GetRotation()->Getphi() - 30*DEGTORAD;
112 control_max=fdmex->GetRotation()->Getphi() + 30*DEGTORAD;
113 state_convert=RADTODEG;
114 control_convert=RADTODEG;
117 solver_eps=tolerance/100;
118 control_min=-80*DEGTORAD;
119 control_max=80*DEGTORAD;
120 control_convert=RADTODEG;
123 control_min=fdmex->GetRotation()->Getpsi() - 30*DEGTORAD;
124 control_max=fdmex->GetRotation()->Getpsi() + 30*DEGTORAD;
125 state_convert=RADTODEG;
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;
139 if (debug_lvl & 2) cout << "Instantiated: FGTrimAxis" << endl;
142 /*****************************************************************************/
144 FGTrimAxis::~FGTrimAxis()
146 if (debug_lvl & 2) cout << "Destroyed: FGTrimAxis" << endl;
149 /*****************************************************************************/
151 void FGTrimAxis::getState(void) {
153 case tUdot: state_value=fdmex->GetAircraft()->GetBodyAccel()(1); break;
154 case tVdot: state_value=fdmex->GetAircraft()->GetBodyAccel()(2); break;
155 case tWdot: state_value=fdmex->GetAircraft()->GetBodyAccel()(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;
163 /*****************************************************************************/
165 //States are not settable
167 void FGTrimAxis::getControl(void) {
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;
175 case tAileron: control_value=fdmex->GetFCS() -> GetDaCmd(); break;
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;
186 /*****************************************************************************/
188 float FGTrimAxis::computeHmgt(void) {
191 diff = fdmex->GetRotation()->Getpsi() -
192 fdmex->GetPosition()->GetGroundTrack();
195 return (diff + 2*M_PI);
196 } else if( diff > M_PI ) {
197 return (diff - 2*M_PI);
204 /*****************************************************************************/
207 void FGTrimAxis::setControl(void) {
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;
215 case tAileron: fdmex->GetFCS()->SetDaCmd(control_value); break;
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;
230 /*****************************************************************************/
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) {
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->GetGroundReactions()->GetNumGearUnits()) ) {
246 if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) {
247 if(fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01)
254 if((ref < 0) && (center >= 0)) {
257 cout << "SetThetaOnGround ref gear: " << ref << endl;
259 float sp=fdmex->GetRotation()->GetSinphi();
260 float cp=fdmex->GetRotation()->GetCosphi();
261 float lx=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
262 float ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
263 float lz=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
264 float hagl = -1*lx*sin(ff) +
268 fgic->SetAltitudeAGLFtIC(hagl);
269 cout << "SetThetaOnGround new alt: " << hagl << endl;
271 fgic->SetPitchAngleRadIC(ff);
272 cout << "SetThetaOnGround new theta: " << ff << endl;
275 /*****************************************************************************/
277 bool FGTrimAxis::initTheta(void) {
278 int i,N,iAft, iForward;
279 float zAft,zForward,zDiff,theta;
283 saveAlt=fgic->GetAltitudeAGLFtIC();
284 fgic->SetAltitudeAGLFtIC(100);
287 N=fdmex->GetGroundReactions()->GetNumGearUnits();
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->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) > 0 ) {
297 //now find the first wheel unit aft of the cg
298 for( i=0; i<N; i++ ) {
299 if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) < 0 ) {
305 // now adjust theta till the wheels are the same distance from the ground
306 zAft=fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear(3);
307 zForward=fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear(3);
308 zDiff = zForward - zAft;
310 theta=fgic->GetPitchAngleDegIC();
311 while(!level && (i < 100)) {
313 fgic->SetPitchAngleDegIC(theta);
315 zAft=fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear(3);
316 zForward=fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear(3);
317 zDiff = zForward - zAft;
318 //cout << endl << theta << " " << zDiff << endl;
319 //cout << "0: " << fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear() << endl;
320 //cout << "1: " << fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear() << endl;
322 if(fabs(zDiff ) < 0.1)
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);
337 /*****************************************************************************/
339 void FGTrimAxis::SetPhiOnGround(float ff) {
343 //must have an off-center unit here
344 while( (ref < 0) && (i < fdmex->GetGroundReactions()->GetNumGearUnits()) ) {
345 if( (fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) &&
346 (fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01))
351 float st=fdmex->GetRotation()->GetSintht();
352 float ct=fdmex->GetRotation()->GetCostht();
353 float lx=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
354 float ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
355 float lz=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
356 float hagl = -1*lx*st +
360 fgic->SetAltitudeAGLFtIC(hagl);
362 fgic->SetRollAngleRadIC(ff);
366 /*****************************************************************************/
368 void FGTrimAxis::Run(void) {
370 float last_state_value;
373 //cout << "FGTrimAxis::Run: " << control_value << endl;
378 last_state_value=state_value;
382 if((fabs(last_state_value - state_value) < tolerance) || (i >= 100) )
387 its_to_stable_value=i;
388 total_stability_iterations+=its_to_stable_value;
392 /*****************************************************************************/
394 void FGTrimAxis::setThrottlesPct(void) {
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 //cout << "setThrottlespct: " << fdmex->GetFCS()->GetThrottleCmd(i) << endl;
402 fdmex->RunIC(fgic); //apply throttle change
403 fdmex->GetPropulsion()->GetSteadyState();
407 /*****************************************************************************/
409 void FGTrimAxis::AxisReport(void) {
412 sprintf(out," %20s: %6.2f %5s: %9.2e Tolerance: %3.0e\n",
413 GetControlName().c_str(), GetControl()*control_convert,
414 GetStateName().c_str(), GetState(), GetTolerance());
419 /*****************************************************************************/
421 float FGTrimAxis::GetAvgStability( void ) {
422 if(total_iterations > 0) {
423 return float(total_stability_iterations)/float(total_iterations);
428 /*****************************************************************************/
430 void FGTrimAxis::Debug(void)