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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36 # pragma warning (disable : 4786)
42 #include "FGFDMExec.h"
43 #include "FGAtmosphere.h"
44 #include "FGInitialCondition.h"
45 #include "FGTrimAxis.h"
46 #include "FGAircraft.h"
47 #include "FGPropulsion.h"
48 #include "FGAerodynamics.h"
51 static const char *IdSrc = "$Id$";
52 static const char *IdHdr = ID_TRIMAXIS;
54 /*****************************************************************************/
56 FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, State st,
65 its_to_stable_value=0;
67 total_stability_iterations=0;
73 case tUdot: tolerance = DEFAULT_TOLERANCE; break;
74 case tVdot: tolerance = DEFAULT_TOLERANCE; break;
75 case tWdot: tolerance = DEFAULT_TOLERANCE; break;
76 case tQdot: tolerance = DEFAULT_TOLERANCE / 10; break;
77 case tPdot: tolerance = DEFAULT_TOLERANCE / 10; break;
78 case tRdot: tolerance = DEFAULT_TOLERANCE / 10; break;
79 case tHmgt: tolerance = 0.01; break;
80 case tNlf: state_target=1.0; tolerance = 1E-5; break;
92 control_min=-30*degtorad;
93 control_max=30*degtorad;
94 control_convert=radtodeg;
97 control_min=fdmex->GetAerodynamics()->GetAlphaCLMin();
98 control_max=fdmex->GetAerodynamics()->GetAlphaCLMax();
99 if(control_max <= control_min) {
100 control_max=20*degtorad;
101 control_min=-5*degtorad;
103 control_value= (control_min+control_max)/2;
104 control_convert=radtodeg;
105 solver_eps=tolerance/100;
115 state_convert=radtodeg;
116 solver_eps=tolerance/100;
121 control_value=fdmex->GetPosition()->GetDistanceAGL();
122 solver_eps=tolerance/100;
125 control_min=fdmex->GetRotation()->Gettht() - 5*degtorad;
126 control_max=fdmex->GetRotation()->Gettht() + 5*degtorad;
127 state_convert=radtodeg;
130 control_min=fdmex->GetRotation()->Getphi() - 30*degtorad;
131 control_max=fdmex->GetRotation()->Getphi() + 30*degtorad;
132 state_convert=radtodeg;
133 control_convert=radtodeg;
136 solver_eps=tolerance/100;
137 control_min=-80*degtorad;
138 control_max=80*degtorad;
139 control_convert=radtodeg;
142 control_min=fdmex->GetRotation()->Getpsi() - 30*degtorad;
143 control_max=fdmex->GetRotation()->Getpsi() + 30*degtorad;
144 state_convert=radtodeg;
152 /*****************************************************************************/
154 FGTrimAxis::~FGTrimAxis(void)
159 /*****************************************************************************/
161 void FGTrimAxis::getState(void) {
163 case tUdot: state_value=fdmex->GetTranslation()->GetUVWdot(1)-state_target; break;
164 case tVdot: state_value=fdmex->GetTranslation()->GetUVWdot(2)-state_target; break;
165 case tWdot: state_value=fdmex->GetTranslation()->GetUVWdot(3)-state_target; break;
166 case tQdot: state_value=fdmex->GetRotation()->GetPQRdot(2)-state_target;break;
167 case tPdot: state_value=fdmex->GetRotation()->GetPQRdot(1)-state_target; break;
168 case tRdot: state_value=fdmex->GetRotation()->GetPQRdot(3)-state_target; break;
169 case tHmgt: state_value=computeHmgt()-state_target; break;
170 case tNlf: state_value=fdmex->GetAircraft()->GetNlf()-state_target; break;
175 /*****************************************************************************/
177 //States are not settable
179 void FGTrimAxis::getControl(void) {
181 case tThrottle: control_value=fdmex->GetFCS()->GetThrottleCmd(0); break;
182 case tBeta: control_value=fdmex->GetTranslation()->Getalpha(); break;
183 case tAlpha: control_value=fdmex->GetTranslation()->Getbeta(); break;
184 case tPitchTrim: control_value=fdmex->GetFCS() -> GetPitchTrimCmd(); break;
185 case tElevator: control_value=fdmex->GetFCS() -> GetDeCmd(); break;
187 case tAileron: control_value=fdmex->GetFCS() -> GetDaCmd(); break;
189 case tRudder: control_value=fdmex->GetFCS() -> GetDrCmd(); break;
190 case tAltAGL: control_value=fdmex->GetPosition()->GetDistanceAGL();break;
191 case tTheta: control_value=fdmex->GetRotation()->Gettht(); break;
192 case tPhi: control_value=fdmex->GetRotation()->Getphi(); break;
193 case tGamma: control_value=fdmex->GetPosition()->GetGamma();break;
194 case tHeading: control_value=fdmex->GetRotation()->Getpsi(); break;
198 /*****************************************************************************/
200 double FGTrimAxis::computeHmgt(void) {
203 diff = fdmex->GetRotation()->Getpsi() -
204 fdmex->GetPosition()->GetGroundTrack();
207 return (diff + 2*M_PI);
208 } else if( diff > M_PI ) {
209 return (diff - 2*M_PI);
216 /*****************************************************************************/
219 void FGTrimAxis::setControl(void) {
221 case tThrottle: setThrottlesPct(); break;
222 case tBeta: fgic->SetBetaRadIC(control_value); break;
223 case tAlpha: fgic->SetAlphaRadIC(control_value); break;
224 case tPitchTrim: fdmex->GetFCS()->SetPitchTrimCmd(control_value); break;
225 case tElevator: fdmex->GetFCS()->SetDeCmd(control_value); break;
227 case tAileron: fdmex->GetFCS()->SetDaCmd(control_value); break;
229 case tRudder: fdmex->GetFCS()->SetDrCmd(control_value); break;
230 case tAltAGL: fgic->SetAltitudeAGLFtIC(control_value); break;
231 case tTheta: fgic->SetPitchAngleRadIC(control_value); break;
232 case tPhi: fgic->SetRollAngleRadIC(control_value); break;
233 case tGamma: fgic->SetFlightPathAngleRadIC(control_value); break;
234 case tHeading: fgic->SetTrueHeadingRadIC(control_value); break;
242 /*****************************************************************************/
244 // the aircraft center of rotation is no longer the cg once the gear
245 // contact the ground so the altitude needs to be changed when pitch
246 // and roll angle are adjusted. Instead of attempting to calculate the
247 // new center of rotation, pick a gear unit as a reference and use its
248 // location vector to calculate the new height change. i.e. new altitude =
249 // earth z component of that vector (which is in body axes )
250 void FGTrimAxis::SetThetaOnGround(double ff) {
253 // favor an off-center unit so that the same one can be used for both
254 // pitch and roll. An on-center unit is used (for pitch)if that's all
255 // that's in contact with the ground.
256 i=0; ref=-1; center=-1;
257 while( (ref < 0) && (i < fdmex->GetGroundReactions()->GetNumGearUnits()) ) {
258 if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) {
259 if(fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01)
266 if((ref < 0) && (center >= 0)) {
269 cout << "SetThetaOnGround ref gear: " << ref << endl;
271 double sp=fdmex->GetRotation()->GetSinphi();
272 double cp=fdmex->GetRotation()->GetCosphi();
273 double lx=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
274 double ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
275 double lz=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
276 double hagl = -1*lx*sin(ff) +
280 fgic->SetAltitudeAGLFtIC(hagl);
281 cout << "SetThetaOnGround new alt: " << hagl << endl;
283 fgic->SetPitchAngleRadIC(ff);
284 cout << "SetThetaOnGround new theta: " << ff << endl;
287 /*****************************************************************************/
289 bool FGTrimAxis::initTheta(void) {
290 int i,N,iAft, iForward;
291 double zAft,zForward,zDiff,theta;
295 saveAlt=fgic->GetAltitudeAGLFtIC();
296 fgic->SetAltitudeAGLFtIC(100);
299 N=fdmex->GetGroundReactions()->GetNumGearUnits();
301 //find the first wheel unit forward of the cg
302 //the list is short so a simple linear search is fine
303 for( i=0; i<N; i++ ) {
304 if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) > 0 ) {
309 //now find the first wheel unit aft of the cg
310 for( i=0; i<N; i++ ) {
311 if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) < 0 ) {
317 // now adjust theta till the wheels are the same distance from the ground
318 zAft=fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear(3);
319 zForward=fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear(3);
320 zDiff = zForward - zAft;
322 theta=fgic->GetPitchAngleDegIC();
323 while(!level && (i < 100)) {
325 fgic->SetPitchAngleDegIC(theta);
327 zAft=fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear(3);
328 zForward=fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear(3);
329 zDiff = zForward - zAft;
330 //cout << endl << theta << " " << zDiff << endl;
331 //cout << "0: " << fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear() << endl;
332 //cout << "1: " << fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear() << endl;
334 if(fabs(zDiff ) < 0.1)
339 cout << " Initial Theta: " << fdmex->GetRotation()->Gettht()*radtodeg << endl;
340 control_min=(theta+5)*degtorad;
341 control_max=(theta-5)*degtorad;
342 fgic->SetAltitudeAGLFtIC(saveAlt);
349 /*****************************************************************************/
351 void FGTrimAxis::SetPhiOnGround(double ff) {
355 //must have an off-center unit here
356 while( (ref < 0) && (i < fdmex->GetGroundReactions()->GetNumGearUnits()) ) {
357 if( (fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) &&
358 (fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01))
363 double st=fdmex->GetRotation()->GetSintht();
364 double ct=fdmex->GetRotation()->GetCostht();
365 double lx=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
366 double ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
367 double lz=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
368 double hagl = -1*lx*st +
372 fgic->SetAltitudeAGLFtIC(hagl);
374 fgic->SetRollAngleRadIC(ff);
378 /*****************************************************************************/
380 void FGTrimAxis::Run(void) {
382 double last_state_value;
385 //cout << "FGTrimAxis::Run: " << control_value << endl;
390 last_state_value=state_value;
394 if((fabs(last_state_value - state_value) < tolerance) || (i >= 100) )
399 its_to_stable_value=i;
400 total_stability_iterations+=its_to_stable_value;
404 /*****************************************************************************/
406 void FGTrimAxis::setThrottlesPct(void) {
408 for(unsigned i=0;i<fdmex->GetPropulsion()->GetNumEngines();i++) {
409 tMin=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMin();
410 tMax=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMax();
411 //cout << "setThrottlespct: " << i << ", " << control_min << ", " << control_max << ", " << control_value;
412 fdmex->GetFCS()->SetThrottleCmd(i,tMin+control_value*(tMax-tMin));
413 //cout << "setThrottlespct: " << fdmex->GetFCS()->GetThrottleCmd(i) << endl;
414 fdmex->RunIC(); //apply throttle change
415 fdmex->GetPropulsion()->GetSteadyState();
419 /*****************************************************************************/
421 void FGTrimAxis::AxisReport(void) {
424 sprintf(out," %20s: %6.2f %5s: %9.2e Tolerance: %3.0e\n",
425 GetControlName().c_str(), GetControl()*control_convert,
426 GetStateName().c_str(), GetState()+state_target, GetTolerance());
431 /*****************************************************************************/
433 double FGTrimAxis::GetAvgStability( void ) {
434 if(total_iterations > 0) {
435 return double(total_stability_iterations)/double(total_iterations);
440 /*****************************************************************************/
441 // The bitmasked value choices are as follows:
442 // unset: In this case (the default) JSBSim would only print
443 // out the normally expected messages, essentially echoing
444 // the config files as they are read. If the environment
445 // variable is not set, debug_lvl is set to 1 internally
446 // 0: This requests JSBSim not to output any messages
448 // 1: This value explicity requests the normal JSBSim
450 // 2: This value asks for a message to be printed out when
451 // a class is instantiated
452 // 4: When this value is set, a message is displayed when a
453 // FGModel object executes its Run() method
454 // 8: When this value is set, various runtime state variables
455 // are printed out periodically
456 // 16: When set various parameters are sanity checked and
457 // a message is printed out when they go out of bounds
459 void FGTrimAxis::Debug(int from)
462 if (debug_lvl <= 0) return;
463 if (debug_lvl & 1 ) { // Standard console startup message output
464 if (from == 0) { // Constructor
468 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
469 if (from == 0) cout << "Instantiated: FGTrimAxis" << endl;
470 if (from == 1) cout << "Destroyed: FGTrimAxis" << endl;
472 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
474 if (debug_lvl & 8 ) { // Runtime state variables
476 if (debug_lvl & 16) { // Sanity checking
478 if (debug_lvl & 64) {
479 if (from == 0) { // Constructor
480 cout << IdSrc << endl;
481 cout << IdHdr << endl;