JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
FGTrimAnalysis.h
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Header: FGTrimAnalysis.h
4  Author: Agostino De Marco
5  Date started: Dec/14/2006
6 
7  ------------- Copyright (C) 2006 Agostino De Marco (agodemar@unina.it) -------
8 
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser 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 Lesser General Public License for more
17  details.
18 
19  You should have received a copy of the GNU Lesser 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 Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25 
26 
27  HISTORY
28 --------------------------------------------------------------------------------
29 12/14/06 ADM Created
30 
31 
32 FUNCTIONAL DESCRIPTION
33 --------------------------------------------------------------------------------
34 
35 This class takes the given set of IC's and analyzes the possible trim states
36 of the aircraft, i.e. finds the aircraft state required to
37 maintain a specified flight condition. This flight condition can be
38 steady-level, a steady turn, a pull-up or pushover.
39 It is implemented using an iterative, direct search of a cost function minimum.
40 
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 SENTRY
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 
45 #ifndef FGTRIMANAlYSIS_H
46 #define FGTRIMANAlYSIS_H
47 
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 INCLUDES
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 
52 #include "FGFDMExec.h"
53 #include "FGJSBBase.h"
54 #include "FGTrimAnalysisControl.h"
55 
56 #include "models/FGAtmosphere.h"
57 #include "initialization/FGInitialCondition.h"
58 #include "models/FGAircraft.h"
59 #include "models/FGMassBalance.h"
60 #include "models/FGGroundReactions.h"
61 #include "models/FGInertial.h"
62 #include "models/FGAerodynamics.h"
63 #include "math/FGColumnVector3.h"
64 #include "models/FGAuxiliary.h"
65 #include "models/FGPropulsion.h"
66 
67 #include <vector>
68 #include <map>
69 #include <string>
70 
71 #include "math/direct_search/vec.h"
72 
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 DEFINITIONS
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76 
77 #define ID_FGTRIMANALYSIS "$Id: FGTrimAnalysis.h,v 1.8 2009/10/02 10:30:09 jberndt Exp $"
78 
79 #if defined(_WIN32) && !defined(__CYGWIN__)
80  #define snprintf _snprintf
81 #endif
82 
83 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 FORWARD DECLARATIONS
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
86 
87 namespace JSBSim {
88 
89 typedef enum { taLongitudinal=0, taFull, taFullWingsLevel, taTurn, taPullup, taTurnFull,
90  taGround, taCustom, taNone } TrimAnalysisMode;
91 
92 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 CLASS DOCUMENTATION
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
95 
123 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 CLASS DECLARATION
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
126 
127 class FGTrimAnalysis;
128 
129 class Objective {
130 public:
133  Objective(FGFDMExec* fdmex, FGTrimAnalysis* ta, double x);
137 
145  void CostFunctionFull (long vars, Vector<double> &v, double & f);
153  void CostFunctionFullWingsLevel (long vars, Vector<double> &v, double & f);
161  void CostFunctionLongitudinal (long vars, Vector<double> &v, double & f);
169  void CostFunctionFullTurn (long vars, Vector<double> &v, double & f);
177  void CostFunctionFullCoordinatedTurn (long vars, Vector<double> &v, double & f);
178 
186  void CostFunctionPullUp (long vars, Vector<double> &v, double & f);
187 
200  friend void find_CostFunctionFull(long vars, Vector<double> &v, double & f,
201  bool & success, void* t_ptr);
202  /* function definiation moved outside of class declaration */
203 
216  friend void find_CostFunctionFullWingsLevel(long vars, Vector<double> &v, double & f,
217  bool & success, void* t_ptr);
218  /* function definiation moved outside of class declaration */
219 
232  friend void find_CostFunctionLongitudinal(long vars, Vector<double> &v, double & f,
233  bool & success, void* t_ptr);
234  /* function definiation moved outside of class declaration */
235 
248  friend void find_CostFunctionFullCoordinatedTurn(long vars, Vector<double> &v, double & f,
249  bool & success, void* t_ptr);
250  /* function definiation moved outside of class declaration */
251 
264  friend void find_CostFunctionFullTurn(long vars, Vector<double> &v, double & f,
265  bool & success, void* t_ptr);
266  /* function definiation moved outside of class declaration */
267 
280  friend void find_CostFunctionPullUp(long vars, Vector<double> &v, double & f,
281  bool & success, void* t_ptr);
282  /* function definiation moved outside of class declaration */
283 
284  void Set_x_val(double new_x);
285  double Get_x_val() const {return _x;}
286 
289  typedef void (*PF)(long vars, Vector<double> &v, double & f, bool & success, void* t_ptr);
290 
291  map<TrimAnalysisMode,PF> mpCostFunctions; // primordial...
292 
293 private:
297  void calculateDottedStates(double delta_cmd_T, double delta_cmd_E, double delta_cmd_A, double delta_cmd_R,
298  double phi, double theta, double psi,
299  TrimAnalysisMode trimMode,
300  double& alpha, double& beta, double& gamma,
301  //FGColumnVector3& vUVW, FGColumnVector3& vPQR,
303  FGColumnVector3& vUVWdot, FGColumnVector3& vPQRdot );
304 
307  double myCostFunctionFull (Vector<double> & vec1);
310  double myCostFunctionFullWingsLevel (Vector<double> & vec1);
313  double myCostFunctionLongitudinal (Vector<double> & vec1);
316  double myCostFunctionFullCoordinatedTurn (Vector<double> & vec1);
319  double myCostFunctionFullTurn (Vector<double> & vec1);
322  double myCostFunctionPullUp (Vector<double> & vec1);
323 
324  double _x;
325  FGFDMExec* FDMExec;
326  FGTrimAnalysis* TrimAnalysis;
327 };
328 
329 
330 class FGTrimAnalysis : public FGJSBBase, public FGXMLFileRead
331 {
332 private:
333  vector<FGTrimAnalysisControl*> vTrimAnalysisControls;
334  double cost_function_value;
335  unsigned int current_ctrl;
336  int N, Nsub;
337  TrimAnalysisMode mode;
338  int DebugLevel, Debug;
339 
340  double dth;
341  bool trimudot;
342  bool gamma_fallback;
343 
344  bool trim_failed;
345  unsigned int ctrl_count;
346 
347  FGFDMExec* fdmex;
348  FGInitialCondition * fgic; // IC;
349 
350  FGAuxiliary * Auxiliary;
351  FGAerodynamics * Aerodynamics;
352  FGPropulsion * Propulsion;
353  FGFCS * FCS;
354 
355  vector<double> vAlphaDeg, vAlphaTrimDeg; // DEG !!!
356  vector<double> vCL, vCD, vCm;
357  vector<double> vCLE, vCDE, vCmE;
358  vector<vector <double> > mCL, mCD, mCm, mTa;
359  vector<double> vThrottleCmd, vElevatorCmd;
360  vector<double> vVn, vTn, vTa, vVnE, vTE;
361  vector<double> vCt, vCq, vRPM;
362  vector<vector <double> > mCt, mCq, mRPM;
363  vector<bool> hasThrustTrim, hasCmTrim;
364 
365  string trim_id;
366 
367  // direct search stuff
368  string search_type;
369  double sigma_nm, alpha_nm, beta_nm, gamma_nm;
370  double initial_step;
371  double tolerance;
372  string stop_criterion;
373  int max_iterations;
374  int total_its;
375 
376  // results file
377  mutable ofstream rf; // see Stroustrup 10.2.7.2; alternative: use a cache struct
378  string rf_name;
379 
380  double _u,_v,_w;
381  double _p,_q,_r;
382  //double uw,vw,ww;
383  //double vnorth,veast,vdown;
384  //double wnorth,weast,wdown;
385  double _alpha, _beta, _theta, _phi, _psi, _psiW, _gamma, _phiW;
386 
387  double _stheta, _sphi, _spsi;
388  double _ctheta, _cphi, _cpsi;
389 
390  double _vtIC, _hIC, _gammaIC, _rocIC, _vdownIC, _psiIC, _psigtIC, _vgIC,
391  _vnorthIC, _veastIC, wnorthIC, _weastIC, _wdownIC;
392 
393  double _udot, _vdot, _wdot, _pdot, _qdot, _rdot;
394  double _targetNlf;
395  double _psiWdot, _phiWdot, _gammadot, _psidot, _thetadot;
396 
397  double C1, C2, C3, _calpha, _salpha, _cbeta, _sbeta;
398 
399  void setupPullup(void);
400  void setupTurn(void);
401  void setupTurn(double phiW); // recalculate target Nlf
402  void setupTurnPhi(double psi, double theta); // recalculate only phi
403 
404  void updateRates(void);
405 
406  void setDebug(void);
407 
408  bool ensureRunning(void);
409  bool ensureRunning(unsigned int i);
410  bool runForAWhile(int nruns);
411 
412  bool populateVecAlphaDeg(double vmin, double vmax, int n);
413  bool populateVecThrottleCmd(double vmin, double vmax, int n);
414  bool populateVecElevatorCmd(double vmin, double vmax, int n);
415 
416  bool calculateAerodynamics(
417  double dE_cmd,
418  double Vt,
419  double alpha_deg,
420  double altitude,
421  double rho,
422  double S, double mac, double bw,
423  double& CL, double& CD, double& Cm);
424 
425 
426  bool getSteadyState(int nrepeat);
427  bool InitializeTrimControl(double default_value, Element* el,
428  string unit, TaControl type);
429 
430 public:
435  FGTrimAnalysis(FGFDMExec *FDMExec, TrimAnalysisMode tam=taFull );
436 
438  ~FGTrimAnalysis(void);
439 
440  friend class Objective;
441 
446  bool Load(string fname, bool useStoredPath = true );
447 
450  bool DoTrim(void);
451 
457  void Report(void);
458 
461  void TrimStats();
462 
468  bool SetResultsFile(string name);
469 
473  ofstream* GetResultsFile() const { if (rf.is_open()) return &rf; else return 0; } // const_cast<ofstream*>(&rf) (if rf is not mutable)
474 
478  inline void SetCostFunctionValue(double value){cost_function_value = value;}
479 
482  inline double GetCostFunctionValue() const { return cost_function_value;}
483 
490  void SetMode(TrimAnalysisMode tam);
491 
495  inline TrimAnalysisMode GetMode() const { return mode;};
496 
497  inline vector<FGTrimAnalysisControl*>* GetControls(){return &vTrimAnalysisControls;}
498 
505  void ClearControls(void);
506 
515  bool AddControl( TaControl control );
516 
521  bool RemoveControl( TaControl control );
522 
530  bool EditState( TaControl new_control, double new_initvalue, double new_step, double new_min, double new_max );
531 
532 
536  inline double GetGamma() { return _gamma; }
537 
543  inline void SetGammaFallback(bool bb) { gamma_fallback=bb; }
544 
548  inline bool GetGammaFallback(void) { return gamma_fallback; }
549 
555  inline void SetMaxCycles(int ii) { max_iterations = ii; }
556 
563  inline void SetTolerance(double tt) {
564  tolerance = tt;
565  }
569  inline double GetTolerance(void) {return tolerance; }
570 
574  inline void SetTrimFailed(bool tf) { trim_failed = tf; }
578  inline bool GetTrimFailed(void) { return trim_failed; }
579  inline void SetTrimSuccessfull() { trim_failed = false; }
580 
581 
596  void SetState(double u0, double v0, double w0, double p0, double q0, double r0,
597  double alpha0, double beta0, double phi0, double theta0, double psi0, double gamma0);
598 
601  void SetEulerAngles(double phi0, double theta0, double psi0);
602 
606  double GetPhiRad (){ return _phi;}
610  double GetThetaRad(){ return _theta;}
614  double GetPsiRad (){ return _psi;}
615 
619  double GetPhiWRad (){ return _phiW;}
623  double GetGammaRad (){ return _gamma;}
627  double GetVtFps (){ return _vtIC;}
628 
632  void CalculatePhiWFromTargetNlfTurn(double nlf);
633 
638  FGColumnVector3 UpdateRatesTurn(double psi, double theta, double phi, double phiW);
639 
643  FGColumnVector3 UpdateRatesPullup(void);
644 
647  void SetDottedValues(double udot, double vdot, double wdot, double pdot, double qdot, double rdot);
648 
653  inline void SetDebug(int level) { DebugLevel = level; }
654  inline void ClearDebug(void) { DebugLevel = 0; }
655 
658  inline void SetTargetNlf(double nlf) { _targetNlf=nlf; }
659 
663  inline double GetTargetNlf(void) { return _targetNlf; }
664 
665  //void eom_costf (long vars, Vector<double> &x, double & f, bool& flag, void* an_obj);
666  //inline FGFDMExec* GetFDMExec() const { return fdmex; }
667 
668 };
669 }
670 
671 #endif
bool GetGammaFallback(void)
query the fallback state
double GetPhiRad()
Gets Euler angle phi.
double GetGamma()
Return the current flight path angle in TrimAnalysis object.
double GetPhiWRad()
Gets Euler angle phiW (wind axes)
TrimAnalysisMode GetMode() const
void SetDebug(int level)
Debug level 1 shows results of each top-level iteration Debug level 2 shows level 1 & results of each...
friend void find_CostFunctionFullWingsLevel(long vars, Vector< double > &v, double &f, bool &success, void *t_ptr)
Wrapping function for the effective Wings Level Trim cost function, to be called by optimization meth...
void SetMaxCycles(int ii)
Set the iteration limit.
ofstream * GetResultsFile() const
Get the pointer to the file where trim analysis results are written,.
void SetCostFunctionValue(double value)
Set the value of the cost function.
Encapsulates the aerodynamic calculations.
friend void find_CostFunctionLongitudinal(long vars, Vector< double > &v, double &f, bool &success, void *t_ptr)
Wrapping function for the effective Longitudinal Trim cost function, to be called by optimization met...
double GetThetaRad()
Gets Euler angle theta.
void SetTrimFailed(bool tf)
Sets trim result status.
The current vehicle state vector structure contains the translational and angular position...
Definition: FGPropagate.h:107
void CostFunctionFullTurn(long vars, Vector< double > &v, double &f)
Steady Turn Trim cost function.
friend void find_CostFunctionPullUp(long vars, Vector< double > &v, double &f, bool &success, void *t_ptr)
Wrapping function for the effective Pullup Trim cost function, to be called by optimization method...
Objective(FGFDMExec *fdmex, FGTrimAnalysis *ta, double x)
Constructor.
~Objective()
Destructor.
void CostFunctionFullCoordinatedTurn(long vars, Vector< double > &v, double &f)
Steady Turn Trim cost function, NON-coordinated.
double GetVtFps()
Gets true speed [fps] from IC.
Propulsion management class.
Definition: FGPropulsion.h:106
JSBSim Base class.
Definition: FGJSBBase.h:80
double GetTargetNlf(void)
Gets target normal load factor in steady turn.
void(* PF)(long vars, Vector< double > &v, double &f, bool &success, void *t_ptr)
Pointer to cost function implementation.
void CostFunctionLongitudinal(long vars, Vector< double > &v, double &f)
Longitudinal Trim cost function.
void CostFunctionFullWingsLevel(long vars, Vector< double > &v, double &f)
Wings Level Trim cost function.
void SetGammaFallback(bool bb)
automatically switch to trimming longitudinal acceleration with flight path angle (gamma) once it bec...
This class implements a 3 element column vector.
double GetPsiRad()
Gets Euler angle psi.
void CostFunctionPullUp(long vars, Vector< double > &v, double &f)
Pullup Trim cost function.
Initializes the simulation run.
Encapsulates the Flight Control System (FCS) functionality.
Definition: FGFCS.h:193
double GetGammaRad()
Gets flight path angle.
friend void find_CostFunctionFull(long vars, Vector< double > &v, double &f, bool &success, void *t_ptr)
Wrapping function for the effective Full Trim cost function, to be called by optimization method...
Encapsulates various uncategorized scheduled functions.
Definition: FGAuxiliary.h:109
bool GetTrimFailed(void)
Gets trim result status.
void SetTolerance(double tt)
Set the tolerance for declaring a state trimmed.
void CostFunctionFull(long vars, Vector< double > &v, double &f)
Full Trim cost function.
Encapsulates the JSBSim simulation executive.
Definition: FGFDMExec.h:189
double GetCostFunctionValue() const
double GetTolerance(void)
Get the tolerance for declaring a state trimmed.
void SetTargetNlf(double nlf)
Sets target normal load factor in steady turn.
friend void find_CostFunctionFullCoordinatedTurn(long vars, Vector< double > &v, double &f, bool &success, void *t_ptr)
Wrapping function for the effective Steady Turn Trim cost function, to be called by optimization meth...
friend void find_CostFunctionFullTurn(long vars, Vector< double > &v, double &f, bool &success, void *t_ptr)
Wrapping function for the effective Steady Turn Trim cost function, to be called by optimization meth...