]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGOutput.cpp
Synced with latest JSBSim. X15 works for me, but C172 segfaults.
[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 "FGAircraft.h"
47 #include "FGTranslation.h"
48 #include "FGRotation.h"
49 #include "FGPosition.h"
50 #include "FGAuxiliary.h"
51
52 static const char *IdSrc = "$Id$";
53 static const char *IdHdr = ID_OUTPUT;
54
55 extern short debug_lvl;
56
57 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 CLASS IMPLEMENTATION
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60
61 FGOutput::FGOutput(FGFDMExec* fdmex) : FGModel(fdmex)
62 {
63   Name = "FGOutput";
64   sFirstPass = dFirstPass = true;
65   socket = 0;
66   Type = otNone;
67   Filename = "JSBSim.out";
68   SubSystems = 0;
69   enabled = true;
70   
71 #ifdef FG_WITH_JSBSIM_SOCKET
72   socket = new FGfdmSocket("localhost",1138);
73 #endif
74   if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
75 }
76
77 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78
79 FGOutput::~FGOutput()
80 {
81   if (socket) delete socket;
82   if (debug_lvl & 2) cout << "Destroyed:    FGOutput" << endl;
83 }
84
85 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86
87 bool FGOutput::Run(void)
88 {
89   if (enabled) {
90     if (!FGModel::Run()) {
91
92       if (Type == otSocket) {
93         SocketOutput();
94       } else if (Type == otCSV) {
95         if (Filename != "COUT" && Filename != "cout" && Filename.size() > 0) {
96           DelimitedOutput(Filename);
97         } else {
98           DelimitedOutput();
99         }
100       } else if (Type == otTerminal) {
101         // Not done yet
102       } else if (Type == otNone) {
103         // Do nothing
104       } else {
105         // Not a valid type of output
106       }
107
108     } else {
109     }
110   }
111   return false;
112 }
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116 void FGOutput::SetType(string type)
117 {
118   if (type == "CSV") {
119     Type = otCSV;
120   } else if (type == "TABULAR") {
121     Type = otTab;
122   } else if (type == "SOCKET") {
123     Type = otSocket;
124   } else if (type == "TERMINAL") {
125     Type = otTerminal;
126   } else if (type != "NONE"){
127     Type = otUnknown;
128     cerr << "Unknown type of output specified in config file" << endl;
129   }
130 }
131
132 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133
134 void FGOutput::DelimitedOutput(void)
135 {
136   if (dFirstPass) {
137     cout << "Time";
138     if (SubSystems & FGAircraft::ssSimulation) {
139       // Nothing here, yet
140     }
141     if (SubSystems & FGAircraft::ssAerosurfaces) {
142       cout << ", ";
143       cout << "Throttle, ";
144       cout << "Aileron Cmd, ";
145       cout << "Elevator Cmd, ";
146       cout << "Rudder Cmd, ";
147       cout << "Aileron Pos, ";
148       cout << "Elevator Pos, ";
149       cout << "Rudder Pos";
150     }
151     if (SubSystems & FGAircraft::ssRates) {
152       cout << ", ";
153       cout << "P, Q, R";
154     }
155     if (SubSystems & FGAircraft::ssVelocities) {
156       cout << ", ";
157       cout << "QBar, ";
158       cout << "Vtotal, ";
159       cout << "U, V, W, ";
160       cout << "Vn, Ve, Vd";
161     }
162     if (SubSystems & FGAircraft::ssForces) {
163       cout << ", ";
164       cout << "Drag, Side, Lift, ";
165       cout << "L/D, ";
166       cout << "Xforce, Yforce, Zforce";
167     }
168     if (SubSystems & FGAircraft::ssMoments) {
169       cout << ", ";
170       cout << "L, M, N";
171     }
172     if (SubSystems & FGAircraft::ssAtmosphere) {
173       cout << ", ";
174       cout << "Rho";
175     }
176     if (SubSystems & FGAircraft::ssMassProps) {
177       cout << ", ";
178       cout << "Ixx, ";
179       cout << "Iyy, ";
180       cout << "Izz, ";
181       cout << "Ixz, ";
182       cout << "Mass, ";
183       cout << "Xcg, Ycg, Zcg";
184     }
185     if (SubSystems & FGAircraft::ssPosition) {
186       cout << ", ";
187       cout << "Altitude, ";
188       cout << "Phi, Tht, Psi, ";
189       cout << "Alpha, ";
190       cout << "Latitude, ";
191       cout << "Longitude, ";
192       cout << "Distance AGL, ";
193       cout << "Runway Radius";
194     }
195     if (SubSystems & FGAircraft::ssCoefficients) {
196       cout << ", ";
197       cout << Aircraft->GetCoefficientStrings();
198     }
199     if (SubSystems & FGAircraft::ssGroundReactions) {
200       cout << ", ";
201       cout << Aircraft->GetGroundReactionStrings();
202     }
203     if (SubSystems & FGAircraft::ssPropulsion) {
204       cout << ", ";
205       cout << Propulsion->GetPropulsionStrings();
206     }
207
208     cout << endl;
209     dFirstPass = false;
210   }
211
212   cout << State->Getsim_time();
213   if (SubSystems & FGAircraft::ssSimulation) {
214   }
215   if (SubSystems & FGAircraft::ssAerosurfaces) {
216     cout << ", ";
217     cout << FCS->GetThrottlePos(0) << ", ";
218     cout << FCS->GetDaCmd() << ", ";
219     cout << FCS->GetDeCmd() << ", ";
220     cout << FCS->GetDrCmd() << ", ";
221     cout << FCS->GetDaPos() << ", ";
222     cout << FCS->GetDePos() << ", ";
223     cout << FCS->GetDrPos();
224   }
225   if (SubSystems & FGAircraft::ssRates) {
226     cout << ", ";
227     cout << Rotation->GetPQR();
228   }
229   if (SubSystems & FGAircraft::ssVelocities) {
230     cout << ", ";
231     cout << Translation->Getqbar() << ", ";
232     cout << Translation->GetVt() << ", ";
233     cout << Translation->GetUVW() << ", ";
234     cout << Position->GetVel();
235   }
236   if (SubSystems & FGAircraft::ssForces) {
237     cout << ", ";
238     cout << Aircraft->GetvFs() << ", ";
239     cout << Aircraft->GetLoD() << ", ";
240     cout << Aircraft->GetForces();
241   }
242   if (SubSystems & FGAircraft::ssMoments) {
243     cout << ", ";
244     cout << Aircraft->GetMoments();
245   }
246   if (SubSystems & FGAircraft::ssAtmosphere) {
247     cout << ", ";
248     cout << Atmosphere->GetDensity();
249   }
250   if (SubSystems & FGAircraft::ssMassProps) {
251     cout << ", ";
252     cout << Aircraft->GetIxx() << ", ";
253     cout << Aircraft->GetIyy() << ", ";
254     cout << Aircraft->GetIzz() << ", ";
255     cout << Aircraft->GetIxz() << ", ";
256     cout << Aircraft->GetMass() << ", ";
257     cout << Aircraft->GetXYZcg();
258   }
259   if (SubSystems & FGAircraft::ssPosition) {
260     cout << ", ";
261     cout << Position->Geth() << ", ";
262     cout << Rotation->GetEuler() << ", ";
263     cout << Translation->Getalpha() << ", ";
264     cout << Position->GetLatitude() << ", ";
265     cout << Position->GetLongitude() << ", ";
266     cout << Position->GetDistanceAGL() << ", ";
267     cout << Position->GetRunwayRadius();
268   }
269   if (SubSystems & FGAircraft::ssCoefficients) {
270     cout << ", ";
271     cout << Aircraft->GetCoefficientValues();
272   }
273   if (SubSystems & FGAircraft::ssGroundReactions) {
274     cout << ", ";
275     cout << Aircraft->GetGroundReactionValues();
276   }
277   if (SubSystems & FGAircraft::ssPropulsion) {
278     cout << ", ";
279     cout << Propulsion->GetPropulsionValues();
280   }
281
282   cout << endl;
283 }
284
285 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286
287 void FGOutput::DelimitedOutput(string fname)
288 {
289   if (sFirstPass) {
290     datafile.open(fname.c_str());
291     datafile << "Time";
292     if (SubSystems & FGAircraft::ssSimulation) {
293       // Nothing here, yet
294     }
295     if (SubSystems & FGAircraft::ssAerosurfaces) {
296       datafile << ", ";
297       datafile << "Throttle, ";
298       datafile << "Aileron Cmd, ";
299       datafile << "Elevator Cmd, ";
300       datafile << "Rudder Cmd, ";
301       datafile << "Aileron Pos, ";
302       datafile << "Elevator Pos, ";
303       datafile << "Rudder Pos";
304     }
305     if (SubSystems & FGAircraft::ssRates) {
306       datafile << ", ";
307       datafile << "P, Q, R";
308     }
309     if (SubSystems & FGAircraft::ssVelocities) {
310       datafile << ", ";
311       datafile << "QBar, ";
312       datafile << "Vtotal, ";
313       datafile << "U, V, W, ";
314       datafile << "Vn, Ve, Vd";
315     }
316     if (SubSystems & FGAircraft::ssForces) {
317       datafile << ", ";
318       datafile << "Drag, Side, Lift, ";
319       datafile << "L/D, ";
320       datafile << "Xforce, Yforce, Zforce";
321     }
322     if (SubSystems & FGAircraft::ssMoments) {
323       datafile << ", ";
324       datafile << "L, M, N";
325     }
326     if (SubSystems & FGAircraft::ssAtmosphere) {
327       datafile << ", ";
328       datafile << "Rho";
329     }
330     if (SubSystems & FGAircraft::ssMassProps) {
331       datafile << ", ";
332       datafile << "Ixx, ";
333       datafile << "Iyy, ";
334       datafile << "Izz, ";
335       datafile << "Ixz, ";
336       datafile << "Mass, ";
337       datafile << "Xcg, Ycg, Zcg";
338     }
339     if (SubSystems & FGAircraft::ssPosition) {
340       datafile << ", ";
341       datafile << "Altitude, ";
342       datafile << "Phi, Tht, Psi, ";
343       datafile << "Alpha, ";
344       datafile << "Latitude, ";
345       datafile << "Longitude, ";
346       datafile << "Distance AGL, ";
347       datafile << "Runway Radius";
348     }
349     if (SubSystems & FGAircraft::ssCoefficients) {
350       datafile << ", ";
351       datafile << Aircraft->GetCoefficientStrings();
352     }
353     if (SubSystems & FGAircraft::ssGroundReactions) {
354       datafile << ", ";
355       datafile << Aircraft->GetGroundReactionStrings();
356     }
357     if (SubSystems & FGAircraft::ssFCS) {
358       datafile << ", ";
359       datafile << FCS->GetComponentStrings();
360     }
361     if (SubSystems & FGAircraft::ssPropulsion) {
362       datafile << ", ";
363       datafile << Propulsion->GetPropulsionStrings();
364     }
365     datafile << endl;
366     sFirstPass = false;
367   }
368
369   datafile << State->Getsim_time();
370   if (SubSystems & FGAircraft::ssSimulation) {
371   }
372   if (SubSystems & FGAircraft::ssAerosurfaces) {
373     datafile << ", ";
374     datafile << FCS->GetThrottlePos(0) << ", ";
375     datafile << FCS->GetDaCmd() << ", ";
376     datafile << FCS->GetDeCmd() << ", ";
377     datafile << FCS->GetDrCmd() << ", ";
378     datafile << FCS->GetDaPos() << ", ";
379     datafile << FCS->GetDePos() << ", ";
380     datafile << FCS->GetDrPos();
381   }
382   if (SubSystems & FGAircraft::ssRates) {
383     datafile << ", ";
384     datafile << Rotation->GetPQR();
385   }
386   if (SubSystems & FGAircraft::ssVelocities) {
387     datafile << ", ";
388     datafile << Translation->Getqbar() << ", ";
389     datafile << Translation->GetVt() << ", ";
390     datafile << Translation->GetUVW() << ", ";
391     datafile << Position->GetVel();
392   }
393   if (SubSystems & FGAircraft::ssForces) {
394     datafile << ", ";
395     datafile << Aircraft->GetvFs() << ", ";
396     datafile << Aircraft->GetLoD() << ", ";
397     datafile << Aircraft->GetForces();
398   }
399   if (SubSystems & FGAircraft::ssMoments) {
400     datafile << ", ";
401     datafile << Aircraft->GetMoments();
402   }
403   if (SubSystems & FGAircraft::ssAtmosphere) {
404     datafile << ", ";
405     datafile << Atmosphere->GetDensity();
406   }
407   if (SubSystems & FGAircraft::ssMassProps) {
408     datafile << ", ";
409     datafile << Aircraft->GetIxx() << ", ";
410     datafile << Aircraft->GetIyy() << ", ";
411     datafile << Aircraft->GetIzz() << ", ";
412     datafile << Aircraft->GetIxz() << ", ";
413     datafile << Aircraft->GetMass() << ", ";
414     datafile << Aircraft->GetXYZcg();
415   }
416   if (SubSystems & FGAircraft::ssPosition) {
417     datafile << ", ";
418     datafile << Position->Geth() << ", ";
419     datafile << Rotation->GetEuler() << ", ";
420     datafile << Translation->Getalpha() << ", ";
421     datafile << Position->GetLatitude() << ", ";
422     datafile << Position->GetLongitude() << ", ";
423     datafile << Position->GetDistanceAGL() << ", ";
424     datafile << Position->GetRunwayRadius();
425   }
426   if (SubSystems & FGAircraft::ssCoefficients) {
427     datafile << ", ";
428     datafile << Aircraft->GetCoefficientValues();
429   }
430   if (SubSystems & FGAircraft::ssGroundReactions) {
431     datafile << ", ";
432     datafile << Aircraft->GetGroundReactionValues();
433   }
434   if (SubSystems & FGAircraft::ssFCS) {
435     datafile << ", ";
436     datafile << FCS->GetComponentValues();
437   }
438   if (SubSystems & FGAircraft::ssPropulsion) {
439     datafile << ", ";
440     datafile << Propulsion->GetPropulsionValues();
441   }
442   datafile << endl;
443   datafile.flush();
444 }
445
446 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447
448 void FGOutput::SocketOutput(void)
449 {
450   string asciiData;
451   /*
452   if (socket == NULL) return;
453
454   socket->Clear();
455   if (sFirstPass) {
456     socket->Append("<LABELS>");
457     socket->Append("Time");
458     socket->Append("Altitude");
459     socket->Append("Phi");
460     socket->Append("Tht");
461     socket->Append("Psi");
462     socket->Append("Rho");
463     socket->Append("Vtotal");
464     socket->Append("U");
465     socket->Append("V");
466     socket->Append("W");
467     socket->Append("Vn");
468     socket->Append("Ve");
469     socket->Append("Vd");
470     socket->Append("Udot");
471     socket->Append("Vdot");
472     socket->Append("Wdot");
473     socket->Append("P");
474     socket->Append("Q");
475     socket->Append("R");
476     socket->Append("PDot");
477     socket->Append("QDot");
478     socket->Append("RDot");
479     socket->Append("Fx");
480     socket->Append("Fy");
481     socket->Append("Fz");
482     socket->Append("Latitude");
483     socket->Append("Longitude");
484     socket->Append("QBar");
485     socket->Append("Alpha");
486     socket->Append("L");
487     socket->Append("M");
488     socket->Append("N");
489     socket->Append("Throttle");
490     socket->Append("Aileron");
491     socket->Append("Elevator");
492     socket->Append("Rudder");
493     sFirstPass = false;
494     socket->Send();
495   }
496
497   socket->Clear();
498   socket->Append(State->Getsim_time());
499   socket->Append(Position->Geth());
500   socket->Append(Rotation->Getphi());
501   socket->Append(Rotation->Gettht());
502   socket->Append(Rotation->Getpsi());
503   socket->Append(Atmosphere->GetDensity());
504   socket->Append(Translation->GetVt());
505   socket->Append(Translation->GetU());
506   socket->Append(Translation->GetV());
507   socket->Append(Translation->GetW());
508   socket->Append(Position->GetVn());
509   socket->Append(Position->GetVe());
510   socket->Append(Position->GetVd());
511   socket->Append(Translation->GetUdot());
512   socket->Append(Translation->GetVdot());
513   socket->Append(Translation->GetWdot());
514   socket->Append(Rotation->GetP());
515   socket->Append(Rotation->GetQ());
516   socket->Append(Rotation->GetR());
517   socket->Append(Rotation->GetPdot());
518   socket->Append(Rotation->GetQdot());
519   socket->Append(Rotation->GetRdot());
520   socket->Append(Aircraft->GetFx());
521   socket->Append(Aircraft->GetFy());
522   socket->Append(Aircraft->GetFz());
523   socket->Append(Position->GetLatitude());
524   socket->Append(Position->GetLongitude());
525   socket->Append(Translation->Getqbar());
526   socket->Append(Translation->Getalpha());
527   socket->Append(Aircraft->GetL());
528   socket->Append(Aircraft->GetM());
529   socket->Append(Aircraft->GetN());
530   socket->Append(FCS->GetThrottle(0));
531   socket->Append(FCS->GetDa());
532   socket->Append(FCS->GetDe());
533   socket->Append(FCS->GetDr());
534   socket->Send(); */
535 }
536
537 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538
539 void FGOutput::SocketStatusOutput(string out_str)
540 {
541   string asciiData;
542
543   if (socket == NULL) return;
544
545   socket->Clear();
546   asciiData = string("<STATUS>") + out_str;
547   socket->Append(asciiData.c_str());
548   socket->Send();
549 }
550
551 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552
553 void FGOutput::Debug(void)
554 {
555     //TODO: Add your source code here
556 }
557