]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGOutput.cpp
Latest JSBSim updates.
[flightgear.git] / src / FDM / JSBSim / 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 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 General Public License for more
19  details.
20
21  You should have received a copy of the GNU 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 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
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41 #include "FGOutput.h"
42 #include "FGState.h"
43 #include "FGFDMExec.h"
44 #include "FGAtmosphere.h"
45 #include "FGFCS.h"
46 #include "FGAerodynamics.h"
47 #include "FGGroundReactions.h"
48 #include "FGAircraft.h"
49 #include "FGMassBalance.h"
50 #include "FGTranslation.h"
51 #include "FGRotation.h"
52 #include "FGPosition.h"
53 #include "FGAuxiliary.h"
54
55 static const char *IdSrc = "$Id$";
56 static const char *IdHdr = ID_OUTPUT;
57
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 CLASS IMPLEMENTATION
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61
62 FGOutput::FGOutput(FGFDMExec* fdmex) : FGModel(fdmex)
63 {
64   Name = "FGOutput";
65   sFirstPass = dFirstPass = true;
66   socket = 0;
67   Type = otNone;
68   Filename = "JSBSim.out";
69   SubSystems = 0;
70   enabled = true;
71   
72   Debug(0);
73 }
74
75 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76
77 FGOutput::~FGOutput()
78 {
79   if (socket) delete socket;
80   Debug(1);
81 }
82
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84
85 bool FGOutput::Run(void)
86 {
87   if (enabled) {
88     if (!FGModel::Run()) {
89
90       if (Type == otSocket) {
91         SocketOutput();
92       } else if (Type == otCSV) {
93         DelimitedOutput(Filename);
94       } else if (Type == otTerminal) {
95         // Not done yet
96       } else if (Type == otNone) {
97         // Do nothing
98       } else {
99         // Not a valid type of output
100       }
101           return false;
102     } else {
103           return true;
104     }
105   }
106   return false;
107 }
108
109 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110
111 void FGOutput::SetType(string type)
112 {
113   if (type == "CSV") {
114     Type = otCSV;
115   } else if (type == "TABULAR") {
116     Type = otTab;
117   } else if (type == "SOCKET") {
118     Type = otSocket;
119   } else if (type == "TERMINAL") {
120     Type = otTerminal;
121   } else if (type != string("NONE")) {
122     Type = otUnknown;
123     cerr << "Unknown type of output specified in config file" << endl;
124   }
125 }
126
127 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128
129 void FGOutput::DelimitedOutput(string fname)
130 {
131   streambuf* buffer;
132
133   if (fname == "COUT" || fname == "cout") {
134     buffer = cout.rdbuf();
135   } else {
136     datafile.open(fname.c_str());
137     buffer = datafile.rdbuf();
138   }
139
140   ostream outstream(buffer);
141
142   if (dFirstPass) {
143     outstream << "Time";
144     if (SubSystems & ssSimulation) {
145       // Nothing here, yet
146     }
147     if (SubSystems & ssAerosurfaces) {
148       outstream << ", ";
149       outstream << "Aileron Cmd, ";
150       outstream << "Elevator Cmd, ";
151       outstream << "Rudder Cmd, ";
152       outstream << "Flap Cmd, ";
153       outstream << "Left Aileron Pos, ";
154       outstream << "Right Aileron Pos, ";
155       outstream << "Elevator Pos, ";
156       outstream << "Rudder Pos, ";
157       outstream << "Flap Pos"; 
158     }
159     if (SubSystems & ssRates) {
160       outstream << ", ";
161       outstream << "P, Q, R, ";
162       outstream << "Pdot, Qdot, Rdot";
163     }
164     if (SubSystems & ssVelocities) {
165       outstream << ", ";
166       outstream << "QBar, ";
167       outstream << "Vtotal, ";
168       outstream << "UBody, VBody, WBody, ";
169       outstream << "UAero, VAero, WAero, ";
170       outstream << "Vn, Ve, Vd";
171     }
172     if (SubSystems & ssForces) {
173       outstream << ", ";
174       outstream << "Drag, Side, Lift, ";
175       outstream << "L/D, ";
176       outstream << "Xforce, Yforce, Zforce, ";
177       outstream << "xGravity, yGravity, zGravity, ";
178       outstream << "xCoriolis, yCoriolis, zCoriolis, ";
179       outstream << "xCentrifugal, yCentrifugal, zCentrifugal";
180     }
181     if (SubSystems & ssMoments) {
182       outstream << ", ";
183       outstream << "L, M, N";
184     }
185     if (SubSystems & ssAtmosphere) {
186       outstream << ", ";
187       outstream << "Rho, ";
188       outstream << "NWind, EWind, DWind";
189     }
190     if (SubSystems & ssMassProps) {
191       outstream << ", ";
192       outstream << "Ixx, ";
193       outstream << "Iyy, ";
194       outstream << "Izz, ";
195       outstream << "Ixz, ";
196       outstream << "Mass, ";
197       outstream << "Xcg, Ycg, Zcg";
198     }
199     if (SubSystems & ssPosition) {
200       outstream << ", ";
201       outstream << "Altitude, ";
202       outstream << "Phi, Tht, Psi, ";
203       outstream << "Alpha, ";
204       outstream << "Beta, ";
205       outstream << "Latitude, ";
206       outstream << "Longitude, ";
207       outstream << "Distance AGL, ";
208       outstream << "Runway Radius";
209     }
210     if (SubSystems & ssCoefficients) {
211       outstream << ", ";
212       outstream << Aerodynamics->GetCoefficientStrings();
213     }
214     if (SubSystems & ssFCS) {
215       outstream << ", ";
216       outstream << FCS->GetComponentStrings();
217     }
218     if (SubSystems & ssGroundReactions) {
219       outstream << ", ";
220       outstream << GroundReactions->GetGroundReactionStrings();
221     }
222     if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
223       outstream << ", ";
224       outstream << Propulsion->GetPropulsionStrings();
225     }
226
227     outstream << endl;
228     dFirstPass = false;
229   }
230
231   outstream << State->Getsim_time();
232   if (SubSystems & ssSimulation) {
233   }
234   if (SubSystems & ssAerosurfaces) {
235     outstream << ", ";
236     outstream << FCS->GetDaCmd() << ", ";
237     outstream << FCS->GetDeCmd() << ", ";
238     outstream << FCS->GetDrCmd() << ", ";
239     outstream << FCS->GetDfCmd() << ", ";
240     outstream << FCS->GetDaLPos() << ", ";
241     outstream << FCS->GetDaRPos() << ", ";
242     outstream << FCS->GetDePos() << ", ";
243     outstream << FCS->GetDrPos() << ", ";
244     outstream << FCS->GetDfPos();
245   }
246   if (SubSystems & ssRates) {
247     outstream << ", ";
248     outstream << Rotation->GetPQR() << ", ";
249     outstream << Rotation->GetPQRdot();
250   }
251   if (SubSystems & ssVelocities) {
252     outstream << ", ";
253     outstream << Translation->Getqbar() << ", ";
254     outstream << Translation->GetVt() << ", ";
255     outstream << Translation->GetUVW() << ", ";
256     outstream << Translation->GetAeroUVW() << ", ";
257     outstream << Position->GetVel();
258   }
259   if (SubSystems & ssForces) {
260     outstream << ", ";
261     outstream << Aerodynamics->GetvFs() << ", ";
262     outstream << Aerodynamics->GetLoD() << ", ";
263     outstream << Aircraft->GetForces() << ", ";
264     outstream << Inertial->GetGravity() << ", ";
265     outstream << Inertial->GetCoriolis() << ", ";
266     outstream << Inertial->GetCentrifugal();
267   }
268   if (SubSystems & ssMoments) {
269     outstream << ", ";
270     outstream << Aircraft->GetMoments();
271   }
272   if (SubSystems & ssAtmosphere) {
273     outstream << ", ";
274     outstream << Atmosphere->GetDensity() << ", ";
275     outstream << Atmosphere->GetWindNED();
276   }
277   if (SubSystems & ssMassProps) {
278     outstream << ", ";
279     outstream << MassBalance->GetIxx() << ", ";
280     outstream << MassBalance->GetIyy() << ", ";
281     outstream << MassBalance->GetIzz() << ", ";
282     outstream << MassBalance->GetIxz() << ", ";
283     outstream << MassBalance->GetMass() << ", ";
284     outstream << MassBalance->GetXYZcg();
285   }
286   if (SubSystems & ssPosition) {
287     outstream << ", ";
288     outstream << Position->Geth() << ", ";
289     outstream << Rotation->GetEuler() << ", ";
290     outstream << Translation->Getalpha() << ", ";
291     outstream << Translation->Getbeta() << ", ";
292     outstream << Position->GetLatitude() << ", ";
293     outstream << Position->GetLongitude() << ", ";
294     outstream << Position->GetDistanceAGL() << ", ";
295     outstream << Position->GetRunwayRadius();
296   }
297   if (SubSystems & ssCoefficients) {
298     outstream << ", ";
299     outstream << Aerodynamics->GetCoefficientValues();
300   }
301   if (SubSystems & ssFCS) {
302     outstream << ", ";
303     outstream << FCS->GetComponentValues();
304   }
305   if (SubSystems & ssGroundReactions) {
306     outstream << ", ";
307     outstream << GroundReactions->GetGroundReactionValues();
308   }
309   if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
310     outstream << ", ";
311     outstream << Propulsion->GetPropulsionValues();
312   }
313
314   outstream << endl;
315   outstream.flush();
316 }
317
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319
320 void FGOutput::SocketOutput(void)
321 {
322   string asciiData;
323
324   if (socket == NULL) return;
325   if (!socket->GetConnectStatus()) return;
326
327   socket->Clear();
328   if (sFirstPass) {
329     socket->Append("<LABELS>");
330     socket->Append("Time");
331     socket->Append("Altitude");
332     socket->Append("Phi");
333     socket->Append("Tht");
334     socket->Append("Psi");
335     socket->Append("Rho");
336     socket->Append("Vtotal");
337     socket->Append("UBody");
338     socket->Append("VBody");
339     socket->Append("WBody");
340     socket->Append("UAero");
341     socket->Append("VAero");
342     socket->Append("WAero");
343     socket->Append("Vn");
344     socket->Append("Ve");
345     socket->Append("Vd");
346     socket->Append("Udot");
347     socket->Append("Vdot");
348     socket->Append("Wdot");
349     socket->Append("P");
350     socket->Append("Q");
351     socket->Append("R");
352     socket->Append("PDot");
353     socket->Append("QDot");
354     socket->Append("RDot");
355     socket->Append("Fx");
356     socket->Append("Fy");
357     socket->Append("Fz");
358     socket->Append("Latitude");
359     socket->Append("Longitude");
360     socket->Append("QBar");
361     socket->Append("Alpha");
362     socket->Append("L");
363     socket->Append("M");
364     socket->Append("N");
365     socket->Append("Throttle Position");
366     socket->Append("Left Aileron Position");
367     socket->Append("Right Aileron Position");
368     socket->Append("Elevator Position");
369     socket->Append("Rudder Position");
370     sFirstPass = false;
371     socket->Send();
372   }
373
374   socket->Clear();
375   socket->Append(State->Getsim_time());
376   socket->Append(Position->Geth());
377   socket->Append(Rotation->Getphi());
378   socket->Append(Rotation->Gettht());
379   socket->Append(Rotation->Getpsi());
380   socket->Append(Atmosphere->GetDensity());
381   socket->Append(Translation->GetVt());
382   socket->Append(Translation->GetUVW(eU));
383   socket->Append(Translation->GetUVW(eV));
384   socket->Append(Translation->GetUVW(eW));
385   socket->Append(Translation->GetAeroUVW(eU));
386   socket->Append(Translation->GetAeroUVW(eV));
387   socket->Append(Translation->GetAeroUVW(eW));
388   socket->Append(Position->GetVn());
389   socket->Append(Position->GetVe());
390   socket->Append(Position->GetVd());
391   socket->Append(Translation->GetUVWdot(eU));
392   socket->Append(Translation->GetUVWdot(eV));
393   socket->Append(Translation->GetUVWdot(eW));
394   socket->Append(Rotation->GetPQR(eP));
395   socket->Append(Rotation->GetPQR(eQ));
396   socket->Append(Rotation->GetPQR(eR));
397   socket->Append(Rotation->GetPQRdot(eP));
398   socket->Append(Rotation->GetPQRdot(eQ));
399   socket->Append(Rotation->GetPQRdot(eR));
400   socket->Append(Aircraft->GetForces(eX));
401   socket->Append(Aircraft->GetForces(eY));
402   socket->Append(Aircraft->GetForces(eZ));
403   socket->Append(Position->GetLatitude());
404   socket->Append(Position->GetLongitude());
405   socket->Append(Translation->Getqbar());
406   socket->Append(Translation->Getalpha());
407   socket->Append(Aircraft->GetMoments(eL));
408   socket->Append(Aircraft->GetMoments(eM));
409   socket->Append(Aircraft->GetMoments(eN));
410   socket->Append(FCS->GetThrottlePos(0));
411   socket->Append(FCS->GetDaLPos());
412   socket->Append(FCS->GetDaRPos());
413   socket->Append(FCS->GetDePos());
414   socket->Append(FCS->GetDrPos());
415   socket->Send();
416 }
417
418 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419
420 void FGOutput::SocketStatusOutput(string out_str)
421 {
422   string asciiData;
423
424   if (socket == NULL) return;
425
426   socket->Clear();
427   asciiData = string("<STATUS>") + out_str;
428   socket->Append(asciiData.c_str());
429   socket->Send();
430 }
431
432 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
433
434 bool FGOutput::Load(FGConfigFile* AC_cfg)
435 {
436   string token, parameter;
437   int OutRate = 0;
438
439   token = AC_cfg->GetValue("NAME");
440   Output->SetFilename(token);
441   token = AC_cfg->GetValue("TYPE");
442   Output->SetType(token);
443
444 #if defined( FG_WITH_JSBSIM_SOCKET ) || !defined( FGFS )
445   if (token == "SOCKET") {
446     socket = new FGfdmSocket("localhost",1138);
447   }
448 #endif
449   
450   AC_cfg->GetNextConfigLine();
451
452   while ((token = AC_cfg->GetValue()) != string("/OUTPUT")) {
453     *AC_cfg >> parameter;
454     if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
455     if (parameter == "SIMULATION") {
456       *AC_cfg >> parameter;
457       if (parameter == "ON") SubSystems += ssSimulation;
458     }
459     if (parameter == "AEROSURFACES") {
460       *AC_cfg >> parameter;
461       if (parameter == "ON") SubSystems += ssAerosurfaces;
462     }
463     if (parameter == "RATES") {
464       *AC_cfg >> parameter;
465       if (parameter == "ON") SubSystems += ssRates;
466     }
467     if (parameter == "VELOCITIES") {
468       *AC_cfg >> parameter;
469       if (parameter == "ON") SubSystems += ssVelocities;
470     }
471     if (parameter == "FORCES") {
472       *AC_cfg >> parameter;
473       if (parameter == "ON") SubSystems += ssForces;
474     }
475     if (parameter == "MOMENTS") {
476       *AC_cfg >> parameter;
477       if (parameter == "ON") SubSystems += ssMoments;
478     }
479     if (parameter == "ATMOSPHERE") {
480       *AC_cfg >> parameter;
481       if (parameter == "ON") SubSystems += ssAtmosphere;
482     }
483     if (parameter == "MASSPROPS") {
484       *AC_cfg >> parameter;
485       if (parameter == "ON") SubSystems += ssMassProps;
486     }
487     if (parameter == "POSITION") {
488       *AC_cfg >> parameter;
489       if (parameter == "ON") SubSystems += ssPosition;
490     }
491     if (parameter == "COEFFICIENTS") {
492       *AC_cfg >> parameter;
493       if (parameter == "ON") SubSystems += ssCoefficients;
494     }
495     if (parameter == "GROUND_REACTIONS") {
496       *AC_cfg >> parameter;
497       if (parameter == "ON") SubSystems += ssGroundReactions;
498     }
499     if (parameter == "FCS") {
500       *AC_cfg >> parameter;
501       if (parameter == "ON") SubSystems += ssFCS;
502     }
503     if (parameter == "PROPULSION") {
504       *AC_cfg >> parameter;
505       if (parameter == "ON") SubSystems += ssPropulsion;
506     }
507   }
508
509   OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
510   rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
511
512   return true;
513 }
514
515 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 //    The bitmasked value choices are as follows:
517 //    unset: In this case (the default) JSBSim would only print
518 //       out the normally expected messages, essentially echoing
519 //       the config files as they are read. If the environment
520 //       variable is not set, debug_lvl is set to 1 internally
521 //    0: This requests JSBSim not to output any messages
522 //       whatsoever.
523 //    1: This value explicity requests the normal JSBSim
524 //       startup messages
525 //    2: This value asks for a message to be printed out when
526 //       a class is instantiated
527 //    4: When this value is set, a message is displayed when a
528 //       FGModel object executes its Run() method
529 //    8: When this value is set, various runtime state variables
530 //       are printed out periodically
531 //    16: When set various parameters are sanity checked and
532 //       a message is printed out when they go out of bounds
533
534 void FGOutput::Debug(int from)
535 {
536   if (debug_lvl <= 0) return;
537
538   if (debug_lvl & 1) { // Standard console startup message output
539     if (from == 0) { // Constructor
540
541     }
542   }
543   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
544     if (from == 0) cout << "Instantiated: FGOutput" << endl;
545     if (from == 1) cout << "Destroyed:    FGOutput" << endl;
546   }
547   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
548   }
549   if (debug_lvl & 8 ) { // Runtime state variables
550   }
551   if (debug_lvl & 16) { // Sanity checking
552   }
553   if (debug_lvl & 64) {
554     if (from == 0) { // Constructor
555       cout << IdSrc << endl;
556       cout << IdHdr << endl;
557     }
558   }
559 }
560