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"
57 static const char *IdSrc = "$Id$";
58 static const char *IdHdr = ID_OUTPUT;
60 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64 FGOutput::FGOutput(FGFDMExec* fdmex) : FGModel(fdmex)
67 sFirstPass = dFirstPass = true;
73 outputInFileName = "";
78 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 if (socket) delete socket;
83 for (int i=0; i<OutputProperties.size(); i++) delete OutputProperties[i];
88 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 bool FGOutput::Run(void)
93 if (!FGModel::Run()) {
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
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 void FGOutput::SetType(string type)
120 } else if (type == "TABULAR") {
122 } else if (type == "SOCKET") {
124 } else if (type == "TERMINAL") {
126 } else if (type != string("NONE")) {
128 cerr << "Unknown type of output specified in config file" << endl;
132 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 void FGOutput::DelimitedOutput(string fname)
138 if (fname == "COUT" || fname == "cout") {
139 buffer = cout.rdbuf();
141 datafile.open(fname.c_str());
142 buffer = datafile.rdbuf();
145 ostream outstream(buffer);
149 if (SubSystems & ssSimulation) {
152 if (SubSystems & ssAerosurfaces) {
154 outstream << "Aileron Cmd, ";
155 outstream << "Elevator Cmd, ";
156 outstream << "Rudder Cmd, ";
157 outstream << "Flap Cmd, ";
158 outstream << "Left Aileron Pos, ";
159 outstream << "Right Aileron Pos, ";
160 outstream << "Elevator Pos, ";
161 outstream << "Rudder Pos, ";
162 outstream << "Flap Pos";
164 if (SubSystems & ssRates) {
166 outstream << "P, Q, R, ";
167 outstream << "Pdot, Qdot, Rdot";
169 if (SubSystems & ssVelocities) {
171 outstream << "QBar, ";
172 outstream << "Vtotal, ";
173 outstream << "UBody, VBody, WBody, ";
174 outstream << "UAero, VAero, WAero, ";
175 outstream << "Vn, Ve, Vd";
177 if (SubSystems & ssForces) {
179 outstream << "Drag, Side, Lift, ";
180 outstream << "L/D, ";
181 outstream << "Xforce, Yforce, Zforce, ";
182 outstream << "xGravity, yGravity, zGravity, ";
183 outstream << "xCoriolis, yCoriolis, zCoriolis, ";
184 outstream << "xCentrifugal, yCentrifugal, zCentrifugal";
186 if (SubSystems & ssMoments) {
188 outstream << "L, M, N";
190 if (SubSystems & ssAtmosphere) {
192 outstream << "Rho, ";
193 outstream << "NWind, EWind, DWind";
195 if (SubSystems & ssMassProps) {
197 outstream << "Ixx, ";
198 outstream << "Iyy, ";
199 outstream << "Izz, ";
200 outstream << "Ixz, ";
201 outstream << "Mass, ";
202 outstream << "Xcg, Ycg, Zcg";
204 if (SubSystems & ssPosition) {
206 outstream << "Altitude, ";
207 outstream << "Phi, Tht, Psi, ";
208 outstream << "Alpha, ";
209 outstream << "Beta, ";
210 outstream << "Latitude, ";
211 outstream << "Longitude, ";
212 outstream << "Distance AGL, ";
213 outstream << "Runway Radius";
215 if (SubSystems & ssCoefficients) {
217 outstream << Aerodynamics->GetCoefficientStrings();
219 if (SubSystems & ssFCS) {
221 outstream << FCS->GetComponentStrings();
223 if (SubSystems & ssGroundReactions) {
225 outstream << GroundReactions->GetGroundReactionStrings();
227 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
229 outstream << Propulsion->GetPropulsionStrings();
231 if (OutputProperties.size() > 0) {
232 for (int i=0;i<OutputProperties.size();i++) {
233 outstream << ", " << OutputProperties[i]->GetName();
241 outstream << State->Getsim_time();
242 if (SubSystems & ssSimulation) {
244 if (SubSystems & ssAerosurfaces) {
246 outstream << FCS->GetDaCmd() << ", ";
247 outstream << FCS->GetDeCmd() << ", ";
248 outstream << FCS->GetDrCmd() << ", ";
249 outstream << FCS->GetDfCmd() << ", ";
250 outstream << FCS->GetDaLPos() << ", ";
251 outstream << FCS->GetDaRPos() << ", ";
252 outstream << FCS->GetDePos() << ", ";
253 outstream << FCS->GetDrPos() << ", ";
254 outstream << FCS->GetDfPos();
256 if (SubSystems & ssRates) {
258 outstream << Rotation->GetPQR() << ", ";
259 outstream << Rotation->GetPQRdot();
261 if (SubSystems & ssVelocities) {
263 outstream << Translation->Getqbar() << ", ";
264 outstream << Translation->GetVt() << ", ";
265 outstream << Translation->GetUVW() << ", ";
266 outstream << Translation->GetAeroUVW() << ", ";
267 outstream << Position->GetVel();
269 if (SubSystems & ssForces) {
271 outstream << Aerodynamics->GetvFs() << ", ";
272 outstream << Aerodynamics->GetLoD() << ", ";
273 outstream << Aircraft->GetForces() << ", ";
274 outstream << Inertial->GetGravity() << ", ";
275 outstream << Inertial->GetCoriolis() << ", ";
276 outstream << Inertial->GetCentrifugal();
278 if (SubSystems & ssMoments) {
280 outstream << Aircraft->GetMoments();
282 if (SubSystems & ssAtmosphere) {
284 outstream << Atmosphere->GetDensity() << ", ";
285 outstream << Atmosphere->GetWindNED();
287 if (SubSystems & ssMassProps) {
289 outstream << MassBalance->GetIxx() << ", ";
290 outstream << MassBalance->GetIyy() << ", ";
291 outstream << MassBalance->GetIzz() << ", ";
292 outstream << MassBalance->GetIxz() << ", ";
293 outstream << MassBalance->GetMass() << ", ";
294 outstream << MassBalance->GetXYZcg();
296 if (SubSystems & ssPosition) {
298 outstream << Position->Geth() << ", ";
299 outstream << Rotation->GetEuler() << ", ";
300 outstream << Translation->Getalpha() << ", ";
301 outstream << Translation->Getbeta() << ", ";
302 outstream << Position->GetLatitude() << ", ";
303 outstream << Position->GetLongitude() << ", ";
304 outstream << Position->GetDistanceAGL() << ", ";
305 outstream << Position->GetRunwayRadius();
307 if (SubSystems & ssCoefficients) {
309 outstream << Aerodynamics->GetCoefficientValues();
311 if (SubSystems & ssFCS) {
313 outstream << FCS->GetComponentValues();
315 if (SubSystems & ssGroundReactions) {
317 outstream << GroundReactions->GetGroundReactionValues();
319 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
321 outstream << Propulsion->GetPropulsionValues();
324 for (int i=0;i<OutputProperties.size();i++) {
325 outstream << ", " << OutputProperties[i]->getDoubleValue();
332 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 void FGOutput::SocketOutput(void)
338 if (socket == NULL) return;
339 if (!socket->GetConnectStatus()) return;
343 socket->Append("<LABELS>");
344 socket->Append("Time");
345 socket->Append("Altitude");
346 socket->Append("Phi");
347 socket->Append("Tht");
348 socket->Append("Psi");
349 socket->Append("Rho");
350 socket->Append("Vtotal");
351 socket->Append("UBody");
352 socket->Append("VBody");
353 socket->Append("WBody");
354 socket->Append("UAero");
355 socket->Append("VAero");
356 socket->Append("WAero");
357 socket->Append("Vn");
358 socket->Append("Ve");
359 socket->Append("Vd");
360 socket->Append("Udot");
361 socket->Append("Vdot");
362 socket->Append("Wdot");
366 socket->Append("PDot");
367 socket->Append("QDot");
368 socket->Append("RDot");
369 socket->Append("Fx");
370 socket->Append("Fy");
371 socket->Append("Fz");
372 socket->Append("Latitude");
373 socket->Append("Longitude");
374 socket->Append("QBar");
375 socket->Append("Alpha");
379 socket->Append("Throttle Position");
380 socket->Append("Left Aileron Position");
381 socket->Append("Right Aileron Position");
382 socket->Append("Elevator Position");
383 socket->Append("Rudder Position");
389 socket->Append(State->Getsim_time());
390 socket->Append(Position->Geth());
391 socket->Append(Rotation->Getphi());
392 socket->Append(Rotation->Gettht());
393 socket->Append(Rotation->Getpsi());
394 socket->Append(Atmosphere->GetDensity());
395 socket->Append(Translation->GetVt());
396 socket->Append(Translation->GetUVW(eU));
397 socket->Append(Translation->GetUVW(eV));
398 socket->Append(Translation->GetUVW(eW));
399 socket->Append(Translation->GetAeroUVW(eU));
400 socket->Append(Translation->GetAeroUVW(eV));
401 socket->Append(Translation->GetAeroUVW(eW));
402 socket->Append(Position->GetVn());
403 socket->Append(Position->GetVe());
404 socket->Append(Position->GetVd());
405 socket->Append(Translation->GetUVWdot(eU));
406 socket->Append(Translation->GetUVWdot(eV));
407 socket->Append(Translation->GetUVWdot(eW));
408 socket->Append(Rotation->GetPQR(eP));
409 socket->Append(Rotation->GetPQR(eQ));
410 socket->Append(Rotation->GetPQR(eR));
411 socket->Append(Rotation->GetPQRdot(eP));
412 socket->Append(Rotation->GetPQRdot(eQ));
413 socket->Append(Rotation->GetPQRdot(eR));
414 socket->Append(Aircraft->GetForces(eX));
415 socket->Append(Aircraft->GetForces(eY));
416 socket->Append(Aircraft->GetForces(eZ));
417 socket->Append(Position->GetLatitude());
418 socket->Append(Position->GetLongitude());
419 socket->Append(Translation->Getqbar());
420 socket->Append(Translation->Getalpha());
421 socket->Append(Aircraft->GetMoments(eL));
422 socket->Append(Aircraft->GetMoments(eM));
423 socket->Append(Aircraft->GetMoments(eN));
424 socket->Append(FCS->GetThrottlePos(0));
425 socket->Append(FCS->GetDaLPos());
426 socket->Append(FCS->GetDaRPos());
427 socket->Append(FCS->GetDePos());
428 socket->Append(FCS->GetDrPos());
432 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 void FGOutput::SocketStatusOutput(string out_str)
438 if (socket == NULL) return;
441 asciiData = string("<STATUS>") + out_str;
442 socket->Append(asciiData.c_str());
446 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448 bool FGOutput::Load(FGConfigFile* AC_cfg)
450 string token="", parameter="", separator="";
451 string name="", fname="";
453 FGConfigFile* Output_cfg;
462 name = AC_cfg->GetValue("NAME");
463 fname = AC_cfg->GetValue("FILE");
464 token = AC_cfg->GetValue("TYPE");
465 Output->SetType(token);
467 #if defined( FG_WITH_JSBSIM_SOCKET ) || !defined( FGFS )
468 if (token == "SOCKET") {
469 socket = new FGfdmSocket("localhost",1138);
473 if (!fname.empty()) {
474 outputInFileName = FDMExec->GetAircraftPath() + separator
475 + FDMExec->GetModelName() + separator + fname + ".xml";
476 Output_cfg = new FGConfigFile(outputInFileName);
477 if (!Output_cfg->IsOpen()) {
478 cerr << "Could not open file: " << outputInFileName << endl;
484 Output->SetFilename(name);
486 while ((token = Output_cfg->GetValue()) != string("/OUTPUT")) {
487 *Output_cfg >> parameter;
488 if (parameter == "RATE_IN_HZ") {
489 *Output_cfg >> OutRate;
491 if (parameter == "SIMULATION") {
492 *Output_cfg >> parameter;
493 if (parameter == "ON") SubSystems += ssSimulation;
495 if (parameter == "AEROSURFACES") {
496 *Output_cfg >> parameter;
497 if (parameter == "ON") SubSystems += ssAerosurfaces;
499 if (parameter == "RATES") {
500 *Output_cfg >> parameter;
501 if (parameter == "ON") SubSystems += ssRates;
503 if (parameter == "VELOCITIES") {
504 *Output_cfg >> parameter;
505 if (parameter == "ON") SubSystems += ssVelocities;
507 if (parameter == "FORCES") {
508 *Output_cfg >> parameter;
509 if (parameter == "ON") SubSystems += ssForces;
511 if (parameter == "MOMENTS") {
512 *Output_cfg >> parameter;
513 if (parameter == "ON") SubSystems += ssMoments;
515 if (parameter == "ATMOSPHERE") {
516 *Output_cfg >> parameter;
517 if (parameter == "ON") SubSystems += ssAtmosphere;
519 if (parameter == "MASSPROPS") {
520 *Output_cfg >> parameter;
521 if (parameter == "ON") SubSystems += ssMassProps;
523 if (parameter == "POSITION") {
524 *Output_cfg >> parameter;
525 if (parameter == "ON") SubSystems += ssPosition;
527 if (parameter == "COEFFICIENTS") {
528 *Output_cfg >> parameter;
529 if (parameter == "ON") SubSystems += ssCoefficients;
531 if (parameter == "GROUND_REACTIONS") {
532 *Output_cfg >> parameter;
533 if (parameter == "ON") SubSystems += ssGroundReactions;
535 if (parameter == "FCS") {
536 *Output_cfg >> parameter;
537 if (parameter == "ON") SubSystems += ssFCS;
539 if (parameter == "PROPULSION") {
540 *Output_cfg >> parameter;
541 if (parameter == "ON") SubSystems += ssPropulsion;
543 if (parameter == "PROPERTY") {
544 *Output_cfg >> property;
545 OutputProperties.push_back(PropertyManager->GetNode(property));
548 if (parameter == "EOF") break;
551 OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
552 rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
559 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 // The bitmasked value choices are as follows:
561 // unset: In this case (the default) JSBSim would only print
562 // out the normally expected messages, essentially echoing
563 // the config files as they are read. If the environment
564 // variable is not set, debug_lvl is set to 1 internally
565 // 0: This requests JSBSim not to output any messages
567 // 1: This value explicity requests the normal JSBSim
569 // 2: This value asks for a message to be printed out when
570 // a class is instantiated
571 // 4: When this value is set, a message is displayed when a
572 // FGModel object executes its Run() method
573 // 8: When this value is set, various runtime state variables
574 // are printed out periodically
575 // 16: When set various parameters are sanity checked and
576 // a message is printed out when they go out of bounds
578 void FGOutput::Debug(int from)
582 if (debug_lvl <= 0) return;
584 if (debug_lvl & 1) { // Standard console startup message output
585 if (from == 0) { // Constructor
589 if (outputInFileName.empty())
590 cout << " " << "Output parameters read inline" << endl;
592 cout << " Output parameters read from file: " << outputInFileName << endl;
594 if (Filename == "cout" || Filename == "COUT") {
595 scratch = " Log output goes to screen console";
596 } else if (!Filename.empty()) {
597 scratch = " Log output goes to file: " + Filename;
601 cout << scratch << " in CSV format output at rate " << 120/rate << " Hz" << endl;
604 cout << " No log output" << endl;
608 if (SubSystems & ssSimulation) cout << " Simulation parameters logged" << endl;
609 if (SubSystems & ssAerosurfaces) cout << " Aerosurface parameters logged" << endl;
610 if (SubSystems & ssRates) cout << " Rate parameters logged" << endl;
611 if (SubSystems & ssVelocities) cout << " Velocity parameters logged" << endl;
612 if (SubSystems & ssForces) cout << " Force parameters logged" << endl;
613 if (SubSystems & ssMoments) cout << " Moments parameters logged" << endl;
614 if (SubSystems & ssAtmosphere) cout << " Atmosphere parameters logged" << endl;
615 if (SubSystems & ssMassProps) cout << " Mass parameters logged" << endl;
616 if (SubSystems & ssCoefficients) cout << " Coefficient parameters logged" << endl;
617 if (SubSystems & ssPosition) cout << " Position parameters logged" << endl;
618 if (SubSystems & ssGroundReactions) cout << " Ground parameters logged" << endl;
619 if (SubSystems & ssFCS) cout << " FCS parameters logged" << endl;
620 if (SubSystems & ssPropulsion) cout << " Propulsion parameters logged" << endl;
623 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
624 if (from == 0) cout << "Instantiated: FGOutput" << endl;
625 if (from == 1) cout << "Destroyed: FGOutput" << endl;
627 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
629 if (debug_lvl & 8 ) { // Runtime state variables
631 if (debug_lvl & 16) { // Sanity checking
633 if (debug_lvl & 64) {
634 if (from == 0) { // Constructor
635 cout << IdSrc << endl;
636 cout << IdHdr << endl;