]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.cpp
Merge branch 'work4' into next
[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 (jon@jsbsim.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 #include <cmath>
40 #include <iostream>
41
42 #include "FGState.h"
43
44 using namespace std;
45
46 namespace JSBSim {
47
48 static const char *IdSrc = "$Id: FGState.cpp,v 1.15 2009/10/24 22:59:30 jberndt Exp $";
49 static const char *IdHdr = ID_STATE;
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 MACROS
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 CLASS IMPLEMENTATION
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58
59 FGState::FGState(FGFDMExec* fdex)
60 {
61   FDMExec = fdex;
62
63   sim_time = 0.0;
64   dt = 1.0/120.0; // a default timestep size. This is needed for when JSBSim is
65                   // run in standalone mode with no initialization file.
66
67   Aircraft     = FDMExec->GetAircraft();
68   Propagate    = FDMExec->GetPropagate();
69   Auxiliary    = FDMExec->GetAuxiliary();
70   FCS          = FDMExec->GetFCS();
71   Atmosphere   = FDMExec->GetAtmosphere();
72   Aerodynamics = FDMExec->GetAerodynamics();
73   GroundReactions = FDMExec->GetGroundReactions();
74   Propulsion      = FDMExec->GetPropulsion();
75   PropertyManager = FDMExec->GetPropertyManager();
76
77   bind();
78
79   Debug(0);
80 }
81
82 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83
84 FGState::~FGState()
85 {
86   Debug(1);
87 }
88
89 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90
91 void FGState::Initialize(FGInitialCondition *FGIC)
92 {
93   sim_time = 0.0;
94
95   Propagate->SetInitialState( FGIC );
96
97   Atmosphere->Run();
98   Atmosphere->SetWindNED( FGIC->GetWindNFpsIC(),
99                           FGIC->GetWindEFpsIC(),
100                           FGIC->GetWindDFpsIC() );
101
102   FGColumnVector3 vAeroUVW;
103   vAeroUVW = Propagate->GetUVW() + Propagate->GetTl2b()*Atmosphere->GetTotalWindNED();
104
105   double alpha, beta;
106   if (vAeroUVW(eW) != 0.0)
107     alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
108   else
109     alpha = 0.0;
110   if (vAeroUVW(eV) != 0.0)
111     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;
112   else
113     beta = 0.0;
114
115   Auxiliary->SetAB(alpha, beta);
116
117   double Vt = vAeroUVW.Magnitude();
118   Auxiliary->SetVt(Vt);
119
120   Auxiliary->SetMach(Vt/Atmosphere->GetSoundSpeed());
121
122   double qbar = 0.5*Vt*Vt*Atmosphere->GetDensity();
123   Auxiliary->Setqbar(qbar);
124 }
125
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128 void FGState::bind(void)
129 {
130   PropertyManager->Tie("sim-time-sec", this, &FGState::Getsim_time);
131 }
132
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 //    The bitmasked value choices are as follows:
135 //    unset: In this case (the default) JSBSim would only print
136 //       out the normally expected messages, essentially echoing
137 //       the config files as they are read. If the environment
138 //       variable is not set, debug_lvl is set to 1 internally
139 //    0: This requests JSBSim not to output any messages
140 //       whatsoever.
141 //    1: This value explicity requests the normal JSBSim
142 //       startup messages
143 //    2: This value asks for a message to be printed out when
144 //       a class is instantiated
145 //    4: When this value is set, a message is displayed when a
146 //       FGModel object executes its Run() method
147 //    8: When this value is set, various runtime state variables
148 //       are printed out periodically
149 //    16: When set various parameters are sanity checked and
150 //       a message is printed out when they go out of bounds
151
152 void FGState::Debug(int from)
153 {
154   if (debug_lvl <= 0) return;
155
156   if (debug_lvl & 1) { // Standard console startup message output
157     if (from == 0) { // Constructor
158
159     }
160   }
161   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
162     if (from == 0) cout << "Instantiated: FGState" << endl;
163     if (from == 1) cout << "Destroyed:    FGState" << endl;
164   }
165   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
166   }
167   if (debug_lvl & 8 ) { // Runtime state variables
168   }
169   if (debug_lvl & 16) { // Sanity checking
170   }
171   if (debug_lvl & 64) {
172     if (from == 0) { // Constructor
173       cout << IdSrc << endl;
174       cout << IdHdr << endl;
175     }
176   }
177 }
178 }