]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGOutput.cpp
Removed FGMatrix.* because it is no longer used by JSBSim.
[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 #ifdef FG_WITH_JSBSIM_SOCKET
73   socket = new FGfdmSocket("localhost",1138);
74 #endif
75   Debug(0);
76 }
77
78 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79
80 FGOutput::~FGOutput()
81 {
82   if (socket) delete socket;
83   Debug(1);
84 }
85
86 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87
88 bool FGOutput::Run(void)
89 {
90   if (enabled) {
91     if (!FGModel::Run()) {
92
93       if (Type == otSocket) {
94         SocketOutput();
95       } else if (Type == otCSV) {
96         DelimitedOutput(Filename);
97       } else if (Type == otTerminal) {
98         // Not done yet
99       } else if (Type == otNone) {
100         // Do nothing
101       } else {
102         // Not a valid type of output
103       }
104
105     } else {
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 << "Aileron Pos, ";
155       outstream << "Elevator Pos, ";
156       outstream << "Rudder Pos";
157     }
158     if (SubSystems & ssRates) {
159       outstream << ", ";
160       outstream << "P, Q, R, ";
161       outstream << "Pdot, Qdot, Rdot";
162     }
163     if (SubSystems & ssVelocities) {
164       outstream << ", ";
165       outstream << "QBar, ";
166       outstream << "Vtotal, ";
167       outstream << "UBody, VBody, WBody, ";
168       outstream << "UAero, VAero, WAero, ";
169       outstream << "Vn, Ve, Vd";
170     }
171     if (SubSystems & ssForces) {
172       outstream << ", ";
173       outstream << "Drag, Side, Lift, ";
174       outstream << "L/D, ";
175       outstream << "Xforce, Yforce, Zforce";
176     }
177     if (SubSystems & ssMoments) {
178       outstream << ", ";
179       outstream << "L, M, N";
180     }
181     if (SubSystems & ssAtmosphere) {
182       outstream << ", ";
183       outstream << "Rho, ";
184       outstream << "NWind, EWind, DWind";
185     }
186     if (SubSystems & ssMassProps) {
187       outstream << ", ";
188       outstream << "Ixx, ";
189       outstream << "Iyy, ";
190       outstream << "Izz, ";
191       outstream << "Ixz, ";
192       outstream << "Mass, ";
193       outstream << "Xcg, Ycg, Zcg";
194     }
195     if (SubSystems & ssPosition) {
196       outstream << ", ";
197       outstream << "Altitude, ";
198       outstream << "Phi, Tht, Psi, ";
199       outstream << "Alpha, ";
200       outstream << "Beta, ";
201       outstream << "Latitude, ";
202       outstream << "Longitude, ";
203       outstream << "Distance AGL, ";
204       outstream << "Runway Radius";
205     }
206     if (SubSystems & ssCoefficients) {
207       outstream << ", ";
208       outstream << Aerodynamics->GetCoefficientStrings();
209     }
210     if (SubSystems & ssGroundReactions) {
211       outstream << ", ";
212       outstream << GroundReactions->GetGroundReactionStrings();
213     }
214     if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
215       outstream << ", ";
216       outstream << Propulsion->GetPropulsionStrings();
217     }
218
219     outstream << endl;
220     dFirstPass = false;
221   }
222
223   outstream << State->Getsim_time();
224   if (SubSystems & ssSimulation) {
225   }
226   if (SubSystems & ssAerosurfaces) {
227     outstream << ", ";
228     outstream << FCS->GetDaCmd() << ", ";
229     outstream << FCS->GetDeCmd() << ", ";
230     outstream << FCS->GetDrCmd() << ", ";
231     outstream << FCS->GetDaPos() << ", ";
232     outstream << FCS->GetDePos() << ", ";
233     outstream << FCS->GetDrPos();
234   }
235   if (SubSystems & ssRates) {
236     outstream << ", ";
237     outstream << Rotation->GetPQR() << ", ";
238     outstream << Rotation->GetPQRdot();
239   }
240   if (SubSystems & ssVelocities) {
241     outstream << ", ";
242     outstream << Translation->Getqbar() << ", ";
243     outstream << Translation->GetVt() << ", ";
244     outstream << Translation->GetUVW() << ", ";
245     outstream << Translation->GetvAeroUVW() << ", ";
246     outstream << Position->GetVel();
247   }
248   if (SubSystems & ssForces) {
249     outstream << ", ";
250     outstream << Aerodynamics->GetvFs() << ", ";
251     outstream << Aerodynamics->GetLoD() << ", ";
252     outstream << Aircraft->GetForces();
253   }
254   if (SubSystems & ssMoments) {
255     outstream << ", ";
256     outstream << Aircraft->GetMoments();
257   }
258   if (SubSystems & ssAtmosphere) {
259     outstream << ", ";
260     outstream << Atmosphere->GetDensity() << ", ";
261     outstream << Atmosphere->GetWindNED();
262   }
263   if (SubSystems & ssMassProps) {
264     outstream << ", ";
265     outstream << MassBalance->GetIxx() << ", ";
266     outstream << MassBalance->GetIyy() << ", ";
267     outstream << MassBalance->GetIzz() << ", ";
268     outstream << MassBalance->GetIxz() << ", ";
269     outstream << MassBalance->GetMass() << ", ";
270     outstream << MassBalance->GetXYZcg();
271   }
272   if (SubSystems & ssPosition) {
273     outstream << ", ";
274     outstream << Position->Geth() << ", ";
275     outstream << Rotation->GetEuler() << ", ";
276     outstream << Translation->Getalpha() << ", ";
277     outstream << Translation->Getbeta() << ", ";
278     outstream << Position->GetLatitude() << ", ";
279     outstream << Position->GetLongitude() << ", ";
280     outstream << Position->GetDistanceAGL() << ", ";
281     outstream << Position->GetRunwayRadius();
282   }
283   if (SubSystems & ssCoefficients) {
284     outstream << ", ";
285     outstream << Aerodynamics->GetCoefficientValues();
286   }
287   if (SubSystems & ssGroundReactions) {
288     outstream << ", ";
289     outstream << GroundReactions->GetGroundReactionValues();
290   }
291   if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
292     outstream << ", ";
293     outstream << Propulsion->GetPropulsionValues();
294   }
295
296   outstream << endl;
297   outstream.flush();
298 }
299
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301
302 void FGOutput::SocketOutput(void)
303 {
304   string asciiData;
305   /*
306   if (socket == NULL) return;
307
308   socket->Clear();
309   if (sFirstPass) {
310     socket->Append("<LABELS>");
311     socket->Append("Time");
312     socket->Append("Altitude");
313     socket->Append("Phi");
314     socket->Append("Tht");
315     socket->Append("Psi");
316     socket->Append("Rho");
317     socket->Append("Vtotal");
318     socket->Append("UBody");
319     socket->Append("VBody");
320     socket->Append("WBody");
321     socket->Append("UAero");
322     socket->Append("VAero");
323     socket->Append("WAero");
324     socket->Append("Vn");
325     socket->Append("Ve");
326     socket->Append("Vd");
327     socket->Append("Udot");
328     socket->Append("Vdot");
329     socket->Append("Wdot");
330     socket->Append("P");
331     socket->Append("Q");
332     socket->Append("R");
333     socket->Append("PDot");
334     socket->Append("QDot");
335     socket->Append("RDot");
336     socket->Append("Fx");
337     socket->Append("Fy");
338     socket->Append("Fz");
339     socket->Append("Latitude");
340     socket->Append("Longitude");
341     socket->Append("QBar");
342     socket->Append("Alpha");
343     socket->Append("L");
344     socket->Append("M");
345     socket->Append("N");
346     socket->Append("Throttle");
347     socket->Append("Aileron");
348     socket->Append("Elevator");
349     socket->Append("Rudder");
350     sFirstPass = false;
351     socket->Send();
352   }
353
354   socket->Clear();
355   socket->Append(State->Getsim_time());
356   socket->Append(Position->Geth());
357   socket->Append(Rotation->Getphi());
358   socket->Append(Rotation->Gettht());
359   socket->Append(Rotation->Getpsi());
360   socket->Append(Atmosphere->GetDensity());
361   socket->Append(Translation->GetVt());
362   socket->Append(Translation->GetUVW(eU));
363   socket->Append(Translation->GetUVW(eV));
364   socket->Append(Translation->GetUVW(eW));
365   socket->Append(Translation->GetvAeroUVW(eU));
366   socket->Append(Translation->GetvAeroUVW(eV));
367   socket->Append(Translation->GetvAeroUVW(eW));
368   socket->Append(Position->GetVn());
369   socket->Append(Position->GetVe());
370   socket->Append(Position->GetVd());
371   socket->Append(Translation->GetUdot());
372   socket->Append(Translation->GetVdot());
373   socket->Append(Translation->GetWdot());
374   socket->Append(Rotation->GetP());
375   socket->Append(Rotation->GetQ());
376   socket->Append(Rotation->GetR());
377   socket->Append(Rotation->GetPdot());
378   socket->Append(Rotation->GetQdot());
379   socket->Append(Rotation->GetRdot());
380   socket->Append(Aircraft->GetFx());
381   socket->Append(Aircraft->GetFy());
382   socket->Append(Aircraft->GetFz());
383   socket->Append(Position->GetLatitude());
384   socket->Append(Position->GetLongitude());
385   socket->Append(Translation->Getqbar());
386   socket->Append(Translation->Getalpha());
387   socket->Append(Aircraft->GetL());
388   socket->Append(Aircraft->GetM());
389   socket->Append(Aircraft->GetN());
390   socket->Append(FCS->GetThrottle(0));
391   socket->Append(FCS->GetDa());
392   socket->Append(FCS->GetDe());
393   socket->Append(FCS->GetDr());
394   socket->Send(); */
395 }
396
397 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398
399 void FGOutput::SocketStatusOutput(string out_str)
400 {
401   string asciiData;
402
403   if (socket == NULL) return;
404
405   socket->Clear();
406   asciiData = string("<STATUS>") + out_str;
407   socket->Append(asciiData.c_str());
408   socket->Send();
409 }
410
411 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412
413 bool FGOutput::Load(FGConfigFile* AC_cfg)
414 {
415   string token, parameter;
416   int OutRate = 0;
417
418   token = AC_cfg->GetValue("NAME");
419   Output->SetFilename(token);
420   token = AC_cfg->GetValue("TYPE");
421   Output->SetType(token);
422   AC_cfg->GetNextConfigLine();
423
424   while ((token = AC_cfg->GetValue()) != string("/OUTPUT")) {
425     *AC_cfg >> parameter;
426     if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
427     if (parameter == "SIMULATION") {
428       *AC_cfg >> parameter;
429       if (parameter == "ON") SubSystems += ssSimulation;
430     }
431     if (parameter == "AEROSURFACES") {
432       *AC_cfg >> parameter;
433       if (parameter == "ON") SubSystems += ssAerosurfaces;
434     }
435     if (parameter == "RATES") {
436       *AC_cfg >> parameter;
437       if (parameter == "ON") SubSystems += ssRates;
438     }
439     if (parameter == "VELOCITIES") {
440       *AC_cfg >> parameter;
441       if (parameter == "ON") SubSystems += ssVelocities;
442     }
443     if (parameter == "FORCES") {
444       *AC_cfg >> parameter;
445       if (parameter == "ON") SubSystems += ssForces;
446     }
447     if (parameter == "MOMENTS") {
448       *AC_cfg >> parameter;
449       if (parameter == "ON") SubSystems += ssMoments;
450     }
451     if (parameter == "ATMOSPHERE") {
452       *AC_cfg >> parameter;
453       if (parameter == "ON") SubSystems += ssAtmosphere;
454     }
455     if (parameter == "MASSPROPS") {
456       *AC_cfg >> parameter;
457       if (parameter == "ON") SubSystems += ssMassProps;
458     }
459     if (parameter == "POSITION") {
460       *AC_cfg >> parameter;
461       if (parameter == "ON") SubSystems += ssPosition;
462     }
463     if (parameter == "COEFFICIENTS") {
464       *AC_cfg >> parameter;
465       if (parameter == "ON") SubSystems += ssCoefficients;
466     }
467     if (parameter == "GROUND_REACTIONS") {
468       *AC_cfg >> parameter;
469       if (parameter == "ON") SubSystems += ssGroundReactions;
470     }
471     if (parameter == "FCS") {
472       *AC_cfg >> parameter;
473       if (parameter == "ON") SubSystems += ssFCS;
474     }
475     if (parameter == "PROPULSION") {
476       *AC_cfg >> parameter;
477       if (parameter == "ON") SubSystems += ssPropulsion;
478     }
479   }
480
481   OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
482   rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
483
484   return true;
485 }
486
487 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488 //    The bitmasked value choices are as follows:
489 //    unset: In this case (the default) JSBSim would only print
490 //       out the normally expected messages, essentially echoing
491 //       the config files as they are read. If the environment
492 //       variable is not set, debug_lvl is set to 1 internally
493 //    0: This requests JSBSim not to output any messages
494 //       whatsoever.
495 //    1: This value explicity requests the normal JSBSim
496 //       startup messages
497 //    2: This value asks for a message to be printed out when
498 //       a class is instantiated
499 //    4: When this value is set, a message is displayed when a
500 //       FGModel object executes its Run() method
501 //    8: When this value is set, various runtime state variables
502 //       are printed out periodically
503 //    16: When set various parameters are sanity checked and
504 //       a message is printed out when they go out of bounds
505
506 void FGOutput::Debug(int from)
507 {
508   if (debug_lvl <= 0) return;
509
510   if (debug_lvl & 1) { // Standard console startup message output
511     if (from == 0) { // Constructor
512
513     }
514   }
515   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
516     if (from == 0) cout << "Instantiated: FGOutput" << endl;
517     if (from == 1) cout << "Destroyed:    FGOutput" << endl;
518   }
519   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
520   }
521   if (debug_lvl & 8 ) { // Runtime state variables
522   }
523   if (debug_lvl & 16) { // Sanity checking
524   }
525   if (debug_lvl & 64) {
526     if (from == 0) { // Constructor
527       cout << IdSrc << endl;
528       cout << IdHdr << endl;
529     }
530   }
531 }
532