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 --------------------------------------------------------------------------------
32 FUNCTIONAL DESCRIPTION
33 --------------------------------------------------------------------------------
35 This class takes the given set of IC's and finds the angle of attack, elevator,
36 and throttle setting required to fly steady level. This is currently for in-air
37 conditions only. It is implemented using an iterative, one-axis-at-a-time
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 #include "FGFDMExec.h"
52 #include "FGJSBBase.h"
53 #include "FGRotation.h"
54 #include "FGAtmosphere.h"
57 #include "FGAircraft.h"
58 #include "FGTranslation.h"
59 #include "FGPosition.h"
60 #include "FGAuxiliary.h"
63 #include "FGTrimAxis.h"
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71 #define ID_TRIM "$Id$"
73 typedef enum { tLongitudinal, tFull, tGround, tCustom, tNone } TrimMode;
75 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
79 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
83 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
87 /** FGTrim -- the trimming routine for JSBSim.
88 FGTrim finds the aircraft attitude and control settings needed to maintain
89 the steady state described by the FGInitialCondition object . It does this
90 iteratively by assigning a control to each state and adjusting that control
91 until the state is within a specified tolerance of zero. States include the
92 recti-linear accelerations udot, vdot, and wdot, the angular accelerations
93 qdot, pdot, and rdot, and the difference between heading and ground track.
94 Controls include the usual flight deck controls available to the pilot plus
95 angle of attack (alpha), sideslip angle(beta), flight path angle (gamma),
96 pitch attitude(theta), roll attitude(phi), and altitude above ground. The
97 last three are used for on-ground trimming. The state-control pairs used in
98 a given trim are completely user configurable and several pre-defined modes
99 are provided as well. They are:
101 <li> tLongitudinal: Trim wdot with alpha, udot with thrust, qdot with elevator</li>
102 <li> tFull: tLongitudinal + vdot with phi, pdot with aileron, rdot with rudder
103 and heading minus ground track (hmgt) with beta</li>
104 <li> tGround: wdot with altitude, qdot with theta, and pdot with phi</li>
105 The remaining modes include <b>tCustom</b>, which is completely user defined and
108 Currently, this class cannot trim a non-1g condition and is limited to
109 trimming for constant true airspeed in climbs and descents.
111 Note that trims can (and do) fail for reasons that are completely outside
112 the control of the trimming routine itself. The most common problem is the
113 initial conditions: is the model capable of steady state flight
114 at those conditions? Check the speed, altitude, configuration (flaps,
115 gear, etc.), weight, cg, and anything else that may be relevant.
118 FGFDMExec* FDMExec = new FGFDMExec();
122 FGInitialCondition* fgic = new FGInitialCondition(FDMExec);
123 FGTrim *fgt(FDMExec,fgic,tFull);
124 fgic->SetVcaibratedKtsIC(100);
125 fgic->SetAltitudeFtIC(1000);
126 fgic->SetClimbRate(500);
127 if( !fgt->DoTrim() ) {
128 cout << "Trim Failed" << endl;
135 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
139 class FGTrim : public FGJSBBase
143 vector<FGTrimAxis*> TrimAxes;
148 double Tolerance, A_Tolerance;
149 double wdot,udot,qdot;
151 double *sub_iterations;
154 int max_sub_iterations;
162 double xlo,xhi,alo,ahi;
165 FGInitialCondition* fgic;
169 /** @return false if there is no change in the current axis accel
170 between accel(control_min) and accel(control_max). If there is a
171 change, sets solutionDomain to:
172 0 for no sign change,
173 -1 if sign change between accel(control_min) and accel(0)
174 1 if sign between accel(0) and accel(control_max)
176 bool findInterval(void);
178 bool checkLimits(void);
181 /** Initializes the trimming class
182 @param FDMExec pointer to a JSBSim executive object.
183 @param FGIC pointer to a FGInitialCondition object
184 @param TrimMode the set of axes to trim. Can be:
185 tLongitudinal, tFull, tGround, tCustom, or tNone
187 FGTrim(FGFDMExec *FDMExec, FGInitialCondition *FGIC, TrimMode tt);
195 /** Print the results of the trim. For each axis trimmed, this
196 includes the final state value, control value, and tolerance
198 @return true if trim succeeds
202 /** Iteration statistics
206 /** Clear all state-control pairs from the current configuration.
207 The trimming routine must have at least one state-control pair
208 configured to be useful
210 void ClearStates(void);
212 /** Add a state-control pair to the current configuration. See the enums
213 State and Control in FGTrimAxis.h for the available options.
214 Will fail if the given state is already configured.
215 @param state the accel or other condition to zero
216 @param control the control used to zero the state
217 @return true if add is successful
219 bool AddState( State state, Control control );
221 /** Remove a specific state-control pair from the current configuration
222 @param state the state to remove
223 @return true if removal is successful
225 bool RemoveState( State state );
227 /** Change the control used to zero a state previously configured
228 @param state the accel or other condition to zero
229 @param control the control used to zero the state
231 bool EditState( State state, Control new_control );
233 /** automatically switch to trimming longitudinal acceleration with
234 flight path angle (gamma) once it becomes apparent that there
235 is not enough/too much thrust.
236 @param gamma_fallback true to enable fallback
238 inline void SetGammaFallback(bool bb) { gamma_fallback=true; }
240 /** query the fallback state
241 @return true if fallback is enabled.
243 inline bool GetGammaFallback(void) { return gamma_fallback; }
245 /** Set the iteration limit. DoTrim() will return false if limit
246 iterations are reached before trim is achieved. The default
247 is 60. This does not ordinarily need to be changed.
248 @param ii integer iteration limit
250 inline void SetMaxCycles(int ii) { max_iterations = ii; }
252 /** Set the per-axis iteration limit. Attempt to zero each state
253 by iterating limit times before moving on to the next. The
254 default limit is 100 and also does not ordinarily need to
256 @param ii integer iteration limit
258 inline void SetMaxCyclesPerAxis(int ii) { max_sub_iterations = ii; }
260 /** Set the tolerance for declaring a state trimmed. Angular accels are
261 held to a tolerance of 1/10th of the given. The default is
262 0.001 for the recti-linear accelerations and 0.0001 for the angular.
264 inline void SetTolerance(double tt) {
266 A_Tolerance = tt / 10;
269 //Debug level 1 shows results of each top-level iteration
270 //Debug level 2 shows level 1 & results of each per-axis iteration
271 inline void SetDebug(int level) { Debug = level; }
272 inline void ClearDebug(void) { Debug = 0; }