1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Header:       FGTrimAxis.h
4  Author:       Tony Peden
5  Date started: 7/3/00
6 
7  ------------- Copyright (C) 1999  Anthony K. Peden (apeden@earthlink.net) -------------
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  HISTORY
27 --------------------------------------------------------------------------------
28 7/3/00  TP   Created
29 
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 SENTRY
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 
34 #ifndef FGTRIMAXIS_H
35 #define FGTRIMAXIS_H
36 
37 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 
41 #include <string>
42 
43 #include "FGFDMExec.h"
44 #include "FGJSBBase.h"
45 #include "FGInitialCondition.h"
46 
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 DEFINITIONS
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50 
51 #define DEFAULT_TOLERANCE 0.001
52 
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 FORWARD DECLARATIONS
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 
57 namespace JSBSim {
58 
59 const std::string StateNames[] =   { "all","udot","vdot","wdot","qdot","pdot",
60                                      "rdot","hmgt","nlf"
61                                    };
62 const std::string ControlNames[] =  { "Throttle","Sideslip","Angle of Attack",
63                                       "Elevator","Ailerons","Rudder",
64                                       "Altitude AGL", "Pitch Angle",
65                                       "Roll Angle", "Flight Path Angle",
66                                       "Pitch Trim", "Roll Trim", "Yaw Trim",
67                                       "Heading"
68                                     };
69 
70 class FGInitialCondition;
71 
72 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 CLASS DOCUMENTATION
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 
76 /** Models an aircraft axis for purposes of trimming.
77   */
78 
79 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 CLASS DECLARATION
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
82 
83 enum State { tAll,tUdot,tVdot,tWdot,tQdot,tPdot,tRdot,tHmgt,tNlf };
84 enum Control { tThrottle, tBeta, tAlpha, tElevator, tAileron, tRudder, tAltAGL,
85                tTheta, tPhi, tGamma, tPitchTrim, tRollTrim, tYawTrim, tHeading };
86 
87 class FGTrimAxis : public FGJSBBase
88 {
89 public:
90   /**  Constructor for Trim Axis class.
91        @param fdmex FGFDMExec pointer
92        @param IC pointer to initial conditions instance
93        @param state a State type (enum)
94        @param control a Control type (enum) */
95   FGTrimAxis(FGFDMExec* fdmex,
96              FGInitialCondition *IC,
97              State state,
98              Control control );
99   /// Destructor
100   ~FGTrimAxis();
101 
102   /** This function iterates through a call to the FGFDMExec::RunIC()
103       function until the desired trimming condition falls inside a tolerance.*/
104   void Run(void);
105 
GetState(void)106   double GetState(void) { getState(); return state_value; }
107   //Accels are not settable
SetControl(double value)108   inline void SetControl(double value ) { control_value=value; }
GetControl(void)109   inline double GetControl(void) { return control_value; }
110 
GetStateType(void)111   inline State GetStateType(void) { return state; }
GetControlType(void)112   inline Control GetControlType(void) { return control; }
113 
GetStateName(void)114   inline std::string GetStateName(void) { return StateNames[state]; }
GetControlName(void)115   inline std::string GetControlName(void) { return ControlNames[control]; }
116 
GetControlMin(void)117   inline double GetControlMin(void) { return control_min; }
GetControlMax(void)118   inline double GetControlMax(void) { return control_max; }
119 
SetControlToMin(void)120   inline void SetControlToMin(void) { control_value=control_min; }
SetControlToMax(void)121   inline void SetControlToMax(void) { control_value=control_max; }
122 
SetControlLimits(double min,double max)123   inline void SetControlLimits(double min, double max) {
124       control_min=min;
125       control_max=max;
126   }
127 
SetTolerance(double ff)128   inline void  SetTolerance(double ff) { tolerance=ff;}
GetTolerance(void)129   inline double GetTolerance(void) { return tolerance; }
130 
GetSolverEps(void)131   inline double GetSolverEps(void) { return solver_eps; }
SetSolverEps(double ff)132   inline void SetSolverEps(double ff) { solver_eps=ff; }
133 
GetIterationLimit(void)134   inline int  GetIterationLimit(void) { return max_iterations; }
SetIterationLimit(int ii)135   inline void SetIterationLimit(int ii) { max_iterations=ii; }
136 
GetStability(void)137   inline int GetStability(void) { return its_to_stable_value; }
GetRunCount(void)138   inline int GetRunCount(void) { return total_stability_iterations; }
139   double GetAvgStability( void );
140 
SetStateTarget(double target)141   inline void SetStateTarget(double target) { state_target=target; }
GetStateTarget(void)142   inline double GetStateTarget(void) { return state_target; }
143 
144   void AxisReport(void);
145 
InTolerance(void)146   bool InTolerance(void) { getState(); return (fabs(state_value) <= tolerance); }
147 
148 private:
149   FGFDMExec *fdmex;
150   FGInitialCondition *fgic;
151 
152   State   state;
153   Control control;
154 
155   double state_target;
156 
157   double state_value;
158   double control_value;
159 
160   double control_min;
161   double control_max;
162 
163   double tolerance;
164 
165   double solver_eps;
166 
167   double state_convert;
168   double control_convert;
169 
170   int max_iterations;
171 
172   int its_to_stable_value;
173   int total_stability_iterations;
174   int total_iterations;
175 
176   void setThrottlesPct(void);
177 
178   void getState(void);
179   void getControl(void);
180   void setControl(void);
181 
182   double computeHmgt(void);
183 
184   void Debug(int from);
185 };
186 }
187 #endif
188