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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47 # include <simgear/compiler.h>
48 # include STL_IOSTREAM
49 # include STL_ITERATOR
56 #include "FGFDMExec.h"
58 #include "FGAtmosphere.h"
60 #include "FGPropulsion.h"
61 #include "FGMassBalance.h"
62 #include "FGAerodynamics.h"
63 #include "FGInertial.h"
64 #include "FGAircraft.h"
65 #include "FGTranslation.h"
66 #include "FGRotation.h"
67 #include "FGPosition.h"
68 #include "FGAuxiliary.h"
70 #include "FGConfigFile.h"
72 static const char *IdSrc = "$Id$";
73 static const char *IdHdr = ID_FDMEXEC;
75 char highint[5] = {27, '[', '1', 'm', '\0' };
76 char halfint[5] = {27, '[', '2', 'm', '\0' };
77 char normint[6] = {27, '[', '2', '2', 'm', '\0' };
78 char reset[5] = {27, '[', '0', 'm', '\0' };
79 char underon[5] = {27, '[', '4', 'm', '\0' };
80 char underoff[6] = {27, '[', '2', '4', 'm', '\0' };
81 char fgblue[6] = {27, '[', '3', '4', 'm', '\0' };
82 char fgcyan[6] = {27, '[', '3', '6', 'm', '\0' };
83 char fgred[6] = {27, '[', '3', '1', 'm', '\0' };
84 char fggreen[6] = {27, '[', '3', '2', 'm', '\0' };
85 char fgdef[6] = {27, '[', '3', '9', 'm', '\0' };
87 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
91 short debug_lvl; // This describes to any interested entity the debug level
92 // requested by setting the JSBSIM_DEBUG environment variable.
93 // The bitmasked value choices are as follows:
94 // a) unset: In this case (the default) JSBSim would only print
95 // out the normally expected messages, essentially echoing
96 // the config files as they are read. If the environment
97 // variable is not set, debug_lvl is set to 1 internally
98 // b) 0: This requests JSBSim not to output any messages
100 // c) 1: This value explicity requests the normal JSBSim
102 // d) 2: This value asks for a message to be printed out when
103 // a class is instantiated
104 // e) 4: When this value is set, a message is displayed when a
105 // FGModel object executes its Run() method
106 // f) 8: When this value is set, various runtime state variables
107 // are printed out periodically
108 // g) 16: When set various parameters are sanity checked and
109 // a message is printed out when they go out of bounds.
111 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
117 FGFDMExec::FGFDMExec(void)
142 char* num = getenv("JSBSIM_DEBUG");
143 if (!num) debug_lvl = 1;
144 else debug_lvl = atoi(num); // set debug level
145 } catch (...) { // if error set to 1
150 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
151 << JSBSIM_VERSION << underoff << normint << endl;
152 cout << halfint << " [cfg file spec v" << NEEDED_CFG_VERSION << "]\n\n";
153 cout << normint << "JSBSim startup beginning ...\n\n";
156 if (debug_lvl & 2) cout << "Instantiated: FGFDMExec" << endl;
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 FGFDMExec::~FGFDMExec() {
165 if (debug_lvl & 2) cout << "Destroyed: FGFDMExec" << endl;
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 bool FGFDMExec::Allocate(void) {
174 Atmosphere = new FGAtmosphere(this);
175 FCS = new FGFCS(this);
176 Propulsion = new FGPropulsion(this);
177 MassBalance = new FGMassBalance(this);
178 Aerodynamics = new FGAerodynamics (this);
179 Inertial = new FGInertial(this);
180 Aircraft = new FGAircraft(this);
181 Translation = new FGTranslation(this);
182 Rotation = new FGRotation(this);
183 Position = new FGPosition(this);
184 Auxiliary = new FGAuxiliary(this);
185 Output = new FGOutput(this);
187 State = new FGState(this); // This must be done here, as the FGState
188 // class needs valid pointers to the above
191 // Initialize models so they can communicate with each other
193 if (!Atmosphere->InitModel()) {
194 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
196 if (!FCS->InitModel()) {
197 cerr << fgred << "FCS model init failed" << fgdef << endl;
199 if (!Propulsion->InitModel()) {
200 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
202 if (!MassBalance->InitModel()) {
203 cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
205 if (!Aerodynamics->InitModel()) {
206 cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
208 if (!Inertial->InitModel()) {
209 cerr << fgred << "FGInertial model init failed" << fgdef << endl;
211 if (!Aircraft->InitModel()) {
212 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
214 if (!Translation->InitModel()){
215 cerr << fgred << "Translation model init failed" << fgdef << endl;
217 if (!Rotation->InitModel()) {
218 cerr << fgred << "Rotation model init failed" << fgdef << endl;
220 if (!Position->InitModel()) {
221 cerr << fgred << "Position model init failed" << fgdef << endl;
223 if (!Auxiliary->InitModel()) {
224 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
226 if (!Output->InitModel()) {
227 cerr << fgred << "Output model init failed" << fgdef << endl;
230 if (Error > 0) result = false;
232 // Schedule a model. The second arg (the integer) is the pass number. For
233 // instance, the atmosphere model gets executed every fifth pass it is called
234 // by the executive. Everything else here gets executed each pass.
236 Schedule(Atmosphere, 1);
238 Schedule(Propulsion, 1);
239 Schedule(MassBalance, 1);
240 Schedule(Aerodynamics, 1);
241 Schedule(Inertial, 1);
242 Schedule(Aircraft, 1);
243 Schedule(Rotation, 1);
244 Schedule(Translation, 1);
245 Schedule(Position, 1);
246 Schedule(Auxiliary, 1);
254 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 bool FGFDMExec::DeAllocate(void) {
258 if ( Atmosphere != 0 ) delete Atmosphere;
259 if ( FCS != 0 ) delete FCS;
260 if ( Propulsion != 0) delete Propulsion;
261 if ( MassBalance != 0) delete MassBalance;
262 if ( Aerodynamics != 0) delete Aerodynamics;
263 if ( Inertial != 0) delete Inertial;
264 if ( Aircraft != 0 ) delete Aircraft;
265 if ( Translation != 0 ) delete Translation;
266 if ( Rotation != 0 ) delete Rotation;
267 if ( Position != 0 ) delete Position;
268 if ( Auxiliary != 0 ) delete Auxiliary;
269 if ( Output != 0 ) delete Output;
270 if ( State != 0 ) delete State;
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 int FGFDMExec::Schedule(FGModel* model, int rate)
297 FGModel* model_iterator;
299 model_iterator = FirstModel;
301 if (model_iterator == 0L) { // this is the first model
304 FirstModel->NextModel = 0L;
305 FirstModel->SetRate(rate);
307 } else { // subsequent model
309 while (model_iterator->NextModel != 0L) {
310 model_iterator = model_iterator->NextModel;
312 model_iterator->NextModel = model;
313 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;
336 cout << "================== Frame: " << Frame << " Time: "
337 << State->Getsim_time() << endl;
339 while (!model_iterator->Run()) {
340 model_iterator = model_iterator->NextModel;
341 if (model_iterator == 0L) break;
350 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
355 State->Initialize(fgic);
361 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
372 AircraftPath = APath;
374 result = Aircraft->LoadAircraft(AircraftPath, EnginePath, model);
380 << "FGFDMExec: Failed to load aircraft and/or engine model"
384 if (debug_lvl > 0) cout << "\n\nJSBSim startup complete\n\n";
388 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 bool FGFDMExec::LoadScript(string script)
392 FGConfigFile Script(script);
395 string initialize="";
399 struct condition *newCondition;
401 if (!Script.IsOpen()) return false;
403 Script.GetNextConfigLine();
404 ScriptName = Script.GetValue("name");
406 if (debug_lvl > 0) cout << "Reading Script File " << ScriptName << endl;
408 while (Script.GetNextConfigLine() != "EOF" && Script.GetValue() != "/runscript") {
409 token = Script.GetValue();
410 if (token == "use") {
411 if ((token = Script.GetValue("aircraft")) != "") {
413 if (debug_lvl > 0) cout << " Use aircraft: " << token << endl;
414 } else if ((token = Script.GetValue("initialize")) != "") {
416 if (debug_lvl > 0) cout << " Use reset file: " << token << endl;
418 cerr << "Unknown 'use' keyword: \"" << token << "\"" << endl;
420 } else if (token == "run") {
421 StartTime = strtod(Script.GetValue("start").c_str(), NULL);
422 State->Setsim_time(StartTime);
423 EndTime = strtod(Script.GetValue("end").c_str(), NULL);
424 dt = strtod(Script.GetValue("dt").c_str(), NULL);
426 Script.GetNextConfigLine();
427 token = Script.GetValue();
428 while (token != "/run") {
430 if (token == "when") {
431 Script.GetNextConfigLine();
432 token = Script.GetValue();
433 newCondition = new struct condition();
434 while (token != "/when") {
435 if (token == "parameter") {
436 newCondition->TestParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
437 newCondition->TestValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
438 newCondition->Comparison.push_back(Script.GetValue("comparison"));
439 } else if (token == "set") {
440 newCondition->SetParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
441 newCondition->SetValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
442 newCondition->Triggered.push_back(false);
443 newCondition->OriginalValue.push_back(0.0);
444 newCondition->newValue.push_back(0.0);
445 newCondition->StartTime.push_back(0.0);
446 newCondition->EndTime.push_back(0.0);
447 string tempCompare = Script.GetValue("type");
448 if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
449 else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
450 else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
451 else newCondition->Type.push_back((eType)0);
452 tempCompare = Script.GetValue("action");
453 if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
454 else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
455 else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
456 else newCondition->Action.push_back((eAction)0);
458 if (Script.GetValue("persistent") == "true")
459 newCondition->Persistent.push_back(true);
461 newCondition->Persistent.push_back(false);
463 newCondition->TC.push_back(strtod(Script.GetValue("tc").c_str(), NULL));
466 cerr << "Unrecognized keyword in script file: \" [when] " << token << "\"" << endl;
468 Script.GetNextConfigLine();
469 token = Script.GetValue();
471 Conditions.push_back(*newCondition);
472 Script.GetNextConfigLine();
473 token = Script.GetValue();
476 cerr << "Error reading script file: expected \"when\", got \"" << token << "\"" << endl;
481 cerr << "Unrecognized keyword in script file: \"" << token << "\" [runscript] " << endl;
485 if (aircraft == "") {
486 cerr << "Aircraft file not loaded in script" << endl;
490 // print out conditions for double-checking if requested
493 vector <struct condition>::iterator iterConditions = Conditions.begin();
496 cout << "\n Script goes from " << StartTime << " to " << EndTime
497 << " with dt = " << dt << endl << endl;
499 while (iterConditions < Conditions.end()) {
500 cout << " Condition: " << count++ << endl;
503 for (i=0; i<iterConditions->TestValue.size(); i++) {
504 if (i>0) cout << " and" << endl << " ";
505 cout << "(" << State->paramdef[iterConditions->TestParam[i]]
506 << iterConditions->Comparison[i] << " "
507 << iterConditions->TestValue[i] << ")";
509 cout << ") then {" << endl;
511 for (i=0; i<iterConditions->SetValue.size(); i++) {
512 cout << " set" << State->paramdef[iterConditions->SetParam[i]]
513 << "to " << iterConditions->SetValue[i];
515 switch (iterConditions->Type[i]) {
517 cout << " (constant";
526 cout << " (unspecified type";
529 switch (iterConditions->Action[i]) {
537 cout << " via exponential approach";
540 cout << " via unspecified action";
543 if (!iterConditions->Persistent[i]) cout << endl
548 if (iterConditions->Action[i] == FG_RAMP ||
549 iterConditions->Action[i] == FG_EXP) cout << endl
550 << " with time constant "
551 << iterConditions->TC[i];
553 cout << ")" << endl << " }" << endl << endl;
561 result = LoadModel("aircraft", "engine", aircraft);
563 cerr << "Aircraft file " << aircraft << " was not found" << endl;
566 if ( ! State->Reset("aircraft", aircraft, initialize))
567 State->Initialize(2000,0,0,0,0,0,0.5,0.5,40000);
572 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 void FGFDMExec::RunScript(void)
576 vector <struct condition>::iterator iC = Conditions.begin();
583 float currentTime = State->Getsim_time();
586 while (iC < Conditions.end()) {
587 // determine whether the set of conditional tests for this condition equate
589 for (i=0; i<iC->TestValue.size(); i++) {
590 if (iC->Comparison[i] == "lt")
591 truth = State->GetParameter(iC->TestParam[i]) < iC->TestValue[i];
592 else if (iC->Comparison[i] == "le")
593 truth = State->GetParameter(iC->TestParam[i]) <= iC->TestValue[i];
594 else if (iC->Comparison[i] == "eq")
595 truth = State->GetParameter(iC->TestParam[i]) == iC->TestValue[i];
596 else if (iC->Comparison[i] == "ge")
597 truth = State->GetParameter(iC->TestParam[i]) >= iC->TestValue[i];
598 else if (iC->Comparison[i] == "gt")
599 truth = State->GetParameter(iC->TestParam[i]) > iC->TestValue[i];
600 else if (iC->Comparison[i] == "ne")
601 truth = State->GetParameter(iC->TestParam[i]) != iC->TestValue[i];
603 cerr << "Bad comparison" << endl;
605 if (i == 0) WholeTruth = truth;
606 else WholeTruth = WholeTruth && truth;
608 if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
611 // if the conditions are true, do the setting of the desired parameters
614 for (i=0; i<iC->SetValue.size(); i++) {
615 if ( ! iC->Triggered[i]) {
616 iC->OriginalValue[i] = State->GetParameter(iC->SetParam[i]);
617 switch (iC->Type[i]) {
619 iC->newValue[i] = iC->SetValue[i];
622 iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
627 cerr << "Invalid Type specified" << endl;
630 iC->Triggered[i] = true;
631 iC->StartTime[i] = currentTime;
634 switch (iC->Action[i]) {
636 newSetValue = (currentTime - iC->StartTime[i])/(iC->TC[i])
637 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
638 if (newSetValue > iC->newValue[i]) newSetValue = iC->newValue[i];
641 newSetValue = iC->newValue[i];
644 newSetValue = (1 - exp(-(currentTime - iC->StartTime[i])/(iC->TC[i])))
645 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
648 cerr << "Invalid Action specified" << endl;
651 State->SetParameter(iC->SetParam[i], newSetValue);
658 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
660 void FGFDMExec::Debug(void)
662 //TODO: Add your source code here