| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Module: JSBSim.cpp |
| 4 | Author: Jon S. Berndt |
| 5 | Date started: 08/17/99 |
| 6 | Purpose: Standalone version of JSBSim. |
| 7 | Called by: The USER. |
| 8 | |
| 9 | ------------- Copyright (C) 1999 Jon S. Berndt ([email protected]) ------------- |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify it under |
| 12 | the terms of the GNU Lesser General Public License as published by the Free |
| 13 | Software Foundation; either version 2 of the License, or (at your option) any |
| 14 | later 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 Lesser General Public License for more |
| 19 | details. |
| 20 | |
| 21 | You should have received a copy of the GNU Lesser General Public License along |
| 22 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 23 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | Further information about the GNU Lesser General Public License can also be |
| 26 | found on the world wide web at http://www.gnu.org. |
| 27 | |
| 28 | FUNCTIONAL DESCRIPTION |
| 29 | -------------------------------------------------------------------------------- |
| 30 | |
| 31 | This class implements the JSBSim standalone application. It is set up for |
| 32 | compilation under gnu C++, MSVC++, or other compiler. |
| 33 | |
| 34 | HISTORY |
| 35 | -------------------------------------------------------------------------------- |
| 36 | 08/17/99 JSB Created |
| 37 | |
| 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 39 | INCLUDES |
| 40 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 41 | |
| 42 | #include "initialization/FGTrim.h" |
| 43 | #include "FGFDMExec.h" |
| 44 | #include "input_output/FGXMLFileRead.h" |
| 45 | |
| 46 | #if !defined(__GNUC__) && !defined(sgi) && !defined(_MSC_VER) |
| 47 | # include <time> |
| 48 | #else |
| 49 | # include <time.h> |
| 50 | #endif |
| 51 | |
| 52 | #if defined(_MSC_VER) |
| 53 | # include <float.h> |
| 54 | #elif defined(__GNUC__) && !defined(sgi) |
| 55 | # define __GNU_VISIBLE 1 |
| 56 | # include <fenv.h> |
| 57 | #endif |
| 58 | |
| 59 | #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__) |
| 60 | # define WIN32_LEAN_AND_MEAN |
| 61 | # include <windows.h> |
| 62 | # include <mmsystem.h> |
| 63 | # include <regstr.h> |
| 64 | # include <sys/types.h> |
| 65 | # include <sys/timeb.h> |
| 66 | #else |
| 67 | # include <sys/time.h> |
| 68 | #endif |
| 69 | |
| 70 | #include <iostream> |
| 71 | #include <cstdlib> |
| 72 | |
| 73 | using namespace std; |
| 74 | using JSBSim::FGXMLFileRead; |
| 75 | using JSBSim::Element; |
| 76 | |
| 77 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 78 | GLOBAL DATA |
| 79 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 80 | |
| 81 | SGPath RootDir; |
| 82 | SGPath ScriptName; |
| 83 | string AircraftName; |
| 84 | SGPath ResetName; |
| 85 | SGPath PlanetName; |
| 86 | vector <string> LogOutputName; |
| 87 | vector <SGPath> LogDirectiveName; |
| 88 | vector <string> CommandLineProperties; |
| 89 | vector <double> CommandLinePropertyValues; |
| 90 | JSBSim::FGFDMExec* FDMExec; |
| 91 | JSBSim::FGTrim* trimmer; |
| 92 | |
| 93 | bool realtime; |
| 94 | bool play_nice; |
| 95 | bool suspend; |
| 96 | bool catalog; |
| 97 | bool nohighlight; |
| 98 | |
| 99 | double end_time = 1e99; |
| 100 | double simulation_rate = 1./120.; |
| 101 | bool override_sim_rate = false; |
| 102 | double sleep_period=0.01; |
| 103 | |
| 104 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 105 | FORWARD DECLARATIONS |
| 106 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 107 | |
| 108 | bool options(int, char**); |
| 109 | int real_main(int argc, char* argv[]); |
| 110 | void PrintHelp(void); |
| 111 | |
| 112 | #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__) |
| 113 | double getcurrentseconds(void) |
| 114 | { |
| 115 | struct timeb tm_ptr; |
| 116 | ftime(&tm_ptr); |
| 117 | return tm_ptr.time + tm_ptr.millitm*0.001; |
| 118 | } |
| 119 | #else |
| 120 | double getcurrentseconds(void) |
| 121 | { |
| 122 | struct timeval tval; |
| 123 | struct timezone tz; |
| 124 | |
| 125 | gettimeofday(&tval, &tz); |
| 126 | return (tval.tv_sec + tval.tv_usec*1e-6); |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__) |
| 131 | void sim_nsleep(long nanosec) |
| 132 | { |
| 133 | Sleep((DWORD)(nanosec*1e-6)); // convert nanoseconds (passed in) to milliseconds for Win32. |
| 134 | } |
| 135 | #else |
| 136 | void sim_nsleep(long nanosec) |
| 137 | { |
| 138 | struct timespec ts, ts1; |
| 139 | |
| 140 | ts.tv_sec = 0; |
| 141 | ts.tv_nsec = nanosec; |
| 142 | nanosleep(&ts, &ts1); |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | /** This class is solely for the purpose of determining what type |
| 147 | of file is given on the command line */ |
| 148 | class XMLFile : public FGXMLFileRead { |
| 149 | public: |
| 150 | bool IsScriptFile(const SGPath& filename) { |
| 151 | bool result=false; |
| 152 | Element *document = LoadXMLDocument(filename, false); |
| 153 | if (document && document->GetName() == "runscript") result = true; |
| 154 | ResetParser(); |
| 155 | return result; |
| 156 | } |
| 157 | bool IsLogDirectiveFile(const SGPath& filename) { |
| 158 | bool result=false; |
| 159 | Element *document = LoadXMLDocument(filename, false); |
| 160 | if (document && document->GetName() == "output") result = true; |
| 161 | ResetParser(); |
| 162 | return result; |
| 163 | } |
| 164 | bool IsAircraftFile(const SGPath& filename) { |
| 165 | bool result=false; |
| 166 | Element* document = LoadXMLDocument(filename, false); |
| 167 | if (document && document->GetName() == "fdm_config") result = true; |
| 168 | ResetParser(); |
| 169 | return result; |
| 170 | } |
| 171 | bool IsInitFile(const SGPath& filename) { |
| 172 | bool result=false; |
| 173 | Element *document = LoadXMLDocument(filename, false); |
| 174 | if (document && document->GetName() == "initialize") result = true; |
| 175 | ResetParser(); |
| 176 | return result; |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 181 | CLASS DOCUMENTATION |
| 182 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 183 | |
| 184 | /** \mainpage JSBSim |
| 185 | * An Open Source, Object-Oriented, Cross-Platform Flight Dynamics Model in C++ |
| 186 | |
| 187 | * \section intro Introduction |
| 188 | * |
| 189 | * JSBSim is an open source, multi-platform, object-oriented flight dynamics |
| 190 | * model (FDM) framework written in the C++ programming language. It is |
| 191 | * designed to support simulation modeling of any aerospace craft without the |
| 192 | * need for specific compiled and linked program code, instead relying on a |
| 193 | * versatile and powerful specification written in an XML format. The format is |
| 194 | * formally known as JSBSim-ML (JSBSim Markup Language). |
| 195 | * |
| 196 | * JSBSim (<a href="https://www.jsbsim.org">www.jsbsim.org</a>) was created |
| 197 | * initially for the open source FlightGear flight simulator (<a |
| 198 | * href="https://www.flightgear.org">www.flightgear.org</a>). JSBSim maintains |
| 199 | * the ability to run as a standalone executable in soft real-time, or batch |
| 200 | * mode. This is useful for running tests or sets of tests automatically using |
| 201 | * the internal scripting capability. |
| 202 | * |
| 203 | * JSBSim does not model specific aircraft in program code. The aircraft itself |
| 204 | * is defined in a file written in an XML-based format where the aircraft mass |
| 205 | * and geometric properties are specified. Additional statements define such |
| 206 | * characteristics as: |
| 207 | * |
| 208 | * - Landing gear location and properties. |
| 209 | * - Pilot eyepoint |
| 210 | * - Additional point masses (passengers, cargo, etc.) |
| 211 | * - Propulsion system (engines, fuel tanks, and "thrusters") |
| 212 | * - Flight control system |
| 213 | * - Autopilot |
| 214 | * - Aerodynamic stability derivatives and coefficients |
| 215 | * |
| 216 | * The configuration file format is set up to be easily comprehensible, for |
| 217 | * instance featuring textbook-like coefficients, which enables newcomers to |
| 218 | * become immediately fluent in describing vehicles, and requiring only prior |
| 219 | * basic theoretical aero knowledge. |
| 220 | * |
| 221 | * One of the more unique features of JSBSim is its method of modeling aircraft |
| 222 | * systems such as a flight control system, autopilot, electrical, etc. These |
| 223 | * are modeled by assembling strings of components that represent filters, |
| 224 | * switches, summers, gains, sensors, and so on. |
| 225 | * |
| 226 | * Another unique feature is displayed in the use of "properties". Properties |
| 227 | * essentially expose chosen variables as nodes in a tree, in a directory-like |
| 228 | * hierarchy. This approach facilitates plugging in different FDMs (Flight |
| 229 | * Dynamics Model) into FlightGear, but it also is a fundamental tool in |
| 230 | * allowing a wide range of aircraft to be modeled, each having its own unique |
| 231 | * control system, aerosurfaces, and flight deck instrument panel. The use of |
| 232 | * properties allows all these items for a craft to be modeled and integrated |
| 233 | * without the need for specific and unique program source code. |
| 234 | * |
| 235 | * The equations of motion are modeled essentially as they are presented in |
| 236 | * aerospace textbooks for the benefit of student users, but quaternions are |
| 237 | * used to track orientation, avoiding "gimbal lock". JSBSim can model the |
| 238 | * atmospheric flight of an aircraft, or the motion of a spacecraft in orbit. |
| 239 | * Coriolis and centripetal accelerations are incorporated into the EOM. |
| 240 | * |
| 241 | * JSBSim can output (log) data in a configurable way. Sets of data that are |
| 242 | * logically related can be selected to be output at a chosen rate, and |
| 243 | * individual properties can be selected for output. The output can be streamed |
| 244 | * to the console, and/or to a file (or files), and/or can be transmitted |
| 245 | * through a socket or sockets, or any combination of the aforementioned |
| 246 | * methods. |
| 247 | * |
| 248 | * JSBSim has been used in a variety of ways: |
| 249 | * |
| 250 | * - For developing control laws for a sounding rocket |
| 251 | * - For crafting an aircraft autopilot as part of a thesis project |
| 252 | * - As a flight model for FlightGear |
| 253 | * - As an FDM that drives motion base simulators for some |
| 254 | * commercial/entertainment simulators |
| 255 | * |
| 256 | * \section platforms Supported Platforms |
| 257 | * JSBSim has been built on the following platforms: |
| 258 | * |
| 259 | * - Linux (x86) |
| 260 | * - Windows (MSVC, Cygwin, Mingwin) |
| 261 | * - Mac OS X |
| 262 | * - FreeBSD |
| 263 | * |
| 264 | * \section depends Dependencies |
| 265 | * |
| 266 | * JSBSim has no external dependencies at present. No code is autogenerated. |
| 267 | * |
| 268 | * \section license Licensing |
| 269 | * |
| 270 | * JSBSim is licensed under the terms of the Lesser GPL (LGPL) |
| 271 | * |
| 272 | * \section website Website |
| 273 | * |
| 274 | * For more information, see the JSBSim web site: <a> |
| 275 | * href="https://www.jsbsim.org">www.jsbsim.org</a>. |
| 276 | */ |
| 277 | |
| 278 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 279 | IMPLEMENTATION |
| 280 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 281 | |
| 282 | int main(int argc, char* argv[]) |
| 283 | { |
| 284 | #if defined(_MSC_VER) || defined(__MINGW32__) |
| 285 | _clearfp(); |
| 286 | _controlfp(_controlfp(0, 0) & ~(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), |
| 287 | _MCW_EM); |
| 288 | #elif defined(__GNUC__) && !defined(sgi) && !defined(__APPLE__) |
| 289 | feenableexcept(FE_DIVBYZERO | FE_INVALID); |
| 290 | #endif |
| 291 | |
| 292 | try { |
| 293 | real_main(argc, argv); |
| 294 | } catch (string& msg) { |
| 295 | std::cerr << "FATAL ERROR: JSBSim terminated with an exception." |
| 296 | << std::endl << "The message was: " << msg << std::endl; |
| 297 | return 1; |
| 298 | } catch (const char* msg) { |
| 299 | std::cerr << "FATAL ERROR: JSBSim terminated with an exception." |
| 300 | << std::endl << "The message was: " << msg << std::endl; |
| 301 | return 1; |
| 302 | } catch (const JSBSim::BaseException& e) { |
| 303 | std::cerr << "FATAL ERROR: JSBSim terminated with an exception." |
| 304 | << std::endl << "The message was: " << e.what() << std::endl; |
| 305 | return 1; |
| 306 | } catch (...) { |
| 307 | std::cerr << "FATAL ERROR: JSBSim terminated with an unknown exception." |
| 308 | << std::endl; |
| 309 | return 1; |
| 310 | } |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | int real_main(int argc, char* argv[]) |
| 315 | { |
| 316 | // *** INITIALIZATIONS *** // |
| 317 | |
| 318 | ScriptName = ""; |
| 319 | AircraftName = ""; |
| 320 | ResetName = ""; |
| 321 | PlanetName = ""; |
| 322 | LogOutputName.clear(); |
| 323 | LogDirectiveName.clear(); |
| 324 | bool result = false, success; |
| 325 | bool was_paused = false; |
| 326 | |
| 327 | double frame_duration; |
| 328 | |
| 329 | double new_five_second_value = 0.0; |
| 330 | double actual_elapsed_time = 0; |
| 331 | double initial_seconds = 0; |
| 332 | double current_seconds = 0.0; |
| 333 | double paused_seconds = 0.0; |
| 334 | double sim_lag_time = 0; |
| 335 | double cycle_duration = 0.0; |
| 336 | double override_sim_rate_value = 0.0; |
| 337 | long sleep_nseconds = 0; |
| 338 | |
| 339 | realtime = false; |
| 340 | play_nice = false; |
| 341 | suspend = false; |
| 342 | catalog = false; |
| 343 | nohighlight = false; |
| 344 | |
| 345 | // *** PARSE OPTIONS PASSED INTO THIS SPECIFIC APPLICATION: JSBSim *** // |
| 346 | success = options(argc, argv); |
| 347 | if (!success) { |
| 348 | PrintHelp(); |
| 349 | exit(-1); |
| 350 | } |
| 351 | |
| 352 | // *** SET UP JSBSIM *** // |
| 353 | FDMExec = new JSBSim::FGFDMExec(); |
| 354 | FDMExec->SetRootDir(RootDir); |
| 355 | FDMExec->SetAircraftPath(SGPath("aircraft")); |
| 356 | FDMExec->SetEnginePath(SGPath("engine")); |
| 357 | FDMExec->SetSystemsPath(SGPath("systems")); |
| 358 | FDMExec->SetOutputPath(SGPath(".")); |
| 359 | FDMExec->GetPropertyManager()->Tie("simulation/frame_start_time", &actual_elapsed_time); |
| 360 | FDMExec->GetPropertyManager()->Tie("simulation/cycle_duration", &cycle_duration); |
| 361 | |
| 362 | if (nohighlight) FDMExec->disableHighLighting(); |
| 363 | |
| 364 | if (simulation_rate < 1.0 ) |
| 365 | FDMExec->Setdt(simulation_rate); |
| 366 | else |
| 367 | FDMExec->Setdt(1.0/simulation_rate); |
| 368 | |
| 369 | if (override_sim_rate) override_sim_rate_value = FDMExec->GetDeltaT(); |
| 370 | |
| 371 | // SET PROPERTY VALUES THAT ARE GIVEN ON THE COMMAND LINE and which are for the simulation only. |
| 372 | |
| 373 | for (unsigned int i=0; i<CommandLineProperties.size(); i++) { |
| 374 | |
| 375 | if (CommandLineProperties[i].find("simulation") != std::string::npos) { |
| 376 | if (FDMExec->GetPropertyManager()->GetNode(CommandLineProperties[i])) { |
| 377 | FDMExec->SetPropertyValue(CommandLineProperties[i], CommandLinePropertyValues[i]); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (!PlanetName.isNull()) { |
| 383 | result = FDMExec->LoadPlanet(PlanetName, false); |
| 384 | |
| 385 | if (!result) { |
| 386 | cerr << "Planet file " << PlanetName << " was not successfully loaded" << endl; |
| 387 | delete FDMExec; |
| 388 | exit(-1); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // *** OPTION A: LOAD A SCRIPT, WHICH LOADS EVERYTHING ELSE *** // |
| 393 | if (!ScriptName.isNull()) { |
| 394 | |
| 395 | result = FDMExec->LoadScript(ScriptName, override_sim_rate_value, ResetName); |
| 396 | |
| 397 | if (!result) { |
| 398 | cerr << "Script file " << ScriptName << " was not successfully loaded" << endl; |
| 399 | delete FDMExec; |
| 400 | exit(-1); |
| 401 | } |
| 402 | |
| 403 | // *** OPTION B: LOAD AN AIRCRAFT AND A SET OF INITIAL CONDITIONS *** // |
| 404 | } else if (!AircraftName.empty() || !ResetName.isNull()) { |
| 405 | |
| 406 | if (catalog) FDMExec->SetDebugLevel(0); |
| 407 | |
| 408 | if ( ! FDMExec->LoadModel(SGPath("aircraft"), |
| 409 | SGPath("engine"), |
| 410 | SGPath("systems"), |
| 411 | AircraftName)) { |
| 412 | cerr << " JSBSim could not be started" << endl << endl; |
| 413 | delete FDMExec; |
| 414 | exit(-1); |
| 415 | } |
| 416 | |
| 417 | if (catalog) { |
| 418 | FDMExec->PrintPropertyCatalog(); |
| 419 | delete FDMExec; |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | JSBSim::FGInitialCondition *IC = FDMExec->GetIC(); |
| 424 | if ( ! IC->Load(ResetName)) { |
| 425 | delete FDMExec; |
| 426 | cerr << "Initialization unsuccessful" << endl; |
| 427 | exit(-1); |
| 428 | } |
| 429 | |
| 430 | } else { |
| 431 | cout << " No Aircraft, Script, or Reset information given" << endl << endl; |
| 432 | delete FDMExec; |
| 433 | exit(-1); |
| 434 | } |
| 435 | |
| 436 | // Load output directives file[s], if given |
| 437 | for (unsigned int i=0; i<LogDirectiveName.size(); i++) { |
| 438 | if (!LogDirectiveName[i].isNull()) { |
| 439 | if (!FDMExec->SetOutputDirectives(LogDirectiveName[i])) { |
| 440 | cout << "Output directives not properly set in file " << LogDirectiveName[i] << endl; |
| 441 | delete FDMExec; |
| 442 | exit(-1); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // OVERRIDE OUTPUT FILE NAME. THIS IS USEFUL FOR CASES WHERE MULTIPLE |
| 448 | // RUNS ARE BEING MADE (SUCH AS IN A MONTE CARLO STUDY) AND THE OUTPUT FILE |
| 449 | // NAME MUST BE SET EACH TIME TO AVOID THE PREVIOUS RUN DATA FROM BEING OVER- |
| 450 | // WRITTEN. |
| 451 | for (unsigned int i=0; i<LogOutputName.size(); i++) { |
| 452 | string old_filename = FDMExec->GetOutputFileName(i); |
| 453 | if (!FDMExec->SetOutputFileName(i, LogOutputName[i])) { |
| 454 | cout << "Output filename could not be set" << endl; |
| 455 | } else { |
| 456 | cout << "Output filename change from " << old_filename << " from aircraft" |
| 457 | " configuration file to " << LogOutputName[i] << " specified on" |
| 458 | " command line" << endl; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // SET PROPERTY VALUES THAT ARE GIVEN ON THE COMMAND LINE |
| 463 | |
| 464 | for (unsigned int i=0; i<CommandLineProperties.size(); i++) { |
| 465 | |
| 466 | if (!FDMExec->GetPropertyManager()->GetNode(CommandLineProperties[i])) { |
| 467 | cerr << endl << " No property by the name " << CommandLineProperties[i] << endl; |
| 468 | delete FDMExec; |
| 469 | exit(-1); |
| 470 | } else { |
| 471 | FDMExec->SetPropertyValue(CommandLineProperties[i], CommandLinePropertyValues[i]); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | FDMExec->RunIC(); |
| 476 | |
| 477 | // PRINT SIMULATION CONFIGURATION |
| 478 | FDMExec->PrintSimulationConfiguration(); |
| 479 | |
| 480 | // Dump the simulation state (position, orientation, etc.) |
| 481 | FDMExec->GetPropagate()->DumpState(); |
| 482 | |
| 483 | // Perform trim if requested via the initialization file |
| 484 | JSBSim::TrimMode icTrimRequested = (JSBSim::TrimMode)FDMExec->GetIC()->TrimRequested(); |
| 485 | if (icTrimRequested != JSBSim::TrimMode::tNone) { |
| 486 | trimmer = new JSBSim::FGTrim( FDMExec, icTrimRequested ); |
| 487 | try { |
| 488 | trimmer->DoTrim(); |
| 489 | |
| 490 | if (FDMExec->GetDebugLevel() > 0) |
| 491 | trimmer->Report(); |
| 492 | |
| 493 | delete trimmer; |
| 494 | } catch (string& msg) { |
| 495 | cerr << endl << msg << endl << endl; |
| 496 | exit(1); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | cout << endl << JSBSim::FGFDMExec::fggreen << JSBSim::FGFDMExec::highint |
| 501 | << "---- JSBSim Execution beginning ... --------------------------------------------" |
| 502 | << JSBSim::FGFDMExec::reset << endl << endl; |
| 503 | |
| 504 | result = FDMExec->Run(); // MAKE AN INITIAL RUN |
| 505 | |
| 506 | if (suspend) FDMExec->Hold(); |
| 507 | |
| 508 | // Print actual time at start |
| 509 | char s[100]; |
| 510 | time_t tod; |
| 511 | time(&tod); |
| 512 | struct tm local; |
| 513 | #if defined(_MSC_VER) || defined(__MINGW32__) |
| 514 | localtime_s(&local, &tod); |
| 515 | #else |
| 516 | localtime_r(&tod, &local); |
| 517 | #endif |
| 518 | strftime(s, 99, "%A %B %d %Y %X", &local); |
| 519 | cout << "Start: " << s << " (HH:MM:SS)" << endl; |
| 520 | |
| 521 | frame_duration = FDMExec->GetDeltaT(); |
| 522 | if (realtime) sleep_nseconds = (long)(frame_duration*1e9); |
| 523 | else sleep_nseconds = (sleep_period )*1e9; // 0.01 seconds |
| 524 | |
| 525 | tzset(); |
| 526 | current_seconds = initial_seconds = getcurrentseconds(); |
| 527 | |
| 528 | // *** CYCLIC EXECUTION LOOP, AND MESSAGE READING *** // |
| 529 | while (result && FDMExec->GetSimTime() <= end_time) { |
| 530 | // Check if increment then hold is on and take appropriate actions if it is |
| 531 | // Iterate is not supported in realtime - only in batch and playnice modes |
| 532 | FDMExec->CheckIncrementalHold(); |
| 533 | |
| 534 | // if running realtime, throttle the execution, else just run flat-out fast |
| 535 | // unless "playing nice", in which case sleep for a while (0.01 seconds) each frame. |
| 536 | // If suspended, then don't increment cumulative realtime "stopwatch". |
| 537 | |
| 538 | if ( ! FDMExec->Holding()) { |
| 539 | if ( ! realtime ) { // ------------ RUNNING IN BATCH MODE |
| 540 | |
| 541 | result = FDMExec->Run(); |
| 542 | |
| 543 | if (play_nice) sim_nsleep(sleep_nseconds); |
| 544 | |
| 545 | } else { // ------------ RUNNING IN REALTIME MODE |
| 546 | |
| 547 | // "was_paused" will be true if entering this "run" loop from a paused state. |
| 548 | if (was_paused) { |
| 549 | initial_seconds += paused_seconds; |
| 550 | was_paused = false; |
| 551 | } |
| 552 | current_seconds = getcurrentseconds(); // Seconds since 1 Jan 1970 |
| 553 | actual_elapsed_time = current_seconds - initial_seconds; // Real world elapsed seconds since start |
| 554 | sim_lag_time = actual_elapsed_time - FDMExec->GetSimTime(); // How far behind sim-time is from actual |
| 555 | // elapsed time. |
| 556 | for (int i=0; i<(int)(sim_lag_time/frame_duration); i++) { // catch up sim time to actual elapsed time. |
| 557 | result = FDMExec->Run(); |
| 558 | cycle_duration = getcurrentseconds() - current_seconds; // Calculate cycle duration |
| 559 | current_seconds = getcurrentseconds(); // Get new current_seconds |
| 560 | if (FDMExec->Holding()) break; |
| 561 | } |
| 562 | |
| 563 | if (play_nice) sim_nsleep(sleep_nseconds); |
| 564 | |
| 565 | if (FDMExec->GetSimTime() >= new_five_second_value) { // Print out elapsed time every five seconds. |
| 566 | cout << "Simulation elapsed time: " << FDMExec->GetSimTime() << endl; |
| 567 | new_five_second_value += 5.0; |
| 568 | } |
| 569 | } |
| 570 | } else { // Suspended |
| 571 | was_paused = true; |
| 572 | paused_seconds = getcurrentseconds() - current_seconds; |
| 573 | sim_nsleep(sleep_nseconds); |
| 574 | result = FDMExec->Run(); |
| 575 | } |
| 576 | |
| 577 | } |
| 578 | |
| 579 | // PRINT ENDING CLOCK TIME |
| 580 | time(&tod); |
| 581 | #if defined(_MSC_VER) || defined(__MINGW32__) |
| 582 | localtime_s(&local, &tod); |
| 583 | #else |
| 584 | localtime_r(&tod, &local); |
| 585 | #endif |
| 586 | strftime(s, 99, "%A %B %d %Y %X", &local); |
| 587 | cout << "End: " << s << " (HH:MM:SS)" << endl; |
| 588 | |
| 589 | // CLEAN UP |
| 590 | delete FDMExec; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 596 | |
| 597 | #define gripe cerr << "Option '" << keyword \ |
| 598 | << "' requires a value, as in '" \ |
| 599 | << keyword << "=something'" << endl << endl;/**/ |
| 600 | |
| 601 | bool options(int count, char **arg) |
| 602 | { |
| 603 | int i; |
| 604 | bool result = true; |
| 605 | |
| 606 | if (count == 1) { |
| 607 | PrintHelp(); |
| 608 | exit(0); |
| 609 | } |
| 610 | |
| 611 | cout.setf(ios_base::fixed); |
| 612 | |
| 613 | for (i=1; i<count; i++) { |
| 614 | string argument = string(arg[i]); |
| 615 | string keyword(argument); |
| 616 | string value(""); |
| 617 | string::size_type n=argument.find("="); |
| 618 | |
| 619 | if (n != string::npos && n > 0) { |
| 620 | keyword = argument.substr(0, n); |
| 621 | value = argument.substr(n+1); |
| 622 | } |
| 623 | |
| 624 | if (keyword == "--help") { |
| 625 | PrintHelp(); |
| 626 | exit(0); |
| 627 | } else if (keyword == "--version") { |
| 628 | cout << endl << " JSBSim Version: " << FDMExec->GetVersion() << endl << endl; |
| 629 | exit (0); |
| 630 | } else if (keyword == "--realtime") { |
| 631 | realtime = true; |
| 632 | } else if (keyword == "--nice") { |
| 633 | play_nice = true; |
| 634 | if (n != string::npos) { |
| 635 | try { |
| 636 | sleep_period = atof( value.c_str() ); |
| 637 | } catch (...) { |
| 638 | cerr << endl << " Invalid sleep period given!" << endl << endl; |
| 639 | result = false; |
| 640 | } |
| 641 | } else { |
| 642 | sleep_period = 0.01; |
| 643 | } |
| 644 | } else if (keyword == "--suspend") { |
| 645 | suspend = true; |
| 646 | } else if (keyword == "--nohighlight") { |
| 647 | nohighlight = true; |
| 648 | } else if (keyword == "--outputlogfile") { |
| 649 | if (n != string::npos) { |
| 650 | LogOutputName.push_back(value); |
| 651 | } |
| 652 | } else if (keyword == "--logdirectivefile") { |
| 653 | if (n != string::npos) { |
| 654 | LogDirectiveName.push_back(SGPath::fromLocal8Bit(value.c_str())); |
| 655 | } else { |
| 656 | gripe; |
| 657 | exit(1); |
| 658 | } |
| 659 | } else if (keyword == "--root") { |
| 660 | if (n != string::npos) { |
| 661 | RootDir = SGPath::fromLocal8Bit(value.c_str()); |
| 662 | } else { |
| 663 | gripe; |
| 664 | exit(1); |
| 665 | } |
| 666 | } else if (keyword == "--aircraft") { |
| 667 | if (n != string::npos) { |
| 668 | AircraftName = value; |
| 669 | } else { |
| 670 | gripe; |
| 671 | exit(1); |
| 672 | } |
| 673 | } else if (keyword == "--script") { |
| 674 | if (n != string::npos) { |
| 675 | ScriptName = SGPath::fromLocal8Bit(value.c_str()); |
| 676 | } else { |
| 677 | gripe; |
| 678 | exit(1); |
| 679 | } |
| 680 | } else if (keyword == "--initfile") { |
| 681 | if (n != string::npos) { |
| 682 | ResetName = SGPath::fromLocal8Bit(value.c_str()); |
| 683 | } else { |
| 684 | gripe; |
| 685 | exit(1); |
| 686 | } |
| 687 | } else if (keyword == "--planet") { |
| 688 | if (n != string::npos) { |
| 689 | PlanetName = SGPath::fromLocal8Bit(value.c_str()); |
| 690 | } else { |
| 691 | gripe; |
| 692 | exit(1); |
| 693 | } |
| 694 | } else if (keyword == "--property") { |
| 695 | if (n != string::npos) { |
| 696 | string propName = value.substr(0,value.find("=")); |
| 697 | string propValueString = value.substr(value.find("=")+1); |
| 698 | double propValue = atof(propValueString.c_str()); |
| 699 | CommandLineProperties.push_back(propName); |
| 700 | CommandLinePropertyValues.push_back(propValue); |
| 701 | } else { |
| 702 | gripe; |
| 703 | exit(1); |
| 704 | } |
| 705 | |
| 706 | } else if (keyword.substr(0,5) == "--end") { |
| 707 | if (n != string::npos) { |
| 708 | try { |
| 709 | end_time = atof( value.c_str() ); |
| 710 | } catch (...) { |
| 711 | cerr << endl << " Invalid end time given!" << endl << endl; |
| 712 | result = false; |
| 713 | } |
| 714 | } else { |
| 715 | gripe; |
| 716 | exit(1); |
| 717 | } |
| 718 | |
| 719 | } else if (keyword == "--simulation-rate") { |
| 720 | if (n != string::npos) { |
| 721 | try { |
| 722 | simulation_rate = atof( value.c_str() ); |
| 723 | override_sim_rate = true; |
| 724 | } catch (...) { |
| 725 | cerr << endl << " Invalid simulation rate given!" << endl << endl; |
| 726 | result = false; |
| 727 | } |
| 728 | } else { |
| 729 | gripe; |
| 730 | exit(1); |
| 731 | } |
| 732 | |
| 733 | } else if (keyword == "--catalog") { |
| 734 | catalog = true; |
| 735 | if (!value.empty()) AircraftName=value; |
| 736 | } else if (keyword.substr(0,2) != "--" && value.empty() ) { |
| 737 | // See what kind of files we are specifying on the command line |
| 738 | |
| 739 | XMLFile xmlFile; |
| 740 | SGPath path = SGPath::fromLocal8Bit(keyword.c_str()); |
| 741 | |
| 742 | if (xmlFile.IsScriptFile(path)) ScriptName = path; |
| 743 | else if (xmlFile.IsLogDirectiveFile(path)) LogDirectiveName.push_back(path); |
| 744 | else if (xmlFile.IsAircraftFile(SGPath("aircraft")/keyword/keyword)) AircraftName = keyword; |
| 745 | else if (xmlFile.IsInitFile(path)) ResetName = path; |
| 746 | else if (xmlFile.IsInitFile(SGPath("aircraft")/AircraftName/keyword)) ResetName = SGPath("aircraft")/AircraftName/keyword; |
| 747 | else { |
| 748 | cerr << "The argument \"" << keyword << "\" cannot be interpreted as a file name or option." << endl; |
| 749 | exit(1); |
| 750 | } |
| 751 | |
| 752 | } |
| 753 | else //Unknown keyword so print the help file, the bad keyword and abort |
| 754 | { |
| 755 | PrintHelp(); |
| 756 | cerr << "The argument \"" << keyword << "\" cannot be interpreted as a file name or option." << endl; |
| 757 | exit(1); |
| 758 | } |
| 759 | |
| 760 | } |
| 761 | |
| 762 | // Post-processing for script options. check for incompatible options. |
| 763 | |
| 764 | if (catalog && !ScriptName.isNull()) { |
| 765 | cerr << "Cannot specify catalog with script option" << endl << endl; |
| 766 | result = false; |
| 767 | } |
| 768 | if (!AircraftName.empty() && ResetName.isNull() && !catalog) { |
| 769 | cerr << "You must specify an initialization file with the aircraft name." << endl << endl; |
| 770 | result = false; |
| 771 | } |
| 772 | if (!ScriptName.isNull() && !AircraftName.empty()) { |
| 773 | cerr << "You cannot specify an aircraft file with a script." << endl; |
| 774 | result = false; |
| 775 | } |
| 776 | |
| 777 | return result; |
| 778 | |
| 779 | } |
| 780 | |
| 781 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 782 | |
| 783 | void PrintHelp(void) |
| 784 | { |
| 785 | cout << endl << " JSBSim version " << FDMExec->GetVersion() << endl << endl; |
| 786 | cout << " Usage: jsbsim [script file name] [output file names] <options>" << endl << endl; |
| 787 | cout << " options:" << endl; |
| 788 | cout << " --help returns this message" << endl; |
| 789 | cout << " --version returns the version number" << endl; |
| 790 | cout << " --outputlogfile=<filename> sets (overrides) the name of a data output file" << endl; |
| 791 | cout << " --logdirectivefile=<filename> specifies the name of a data logging directives file" << endl; |
| 792 | cout << " (can appear multiple times)" << endl; |
| 793 | cout << " --root=<path> specifies the JSBSim root directory (where aircraft/, engine/, etc. reside)" << endl; |
| 794 | cout << " --aircraft=<filename> specifies the name of the aircraft to be modeled" << endl; |
| 795 | cout << " --script=<filename> specifies a script to run" << endl; |
| 796 | cout << " --realtime specifies to run in actual real world time" << endl; |
| 797 | cout << " --nice specifies to run at lower CPU usage" << endl; |
| 798 | cout << " --nohighlight specifies that console output should be pure text only (no color)" << endl; |
| 799 | cout << " --suspend specifies to suspend the simulation after initialization" << endl; |
| 800 | cout << " --initfile=<filename> specifies an initialization file" << endl; |
| 801 | cout << " --planet=<filename> specifies a planet definition file" << endl; |
| 802 | cout << " --catalog specifies that all properties for this aircraft model should be printed" << endl; |
| 803 | cout << " (catalog=aircraftname is an optional format)" << endl; |
| 804 | cout << " --property=<name=value> e.g. --property=simulation/integrator/rate/rotational=1" << endl; |
| 805 | cout << " --simulation-rate=<rate (double)> specifies the sim dT time or frequency" << endl; |
| 806 | cout << " If rate specified is less than 1, it is interpreted as" << endl; |
| 807 | cout << " a time step size, otherwise it is assumed to be a rate in Hertz." << endl; |
| 808 | cout << " --end=<time (double)> specifies the sim end time" << endl << endl; |
| 809 | |
| 810 | cout << " NOTE: There can be no spaces around the = sign when" << endl; |
| 811 | cout << " an option is followed by a filename" << endl << endl; |
| 812 | } |