1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Schedules and runs the model routines.
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 --------------------------------------------------------------------------------
31 This class wraps up the simulation scheduling routines.
34 --------------------------------------------------------------------------------
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 COMMENTS, REFERENCES, and NOTES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
46 # include <simgear/compiler.h>
47 # include STL_IOSTREAM
48 # include STL_ITERATOR
50 # if defined(sgi) && !defined(__GNUC__)
51 # include <iostream.h>
58 #include "FGFDMExec.h"
60 #include "FGAtmosphere.h"
62 #include "FGPropulsion.h"
63 #include "FGMassBalance.h"
64 #include "FGGroundReactions.h"
65 #include "FGAerodynamics.h"
66 #include "FGInertial.h"
67 #include "FGAircraft.h"
68 #include "FGTranslation.h"
69 #include "FGRotation.h"
70 #include "FGPosition.h"
71 #include "FGAuxiliary.h"
73 #include "FGConfigFile.h"
75 static const char *IdSrc = "$Id$";
76 static const char *IdHdr = ID_FDMEXEC;
78 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
82 short debug_lvl; // This describes to any interested entity the debug level
83 // requested by setting the JSBSIM_DEBUG environment variable.
84 // The bitmasked value choices are as follows:
85 // a) unset: In this case (the default) JSBSim would only print
86 // out the normally expected messages, essentially echoing
87 // the config files as they are read. If the environment
88 // variable is not set, debug_lvl is set to 1 internally
89 // b) 0: This requests JSBSim not to output any messages
91 // c) 1: This value explicity requests the normal JSBSim
93 // d) 2: This value asks for a message to be printed out when
94 // a class is instantiated
95 // e) 4: When this value is set, a message is displayed when a
96 // FGModel object executes its Run() method
97 // f) 8: When this value is set, various runtime state variables
98 // are printed out periodically
99 // g) 16: When set various parameters are sanity checked and
100 // a message is printed out when they go out of bounds.
102 unsigned int FGFDMExec::FDMctr = 0;
104 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
110 FGFDMExec::FGFDMExec(void)
139 char* num = getenv("JSBSIM_DEBUG");
140 if (!num) debug_lvl = 1;
141 else debug_lvl = atoi(num); // set debug level
142 } catch (...) { // if error set to 1
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 FGFDMExec::~FGFDMExec()
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 bool FGFDMExec::Allocate(void)
166 Atmosphere = new FGAtmosphere(this);
167 FCS = new FGFCS(this);
168 Propulsion = new FGPropulsion(this);
169 MassBalance = new FGMassBalance(this);
170 Aerodynamics = new FGAerodynamics (this);
171 Inertial = new FGInertial(this);
172 GroundReactions = new FGGroundReactions(this);
173 Aircraft = new FGAircraft(this);
174 Translation = new FGTranslation(this);
175 Rotation = new FGRotation(this);
176 Position = new FGPosition(this);
177 Auxiliary = new FGAuxiliary(this);
178 Output = new FGOutput(this);
180 State = new FGState(this); // This must be done here, as the FGState
181 // class needs valid pointers to the above
184 // Initialize models so they can communicate with each other
186 if (!Atmosphere->InitModel()) {
187 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
189 if (!FCS->InitModel()) {
190 cerr << fgred << "FCS model init failed" << fgdef << endl;
192 if (!Propulsion->InitModel()) {
193 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
195 if (!MassBalance->InitModel()) {
196 cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
198 if (!Aerodynamics->InitModel()) {
199 cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
201 if (!Inertial->InitModel()) {
202 cerr << fgred << "FGInertial model init failed" << fgdef << endl;
204 if (!GroundReactions->InitModel()) {
205 cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
207 if (!Aircraft->InitModel()) {
208 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
210 if (!Translation->InitModel()){
211 cerr << fgred << "Translation model init failed" << fgdef << endl;
213 if (!Rotation->InitModel()) {
214 cerr << fgred << "Rotation model init failed" << fgdef << endl;
216 if (!Position->InitModel()) {
217 cerr << fgred << "Position model init failed" << fgdef << endl;
219 if (!Auxiliary->InitModel()) {
220 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
222 if (!Output->InitModel()) {
223 cerr << fgred << "Output model init failed" << fgdef << endl;
226 if (Error > 0) result = false;
228 // Schedule a model. The second arg (the integer) is the pass number. For
229 // instance, the atmosphere model gets executed every fifth pass it is called
230 // by the executive. Everything else here gets executed each pass.
232 Schedule(Atmosphere, 1);
234 Schedule(Propulsion, 1);
235 Schedule(MassBalance, 1);
236 Schedule(Aerodynamics, 1);
237 Schedule(Inertial, 1);
238 Schedule(GroundReactions, 1);
239 Schedule(Aircraft, 1);
240 Schedule(Rotation, 1);
241 Schedule(Translation, 1);
242 Schedule(Position, 1);
243 Schedule(Auxiliary, 1);
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 bool FGFDMExec::DeAllocate(void) {
255 if ( Atmosphere != 0 ) delete Atmosphere;
256 if ( FCS != 0 ) delete FCS;
257 if ( Propulsion != 0) delete Propulsion;
258 if ( MassBalance != 0) delete MassBalance;
259 if ( Aerodynamics != 0) delete Aerodynamics;
260 if ( Inertial != 0) delete Inertial;
261 if ( GroundReactions != 0) delete GroundReactions;
262 if ( Aircraft != 0 ) delete Aircraft;
263 if ( Translation != 0 ) delete Translation;
264 if ( Rotation != 0 ) delete Rotation;
265 if ( Position != 0 ) delete Position;
266 if ( Auxiliary != 0 ) delete Auxiliary;
267 if ( Output != 0 ) delete Output;
268 if ( State != 0 ) delete State;
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 int FGFDMExec::Schedule(FGModel* model, int rate)
296 FGModel* model_iterator;
298 model_iterator = FirstModel;
300 if (model_iterator == 0L) { // this is the first model
303 FirstModel->NextModel = 0L;
304 FirstModel->SetRate(rate);
306 } else { // subsequent model
308 while (model_iterator->NextModel != 0L) {
309 model_iterator = model_iterator->NextModel;
311 model_iterator->NextModel = model;
312 model_iterator->NextModel->SetRate(rate);
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 bool FGFDMExec::Run(void)
323 FGModel* model_iterator;
325 if (frozen) return true;
327 model_iterator = FirstModel;
328 if (model_iterator == 0L) return false;
332 if (State->Getsim_time() >= EndTime) return false;
337 while (!model_iterator->Run()) {
338 model_iterator = model_iterator->NextModel;
339 if (model_iterator == 0L) break;
348 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
353 State->Initialize(fgic);
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
365 string aircraftCfgFileName;
367 AircraftPath = APath;
371 aircraftCfgFileName = AircraftPath + "/" + model + "/" + model + ".xml";
373 aircraftCfgFileName = AircraftPath + ";" + model + ";" + model + ".xml";
376 FGConfigFile AC_cfg(aircraftCfgFileName);
377 if (!AC_cfg.IsOpen()) return false;
384 result = Aircraft->Load(&AC_cfg);
391 << " FGFDMExec: Failed to load aircraft and/or engine model"
398 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 bool FGFDMExec::LoadScript(string script)
402 FGConfigFile Script(script);
405 string initialize="";
409 struct condition *newCondition;
411 if (!Script.IsOpen()) return false;
413 Script.GetNextConfigLine();
414 ScriptName = Script.GetValue("name");
416 if (debug_lvl > 0) cout << "Reading Script File " << ScriptName << endl;
418 while (Script.GetNextConfigLine() != string("EOF") && Script.GetValue() != string("/runscript")) {
419 token = Script.GetValue();
420 if (token == "use") {
421 if ((token = Script.GetValue("aircraft")) != string("")) {
423 if (debug_lvl > 0) cout << " Use aircraft: " << token << endl;
424 } else if ((token = Script.GetValue("initialize")) != string("")) {
426 if (debug_lvl > 0) cout << " Use reset file: " << token << endl;
428 cerr << "Unknown 'use' keyword: \"" << token << "\"" << endl;
430 } else if (token == "run") {
431 StartTime = strtod(Script.GetValue("start").c_str(), NULL);
432 State->Setsim_time(StartTime);
433 EndTime = strtod(Script.GetValue("end").c_str(), NULL);
434 dt = strtod(Script.GetValue("dt").c_str(), NULL);
436 Script.GetNextConfigLine();
437 token = Script.GetValue();
438 while (token != string("/run")) {
440 if (token == "when") {
441 Script.GetNextConfigLine();
442 token = Script.GetValue();
443 newCondition = new struct condition();
444 while (token != string("/when")) {
445 if (token == "parameter") {
446 newCondition->TestParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
447 newCondition->TestValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
448 newCondition->Comparison.push_back(Script.GetValue("comparison"));
449 } else if (token == "set") {
450 newCondition->SetParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
451 newCondition->SetValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
452 newCondition->Triggered.push_back(false);
453 newCondition->OriginalValue.push_back(0.0);
454 newCondition->newValue.push_back(0.0);
455 newCondition->StartTime.push_back(0.0);
456 newCondition->EndTime.push_back(0.0);
457 string tempCompare = Script.GetValue("type");
458 if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
459 else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
460 else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
461 else newCondition->Type.push_back((eType)0);
462 tempCompare = Script.GetValue("action");
463 if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
464 else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
465 else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
466 else newCondition->Action.push_back((eAction)0);
468 if (Script.GetValue("persistent") == "true")
469 newCondition->Persistent.push_back(true);
471 newCondition->Persistent.push_back(false);
473 newCondition->TC.push_back(strtod(Script.GetValue("tc").c_str(), NULL));
476 cerr << "Unrecognized keyword in script file: \" [when] " << token << "\"" << endl;
478 Script.GetNextConfigLine();
479 token = Script.GetValue();
481 Conditions.push_back(*newCondition);
482 Script.GetNextConfigLine();
483 token = Script.GetValue();
486 cerr << "Error reading script file: expected \"when\", got \"" << token << "\"" << endl;
490 } else if (token.empty()) {
493 cerr << "Unrecognized keyword in script file: \"" << token << "\" [runscript] " << endl;
497 if (aircraft == "") {
498 cerr << "Aircraft file not loaded in script" << endl;
504 result = LoadModel("aircraft", "engine", aircraft);
506 cerr << "Aircraft file " << aircraft << " was not found" << endl;
513 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515 void FGFDMExec::RunScript(void)
517 vector <struct condition>::iterator iC = Conditions.begin();
519 bool WholeTruth = false;
522 double currentTime = State->Getsim_time();
523 double newSetValue = 0;
525 while (iC < Conditions.end()) {
526 // determine whether the set of conditional tests for this condition equate
528 for (i=0; i<iC->TestValue.size(); i++) {
529 if (iC->Comparison[i] == "lt")
530 truth = State->GetParameter(iC->TestParam[i]) < iC->TestValue[i];
531 else if (iC->Comparison[i] == "le")
532 truth = State->GetParameter(iC->TestParam[i]) <= iC->TestValue[i];
533 else if (iC->Comparison[i] == "eq")
534 truth = State->GetParameter(iC->TestParam[i]) == iC->TestValue[i];
535 else if (iC->Comparison[i] == "ge")
536 truth = State->GetParameter(iC->TestParam[i]) >= iC->TestValue[i];
537 else if (iC->Comparison[i] == "gt")
538 truth = State->GetParameter(iC->TestParam[i]) > iC->TestValue[i];
539 else if (iC->Comparison[i] == "ne")
540 truth = State->GetParameter(iC->TestParam[i]) != iC->TestValue[i];
542 cerr << "Bad comparison" << endl;
544 if (i == 0) WholeTruth = truth;
545 else WholeTruth = WholeTruth && truth;
547 if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
550 // if the conditions are true, do the setting of the desired parameters
553 for (i=0; i<iC->SetValue.size(); i++) {
554 if ( ! iC->Triggered[i]) {
555 iC->OriginalValue[i] = State->GetParameter(iC->SetParam[i]);
556 switch (iC->Type[i]) {
558 iC->newValue[i] = iC->SetValue[i];
561 iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
564 iC->newValue[i] = iC->SetValue[i];
567 cerr << "Invalid Type specified" << endl;
570 iC->Triggered[i] = true;
571 iC->StartTime[i] = currentTime;
574 switch (iC->Action[i]) {
576 newSetValue = (currentTime - iC->StartTime[i])/(iC->TC[i])
577 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
578 if (newSetValue > iC->newValue[i]) newSetValue = iC->newValue[i];
581 newSetValue = iC->newValue[i];
584 newSetValue = (1 - exp(-(currentTime - iC->StartTime[i])/(iC->TC[i])))
585 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
588 cerr << "Invalid Action specified" << endl;
591 State->SetParameter(iC->SetParam[i], newSetValue);
598 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599 // The bitmasked value choices are as follows:
600 // unset: In this case (the default) JSBSim would only print
601 // out the normally expected messages, essentially echoing
602 // the config files as they are read. If the environment
603 // variable is not set, debug_lvl is set to 1 internally
604 // 0: This requests JSBSim not to output any messages
606 // 1: This value explicity requests the normal JSBSim
608 // 2: This value asks for a message to be printed out when
609 // a class is instantiated
610 // 4: When this value is set, a message is displayed when a
611 // FGModel object executes its Run() method
612 // 8: When this value is set, various runtime state variables
613 // are printed out periodically
614 // 16: When set various parameters are sanity checked and
615 // a message is printed out when they go out of bounds
617 void FGFDMExec::Debug(int from)
621 if (debug_lvl <= 0) return;
623 if (debug_lvl & 1) { // Standard console startup message output
624 if (from == 0) { // Constructor
625 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
626 << JSBSim_version << underoff << normint << endl;
627 cout << halfint << " [cfg file spec v" << needed_cfg_version << "]\n\n";
628 cout << normint << "JSBSim startup beginning ...\n\n";
629 } else if (from == 3) {
630 cout << "\n\nJSBSim startup complete\n\n";
631 } else if (from == 4) { // print out script data
632 vector <struct condition>::iterator iterConditions = Conditions.begin();
635 cout << "\n Script goes from " << StartTime << " to " << EndTime
636 << " with dt = " << State->Getdt() << endl << endl;
638 while (iterConditions < Conditions.end()) {
639 cout << " Condition: " << count++ << endl;
642 for (i=0; i<iterConditions->TestValue.size(); i++) {
643 if (i>0) cout << " and" << endl << " ";
644 cout << "(" << State->paramdef[iterConditions->TestParam[i]]
645 << iterConditions->Comparison[i] << " "
646 << iterConditions->TestValue[i] << ")";
650 for (i=0; i<iterConditions->SetValue.size(); i++) {
651 cout << endl << " set" << State->paramdef[iterConditions->SetParam[i]]
652 << "to " << iterConditions->SetValue[i];
654 switch (iterConditions->Type[i]) {
656 cout << " (constant";
665 cout << " (unspecified type";
668 switch (iterConditions->Action[i]) {
676 cout << " via exponential approach";
679 cout << " via unspecified action";
682 if (!iterConditions->Persistent[i]) cout << endl
687 if (iterConditions->Action[i] == FG_RAMP ||
688 iterConditions->Action[i] == FG_EXP) cout << endl
689 << " with time constant "
690 << iterConditions->TC[i];
692 cout << ")" << endl << " }" << endl << endl;
700 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
701 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
702 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
704 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
706 cout << "================== Frame: " << Frame << " Time: "
707 << State->Getsim_time() << endl;
710 if (debug_lvl & 8 ) { // Runtime state variables
712 if (debug_lvl & 16) { // Sanity checking