]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGOutput.cpp
Sync. with JSBSim CVS (header cleanups).
[flightgear.git] / src / FDM / JSBSim / models / FGOutput.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGOutput.cpp
4  Author:       Jon Berndt
5  Date started: 12/02/98
6  Purpose:      Manage output of sim parameters to file or stdout
7  Called by:    FGSimExec
8
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
10
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
14  version.
15
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
19  details.
20
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.
24
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.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This is the place where you create output routines to dump data for perusal
31 later.
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 12/02/98   JSB   Created
36 11/09/07   HDW   Added FlightGear Socket Interface
37
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 INCLUDES
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42 #include "FGOutput.h"
43 #include "FGState.h"
44 #include "FGFDMExec.h"
45 #include "FGAtmosphere.h"
46 #include "FGFCS.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"
56 #include <fstream>
57 #include <iomanip>
58 #include <cstring>
59
60 #include "input_output/net_fdm.hxx"
61
62 #if defined(WIN32) && !defined(__CYGWIN__)
63 #  include <windows.h>
64 #else
65 #  include <netinet/in.h>       // htonl() ntohl()
66 #endif
67
68 static const int endianTest = 1;
69 #define isLittleEndian (*((char *) &endianTest ) != 0)
70
71 namespace JSBSim {
72
73 static const char *IdSrc = "$Id$";
74 static const char *IdHdr = ID_OUTPUT;
75
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
85 // possible
86 static void htond (double &x)
87 {
88     if ( isLittleEndian ) {
89         int    *Double_Overlay;
90         int     Holding_Buffer;
91
92         Double_Overlay = (int *) &x;
93         Holding_Buffer = Double_Overlay [0];
94
95         Double_Overlay [0] = htonl (Double_Overlay [1]);
96         Double_Overlay [1] = htonl (Holding_Buffer);
97     } else {
98         return;
99     }
100 }
101
102 // Float version
103 static void htonf (float &x)
104 {
105     if ( isLittleEndian ) {
106         int    *Float_Overlay;
107         int     Holding_Buffer;
108
109         Float_Overlay = (int *) &x;
110         Holding_Buffer = Float_Overlay [0];
111
112         Float_Overlay [0] = htonl (Holding_Buffer);
113     } else {
114         return;
115     }
116 }
117
118
119 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 CLASS IMPLEMENTATION
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
122
123 FGOutput::FGOutput(FGFDMExec* fdmex) : FGModel(fdmex)
124 {
125   Name = "FGOutput";
126   sFirstPass = dFirstPass = true;
127   socket = 0;
128   flightGearSocket = 0;
129   Type = otNone;
130   SubSystems = 0;
131   enabled = true;
132   delimeter = ", ";
133   Filename = "";
134   DirectivesFile = "";
135   output_file_name = "";
136
137   memset(&fgSockBuf, 0x00, sizeof(fgSockBuf));
138
139   Debug(0);
140 }
141
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143
144 FGOutput::~FGOutput()
145 {
146   delete socket;
147   delete flightGearSocket;
148   OutputProperties.clear();
149   Debug(1);
150 }
151
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153
154 bool FGOutput::InitModel(void)
155 {
156   if (!FGModel::InitModel()) return false;
157
158   return true;
159 }
160
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162
163 bool FGOutput::Run(void)
164 {
165   if (FGModel::Run()) return true;
166
167   if (enabled && !State->IntegrationSuspended()&& !FDMExec->Holding()) {
168     if (Type == otSocket) {
169       SocketOutput();
170     } else if (Type == otFlightGear) {
171       FlightGearSocketOutput();
172     } else if (Type == otCSV || Type == otTab) {
173       DelimitedOutput(Filename);
174     } else if (Type == otTerminal) {
175       // Not done yet
176     } else if (Type == otNone) {
177       // Do nothing
178     } else {
179       // Not a valid type of output
180     }
181   }
182   return false;
183 }
184
185 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186
187 void FGOutput::SetType(string type)
188 {
189   if (type == "CSV") {
190     Type = otCSV;
191     delimeter = ", ";
192   } else if (type == "TABULAR") {
193     Type = otTab;
194     delimeter = "\t";
195   } else if (type == "SOCKET") {
196     Type = otSocket;
197   } else if (type == "FLIGHTGEAR") {
198     Type = otFlightGear;
199   } else if (type == "TERMINAL") {
200     Type = otTerminal;
201   } else if (type != string("NONE")) {
202     Type = otUnknown;
203     cerr << "Unknown type of output specified in config file" << endl;
204   }
205 }
206
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208
209 void FGOutput::DelimitedOutput(string fname)
210 {
211   streambuf* buffer;
212   string scratch = "";
213
214   if (fname == "COUT" || fname == "cout") {
215     buffer = cout.rdbuf();
216   } else {
217     datafile.open(fname.c_str());
218     buffer = datafile.rdbuf();
219   }
220
221   ostream outstream(buffer);
222
223   outstream.precision(10);
224
225   if (dFirstPass) {
226     outstream << "Time";
227     if (SubSystems & ssSimulation) {
228       // Nothing here, yet
229     }
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)";
241     }
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)";
246     }
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)";
255     }
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)";
261     }
262     if (SubSystems & ssMoments) {
263       outstream << delimeter;
264       outstream << "L (ft-lbs)" + delimeter + "M (ft-lbs)" + delimeter + "N (ft-lbs)";
265     }
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)";
274     }
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";
288     }
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)";
301     }
302     if (SubSystems & ssCoefficients) {
303       scratch = Aerodynamics->GetCoefficientStrings(delimeter);
304       if (scratch.length() != 0) outstream << delimeter << scratch;
305     }
306     if (SubSystems & ssFCS) {
307       scratch = FCS->GetComponentStrings(delimeter);
308       if (scratch.length() != 0) outstream << delimeter << scratch;
309     }
310     if (SubSystems & ssGroundReactions) {
311       outstream << delimeter;
312       outstream << GroundReactions->GetGroundReactionStrings(delimeter);
313     }
314     if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
315       outstream << delimeter;
316       outstream << Propulsion->GetPropulsionStrings(delimeter);
317     }
318     if (OutputProperties.size() > 0) {
319       for (unsigned int i=0;i<OutputProperties.size();i++) {
320         outstream << delimeter << OutputProperties[i]->GetPrintableName();
321       }
322     }
323
324     outstream << endl;
325     dFirstPass = false;
326   }
327
328   outstream << State->Getsim_time();
329   if (SubSystems & ssSimulation) {
330   }
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);
342   }
343   if (SubSystems & ssRates) {
344     outstream << delimeter;
345     outstream << (radtodeg*Propagate->GetPQR()).Dump(delimeter) << delimeter;
346     outstream << (radtodeg*Propagate->GetPQRdot()).Dump(delimeter);
347   }
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);
356   }
357   if (SubSystems & ssForces) {
358     outstream << delimeter;
359     outstream << Aerodynamics->GetvFw() << delimeter;
360     outstream << Aerodynamics->GetLoD() << delimeter;
361     outstream << Aircraft->GetForces().Dump(delimeter);
362   }
363   if (SubSystems & ssMoments) {
364     outstream << delimeter;
365     outstream << Aircraft->GetMoments().Dump(delimeter);
366   }
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->GetWindNED().Dump(delimeter);
375   }
376   if (SubSystems & ssMassProps) {
377     outstream << delimeter;
378     outstream << MassBalance->GetJ() << delimeter;
379     outstream << MassBalance->GetMass() << delimeter;
380     outstream << MassBalance->GetXYZcg();
381   }
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();
394   }
395   if (SubSystems & ssCoefficients) {
396     scratch = Aerodynamics->GetCoefficientValues(delimeter);
397     if (scratch.length() != 0) outstream << delimeter << scratch;
398   }
399   if (SubSystems & ssFCS) {
400     scratch = FCS->GetComponentValues(delimeter);
401     if (scratch.length() != 0) outstream << delimeter << scratch;
402   }
403   if (SubSystems & ssGroundReactions) {
404     outstream << delimeter;
405     outstream << GroundReactions->GetGroundReactionValues(delimeter);
406   }
407   if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
408     outstream << delimeter;
409     outstream << Propulsion->GetPropulsionValues(delimeter);
410   }
411
412   for (unsigned int i=0;i<OutputProperties.size();i++) {
413     outstream << delimeter << OutputProperties[i]->getDoubleValue();
414   }
415
416   outstream << endl;
417   outstream.flush();
418 }
419
420 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421
422 void FGOutput::SocketDataFill(FGNetFDM* net)
423 {
424     unsigned int i;
425
426     // Version
427     net->version = FG_NET_FDM_VERSION;
428
429     // Positions
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)
434
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)
438
439     net->alpha     = (float)(Auxiliary->Getalpha()); // angle of attack (radians)
440     net->beta      = (float)(Auxiliary->Getbeta()); // side slip angle (radians)
441
442     // Velocities
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
455
456     // Accelerations
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
460
461     // Stall
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
464
465     // Engine status
466     net->num_engines = Propulsion->GetNumEngines(); // Number of valid engines
467
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
473        else
474           net->eng_state[i] = 0;       // Engine state off
475
476        switch (Propulsion->GetEngine(i)->GetType()) {
477        case (FGEngine::etRocket):
478        break;
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)
489        break;
490        case (FGEngine::etTurbine):
491        break;
492        case (FGEngine::etTurboprop):
493        break;
494        case (FGEngine::etElectric):
495        break;
496        case (FGEngine::etUnknown):
497        break;
498        }
499     }
500
501
502     // Consumables
503     net->num_tanks = Propulsion->GetNumTanks();   // Max number of fuel tanks
504
505     for (i=0; i<net->num_tanks; i++) {
506        net->fuel_quantity[i] = (float)(((FGTank *)Propulsion->GetTank(i))->GetContents());
507     }
508
509
510     // Gear status
511     net->num_wheels  = GroundReactions->GetNumGearUnits();
512
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
517        else
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());
521     }
522
523
524     // Environment
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)
528
529
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, --
541
542
543     // Convert the net buffer to network format
544     if ( isLittleEndian ) {
545         net->version = htonl(net->version);
546
547         htond(net->longitude);
548         htond(net->latitude);
549         htond(net->altitude);
550         htonf(net->agl);
551         htonf(net->phi);
552         htonf(net->theta);
553         htonf(net->psi);
554         htonf(net->alpha);
555         htonf(net->beta);
556
557         htonf(net->phidot);
558         htonf(net->thetadot);
559         htonf(net->psidot);
560         htonf(net->vcas);
561         htonf(net->climb_rate);
562         htonf(net->v_north);
563         htonf(net->v_east);
564         htonf(net->v_down);
565         htonf(net->v_wind_body_north);
566         htonf(net->v_wind_body_east);
567         htonf(net->v_wind_body_down);
568
569         htonf(net->A_X_pilot);
570         htonf(net->A_Y_pilot);
571         htonf(net->A_Z_pilot);
572
573         htonf(net->stall_warning);
574         htonf(net->slip_deg);
575
576         for (i=0; i<net->num_engines; ++i ) {
577             net->eng_state[i] = htonl(net->eng_state[i]);
578             htonf(net->rpm[i]);
579             htonf(net->fuel_flow[i]);
580             htonf(net->fuel_px[i]);
581             htonf(net->egt[i]);
582             htonf(net->cht[i]);
583             htonf(net->mp_osi[i]);
584             htonf(net->tit[i]);
585             htonf(net->oil_temp[i]);
586             htonf(net->oil_px[i]);
587         }
588         net->num_engines = htonl(net->num_engines);
589
590         for (i=0; i<net->num_tanks; ++i ) {
591             htonf(net->fuel_quantity[i]);
592         }
593         net->num_tanks = htonl(net->num_tanks);
594
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]);
600         }
601         net->num_wheels = htonl(net->num_wheels);
602
603         net->cur_time = htonl( net->cur_time );
604         net->warp = htonl( net->warp );
605         htonf(net->visibility);
606
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);
613         htonf(net->rudder);
614         htonf(net->nose_wheel);
615         htonf(net->speedbrake);
616         htonf(net->spoilers);
617     }
618 }
619
620 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621
622 void FGOutput::FlightGearSocketOutput(void)
623 {
624   int length = sizeof(fgSockBuf);
625
626
627   if (flightGearSocket == NULL) return;
628   if (!flightGearSocket->GetConnectStatus()) return;
629
630   SocketDataFill(&fgSockBuf);
631   flightGearSocket->Send((char *)&fgSockBuf, length);
632 }
633
634 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635
636 void FGOutput::SocketOutput(void)
637 {
638   string asciiData, scratch;
639
640   if (socket == NULL) return;
641   if (!socket->GetConnectStatus()) return;
642
643   socket->Clear();
644   if (sFirstPass) {
645     socket->Clear("<LABELS>");
646     socket->Append("Time");
647
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");
658     }
659
660     if (SubSystems & ssRates) {
661       socket->Append("P");
662       socket->Append("Q");
663       socket->Append("R");
664       socket->Append("PDot");
665       socket->Append("QDot");
666       socket->Append("RDot");
667     }
668
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");
681     }
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");
690     }
691     if (SubSystems & ssMoments) {
692       socket->Append("L");
693       socket->Append("M");
694       socket->Append("N");
695     }
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");
707     }
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");
722     }
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)");
732     }
733     if (SubSystems & ssCoefficients) {
734       scratch = Aerodynamics->GetCoefficientStrings(",");
735       if (scratch.length() != 0) socket->Append(scratch);
736     }
737     if (SubSystems & ssFCS) {
738       scratch = FCS->GetComponentStrings(",");
739       if (scratch.length() != 0) socket->Append(scratch);
740     }
741     if (SubSystems & ssGroundReactions) {
742       socket->Append(GroundReactions->GetGroundReactionStrings(","));
743     }
744     if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
745       socket->Append(Propulsion->GetPropulsionStrings(","));
746     }
747     if (OutputProperties.size() > 0) {
748       for (unsigned int i=0;i<OutputProperties.size();i++) {
749         socket->Append(OutputProperties[i]->GetPrintableName());
750       }
751     }
752
753     sFirstPass = false;
754     socket->Send();
755   }
756
757   socket->Clear();
758   socket->Append(State->Getsim_time());
759
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());
770   }
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));
778   }
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));
791   }
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));
800   }
801   if (SubSystems & ssMoments) {
802     socket->Append(Aircraft->GetMoments(eL));
803     socket->Append(Aircraft->GetMoments(eM));
804     socket->Append(Aircraft->GetMoments(eN));
805   }
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->GetWindNED().Dump(","));
813   }
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));
828   }
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());
838   }
839   if (SubSystems & ssCoefficients) {
840     scratch = Aerodynamics->GetCoefficientValues(",");
841     if (scratch.length() != 0) socket->Append(scratch);
842   }
843   if (SubSystems & ssFCS) {
844     scratch = FCS->GetComponentValues(",");
845     if (scratch.length() != 0) socket->Append(scratch);
846   }
847   if (SubSystems & ssGroundReactions) {
848     socket->Append(GroundReactions->GetGroundReactionValues(","));
849   }
850   if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
851     socket->Append(Propulsion->GetPropulsionValues(","));
852   }
853
854   for (unsigned int i=0;i<OutputProperties.size();i++) {
855     socket->Append(OutputProperties[i]->getDoubleValue());
856   }
857
858   socket->Send();
859 }
860
861 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862
863 void FGOutput::SocketStatusOutput(string out_str)
864 {
865   string asciiData;
866
867   if (socket == NULL) return;
868
869   socket->Clear();
870   asciiData = string("<STATUS>") + out_str;
871   socket->Append(asciiData.c_str());
872   socket->Send();
873 }
874
875 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876
877 bool FGOutput::Load(Element* element)
878 {
879   string type="", parameter="";
880   string name="";
881   string protocol="tcp";
882   int OutRate = 0;
883   string property;
884   unsigned int port;
885   Element *property_element;
886
887   string separator = "/";
888 # ifdef macintosh
889   separator = ";";
890 # endif
891
892   if (!DirectivesFile.empty()) { // A directives filename from the command line overrides
893     output_file_name = DirectivesFile;      // one found in the config file.
894     document = LoadXMLDocument(output_file_name);
895   } else if (!element->GetAttributeValue("file").empty()) {
896     output_file_name = element->GetAttributeValue("file");
897     document = LoadXMLDocument(output_file_name);
898   } else {
899     document = element;
900   }
901
902   name = document->GetAttributeValue("name");
903   type = document->GetAttributeValue("type");
904   SetType(type);
905   if (!document->GetAttributeValue("port").empty() && type == string("SOCKET")) {
906     port = atoi(document->GetAttributeValue("port").c_str());
907     socket = new FGfdmSocket(name, port);
908   } else if (!document->GetAttributeValue("port").empty() && type == string("FLIGHTGEAR")) {
909     port = atoi(document->GetAttributeValue("port").c_str());
910     if (!document->GetAttributeValue("protocol").empty())
911        protocol = document->GetAttributeValue("protocol");
912     if (protocol == "udp")
913        flightGearSocket = new FGfdmSocket(name, port, FGfdmSocket::ptUDP);  // create udp socket
914     else
915        flightGearSocket = new FGfdmSocket(name, port, FGfdmSocket::ptTCP);  // create tcp socket (default)
916   } else {
917     Filename = name;
918   }
919   if (!document->GetAttributeValue("rate").empty()) {
920     OutRate = (int)document->GetAttributeValueAsNumber("rate");
921   } else {
922     OutRate = 1;
923   }
924
925   if (document->FindElementValue("simulation") == string("ON"))
926     SubSystems += ssSimulation;
927   if (document->FindElementValue("aerosurfaces") == string("ON"))
928     SubSystems += ssAerosurfaces;
929   if (document->FindElementValue("rates") == string("ON"))
930     SubSystems += ssRates;
931   if (document->FindElementValue("velocities") == string("ON"))
932     SubSystems += ssVelocities;
933   if (document->FindElementValue("forces") == string("ON"))
934     SubSystems += ssForces;
935   if (document->FindElementValue("moments") == string("ON"))
936     SubSystems += ssMoments;
937   if (document->FindElementValue("atmosphere") == string("ON"))
938     SubSystems += ssAtmosphere;
939   if (document->FindElementValue("massprops") == string("ON"))
940     SubSystems += ssMassProps;
941   if (document->FindElementValue("position") == string("ON"))
942     SubSystems += ssPropagate;
943   if (document->FindElementValue("coefficients") == string("ON"))
944     SubSystems += ssCoefficients;
945   if (document->FindElementValue("ground_reactions") == string("ON"))
946     SubSystems += ssGroundReactions;
947   if (document->FindElementValue("fcs") == string("ON"))
948     SubSystems += ssFCS;
949   if (document->FindElementValue("propulsion") == string("ON"))
950     SubSystems += ssPropulsion;
951   property_element = document->FindElement("property");
952   while (property_element) {
953     string property_str = property_element->GetDataLine();
954     FGPropertyManager* node = PropertyManager->GetNode(property_str);
955     if (!node) {
956       cerr << fgred << highint << endl << "  No property by the name "
957            << property_str << " has been defined. This property will " << endl
958            << "  not be logged. You should check your configuration file."
959            << reset << endl;
960     } else {
961       OutputProperties.push_back(node);
962     }
963     property_element = document->FindNextElement("property");
964   }
965
966   OutRate = OutRate>1000?1000:(OutRate<0?0:OutRate);
967   rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
968
969   Debug(2);
970
971   return true;
972 }
973
974 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975 //    The bitmasked value choices are as follows:
976 //    unset: In this case (the default) JSBSim would only print
977 //       out the normally expected messages, essentially echoing
978 //       the config files as they are read. If the environment
979 //       variable is not set, debug_lvl is set to 1 internally
980 //    0: This requests JSBSim not to output any messages
981 //       whatsoever.
982 //    1: This value explicity requests the normal JSBSim
983 //       startup messages
984 //    2: This value asks for a message to be printed out when
985 //       a class is instantiated
986 //    4: When this value is set, a message is displayed when a
987 //       FGModel object executes its Run() method
988 //    8: When this value is set, various runtime state variables
989 //       are printed out periodically
990 //    16: When set various parameters are sanity checked and
991 //       a message is printed out when they go out of bounds
992
993 void FGOutput::Debug(int from)
994 {
995   string scratch="";
996
997   if (debug_lvl <= 0) return;
998
999   if (debug_lvl & 1) { // Standard console startup message output
1000     if (from == 0) { // Constructor
1001
1002     }
1003     if (from == 2) {
1004       if (output_file_name.empty())
1005         cout << "  " << "Output parameters read inline" << endl;
1006       else
1007         cout << "    Output parameters read from file: " << output_file_name << endl;
1008
1009       if (Filename == "cout" || Filename == "COUT") {
1010         scratch = "    Log output goes to screen console";
1011       } else if (!Filename.empty()) {
1012         scratch = "    Log output goes to file: " + Filename;
1013       }
1014       switch (Type) {
1015       case otCSV:
1016         cout << scratch << " in CSV format output at rate " << 1/(State->Getdt()*rate) << " Hz" << endl;
1017         break;
1018       case otNone:
1019         cout << "  No log output" << endl;
1020         break;
1021       }
1022
1023       if (SubSystems & ssSimulation)      cout << "    Simulation parameters logged" << endl;
1024       if (SubSystems & ssAerosurfaces)    cout << "    Aerosurface parameters logged" << endl;
1025       if (SubSystems & ssRates)           cout << "    Rate parameters logged" << endl;
1026       if (SubSystems & ssVelocities)      cout << "    Velocity parameters logged" << endl;
1027       if (SubSystems & ssForces)          cout << "    Force parameters logged" << endl;
1028       if (SubSystems & ssMoments)         cout << "    Moments parameters logged" << endl;
1029       if (SubSystems & ssAtmosphere)      cout << "    Atmosphere parameters logged" << endl;
1030       if (SubSystems & ssMassProps)       cout << "    Mass parameters logged" << endl;
1031       if (SubSystems & ssCoefficients)    cout << "    Coefficient parameters logged" << endl;
1032       if (SubSystems & ssPropagate)       cout << "    Propagate parameters logged" << endl;
1033       if (SubSystems & ssGroundReactions) cout << "    Ground parameters logged" << endl;
1034       if (SubSystems & ssFCS)             cout << "    FCS parameters logged" << endl;
1035       if (SubSystems & ssPropulsion)      cout << "    Propulsion parameters logged" << endl;
1036       if (OutputProperties.size() > 0)    cout << "    Properties logged:" << endl;
1037       for (unsigned int i=0;i<OutputProperties.size();i++) {
1038         cout << "      - " << OutputProperties[i]->GetName() << endl;
1039       }
1040     }
1041   }
1042   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1043     if (from == 0) cout << "Instantiated: FGOutput" << endl;
1044     if (from == 1) cout << "Destroyed:    FGOutput" << endl;
1045   }
1046   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1047   }
1048   if (debug_lvl & 8 ) { // Runtime state variables
1049   }
1050   if (debug_lvl & 16) { // Sanity checking
1051   }
1052   if (debug_lvl & 64) {
1053     if (from == 0) { // Constructor
1054       cout << IdSrc << endl;
1055       cout << IdHdr << endl;
1056     }
1057   }
1058 }
1059 }