1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Called by: FGFDMExec and accessed by all models.
8 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
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 General Public License for more
20 You should have received a copy of the GNU 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.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
32 --------------------------------------------------------------------------------
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 # include <simgear/compiler.h>
43 # if defined(sgi) && !defined(__GNUC__)
51 //#define snprintf _snprintf
58 static const char *IdSrc = "$Id$";
59 static const char *IdHdr = ID_STATE;
61 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
69 FGState::FGState(FGFDMExec* fdex)
76 Aircraft = FDMExec->GetAircraft();
77 Propagate = FDMExec->GetPropagate();
78 Auxiliary = FDMExec->GetAuxiliary();
79 FCS = FDMExec->GetFCS();
80 Atmosphere = FDMExec->GetAtmosphere();
81 Aerodynamics = FDMExec->GetAerodynamics();
82 GroundReactions = FDMExec->GetGroundReactions();
83 Propulsion = FDMExec->GetPropulsion();
84 PropertyManager = FDMExec->GetPropertyManager();
91 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 void FGState::Initialize(FGInitialCondition *FGIC)
105 Propagate->SetInitialState( FGIC );
108 Atmosphere->SetWindNED( FGIC->GetWindNFpsIC(),
109 FGIC->GetWindEFpsIC(),
110 FGIC->GetWindDFpsIC() );
112 FGColumnVector3 vAeroUVW;
113 vAeroUVW = Propagate->GetUVW() + Propagate->GetTl2b()*Atmosphere->GetWindNED();
116 if (vAeroUVW(eW) != 0.0)
117 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
120 if (vAeroUVW(eV) != 0.0)
121 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;
125 Auxiliary->SetAB(alpha, beta);
127 double Vt = vAeroUVW.Magnitude();
128 Auxiliary->SetVt(Vt);
130 Auxiliary->SetMach(Vt/Atmosphere->GetSoundSpeed());
132 double qbar = 0.5*Vt*Vt*Atmosphere->GetDensity();
133 Auxiliary->Setqbar(qbar);
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 FGMatrix33& FGState::GetTs2b(void)
140 double ca, cb, sa, sb;
142 double alpha = Auxiliary->Getalpha();
143 double beta = Auxiliary->Getbeta();
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 FGMatrix33& FGState::GetTb2s(void)
168 double ca, cb, sa, sb;
170 alpha = Auxiliary->Getalpha();
171 beta = Auxiliary->Getbeta();
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 void FGState::ReportState(void)
196 #if !defined(__BORLANDCPP__)
197 char out[80], flap[10], gear[12];
199 cout << endl << " JSBSim State" << endl;
200 snprintf(out,80," Weight: %7.0f lbs. CG: %5.1f, %5.1f, %5.1f inches\n",
201 FDMExec->GetMassBalance()->GetWeight(),
202 FDMExec->GetMassBalance()->GetXYZcg(1),
203 FDMExec->GetMassBalance()->GetXYZcg(2),
204 FDMExec->GetMassBalance()->GetXYZcg(3));
206 if ( FCS->GetDfPos() <= 0.01)
207 snprintf(flap,10,"Up");
209 snprintf(flap,10,"%2.0f",FCS->GetDfPos());
211 if (FCS->GetGearPos() < 0.01)
212 snprintf(gear,12,"Up");
213 else if (FCS->GetGearPos() > 0.99)
214 snprintf(gear,12,"Down");
216 snprintf(gear,12,"In Transit");
218 snprintf(out,80, " Flaps: %3s Gear: %12s\n",flap,gear);
220 snprintf(out,80, " Speed: %4.0f KCAS Mach: %5.2f\n",
221 Auxiliary->GetVcalibratedKTS(),
222 Auxiliary->GetMach() );
224 snprintf(out,80, " Altitude: %7.0f ft. AGL Altitude: %7.0f ft.\n",
226 Propagate->GetDistanceAGL() );
228 snprintf(out,80, " Angle of Attack: %6.2f deg Pitch Angle: %6.2f deg\n",
229 Auxiliary->Getalpha()*radtodeg,
230 Propagate->Gettht()*radtodeg );
232 snprintf(out,80, " Flight Path Angle: %6.2f deg Climb Rate: %5.0f ft/min\n",
233 Auxiliary->GetGamma()*radtodeg,
234 Propagate->Gethdot()*60 );
236 snprintf(out,80, " Normal Load Factor: %4.2f g's Pitch Rate: %5.2f deg/s\n",
238 Propagate->GetPQR(2)*radtodeg );
240 snprintf(out,80, " Heading: %3.0f deg true Sideslip: %5.2f deg Yaw Rate: %5.2f deg/s\n",
241 Propagate->Getpsi()*radtodeg,
242 Auxiliary->Getbeta()*radtodeg,
243 Propagate->GetPQR(3)*radtodeg );
245 snprintf(out,80, " Bank Angle: %5.2f deg Roll Rate: %5.2f deg/s\n",
246 Propagate->Getphi()*radtodeg,
247 Propagate->GetPQR(1)*radtodeg );
249 snprintf(out,80, " Elevator: %5.2f deg Left Aileron: %5.2f deg Rudder: %5.2f deg\n",
250 FCS->GetDePos(ofRad)*radtodeg,
251 FCS->GetDaLPos(ofRad)*radtodeg,
252 FCS->GetDrPos(ofRad)*radtodeg );
254 snprintf(out,80, " Throttle: %5.2f%c\n",
255 FCS->GetThrottlePos(0)*100,'%' );
258 snprintf(out,80, " Wind Components: %5.2f kts head wind, %5.2f kts cross wind\n",
259 Auxiliary->GetHeadWind()*fpstokts,
260 Auxiliary->GetCrossWind()*fpstokts );
263 snprintf(out,80, " Ground Speed: %4.0f knots , Ground Track: %3.0f deg true\n",
264 Auxiliary->GetVground()*fpstokts,
265 Auxiliary->GetGroundTrack()*radtodeg );
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 void FGState::bind(void)
275 PropertyManager->Tie("sim-time-sec", this, &FGState::Getsim_time);
278 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 void FGState::unbind(void)
282 PropertyManager->Untie("sim-time-sec");
285 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 // The bitmasked value choices are as follows:
287 // unset: In this case (the default) JSBSim would only print
288 // out the normally expected messages, essentially echoing
289 // the config files as they are read. If the environment
290 // variable is not set, debug_lvl is set to 1 internally
291 // 0: This requests JSBSim not to output any messages
293 // 1: This value explicity requests the normal JSBSim
295 // 2: This value asks for a message to be printed out when
296 // a class is instantiated
297 // 4: When this value is set, a message is displayed when a
298 // FGModel object executes its Run() method
299 // 8: When this value is set, various runtime state variables
300 // are printed out periodically
301 // 16: When set various parameters are sanity checked and
302 // a message is printed out when they go out of bounds
304 void FGState::Debug(int from)
306 if (debug_lvl <= 0) return;
308 if (debug_lvl & 1) { // Standard console startup message output
309 if (from == 0) { // Constructor
313 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
314 if (from == 0) cout << "Instantiated: FGState" << endl;
315 if (from == 1) cout << "Destroyed: FGState" << endl;
317 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
319 if (debug_lvl & 8 ) { // Runtime state variables
321 if (debug_lvl & 16) { // Sanity checking
323 if (debug_lvl & 64) {
324 if (from == 0) { // Constructor
325 cout << IdSrc << endl;
326 cout << IdHdr << endl;