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