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)
139 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
140 << JSBSIM_VERSION << underoff << normint << endl;
141 cout << halfint << " [cfg file spec v" << NEEDED_CFG_VERSION << "]\n\n";
142 cout << normint << "JSBSim startup beginning ...\n\n";
145 char* num = getenv("JSBSIM_DEBUG");
146 if (!num) debug_lvl = 1;
147 else debug_lvl = atoi(num); // set debug level
148 } catch (...) { // if error set to 1
152 if (debug_lvl & 2) cout << "Instantiated: FGFDMExec" << endl;
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 FGFDMExec::~FGFDMExec() {
161 if (debug_lvl & 2) cout << "Destroyed: FGFDMExec" << endl;
164 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 bool FGFDMExec::Allocate(void) {
170 Atmosphere = new FGAtmosphere(this);
171 FCS = new FGFCS(this);
172 Propulsion = new FGPropulsion(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);
182 // Initialize models so they can communicate with each other
184 if (!Atmosphere->InitModel()) {
185 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
187 if (!FCS->InitModel()) {
188 cerr << fgred << "FCS model init failed" << fgdef << endl;
190 if (!Propulsion->InitModel()) {
191 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
193 if (!Aircraft->InitModel()) {
194 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
196 if (!Translation->InitModel()){
197 cerr << fgred << "Translation model init failed" << fgdef << endl;
199 if (!Rotation->InitModel()) {
200 cerr << fgred << "Rotation model init failed" << fgdef << endl;
202 if (!Position->InitModel()) {
203 cerr << fgred << "Position model init failed" << fgdef << endl;
205 if (!Auxiliary->InitModel()) {
206 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
208 if (!Output->InitModel()) {
209 cerr << fgred << "Output model init failed" << fgdef << endl;
212 if (Error > 0) result = false;
214 // Schedule a model. The second arg (the integer) is the pass number. For
215 // instance, the atmosphere model gets executed every fifth pass it is called
216 // by the executive. Everything else here gets executed each pass.
218 Schedule(Atmosphere, 1);
220 Schedule(Propulsion, 1);
221 Schedule(Aircraft, 1);
222 Schedule(Rotation, 1);
223 Schedule(Translation, 1);
224 Schedule(Position, 1);
225 Schedule(Auxiliary, 1);
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 bool FGFDMExec::DeAllocate(void) {
237 if ( Atmosphere != 0 ) delete Atmosphere;
238 if ( FCS != 0 ) delete FCS;
239 if ( Propulsion != 0) delete Propulsion;
240 if ( Aircraft != 0 ) delete Aircraft;
241 if ( Translation != 0 ) delete Translation;
242 if ( Rotation != 0 ) delete Rotation;
243 if ( Position != 0 ) delete Position;
244 if ( Auxiliary != 0 ) delete Auxiliary;
245 if ( Output != 0 ) delete Output;
246 if ( State != 0 ) delete State;
266 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 int FGFDMExec::Schedule(FGModel* model, int rate)
270 FGModel* model_iterator;
272 model_iterator = FirstModel;
274 if (model_iterator == 0L) { // this is the first model
277 FirstModel->NextModel = 0L;
278 FirstModel->SetRate(rate);
280 } else { // subsequent model
282 while (model_iterator->NextModel != 0L) {
283 model_iterator = model_iterator->NextModel;
285 model_iterator->NextModel = model;
286 model_iterator->NextModel->SetRate(rate);
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 bool FGFDMExec::Run(void)
296 FGModel* model_iterator;
298 if (frozen) return true;
300 model_iterator = FirstModel;
301 if (model_iterator == 0L) return false;
305 if (State->Getsim_time() >= EndTime) return false;
309 cout << "================== Frame: " << Frame << " Time: "
310 << State->Getsim_time() << endl;
312 while (!model_iterator->Run()) {
313 model_iterator = model_iterator->NextModel;
314 if (model_iterator == 0L) break;
323 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
328 State->Initialize(fgic);
334 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
345 AircraftPath = APath;
347 result = Aircraft->LoadAircraft(AircraftPath, EnginePath, model);
353 << "FGFDMExec: Failed to load aircraft and/or engine model"
357 cout << "\n\nJSBSim startup complete\n\n";
361 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363 bool FGFDMExec::LoadScript(string script)
365 FGConfigFile Script(script);
368 string initialize="";
371 struct condition *newCondition;
373 if (!Script.IsOpen()) return false;
375 Script.GetNextConfigLine();
376 ScriptName = Script.GetValue("name");
378 cout << "Reading Script File " << ScriptName << endl;
380 while (Script.GetNextConfigLine() != "EOF" && Script.GetValue() != "/runscript") {
381 token = Script.GetValue();
382 if (token == "use") {
383 if ((token = Script.GetValue("aircraft")) != "") {
385 cout << " Use aircraft: " << token << endl;
386 } else if ((token = Script.GetValue("initialize")) != "") {
388 cout << " Use reset file: " << token << endl;
390 cerr << "Unknown 'use' keyword: \"" << token << "\"" << endl;
392 } else if (token == "run") {
393 StartTime = strtod(Script.GetValue("start").c_str(), NULL);
394 State->Setsim_time(StartTime);
395 EndTime = strtod(Script.GetValue("end").c_str(), NULL);
396 dt = strtod(Script.GetValue("dt").c_str(), NULL);
398 Script.GetNextConfigLine();
399 token = Script.GetValue();
400 while (token != "/run") {
402 if (token == "when") {
403 Script.GetNextConfigLine();
404 token = Script.GetValue();
405 newCondition = new struct condition();
406 while (token != "/when") {
407 if (token == "parameter") {
408 newCondition->TestParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
409 newCondition->TestValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
410 newCondition->Comparison.push_back(Script.GetValue("comparison"));
411 } else if (token == "set") {
412 newCondition->SetParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
413 newCondition->SetValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
414 newCondition->Triggered.push_back(false);
415 newCondition->OriginalValue.push_back(0.0);
416 newCondition->newValue.push_back(0.0);
417 newCondition->StartTime.push_back(0.0);
418 newCondition->EndTime.push_back(0.0);
419 string tempCompare = Script.GetValue("type");
420 if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
421 else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
422 else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
423 else newCondition->Type.push_back((eType)0);
424 tempCompare = Script.GetValue("action");
425 if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
426 else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
427 else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
428 else newCondition->Action.push_back((eAction)0);
430 if (Script.GetValue("persistent") == "true")
431 newCondition->Persistent.push_back(true);
433 newCondition->Persistent.push_back(false);
435 newCondition->TC.push_back(strtod(Script.GetValue("tc").c_str(), NULL));
438 cerr << "Unrecognized keyword in script file: \" [when] " << token << "\"" << endl;
440 Script.GetNextConfigLine();
441 token = Script.GetValue();
443 Conditions.push_back(*newCondition);
444 Script.GetNextConfigLine();
445 token = Script.GetValue();
448 cerr << "Error reading script file: expected \"when\", got \"" << token << "\"" << endl;
453 cerr << "Unrecognized keyword in script file: \"" << token << "\" [runscript] " << endl;
457 if (aircraft == "") {
458 cerr << "Aircraft file not loaded in script" << endl;
462 // print out conditions for double-checking
464 vector <struct condition>::iterator iterConditions = Conditions.begin();
468 cout << "\n Script goes from " << StartTime << " to " << EndTime
469 << " with dt = " << dt << endl << endl;
471 while (iterConditions < Conditions.end()) {
472 cout << " Condition: " << count++ << endl;
475 for (int i=0; i<iterConditions->TestValue.size(); i++) {
476 if (i>0) cout << " and" << endl << " ";
477 cout << "(" << State->paramdef[iterConditions->TestParam[i]]
478 << iterConditions->Comparison[i] << " "
479 << iterConditions->TestValue[i] << ")";
481 cout << ") then {" << endl;
483 for (int i=0; i<iterConditions->SetValue.size(); i++) {
484 cout << " set" << State->paramdef[iterConditions->SetParam[i]]
485 << "to " << iterConditions->SetValue[i];
487 switch (iterConditions->Type[i]) {
489 cout << " (constant";
498 cout << " (unspecified type";
501 switch (iterConditions->Action[i]) {
509 cout << " via exponential approach";
512 cout << " via unspecified action";
515 if (!iterConditions->Persistent[i]) cout << endl
520 if (iterConditions->Action[i] == FG_RAMP ||
521 iterConditions->Action[i] == FG_EXP) cout << endl
522 << " with time constant "
523 << iterConditions->TC[i];
525 cout << ")" << endl << " }" << endl << endl;
532 result = LoadModel("aircraft", "engine", aircraft);
534 cerr << "Aircraft file " << aircraft << " was not found" << endl;
537 if ( ! State->Reset("aircraft", aircraft, initialize))
538 State->Initialize(2000,0,0,0,0,0,0.5,0.5,40000);
543 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545 void FGFDMExec::RunScript(void)
547 vector <struct condition>::iterator iC = Conditions.begin();
553 float currentTime = State->Getsim_time();
556 while (iC < Conditions.end()) {
557 // determine whether the set of conditional tests for this condition equate
559 for (int i=0; i<iC->TestValue.size(); i++) {
560 if (iC->Comparison[i] == "lt")
561 truth = State->GetParameter(iC->TestParam[i]) < iC->TestValue[i];
562 else if (iC->Comparison[i] == "le")
563 truth = State->GetParameter(iC->TestParam[i]) <= iC->TestValue[i];
564 else if (iC->Comparison[i] == "eq")
565 truth = State->GetParameter(iC->TestParam[i]) == iC->TestValue[i];
566 else if (iC->Comparison[i] == "ge")
567 truth = State->GetParameter(iC->TestParam[i]) >= iC->TestValue[i];
568 else if (iC->Comparison[i] == "gt")
569 truth = State->GetParameter(iC->TestParam[i]) > iC->TestValue[i];
570 else if (iC->Comparison[i] == "ne")
571 truth = State->GetParameter(iC->TestParam[i]) != iC->TestValue[i];
573 cerr << "Bad comparison" << endl;
575 if (i == 0) WholeTruth = truth;
576 else WholeTruth = WholeTruth && truth;
578 if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
581 // if the conditions are true, do the setting of the desired parameters
584 for (int i=0; i<iC->SetValue.size(); i++) {
585 if ( ! iC->Triggered[i]) {
586 iC->OriginalValue[i] = State->GetParameter(iC->SetParam[i]);
587 switch (iC->Type[i]) {
589 iC->newValue[i] = iC->SetValue[i];
592 iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
597 cerr << "Invalid Type specified" << endl;
600 iC->Triggered[i] = true;
601 iC->StartTime[i] = currentTime;
604 switch (iC->Action[i]) {
606 newSetValue = (currentTime - iC->StartTime[i])/(iC->TC[i])
607 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
608 if (newSetValue > iC->newValue[i]) newSetValue = iC->newValue[i];
611 newSetValue = iC->newValue[i];
614 newSetValue = (1 - exp(-(currentTime - iC->StartTime[i])/(iC->TC[i])))
615 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
618 cerr << "Invalid Action specified" << endl;
621 State->SetParameter(iC->SetParam[i], newSetValue);
628 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 void FGFDMExec::Debug(void)
632 //TODO: Add your source code here