]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGOutput.cpp
Initial revision.
[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 << "Throttle, ";
152       outstream << "Mixture, ";
153       outstream << "Aileron Cmd, ";
154       outstream << "Elevator Cmd, ";
155       outstream << "Rudder Cmd, ";
156       outstream << "Aileron Pos, ";
157       outstream << "Elevator Pos, ";
158       outstream << "Rudder Pos";
159     }
160     if (SubSystems & ssRates) {
161       outstream << ", ";
162       outstream << "P, Q, R";
163     }
164     if (SubSystems & ssVelocities) {
165       outstream << ", ";
166       outstream << "QBar, ";
167       outstream << "Vtotal, ";
168       outstream << "UBody, VBody, WBody, ";
169       outstream << "UAero, VAero, WAero, ";
170       outstream << "Vn, Ve, Vd";
171     }
172     if (SubSystems & ssForces) {
173       outstream << ", ";
174       outstream << "Drag, Side, Lift, ";
175       outstream << "L/D, ";
176       outstream << "Xforce, Yforce, Zforce";
177     }
178     if (SubSystems & ssMoments) {
179       outstream << ", ";
180       outstream << "L, M, N";
181     }
182     if (SubSystems & ssAtmosphere) {
183       outstream << ", ";
184       outstream << "Rho";
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 << "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) {
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->GetThrottlePos(0) << ", ";
228     outstream << FCS->GetMixturePos(0) << ", ";
229     outstream << FCS->GetDaCmd() << ", ";
230     outstream << FCS->GetDeCmd() << ", ";
231     outstream << FCS->GetDrCmd() << ", ";
232     outstream << FCS->GetDaPos() << ", ";
233     outstream << FCS->GetDePos() << ", ";
234     outstream << FCS->GetDrPos();
235   }
236   if (SubSystems & ssRates) {
237     outstream << ", ";
238     outstream << Rotation->GetPQR();
239   }
240   if (SubSystems & ssVelocities) {
241     outstream << ", ";
242     outstream << Translation->Getqbar() << ", ";
243     outstream << Translation->GetVt() << ", ";
244     outstream << Translation->GetUVW() << ", ";
245     outstream << Translation->GetvAero() << ", ";
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   }
262   if (SubSystems & ssMassProps) {
263     outstream << ", ";
264     outstream << MassBalance->GetIxx() << ", ";
265     outstream << MassBalance->GetIyy() << ", ";
266     outstream << MassBalance->GetIzz() << ", ";
267     outstream << MassBalance->GetIxz() << ", ";
268     outstream << MassBalance->GetMass() << ", ";
269     outstream << MassBalance->GetXYZcg();
270   }
271   if (SubSystems & ssPosition) {
272     outstream << ", ";
273     outstream << Position->Geth() << ", ";
274     outstream << Rotation->GetEuler() << ", ";
275     outstream << Translation->Getalpha() << ", ";
276     outstream << Position->GetLatitude() << ", ";
277     outstream << Position->GetLongitude() << ", ";
278     outstream << Position->GetDistanceAGL() << ", ";
279     outstream << Position->GetRunwayRadius();
280   }
281   if (SubSystems & ssCoefficients) {
282     outstream << ", ";
283     outstream << Aerodynamics->GetCoefficientValues();
284   }
285   if (SubSystems & ssGroundReactions) {
286     outstream << ", ";
287     outstream << GroundReactions->GetGroundReactionValues();
288   }
289   if (SubSystems & ssPropulsion) {
290     outstream << ", ";
291     outstream << Propulsion->GetPropulsionValues();
292   }
293
294   outstream << endl;
295   outstream.flush();
296 }
297
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299
300 void FGOutput::SocketOutput(void)
301 {
302   string asciiData;
303   /*
304   if (socket == NULL) 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");
345     socket->Append("Aileron");
346     socket->Append("Elevator");
347     socket->Append("Rudder");
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->GetvAero(eU));
364   socket->Append(Translation->GetvAero(eV));
365   socket->Append(Translation->GetvAero(eW));
366   socket->Append(Position->GetVn());
367   socket->Append(Position->GetVe());
368   socket->Append(Position->GetVd());
369   socket->Append(Translation->GetUdot());
370   socket->Append(Translation->GetVdot());
371   socket->Append(Translation->GetWdot());
372   socket->Append(Rotation->GetP());
373   socket->Append(Rotation->GetQ());
374   socket->Append(Rotation->GetR());
375   socket->Append(Rotation->GetPdot());
376   socket->Append(Rotation->GetQdot());
377   socket->Append(Rotation->GetRdot());
378   socket->Append(Aircraft->GetFx());
379   socket->Append(Aircraft->GetFy());
380   socket->Append(Aircraft->GetFz());
381   socket->Append(Position->GetLatitude());
382   socket->Append(Position->GetLongitude());
383   socket->Append(Translation->Getqbar());
384   socket->Append(Translation->Getalpha());
385   socket->Append(Aircraft->GetL());
386   socket->Append(Aircraft->GetM());
387   socket->Append(Aircraft->GetN());
388   socket->Append(FCS->GetThrottle(0));
389   socket->Append(FCS->GetDa());
390   socket->Append(FCS->GetDe());
391   socket->Append(FCS->GetDr());
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   AC_cfg->GetNextConfigLine();
421
422   while ((token = AC_cfg->GetValue()) != string("/OUTPUT")) {
423     *AC_cfg >> parameter;
424     if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
425     if (parameter == "SIMULATION") {
426       *AC_cfg >> parameter;
427       if (parameter == "ON") SubSystems += ssSimulation;
428     }
429     if (parameter == "AEROSURFACES") {
430       *AC_cfg >> parameter;
431       if (parameter == "ON") SubSystems += ssAerosurfaces;
432     }
433     if (parameter == "RATES") {
434       *AC_cfg >> parameter;
435       if (parameter == "ON") SubSystems += ssRates;
436     }
437     if (parameter == "VELOCITIES") {
438       *AC_cfg >> parameter;
439       if (parameter == "ON") SubSystems += ssVelocities;
440     }
441     if (parameter == "FORCES") {
442       *AC_cfg >> parameter;
443       if (parameter == "ON") SubSystems += ssForces;
444     }
445     if (parameter == "MOMENTS") {
446       *AC_cfg >> parameter;
447       if (parameter == "ON") SubSystems += ssMoments;
448     }
449     if (parameter == "ATMOSPHERE") {
450       *AC_cfg >> parameter;
451       if (parameter == "ON") SubSystems += ssAtmosphere;
452     }
453     if (parameter == "MASSPROPS") {
454       *AC_cfg >> parameter;
455       if (parameter == "ON") SubSystems += ssMassProps;
456     }
457     if (parameter == "POSITION") {
458       *AC_cfg >> parameter;
459       if (parameter == "ON") SubSystems += ssPosition;
460     }
461     if (parameter == "COEFFICIENTS") {
462       *AC_cfg >> parameter;
463       if (parameter == "ON") SubSystems += ssCoefficients;
464     }
465     if (parameter == "GROUND_REACTIONS") {
466       *AC_cfg >> parameter;
467       if (parameter == "ON") SubSystems += ssGroundReactions;
468     }
469     if (parameter == "FCS") {
470       *AC_cfg >> parameter;
471       if (parameter == "ON") SubSystems += ssFCS;
472     }
473     if (parameter == "PROPULSION") {
474       *AC_cfg >> parameter;
475       if (parameter == "ON") SubSystems += ssPropulsion;
476     }
477   }
478
479   OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
480   rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
481
482   return true;
483 }
484
485 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486
487 void FGOutput::Debug(void)
488 {
489     //TODO: Add your source code here
490 }
491