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