]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGOutput.cpp
Sync with 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 #ifdef FG_WITH_JSBSIM_SOCKET
73   socket = new FGfdmSocket("localhost",1138);
74 #endif
75   if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
76 }
77
78 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79
80 FGOutput::~FGOutput()
81 {
82   if (socket) delete socket;
83   if (debug_lvl & 2) cout << "Destroyed:    FGOutput" << endl;
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     }
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     }
184     if (SubSystems & ssMassProps) {
185       outstream << ", ";
186       outstream << "Ixx, ";
187       outstream << "Iyy, ";
188       outstream << "Izz, ";
189       outstream << "Ixz, ";
190       outstream << "Mass, ";
191       outstream << "Xcg, Ycg, Zcg";
192     }
193     if (SubSystems & ssPosition) {
194       outstream << ", ";
195       outstream << "Altitude, ";
196       outstream << "Phi, Tht, Psi, ";
197       outstream << "Alpha, ";
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) {
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   }
236   if (SubSystems & ssVelocities) {
237     outstream << ", ";
238     outstream << Translation->Getqbar() << ", ";
239     outstream << Translation->GetVt() << ", ";
240     outstream << Translation->GetUVW() << ", ";
241     outstream << Translation->GetvAeroUVW() << ", ";
242     outstream << Position->GetVel();
243   }
244   if (SubSystems & ssForces) {
245     outstream << ", ";
246     outstream << Aerodynamics->GetvFs() << ", ";
247     outstream << Aerodynamics->GetLoD() << ", ";
248     outstream << Aircraft->GetForces();
249   }
250   if (SubSystems & ssMoments) {
251     outstream << ", ";
252     outstream << Aircraft->GetMoments();
253   }
254   if (SubSystems & ssAtmosphere) {
255     outstream << ", ";
256     outstream << Atmosphere->GetDensity();
257   }
258   if (SubSystems & ssMassProps) {
259     outstream << ", ";
260     outstream << MassBalance->GetIxx() << ", ";
261     outstream << MassBalance->GetIyy() << ", ";
262     outstream << MassBalance->GetIzz() << ", ";
263     outstream << MassBalance->GetIxz() << ", ";
264     outstream << MassBalance->GetMass() << ", ";
265     outstream << MassBalance->GetXYZcg();
266   }
267   if (SubSystems & ssPosition) {
268     outstream << ", ";
269     outstream << Position->Geth() << ", ";
270     outstream << Rotation->GetEuler() << ", ";
271     outstream << Translation->Getalpha() << ", ";
272     outstream << Position->GetLatitude() << ", ";
273     outstream << Position->GetLongitude() << ", ";
274     outstream << Position->GetDistanceAGL() << ", ";
275     outstream << Position->GetRunwayRadius();
276   }
277   if (SubSystems & ssCoefficients) {
278     outstream << ", ";
279     outstream << Aerodynamics->GetCoefficientValues();
280   }
281   if (SubSystems & ssGroundReactions) {
282     outstream << ", ";
283     outstream << GroundReactions->GetGroundReactionValues();
284   }
285   if (SubSystems & ssPropulsion) {
286     outstream << ", ";
287     outstream << Propulsion->GetPropulsionValues();
288   }
289
290   outstream << endl;
291   outstream.flush();
292 }
293
294 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295
296 void FGOutput::SocketOutput(void)
297 {
298   string asciiData;
299   /*
300   if (socket == NULL) return;
301
302   socket->Clear();
303   if (sFirstPass) {
304     socket->Append("<LABELS>");
305     socket->Append("Time");
306     socket->Append("Altitude");
307     socket->Append("Phi");
308     socket->Append("Tht");
309     socket->Append("Psi");
310     socket->Append("Rho");
311     socket->Append("Vtotal");
312     socket->Append("UBody");
313     socket->Append("VBody");
314     socket->Append("WBody");
315     socket->Append("UAero");
316     socket->Append("VAero");
317     socket->Append("WAero");
318     socket->Append("Vn");
319     socket->Append("Ve");
320     socket->Append("Vd");
321     socket->Append("Udot");
322     socket->Append("Vdot");
323     socket->Append("Wdot");
324     socket->Append("P");
325     socket->Append("Q");
326     socket->Append("R");
327     socket->Append("PDot");
328     socket->Append("QDot");
329     socket->Append("RDot");
330     socket->Append("Fx");
331     socket->Append("Fy");
332     socket->Append("Fz");
333     socket->Append("Latitude");
334     socket->Append("Longitude");
335     socket->Append("QBar");
336     socket->Append("Alpha");
337     socket->Append("L");
338     socket->Append("M");
339     socket->Append("N");
340     socket->Append("Throttle");
341     socket->Append("Aileron");
342     socket->Append("Elevator");
343     socket->Append("Rudder");
344     sFirstPass = false;
345     socket->Send();
346   }
347
348   socket->Clear();
349   socket->Append(State->Getsim_time());
350   socket->Append(Position->Geth());
351   socket->Append(Rotation->Getphi());
352   socket->Append(Rotation->Gettht());
353   socket->Append(Rotation->Getpsi());
354   socket->Append(Atmosphere->GetDensity());
355   socket->Append(Translation->GetVt());
356   socket->Append(Translation->GetUVW(eU));
357   socket->Append(Translation->GetUVW(eV));
358   socket->Append(Translation->GetUVW(eW));
359   socket->Append(Translation->GetvAeroUVW(eU));
360   socket->Append(Translation->GetvAeroUVW(eV));
361   socket->Append(Translation->GetvAeroUVW(eW));
362   socket->Append(Position->GetVn());
363   socket->Append(Position->GetVe());
364   socket->Append(Position->GetVd());
365   socket->Append(Translation->GetUdot());
366   socket->Append(Translation->GetVdot());
367   socket->Append(Translation->GetWdot());
368   socket->Append(Rotation->GetP());
369   socket->Append(Rotation->GetQ());
370   socket->Append(Rotation->GetR());
371   socket->Append(Rotation->GetPdot());
372   socket->Append(Rotation->GetQdot());
373   socket->Append(Rotation->GetRdot());
374   socket->Append(Aircraft->GetFx());
375   socket->Append(Aircraft->GetFy());
376   socket->Append(Aircraft->GetFz());
377   socket->Append(Position->GetLatitude());
378   socket->Append(Position->GetLongitude());
379   socket->Append(Translation->Getqbar());
380   socket->Append(Translation->Getalpha());
381   socket->Append(Aircraft->GetL());
382   socket->Append(Aircraft->GetM());
383   socket->Append(Aircraft->GetN());
384   socket->Append(FCS->GetThrottle(0));
385   socket->Append(FCS->GetDa());
386   socket->Append(FCS->GetDe());
387   socket->Append(FCS->GetDr());
388   socket->Send(); */
389 }
390
391 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392
393 void FGOutput::SocketStatusOutput(string out_str)
394 {
395   string asciiData;
396
397   if (socket == NULL) return;
398
399   socket->Clear();
400   asciiData = string("<STATUS>") + out_str;
401   socket->Append(asciiData.c_str());
402   socket->Send();
403 }
404
405 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406
407 bool FGOutput::Load(FGConfigFile* AC_cfg)
408 {
409   string token, parameter;
410   int OutRate = 0;
411
412   token = AC_cfg->GetValue("NAME");
413   Output->SetFilename(token);
414   token = AC_cfg->GetValue("TYPE");
415   Output->SetType(token);
416   AC_cfg->GetNextConfigLine();
417
418   while ((token = AC_cfg->GetValue()) != string("/OUTPUT")) {
419     *AC_cfg >> parameter;
420     if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
421     if (parameter == "SIMULATION") {
422       *AC_cfg >> parameter;
423       if (parameter == "ON") SubSystems += ssSimulation;
424     }
425     if (parameter == "AEROSURFACES") {
426       *AC_cfg >> parameter;
427       if (parameter == "ON") SubSystems += ssAerosurfaces;
428     }
429     if (parameter == "RATES") {
430       *AC_cfg >> parameter;
431       if (parameter == "ON") SubSystems += ssRates;
432     }
433     if (parameter == "VELOCITIES") {
434       *AC_cfg >> parameter;
435       if (parameter == "ON") SubSystems += ssVelocities;
436     }
437     if (parameter == "FORCES") {
438       *AC_cfg >> parameter;
439       if (parameter == "ON") SubSystems += ssForces;
440     }
441     if (parameter == "MOMENTS") {
442       *AC_cfg >> parameter;
443       if (parameter == "ON") SubSystems += ssMoments;
444     }
445     if (parameter == "ATMOSPHERE") {
446       *AC_cfg >> parameter;
447       if (parameter == "ON") SubSystems += ssAtmosphere;
448     }
449     if (parameter == "MASSPROPS") {
450       *AC_cfg >> parameter;
451       if (parameter == "ON") SubSystems += ssMassProps;
452     }
453     if (parameter == "POSITION") {
454       *AC_cfg >> parameter;
455       if (parameter == "ON") SubSystems += ssPosition;
456     }
457     if (parameter == "COEFFICIENTS") {
458       *AC_cfg >> parameter;
459       if (parameter == "ON") SubSystems += ssCoefficients;
460     }
461     if (parameter == "GROUND_REACTIONS") {
462       *AC_cfg >> parameter;
463       if (parameter == "ON") SubSystems += ssGroundReactions;
464     }
465     if (parameter == "FCS") {
466       *AC_cfg >> parameter;
467       if (parameter == "ON") SubSystems += ssFCS;
468     }
469     if (parameter == "PROPULSION") {
470       *AC_cfg >> parameter;
471       if (parameter == "ON") SubSystems += ssPropulsion;
472     }
473   }
474
475   OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
476   rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
477
478   return true;
479 }
480
481 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482
483 void FGOutput::Debug(void)
484 {
485     //TODO: Add your source code here
486 }
487