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