1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Manage output of sim parameters to file or stdout
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
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
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
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.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This is the place where you create output routines to dump data for perusal
34 --------------------------------------------------------------------------------
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43 #include "FGFDMExec.h"
44 #include "FGAtmosphere.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 #include "FGInertial.h"
58 static const char *IdSrc = "$Id$";
59 static const char *IdHdr = ID_OUTPUT;
61 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65 FGOutput::FGOutput(FGFDMExec* fdmex) : FGModel(fdmex)
68 sFirstPass = dFirstPass = true;
74 outputInFileName = "";
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 if (socket) delete socket;
84 for (unsigned int i=0; i<OutputProperties.size(); i++) delete OutputProperties[i];
89 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 bool FGOutput::Run(void)
94 if (!FGModel::Run()) {
96 if (Type == otSocket) {
98 } else if (Type == otCSV) {
99 DelimitedOutput(Filename);
100 } else if (Type == otTerminal) {
102 } else if (Type == otNone) {
105 // Not a valid type of output
115 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 void FGOutput::SetType(string type)
121 } else if (type == "TABULAR") {
123 } else if (type == "SOCKET") {
125 } else if (type == "TERMINAL") {
127 } else if (type != string("NONE")) {
129 cerr << "Unknown type of output specified in config file" << endl;
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 void FGOutput::DelimitedOutput(string fname)
140 if (fname == "COUT" || fname == "cout") {
141 buffer = cout.rdbuf();
143 datafile.open(fname.c_str());
144 buffer = datafile.rdbuf();
147 ostream outstream(buffer);
151 if (SubSystems & ssSimulation) {
154 if (SubSystems & ssAerosurfaces) {
156 outstream << "Aileron Cmd, ";
157 outstream << "Elevator Cmd, ";
158 outstream << "Rudder Cmd, ";
159 outstream << "Flap Cmd, ";
160 outstream << "Left Aileron Pos, ";
161 outstream << "Right Aileron Pos, ";
162 outstream << "Elevator Pos, ";
163 outstream << "Rudder Pos, ";
164 outstream << "Flap Pos";
166 if (SubSystems & ssRates) {
168 outstream << "P, Q, R, ";
169 outstream << "Pdot, Qdot, Rdot";
171 if (SubSystems & ssVelocities) {
173 outstream << "QBar, ";
174 outstream << "Vtotal, ";
175 outstream << "UBody, VBody, WBody, ";
176 outstream << "UAero, VAero, WAero, ";
177 outstream << "Vn, Ve, Vd";
179 if (SubSystems & ssForces) {
181 outstream << "Drag, Side, Lift, ";
182 outstream << "L/D, ";
183 outstream << "Xforce, Yforce, Zforce, ";
184 outstream << "xGravity, yGravity, zGravity, ";
185 outstream << "xCoriolis, yCoriolis, zCoriolis, ";
186 outstream << "xCentrifugal, yCentrifugal, zCentrifugal";
188 if (SubSystems & ssMoments) {
190 outstream << "L, M, N";
192 if (SubSystems & ssAtmosphere) {
194 outstream << "Rho, ";
195 outstream << "NWind, EWind, DWind";
197 if (SubSystems & ssMassProps) {
199 outstream << "Ixx, ";
200 outstream << "Ixy, ";
201 outstream << "Ixz, ";
202 outstream << "Iyx, ";
203 outstream << "Iyy, ";
204 outstream << "Iyz, ";
205 outstream << "Izx, ";
206 outstream << "Izy, ";
207 outstream << "Izz, ";
208 outstream << "Mass, ";
209 outstream << "Xcg, Ycg, Zcg";
211 if (SubSystems & ssPosition) {
213 outstream << "Altitude, ";
214 outstream << "Phi, Tht, Psi, ";
215 outstream << "Alpha, ";
216 outstream << "Beta, ";
217 outstream << "Latitude, ";
218 outstream << "Longitude, ";
219 outstream << "Distance AGL, ";
220 outstream << "Runway Radius";
222 if (SubSystems & ssCoefficients) {
223 scratch = Aerodynamics->GetCoefficientStrings();
224 if (scratch.length() != 0) outstream << ", " << scratch;
226 if (SubSystems & ssFCS) {
227 scratch = FCS->GetComponentStrings();
228 if (scratch.length() != 0) outstream << ", " << scratch;
230 if (SubSystems & ssGroundReactions) {
232 outstream << GroundReactions->GetGroundReactionStrings();
234 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
236 outstream << Propulsion->GetPropulsionStrings();
238 if (OutputProperties.size() > 0) {
239 for (unsigned int i=0;i<OutputProperties.size();i++) {
240 outstream << ", " << OutputProperties[i]->GetName();
248 outstream << State->Getsim_time();
249 if (SubSystems & ssSimulation) {
251 if (SubSystems & ssAerosurfaces) {
253 outstream << FCS->GetDaCmd() << ", ";
254 outstream << FCS->GetDeCmd() << ", ";
255 outstream << FCS->GetDrCmd() << ", ";
256 outstream << FCS->GetDfCmd() << ", ";
257 outstream << FCS->GetDaLPos() << ", ";
258 outstream << FCS->GetDaRPos() << ", ";
259 outstream << FCS->GetDePos() << ", ";
260 outstream << FCS->GetDrPos() << ", ";
261 outstream << FCS->GetDfPos();
263 if (SubSystems & ssRates) {
265 outstream << Rotation->GetPQR() << ", ";
266 outstream << Rotation->GetPQRdot();
268 if (SubSystems & ssVelocities) {
270 outstream << Translation->Getqbar() << ", ";
271 outstream << Translation->GetVt() << ", ";
272 outstream << Translation->GetUVW() << ", ";
273 outstream << Translation->GetAeroUVW() << ", ";
274 outstream << Position->GetVel();
276 if (SubSystems & ssForces) {
278 outstream << Aerodynamics->GetvFs() << ", ";
279 outstream << Aerodynamics->GetLoD() << ", ";
280 outstream << Aircraft->GetForces() << ", ";
281 outstream << Inertial->GetGravity() << ", ";
282 outstream << Inertial->GetCoriolis() << ", ";
283 outstream << Inertial->GetCentrifugal();
285 if (SubSystems & ssMoments) {
287 outstream << Aircraft->GetMoments();
289 if (SubSystems & ssAtmosphere) {
291 outstream << Atmosphere->GetDensity() << ", ";
292 outstream << Atmosphere->GetWindNED();
294 if (SubSystems & ssMassProps) {
296 outstream << MassBalance->GetJ() << ", ";
297 outstream << MassBalance->GetMass() << ", ";
298 outstream << MassBalance->GetXYZcg();
300 if (SubSystems & ssPosition) {
302 outstream << Position->Geth() << ", ";
303 outstream << Rotation->GetEuler() << ", ";
304 outstream << Translation->Getalpha() << ", ";
305 outstream << Translation->Getbeta() << ", ";
306 outstream << Position->GetLatitude() << ", ";
307 outstream << Position->GetLongitude() << ", ";
308 outstream << Position->GetDistanceAGL() << ", ";
309 outstream << Position->GetRunwayRadius();
311 if (SubSystems & ssCoefficients) {
312 scratch = Aerodynamics->GetCoefficientValues();
313 if (scratch.length() != 0) outstream << ", " << scratch;
315 if (SubSystems & ssFCS) {
316 scratch = FCS->GetComponentValues();
317 if (scratch.length() != 0) outstream << ", " << scratch;
319 if (SubSystems & ssGroundReactions) {
321 outstream << GroundReactions->GetGroundReactionValues();
323 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
325 outstream << Propulsion->GetPropulsionValues();
328 for (unsigned int i=0;i<OutputProperties.size();i++) {
329 outstream << ", " << OutputProperties[i]->getDoubleValue();
336 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 void FGOutput::SocketOutput(void)
342 if (socket == NULL) return;
343 if (!socket->GetConnectStatus()) return;
347 socket->Append("<LABELS>");
348 socket->Append("Time");
349 socket->Append("Altitude");
350 socket->Append("Phi");
351 socket->Append("Tht");
352 socket->Append("Psi");
353 socket->Append("Rho");
354 socket->Append("Vtotal");
355 socket->Append("UBody");
356 socket->Append("VBody");
357 socket->Append("WBody");
358 socket->Append("UAero");
359 socket->Append("VAero");
360 socket->Append("WAero");
361 socket->Append("Vn");
362 socket->Append("Ve");
363 socket->Append("Vd");
364 socket->Append("Udot");
365 socket->Append("Vdot");
366 socket->Append("Wdot");
370 socket->Append("PDot");
371 socket->Append("QDot");
372 socket->Append("RDot");
373 socket->Append("Fx");
374 socket->Append("Fy");
375 socket->Append("Fz");
376 socket->Append("Latitude");
377 socket->Append("Longitude");
378 socket->Append("QBar");
379 socket->Append("Alpha");
383 socket->Append("Throttle Position");
384 socket->Append("Left Aileron Position");
385 socket->Append("Right Aileron Position");
386 socket->Append("Elevator Position");
387 socket->Append("Rudder Position");
393 socket->Append(State->Getsim_time());
394 socket->Append(Position->Geth());
395 socket->Append(Rotation->Getphi());
396 socket->Append(Rotation->Gettht());
397 socket->Append(Rotation->Getpsi());
398 socket->Append(Atmosphere->GetDensity());
399 socket->Append(Translation->GetVt());
400 socket->Append(Translation->GetUVW(eU));
401 socket->Append(Translation->GetUVW(eV));
402 socket->Append(Translation->GetUVW(eW));
403 socket->Append(Translation->GetAeroUVW(eU));
404 socket->Append(Translation->GetAeroUVW(eV));
405 socket->Append(Translation->GetAeroUVW(eW));
406 socket->Append(Position->GetVn());
407 socket->Append(Position->GetVe());
408 socket->Append(Position->GetVd());
409 socket->Append(Translation->GetUVWdot(eU));
410 socket->Append(Translation->GetUVWdot(eV));
411 socket->Append(Translation->GetUVWdot(eW));
412 socket->Append(Rotation->GetPQR(eP));
413 socket->Append(Rotation->GetPQR(eQ));
414 socket->Append(Rotation->GetPQR(eR));
415 socket->Append(Rotation->GetPQRdot(eP));
416 socket->Append(Rotation->GetPQRdot(eQ));
417 socket->Append(Rotation->GetPQRdot(eR));
418 socket->Append(Aircraft->GetForces(eX));
419 socket->Append(Aircraft->GetForces(eY));
420 socket->Append(Aircraft->GetForces(eZ));
421 socket->Append(Position->GetLatitude());
422 socket->Append(Position->GetLongitude());
423 socket->Append(Translation->Getqbar());
424 socket->Append(Translation->Getalpha());
425 socket->Append(Aircraft->GetMoments(eL));
426 socket->Append(Aircraft->GetMoments(eM));
427 socket->Append(Aircraft->GetMoments(eN));
428 socket->Append(FCS->GetThrottlePos(0));
429 socket->Append(FCS->GetDaLPos());
430 socket->Append(FCS->GetDaRPos());
431 socket->Append(FCS->GetDePos());
432 socket->Append(FCS->GetDrPos());
436 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 void FGOutput::SocketStatusOutput(string out_str)
442 if (socket == NULL) return;
445 asciiData = string("<STATUS>") + out_str;
446 socket->Append(asciiData.c_str());
450 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 bool FGOutput::Load(FGConfigFile* AC_cfg)
454 string token="", parameter="", separator="";
455 string name="", fname="";
457 FGConfigFile* Output_cfg;
466 name = AC_cfg->GetValue("NAME");
467 fname = AC_cfg->GetValue("FILE");
468 token = AC_cfg->GetValue("TYPE");
469 Output->SetType(token);
471 #if defined( FG_WITH_JSBSIM_SOCKET ) || !defined( FGFS )
472 if (token == "SOCKET") {
473 socket = new FGfdmSocket("localhost",1138);
477 if (!fname.empty()) {
478 outputInFileName = FDMExec->GetAircraftPath() + separator
479 + FDMExec->GetModelName() + separator + fname + ".xml";
480 Output_cfg = new FGConfigFile(outputInFileName);
481 if (!Output_cfg->IsOpen()) {
482 cerr << "Could not open file: " << outputInFileName << endl;
488 Output->SetFilename(name);
490 while ((token = Output_cfg->GetValue()) != string("/OUTPUT")) {
491 *Output_cfg >> parameter;
492 if (parameter == "RATE_IN_HZ") {
493 *Output_cfg >> OutRate;
495 if (parameter == "SIMULATION") {
496 *Output_cfg >> parameter;
497 if (parameter == "ON") SubSystems += ssSimulation;
499 if (parameter == "AEROSURFACES") {
500 *Output_cfg >> parameter;
501 if (parameter == "ON") SubSystems += ssAerosurfaces;
503 if (parameter == "RATES") {
504 *Output_cfg >> parameter;
505 if (parameter == "ON") SubSystems += ssRates;
507 if (parameter == "VELOCITIES") {
508 *Output_cfg >> parameter;
509 if (parameter == "ON") SubSystems += ssVelocities;
511 if (parameter == "FORCES") {
512 *Output_cfg >> parameter;
513 if (parameter == "ON") SubSystems += ssForces;
515 if (parameter == "MOMENTS") {
516 *Output_cfg >> parameter;
517 if (parameter == "ON") SubSystems += ssMoments;
519 if (parameter == "ATMOSPHERE") {
520 *Output_cfg >> parameter;
521 if (parameter == "ON") SubSystems += ssAtmosphere;
523 if (parameter == "MASSPROPS") {
524 *Output_cfg >> parameter;
525 if (parameter == "ON") SubSystems += ssMassProps;
527 if (parameter == "POSITION") {
528 *Output_cfg >> parameter;
529 if (parameter == "ON") SubSystems += ssPosition;
531 if (parameter == "COEFFICIENTS") {
532 *Output_cfg >> parameter;
533 if (parameter == "ON") SubSystems += ssCoefficients;
535 if (parameter == "GROUND_REACTIONS") {
536 *Output_cfg >> parameter;
537 if (parameter == "ON") SubSystems += ssGroundReactions;
539 if (parameter == "FCS") {
540 *Output_cfg >> parameter;
541 if (parameter == "ON") SubSystems += ssFCS;
543 if (parameter == "PROPULSION") {
544 *Output_cfg >> parameter;
545 if (parameter == "ON") SubSystems += ssPropulsion;
547 if (parameter == "PROPERTY") {
548 *Output_cfg >> property;
549 OutputProperties.push_back(PropertyManager->GetNode(property));
552 if (parameter == "EOF") break;
555 OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
556 rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
563 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564 // The bitmasked value choices are as follows:
565 // unset: In this case (the default) JSBSim would only print
566 // out the normally expected messages, essentially echoing
567 // the config files as they are read. If the environment
568 // variable is not set, debug_lvl is set to 1 internally
569 // 0: This requests JSBSim not to output any messages
571 // 1: This value explicity requests the normal JSBSim
573 // 2: This value asks for a message to be printed out when
574 // a class is instantiated
575 // 4: When this value is set, a message is displayed when a
576 // FGModel object executes its Run() method
577 // 8: When this value is set, various runtime state variables
578 // are printed out periodically
579 // 16: When set various parameters are sanity checked and
580 // a message is printed out when they go out of bounds
582 void FGOutput::Debug(int from)
586 if (debug_lvl <= 0) return;
588 if (debug_lvl & 1) { // Standard console startup message output
589 if (from == 0) { // Constructor
593 if (outputInFileName.empty())
594 cout << " " << "Output parameters read inline" << endl;
596 cout << " Output parameters read from file: " << outputInFileName << endl;
598 if (Filename == "cout" || Filename == "COUT") {
599 scratch = " Log output goes to screen console";
600 } else if (!Filename.empty()) {
601 scratch = " Log output goes to file: " + Filename;
605 cout << scratch << " in CSV format output at rate " << 120/rate << " Hz" << endl;
608 cout << " No log output" << endl;
612 if (SubSystems & ssSimulation) cout << " Simulation parameters logged" << endl;
613 if (SubSystems & ssAerosurfaces) cout << " Aerosurface parameters logged" << endl;
614 if (SubSystems & ssRates) cout << " Rate parameters logged" << endl;
615 if (SubSystems & ssVelocities) cout << " Velocity parameters logged" << endl;
616 if (SubSystems & ssForces) cout << " Force parameters logged" << endl;
617 if (SubSystems & ssMoments) cout << " Moments parameters logged" << endl;
618 if (SubSystems & ssAtmosphere) cout << " Atmosphere parameters logged" << endl;
619 if (SubSystems & ssMassProps) cout << " Mass parameters logged" << endl;
620 if (SubSystems & ssCoefficients) cout << " Coefficient parameters logged" << endl;
621 if (SubSystems & ssPosition) cout << " Position parameters logged" << endl;
622 if (SubSystems & ssGroundReactions) cout << " Ground parameters logged" << endl;
623 if (SubSystems & ssFCS) cout << " FCS parameters logged" << endl;
624 if (SubSystems & ssPropulsion) cout << " Propulsion parameters logged" << endl;
627 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
628 if (from == 0) cout << "Instantiated: FGOutput" << endl;
629 if (from == 1) cout << "Destroyed: FGOutput" << endl;
631 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
633 if (debug_lvl & 8 ) { // Runtime state variables
635 if (debug_lvl & 16) { // Sanity checking
637 if (debug_lvl & 64) {
638 if (from == 0) { // Constructor
639 cout << IdSrc << endl;
640 cout << IdHdr << endl;