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