1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Manage output of sim parameters to file or stdout
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU Lesser General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 You should have received a copy of the GNU Lesser General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
25 Further information about the GNU Lesser General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This is the place where you create output routines to dump data for perusal
34 --------------------------------------------------------------------------------
36 11/09/07 HDW Added FlightGear Socket Interface
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 #include "FGFDMExec.h"
45 #include "FGAtmosphere.h"
47 #include "FGAerodynamics.h"
48 #include "FGGroundReactions.h"
49 #include "FGExternalReactions.h"
50 #include "FGBuoyantForces.h"
51 #include "FGAircraft.h"
52 #include "FGMassBalance.h"
53 #include "FGPropagate.h"
54 #include "FGAuxiliary.h"
55 #include "FGInertial.h"
56 #include "FGPropulsion.h" //access to FGEngine, FGTank
57 #include "models/propulsion/FGPiston.h"
62 #include "input_output/net_fdm.hxx"
64 #if defined(WIN32) && !defined(__CYGWIN__)
67 # include <netinet/in.h> // htonl() ntohl()
70 static const int endianTest = 1;
71 #define isLittleEndian (*((char *) &endianTest ) != 0)
75 static const char *IdSrc = "$Id$";
76 static const char *IdHdr = ID_OUTPUT;
78 // (stolen from FGFS native_fdm.cxx)
79 // The function htond is defined this way due to the way some
80 // processors and OSes treat floating point values. Some will raise
81 // an exception whenever a "bad" floating point value is loaded into a
82 // floating point register. Solaris is notorious for this, but then
83 // so is LynxOS on the PowerPC. By translating the data in place,
84 // there is no need to load a FP register with the "corruped" floating
85 // point value. By doing the BIG_ENDIAN test, I can optimize the
86 // routine for big-endian processors so it can be as efficient as
88 static void htond (double &x)
90 if ( isLittleEndian ) {
94 Double_Overlay = (int *) &x;
95 Holding_Buffer = Double_Overlay [0];
97 Double_Overlay [0] = htonl (Double_Overlay [1]);
98 Double_Overlay [1] = htonl (Holding_Buffer);
105 static void htonf (float &x)
107 if ( isLittleEndian ) {
111 Float_Overlay = (int *) &x;
112 Holding_Buffer = Float_Overlay [0];
114 Float_Overlay [0] = htonl (Holding_Buffer);
121 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
125 FGOutput::FGOutput(FGFDMExec* fdmex) : FGModel(fdmex)
128 sFirstPass = dFirstPass = true;
130 flightGearSocket = 0;
135 StartNewFile = false;
137 BaseFilename = Filename = "";
139 output_file_name = "";
141 memset(&fgSockBuf, 0x00, sizeof(fgSockBuf));
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 FGOutput::~FGOutput()
151 delete flightGearSocket;
152 OutputProperties.clear();
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 bool FGOutput::InitModel(void)
160 char fname[1000] = "";
162 if (!FGModel::InitModel()) return false;
164 if (Filename.size() > 0 && StartNewFile) {
165 int idx = BaseFilename.find_last_of(".");
166 int len = BaseFilename.length();
167 string extension = "";
168 if (idx != string::npos) {
169 extension = BaseFilename.substr(idx, len-idx);
170 len -= extension.length();
172 sprintf(fname, "%s_%d%s", BaseFilename.substr(0,len).c_str(), runID_postfix++, extension.c_str());
173 Filename = string(fname);
175 StartNewFile = false;
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 bool FGOutput::Run(void)
186 if (FGModel::Run()) return true;
188 if (enabled && !State->IntegrationSuspended()&& !FDMExec->Holding()) {
189 if (Type == otSocket) {
191 } else if (Type == otFlightGear) {
192 FlightGearSocketOutput();
193 } else if (Type == otCSV || Type == otTab) {
194 DelimitedOutput(Filename);
195 } else if (Type == otTerminal) {
197 } else if (Type == otNone) {
200 // Not a valid type of output
206 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 void FGOutput::SetType(string type)
213 } else if (type == "TABULAR") {
216 } else if (type == "SOCKET") {
218 } else if (type == "FLIGHTGEAR") {
220 } else if (type == "TERMINAL") {
222 } else if (type != string("NONE")) {
224 cerr << "Unknown type of output specified in config file" << endl;
228 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 void FGOutput::DelimitedOutput(string fname)
235 if (fname == "COUT" || fname == "cout") {
236 buffer = cout.rdbuf();
238 if (!datafile.is_open()) datafile.open(fname.c_str());
239 buffer = datafile.rdbuf();
242 ostream outstream(buffer);
244 outstream.precision(10);
248 if (SubSystems & ssSimulation) {
251 if (SubSystems & ssAerosurfaces) {
252 outstream << delimeter;
253 outstream << "Aileron Command (norm)" + delimeter;
254 outstream << "Elevator Command (norm)" + delimeter;
255 outstream << "Rudder Command (norm)" + delimeter;
256 outstream << "Flap Command (norm)" + delimeter;
257 outstream << "Left Aileron Position (deg)" + delimeter;
258 outstream << "Right Aileron Position (deg)" + delimeter;
259 outstream << "Elevator Position (deg)" + delimeter;
260 outstream << "Rudder Position (deg)" + delimeter;
261 outstream << "Flap Position (deg)";
263 if (SubSystems & ssRates) {
264 outstream << delimeter;
265 outstream << "P (deg/s)" + delimeter + "Q (deg/s)" + delimeter + "R (deg/s)" + delimeter;
266 outstream << "P dot (deg/s^2)" + delimeter + "Q dot (deg/s^2)" + delimeter + "R dot (deg/s^2)";
268 if (SubSystems & ssVelocities) {
269 outstream << delimeter;
270 outstream << "q bar (psf)" + delimeter;
271 outstream << "Reynolds Number" + delimeter;
272 outstream << "V_{Total} (ft/s)" + delimeter;
273 outstream << "V_{Inertial} (ft/s)" + delimeter;
274 outstream << "UBody" + delimeter + "VBody" + delimeter + "WBody" + delimeter;
275 outstream << "Aero V_{X Body} (ft/s)" + delimeter + "Aero V_{Y Body} (ft/s)" + delimeter + "Aero V_{Z Body} (ft/s)" + delimeter;
276 outstream << "V_{North} (ft/s)" + delimeter + "V_{East} (ft/s)" + delimeter + "V_{Down} (ft/s)";
278 if (SubSystems & ssForces) {
279 outstream << delimeter;
280 outstream << "F_{Drag} (lbs)" + delimeter + "F_{Side} (lbs)" + delimeter + "F_{Lift} (lbs)" + delimeter;
281 outstream << "L/D" + delimeter;
282 outstream << "F_{Aero x} (lbs)" + delimeter + "F_{Aero y} (lbs)" + delimeter + "F_{Aero z} (lbs)" + delimeter;
283 outstream << "F_{Prop x} (lbs)" + delimeter + "F_{Prop y} (lbs)" + delimeter + "F_{Prop z} (lbs)" + delimeter;
284 outstream << "F_{Gear x} (lbs)" + delimeter + "F_{Gear y} (lbs)" + delimeter + "F_{Gear z} (lbs)" + delimeter;
285 outstream << "F_{Ext x} (lbs)" + delimeter + "F_{Ext y} (lbs)" + delimeter + "F_{Ext z} (lbs)" + delimeter;
286 outstream << "F_{Buoyant x} (lbs)" + delimeter + "F_{Buoyant y} (lbs)" + delimeter + "F_{Buoyant z} (lbs)" + delimeter;
287 outstream << "F_{Total x} (lbs)" + delimeter + "F_{Total y} (lbs)" + delimeter + "F_{Total z} (lbs)";
289 if (SubSystems & ssMoments) {
290 outstream << delimeter;
291 outstream << "L_{Aero} (ft-lbs)" + delimeter + "M_{Aero} ( ft-lbs)" + delimeter + "N_{Aero} (ft-lbs)" + delimeter;
292 outstream << "L_{Prop} (ft-lbs)" + delimeter + "M_{Prop} (ft-lbs)" + delimeter + "N_{Prop} (ft-lbs)" + delimeter;
293 outstream << "L_{Gear} (ft-lbs)" + delimeter + "M_{Gear} (ft-lbs)" + delimeter + "N_{Gear} (ft-lbs)" + delimeter;
294 outstream << "L_{ext} (ft-lbs)" + delimeter + "M_{ext} (ft-lbs)" + delimeter + "N_{ext} (ft-lbs)" + delimeter;
295 outstream << "L_{Buoyant} (ft-lbs)" + delimeter + "M_{Buoyant} (ft-lbs)" + delimeter + "N_{Buoyant} (ft-lbs)" + delimeter;
296 outstream << "L_{Total} (ft-lbs)" + delimeter + "M_{Total} (ft-lbs)" + delimeter + "N_{Total} (ft-lbs)";
298 if (SubSystems & ssAtmosphere) {
299 outstream << delimeter;
300 outstream << "Rho (slugs/ft^3)" + delimeter;
301 outstream << "Absolute Viscosity" + delimeter;
302 outstream << "Kinematic Viscosity" + delimeter;
303 outstream << "Temperature (R)" + delimeter;
304 outstream << "P_{SL} (psf)" + delimeter;
305 outstream << "P_{Ambient} (psf)" + delimeter;
306 outstream << "Turbulence Magnitude (ft/sec)" + delimeter;
307 outstream << "Turbulence X Direction (rad)" + delimeter + "Turbulence Y Direction (rad)" + delimeter + "Turbulence Z Direction (rad)" + delimeter;
308 outstream << "Wind V_{North} (ft/s)" + delimeter + "Wind V_{East} (ft/s)" + delimeter + "Wind V_{Down} (ft/s)";
310 if (SubSystems & ssMassProps) {
311 outstream << delimeter;
312 outstream << "I_{xx}" + delimeter;
313 outstream << "I_{xy}" + delimeter;
314 outstream << "I_{xz}" + delimeter;
315 outstream << "I_{yx}" + delimeter;
316 outstream << "I_{yy}" + delimeter;
317 outstream << "I_{yz}" + delimeter;
318 outstream << "I_{zx}" + delimeter;
319 outstream << "I_{zy}" + delimeter;
320 outstream << "I_{zz}" + delimeter;
321 outstream << "Mass" + delimeter;
322 outstream << "X_{cg}" + delimeter + "Y_{cg}" + delimeter + "Z_{cg}";
324 if (SubSystems & ssPropagate) {
325 outstream << delimeter;
326 outstream << "Altitude ASL (ft)" + delimeter;
327 outstream << "Altitude AGL (ft)" + delimeter;
328 outstream << "Phi (deg)" + delimeter + "Theta (deg)" + delimeter + "Psi (deg)" + delimeter;
329 outstream << "Alpha (deg)" + delimeter;
330 outstream << "Beta (deg)" + delimeter;
331 outstream << "Latitude (deg)" + delimeter;
332 outstream << "Longitude (deg)" + delimeter;
333 outstream << "ECEF X (ft)" + delimeter + "ECEF Y (ft)" + delimeter + "ECEF Z (ft)" + delimeter;
334 outstream << "EPA (deg)" + delimeter;
335 outstream << "Distance AGL (ft)" + delimeter;
336 outstream << "Terrain Radius (ft)";
338 if (SubSystems & ssCoefficients) {
339 scratch = Aerodynamics->GetCoefficientStrings(delimeter);
340 if (scratch.length() != 0) outstream << delimeter << scratch;
342 if (SubSystems & ssFCS) {
343 scratch = FCS->GetComponentStrings(delimeter);
344 if (scratch.length() != 0) outstream << delimeter << scratch;
346 if (SubSystems & ssGroundReactions) {
347 outstream << delimeter;
348 outstream << GroundReactions->GetGroundReactionStrings(delimeter);
350 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
351 outstream << delimeter;
352 outstream << Propulsion->GetPropulsionStrings(delimeter);
354 if (OutputProperties.size() > 0) {
355 for (unsigned int i=0;i<OutputProperties.size();i++) {
356 outstream << delimeter << OutputProperties[i]->GetPrintableName();
364 outstream << State->Getsim_time();
365 if (SubSystems & ssSimulation) {
367 if (SubSystems & ssAerosurfaces) {
368 outstream << delimeter;
369 outstream << FCS->GetDaCmd() << delimeter;
370 outstream << FCS->GetDeCmd() << delimeter;
371 outstream << FCS->GetDrCmd() << delimeter;
372 outstream << FCS->GetDfCmd() << delimeter;
373 outstream << FCS->GetDaLPos(ofDeg) << delimeter;
374 outstream << FCS->GetDaRPos(ofDeg) << delimeter;
375 outstream << FCS->GetDePos(ofDeg) << delimeter;
376 outstream << FCS->GetDrPos(ofDeg) << delimeter;
377 outstream << FCS->GetDfPos(ofDeg);
379 if (SubSystems & ssRates) {
380 outstream << delimeter;
381 outstream << (radtodeg*Propagate->GetPQR()).Dump(delimeter) << delimeter;
382 outstream << (radtodeg*Propagate->GetPQRdot()).Dump(delimeter);
384 if (SubSystems & ssVelocities) {
385 outstream << delimeter;
386 outstream << Auxiliary->Getqbar() << delimeter;
387 outstream << Auxiliary->GetReynoldsNumber() << delimeter;
388 outstream << setprecision(12) << Auxiliary->GetVt() << delimeter;
389 outstream << Propagate->GetInertialVelocityMagnitude() << delimeter;
390 outstream << setprecision(12) << Propagate->GetUVW().Dump(delimeter) << delimeter;
391 outstream << Auxiliary->GetAeroUVW().Dump(delimeter) << delimeter;
392 outstream << Propagate->GetVel().Dump(delimeter);
393 outstream.precision(10);
395 if (SubSystems & ssForces) {
396 outstream << delimeter;
397 outstream << Aerodynamics->GetvFw() << delimeter;
398 outstream << Aerodynamics->GetLoD() << delimeter;
399 outstream << Aerodynamics->GetForces() << delimeter;
400 outstream << Propulsion->GetForces() << delimeter;
401 outstream << GroundReactions->GetForces() << delimeter;
402 outstream << ExternalReactions->GetForces() << delimeter;
403 outstream << BuoyantForces->GetForces() << delimeter;
404 outstream << Aircraft->GetForces().Dump(delimeter);
406 if (SubSystems & ssMoments) {
407 outstream << delimeter;
408 outstream << Aerodynamics->GetMoments() << delimeter;
409 outstream << Propulsion->GetMoments() << delimeter;
410 outstream << GroundReactions->GetMoments() << delimeter;
411 outstream << ExternalReactions->GetMoments() << delimeter;
412 outstream << BuoyantForces->GetMoments() << delimeter;
413 outstream << Aircraft->GetMoments().Dump(delimeter);
415 if (SubSystems & ssAtmosphere) {
416 outstream << delimeter;
417 outstream << Atmosphere->GetDensity() << delimeter;
418 outstream << Atmosphere->GetAbsoluteViscosity() << delimeter;
419 outstream << Atmosphere->GetKinematicViscosity() << delimeter;
420 outstream << Atmosphere->GetTemperature() << delimeter;
421 outstream << Atmosphere->GetPressureSL() << delimeter;
422 outstream << Atmosphere->GetPressure() << delimeter;
423 outstream << Atmosphere->GetTurbMagnitude() << delimeter;
424 outstream << Atmosphere->GetTurbDirection().Dump(delimeter) << delimeter;
425 outstream << Atmosphere->GetTotalWindNED().Dump(delimeter);
427 if (SubSystems & ssMassProps) {
428 outstream << delimeter;
429 outstream << MassBalance->GetJ() << delimeter;
430 outstream << MassBalance->GetMass() << delimeter;
431 outstream << MassBalance->GetXYZcg();
433 if (SubSystems & ssPropagate) {
434 outstream.precision(14);
435 outstream << delimeter;
436 outstream << Propagate->GetAltitudeASL() << delimeter;
437 outstream << Propagate->GetDistanceAGL() << delimeter;
438 outstream << (radtodeg*Propagate->GetEuler()).Dump(delimeter) << delimeter;
439 outstream << Auxiliary->Getalpha(inDegrees) << delimeter;
440 outstream << Auxiliary->Getbeta(inDegrees) << delimeter;
441 outstream << Propagate->GetLocation().GetLatitudeDeg() << delimeter;
442 outstream << Propagate->GetLocation().GetLongitudeDeg() << delimeter;
443 outstream.precision(18);
444 outstream << ((FGColumnVector3)Propagate->GetLocation()).Dump(delimeter) << delimeter;
445 outstream.precision(14);
446 outstream << Inertial->GetEarthPositionAngleDeg() << delimeter;
447 outstream << Propagate->GetDistanceAGL() << delimeter;
448 outstream << Propagate->GetLocalTerrainRadius();
449 outstream.precision(10);
451 if (SubSystems & ssCoefficients) {
452 scratch = Aerodynamics->GetCoefficientValues(delimeter);
453 if (scratch.length() != 0) outstream << delimeter << scratch;
455 if (SubSystems & ssFCS) {
456 scratch = FCS->GetComponentValues(delimeter);
457 if (scratch.length() != 0) outstream << delimeter << scratch;
459 if (SubSystems & ssGroundReactions) {
460 outstream << delimeter;
461 outstream << GroundReactions->GetGroundReactionValues(delimeter);
463 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
464 outstream << delimeter;
465 outstream << Propulsion->GetPropulsionValues(delimeter);
468 for (unsigned int i=0;i<OutputProperties.size();i++) {
469 outstream << delimeter << OutputProperties[i]->getDoubleValue();
476 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 void FGOutput::SocketDataFill(FGNetFDM* net)
483 net->version = FG_NET_FDM_VERSION;
486 net->longitude = Propagate->GetLocation().GetLongitude(); // geodetic (radians)
487 net->latitude = Propagate->GetLocation().GetLatitude(); // geodetic (radians)
488 net->altitude = Propagate->GetAltitudeASL()*0.3048; // altitude, above sea level (meters)
489 net->agl = (float)(Propagate->GetDistanceAGL()*0.3048); // altitude, above ground level (meters)
491 net->phi = (float)(Propagate->GetEuler(ePhi)); // roll (radians)
492 net->theta = (float)(Propagate->GetEuler(eTht)); // pitch (radians)
493 net->psi = (float)(Propagate->GetEuler(ePsi)); // yaw or true heading (radians)
495 net->alpha = (float)(Auxiliary->Getalpha()); // angle of attack (radians)
496 net->beta = (float)(Auxiliary->Getbeta()); // side slip angle (radians)
499 net->phidot = (float)(Auxiliary->GetEulerRates(ePhi)); // roll rate (radians/sec)
500 net->thetadot = (float)(Auxiliary->GetEulerRates(eTht)); // pitch rate (radians/sec)
501 net->psidot = (float)(Auxiliary->GetEulerRates(ePsi)); // yaw rate (radians/sec)
502 net->vcas = (float)(Auxiliary->GetVcalibratedFPS()); // VCAS, ft/sec
503 net->climb_rate = (float)(Propagate->Gethdot()); // altitude rate, ft/sec
504 net->v_north = (float)(Propagate->GetVel(eNorth)); // north vel in NED frame, fps
505 net->v_east = (float)(Propagate->GetVel(eEast)); // east vel in NED frame, fps
506 net->v_down = (float)(Propagate->GetVel(eDown)); // down vel in NED frame, fps
507 //---ADD METHOD TO CALCULATE THESE TERMS---
508 net->v_wind_body_north = (float)(Propagate->GetVel(eNorth)); // north vel in NED relative to airmass, fps
509 net->v_wind_body_east = (float)(Propagate->GetVel(eEast)); // east vel in NED relative to airmass, fps
510 net->v_wind_body_down = (float)(Propagate->GetVel(eDown)); // down vel in NED relative to airmass, fps
513 net->A_X_pilot = (float)(Auxiliary->GetPilotAccel(1)); // X body accel, ft/s/s
514 net->A_Y_pilot = (float)(Auxiliary->GetPilotAccel(2)); // Y body accel, ft/s/s
515 net->A_Z_pilot = (float)(Auxiliary->GetPilotAccel(3)); // Z body accel, ft/s/s
518 net->stall_warning = 0.0; // 0.0 - 1.0 indicating the amount of stall
519 net->slip_deg = (float)(Auxiliary->Getbeta(inDegrees)); // slip ball deflection, deg
522 net->num_engines = Propulsion->GetNumEngines(); // Number of valid engines
524 for (i=0; i<net->num_engines; i++) {
525 if (Propulsion->GetEngine(i)->GetRunning())
526 net->eng_state[i] = 2; // Engine state running
527 else if (Propulsion->GetEngine(i)->GetCranking())
528 net->eng_state[i] = 1; // Engine state cranking
530 net->eng_state[i] = 0; // Engine state off
532 switch (Propulsion->GetEngine(i)->GetType()) {
533 case (FGEngine::etRocket):
535 case (FGEngine::etPiston):
536 net->rpm[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getRPM());
537 net->fuel_flow[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getFuelFlow_gph());
538 net->fuel_px[i] = 0; // Fuel pressure, psi (N/A in current model)
539 net->egt[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->GetEGT());
540 net->cht[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getCylinderHeadTemp_degF());
541 net->mp_osi[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getManifoldPressure_inHg());
542 net->oil_temp[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getOilTemp_degF());
543 net->oil_px[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getOilPressure_psi());
544 net->tit[i] = 0; // Turbine Inlet Temperature (N/A for piston)
546 case (FGEngine::etTurbine):
548 case (FGEngine::etTurboprop):
550 case (FGEngine::etElectric):
552 case (FGEngine::etUnknown):
559 net->num_tanks = Propulsion->GetNumTanks(); // Max number of fuel tanks
561 for (i=0; i<net->num_tanks; i++) {
562 net->fuel_quantity[i] = (float)(((FGTank *)Propulsion->GetTank(i))->GetContents());
567 net->num_wheels = GroundReactions->GetNumGearUnits();
569 for (i=0; i<net->num_wheels; i++) {
570 net->wow[i] = GroundReactions->GetGearUnit(i)->GetWOW();
571 if (GroundReactions->GetGearUnit(i)->GetGearUnitDown())
572 net->gear_pos[i] = 1; //gear down, using FCS convention
574 net->gear_pos[i] = 0; //gear up, using FCS convention
575 net->gear_steer[i] = (float)(GroundReactions->GetGearUnit(i)->GetSteerNorm());
576 net->gear_compression[i] = (float)(GroundReactions->GetGearUnit(i)->GetCompLen());
581 net->cur_time = (long int)1234567890; // Friday, Feb 13, 2009, 23:31:30 UTC (not processed by FGFS anyway)
582 net->warp = 0; // offset in seconds to unix time
583 net->visibility = 25000.0; // visibility in meters (for env. effects)
586 // Control surface positions (normalized values)
587 net->elevator = (float)(FCS->GetDePos(ofNorm)); // Norm Elevator Pos, --
588 net->elevator_trim_tab = (float)(FCS->GetPitchTrimCmd()); // Norm Elev Trim Tab Pos, --
589 net->left_flap = (float)(FCS->GetDfPos(ofNorm)); // Norm Flap Pos, --
590 net->right_flap = (float)(FCS->GetDfPos(ofNorm)); // Norm Flap Pos, --
591 net->left_aileron = (float)(FCS->GetDaLPos(ofNorm)); // Norm L Aileron Pos, --
592 net->right_aileron = (float)(FCS->GetDaRPos(ofNorm)); // Norm R Aileron Pos, --
593 net->rudder = (float)(FCS->GetDrPos(ofNorm)); // Norm Rudder Pos, --
594 net->nose_wheel = (float)(FCS->GetDrPos(ofNorm)); // *** FIX *** Using Rudder Pos for NWS, --
595 net->speedbrake = (float)(FCS->GetDsbPos(ofNorm)); // Norm Speedbrake Pos, --
596 net->spoilers = (float)(FCS->GetDspPos(ofNorm)); // Norm Spoiler Pos, --
599 // Convert the net buffer to network format
600 if ( isLittleEndian ) {
601 net->version = htonl(net->version);
603 htond(net->longitude);
604 htond(net->latitude);
605 htond(net->altitude);
614 htonf(net->thetadot);
617 htonf(net->climb_rate);
621 htonf(net->v_wind_body_north);
622 htonf(net->v_wind_body_east);
623 htonf(net->v_wind_body_down);
625 htonf(net->A_X_pilot);
626 htonf(net->A_Y_pilot);
627 htonf(net->A_Z_pilot);
629 htonf(net->stall_warning);
630 htonf(net->slip_deg);
632 for (i=0; i<net->num_engines; ++i ) {
633 net->eng_state[i] = htonl(net->eng_state[i]);
635 htonf(net->fuel_flow[i]);
636 htonf(net->fuel_px[i]);
639 htonf(net->mp_osi[i]);
641 htonf(net->oil_temp[i]);
642 htonf(net->oil_px[i]);
644 net->num_engines = htonl(net->num_engines);
646 for (i=0; i<net->num_tanks; ++i ) {
647 htonf(net->fuel_quantity[i]);
649 net->num_tanks = htonl(net->num_tanks);
651 for (i=0; i<net->num_wheels; ++i ) {
652 net->wow[i] = htonl(net->wow[i]);
653 htonf(net->gear_pos[i]);
654 htonf(net->gear_steer[i]);
655 htonf(net->gear_compression[i]);
657 net->num_wheels = htonl(net->num_wheels);
659 net->cur_time = htonl( net->cur_time );
660 net->warp = htonl( net->warp );
661 htonf(net->visibility);
663 htonf(net->elevator);
664 htonf(net->elevator_trim_tab);
665 htonf(net->left_flap);
666 htonf(net->right_flap);
667 htonf(net->left_aileron);
668 htonf(net->right_aileron);
670 htonf(net->nose_wheel);
671 htonf(net->speedbrake);
672 htonf(net->spoilers);
676 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678 void FGOutput::FlightGearSocketOutput(void)
680 int length = sizeof(fgSockBuf);
683 if (flightGearSocket == NULL) return;
684 if (!flightGearSocket->GetConnectStatus()) return;
686 SocketDataFill(&fgSockBuf);
687 flightGearSocket->Send((char *)&fgSockBuf, length);
690 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
692 void FGOutput::SocketOutput(void)
694 string asciiData, scratch;
696 if (socket == NULL) return;
697 if (!socket->GetConnectStatus()) return;
701 socket->Clear("<LABELS>");
702 socket->Append("Time");
704 if (SubSystems & ssAerosurfaces) {
705 socket->Append("Aileron Command");
706 socket->Append("Elevator Command");
707 socket->Append("Rudder Command");
708 socket->Append("Flap Command");
709 socket->Append("Left Aileron Position");
710 socket->Append("Right Aileron Position");
711 socket->Append("Elevator Position");
712 socket->Append("Rudder Position");
713 socket->Append("Flap Position");
716 if (SubSystems & ssRates) {
720 socket->Append("PDot");
721 socket->Append("QDot");
722 socket->Append("RDot");
725 if (SubSystems & ssVelocities) {
726 socket->Append("QBar");
727 socket->Append("Vtotal");
728 socket->Append("UBody");
729 socket->Append("VBody");
730 socket->Append("WBody");
731 socket->Append("UAero");
732 socket->Append("VAero");
733 socket->Append("WAero");
734 socket->Append("Vn");
735 socket->Append("Ve");
736 socket->Append("Vd");
738 if (SubSystems & ssForces) {
739 socket->Append("F_Drag");
740 socket->Append("F_Side");
741 socket->Append("F_Lift");
742 socket->Append("LoD");
743 socket->Append("Fx");
744 socket->Append("Fy");
745 socket->Append("Fz");
747 if (SubSystems & ssMoments) {
752 if (SubSystems & ssAtmosphere) {
753 socket->Append("Rho");
754 socket->Append("SL pressure");
755 socket->Append("Ambient pressure");
756 socket->Append("Turbulence Magnitude");
757 socket->Append("Turbulence Direction X");
758 socket->Append("Turbulence Direction Y");
759 socket->Append("Turbulence Direction Z");
760 socket->Append("NWind");
761 socket->Append("EWind");
762 socket->Append("DWind");
764 if (SubSystems & ssMassProps) {
765 socket->Append("Ixx");
766 socket->Append("Ixy");
767 socket->Append("Ixz");
768 socket->Append("Iyx");
769 socket->Append("Iyy");
770 socket->Append("Iyz");
771 socket->Append("Izx");
772 socket->Append("Izy");
773 socket->Append("Izz");
774 socket->Append("Mass");
775 socket->Append("Xcg");
776 socket->Append("Ycg");
777 socket->Append("Zcg");
779 if (SubSystems & ssPropagate) {
780 socket->Append("Altitude");
781 socket->Append("Phi (deg)");
782 socket->Append("Tht (deg)");
783 socket->Append("Psi (deg)");
784 socket->Append("Alpha (deg)");
785 socket->Append("Beta (deg)");
786 socket->Append("Latitude (deg)");
787 socket->Append("Longitude (deg)");
789 if (SubSystems & ssCoefficients) {
790 scratch = Aerodynamics->GetCoefficientStrings(",");
791 if (scratch.length() != 0) socket->Append(scratch);
793 if (SubSystems & ssFCS) {
794 scratch = FCS->GetComponentStrings(",");
795 if (scratch.length() != 0) socket->Append(scratch);
797 if (SubSystems & ssGroundReactions) {
798 socket->Append(GroundReactions->GetGroundReactionStrings(","));
800 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
801 socket->Append(Propulsion->GetPropulsionStrings(","));
803 if (OutputProperties.size() > 0) {
804 for (unsigned int i=0;i<OutputProperties.size();i++) {
805 socket->Append(OutputProperties[i]->GetPrintableName());
814 socket->Append(State->Getsim_time());
816 if (SubSystems & ssAerosurfaces) {
817 socket->Append(FCS->GetDaCmd());
818 socket->Append(FCS->GetDeCmd());
819 socket->Append(FCS->GetDrCmd());
820 socket->Append(FCS->GetDfCmd());
821 socket->Append(FCS->GetDaLPos());
822 socket->Append(FCS->GetDaRPos());
823 socket->Append(FCS->GetDePos());
824 socket->Append(FCS->GetDrPos());
825 socket->Append(FCS->GetDfPos());
827 if (SubSystems & ssRates) {
828 socket->Append(radtodeg*Propagate->GetPQR(eP));
829 socket->Append(radtodeg*Propagate->GetPQR(eQ));
830 socket->Append(radtodeg*Propagate->GetPQR(eR));
831 socket->Append(radtodeg*Propagate->GetPQRdot(eP));
832 socket->Append(radtodeg*Propagate->GetPQRdot(eQ));
833 socket->Append(radtodeg*Propagate->GetPQRdot(eR));
835 if (SubSystems & ssVelocities) {
836 socket->Append(Auxiliary->Getqbar());
837 socket->Append(Auxiliary->GetVt());
838 socket->Append(Propagate->GetUVW(eU));
839 socket->Append(Propagate->GetUVW(eV));
840 socket->Append(Propagate->GetUVW(eW));
841 socket->Append(Auxiliary->GetAeroUVW(eU));
842 socket->Append(Auxiliary->GetAeroUVW(eV));
843 socket->Append(Auxiliary->GetAeroUVW(eW));
844 socket->Append(Propagate->GetVel(eNorth));
845 socket->Append(Propagate->GetVel(eEast));
846 socket->Append(Propagate->GetVel(eDown));
848 if (SubSystems & ssForces) {
849 socket->Append(Aerodynamics->GetvFw()(eDrag));
850 socket->Append(Aerodynamics->GetvFw()(eSide));
851 socket->Append(Aerodynamics->GetvFw()(eLift));
852 socket->Append(Aerodynamics->GetLoD());
853 socket->Append(Aircraft->GetForces(eX));
854 socket->Append(Aircraft->GetForces(eY));
855 socket->Append(Aircraft->GetForces(eZ));
857 if (SubSystems & ssMoments) {
858 socket->Append(Aircraft->GetMoments(eL));
859 socket->Append(Aircraft->GetMoments(eM));
860 socket->Append(Aircraft->GetMoments(eN));
862 if (SubSystems & ssAtmosphere) {
863 socket->Append(Atmosphere->GetDensity());
864 socket->Append(Atmosphere->GetPressureSL());
865 socket->Append(Atmosphere->GetPressure());
866 socket->Append(Atmosphere->GetTurbMagnitude());
867 socket->Append(Atmosphere->GetTurbDirection().Dump(","));
868 socket->Append(Atmosphere->GetTotalWindNED().Dump(","));
870 if (SubSystems & ssMassProps) {
871 socket->Append(MassBalance->GetJ()(1,1));
872 socket->Append(MassBalance->GetJ()(1,2));
873 socket->Append(MassBalance->GetJ()(1,3));
874 socket->Append(MassBalance->GetJ()(2,1));
875 socket->Append(MassBalance->GetJ()(2,2));
876 socket->Append(MassBalance->GetJ()(2,3));
877 socket->Append(MassBalance->GetJ()(3,1));
878 socket->Append(MassBalance->GetJ()(3,2));
879 socket->Append(MassBalance->GetJ()(3,3));
880 socket->Append(MassBalance->GetMass());
881 socket->Append(MassBalance->GetXYZcg()(eX));
882 socket->Append(MassBalance->GetXYZcg()(eY));
883 socket->Append(MassBalance->GetXYZcg()(eZ));
885 if (SubSystems & ssPropagate) {
886 socket->Append(Propagate->GetAltitudeASL());
887 socket->Append(radtodeg*Propagate->GetEuler(ePhi));
888 socket->Append(radtodeg*Propagate->GetEuler(eTht));
889 socket->Append(radtodeg*Propagate->GetEuler(ePsi));
890 socket->Append(Auxiliary->Getalpha(inDegrees));
891 socket->Append(Auxiliary->Getbeta(inDegrees));
892 socket->Append(Propagate->GetLocation().GetLatitudeDeg());
893 socket->Append(Propagate->GetLocation().GetLongitudeDeg());
895 if (SubSystems & ssCoefficients) {
896 scratch = Aerodynamics->GetCoefficientValues(",");
897 if (scratch.length() != 0) socket->Append(scratch);
899 if (SubSystems & ssFCS) {
900 scratch = FCS->GetComponentValues(",");
901 if (scratch.length() != 0) socket->Append(scratch);
903 if (SubSystems & ssGroundReactions) {
904 socket->Append(GroundReactions->GetGroundReactionValues(","));
906 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
907 socket->Append(Propulsion->GetPropulsionValues(","));
910 for (unsigned int i=0;i<OutputProperties.size();i++) {
911 socket->Append(OutputProperties[i]->getDoubleValue());
917 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 void FGOutput::SocketStatusOutput(string out_str)
923 if (socket == NULL) return;
926 asciiData = string("<STATUS>") + out_str;
927 socket->Append(asciiData.c_str());
931 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933 bool FGOutput::Load(Element* element)
935 string type="", parameter="";
937 string protocol="tcp";
941 Element *property_element;
943 string separator = "/";
945 if (!DirectivesFile.empty()) { // A directives filename from the command line overrides
946 output_file_name = DirectivesFile; // one found in the config file.
947 document = LoadXMLDocument(output_file_name);
948 } else if (!element->GetAttributeValue("file").empty()) {
949 output_file_name = element->GetAttributeValue("file");
950 document = LoadXMLDocument(output_file_name);
955 name = document->GetAttributeValue("name");
956 type = document->GetAttributeValue("type");
958 if (!document->GetAttributeValue("port").empty() && type == string("SOCKET")) {
959 port = atoi(document->GetAttributeValue("port").c_str());
960 socket = new FGfdmSocket(name, port);
961 } else if (!document->GetAttributeValue("port").empty() && type == string("FLIGHTGEAR")) {
962 port = atoi(document->GetAttributeValue("port").c_str());
963 if (!document->GetAttributeValue("protocol").empty())
964 protocol = document->GetAttributeValue("protocol");
965 if (protocol == "udp")
966 flightGearSocket = new FGfdmSocket(name, port, FGfdmSocket::ptUDP); // create udp socket
968 flightGearSocket = new FGfdmSocket(name, port, FGfdmSocket::ptTCP); // create tcp socket (default)
970 BaseFilename = Filename = name;
972 if (!document->GetAttributeValue("rate").empty()) {
973 OutRate = (int)document->GetAttributeValueAsNumber("rate");
978 if (document->FindElementValue("simulation") == string("ON"))
979 SubSystems += ssSimulation;
980 if (document->FindElementValue("aerosurfaces") == string("ON"))
981 SubSystems += ssAerosurfaces;
982 if (document->FindElementValue("rates") == string("ON"))
983 SubSystems += ssRates;
984 if (document->FindElementValue("velocities") == string("ON"))
985 SubSystems += ssVelocities;
986 if (document->FindElementValue("forces") == string("ON"))
987 SubSystems += ssForces;
988 if (document->FindElementValue("moments") == string("ON"))
989 SubSystems += ssMoments;
990 if (document->FindElementValue("atmosphere") == string("ON"))
991 SubSystems += ssAtmosphere;
992 if (document->FindElementValue("massprops") == string("ON"))
993 SubSystems += ssMassProps;
994 if (document->FindElementValue("position") == string("ON"))
995 SubSystems += ssPropagate;
996 if (document->FindElementValue("coefficients") == string("ON"))
997 SubSystems += ssCoefficients;
998 if (document->FindElementValue("ground_reactions") == string("ON"))
999 SubSystems += ssGroundReactions;
1000 if (document->FindElementValue("fcs") == string("ON"))
1001 SubSystems += ssFCS;
1002 if (document->FindElementValue("propulsion") == string("ON"))
1003 SubSystems += ssPropulsion;
1004 property_element = document->FindElement("property");
1005 while (property_element) {
1006 string property_str = property_element->GetDataLine();
1007 FGPropertyManager* node = PropertyManager->GetNode(property_str);
1009 cerr << fgred << highint << endl << " No property by the name "
1010 << property_str << " has been defined. This property will " << endl
1011 << " not be logged. You should check your configuration file."
1014 OutputProperties.push_back(node);
1016 property_element = document->FindNextElement("property");
1019 OutRate = OutRate>1000?1000:(OutRate<0?0:OutRate);
1020 rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
1027 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1028 // The bitmasked value choices are as follows:
1029 // unset: In this case (the default) JSBSim would only print
1030 // out the normally expected messages, essentially echoing
1031 // the config files as they are read. If the environment
1032 // variable is not set, debug_lvl is set to 1 internally
1033 // 0: This requests JSBSim not to output any messages
1035 // 1: This value explicity requests the normal JSBSim
1037 // 2: This value asks for a message to be printed out when
1038 // a class is instantiated
1039 // 4: When this value is set, a message is displayed when a
1040 // FGModel object executes its Run() method
1041 // 8: When this value is set, various runtime state variables
1042 // are printed out periodically
1043 // 16: When set various parameters are sanity checked and
1044 // a message is printed out when they go out of bounds
1046 void FGOutput::Debug(int from)
1050 if (debug_lvl <= 0) return;
1052 if (debug_lvl & 1) { // Standard console startup message output
1053 if (from == 0) { // Constructor
1057 if (output_file_name.empty())
1058 cout << " " << "Output parameters read inline" << endl;
1060 cout << " Output parameters read from file: " << output_file_name << endl;
1062 if (Filename == "cout" || Filename == "COUT") {
1063 scratch = " Log output goes to screen console";
1064 } else if (!Filename.empty()) {
1065 scratch = " Log output goes to file: " + Filename;
1069 cout << scratch << " in CSV format output at rate " << 1/(State->Getdt()*rate) << " Hz" << endl;
1072 cout << " No log output" << endl;
1076 if (SubSystems & ssSimulation) cout << " Simulation parameters logged" << endl;
1077 if (SubSystems & ssAerosurfaces) cout << " Aerosurface parameters logged" << endl;
1078 if (SubSystems & ssRates) cout << " Rate parameters logged" << endl;
1079 if (SubSystems & ssVelocities) cout << " Velocity parameters logged" << endl;
1080 if (SubSystems & ssForces) cout << " Force parameters logged" << endl;
1081 if (SubSystems & ssMoments) cout << " Moments parameters logged" << endl;
1082 if (SubSystems & ssAtmosphere) cout << " Atmosphere parameters logged" << endl;
1083 if (SubSystems & ssMassProps) cout << " Mass parameters logged" << endl;
1084 if (SubSystems & ssCoefficients) cout << " Coefficient parameters logged" << endl;
1085 if (SubSystems & ssPropagate) cout << " Propagate parameters logged" << endl;
1086 if (SubSystems & ssGroundReactions) cout << " Ground parameters logged" << endl;
1087 if (SubSystems & ssFCS) cout << " FCS parameters logged" << endl;
1088 if (SubSystems & ssPropulsion) cout << " Propulsion parameters logged" << endl;
1089 if (OutputProperties.size() > 0) cout << " Properties logged:" << endl;
1090 for (unsigned int i=0;i<OutputProperties.size();i++) {
1091 cout << " - " << OutputProperties[i]->GetName() << endl;
1095 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1096 if (from == 0) cout << "Instantiated: FGOutput" << endl;
1097 if (from == 1) cout << "Destroyed: FGOutput" << endl;
1099 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1101 if (debug_lvl & 8 ) { // Runtime state variables
1103 if (debug_lvl & 16) { // Sanity checking
1105 if (debug_lvl & 64) {
1106 if (from == 0) { // Constructor
1107 cout << IdSrc << endl;
1108 cout << IdHdr << endl;