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 # ifdef FG_HAVE_STD_INCLUDES
52 # include <iostream.h>
54 # include <iterator.h>
62 #include "FGFDMExec.h"
64 #include "FGAtmosphere.h"
66 #include "FGPropulsion.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 char highint[5] = {27, '[', '1', 'm', '\0' };
79 char halfint[5] = {27, '[', '2', 'm', '\0' };
80 char normint[6] = {27, '[', '2', '2', 'm', '\0' };
81 char reset[5] = {27, '[', '0', 'm', '\0' };
82 char underon[5] = {27, '[', '4', 'm', '\0' };
83 char underoff[6] = {27, '[', '2', '4', 'm', '\0' };
84 char fgblue[6] = {27, '[', '3', '4', 'm', '\0' };
85 char fgcyan[6] = {27, '[', '3', '6', 'm', '\0' };
86 char fgred[6] = {27, '[', '3', '1', 'm', '\0' };
87 char fggreen[6] = {27, '[', '3', '2', 'm', '\0' };
88 char fgdef[6] = {27, '[', '3', '9', 'm', '\0' };
90 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
94 short debug_lvl; // This describes to any interested entity the debug level
95 // requested by setting the JSBSIM_DEBUG environment variable.
96 // The bitmasked value choices are as follows:
97 // a) unset: In this case (the default) JSBSim would only print
98 // out the normally expected messages, essentially echoing
99 // the config files as they are read. If the environment
100 // variable is not set, debug_lvl is set to 1 internally
101 // b) 0: This requests JSBSim not to output any messages
103 // c) 1: This value explicity requests the normal JSBSim
105 // d) 2: This value asks for a message to be printed out when
106 // a class is instantiated
107 // e) 4: When this value is set, a message is displayed when a
108 // FGModel object executes its Run() method
109 // f) 8: When this value is set, various runtime state variables
110 // are printed out periodically
112 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
118 FGFDMExec::FGFDMExec(void)
138 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
139 << JSBSIM_VERSION << underoff << normint << endl;
140 cout << halfint << " [cfg file spec v" << NEEDED_CFG_VERSION << "]\n\n";
141 cout << normint << "JSBSim startup beginning ...\n\n";
144 char* num = getenv("JSBSIM_DEBUG");
145 if (!num) debug_lvl = 1;
146 else debug_lvl = atoi(num); // set debug level
147 } catch (...) { // if error set to 1
151 if (debug_lvl & 2) cout << "Instantiated: FGFDMExec" << endl;
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 FGFDMExec::~FGFDMExec() {
160 if (debug_lvl & 2) cout << "Destroyed: FGFDMExec" << endl;
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 bool FGFDMExec::Allocate(void) {
169 Atmosphere = new FGAtmosphere(this);
170 FCS = new FGFCS(this);
171 Propulsion = new FGPropulsion(this);
172 Aircraft = new FGAircraft(this);
173 Translation = new FGTranslation(this);
174 Rotation = new FGRotation(this);
175 Position = new FGPosition(this);
176 Auxiliary = new FGAuxiliary(this);
177 Output = new FGOutput(this);
179 State = new FGState(this);
181 // Initialize models so they can communicate with each other
183 if (!Atmosphere->InitModel()) {
184 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
186 if (!FCS->InitModel()) {
187 cerr << fgred << "FCS model init failed" << fgdef << endl;
189 if (!Propulsion->InitModel()) {
190 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
192 if (!Aircraft->InitModel()) {
193 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
195 if (!Translation->InitModel()){
196 cerr << fgred << "Translation model init failed" << fgdef << endl;
198 if (!Rotation->InitModel()) {
199 cerr << fgred << "Rotation model init failed" << fgdef << endl;
201 if (!Position->InitModel()) {
202 cerr << fgred << "Position model init failed" << fgdef << endl;
204 if (!Auxiliary->InitModel()) {
205 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
207 if (!Output->InitModel()) {
208 cerr << fgred << "Output model init failed" << fgdef << endl;
211 if (Error > 0) result = false;
213 // Schedule a model. The second arg (the integer) is the pass number. For
214 // instance, the atmosphere model gets executed every fifth pass it is called
215 // by the executive. Everything else here gets executed each pass.
217 Schedule(Atmosphere, 1);
219 Schedule(Propulsion, 1);
220 Schedule(Aircraft, 1);
221 Schedule(Rotation, 1);
222 Schedule(Translation, 1);
223 Schedule(Position, 1);
224 Schedule(Auxiliary, 1);
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 bool FGFDMExec::DeAllocate(void) {
236 if ( Atmosphere != 0 ) delete Atmosphere;
237 if ( FCS != 0 ) delete FCS;
238 if ( Propulsion != 0) delete Propulsion;
239 if ( Aircraft != 0 ) delete Aircraft;
240 if ( Translation != 0 ) delete Translation;
241 if ( Rotation != 0 ) delete Rotation;
242 if ( Position != 0 ) delete Position;
243 if ( Auxiliary != 0 ) delete Auxiliary;
244 if ( Output != 0 ) delete Output;
245 if ( State != 0 ) delete State;
265 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 int FGFDMExec::Schedule(FGModel* model, int rate)
269 FGModel* model_iterator;
271 model_iterator = FirstModel;
273 if (model_iterator == 0L) { // this is the first model
276 FirstModel->NextModel = 0L;
277 FirstModel->SetRate(rate);
279 } else { // subsequent model
281 while (model_iterator->NextModel != 0L) {
282 model_iterator = model_iterator->NextModel;
284 model_iterator->NextModel = model;
285 model_iterator->NextModel->SetRate(rate);
291 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 bool FGFDMExec::Run(void)
295 FGModel* model_iterator;
297 if (frozen) return true;
299 model_iterator = FirstModel;
300 if (model_iterator == 0L) return false;
304 if (State->Getsim_time() >= EndTime) return false;
307 if (debug_lvl & 4) cout << "=========================" << endl;
309 while (!model_iterator->Run()) {
310 model_iterator = model_iterator->NextModel;
311 if (model_iterator == 0L) break;
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
324 State->Initialize(fgic);
330 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
341 AircraftPath = APath;
343 result = Aircraft->LoadAircraft(AircraftPath, EnginePath, model);
349 << "FGFDMExec: Failed to load aircraft and/or engine model"
353 cout << "\n\nJSBSim startup complete\n\n";
357 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 bool FGFDMExec::LoadScript(string script)
361 FGConfigFile Script(script);
364 string initialize="";
367 struct condition *newCondition;
369 if (!Script.IsOpen()) return false;
371 Script.GetNextConfigLine();
372 ScriptName = Script.GetValue("name");
374 cout << "Reading Script File " << ScriptName << endl;
376 while (Script.GetNextConfigLine() != "EOF" && Script.GetValue() != "/runscript") {
377 token = Script.GetValue();
378 if (token == "use") {
379 if ((token = Script.GetValue("aircraft")) != "") {
381 cout << " Use aircraft: " << token << endl;
382 } else if ((token = Script.GetValue("initialize")) != "") {
384 cout << " Use reset file: " << token << endl;
386 cerr << "Unknown 'use' keyword: \"" << token << "\"" << endl;
388 } else if (token == "run") {
389 StartTime = strtod(Script.GetValue("start").c_str(), NULL);
390 State->Setsim_time(StartTime);
391 EndTime = strtod(Script.GetValue("end").c_str(), NULL);
392 dt = strtod(Script.GetValue("dt").c_str(), NULL);
394 Script.GetNextConfigLine();
395 token = Script.GetValue();
396 while (token != "/run") {
398 if (token == "when") {
399 Script.GetNextConfigLine();
400 token = Script.GetValue();
401 newCondition = new struct condition();
402 while (token != "/when") {
403 if (token == "parameter") {
404 newCondition->TestParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
405 newCondition->TestValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
406 newCondition->Comparison.push_back(Script.GetValue("comparison"));
407 } else if (token == "set") {
408 newCondition->SetParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
409 newCondition->SetValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
410 newCondition->Triggered.push_back(false);
411 newCondition->OriginalValue.push_back(0.0);
412 newCondition->newValue.push_back(0.0);
413 newCondition->StartTime.push_back(0.0);
414 newCondition->EndTime.push_back(0.0);
415 string tempCompare = Script.GetValue("type");
416 if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
417 else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
418 else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
419 else newCondition->Type.push_back((eType)0);
420 tempCompare = Script.GetValue("action");
421 if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
422 else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
423 else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
424 else newCondition->Action.push_back((eAction)0);
426 if (Script.GetValue("persistent") == "true")
427 newCondition->Persistent.push_back(true);
429 newCondition->Persistent.push_back(false);
431 newCondition->TC.push_back(strtod(Script.GetValue("tc").c_str(), NULL));
434 cerr << "Unrecognized keyword in script file: \" [when] " << token << "\"" << endl;
436 Script.GetNextConfigLine();
437 token = Script.GetValue();
439 Conditions.push_back(*newCondition);
440 Script.GetNextConfigLine();
441 token = Script.GetValue();
444 cerr << "Error reading script file: expected \"when\", got \"" << token << "\"" << endl;
449 cerr << "Unrecognized keyword in script file: \"" << token << "\" [runscript] " << endl;
453 if (aircraft == "") {
454 cerr << "Aircraft file not loaded in script" << endl;
458 // print out conditions for double-checking
460 vector <struct condition>::iterator iterConditions = Conditions.begin();
464 cout << "\n Script goes from " << StartTime << " to " << EndTime
465 << " with dt = " << dt << endl << endl;
467 while (iterConditions < Conditions.end()) {
468 cout << " Condition: " << count++ << endl;
471 for (int i=0; i<iterConditions->TestValue.size(); i++) {
472 if (i>0) cout << " and" << endl << " ";
473 cout << "(" << State->paramdef[iterConditions->TestParam[i]]
474 << iterConditions->Comparison[i] << " "
475 << iterConditions->TestValue[i] << ")";
477 cout << ") then {" << endl;
479 for (int i=0; i<iterConditions->SetValue.size(); i++) {
480 cout << " set" << State->paramdef[iterConditions->SetParam[i]]
481 << "to " << iterConditions->SetValue[i];
483 switch (iterConditions->Type[i]) {
485 cout << " (constant";
494 cout << " (unspecified type";
497 switch (iterConditions->Action[i]) {
505 cout << " via exponential approach";
508 cout << " via unspecified action";
511 if (!iterConditions->Persistent[i]) cout << endl
516 if (iterConditions->Action[i] == FG_RAMP ||
517 iterConditions->Action[i] == FG_EXP) cout << endl
518 << " with time constant "
519 << iterConditions->TC[i];
521 cout << ")" << endl << " }" << endl << endl;
528 result = LoadModel("aircraft", "engine", aircraft);
530 cerr << "Aircraft file " << aircraft << " was not found" << endl;
533 if ( ! State->Reset("aircraft", aircraft, initialize))
534 State->Initialize(2000,0,0,0,0,0,0.5,0.5,40000);
539 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541 void FGFDMExec::RunScript(void)
543 vector <struct condition>::iterator iC = Conditions.begin();
549 float currentTime = State->Getsim_time();
552 while (iC < Conditions.end()) {
553 // determine whether the set of conditional tests for this condition equate
555 for (int i=0; i<iC->TestValue.size(); i++) {
556 if (iC->Comparison[i] == "lt")
557 truth = State->GetParameter(iC->TestParam[i]) < iC->TestValue[i];
558 else if (iC->Comparison[i] == "le")
559 truth = State->GetParameter(iC->TestParam[i]) <= iC->TestValue[i];
560 else if (iC->Comparison[i] == "eq")
561 truth = State->GetParameter(iC->TestParam[i]) == iC->TestValue[i];
562 else if (iC->Comparison[i] == "ge")
563 truth = State->GetParameter(iC->TestParam[i]) >= iC->TestValue[i];
564 else if (iC->Comparison[i] == "gt")
565 truth = State->GetParameter(iC->TestParam[i]) > iC->TestValue[i];
566 else if (iC->Comparison[i] == "ne")
567 truth = State->GetParameter(iC->TestParam[i]) != iC->TestValue[i];
569 cerr << "Bad comparison" << endl;
571 if (i == 0) WholeTruth = truth;
572 else WholeTruth = WholeTruth && truth;
574 if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
577 // if the conditions are true, do the setting of the desired parameters
580 for (int i=0; i<iC->SetValue.size(); i++) {
581 if ( ! iC->Triggered[i]) {
582 iC->OriginalValue[i] = State->GetParameter(iC->SetParam[i]);
583 switch (iC->Type[i]) {
585 iC->newValue[i] = iC->SetValue[i];
588 iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
593 cerr << "Invalid Type specified" << endl;
596 iC->Triggered[i] = true;
597 iC->StartTime[i] = currentTime;
600 switch (iC->Action[i]) {
602 newSetValue = (currentTime - iC->StartTime[i])/(iC->TC[i])
603 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
604 if (newSetValue > iC->newValue[i]) newSetValue = iC->newValue[i];
607 newSetValue = iC->newValue[i];
610 newSetValue = (1 - exp(-(currentTime - iC->StartTime[i])/(iC->TC[i])))
611 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
614 cerr << "Invalid Action specified" << endl;
617 State->SetParameter(iC->SetParam[i], newSetValue);
624 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626 void FGFDMExec::Debug(void)
628 //TODO: Add your source code here