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 "FGAircraft.h"
50 #include "FGMassBalance.h"
51 #include "FGPropagate.h"
52 #include "FGAuxiliary.h"
53 #include "FGInertial.h"
54 #include "FGPropulsion.h" //access to FGEngine, FGTank
55 #include "models/propulsion/FGPiston.h"
60 #include "input_output/net_fdm.hxx"
62 #if defined(WIN32) && !defined(__CYGWIN__)
65 # include <netinet/in.h> // htonl() ntohl()
68 static const int endianTest = 1;
69 #define isLittleEndian (*((char *) &endianTest ) != 0)
73 static const char *IdSrc = "$Id$";
74 static const char *IdHdr = ID_OUTPUT;
76 // (stolen from FGFS native_fdm.cxx)
77 // The function htond is defined this way due to the way some
78 // processors and OSes treat floating point values. Some will raise
79 // an exception whenever a "bad" floating point value is loaded into a
80 // floating point register. Solaris is notorious for this, but then
81 // so is LynxOS on the PowerPC. By translating the data in place,
82 // there is no need to load a FP register with the "corruped" floating
83 // point value. By doing the BIG_ENDIAN test, I can optimize the
84 // routine for big-endian processors so it can be as efficient as
86 static void htond (double &x)
88 if ( isLittleEndian ) {
92 Double_Overlay = (int *) &x;
93 Holding_Buffer = Double_Overlay [0];
95 Double_Overlay [0] = htonl (Double_Overlay [1]);
96 Double_Overlay [1] = htonl (Holding_Buffer);
103 static void htonf (float &x)
105 if ( isLittleEndian ) {
109 Float_Overlay = (int *) &x;
110 Holding_Buffer = Float_Overlay [0];
112 Float_Overlay [0] = htonl (Holding_Buffer);
119 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
123 FGOutput::FGOutput(FGFDMExec* fdmex) : FGModel(fdmex)
126 sFirstPass = dFirstPass = true;
128 flightGearSocket = 0;
135 output_file_name = "";
137 memset(&fgSockBuf, 0x00, sizeof(fgSockBuf));
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 FGOutput::~FGOutput()
147 delete flightGearSocket;
148 OutputProperties.clear();
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 bool FGOutput::InitModel(void)
156 if (!FGModel::InitModel()) return false;
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 bool FGOutput::Run(void)
165 if (FGModel::Run()) return true;
167 if (enabled && !State->IntegrationSuspended()&& !FDMExec->Holding()) {
168 if (Type == otSocket) {
170 } else if (Type == otFlightGear) {
171 FlightGearSocketOutput();
172 } else if (Type == otCSV || Type == otTab) {
173 DelimitedOutput(Filename);
174 } else if (Type == otTerminal) {
176 } else if (Type == otNone) {
179 // Not a valid type of output
185 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 void FGOutput::SetType(string type)
192 } else if (type == "TABULAR") {
195 } else if (type == "SOCKET") {
197 } else if (type == "FLIGHTGEAR") {
199 } else if (type == "TERMINAL") {
201 } else if (type != string("NONE")) {
203 cerr << "Unknown type of output specified in config file" << endl;
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 void FGOutput::DelimitedOutput(string fname)
214 if (fname == "COUT" || fname == "cout") {
215 buffer = cout.rdbuf();
217 datafile.open(fname.c_str());
218 buffer = datafile.rdbuf();
221 ostream outstream(buffer);
223 outstream.precision(10);
227 if (SubSystems & ssSimulation) {
230 if (SubSystems & ssAerosurfaces) {
231 outstream << delimeter;
232 outstream << "Aileron Command (norm)" + delimeter;
233 outstream << "Elevator Command (norm)" + delimeter;
234 outstream << "Rudder Command (norm)" + delimeter;
235 outstream << "Flap Command (norm)" + delimeter;
236 outstream << "Left Aileron Position (deg)" + delimeter;
237 outstream << "Right Aileron Position (deg)" + delimeter;
238 outstream << "Elevator Position (deg)" + delimeter;
239 outstream << "Rudder Position (deg)" + delimeter;
240 outstream << "Flap Position (deg)";
242 if (SubSystems & ssRates) {
243 outstream << delimeter;
244 outstream << "P (deg/s)" + delimeter + "Q (deg/s)" + delimeter + "R (deg/s)" + delimeter;
245 outstream << "P dot (deg/s^2)" + delimeter + "Q dot (deg/s^2)" + delimeter + "R dot (deg/s^2)";
247 if (SubSystems & ssVelocities) {
248 outstream << delimeter;
249 outstream << "q bar (psf)" + delimeter;
250 outstream << "V_{Total} (ft/s)" + delimeter;
251 outstream << "V_{Inertial} (ft/s)" + delimeter;
252 outstream << "UBody" + delimeter + "VBody" + delimeter + "WBody" + delimeter;
253 outstream << "Aero V_{X Body} (ft/s)" + delimeter + "Aero V_{Y Body} (ft/s)" + delimeter + "Aero V_{Z Body} (ft/s)" + delimeter;
254 outstream << "V_{North} (ft/s)" + delimeter + "V_{East} (ft/s)" + delimeter + "V_{Down} (ft/s)";
256 if (SubSystems & ssForces) {
257 outstream << delimeter;
258 outstream << "F_{Drag} (lbs)" + delimeter + "F_{Side} (lbs)" + delimeter + "F_{Lift} (lbs)" + delimeter;
259 outstream << "L/D" + delimeter;
260 outstream << "F_X (lbs)" + delimeter + "F_Y (lbs)" + delimeter + "F_Z (lbs)";
262 if (SubSystems & ssMoments) {
263 outstream << delimeter;
264 outstream << "L (ft-lbs)" + delimeter + "M (ft-lbs)" + delimeter + "N (ft-lbs)";
266 if (SubSystems & ssAtmosphere) {
267 outstream << delimeter;
268 outstream << "Rho (slugs/ft^3)" + delimeter;
269 outstream << "P_{SL} (psf)" + delimeter;
270 outstream << "P_{Ambient} (psf)" + delimeter;
271 outstream << "Turbulence Magnitude (ft/sec)" + delimeter;
272 outstream << "Turbulence X Direction (rad)" + delimeter + "Turbulence Y Direction (rad)" + delimeter + "Turbulence Z Direction (rad)" + delimeter;
273 outstream << "Wind V_{North} (ft/s)" + delimeter + "Wind V_{East} (ft/s)" + delimeter + "Wind V_{Down} (ft/s)";
275 if (SubSystems & ssMassProps) {
276 outstream << delimeter;
277 outstream << "I_xx" + delimeter;
278 outstream << "I_xy" + delimeter;
279 outstream << "I_xz" + delimeter;
280 outstream << "I_yx" + delimeter;
281 outstream << "I_yy" + delimeter;
282 outstream << "I_yz" + delimeter;
283 outstream << "I_zx" + delimeter;
284 outstream << "I_zy" + delimeter;
285 outstream << "I_zz" + delimeter;
286 outstream << "Mass" + delimeter;
287 outstream << "X_cg" + delimeter + "Y_cg" + delimeter + "Z_cg";
289 if (SubSystems & ssPropagate) {
290 outstream << delimeter;
291 outstream << "Altitude (ft)" + delimeter;
292 outstream << "Phi (deg)" + delimeter + "Theta (deg)" + delimeter + "Psi (deg)" + delimeter;
293 outstream << "Alpha (deg)" + delimeter;
294 outstream << "Beta (deg)" + delimeter;
295 outstream << "Latitude (deg)" + delimeter;
296 outstream << "Longitude (deg)" + delimeter;
297 outstream << "ECEF X (ft)" + delimeter + "ECEF Y (ft)" + delimeter + "ECEF Z (ft)" + delimeter;
298 outstream << "EPA (deg)" + delimeter;
299 outstream << "Distance AGL (ft)" + delimeter;
300 outstream << "Runway Radius (ft)";
302 if (SubSystems & ssCoefficients) {
303 scratch = Aerodynamics->GetCoefficientStrings(delimeter);
304 if (scratch.length() != 0) outstream << delimeter << scratch;
306 if (SubSystems & ssFCS) {
307 scratch = FCS->GetComponentStrings(delimeter);
308 if (scratch.length() != 0) outstream << delimeter << scratch;
310 if (SubSystems & ssGroundReactions) {
311 outstream << delimeter;
312 outstream << GroundReactions->GetGroundReactionStrings(delimeter);
314 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
315 outstream << delimeter;
316 outstream << Propulsion->GetPropulsionStrings(delimeter);
318 if (OutputProperties.size() > 0) {
319 for (unsigned int i=0;i<OutputProperties.size();i++) {
320 outstream << delimeter << OutputProperties[i]->GetPrintableName();
328 outstream << State->Getsim_time();
329 if (SubSystems & ssSimulation) {
331 if (SubSystems & ssAerosurfaces) {
332 outstream << delimeter;
333 outstream << FCS->GetDaCmd() << delimeter;
334 outstream << FCS->GetDeCmd() << delimeter;
335 outstream << FCS->GetDrCmd() << delimeter;
336 outstream << FCS->GetDfCmd() << delimeter;
337 outstream << FCS->GetDaLPos(ofDeg) << delimeter;
338 outstream << FCS->GetDaRPos(ofDeg) << delimeter;
339 outstream << FCS->GetDePos(ofDeg) << delimeter;
340 outstream << FCS->GetDrPos(ofDeg) << delimeter;
341 outstream << FCS->GetDfPos(ofDeg);
343 if (SubSystems & ssRates) {
344 outstream << delimeter;
345 outstream << (radtodeg*Propagate->GetPQR()).Dump(delimeter) << delimeter;
346 outstream << (radtodeg*Propagate->GetPQRdot()).Dump(delimeter);
348 if (SubSystems & ssVelocities) {
349 outstream << delimeter;
350 outstream << Auxiliary->Getqbar() << delimeter;
351 outstream << setprecision(12) << Auxiliary->GetVt() << delimeter;
352 outstream << Propagate->GetInertialVelocityMagnitude() << delimeter;
353 outstream << setprecision(12) << Propagate->GetUVW().Dump(delimeter) << delimeter;
354 outstream << Auxiliary->GetAeroUVW().Dump(delimeter) << delimeter;
355 outstream << Propagate->GetVel().Dump(delimeter);
357 if (SubSystems & ssForces) {
358 outstream << delimeter;
359 outstream << Aerodynamics->GetvFw() << delimeter;
360 outstream << Aerodynamics->GetLoD() << delimeter;
361 outstream << Aircraft->GetForces().Dump(delimeter);
363 if (SubSystems & ssMoments) {
364 outstream << delimeter;
365 outstream << Aircraft->GetMoments().Dump(delimeter);
367 if (SubSystems & ssAtmosphere) {
368 outstream << delimeter;
369 outstream << Atmosphere->GetDensity() << delimeter;
370 outstream << Atmosphere->GetPressureSL() << delimeter;
371 outstream << Atmosphere->GetPressure() << delimeter;
372 outstream << Atmosphere->GetTurbMagnitude() << delimeter;
373 outstream << Atmosphere->GetTurbDirection().Dump(delimeter) << delimeter;
374 outstream << Atmosphere->GetTotalWindNED().Dump(delimeter);
376 if (SubSystems & ssMassProps) {
377 outstream << delimeter;
378 outstream << MassBalance->GetJ() << delimeter;
379 outstream << MassBalance->GetMass() << delimeter;
380 outstream << MassBalance->GetXYZcg();
382 if (SubSystems & ssPropagate) {
383 outstream << delimeter;
384 outstream << Propagate->Geth() << delimeter;
385 outstream << (radtodeg*Propagate->GetEuler()).Dump(delimeter) << delimeter;
386 outstream << Auxiliary->Getalpha(inDegrees) << delimeter;
387 outstream << Auxiliary->Getbeta(inDegrees) << delimeter;
388 outstream << Propagate->GetLocation().GetLatitudeDeg() << delimeter;
389 outstream << Propagate->GetLocation().GetLongitudeDeg() << delimeter;
390 outstream << ((FGColumnVector3)Propagate->GetLocation()).Dump(delimeter) << delimeter;
391 outstream << Inertial->GetEarthPositionAngleDeg() << delimeter;
392 outstream << Propagate->GetDistanceAGL() << delimeter;
393 outstream << Propagate->GetRunwayRadius();
395 if (SubSystems & ssCoefficients) {
396 scratch = Aerodynamics->GetCoefficientValues(delimeter);
397 if (scratch.length() != 0) outstream << delimeter << scratch;
399 if (SubSystems & ssFCS) {
400 scratch = FCS->GetComponentValues(delimeter);
401 if (scratch.length() != 0) outstream << delimeter << scratch;
403 if (SubSystems & ssGroundReactions) {
404 outstream << delimeter;
405 outstream << GroundReactions->GetGroundReactionValues(delimeter);
407 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
408 outstream << delimeter;
409 outstream << Propulsion->GetPropulsionValues(delimeter);
412 for (unsigned int i=0;i<OutputProperties.size();i++) {
413 outstream << delimeter << OutputProperties[i]->getDoubleValue();
420 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 void FGOutput::SocketDataFill(FGNetFDM* net)
427 net->version = FG_NET_FDM_VERSION;
430 net->longitude = Propagate->GetLocation().GetLongitude(); // geodetic (radians)
431 net->latitude = Propagate->GetLocation().GetLatitude(); // geodetic (radians)
432 net->altitude = Propagate->Geth()*0.3048; // altitude, above sea level (meters)
433 net->agl = (float)(Propagate->GetDistanceAGL()*0.3048); // altitude, above ground level (meters)
435 net->phi = (float)(Propagate->GetEuler(ePhi)); // roll (radians)
436 net->theta = (float)(Propagate->GetEuler(eTht)); // pitch (radians)
437 net->psi = (float)(Propagate->GetEuler(ePsi)); // yaw or true heading (radians)
439 net->alpha = (float)(Auxiliary->Getalpha()); // angle of attack (radians)
440 net->beta = (float)(Auxiliary->Getbeta()); // side slip angle (radians)
443 net->phidot = (float)(Auxiliary->GetEulerRates(ePhi)); // roll rate (radians/sec)
444 net->thetadot = (float)(Auxiliary->GetEulerRates(eTht)); // pitch rate (radians/sec)
445 net->psidot = (float)(Auxiliary->GetEulerRates(ePsi)); // yaw rate (radians/sec)
446 net->vcas = (float)(Auxiliary->GetVcalibratedFPS()); // VCAS, ft/sec
447 net->climb_rate = (float)(Propagate->Gethdot()); // altitude rate, ft/sec
448 net->v_north = (float)(Propagate->GetVel(eNorth)); // north vel in NED frame, fps
449 net->v_east = (float)(Propagate->GetVel(eEast)); // east vel in NED frame, fps
450 net->v_down = (float)(Propagate->GetVel(eDown)); // down vel in NED frame, fps
451 //---ADD METHOD TO CALCULATE THESE TERMS---
452 net->v_wind_body_north = (float)(Propagate->GetVel(eNorth)); // north vel in NED relative to airmass, fps
453 net->v_wind_body_east = (float)(Propagate->GetVel(eEast)); // east vel in NED relative to airmass, fps
454 net->v_wind_body_down = (float)(Propagate->GetVel(eDown)); // down vel in NED relative to airmass, fps
457 net->A_X_pilot = (float)(Auxiliary->GetPilotAccel(1)); // X body accel, ft/s/s
458 net->A_Y_pilot = (float)(Auxiliary->GetPilotAccel(2)); // Y body accel, ft/s/s
459 net->A_Z_pilot = (float)(Auxiliary->GetPilotAccel(3)); // Z body accel, ft/s/s
462 net->stall_warning = 0.0; // 0.0 - 1.0 indicating the amount of stall
463 net->slip_deg = (float)(Auxiliary->Getbeta(inDegrees)); // slip ball deflection, deg
466 net->num_engines = Propulsion->GetNumEngines(); // Number of valid engines
468 for (i=0; i<net->num_engines; i++) {
469 if (Propulsion->GetEngine(i)->GetRunning())
470 net->eng_state[i] = 2; // Engine state running
471 else if (Propulsion->GetEngine(i)->GetCranking())
472 net->eng_state[i] = 1; // Engine state cranking
474 net->eng_state[i] = 0; // Engine state off
476 switch (Propulsion->GetEngine(i)->GetType()) {
477 case (FGEngine::etRocket):
479 case (FGEngine::etPiston):
480 net->rpm[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getRPM());
481 net->fuel_flow[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getFuelFlow_gph());
482 net->fuel_px[i] = 0; // Fuel pressure, psi (N/A in current model)
483 net->egt[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->GetEGT());
484 net->cht[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getCylinderHeadTemp_degF());
485 net->mp_osi[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getManifoldPressure_inHg());
486 net->oil_temp[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getOilTemp_degF());
487 net->oil_px[i] = (float)(((FGPiston *)Propulsion->GetEngine(i))->getOilPressure_psi());
488 net->tit[i] = 0; // Turbine Inlet Temperature (N/A for piston)
490 case (FGEngine::etTurbine):
492 case (FGEngine::etTurboprop):
494 case (FGEngine::etElectric):
496 case (FGEngine::etUnknown):
503 net->num_tanks = Propulsion->GetNumTanks(); // Max number of fuel tanks
505 for (i=0; i<net->num_tanks; i++) {
506 net->fuel_quantity[i] = (float)(((FGTank *)Propulsion->GetTank(i))->GetContents());
511 net->num_wheels = GroundReactions->GetNumGearUnits();
513 for (i=0; i<net->num_wheels; i++) {
514 net->wow[i] = GroundReactions->GetGearUnit(i)->GetWOW();
515 if (GroundReactions->GetGearUnit(i)->GetGearUnitDown())
516 net->gear_pos[i] = 1; //gear down, using FCS convention
518 net->gear_pos[i] = 0; //gear up, using FCS convention
519 net->gear_steer[i] = (float)(GroundReactions->GetGearUnit(i)->GetSteerNorm());
520 net->gear_compression[i] = (float)(GroundReactions->GetGearUnit(i)->GetCompLen());
525 net->cur_time = (long int)1234567890; // Friday, Feb 13, 2009, 23:31:30 UTC (not processed by FGFS anyway)
526 net->warp = 0; // offset in seconds to unix time
527 net->visibility = 25000.0; // visibility in meters (for env. effects)
530 // Control surface positions (normalized values)
531 net->elevator = (float)(FCS->GetDePos(ofNorm)); // Norm Elevator Pos, --
532 net->elevator_trim_tab = (float)(FCS->GetPitchTrimCmd()); // Norm Elev Trim Tab Pos, --
533 net->left_flap = (float)(FCS->GetDfPos(ofNorm)); // Norm Flap Pos, --
534 net->right_flap = (float)(FCS->GetDfPos(ofNorm)); // Norm Flap Pos, --
535 net->left_aileron = (float)(FCS->GetDaLPos(ofNorm)); // Norm L Aileron Pos, --
536 net->right_aileron = (float)(FCS->GetDaRPos(ofNorm)); // Norm R Aileron Pos, --
537 net->rudder = (float)(FCS->GetDrPos(ofNorm)); // Norm Rudder Pos, --
538 net->nose_wheel = (float)(FCS->GetDrPos(ofNorm)); // *** FIX *** Using Rudder Pos for NWS, --
539 net->speedbrake = (float)(FCS->GetDsbPos(ofNorm)); // Norm Speedbrake Pos, --
540 net->spoilers = (float)(FCS->GetDspPos(ofNorm)); // Norm Spoiler Pos, --
543 // Convert the net buffer to network format
544 if ( isLittleEndian ) {
545 net->version = htonl(net->version);
547 htond(net->longitude);
548 htond(net->latitude);
549 htond(net->altitude);
558 htonf(net->thetadot);
561 htonf(net->climb_rate);
565 htonf(net->v_wind_body_north);
566 htonf(net->v_wind_body_east);
567 htonf(net->v_wind_body_down);
569 htonf(net->A_X_pilot);
570 htonf(net->A_Y_pilot);
571 htonf(net->A_Z_pilot);
573 htonf(net->stall_warning);
574 htonf(net->slip_deg);
576 for (i=0; i<net->num_engines; ++i ) {
577 net->eng_state[i] = htonl(net->eng_state[i]);
579 htonf(net->fuel_flow[i]);
580 htonf(net->fuel_px[i]);
583 htonf(net->mp_osi[i]);
585 htonf(net->oil_temp[i]);
586 htonf(net->oil_px[i]);
588 net->num_engines = htonl(net->num_engines);
590 for (i=0; i<net->num_tanks; ++i ) {
591 htonf(net->fuel_quantity[i]);
593 net->num_tanks = htonl(net->num_tanks);
595 for (i=0; i<net->num_wheels; ++i ) {
596 net->wow[i] = htonl(net->wow[i]);
597 htonf(net->gear_pos[i]);
598 htonf(net->gear_steer[i]);
599 htonf(net->gear_compression[i]);
601 net->num_wheels = htonl(net->num_wheels);
603 net->cur_time = htonl( net->cur_time );
604 net->warp = htonl( net->warp );
605 htonf(net->visibility);
607 htonf(net->elevator);
608 htonf(net->elevator_trim_tab);
609 htonf(net->left_flap);
610 htonf(net->right_flap);
611 htonf(net->left_aileron);
612 htonf(net->right_aileron);
614 htonf(net->nose_wheel);
615 htonf(net->speedbrake);
616 htonf(net->spoilers);
620 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622 void FGOutput::FlightGearSocketOutput(void)
624 int length = sizeof(fgSockBuf);
627 if (flightGearSocket == NULL) return;
628 if (!flightGearSocket->GetConnectStatus()) return;
630 SocketDataFill(&fgSockBuf);
631 flightGearSocket->Send((char *)&fgSockBuf, length);
634 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 void FGOutput::SocketOutput(void)
638 string asciiData, scratch;
640 if (socket == NULL) return;
641 if (!socket->GetConnectStatus()) return;
645 socket->Clear("<LABELS>");
646 socket->Append("Time");
648 if (SubSystems & ssAerosurfaces) {
649 socket->Append("Aileron Command");
650 socket->Append("Elevator Command");
651 socket->Append("Rudder Command");
652 socket->Append("Flap Command");
653 socket->Append("Left Aileron Position");
654 socket->Append("Right Aileron Position");
655 socket->Append("Elevator Position");
656 socket->Append("Rudder Position");
657 socket->Append("Flap Position");
660 if (SubSystems & ssRates) {
664 socket->Append("PDot");
665 socket->Append("QDot");
666 socket->Append("RDot");
669 if (SubSystems & ssVelocities) {
670 socket->Append("QBar");
671 socket->Append("Vtotal");
672 socket->Append("UBody");
673 socket->Append("VBody");
674 socket->Append("WBody");
675 socket->Append("UAero");
676 socket->Append("VAero");
677 socket->Append("WAero");
678 socket->Append("Vn");
679 socket->Append("Ve");
680 socket->Append("Vd");
682 if (SubSystems & ssForces) {
683 socket->Append("F_Drag");
684 socket->Append("F_Side");
685 socket->Append("F_Lift");
686 socket->Append("LoD");
687 socket->Append("Fx");
688 socket->Append("Fy");
689 socket->Append("Fz");
691 if (SubSystems & ssMoments) {
696 if (SubSystems & ssAtmosphere) {
697 socket->Append("Rho");
698 socket->Append("SL pressure");
699 socket->Append("Ambient pressure");
700 socket->Append("Turbulence Magnitude");
701 socket->Append("Turbulence Direction X");
702 socket->Append("Turbulence Direction Y");
703 socket->Append("Turbulence Direction Z");
704 socket->Append("NWind");
705 socket->Append("EWind");
706 socket->Append("DWind");
708 if (SubSystems & ssMassProps) {
709 socket->Append("Ixx");
710 socket->Append("Ixy");
711 socket->Append("Ixz");
712 socket->Append("Iyx");
713 socket->Append("Iyy");
714 socket->Append("Iyz");
715 socket->Append("Izx");
716 socket->Append("Izy");
717 socket->Append("Izz");
718 socket->Append("Mass");
719 socket->Append("Xcg");
720 socket->Append("Ycg");
721 socket->Append("Zcg");
723 if (SubSystems & ssPropagate) {
724 socket->Append("Altitude");
725 socket->Append("Phi (deg)");
726 socket->Append("Tht (deg)");
727 socket->Append("Psi (deg)");
728 socket->Append("Alpha (deg)");
729 socket->Append("Beta (deg)");
730 socket->Append("Latitude (deg)");
731 socket->Append("Longitude (deg)");
733 if (SubSystems & ssCoefficients) {
734 scratch = Aerodynamics->GetCoefficientStrings(",");
735 if (scratch.length() != 0) socket->Append(scratch);
737 if (SubSystems & ssFCS) {
738 scratch = FCS->GetComponentStrings(",");
739 if (scratch.length() != 0) socket->Append(scratch);
741 if (SubSystems & ssGroundReactions) {
742 socket->Append(GroundReactions->GetGroundReactionStrings(","));
744 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
745 socket->Append(Propulsion->GetPropulsionStrings(","));
747 if (OutputProperties.size() > 0) {
748 for (unsigned int i=0;i<OutputProperties.size();i++) {
749 socket->Append(OutputProperties[i]->GetPrintableName());
758 socket->Append(State->Getsim_time());
760 if (SubSystems & ssAerosurfaces) {
761 socket->Append(FCS->GetDaCmd());
762 socket->Append(FCS->GetDeCmd());
763 socket->Append(FCS->GetDrCmd());
764 socket->Append(FCS->GetDfCmd());
765 socket->Append(FCS->GetDaLPos());
766 socket->Append(FCS->GetDaRPos());
767 socket->Append(FCS->GetDePos());
768 socket->Append(FCS->GetDrPos());
769 socket->Append(FCS->GetDfPos());
771 if (SubSystems & ssRates) {
772 socket->Append(radtodeg*Propagate->GetPQR(eP));
773 socket->Append(radtodeg*Propagate->GetPQR(eQ));
774 socket->Append(radtodeg*Propagate->GetPQR(eR));
775 socket->Append(radtodeg*Propagate->GetPQRdot(eP));
776 socket->Append(radtodeg*Propagate->GetPQRdot(eQ));
777 socket->Append(radtodeg*Propagate->GetPQRdot(eR));
779 if (SubSystems & ssVelocities) {
780 socket->Append(Auxiliary->Getqbar());
781 socket->Append(Auxiliary->GetVt());
782 socket->Append(Propagate->GetUVW(eU));
783 socket->Append(Propagate->GetUVW(eV));
784 socket->Append(Propagate->GetUVW(eW));
785 socket->Append(Auxiliary->GetAeroUVW(eU));
786 socket->Append(Auxiliary->GetAeroUVW(eV));
787 socket->Append(Auxiliary->GetAeroUVW(eW));
788 socket->Append(Propagate->GetVel(eNorth));
789 socket->Append(Propagate->GetVel(eEast));
790 socket->Append(Propagate->GetVel(eDown));
792 if (SubSystems & ssForces) {
793 socket->Append(Aerodynamics->GetvFw()(eDrag));
794 socket->Append(Aerodynamics->GetvFw()(eSide));
795 socket->Append(Aerodynamics->GetvFw()(eLift));
796 socket->Append(Aerodynamics->GetLoD());
797 socket->Append(Aircraft->GetForces(eX));
798 socket->Append(Aircraft->GetForces(eY));
799 socket->Append(Aircraft->GetForces(eZ));
801 if (SubSystems & ssMoments) {
802 socket->Append(Aircraft->GetMoments(eL));
803 socket->Append(Aircraft->GetMoments(eM));
804 socket->Append(Aircraft->GetMoments(eN));
806 if (SubSystems & ssAtmosphere) {
807 socket->Append(Atmosphere->GetDensity());
808 socket->Append(Atmosphere->GetPressureSL());
809 socket->Append(Atmosphere->GetPressure());
810 socket->Append(Atmosphere->GetTurbMagnitude());
811 socket->Append(Atmosphere->GetTurbDirection().Dump(","));
812 socket->Append(Atmosphere->GetTotalWindNED().Dump(","));
814 if (SubSystems & ssMassProps) {
815 socket->Append(MassBalance->GetJ()(1,1));
816 socket->Append(MassBalance->GetJ()(1,2));
817 socket->Append(MassBalance->GetJ()(1,3));
818 socket->Append(MassBalance->GetJ()(2,1));
819 socket->Append(MassBalance->GetJ()(2,2));
820 socket->Append(MassBalance->GetJ()(2,3));
821 socket->Append(MassBalance->GetJ()(3,1));
822 socket->Append(MassBalance->GetJ()(3,2));
823 socket->Append(MassBalance->GetJ()(3,3));
824 socket->Append(MassBalance->GetMass());
825 socket->Append(MassBalance->GetXYZcg()(eX));
826 socket->Append(MassBalance->GetXYZcg()(eY));
827 socket->Append(MassBalance->GetXYZcg()(eZ));
829 if (SubSystems & ssPropagate) {
830 socket->Append(Propagate->Geth());
831 socket->Append(radtodeg*Propagate->GetEuler(ePhi));
832 socket->Append(radtodeg*Propagate->GetEuler(eTht));
833 socket->Append(radtodeg*Propagate->GetEuler(ePsi));
834 socket->Append(Auxiliary->Getalpha(inDegrees));
835 socket->Append(Auxiliary->Getbeta(inDegrees));
836 socket->Append(Propagate->GetLocation().GetLatitudeDeg());
837 socket->Append(Propagate->GetLocation().GetLongitudeDeg());
839 if (SubSystems & ssCoefficients) {
840 scratch = Aerodynamics->GetCoefficientValues(",");
841 if (scratch.length() != 0) socket->Append(scratch);
843 if (SubSystems & ssFCS) {
844 scratch = FCS->GetComponentValues(",");
845 if (scratch.length() != 0) socket->Append(scratch);
847 if (SubSystems & ssGroundReactions) {
848 socket->Append(GroundReactions->GetGroundReactionValues(","));
850 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
851 socket->Append(Propulsion->GetPropulsionValues(","));
854 for (unsigned int i=0;i<OutputProperties.size();i++) {
855 socket->Append(OutputProperties[i]->getDoubleValue());
861 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 void FGOutput::SocketStatusOutput(string out_str)
867 if (socket == NULL) return;
870 asciiData = string("<STATUS>") + out_str;
871 socket->Append(asciiData.c_str());
875 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
877 bool FGOutput::Load(Element* element)
879 string type="", parameter="";
881 string protocol="tcp";
885 Element *property_element;
887 string separator = "/";
889 if (!DirectivesFile.empty()) { // A directives filename from the command line overrides
890 output_file_name = DirectivesFile; // one found in the config file.
891 document = LoadXMLDocument(output_file_name);
892 } else if (!element->GetAttributeValue("file").empty()) {
893 output_file_name = element->GetAttributeValue("file");
894 document = LoadXMLDocument(output_file_name);
899 name = document->GetAttributeValue("name");
900 type = document->GetAttributeValue("type");
902 if (!document->GetAttributeValue("port").empty() && type == string("SOCKET")) {
903 port = atoi(document->GetAttributeValue("port").c_str());
904 socket = new FGfdmSocket(name, port);
905 } else if (!document->GetAttributeValue("port").empty() && type == string("FLIGHTGEAR")) {
906 port = atoi(document->GetAttributeValue("port").c_str());
907 if (!document->GetAttributeValue("protocol").empty())
908 protocol = document->GetAttributeValue("protocol");
909 if (protocol == "udp")
910 flightGearSocket = new FGfdmSocket(name, port, FGfdmSocket::ptUDP); // create udp socket
912 flightGearSocket = new FGfdmSocket(name, port, FGfdmSocket::ptTCP); // create tcp socket (default)
916 if (!document->GetAttributeValue("rate").empty()) {
917 OutRate = (int)document->GetAttributeValueAsNumber("rate");
922 if (document->FindElementValue("simulation") == string("ON"))
923 SubSystems += ssSimulation;
924 if (document->FindElementValue("aerosurfaces") == string("ON"))
925 SubSystems += ssAerosurfaces;
926 if (document->FindElementValue("rates") == string("ON"))
927 SubSystems += ssRates;
928 if (document->FindElementValue("velocities") == string("ON"))
929 SubSystems += ssVelocities;
930 if (document->FindElementValue("forces") == string("ON"))
931 SubSystems += ssForces;
932 if (document->FindElementValue("moments") == string("ON"))
933 SubSystems += ssMoments;
934 if (document->FindElementValue("atmosphere") == string("ON"))
935 SubSystems += ssAtmosphere;
936 if (document->FindElementValue("massprops") == string("ON"))
937 SubSystems += ssMassProps;
938 if (document->FindElementValue("position") == string("ON"))
939 SubSystems += ssPropagate;
940 if (document->FindElementValue("coefficients") == string("ON"))
941 SubSystems += ssCoefficients;
942 if (document->FindElementValue("ground_reactions") == string("ON"))
943 SubSystems += ssGroundReactions;
944 if (document->FindElementValue("fcs") == string("ON"))
946 if (document->FindElementValue("propulsion") == string("ON"))
947 SubSystems += ssPropulsion;
948 property_element = document->FindElement("property");
949 while (property_element) {
950 string property_str = property_element->GetDataLine();
951 FGPropertyManager* node = PropertyManager->GetNode(property_str);
953 cerr << fgred << highint << endl << " No property by the name "
954 << property_str << " has been defined. This property will " << endl
955 << " not be logged. You should check your configuration file."
958 OutputProperties.push_back(node);
960 property_element = document->FindNextElement("property");
963 OutRate = OutRate>1000?1000:(OutRate<0?0:OutRate);
964 rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
971 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972 // The bitmasked value choices are as follows:
973 // unset: In this case (the default) JSBSim would only print
974 // out the normally expected messages, essentially echoing
975 // the config files as they are read. If the environment
976 // variable is not set, debug_lvl is set to 1 internally
977 // 0: This requests JSBSim not to output any messages
979 // 1: This value explicity requests the normal JSBSim
981 // 2: This value asks for a message to be printed out when
982 // a class is instantiated
983 // 4: When this value is set, a message is displayed when a
984 // FGModel object executes its Run() method
985 // 8: When this value is set, various runtime state variables
986 // are printed out periodically
987 // 16: When set various parameters are sanity checked and
988 // a message is printed out when they go out of bounds
990 void FGOutput::Debug(int from)
994 if (debug_lvl <= 0) return;
996 if (debug_lvl & 1) { // Standard console startup message output
997 if (from == 0) { // Constructor
1001 if (output_file_name.empty())
1002 cout << " " << "Output parameters read inline" << endl;
1004 cout << " Output parameters read from file: " << output_file_name << endl;
1006 if (Filename == "cout" || Filename == "COUT") {
1007 scratch = " Log output goes to screen console";
1008 } else if (!Filename.empty()) {
1009 scratch = " Log output goes to file: " + Filename;
1013 cout << scratch << " in CSV format output at rate " << 1/(State->Getdt()*rate) << " Hz" << endl;
1016 cout << " No log output" << endl;
1020 if (SubSystems & ssSimulation) cout << " Simulation parameters logged" << endl;
1021 if (SubSystems & ssAerosurfaces) cout << " Aerosurface parameters logged" << endl;
1022 if (SubSystems & ssRates) cout << " Rate parameters logged" << endl;
1023 if (SubSystems & ssVelocities) cout << " Velocity parameters logged" << endl;
1024 if (SubSystems & ssForces) cout << " Force parameters logged" << endl;
1025 if (SubSystems & ssMoments) cout << " Moments parameters logged" << endl;
1026 if (SubSystems & ssAtmosphere) cout << " Atmosphere parameters logged" << endl;
1027 if (SubSystems & ssMassProps) cout << " Mass parameters logged" << endl;
1028 if (SubSystems & ssCoefficients) cout << " Coefficient parameters logged" << endl;
1029 if (SubSystems & ssPropagate) cout << " Propagate parameters logged" << endl;
1030 if (SubSystems & ssGroundReactions) cout << " Ground parameters logged" << endl;
1031 if (SubSystems & ssFCS) cout << " FCS parameters logged" << endl;
1032 if (SubSystems & ssPropulsion) cout << " Propulsion parameters logged" << endl;
1033 if (OutputProperties.size() > 0) cout << " Properties logged:" << endl;
1034 for (unsigned int i=0;i<OutputProperties.size();i++) {
1035 cout << " - " << OutputProperties[i]->GetName() << endl;
1039 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1040 if (from == 0) cout << "Instantiated: FGOutput" << endl;
1041 if (from == 1) cout << "Destroyed: FGOutput" << endl;
1043 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1045 if (debug_lvl & 8 ) { // Runtime state variables
1047 if (debug_lvl & 16) { // Sanity checking
1049 if (debug_lvl & 64) {
1050 if (from == 0) { // Constructor
1051 cout << IdSrc << endl;
1052 cout << IdHdr << endl;