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 = "";
80 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 if (socket) delete socket;
85 for (unsigned int i=0; i<OutputProperties.size(); i++) delete OutputProperties[i];
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 bool FGOutput::Run(void)
95 if (FGModel::Run()) return true;
96 if (Type == otSocket) {
98 } else if (Type == otCSV || Type == otTab) {
99 DelimitedOutput(Filename);
100 } else if (Type == otTerminal) {
102 } else if (Type == otNone) {
105 // Not a valid type of output
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 void FGOutput::SetType(string type)
118 } else if (type == "TABULAR") {
121 } else if (type == "SOCKET") {
123 } else if (type == "TERMINAL") {
125 } else if (type != string("NONE")) {
127 cerr << "Unknown type of output specified in config file" << endl;
131 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 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) {
153 outstream << delimeter;
154 outstream << "Aileron Cmd" + delimeter;
155 outstream << "Elevator Cmd" + delimeter;
156 outstream << "Rudder Cmd" + delimeter;
157 outstream << "Flap Cmd" + delimeter;
158 outstream << "Left Aileron Pos" + delimeter;
159 outstream << "Right Aileron Pos" + delimeter;
160 outstream << "Elevator Pos" + delimeter;
161 outstream << "Rudder Pos" + delimeter;
162 outstream << "Flap Pos";
164 if (SubSystems & ssRates) {
165 outstream << delimeter;
166 outstream << "P" + delimeter + "Q" + delimeter + "R" + delimeter;
167 outstream << "Pdot" + delimeter + "Qdot" + delimeter + "Rdot";
169 if (SubSystems & ssVelocities) {
170 outstream << delimeter;
171 outstream << "QBar" + delimeter;
172 outstream << "Vtotal" + delimeter;
173 outstream << "UBody" + delimeter + "VBody" + delimeter + "WBody" + delimeter;
174 outstream << "UAero" + delimeter + "VAero" + delimeter + "WAero" + delimeter;
175 outstream << "Vn" + delimeter + "Ve" + delimeter + "Vd";
177 if (SubSystems & ssForces) {
178 outstream << delimeter;
179 outstream << "Drag" + delimeter + "Side" + delimeter + "Lift" + delimeter;
180 outstream << "L/D" + delimeter;
181 outstream << "Xforce" + delimeter + "Yforce" + delimeter + "Zforce";
183 if (SubSystems & ssMoments) {
184 outstream << delimeter;
185 outstream << "L" + delimeter + "M" + delimeter + "N";
187 if (SubSystems & ssAtmosphere) {
188 outstream << delimeter;
189 outstream << "Rho" + delimeter;
190 outstream << "NWind" + delimeter + "EWind" + delimeter + "DWind";
192 if (SubSystems & ssMassProps) {
193 outstream << delimeter;
194 outstream << "Ixx" + delimeter;
195 outstream << "Ixy" + delimeter;
196 outstream << "Ixz" + delimeter;
197 outstream << "Iyx" + delimeter;
198 outstream << "Iyy" + delimeter;
199 outstream << "Iyz" + delimeter;
200 outstream << "Izx" + delimeter;
201 outstream << "Izy" + delimeter;
202 outstream << "Izz" + delimeter;
203 outstream << "Mass" + delimeter;
204 outstream << "Xcg" + delimeter + "Ycg" + delimeter + "Zcg";
206 if (SubSystems & ssPropagate) {
207 outstream << delimeter;
208 outstream << "Altitude" + delimeter;
209 outstream << "Phi" + delimeter + "Tht" + delimeter + "Psi" + delimeter;
210 outstream << "Alpha" + delimeter;
211 outstream << "Beta" + delimeter;
212 outstream << "Latitude (Deg)" + delimeter;
213 outstream << "Longitude (Deg)" + delimeter;
214 outstream << "Distance AGL" + delimeter;
215 outstream << "Runway Radius";
217 if (SubSystems & ssCoefficients) {
218 scratch = Aerodynamics->GetCoefficientStrings(delimeter);
219 if (scratch.length() != 0) outstream << delimeter << scratch;
221 if (SubSystems & ssFCS) {
222 scratch = FCS->GetComponentStrings(delimeter);
223 if (scratch.length() != 0) outstream << delimeter << scratch;
225 if (SubSystems & ssGroundReactions) {
226 outstream << delimeter;
227 outstream << GroundReactions->GetGroundReactionStrings(delimeter);
229 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
230 outstream << delimeter;
231 outstream << Propulsion->GetPropulsionStrings(delimeter);
233 if (OutputProperties.size() > 0) {
234 for (unsigned int i=0;i<OutputProperties.size();i++) {
235 outstream << delimeter << OutputProperties[i]->GetName();
243 outstream << State->Getsim_time();
244 if (SubSystems & ssSimulation) {
246 if (SubSystems & ssAerosurfaces) {
247 outstream << delimeter;
248 outstream << FCS->GetDaCmd() << delimeter;
249 outstream << FCS->GetDeCmd() << delimeter;
250 outstream << FCS->GetDrCmd() << delimeter;
251 outstream << FCS->GetDfCmd() << delimeter;
252 outstream << FCS->GetDaLPos() << delimeter;
253 outstream << FCS->GetDaRPos() << delimeter;
254 outstream << FCS->GetDePos() << delimeter;
255 outstream << FCS->GetDrPos() << delimeter;
256 outstream << FCS->GetDfPos();
258 if (SubSystems & ssRates) {
259 outstream << delimeter;
260 outstream << Propagate->GetPQR().Dump(delimeter) << delimeter;
261 outstream << Propagate->GetPQRdot().Dump(delimeter);
263 if (SubSystems & ssVelocities) {
264 outstream << delimeter;
265 outstream << Auxiliary->Getqbar() << delimeter;
266 outstream << setprecision(12) << Auxiliary->GetVt() << delimeter;
267 outstream << setprecision(12) << Propagate->GetUVW().Dump(delimeter) << delimeter;
268 outstream << Auxiliary->GetAeroUVW().Dump(delimeter) << delimeter;
269 outstream << Propagate->GetVel().Dump(delimeter);
271 if (SubSystems & ssForces) {
272 outstream << delimeter;
273 outstream << Aerodynamics->GetvFs() << delimeter;
274 outstream << Aerodynamics->GetLoD() << delimeter;
275 outstream << Aircraft->GetForces().Dump(delimeter);
277 if (SubSystems & ssMoments) {
278 outstream << delimeter;
279 outstream << Aircraft->GetMoments().Dump(delimeter);
281 if (SubSystems & ssAtmosphere) {
282 outstream << delimeter;
283 outstream << Atmosphere->GetDensity() << delimeter;
284 outstream << Atmosphere->GetWindNED().Dump(delimeter);
286 if (SubSystems & ssMassProps) {
287 outstream << delimeter;
288 outstream << MassBalance->GetJ() << delimeter;
289 outstream << MassBalance->GetMass() << delimeter;
290 outstream << MassBalance->GetXYZcg();
292 if (SubSystems & ssPropagate) {
293 outstream << delimeter;
294 outstream << Propagate->Geth() << delimeter;
295 outstream << Propagate->GetEuler().Dump(delimeter) << delimeter;
296 outstream << Auxiliary->Getalpha(inDegrees) << delimeter;
297 outstream << Auxiliary->Getbeta(inDegrees) << delimeter;
298 outstream << Propagate->GetLocation().GetLatitudeDeg() << delimeter;
299 outstream << Propagate->GetLocation().GetLongitudeDeg() << delimeter;
300 outstream << Propagate->GetDistanceAGL() << delimeter;
301 outstream << Propagate->GetRunwayRadius();
303 if (SubSystems & ssCoefficients) {
304 scratch = Aerodynamics->GetCoefficientValues(delimeter);
305 if (scratch.length() != 0) outstream << delimeter << scratch;
307 if (SubSystems & ssFCS) {
308 scratch = FCS->GetComponentValues(delimeter);
309 if (scratch.length() != 0) outstream << delimeter << scratch;
311 if (SubSystems & ssGroundReactions) {
312 outstream << delimeter;
313 outstream << GroundReactions->GetGroundReactionValues(delimeter);
315 if (SubSystems & ssPropulsion && Propulsion->GetNumEngines() > 0) {
316 outstream << delimeter;
317 outstream << Propulsion->GetPropulsionValues(delimeter);
320 for (unsigned int i=0;i<OutputProperties.size();i++) {
321 outstream << delimeter << OutputProperties[i]->getDoubleValue();
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 void FGOutput::SocketOutput(void)
334 if (socket == NULL) return;
335 if (!socket->GetConnectStatus()) return;
339 socket->Append("<LABELS>");
340 socket->Append("Time");
341 socket->Append("Altitude");
342 socket->Append("Phi");
343 socket->Append("Tht");
344 socket->Append("Psi");
345 socket->Append("Rho");
346 socket->Append("Vtotal");
347 socket->Append("UBody");
348 socket->Append("VBody");
349 socket->Append("WBody");
350 socket->Append("UAero");
351 socket->Append("VAero");
352 socket->Append("WAero");
353 socket->Append("Vn");
354 socket->Append("Ve");
355 socket->Append("Vd");
356 socket->Append("Udot");
357 socket->Append("Vdot");
358 socket->Append("Wdot");
362 socket->Append("PDot");
363 socket->Append("QDot");
364 socket->Append("RDot");
365 socket->Append("Fx");
366 socket->Append("Fy");
367 socket->Append("Fz");
368 socket->Append("Latitude (Deg)");
369 socket->Append("Longitude (Deg)");
370 socket->Append("QBar");
371 socket->Append("Alpha");
375 socket->Append("Throttle Position");
376 socket->Append("Left Aileron Position");
377 socket->Append("Right Aileron Position");
378 socket->Append("Elevator Position");
379 socket->Append("Rudder Position");
385 socket->Append(State->Getsim_time());
386 socket->Append(Propagate->Geth());
387 socket->Append(Propagate->GetEuler(ePhi));
388 socket->Append(Propagate->GetEuler(eTht));
389 socket->Append(Propagate->GetEuler(ePsi));
390 socket->Append(Atmosphere->GetDensity());
391 socket->Append(Auxiliary->GetVt());
392 socket->Append(Propagate->GetUVW(eU));
393 socket->Append(Propagate->GetUVW(eV));
394 socket->Append(Propagate->GetUVW(eW));
395 socket->Append(Auxiliary->GetAeroUVW(eU));
396 socket->Append(Auxiliary->GetAeroUVW(eV));
397 socket->Append(Auxiliary->GetAeroUVW(eW));
398 socket->Append(Propagate->GetVel(eNorth));
399 socket->Append(Propagate->GetVel(eEast));
400 socket->Append(Propagate->GetVel(eDown));
401 socket->Append(Propagate->GetUVWdot(eU));
402 socket->Append(Propagate->GetUVWdot(eV));
403 socket->Append(Propagate->GetUVWdot(eW));
404 socket->Append(Propagate->GetPQR(eP));
405 socket->Append(Propagate->GetPQR(eQ));
406 socket->Append(Propagate->GetPQR(eR));
407 socket->Append(Propagate->GetPQRdot(eP));
408 socket->Append(Propagate->GetPQRdot(eQ));
409 socket->Append(Propagate->GetPQRdot(eR));
410 socket->Append(Aircraft->GetForces(eX));
411 socket->Append(Aircraft->GetForces(eY));
412 socket->Append(Aircraft->GetForces(eZ));
413 socket->Append(Propagate->GetLocation().GetLatitudeDeg());
414 socket->Append(Propagate->GetLocation().GetLongitudeDeg());
415 socket->Append(Auxiliary->Getqbar());
416 socket->Append(Auxiliary->Getalpha(inDegrees));
417 socket->Append(Aircraft->GetMoments(eL));
418 socket->Append(Aircraft->GetMoments(eM));
419 socket->Append(Aircraft->GetMoments(eN));
420 socket->Append(FCS->GetThrottlePos(0));
421 socket->Append(FCS->GetDaLPos());
422 socket->Append(FCS->GetDaRPos());
423 socket->Append(FCS->GetDePos());
424 socket->Append(FCS->GetDrPos());
428 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 void FGOutput::SocketStatusOutput(string out_str)
434 if (socket == NULL) return;
437 asciiData = string("<STATUS>") + out_str;
438 socket->Append(asciiData.c_str());
442 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 bool FGOutput::Load(FGConfigFile* AC_cfg)
446 string token="", parameter="", separator="";
447 string name="", fname="";
449 FGConfigFile* Output_cfg;
459 name = AC_cfg->GetValue("NAME");
460 fname = AC_cfg->GetValue("FILE");
461 token = AC_cfg->GetValue("TYPE");
462 port = atoi(AC_cfg->GetValue("PORT").c_str());
464 Output->SetType(token);
466 if (token == "SOCKET") {
467 socket = new FGfdmSocket(name,port);
470 if (!fname.empty()) {
471 outputInFileName = FDMExec->GetAircraftPath() + separator
472 + FDMExec->GetModelName() + separator + fname + ".xml";
473 Output_cfg = new FGConfigFile(outputInFileName);
474 if (!Output_cfg->IsOpen()) {
475 cerr << "Could not open file: " << outputInFileName << endl;
481 Output->SetFilename(name);
483 while ((token = Output_cfg->GetValue()) != string("/OUTPUT")) {
484 *Output_cfg >> parameter;
485 if (parameter == "RATE_IN_HZ") {
486 *Output_cfg >> OutRate;
488 if (parameter == "SIMULATION") {
489 *Output_cfg >> parameter;
490 if (parameter == "ON") SubSystems += ssSimulation;
492 if (parameter == "AEROSURFACES") {
493 *Output_cfg >> parameter;
494 if (parameter == "ON") SubSystems += ssAerosurfaces;
496 if (parameter == "RATES") {
497 *Output_cfg >> parameter;
498 if (parameter == "ON") SubSystems += ssRates;
500 if (parameter == "VELOCITIES") {
501 *Output_cfg >> parameter;
502 if (parameter == "ON") SubSystems += ssVelocities;
504 if (parameter == "FORCES") {
505 *Output_cfg >> parameter;
506 if (parameter == "ON") SubSystems += ssForces;
508 if (parameter == "MOMENTS") {
509 *Output_cfg >> parameter;
510 if (parameter == "ON") SubSystems += ssMoments;
512 if (parameter == "ATMOSPHERE") {
513 *Output_cfg >> parameter;
514 if (parameter == "ON") SubSystems += ssAtmosphere;
516 if (parameter == "MASSPROPS") {
517 *Output_cfg >> parameter;
518 if (parameter == "ON") SubSystems += ssMassProps;
520 if (parameter == "POSITION") {
521 *Output_cfg >> parameter;
522 if (parameter == "ON") SubSystems += ssPropagate;
524 if (parameter == "COEFFICIENTS") {
525 *Output_cfg >> parameter;
526 if (parameter == "ON") SubSystems += ssCoefficients;
528 if (parameter == "GROUND_REACTIONS") {
529 *Output_cfg >> parameter;
530 if (parameter == "ON") SubSystems += ssGroundReactions;
532 if (parameter == "FCS") {
533 *Output_cfg >> parameter;
534 if (parameter == "ON") SubSystems += ssFCS;
536 if (parameter == "PROPULSION") {
537 *Output_cfg >> parameter;
538 if (parameter == "ON") SubSystems += ssPropulsion;
540 if (parameter == "PROPERTY") {
541 *Output_cfg >> property;
542 OutputProperties.push_back(PropertyManager->GetNode(property));
545 if (parameter == "EOF") break;
548 OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
549 rate = (int)(0.5 + 1.0/(State->Getdt()*OutRate));
556 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 // The bitmasked value choices are as follows:
558 // unset: In this case (the default) JSBSim would only print
559 // out the normally expected messages, essentially echoing
560 // the config files as they are read. If the environment
561 // variable is not set, debug_lvl is set to 1 internally
562 // 0: This requests JSBSim not to output any messages
564 // 1: This value explicity requests the normal JSBSim
566 // 2: This value asks for a message to be printed out when
567 // a class is instantiated
568 // 4: When this value is set, a message is displayed when a
569 // FGModel object executes its Run() method
570 // 8: When this value is set, various runtime state variables
571 // are printed out periodically
572 // 16: When set various parameters are sanity checked and
573 // a message is printed out when they go out of bounds
575 void FGOutput::Debug(int from)
579 if (debug_lvl <= 0) return;
581 if (debug_lvl & 1) { // Standard console startup message output
582 if (from == 0) { // Constructor
586 if (outputInFileName.empty())
587 cout << " " << "Output parameters read inline" << endl;
589 cout << " Output parameters read from file: " << outputInFileName << endl;
591 if (Filename == "cout" || Filename == "COUT") {
592 scratch = " Log output goes to screen console";
593 } else if (!Filename.empty()) {
594 scratch = " Log output goes to file: " + Filename;
598 cout << scratch << " in CSV format output at rate " << 120/rate << " Hz" << endl;
601 cout << " No log output" << endl;
605 if (SubSystems & ssSimulation) cout << " Simulation parameters logged" << endl;
606 if (SubSystems & ssAerosurfaces) cout << " Aerosurface parameters logged" << endl;
607 if (SubSystems & ssRates) cout << " Rate parameters logged" << endl;
608 if (SubSystems & ssVelocities) cout << " Velocity parameters logged" << endl;
609 if (SubSystems & ssForces) cout << " Force parameters logged" << endl;
610 if (SubSystems & ssMoments) cout << " Moments parameters logged" << endl;
611 if (SubSystems & ssAtmosphere) cout << " Atmosphere parameters logged" << endl;
612 if (SubSystems & ssMassProps) cout << " Mass parameters logged" << endl;
613 if (SubSystems & ssCoefficients) cout << " Coefficient parameters logged" << endl;
614 if (SubSystems & ssPropagate) cout << " Propagate parameters logged" << endl;
615 if (SubSystems & ssGroundReactions) cout << " Ground parameters logged" << endl;
616 if (SubSystems & ssFCS) cout << " FCS parameters logged" << endl;
617 if (SubSystems & ssPropulsion) cout << " Propulsion parameters logged" << endl;
618 if (OutputProperties.size() > 0) cout << " Properties logged:" << endl;
619 for (unsigned int i=0;i<OutputProperties.size();i++) {
620 cout << " - " << OutputProperties[i]->GetName() << endl;
624 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
625 if (from == 0) cout << "Instantiated: FGOutput" << endl;
626 if (from == 1) cout << "Destroyed: FGOutput" << endl;
628 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
630 if (debug_lvl & 8 ) { // Runtime state variables
632 if (debug_lvl & 16) { // Sanity checking
634 if (debug_lvl & 64) {
635 if (from == 0) { // Constructor
636 cout << IdSrc << endl;
637 cout << IdHdr << endl;