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