]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.cpp
- Added ultra-light traffic is now a separate traffic class that can have its
[flightgear.git] / src / FDM / JSBSim / FGState.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGState.cpp
4  Author:       Jon Berndt
5  Date started: 11/17/98
6  Called by:    FGFDMExec and accessed by all models.
7
8  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
18  details.
19
20  You should have received a copy of the GNU Lesser General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU Lesser General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 See header file.
30
31 HISTORY
32 --------------------------------------------------------------------------------
33 11/17/98   JSB   Created
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #ifdef FGFS
40 #  include <simgear/compiler.h>
41 #  include <math.h>
42 #else
43 #  if defined(sgi) && !defined(__GNUC__)
44 #    include <math.h>
45 #  else
46 #    include <cmath>
47 #  endif
48 #endif
49
50 #include "FGState.h"
51
52 namespace JSBSim {
53
54 static const char *IdSrc = "$Id$";
55 static const char *IdHdr = ID_STATE;
56
57 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 MACROS
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60
61 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 CLASS IMPLEMENTATION
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64
65 FGState::FGState(FGFDMExec* fdex)
66 {
67   FDMExec = fdex;
68
69   sim_time = 0.0;
70   dt = 1.0/120.0;
71
72   Aircraft     = FDMExec->GetAircraft();
73   Propagate    = FDMExec->GetPropagate();
74   Auxiliary    = FDMExec->GetAuxiliary();
75   FCS          = FDMExec->GetFCS();
76   Atmosphere   = FDMExec->GetAtmosphere();
77   Aerodynamics = FDMExec->GetAerodynamics();
78   GroundReactions = FDMExec->GetGroundReactions();
79   Propulsion      = FDMExec->GetPropulsion();
80   PropertyManager = FDMExec->GetPropertyManager();
81
82   bind();
83
84   Debug(0);
85 }
86
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
89 FGState::~FGState()
90 {
91   unbind();
92   Debug(1);
93 }
94
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96
97 void FGState::Initialize(FGInitialCondition *FGIC)
98 {
99   sim_time = 0.0;
100
101   Propagate->SetInitialState( FGIC );
102
103   Atmosphere->Run();
104   Atmosphere->SetWindNED( FGIC->GetWindNFpsIC(),
105                           FGIC->GetWindEFpsIC(),
106                           FGIC->GetWindDFpsIC() );
107
108   FGColumnVector3 vAeroUVW;
109   vAeroUVW = Propagate->GetUVW() + Propagate->GetTl2b()*Atmosphere->GetWindNED();
110
111   double alpha, beta;
112   if (vAeroUVW(eW) != 0.0)
113     alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
114   else
115     alpha = 0.0;
116   if (vAeroUVW(eV) != 0.0)
117     beta = vAeroUVW(eU)*vAeroUVW(eU)+vAeroUVW(eW)*vAeroUVW(eW) > 0.0 ? atan2(vAeroUVW(eV), (fabs(vAeroUVW(eU))/vAeroUVW(eU))*sqrt(vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eW)*vAeroUVW(eW))) : 0.0;
118   else
119     beta = 0.0;
120
121   Auxiliary->SetAB(alpha, beta);
122
123   double Vt = vAeroUVW.Magnitude();
124   Auxiliary->SetVt(Vt);
125
126   Auxiliary->SetMach(Vt/Atmosphere->GetSoundSpeed());
127
128   double qbar = 0.5*Vt*Vt*Atmosphere->GetDensity();
129   Auxiliary->Setqbar(qbar);
130 }
131
132 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133
134 FGMatrix33& FGState::GetTs2b(void)
135 {
136   double ca, cb, sa, sb;
137
138   double alpha = Auxiliary->Getalpha();
139   double beta  = Auxiliary->Getbeta();
140
141   ca = cos(alpha);
142   sa = sin(alpha);
143   cb = cos(beta);
144   sb = sin(beta);
145
146   mTs2b(1,1) = ca*cb;
147   mTs2b(1,2) = -ca*sb;
148   mTs2b(1,3) = -sa;
149   mTs2b(2,1) = sb;
150   mTs2b(2,2) = cb;
151   mTs2b(2,3) = 0.0;
152   mTs2b(3,1) = sa*cb;
153   mTs2b(3,2) = -sa*sb;
154   mTs2b(3,3) = ca;
155
156   return mTs2b;
157 }
158
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161 FGMatrix33& FGState::GetTb2s(void)
162 {
163   double alpha,beta;
164   double ca, cb, sa, sb;
165
166   alpha = Auxiliary->Getalpha();
167   beta  = Auxiliary->Getbeta();
168
169   ca = cos(alpha);
170   sa = sin(alpha);
171   cb = cos(beta);
172   sb = sin(beta);
173
174   mTb2s(1,1) = ca*cb;
175   mTb2s(1,2) = sb;
176   mTb2s(1,3) = sa*cb;
177   mTb2s(2,1) = -ca*sb;
178   mTb2s(2,2) = cb;
179   mTb2s(2,3) = -sa*sb;
180   mTb2s(3,1) = -sa;
181   mTb2s(3,2) = 0.0;
182   mTb2s(3,3) = ca;
183
184   return mTb2s;
185 }
186
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188
189 void FGState::bind(void)
190 {
191   PropertyManager->Tie("sim-time-sec", this, &FGState::Getsim_time);
192 }
193
194 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195
196 void FGState::unbind(void)
197 {
198   PropertyManager->Untie("sim-time-sec");
199 }
200
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 //    The bitmasked value choices are as follows:
203 //    unset: In this case (the default) JSBSim would only print
204 //       out the normally expected messages, essentially echoing
205 //       the config files as they are read. If the environment
206 //       variable is not set, debug_lvl is set to 1 internally
207 //    0: This requests JSBSim not to output any messages
208 //       whatsoever.
209 //    1: This value explicity requests the normal JSBSim
210 //       startup messages
211 //    2: This value asks for a message to be printed out when
212 //       a class is instantiated
213 //    4: When this value is set, a message is displayed when a
214 //       FGModel object executes its Run() method
215 //    8: When this value is set, various runtime state variables
216 //       are printed out periodically
217 //    16: When set various parameters are sanity checked and
218 //       a message is printed out when they go out of bounds
219
220 void FGState::Debug(int from)
221 {
222   if (debug_lvl <= 0) return;
223
224   if (debug_lvl & 1) { // Standard console startup message output
225     if (from == 0) { // Constructor
226
227     }
228   }
229   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
230     if (from == 0) cout << "Instantiated: FGState" << endl;
231     if (from == 1) cout << "Destroyed:    FGState" << endl;
232   }
233   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
234   }
235   if (debug_lvl & 8 ) { // Runtime state variables
236   }
237   if (debug_lvl & 16) { // Sanity checking
238   }
239   if (debug_lvl & 64) {
240     if (from == 0) { // Constructor
241       cout << IdSrc << endl;
242       cout << IdHdr << endl;
243     }
244   }
245 }
246 }