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 "FGPropagate.h"
51 #include "FGAuxiliary.h"
52 #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()) return true;
95 if (Type == otSocket) {
97 } else if (Type == otCSV) {
98 DelimitedOutput(Filename);
99 } else if (Type == otTerminal) {
101 } else if (Type == otNone) {
104 // Not a valid type of output
110 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 void FGOutput::SetType(string type)
116 } else if (type == "TABULAR") {
118 } else if (type == "SOCKET") {
120 } else if (type == "TERMINAL") {
122 } else if (type != string("NONE")) {
124 cerr << "Unknown type of output specified in config file" << endl;
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 void FGOutput::DelimitedOutput(string fname)
135 if (fname == "COUT" || fname == "cout") {
136 buffer = cout.rdbuf();
138 datafile.open(fname.c_str());
139 buffer = datafile.rdbuf();
142 ostream outstream(buffer);
146 if (SubSystems & ssSimulation) {
149 if (SubSystems & ssAerosurfaces) {
151 outstream << "Aileron Cmd, ";
152 outstream << "Elevator Cmd, ";
153 outstream << "Rudder Cmd, ";
154 outstream << "Flap Cmd, ";
155 outstream << "Left Aileron Pos, ";
156 outstream << "Right Aileron Pos, ";
157 outstream << "Elevator Pos, ";
158 outstream << "Rudder Pos, ";
159 outstream << "Flap Pos";
161 if (SubSystems & ssRates) {
163 outstream << "P, Q, R, ";
164 outstream << "Pdot, Qdot, Rdot";
166 if (SubSystems & ssVelocities) {
168 outstream << "QBar, ";
169 outstream << "Vtotal, ";
170 outstream << "UBody, VBody, WBody, ";
171 outstream << "UAero, VAero, WAero, ";
172 outstream << "Vn, Ve, Vd";
174 if (SubSystems & ssForces) {
176 outstream << "Drag, Side, Lift, ";
177 outstream << "L/D, ";
178 outstream << "Xforce, Yforce, Zforce";
180 if (SubSystems & ssMoments) {
182 outstream << "L, M, N";
184 if (SubSystems & ssAtmosphere) {
186 outstream << "Rho, ";
187 outstream << "NWind, EWind, DWind";
189 if (SubSystems & ssMassProps) {
191 outstream << "Ixx, ";
192 outstream << "Ixy, ";
193 outstream << "Ixz, ";
194 outstream << "Iyx, ";
195 outstream << "Iyy, ";
196 outstream << "Iyz, ";
197 outstream << "Izx, ";
198 outstream << "Izy, ";
199 outstream << "Izz, ";
200 outstream << "Mass, ";
201 outstream << "Xcg, Ycg, Zcg";
203 if (SubSystems & ssPropagate) {
205 outstream << "Altitude, ";
206 outstream << "Phi, Tht, Psi, ";
207 outstream << "Alpha, ";
208 outstream << "Beta, ";
209 outstream << "Latitude (Deg), ";
210 outstream << "Longitude (Deg), ";
211 outstream << "Distance AGL, ";
212 outstream << "Runway Radius";
214 if (SubSystems & ssCoefficients) {
215 scratch = Aerodynamics->GetCoefficientStrings();
216 if (scratch.length() != 0) outstream << ", " << scratch;
218 if (SubSystems & ssFCS) {
219 scratch = FCS->GetComponentStrings();
220 if (scratch.length() != 0) outstream << ", " << scratch;
222 if (SubSystems & ssGroundReactions) {
224 outstream << GroundReactions->GetGroundReactionStrings();
226 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
228 outstream << Propulsion->GetPropulsionStrings();
230 if (OutputProperties.size() > 0) {
231 for (unsigned int i=0;i<OutputProperties.size();i++) {
232 outstream << ", " << OutputProperties[i]->GetName();
240 outstream << State->Getsim_time();
241 if (SubSystems & ssSimulation) {
243 if (SubSystems & ssAerosurfaces) {
245 outstream << FCS->GetDaCmd() << ", ";
246 outstream << FCS->GetDeCmd() << ", ";
247 outstream << FCS->GetDrCmd() << ", ";
248 outstream << FCS->GetDfCmd() << ", ";
249 outstream << FCS->GetDaLPos() << ", ";
250 outstream << FCS->GetDaRPos() << ", ";
251 outstream << FCS->GetDePos() << ", ";
252 outstream << FCS->GetDrPos() << ", ";
253 outstream << FCS->GetDfPos();
255 if (SubSystems & ssRates) {
257 outstream << Propagate->GetPQR() << ", ";
258 outstream << Propagate->GetPQRdot();
260 if (SubSystems & ssVelocities) {
262 outstream << Auxiliary->Getqbar() << ", ";
263 outstream << setprecision(12) << Auxiliary->GetVt() << ", ";
264 outstream << setprecision(12) << Propagate->GetUVW() << ", ";
265 outstream << Auxiliary->GetAeroUVW() << ", ";
266 outstream << Propagate->GetVel();
268 if (SubSystems & ssForces) {
270 outstream << Aerodynamics->GetvFs() << ", ";
271 outstream << Aerodynamics->GetLoD() << ", ";
272 outstream << Aircraft->GetForces();
274 if (SubSystems & ssMoments) {
276 outstream << Aircraft->GetMoments();
278 if (SubSystems & ssAtmosphere) {
280 outstream << Atmosphere->GetDensity() << ", ";
281 outstream << Atmosphere->GetWindNED();
283 if (SubSystems & ssMassProps) {
285 outstream << MassBalance->GetJ() << ", ";
286 outstream << MassBalance->GetMass() << ", ";
287 outstream << MassBalance->GetXYZcg();
289 if (SubSystems & ssPropagate) {
291 outstream << Propagate->Geth() << ", ";
292 outstream << Propagate->GetEuler() << ", ";
293 outstream << Auxiliary->Getalpha(inDegrees) << ", ";
294 outstream << Auxiliary->Getbeta(inDegrees) << ", ";
295 outstream << Propagate->GetLocation().GetLatitudeDeg() << ", ";
296 outstream << Propagate->GetLocation().GetLongitudeDeg() << ", ";
297 outstream << Propagate->GetDistanceAGL() << ", ";
298 outstream << Propagate->GetRunwayRadius();
300 if (SubSystems & ssCoefficients) {
301 scratch = Aerodynamics->GetCoefficientValues();
302 if (scratch.length() != 0) outstream << ", " << scratch;
304 if (SubSystems & ssFCS) {
305 scratch = FCS->GetComponentValues();
306 if (scratch.length() != 0) outstream << ", " << scratch;
308 if (SubSystems & ssGroundReactions) {
310 outstream << GroundReactions->GetGroundReactionValues();
312 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
314 outstream << Propulsion->GetPropulsionValues();
317 for (unsigned int i=0;i<OutputProperties.size();i++) {
318 outstream << ", " << OutputProperties[i]->getDoubleValue();
325 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 void FGOutput::SocketOutput(void)
331 if (socket == NULL) return;
332 if (!socket->GetConnectStatus()) return;
336 socket->Append("<LABELS>");
337 socket->Append("Time");
338 socket->Append("Altitude");
339 socket->Append("Phi");
340 socket->Append("Tht");
341 socket->Append("Psi");
342 socket->Append("Rho");
343 socket->Append("Vtotal");
344 socket->Append("UBody");
345 socket->Append("VBody");
346 socket->Append("WBody");
347 socket->Append("UAero");
348 socket->Append("VAero");
349 socket->Append("WAero");
350 socket->Append("Vn");
351 socket->Append("Ve");
352 socket->Append("Vd");
353 socket->Append("Udot");
354 socket->Append("Vdot");
355 socket->Append("Wdot");
359 socket->Append("PDot");
360 socket->Append("QDot");
361 socket->Append("RDot");
362 socket->Append("Fx");
363 socket->Append("Fy");
364 socket->Append("Fz");
365 socket->Append("Latitude (Deg)");
366 socket->Append("Longitude (Deg)");
367 socket->Append("QBar");
368 socket->Append("Alpha");
372 socket->Append("Throttle Position");
373 socket->Append("Left Aileron Position");
374 socket->Append("Right Aileron Position");
375 socket->Append("Elevator Position");
376 socket->Append("Rudder Position");
382 socket->Append(State->Getsim_time());
383 socket->Append(Propagate->Geth());
384 socket->Append(Propagate->GetEuler(ePhi));
385 socket->Append(Propagate->GetEuler(eTht));
386 socket->Append(Propagate->GetEuler(ePsi));
387 socket->Append(Atmosphere->GetDensity());
388 socket->Append(Auxiliary->GetVt());
389 socket->Append(Propagate->GetUVW(eU));
390 socket->Append(Propagate->GetUVW(eV));
391 socket->Append(Propagate->GetUVW(eW));
392 socket->Append(Auxiliary->GetAeroUVW(eU));
393 socket->Append(Auxiliary->GetAeroUVW(eV));
394 socket->Append(Auxiliary->GetAeroUVW(eW));
395 socket->Append(Propagate->GetVel(eNorth));
396 socket->Append(Propagate->GetVel(eEast));
397 socket->Append(Propagate->GetVel(eDown));
398 socket->Append(Propagate->GetUVWdot(eU));
399 socket->Append(Propagate->GetUVWdot(eV));
400 socket->Append(Propagate->GetUVWdot(eW));
401 socket->Append(Propagate->GetPQR(eP));
402 socket->Append(Propagate->GetPQR(eQ));
403 socket->Append(Propagate->GetPQR(eR));
404 socket->Append(Propagate->GetPQRdot(eP));
405 socket->Append(Propagate->GetPQRdot(eQ));
406 socket->Append(Propagate->GetPQRdot(eR));
407 socket->Append(Aircraft->GetForces(eX));
408 socket->Append(Aircraft->GetForces(eY));
409 socket->Append(Aircraft->GetForces(eZ));
410 socket->Append(Propagate->GetLocation().GetLatitudeDeg());
411 socket->Append(Propagate->GetLocation().GetLongitudeDeg());
412 socket->Append(Auxiliary->Getqbar());
413 socket->Append(Auxiliary->Getalpha(inDegrees));
414 socket->Append(Aircraft->GetMoments(eL));
415 socket->Append(Aircraft->GetMoments(eM));
416 socket->Append(Aircraft->GetMoments(eN));
417 socket->Append(FCS->GetThrottlePos(0));
418 socket->Append(FCS->GetDaLPos());
419 socket->Append(FCS->GetDaRPos());
420 socket->Append(FCS->GetDePos());
421 socket->Append(FCS->GetDrPos());
425 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 void FGOutput::SocketStatusOutput(string out_str)
431 if (socket == NULL) return;
434 asciiData = string("<STATUS>") + out_str;
435 socket->Append(asciiData.c_str());
439 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
441 bool FGOutput::Load(FGConfigFile* AC_cfg)
443 string token="", parameter="", separator="";
444 string name="", fname="";
446 FGConfigFile* Output_cfg;
456 name = AC_cfg->GetValue("NAME");
457 fname = AC_cfg->GetValue("FILE");
458 token = AC_cfg->GetValue("TYPE");
459 port = atoi(AC_cfg->GetValue("PORT").c_str());
461 Output->SetType(token);
463 if (token == "SOCKET") {
464 socket = new FGfdmSocket(name,port);
467 if (!fname.empty()) {
468 outputInFileName = FDMExec->GetAircraftPath() + separator
469 + FDMExec->GetModelName() + separator + fname + ".xml";
470 Output_cfg = new FGConfigFile(outputInFileName);
471 if (!Output_cfg->IsOpen()) {
472 cerr << "Could not open file: " << outputInFileName << endl;
478 Output->SetFilename(name);
480 while ((token = Output_cfg->GetValue()) != string("/OUTPUT")) {
481 *Output_cfg >> parameter;
482 if (parameter == "RATE_IN_HZ") {
483 *Output_cfg >> OutRate;
485 if (parameter == "SIMULATION") {
486 *Output_cfg >> parameter;
487 if (parameter == "ON") SubSystems += ssSimulation;
489 if (parameter == "AEROSURFACES") {
490 *Output_cfg >> parameter;
491 if (parameter == "ON") SubSystems += ssAerosurfaces;
493 if (parameter == "RATES") {
494 *Output_cfg >> parameter;
495 if (parameter == "ON") SubSystems += ssRates;
497 if (parameter == "VELOCITIES") {
498 *Output_cfg >> parameter;
499 if (parameter == "ON") SubSystems += ssVelocities;
501 if (parameter == "FORCES") {
502 *Output_cfg >> parameter;
503 if (parameter == "ON") SubSystems += ssForces;
505 if (parameter == "MOMENTS") {
506 *Output_cfg >> parameter;
507 if (parameter == "ON") SubSystems += ssMoments;
509 if (parameter == "ATMOSPHERE") {
510 *Output_cfg >> parameter;
511 if (parameter == "ON") SubSystems += ssAtmosphere;
513 if (parameter == "MASSPROPS") {
514 *Output_cfg >> parameter;
515 if (parameter == "ON") SubSystems += ssMassProps;
517 if (parameter == "POSITION") {
518 *Output_cfg >> parameter;
519 if (parameter == "ON") SubSystems += ssPropagate;
521 if (parameter == "COEFFICIENTS") {
522 *Output_cfg >> parameter;
523 if (parameter == "ON") SubSystems += ssCoefficients;
525 if (parameter == "GROUND_REACTIONS") {
526 *Output_cfg >> parameter;
527 if (parameter == "ON") SubSystems += ssGroundReactions;
529 if (parameter == "FCS") {
530 *Output_cfg >> parameter;
531 if (parameter == "ON") SubSystems += ssFCS;
533 if (parameter == "PROPULSION") {
534 *Output_cfg >> parameter;
535 if (parameter == "ON") SubSystems += ssPropulsion;
537 if (parameter == "PROPERTY") {
538 *Output_cfg >> property;
539 OutputProperties.push_back(PropertyManager->GetNode(property));
542 if (parameter == "EOF") break;
545 OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
546 rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
553 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 // The bitmasked value choices are as follows:
555 // unset: In this case (the default) JSBSim would only print
556 // out the normally expected messages, essentially echoing
557 // the config files as they are read. If the environment
558 // variable is not set, debug_lvl is set to 1 internally
559 // 0: This requests JSBSim not to output any messages
561 // 1: This value explicity requests the normal JSBSim
563 // 2: This value asks for a message to be printed out when
564 // a class is instantiated
565 // 4: When this value is set, a message is displayed when a
566 // FGModel object executes its Run() method
567 // 8: When this value is set, various runtime state variables
568 // are printed out periodically
569 // 16: When set various parameters are sanity checked and
570 // a message is printed out when they go out of bounds
572 void FGOutput::Debug(int from)
576 if (debug_lvl <= 0) return;
578 if (debug_lvl & 1) { // Standard console startup message output
579 if (from == 0) { // Constructor
583 if (outputInFileName.empty())
584 cout << " " << "Output parameters read inline" << endl;
586 cout << " Output parameters read from file: " << outputInFileName << endl;
588 if (Filename == "cout" || Filename == "COUT") {
589 scratch = " Log output goes to screen console";
590 } else if (!Filename.empty()) {
591 scratch = " Log output goes to file: " + Filename;
595 cout << scratch << " in CSV format output at rate " << 120/rate << " Hz" << endl;
598 cout << " No log output" << endl;
602 if (SubSystems & ssSimulation) cout << " Simulation parameters logged" << endl;
603 if (SubSystems & ssAerosurfaces) cout << " Aerosurface parameters logged" << endl;
604 if (SubSystems & ssRates) cout << " Rate parameters logged" << endl;
605 if (SubSystems & ssVelocities) cout << " Velocity parameters logged" << endl;
606 if (SubSystems & ssForces) cout << " Force parameters logged" << endl;
607 if (SubSystems & ssMoments) cout << " Moments parameters logged" << endl;
608 if (SubSystems & ssAtmosphere) cout << " Atmosphere parameters logged" << endl;
609 if (SubSystems & ssMassProps) cout << " Mass parameters logged" << endl;
610 if (SubSystems & ssCoefficients) cout << " Coefficient parameters logged" << endl;
611 if (SubSystems & ssPropagate) cout << " Propagate parameters logged" << endl;
612 if (SubSystems & ssGroundReactions) cout << " Ground parameters logged" << endl;
613 if (SubSystems & ssFCS) cout << " FCS parameters logged" << endl;
614 if (SubSystems & ssPropulsion) cout << " Propulsion parameters logged" << endl;
615 if (OutputProperties.size() > 0) cout << " Properties logged:" << endl;
616 for (unsigned int i=0;i<OutputProperties.size();i++) {
617 cout << " - " << OutputProperties[i]->GetName() << endl;
621 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
622 if (from == 0) cout << "Instantiated: FGOutput" << endl;
623 if (from == 1) cout << "Destroyed: FGOutput" << endl;
625 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
627 if (debug_lvl & 8 ) { // Runtime state variables
629 if (debug_lvl & 16) { // Sanity checking
631 if (debug_lvl & 64) {
632 if (from == 0) { // Constructor
633 cout << IdSrc << endl;
634 cout << IdHdr << endl;