]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGOutput.cpp
More tweaks to the automake/conf configuration scripts.
[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 & ssGroundReactions) {
215       outstream << ", ";
216       outstream << GroundReactions->GetGroundReactionStrings();
217     }
218     if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
219       outstream << ", ";
220       outstream << Propulsion->GetPropulsionStrings();
221     }
222
223     outstream << endl;
224     dFirstPass = false;
225   }
226
227   outstream << State->Getsim_time();
228   if (SubSystems & ssSimulation) {
229   }
230   if (SubSystems & ssAerosurfaces) {
231     outstream << ", ";
232     outstream << FCS->GetDaCmd() << ", ";
233     outstream << FCS->GetDeCmd() << ", ";
234     outstream << FCS->GetDrCmd() << ", ";
235     outstream << FCS->GetDfCmd() << ", ";
236     outstream << FCS->GetDaLPos() << ", ";
237     outstream << FCS->GetDaRPos() << ", ";
238     outstream << FCS->GetDePos() << ", ";
239     outstream << FCS->GetDrPos() << ", ";
240     outstream << FCS->GetDfPos();
241   }
242   if (SubSystems & ssRates) {
243     outstream << ", ";
244     outstream << Rotation->GetPQR() << ", ";
245     outstream << Rotation->GetPQRdot();
246   }
247   if (SubSystems & ssVelocities) {
248     outstream << ", ";
249     outstream << Translation->Getqbar() << ", ";
250     outstream << Translation->GetVt() << ", ";
251     outstream << Translation->GetUVW() << ", ";
252     outstream << Translation->GetAeroUVW() << ", ";
253     outstream << Position->GetVel();
254   }
255   if (SubSystems & ssForces) {
256     outstream << ", ";
257     outstream << Aerodynamics->GetvFs() << ", ";
258     outstream << Aerodynamics->GetLoD() << ", ";
259     outstream << Aircraft->GetForces() << ", ";
260     outstream << Inertial->GetGravity() << ", ";
261     outstream << Inertial->GetCoriolis() << ", ";
262     outstream << Inertial->GetCentrifugal();
263   }
264   if (SubSystems & ssMoments) {
265     outstream << ", ";
266     outstream << Aircraft->GetMoments();
267   }
268   if (SubSystems & ssAtmosphere) {
269     outstream << ", ";
270     outstream << Atmosphere->GetDensity() << ", ";
271     outstream << Atmosphere->GetWindNED();
272   }
273   if (SubSystems & ssMassProps) {
274     outstream << ", ";
275     outstream << MassBalance->GetIxx() << ", ";
276     outstream << MassBalance->GetIyy() << ", ";
277     outstream << MassBalance->GetIzz() << ", ";
278     outstream << MassBalance->GetIxz() << ", ";
279     outstream << MassBalance->GetMass() << ", ";
280     outstream << MassBalance->GetXYZcg();
281   }
282   if (SubSystems & ssPosition) {
283     outstream << ", ";
284     outstream << Position->Geth() << ", ";
285     outstream << Rotation->GetEuler() << ", ";
286     outstream << Translation->Getalpha() << ", ";
287     outstream << Translation->Getbeta() << ", ";
288     outstream << Position->GetLatitude() << ", ";
289     outstream << Position->GetLongitude() << ", ";
290     outstream << Position->GetDistanceAGL() << ", ";
291     outstream << Position->GetRunwayRadius();
292   }
293   if (SubSystems & ssCoefficients) {
294     outstream << ", ";
295     outstream << Aerodynamics->GetCoefficientValues();
296   }
297   if (SubSystems & ssGroundReactions) {
298     outstream << ", ";
299     outstream << GroundReactions->GetGroundReactionValues();
300   }
301   if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
302     outstream << ", ";
303     outstream << Propulsion->GetPropulsionValues();
304   }
305
306   outstream << endl;
307   outstream.flush();
308 }
309
310 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311
312 void FGOutput::SocketOutput(void)
313 {
314   string asciiData;
315
316   if (socket == NULL) return;
317   if (!socket->GetConnectStatus()) return;
318
319   socket->Clear();
320   if (sFirstPass) {
321     socket->Append("<LABELS>");
322     socket->Append("Time");
323     socket->Append("Altitude");
324     socket->Append("Phi");
325     socket->Append("Tht");
326     socket->Append("Psi");
327     socket->Append("Rho");
328     socket->Append("Vtotal");
329     socket->Append("UBody");
330     socket->Append("VBody");
331     socket->Append("WBody");
332     socket->Append("UAero");
333     socket->Append("VAero");
334     socket->Append("WAero");
335     socket->Append("Vn");
336     socket->Append("Ve");
337     socket->Append("Vd");
338     socket->Append("Udot");
339     socket->Append("Vdot");
340     socket->Append("Wdot");
341     socket->Append("P");
342     socket->Append("Q");
343     socket->Append("R");
344     socket->Append("PDot");
345     socket->Append("QDot");
346     socket->Append("RDot");
347     socket->Append("Fx");
348     socket->Append("Fy");
349     socket->Append("Fz");
350     socket->Append("Latitude");
351     socket->Append("Longitude");
352     socket->Append("QBar");
353     socket->Append("Alpha");
354     socket->Append("L");
355     socket->Append("M");
356     socket->Append("N");
357     socket->Append("Throttle Position");
358     socket->Append("Left Aileron Position");
359     socket->Append("Right Aileron Position");
360     socket->Append("Elevator Position");
361     socket->Append("Rudder Position");
362     sFirstPass = false;
363     socket->Send();
364   }
365
366   socket->Clear();
367   socket->Append(State->Getsim_time());
368   socket->Append(Position->Geth());
369   socket->Append(Rotation->Getphi());
370   socket->Append(Rotation->Gettht());
371   socket->Append(Rotation->Getpsi());
372   socket->Append(Atmosphere->GetDensity());
373   socket->Append(Translation->GetVt());
374   socket->Append(Translation->GetUVW(eU));
375   socket->Append(Translation->GetUVW(eV));
376   socket->Append(Translation->GetUVW(eW));
377   socket->Append(Translation->GetAeroUVW(eU));
378   socket->Append(Translation->GetAeroUVW(eV));
379   socket->Append(Translation->GetAeroUVW(eW));
380   socket->Append(Position->GetVn());
381   socket->Append(Position->GetVe());
382   socket->Append(Position->GetVd());
383   socket->Append(Translation->GetUVWdot(eU));
384   socket->Append(Translation->GetUVWdot(eV));
385   socket->Append(Translation->GetUVWdot(eW));
386   socket->Append(Rotation->GetPQR(eP));
387   socket->Append(Rotation->GetPQR(eQ));
388   socket->Append(Rotation->GetPQR(eR));
389   socket->Append(Rotation->GetPQRdot(eP));
390   socket->Append(Rotation->GetPQRdot(eQ));
391   socket->Append(Rotation->GetPQRdot(eR));
392   socket->Append(Aircraft->GetForces(eX));
393   socket->Append(Aircraft->GetForces(eY));
394   socket->Append(Aircraft->GetForces(eZ));
395   socket->Append(Position->GetLatitude());
396   socket->Append(Position->GetLongitude());
397   socket->Append(Translation->Getqbar());
398   socket->Append(Translation->Getalpha());
399   socket->Append(Aircraft->GetMoments(eL));
400   socket->Append(Aircraft->GetMoments(eM));
401   socket->Append(Aircraft->GetMoments(eN));
402   socket->Append(FCS->GetThrottlePos(0));
403   socket->Append(FCS->GetDaLPos());
404   socket->Append(FCS->GetDaRPos());
405   socket->Append(FCS->GetDePos());
406   socket->Append(FCS->GetDrPos());
407   socket->Send();
408 }
409
410 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411
412 void FGOutput::SocketStatusOutput(string out_str)
413 {
414   string asciiData;
415
416   if (socket == NULL) return;
417
418   socket->Clear();
419   asciiData = string("<STATUS>") + out_str;
420   socket->Append(asciiData.c_str());
421   socket->Send();
422 }
423
424 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425
426 bool FGOutput::Load(FGConfigFile* AC_cfg)
427 {
428   string token, parameter;
429   int OutRate = 0;
430
431   token = AC_cfg->GetValue("NAME");
432   Output->SetFilename(token);
433   token = AC_cfg->GetValue("TYPE");
434   Output->SetType(token);
435
436 #if defined( FG_WITH_JSBSIM_SOCKET ) || !defined( FGFS )
437   if (token == "SOCKET") {
438     socket = new FGfdmSocket("localhost",1138);
439   }
440 #endif
441   
442   AC_cfg->GetNextConfigLine();
443
444   while ((token = AC_cfg->GetValue()) != string("/OUTPUT")) {
445     *AC_cfg >> parameter;
446     if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
447     if (parameter == "SIMULATION") {
448       *AC_cfg >> parameter;
449       if (parameter == "ON") SubSystems += ssSimulation;
450     }
451     if (parameter == "AEROSURFACES") {
452       *AC_cfg >> parameter;
453       if (parameter == "ON") SubSystems += ssAerosurfaces;
454     }
455     if (parameter == "RATES") {
456       *AC_cfg >> parameter;
457       if (parameter == "ON") SubSystems += ssRates;
458     }
459     if (parameter == "VELOCITIES") {
460       *AC_cfg >> parameter;
461       if (parameter == "ON") SubSystems += ssVelocities;
462     }
463     if (parameter == "FORCES") {
464       *AC_cfg >> parameter;
465       if (parameter == "ON") SubSystems += ssForces;
466     }
467     if (parameter == "MOMENTS") {
468       *AC_cfg >> parameter;
469       if (parameter == "ON") SubSystems += ssMoments;
470     }
471     if (parameter == "ATMOSPHERE") {
472       *AC_cfg >> parameter;
473       if (parameter == "ON") SubSystems += ssAtmosphere;
474     }
475     if (parameter == "MASSPROPS") {
476       *AC_cfg >> parameter;
477       if (parameter == "ON") SubSystems += ssMassProps;
478     }
479     if (parameter == "POSITION") {
480       *AC_cfg >> parameter;
481       if (parameter == "ON") SubSystems += ssPosition;
482     }
483     if (parameter == "COEFFICIENTS") {
484       *AC_cfg >> parameter;
485       if (parameter == "ON") SubSystems += ssCoefficients;
486     }
487     if (parameter == "GROUND_REACTIONS") {
488       *AC_cfg >> parameter;
489       if (parameter == "ON") SubSystems += ssGroundReactions;
490     }
491     if (parameter == "FCS") {
492       *AC_cfg >> parameter;
493       if (parameter == "ON") SubSystems += ssFCS;
494     }
495     if (parameter == "PROPULSION") {
496       *AC_cfg >> parameter;
497       if (parameter == "ON") SubSystems += ssPropulsion;
498     }
499   }
500
501   OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
502   rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
503
504   return true;
505 }
506
507 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 //    The bitmasked value choices are as follows:
509 //    unset: In this case (the default) JSBSim would only print
510 //       out the normally expected messages, essentially echoing
511 //       the config files as they are read. If the environment
512 //       variable is not set, debug_lvl is set to 1 internally
513 //    0: This requests JSBSim not to output any messages
514 //       whatsoever.
515 //    1: This value explicity requests the normal JSBSim
516 //       startup messages
517 //    2: This value asks for a message to be printed out when
518 //       a class is instantiated
519 //    4: When this value is set, a message is displayed when a
520 //       FGModel object executes its Run() method
521 //    8: When this value is set, various runtime state variables
522 //       are printed out periodically
523 //    16: When set various parameters are sanity checked and
524 //       a message is printed out when they go out of bounds
525
526 void FGOutput::Debug(int from)
527 {
528   if (debug_lvl <= 0) return;
529
530   if (debug_lvl & 1) { // Standard console startup message output
531     if (from == 0) { // Constructor
532
533     }
534   }
535   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
536     if (from == 0) cout << "Instantiated: FGOutput" << endl;
537     if (from == 1) cout << "Destroyed:    FGOutput" << endl;
538   }
539   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
540   }
541   if (debug_lvl & 8 ) { // Runtime state variables
542   }
543   if (debug_lvl & 16) { // Sanity checking
544   }
545   if (debug_lvl & 64) {
546     if (from == 0) { // Constructor
547       cout << IdSrc << endl;
548       cout << IdHdr << endl;
549     }
550   }
551 }
552