X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FFDM%2FJSBSim%2Fmodels%2FFGOutput.cpp;h=ab70cb2d2de6098afa0957099c56824fc0e6ef49;hb=cd24f7b6aaaa54f81655fb574bb050620c900dfe;hp=bc5de4f6b22f8787bf8fb89ea9990d635dcce822;hpb=32e9505eedffe7f98de028513de8a809a366bd98;p=flightgear.git diff --git a/src/FDM/JSBSim/models/FGOutput.cpp b/src/FDM/JSBSim/models/FGOutput.cpp index bc5de4f6b..ab70cb2d2 100644 --- a/src/FDM/JSBSim/models/FGOutput.cpp +++ b/src/FDM/JSBSim/models/FGOutput.cpp @@ -6,7 +6,7 @@ Purpose: Manage output of sim parameters to file or stdout Called by: FGSimExec - ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) ------------- + ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) ------------- This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software @@ -33,33 +33,92 @@ later. HISTORY -------------------------------------------------------------------------------- 12/02/98 JSB Created +11/09/07 HDW Added FlightGear Socket Interface %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% INCLUDES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ #include "FGOutput.h" -#include "FGState.h" #include "FGFDMExec.h" #include "FGAtmosphere.h" #include "FGFCS.h" #include "FGAerodynamics.h" #include "FGGroundReactions.h" +#include "FGExternalReactions.h" +#include "FGBuoyantForces.h" #include "FGAircraft.h" #include "FGMassBalance.h" #include "FGPropagate.h" #include "FGAuxiliary.h" #include "FGInertial.h" - -#include +#include "FGPropulsion.h" +#include "models/propulsion/FGEngine.h" +#include "models/propulsion/FGTank.h" +#include "models/propulsion/FGPiston.h" +#include #include +#include +#include + +#if defined(WIN32) && !defined(__CYGWIN__) +# include +#else +# include // htonl() ntohl() +#endif + +static const int endianTest = 1; +#define isLittleEndian (*((char *) &endianTest ) != 0) + +using namespace std; namespace JSBSim { -static const char *IdSrc = "$Id$"; +static const char *IdSrc = "$Id: FGOutput.cpp,v 1.54 2011/03/11 13:02:26 jberndt Exp $"; static const char *IdHdr = ID_OUTPUT; -using std::setprecision; +// (stolen from FGFS native_fdm.cxx) +// The function htond is defined this way due to the way some +// processors and OSes treat floating point values. Some will raise +// an exception whenever a "bad" floating point value is loaded into a +// floating point register. Solaris is notorious for this, but then +// so is LynxOS on the PowerPC. By translating the data in place, +// there is no need to load a FP register with the "corruped" floating +// point value. By doing the BIG_ENDIAN test, I can optimize the +// routine for big-endian processors so it can be as efficient as +// possible +static void htond (double &x) +{ + if ( isLittleEndian ) { + int *Double_Overlay; + int Holding_Buffer; + + Double_Overlay = (int *) &x; + Holding_Buffer = Double_Overlay [0]; + + Double_Overlay [0] = htonl (Double_Overlay [1]); + Double_Overlay [1] = htonl (Holding_Buffer); + } else { + return; + } +} + +// Float version +static void htonf (float &x) +{ + if ( isLittleEndian ) { + int *Float_Overlay; + int Holding_Buffer; + + Float_Overlay = (int *) &x; + Holding_Buffer = Float_Overlay [0]; + + Float_Overlay [0] = htonl (Holding_Buffer); + } else { + return; + } +} + /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CLASS IMPLEMENTATION @@ -70,12 +129,17 @@ FGOutput::FGOutput(FGFDMExec* fdmex) : FGModel(fdmex) Name = "FGOutput"; sFirstPass = dFirstPass = true; socket = 0; + runID_postfix = 0; Type = otNone; SubSystems = 0; enabled = true; + StartNewFile = false; delimeter = ", "; - Filename = ""; + BaseFilename = Filename = ""; DirectivesFile = ""; + output_file_name = ""; + + memset(&fgSockBuf, 0x00, sizeof(fgSockBuf)); Debug(0); } @@ -91,29 +155,63 @@ FGOutput::~FGOutput() //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -bool FGOutput::Run(void) +bool FGOutput::InitModel(void) { - if (FGModel::Run()) return true; + if (!FGModel::InitModel()) return false; - if (enabled && !State->IntegrationSuspended()&& !FDMExec->Holding()) { - if (Type == otSocket) { - SocketOutput(); - } else if (Type == otCSV || Type == otTab) { - DelimitedOutput(Filename); - } else if (Type == otTerminal) { - // Not done yet - } else if (Type == otNone) { - // Do nothing + if (Filename.size() > 0 && StartNewFile) { + ostringstream buf; + string::size_type dot = BaseFilename.find_last_of('.'); + if (dot != string::npos) { + buf << BaseFilename.substr(0, dot) << '_' << runID_postfix++ << BaseFilename.substr(dot); } else { - // Not a valid type of output + buf << BaseFilename << '_' << runID_postfix++; } + Filename = buf.str(); + datafile.close(); + StartNewFile = false; + dFirstPass = true; + } + + return true; +} + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +bool FGOutput::Run(void) +{ + if (FGModel::Run()) return true; + + if (enabled && !FDMExec->IntegrationSuspended() && !FDMExec->Holding()) { + RunPreFunctions(); + Print(); + RunPostFunctions(); } return false; } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -void FGOutput::SetType(string type) +void FGOutput::Print(void) +{ + if (Type == otSocket) { + SocketOutput(); + } else if (Type == otFlightGear) { + FlightGearSocketOutput(); + } else if (Type == otCSV || Type == otTab) { + DelimitedOutput(Filename); + } else if (Type == otTerminal) { + // Not done yet + } else if (Type == otNone) { + // Do nothing + } else { + // Not a valid type of output + } +} + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +void FGOutput::SetType(const string& type) { if (type == "CSV") { Type = otCSV; @@ -123,6 +221,8 @@ void FGOutput::SetType(string type) delimeter = "\t"; } else if (type == "SOCKET") { Type = otSocket; + } else if (type == "FLIGHTGEAR") { + Type = otFlightGear; } else if (type == "TERMINAL") { Type = otTerminal; } else if (type != string("NONE")) { @@ -133,15 +233,37 @@ void FGOutput::SetType(string type) //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -void FGOutput::DelimitedOutput(string fname) +void FGOutput::SetProtocol(const string& protocol) +{ + if (protocol == "UDP") Protocol = FGfdmSocket::ptUDP; + else if (protocol == "TCP") Protocol = FGfdmSocket::ptTCP; + else Protocol = FGfdmSocket::ptTCP; // Default to TCP +} + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +void FGOutput::DelimitedOutput(const string& fname) { + const FGAerodynamics* Aerodynamics = FDMExec->GetAerodynamics(); + const FGAuxiliary* Auxiliary = FDMExec->GetAuxiliary(); + const FGAircraft* Aircraft = FDMExec->GetAircraft(); + const FGAtmosphere* Atmosphere = FDMExec->GetAtmosphere(); + const FGPropulsion* Propulsion = FDMExec->GetPropulsion(); + const FGMassBalance* MassBalance = FDMExec->GetMassBalance(); + const FGPropagate* Propagate = FDMExec->GetPropagate(); + const FGFCS* FCS = FDMExec->GetFCS(); + const FGInertial* Inertial = FDMExec->GetInertial(); + const FGGroundReactions* GroundReactions = FDMExec->GetGroundReactions(); + const FGExternalReactions* ExternalReactions = FDMExec->GetExternalReactions(); + const FGBuoyantForces* BuoyantForces = FDMExec->GetBuoyantForces(); + streambuf* buffer; string scratch = ""; if (fname == "COUT" || fname == "cout") { buffer = cout.rdbuf(); } else { - datafile.open(fname.c_str()); + if (!datafile.is_open()) datafile.open(fname.c_str()); buffer = datafile.rdbuf(); } @@ -169,60 +291,84 @@ void FGOutput::DelimitedOutput(string fname) if (SubSystems & ssRates) { outstream << delimeter; outstream << "P (deg/s)" + delimeter + "Q (deg/s)" + delimeter + "R (deg/s)" + delimeter; - outstream << "P dot (deg/s^2)" + delimeter + "Q dot (deg/s^2)" + delimeter + "R dot (deg/s^2)"; + outstream << "P dot (deg/s^2)" + delimeter + "Q dot (deg/s^2)" + delimeter + "R dot (deg/s^2)" + delimeter; + outstream << "P_{inertial} (deg/s)" + delimeter + "Q_{inertial} (deg/s)" + delimeter + "R_{inertial} (deg/s)"; } if (SubSystems & ssVelocities) { outstream << delimeter; outstream << "q bar (psf)" + delimeter; + outstream << "Reynolds Number" + delimeter; outstream << "V_{Total} (ft/s)" + delimeter; + outstream << "V_{Inertial} (ft/s)" + delimeter; outstream << "UBody" + delimeter + "VBody" + delimeter + "WBody" + delimeter; outstream << "Aero V_{X Body} (ft/s)" + delimeter + "Aero V_{Y Body} (ft/s)" + delimeter + "Aero V_{Z Body} (ft/s)" + delimeter; + outstream << "V_{X_{inertial}} (ft/s)" + delimeter + "V_{Y_{inertial}} (ft/s)" + delimeter + "V_{Z_{inertial}} (ft/s)" + delimeter; + outstream << "V_{X_{ecef}} (ft/s)" + delimeter + "V_{Y_{ecef}} (ft/s)" + delimeter + "V_{Z_{ecef}} (ft/s)" + delimeter; outstream << "V_{North} (ft/s)" + delimeter + "V_{East} (ft/s)" + delimeter + "V_{Down} (ft/s)"; } if (SubSystems & ssForces) { outstream << delimeter; outstream << "F_{Drag} (lbs)" + delimeter + "F_{Side} (lbs)" + delimeter + "F_{Lift} (lbs)" + delimeter; outstream << "L/D" + delimeter; - outstream << "F_X (lbs)" + delimeter + "F_Y (lbs)" + delimeter + "F_Z (lbs)"; + outstream << "F_{Aero x} (lbs)" + delimeter + "F_{Aero y} (lbs)" + delimeter + "F_{Aero z} (lbs)" + delimeter; + outstream << "F_{Prop x} (lbs)" + delimeter + "F_{Prop y} (lbs)" + delimeter + "F_{Prop z} (lbs)" + delimeter; + outstream << "F_{Gear x} (lbs)" + delimeter + "F_{Gear y} (lbs)" + delimeter + "F_{Gear z} (lbs)" + delimeter; + outstream << "F_{Ext x} (lbs)" + delimeter + "F_{Ext y} (lbs)" + delimeter + "F_{Ext z} (lbs)" + delimeter; + outstream << "F_{Buoyant x} (lbs)" + delimeter + "F_{Buoyant y} (lbs)" + delimeter + "F_{Buoyant z} (lbs)" + delimeter; + outstream << "F_{Total x} (lbs)" + delimeter + "F_{Total y} (lbs)" + delimeter + "F_{Total z} (lbs)"; } if (SubSystems & ssMoments) { outstream << delimeter; - outstream << "L (ft-lbs)" + delimeter + "M (ft-lbs)" + delimeter + "N (ft-lbs)"; + outstream << "L_{Aero} (ft-lbs)" + delimeter + "M_{Aero} ( ft-lbs)" + delimeter + "N_{Aero} (ft-lbs)" + delimeter; + outstream << "L_{Prop} (ft-lbs)" + delimeter + "M_{Prop} (ft-lbs)" + delimeter + "N_{Prop} (ft-lbs)" + delimeter; + outstream << "L_{Gear} (ft-lbs)" + delimeter + "M_{Gear} (ft-lbs)" + delimeter + "N_{Gear} (ft-lbs)" + delimeter; + outstream << "L_{ext} (ft-lbs)" + delimeter + "M_{ext} (ft-lbs)" + delimeter + "N_{ext} (ft-lbs)" + delimeter; + outstream << "L_{Buoyant} (ft-lbs)" + delimeter + "M_{Buoyant} (ft-lbs)" + delimeter + "N_{Buoyant} (ft-lbs)" + delimeter; + outstream << "L_{Total} (ft-lbs)" + delimeter + "M_{Total} (ft-lbs)" + delimeter + "N_{Total} (ft-lbs)"; } if (SubSystems & ssAtmosphere) { outstream << delimeter; outstream << "Rho (slugs/ft^3)" + delimeter; + outstream << "Absolute Viscosity" + delimeter; + outstream << "Kinematic Viscosity" + delimeter; + outstream << "Temperature (R)" + delimeter; outstream << "P_{SL} (psf)" + delimeter; outstream << "P_{Ambient} (psf)" + delimeter; + outstream << "Turbulence Magnitude (ft/sec)" + delimeter; + outstream << "Turbulence X Direction (rad)" + delimeter + "Turbulence Y Direction (rad)" + delimeter + "Turbulence Z Direction (rad)" + delimeter; outstream << "Wind V_{North} (ft/s)" + delimeter + "Wind V_{East} (ft/s)" + delimeter + "Wind V_{Down} (ft/s)"; } if (SubSystems & ssMassProps) { outstream << delimeter; - outstream << "I_xx" + delimeter; - outstream << "I_xy" + delimeter; - outstream << "I_xz" + delimeter; - outstream << "I_yx" + delimeter; - outstream << "I_yy" + delimeter; - outstream << "I_yz" + delimeter; - outstream << "I_zx" + delimeter; - outstream << "I_zy" + delimeter; - outstream << "I_zz" + delimeter; + outstream << "I_{xx}" + delimeter; + outstream << "I_{xy}" + delimeter; + outstream << "I_{xz}" + delimeter; + outstream << "I_{yx}" + delimeter; + outstream << "I_{yy}" + delimeter; + outstream << "I_{yz}" + delimeter; + outstream << "I_{zx}" + delimeter; + outstream << "I_{zy}" + delimeter; + outstream << "I_{zz}" + delimeter; outstream << "Mass" + delimeter; - outstream << "X_cg" + delimeter + "Y_cg" + delimeter + "Z_cg"; + outstream << "X_{cg}" + delimeter + "Y_{cg}" + delimeter + "Z_{cg}"; } if (SubSystems & ssPropagate) { outstream << delimeter; - outstream << "Altitude (ft)" + delimeter; + outstream << "Altitude ASL (ft)" + delimeter; + outstream << "Altitude AGL (ft)" + delimeter; outstream << "Phi (deg)" + delimeter + "Theta (deg)" + delimeter + "Psi (deg)" + delimeter; outstream << "Alpha (deg)" + delimeter; outstream << "Beta (deg)" + delimeter; outstream << "Latitude (deg)" + delimeter; outstream << "Longitude (deg)" + delimeter; + outstream << "X_{ECI} (ft)" + delimeter + "Y_{ECI} (ft)" + delimeter + "Z_{ECI} (ft)" + delimeter; + outstream << "X_{ECEF} (ft)" + delimeter + "Y_{ECEF} (ft)" + delimeter + "Z_{ECEF} (ft)" + delimeter; + outstream << "Earth Position Angle (deg)" + delimeter; outstream << "Distance AGL (ft)" + delimeter; - outstream << "Runway Radius (ft)"; + outstream << "Terrain Elevation (ft)"; } - if (SubSystems & ssCoefficients) { - scratch = Aerodynamics->GetCoefficientStrings(delimeter); + if (SubSystems & ssAeroFunctions) { + scratch = Aerodynamics->GetAeroFunctionStrings(delimeter); if (scratch.length() != 0) outstream << delimeter << scratch; } if (SubSystems & ssFCS) { @@ -247,7 +393,7 @@ void FGOutput::DelimitedOutput(string fname) dFirstPass = false; } - outstream << State->Getsim_time(); + outstream << FDMExec->GetSimTime(); if (SubSystems & ssSimulation) { } if (SubSystems & ssAerosurfaces) { @@ -265,52 +411,81 @@ void FGOutput::DelimitedOutput(string fname) if (SubSystems & ssRates) { outstream << delimeter; outstream << (radtodeg*Propagate->GetPQR()).Dump(delimeter) << delimeter; - outstream << (radtodeg*Propagate->GetPQRdot()).Dump(delimeter); + outstream << (radtodeg*Propagate->GetPQRdot()).Dump(delimeter) << delimeter; + outstream << (radtodeg*Propagate->GetPQRi()).Dump(delimeter); } if (SubSystems & ssVelocities) { outstream << delimeter; outstream << Auxiliary->Getqbar() << delimeter; + outstream << Auxiliary->GetReynoldsNumber() << delimeter; outstream << setprecision(12) << Auxiliary->GetVt() << delimeter; + outstream << Propagate->GetInertialVelocityMagnitude() << delimeter; outstream << setprecision(12) << Propagate->GetUVW().Dump(delimeter) << delimeter; outstream << Auxiliary->GetAeroUVW().Dump(delimeter) << delimeter; + outstream << Propagate->GetInertialVelocity().Dump(delimeter) << delimeter; + outstream << Propagate->GetECEFVelocity().Dump(delimeter) << delimeter; outstream << Propagate->GetVel().Dump(delimeter); + outstream.precision(10); } if (SubSystems & ssForces) { outstream << delimeter; - outstream << Aerodynamics->GetvFs() << delimeter; + outstream << Aerodynamics->GetvFw().Dump(delimeter) << delimeter; outstream << Aerodynamics->GetLoD() << delimeter; + outstream << Aerodynamics->GetForces().Dump(delimeter) << delimeter; + outstream << Propulsion->GetForces().Dump(delimeter) << delimeter; + outstream << GroundReactions->GetForces().Dump(delimeter) << delimeter; + outstream << ExternalReactions->GetForces().Dump(delimeter) << delimeter; + outstream << BuoyantForces->GetForces().Dump(delimeter) << delimeter; outstream << Aircraft->GetForces().Dump(delimeter); } if (SubSystems & ssMoments) { outstream << delimeter; + outstream << Aerodynamics->GetMoments().Dump(delimeter) << delimeter; + outstream << Propulsion->GetMoments().Dump(delimeter) << delimeter; + outstream << GroundReactions->GetMoments().Dump(delimeter) << delimeter; + outstream << ExternalReactions->GetMoments().Dump(delimeter) << delimeter; + outstream << BuoyantForces->GetMoments().Dump(delimeter) << delimeter; outstream << Aircraft->GetMoments().Dump(delimeter); } if (SubSystems & ssAtmosphere) { outstream << delimeter; outstream << Atmosphere->GetDensity() << delimeter; + outstream << Atmosphere->GetAbsoluteViscosity() << delimeter; + outstream << Atmosphere->GetKinematicViscosity() << delimeter; + outstream << Atmosphere->GetTemperature() << delimeter; outstream << Atmosphere->GetPressureSL() << delimeter; outstream << Atmosphere->GetPressure() << delimeter; - outstream << Atmosphere->GetWindNED().Dump(delimeter); + outstream << Atmosphere->GetTurbMagnitude() << delimeter; + outstream << Atmosphere->GetTurbDirection().Dump(delimeter) << delimeter; + outstream << Atmosphere->GetTotalWindNED().Dump(delimeter); } if (SubSystems & ssMassProps) { outstream << delimeter; - outstream << MassBalance->GetJ() << delimeter; + outstream << MassBalance->GetJ().Dump(delimeter) << delimeter; outstream << MassBalance->GetMass() << delimeter; - outstream << MassBalance->GetXYZcg(); + outstream << MassBalance->GetXYZcg().Dump(delimeter); } if (SubSystems & ssPropagate) { + outstream.precision(14); outstream << delimeter; - outstream << Propagate->Geth() << delimeter; + outstream << Propagate->GetAltitudeASL() << delimeter; + outstream << Propagate->GetDistanceAGL() << delimeter; outstream << (radtodeg*Propagate->GetEuler()).Dump(delimeter) << delimeter; outstream << Auxiliary->Getalpha(inDegrees) << delimeter; outstream << Auxiliary->Getbeta(inDegrees) << delimeter; outstream << Propagate->GetLocation().GetLatitudeDeg() << delimeter; outstream << Propagate->GetLocation().GetLongitudeDeg() << delimeter; + outstream.precision(18); + outstream << ((FGColumnVector3)Propagate->GetInertialPosition()).Dump(delimeter) << delimeter; + outstream << ((FGColumnVector3)Propagate->GetLocation()).Dump(delimeter) << delimeter; + outstream.precision(14); + outstream << Inertial->GetEarthPositionAngleDeg() << delimeter; outstream << Propagate->GetDistanceAGL() << delimeter; - outstream << Propagate->GetRunwayRadius(); + outstream << Propagate->GetTerrainElevation(); + outstream.precision(10); } - if (SubSystems & ssCoefficients) { - scratch = Aerodynamics->GetCoefficientValues(delimeter); + if (SubSystems & ssAeroFunctions) { + scratch = Aerodynamics->GetAeroFunctionValues(delimeter); if (scratch.length() != 0) outstream << delimeter << scratch; } if (SubSystems & ssFCS) { @@ -326,9 +501,11 @@ void FGOutput::DelimitedOutput(string fname) outstream << Propulsion->GetPropulsionValues(delimeter); } + outstream.precision(18); for (unsigned int i=0;igetDoubleValue(); } + outstream.precision(10); outstream << endl; outstream.flush(); @@ -336,8 +513,233 @@ void FGOutput::DelimitedOutput(string fname) //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +void FGOutput::SocketDataFill(FGNetFDM* net) +{ + const FGAerodynamics* Aerodynamics = FDMExec->GetAerodynamics(); + const FGAuxiliary* Auxiliary = FDMExec->GetAuxiliary(); + const FGPropulsion* Propulsion = FDMExec->GetPropulsion(); + const FGMassBalance* MassBalance = FDMExec->GetMassBalance(); + const FGPropagate* Propagate = FDMExec->GetPropagate(); + const FGFCS* FCS = FDMExec->GetFCS(); + const FGGroundReactions* GroundReactions = FDMExec->GetGroundReactions(); + unsigned int i; + + // Version + net->version = FG_NET_FDM_VERSION; + + // Positions + net->longitude = Propagate->GetLocation().GetLongitude(); // geodetic (radians) + net->latitude = Propagate->GetLocation().GetLatitude(); // geodetic (radians) + net->altitude = Propagate->GetAltitudeASL()*0.3048; // altitude, above sea level (meters) + net->agl = (float)(Propagate->GetDistanceAGL()*0.3048); // altitude, above ground level (meters) + + net->phi = (float)(Propagate->GetEuler(ePhi)); // roll (radians) + net->theta = (float)(Propagate->GetEuler(eTht)); // pitch (radians) + net->psi = (float)(Propagate->GetEuler(ePsi)); // yaw or true heading (radians) + + net->alpha = (float)(Auxiliary->Getalpha()); // angle of attack (radians) + net->beta = (float)(Auxiliary->Getbeta()); // side slip angle (radians) + + // Velocities + net->phidot = (float)(Auxiliary->GetEulerRates(ePhi)); // roll rate (radians/sec) + net->thetadot = (float)(Auxiliary->GetEulerRates(eTht)); // pitch rate (radians/sec) + net->psidot = (float)(Auxiliary->GetEulerRates(ePsi)); // yaw rate (radians/sec) + net->vcas = (float)(Auxiliary->GetVcalibratedFPS()); // VCAS, ft/sec + net->climb_rate = (float)(Propagate->Gethdot()); // altitude rate, ft/sec + net->v_north = (float)(Propagate->GetVel(eNorth)); // north vel in NED frame, fps + net->v_east = (float)(Propagate->GetVel(eEast)); // east vel in NED frame, fps + net->v_down = (float)(Propagate->GetVel(eDown)); // down vel in NED frame, fps +//---ADD METHOD TO CALCULATE THESE TERMS--- + net->v_wind_body_north = (float)(Propagate->GetVel(eNorth)); // north vel in NED relative to airmass, fps + net->v_wind_body_east = (float)(Propagate->GetVel(eEast)); // east vel in NED relative to airmass, fps + net->v_wind_body_down = (float)(Propagate->GetVel(eDown)); // down vel in NED relative to airmass, fps + + // Accelerations + net->A_X_pilot = (float)(Auxiliary->GetPilotAccel(1)); // X body accel, ft/s/s + net->A_Y_pilot = (float)(Auxiliary->GetPilotAccel(2)); // Y body accel, ft/s/s + net->A_Z_pilot = (float)(Auxiliary->GetPilotAccel(3)); // Z body accel, ft/s/s + + // Stall + net->stall_warning = 0.0; // 0.0 - 1.0 indicating the amount of stall + net->slip_deg = (float)(Auxiliary->Getbeta(inDegrees)); // slip ball deflection, deg + + // Engine status + net->num_engines = Propulsion->GetNumEngines(); // Number of valid engines + + for (i=0; inum_engines; i++) { + if (Propulsion->GetEngine(i)->GetRunning()) + net->eng_state[i] = 2; // Engine state running + else if (Propulsion->GetEngine(i)->GetCranking()) + net->eng_state[i] = 1; // Engine state cranking + else + net->eng_state[i] = 0; // Engine state off + + switch (Propulsion->GetEngine(i)->GetType()) { + case (FGEngine::etRocket): + break; + case (FGEngine::etPiston): + net->rpm[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getRPM()); + net->fuel_flow[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getFuelFlow_gph()); + net->fuel_px[i] = 0; // Fuel pressure, psi (N/A in current model) + net->egt[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->GetEGT()); + net->cht[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getCylinderHeadTemp_degF()); + net->mp_osi[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getManifoldPressure_inHg()); + net->oil_temp[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getOilTemp_degF()); + net->oil_px[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getOilPressure_psi()); + net->tit[i] = 0; // Turbine Inlet Temperature (N/A for piston) + break; + case (FGEngine::etTurbine): + break; + case (FGEngine::etTurboprop): + break; + case (FGEngine::etElectric): + break; + case (FGEngine::etUnknown): + break; + } + } + + // Consumables + net->num_tanks = Propulsion->GetNumTanks(); // Max number of fuel tanks + + for (i=0; inum_tanks; i++) { + net->fuel_quantity[i] = (float)(((FGTank *)Propulsion->GetTank(i))->GetContents()); + } + + // Gear status + net->num_wheels = GroundReactions->GetNumGearUnits(); + + for (i=0; inum_wheels; i++) { + net->wow[i] = GroundReactions->GetGearUnit(i)->GetWOW(); + if (GroundReactions->GetGearUnit(i)->GetGearUnitDown()) + net->gear_pos[i] = 1; //gear down, using FCS convention + else + net->gear_pos[i] = 0; //gear up, using FCS convention + net->gear_steer[i] = (float)(GroundReactions->GetGearUnit(i)->GetSteerNorm()); + net->gear_compression[i] = (float)(GroundReactions->GetGearUnit(i)->GetCompLen()); + } + + // Environment + net->cur_time = (long int)1234567890; // Friday, Feb 13, 2009, 23:31:30 UTC (not processed by FGFS anyway) + net->warp = 0; // offset in seconds to unix time + net->visibility = 25000.0; // visibility in meters (for env. effects) + + // Control surface positions (normalized values) + net->elevator = (float)(FCS->GetDePos(ofNorm)); // Norm Elevator Pos, -- + net->elevator_trim_tab = (float)(FCS->GetPitchTrimCmd()); // Norm Elev Trim Tab Pos, -- + net->left_flap = (float)(FCS->GetDfPos(ofNorm)); // Norm Flap Pos, -- + net->right_flap = (float)(FCS->GetDfPos(ofNorm)); // Norm Flap Pos, -- + net->left_aileron = (float)(FCS->GetDaLPos(ofNorm)); // Norm L Aileron Pos, -- + net->right_aileron = (float)(FCS->GetDaRPos(ofNorm)); // Norm R Aileron Pos, -- + net->rudder = (float)(FCS->GetDrPos(ofNorm)); // Norm Rudder Pos, -- + net->nose_wheel = (float)(FCS->GetDrPos(ofNorm)); // *** FIX *** Using Rudder Pos for NWS, -- + net->speedbrake = (float)(FCS->GetDsbPos(ofNorm)); // Norm Speedbrake Pos, -- + net->spoilers = (float)(FCS->GetDspPos(ofNorm)); // Norm Spoiler Pos, -- + + // Convert the net buffer to network format + if ( isLittleEndian ) { + net->version = htonl(net->version); + + htond(net->longitude); + htond(net->latitude); + htond(net->altitude); + htonf(net->agl); + htonf(net->phi); + htonf(net->theta); + htonf(net->psi); + htonf(net->alpha); + htonf(net->beta); + + htonf(net->phidot); + htonf(net->thetadot); + htonf(net->psidot); + htonf(net->vcas); + htonf(net->climb_rate); + htonf(net->v_north); + htonf(net->v_east); + htonf(net->v_down); + htonf(net->v_wind_body_north); + htonf(net->v_wind_body_east); + htonf(net->v_wind_body_down); + + htonf(net->A_X_pilot); + htonf(net->A_Y_pilot); + htonf(net->A_Z_pilot); + + htonf(net->stall_warning); + htonf(net->slip_deg); + + for (i=0; inum_engines; ++i ) { + net->eng_state[i] = htonl(net->eng_state[i]); + htonf(net->rpm[i]); + htonf(net->fuel_flow[i]); + htonf(net->fuel_px[i]); + htonf(net->egt[i]); + htonf(net->cht[i]); + htonf(net->mp_osi[i]); + htonf(net->tit[i]); + htonf(net->oil_temp[i]); + htonf(net->oil_px[i]); + } + net->num_engines = htonl(net->num_engines); + + for (i=0; inum_tanks; ++i ) { + htonf(net->fuel_quantity[i]); + } + net->num_tanks = htonl(net->num_tanks); + + for (i=0; inum_wheels; ++i ) { + net->wow[i] = htonl(net->wow[i]); + htonf(net->gear_pos[i]); + htonf(net->gear_steer[i]); + htonf(net->gear_compression[i]); + } + net->num_wheels = htonl(net->num_wheels); + + net->cur_time = htonl( net->cur_time ); + net->warp = htonl( net->warp ); + htonf(net->visibility); + + htonf(net->elevator); + htonf(net->elevator_trim_tab); + htonf(net->left_flap); + htonf(net->right_flap); + htonf(net->left_aileron); + htonf(net->right_aileron); + htonf(net->rudder); + htonf(net->nose_wheel); + htonf(net->speedbrake); + htonf(net->spoilers); + } +} + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +void FGOutput::FlightGearSocketOutput(void) +{ + int length = sizeof(fgSockBuf); + + if (socket == NULL) return; + if (!socket->GetConnectStatus()) return; + + SocketDataFill(&fgSockBuf); + socket->Send((char *)&fgSockBuf, length); +} + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + void FGOutput::SocketOutput(void) { + const FGAerodynamics* Aerodynamics = FDMExec->GetAerodynamics(); + const FGAuxiliary* Auxiliary = FDMExec->GetAuxiliary(); + const FGPropulsion* Propulsion = FDMExec->GetPropulsion(); + const FGMassBalance* MassBalance = FDMExec->GetMassBalance(); + const FGPropagate* Propagate = FDMExec->GetPropagate(); + const FGFCS* FCS = FDMExec->GetFCS(); + const FGAtmosphere* Atmosphere = FDMExec->GetAtmosphere(); + const FGAircraft* Aircraft = FDMExec->GetAircraft(); + const FGGroundReactions* GroundReactions = FDMExec->GetGroundReactions(); + string asciiData, scratch; if (socket == NULL) return; @@ -400,6 +802,10 @@ void FGOutput::SocketOutput(void) socket->Append("Rho"); socket->Append("SL pressure"); socket->Append("Ambient pressure"); + socket->Append("Turbulence Magnitude"); + socket->Append("Turbulence Direction X"); + socket->Append("Turbulence Direction Y"); + socket->Append("Turbulence Direction Z"); socket->Append("NWind"); socket->Append("EWind"); socket->Append("DWind"); @@ -429,8 +835,8 @@ void FGOutput::SocketOutput(void) socket->Append("Latitude (deg)"); socket->Append("Longitude (deg)"); } - if (SubSystems & ssCoefficients) { - scratch = Aerodynamics->GetCoefficientStrings(","); + if (SubSystems & ssAeroFunctions) { + scratch = Aerodynamics->GetAeroFunctionStrings(","); if (scratch.length() != 0) socket->Append(scratch); } if (SubSystems & ssFCS) { @@ -454,7 +860,7 @@ void FGOutput::SocketOutput(void) } socket->Clear(); - socket->Append(State->Getsim_time()); + socket->Append(FDMExec->GetSimTime()); if (SubSystems & ssAerosurfaces) { socket->Append(FCS->GetDaCmd()); @@ -489,9 +895,9 @@ void FGOutput::SocketOutput(void) socket->Append(Propagate->GetVel(eDown)); } if (SubSystems & ssForces) { - socket->Append(Aerodynamics->GetvFs()(eDrag)); - socket->Append(Aerodynamics->GetvFs()(eSide)); - socket->Append(Aerodynamics->GetvFs()(eLift)); + socket->Append(Aerodynamics->GetvFw()(eDrag)); + socket->Append(Aerodynamics->GetvFw()(eSide)); + socket->Append(Aerodynamics->GetvFw()(eLift)); socket->Append(Aerodynamics->GetLoD()); socket->Append(Aircraft->GetForces(eX)); socket->Append(Aircraft->GetForces(eY)); @@ -506,7 +912,9 @@ void FGOutput::SocketOutput(void) socket->Append(Atmosphere->GetDensity()); socket->Append(Atmosphere->GetPressureSL()); socket->Append(Atmosphere->GetPressure()); - socket->Append(Atmosphere->GetWindNED().Dump(",")); + socket->Append(Atmosphere->GetTurbMagnitude()); + socket->Append(Atmosphere->GetTurbDirection().Dump(",")); + socket->Append(Atmosphere->GetTotalWindNED().Dump(",")); } if (SubSystems & ssMassProps) { socket->Append(MassBalance->GetJ()(1,1)); @@ -524,7 +932,7 @@ void FGOutput::SocketOutput(void) socket->Append(MassBalance->GetXYZcg()(eZ)); } if (SubSystems & ssPropagate) { - socket->Append(Propagate->Geth()); + socket->Append(Propagate->GetAltitudeASL()); socket->Append(radtodeg*Propagate->GetEuler(ePhi)); socket->Append(radtodeg*Propagate->GetEuler(eTht)); socket->Append(radtodeg*Propagate->GetEuler(ePsi)); @@ -533,8 +941,8 @@ void FGOutput::SocketOutput(void) socket->Append(Propagate->GetLocation().GetLatitudeDeg()); socket->Append(Propagate->GetLocation().GetLongitudeDeg()); } - if (SubSystems & ssCoefficients) { - scratch = Aerodynamics->GetCoefficientValues(","); + if (SubSystems & ssAeroFunctions) { + scratch = Aerodynamics->GetAeroFunctionValues(","); if (scratch.length() != 0) socket->Append(scratch); } if (SubSystems & ssFCS) { @@ -557,7 +965,7 @@ void FGOutput::SocketOutput(void) //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -void FGOutput::SocketStatusOutput(string out_str) +void FGOutput::SocketStatusOutput(const string& out_str) { string asciiData; @@ -573,47 +981,38 @@ void FGOutput::SocketStatusOutput(string out_str) bool FGOutput::Load(Element* element) { - string type="", parameter=""; - string name="", fname=""; - int OutRate = 0; - string property; + string parameter=""; + string name=""; + double OutRate = 0.0; unsigned int port; Element *property_element; string separator = "/"; -# ifdef macintosh - separator = ";"; -# endif if (!DirectivesFile.empty()) { // A directives filename from the command line overrides - fname = DirectivesFile; // one found in the config file. - } else { - fname = element->GetAttributeValue("file"); - } - - if (!fname.empty()) { - int len = fname.size(); - if (fname.find(".xml") != string::npos) { - output_file_name = fname; // Use supplied name if last four letters are ".xml" - } else { - output_file_name = FDMExec->GetFullAircraftPath() + separator + fname + ".xml"; - } + output_file_name = DirectivesFile; // one found in the config file. + document = LoadXMLDocument(output_file_name); + } else if (!element->GetAttributeValue("file").empty()) { + output_file_name = FDMExec->GetRootDir() + element->GetAttributeValue("file"); document = LoadXMLDocument(output_file_name); } else { document = element; } - name = document->GetAttributeValue("name"); - type = document->GetAttributeValue("type"); - SetType(type); - if (!document->GetAttributeValue("port").empty() && type == string("SOCKET")) { - port = atoi(document->GetAttributeValue("port").c_str()); - socket = new FGfdmSocket(name, port); + if (!document) return false; + + name = FDMExec->GetRootDir() + document->GetAttributeValue("name"); + SetType(document->GetAttributeValue("type")); + Port = document->GetAttributeValue("port"); + if (!Port.empty() && (Type == otSocket || Type == otFlightGear)) { + port = atoi(Port.c_str()); + SetProtocol(document->GetAttributeValue("protocol")); + socket = new FGfdmSocket(name, port, Protocol); } else { - Filename = name; + BaseFilename = Filename = name; } if (!document->GetAttributeValue("rate").empty()) { - OutRate = (int)document->GetAttributeValueAsNumber("rate"); + OutRate = document->GetAttributeValueAsNumber("rate"); } else { OutRate = 1; } @@ -637,7 +1036,7 @@ bool FGOutput::Load(Element* element) if (document->FindElementValue("position") == string("ON")) SubSystems += ssPropagate; if (document->FindElementValue("coefficients") == string("ON")) - SubSystems += ssCoefficients; + SubSystems += ssAeroFunctions; if (document->FindElementValue("ground_reactions") == string("ON")) SubSystems += ssGroundReactions; if (document->FindElementValue("fcs") == string("ON")) @@ -646,19 +1045,40 @@ bool FGOutput::Load(Element* element) SubSystems += ssPropulsion; property_element = document->FindElement("property"); while (property_element) { - string property = property_element->GetDataLine(); - OutputProperties.push_back(PropertyManager->GetNode(property)); + string property_str = property_element->GetDataLine(); + FGPropertyManager* node = PropertyManager->GetNode(property_str); + if (!node) { + cerr << fgred << highint << endl << " No property by the name " + << property_str << " has been defined. This property will " << endl + << " not be logged. You should check your configuration file." + << reset << endl; + } else { + OutputProperties.push_back(node); + } property_element = document->FindNextElement("property"); } - OutRate = OutRate>1000?1000:(OutRate<0?0:OutRate); - rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate)); + SetRate(OutRate); Debug(2); return true; } +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +void FGOutput::SetRate(double rtHz) +{ + rtHz = rtHz>1000?1000:(rtHz<0?0:rtHz); + if (rtHz > 0) { + rate = (int)(0.5 + 1.0/(FDMExec->GetDeltaT()*rtHz)); + Enable(); + } else { + rate = 1; + Disable(); + } +} + //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // The bitmasked value choices are as follows: // unset: In this case (the default) JSBSim would only print @@ -701,9 +1121,10 @@ void FGOutput::Debug(int from) } switch (Type) { case otCSV: - cout << scratch << " in CSV format output at rate " << 1/(State->Getdt()*rate) << " Hz" << endl; + cout << scratch << " in CSV format output at rate " << 1/(FDMExec->GetDeltaT()*rate) << " Hz" << endl; break; case otNone: + default: cout << " No log output" << endl; break; } @@ -716,7 +1137,7 @@ void FGOutput::Debug(int from) if (SubSystems & ssMoments) cout << " Moments parameters logged" << endl; if (SubSystems & ssAtmosphere) cout << " Atmosphere parameters logged" << endl; if (SubSystems & ssMassProps) cout << " Mass parameters logged" << endl; - if (SubSystems & ssCoefficients) cout << " Coefficient parameters logged" << endl; + if (SubSystems & ssAeroFunctions) cout << " Coefficient parameters logged" << endl; if (SubSystems & ssPropagate) cout << " Propagate parameters logged" << endl; if (SubSystems & ssGroundReactions) cout << " Ground parameters logged" << endl; if (SubSystems & ssFCS) cout << " FCS parameters logged" << endl;