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
51 # if defined(sgi) && !defined(__GNUC__)
52 # include <iostream.h>
61 #include "FGFDMExec.h"
63 #include "FGAtmosphere.h"
65 #include "FGPropulsion.h"
66 #include "FGMassBalance.h"
67 #include "FGGroundReactions.h"
68 #include "FGAerodynamics.h"
69 #include "FGInertial.h"
70 #include "FGAircraft.h"
71 #include "FGTranslation.h"
72 #include "FGRotation.h"
73 #include "FGPosition.h"
74 #include "FGAuxiliary.h"
76 #include "FGConfigFile.h"
78 static const char *IdSrc = "$Id$";
79 static const char *IdHdr = ID_FDMEXEC;
81 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85 short debug_lvl; // This describes to any interested entity the debug level
86 // requested by setting the JSBSIM_DEBUG environment variable.
87 // The bitmasked value choices are as follows:
88 // a) unset: In this case (the default) JSBSim would only print
89 // out the normally expected messages, essentially echoing
90 // the config files as they are read. If the environment
91 // variable is not set, debug_lvl is set to 1 internally
92 // b) 0: This requests JSBSim not to output any messages
94 // c) 1: This value explicity requests the normal JSBSim
96 // d) 2: This value asks for a message to be printed out when
97 // a class is instantiated
98 // e) 4: When this value is set, a message is displayed when a
99 // FGModel object executes its Run() method
100 // f) 8: When this value is set, various runtime state variables
101 // are printed out periodically
102 // g) 16: When set various parameters are sanity checked and
103 // a message is printed out when they go out of bounds.
105 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
111 FGFDMExec::FGFDMExec(void)
137 char* num = getenv("JSBSIM_DEBUG");
138 if (!num) debug_lvl = 1;
139 else debug_lvl = atoi(num); // set debug level
140 } catch (...) { // if error set to 1
145 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
146 << JSBSIM_VERSION << underoff << normint << endl;
147 cout << halfint << " [cfg file spec v" << NEEDED_CFG_VERSION << "]\n\n";
148 cout << normint << "JSBSim startup beginning ...\n\n";
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 MassBalance = new FGMassBalance(this);
173 Aerodynamics = new FGAerodynamics (this);
174 Inertial = new FGInertial(this);
175 GroundReactions = new FGGroundReactions(this);
176 Aircraft = new FGAircraft(this);
177 Translation = new FGTranslation(this);
178 Rotation = new FGRotation(this);
179 Position = new FGPosition(this);
180 Auxiliary = new FGAuxiliary(this);
181 Output = new FGOutput(this);
183 State = new FGState(this); // This must be done here, as the FGState
184 // class needs valid pointers to the above
187 // Initialize models so they can communicate with each other
189 if (!Atmosphere->InitModel()) {
190 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
192 if (!FCS->InitModel()) {
193 cerr << fgred << "FCS model init failed" << fgdef << endl;
195 if (!Propulsion->InitModel()) {
196 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
198 if (!MassBalance->InitModel()) {
199 cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
201 if (!Aerodynamics->InitModel()) {
202 cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
204 if (!Inertial->InitModel()) {
205 cerr << fgred << "FGInertial model init failed" << fgdef << endl;
207 if (!GroundReactions->InitModel()) {
208 cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
210 if (!Aircraft->InitModel()) {
211 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
213 if (!Translation->InitModel()){
214 cerr << fgred << "Translation model init failed" << fgdef << endl;
216 if (!Rotation->InitModel()) {
217 cerr << fgred << "Rotation model init failed" << fgdef << endl;
219 if (!Position->InitModel()) {
220 cerr << fgred << "Position model init failed" << fgdef << endl;
222 if (!Auxiliary->InitModel()) {
223 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
225 if (!Output->InitModel()) {
226 cerr << fgred << "Output model init failed" << fgdef << endl;
229 if (Error > 0) result = false;
231 // Schedule a model. The second arg (the integer) is the pass number. For
232 // instance, the atmosphere model gets executed every fifth pass it is called
233 // by the executive. Everything else here gets executed each pass.
235 Schedule(Atmosphere, 1);
237 Schedule(Propulsion, 1);
238 Schedule(MassBalance, 1);
239 Schedule(Aerodynamics, 1);
240 Schedule(Inertial, 1);
241 Schedule(GroundReactions, 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 ( GroundReactions != 0) delete GroundReactions;
265 if ( Aircraft != 0 ) delete Aircraft;
266 if ( Translation != 0 ) delete Translation;
267 if ( Rotation != 0 ) delete Rotation;
268 if ( Position != 0 ) delete Position;
269 if ( Auxiliary != 0 ) delete Auxiliary;
270 if ( Output != 0 ) delete Output;
271 if ( State != 0 ) delete State;
295 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 int FGFDMExec::Schedule(FGModel* model, int rate)
299 FGModel* model_iterator;
301 model_iterator = FirstModel;
303 if (model_iterator == 0L) { // this is the first model
306 FirstModel->NextModel = 0L;
307 FirstModel->SetRate(rate);
309 } else { // subsequent model
311 while (model_iterator->NextModel != 0L) {
312 model_iterator = model_iterator->NextModel;
314 model_iterator->NextModel = model;
315 model_iterator->NextModel->SetRate(rate);
321 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 bool FGFDMExec::Run(void)
325 FGModel* model_iterator;
327 if (frozen) return true;
329 model_iterator = FirstModel;
330 if (model_iterator == 0L) return false;
334 if (State->Getsim_time() >= EndTime) return false;
338 cout << "================== Frame: " << Frame << " Time: "
339 << State->Getsim_time() << endl;
341 while (!model_iterator->Run()) {
342 model_iterator = model_iterator->NextModel;
343 if (model_iterator == 0L) break;
352 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
357 State->Initialize(fgic);
363 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
369 string aircraftCfgFileName;
371 AircraftPath = APath;
375 aircraftCfgFileName = AircraftPath + "/" + model + "/" + model + ".xml";
377 aircraftCfgFileName = AircraftPath + ";" + model + ";" + model + ".xml";
380 FGConfigFile AC_cfg(aircraftCfgFileName);
381 if (!AC_cfg.IsOpen()) return false;
388 result = Aircraft->Load(&AC_cfg);
394 << "FGFDMExec: Failed to load aircraft and/or engine model"
398 if (debug_lvl > 0) cout << "\n\nJSBSim startup complete\n\n";
402 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 bool FGFDMExec::LoadScript(string script)
406 FGConfigFile Script(script);
409 string initialize="";
413 struct condition *newCondition;
415 if (!Script.IsOpen()) return false;
417 Script.GetNextConfigLine();
418 ScriptName = Script.GetValue("name");
420 if (debug_lvl > 0) cout << "Reading Script File " << ScriptName << endl;
422 while (Script.GetNextConfigLine() != "EOF" && Script.GetValue() != "/runscript") {
423 token = Script.GetValue();
424 if (token == "use") {
425 if ((token = Script.GetValue("aircraft")) != "") {
427 if (debug_lvl > 0) cout << " Use aircraft: " << token << endl;
428 } else if ((token = Script.GetValue("initialize")) != "") {
430 if (debug_lvl > 0) cout << " Use reset file: " << token << endl;
432 cerr << "Unknown 'use' keyword: \"" << token << "\"" << endl;
434 } else if (token == "run") {
435 StartTime = strtod(Script.GetValue("start").c_str(), NULL);
436 State->Setsim_time(StartTime);
437 EndTime = strtod(Script.GetValue("end").c_str(), NULL);
438 dt = strtod(Script.GetValue("dt").c_str(), NULL);
440 Script.GetNextConfigLine();
441 token = Script.GetValue();
442 while (token != "/run") {
444 if (token == "when") {
445 Script.GetNextConfigLine();
446 token = Script.GetValue();
447 newCondition = new struct condition();
448 while (token != "/when") {
449 if (token == "parameter") {
450 newCondition->TestParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
451 newCondition->TestValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
452 newCondition->Comparison.push_back(Script.GetValue("comparison"));
453 } else if (token == "set") {
454 newCondition->SetParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
455 newCondition->SetValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
456 newCondition->Triggered.push_back(false);
457 newCondition->OriginalValue.push_back(0.0);
458 newCondition->newValue.push_back(0.0);
459 newCondition->StartTime.push_back(0.0);
460 newCondition->EndTime.push_back(0.0);
461 string tempCompare = Script.GetValue("type");
462 if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
463 else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
464 else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
465 else newCondition->Type.push_back((eType)0);
466 tempCompare = Script.GetValue("action");
467 if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
468 else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
469 else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
470 else newCondition->Action.push_back((eAction)0);
472 if (Script.GetValue("persistent") == "true")
473 newCondition->Persistent.push_back(true);
475 newCondition->Persistent.push_back(false);
477 newCondition->TC.push_back(strtod(Script.GetValue("tc").c_str(), NULL));
480 cerr << "Unrecognized keyword in script file: \" [when] " << token << "\"" << endl;
482 Script.GetNextConfigLine();
483 token = Script.GetValue();
485 Conditions.push_back(*newCondition);
486 Script.GetNextConfigLine();
487 token = Script.GetValue();
490 cerr << "Error reading script file: expected \"when\", got \"" << token << "\"" << endl;
495 cerr << "Unrecognized keyword in script file: \"" << token << "\" [runscript] " << endl;
499 if (aircraft == "") {
500 cerr << "Aircraft file not loaded in script" << endl;
504 // print out conditions for double-checking if requested
507 vector <struct condition>::iterator iterConditions = Conditions.begin();
510 cout << "\n Script goes from " << StartTime << " to " << EndTime
511 << " with dt = " << dt << endl << endl;
513 while (iterConditions < Conditions.end()) {
514 cout << " Condition: " << count++ << endl;
517 for (i=0; i<iterConditions->TestValue.size(); i++) {
518 if (i>0) cout << " and" << endl << " ";
519 cout << "(" << State->paramdef[iterConditions->TestParam[i]]
520 << iterConditions->Comparison[i] << " "
521 << iterConditions->TestValue[i] << ")";
525 for (i=0; i<iterConditions->SetValue.size(); i++) {
526 cout << endl << " set" << State->paramdef[iterConditions->SetParam[i]]
527 << "to " << iterConditions->SetValue[i];
529 switch (iterConditions->Type[i]) {
531 cout << " (constant";
540 cout << " (unspecified type";
543 switch (iterConditions->Action[i]) {
551 cout << " via exponential approach";
554 cout << " via unspecified action";
557 if (!iterConditions->Persistent[i]) cout << endl
562 if (iterConditions->Action[i] == FG_RAMP ||
563 iterConditions->Action[i] == FG_EXP) cout << endl
564 << " with time constant "
565 << iterConditions->TC[i];
567 cout << ")" << endl << " }" << endl << endl;
575 result = LoadModel("aircraft", "engine", aircraft);
577 cerr << "Aircraft file " << aircraft << " was not found" << endl;
580 if ( ! State->Reset("aircraft", aircraft, initialize))
581 State->Initialize(2000,0,0,0,0,0,0.5,0.5,40000,0,0,0);
586 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588 void FGFDMExec::RunScript(void)
590 vector <struct condition>::iterator iC = Conditions.begin();
597 float currentTime = State->Getsim_time();
600 while (iC < Conditions.end()) {
601 // determine whether the set of conditional tests for this condition equate
603 for (i=0; i<iC->TestValue.size(); i++) {
604 if (iC->Comparison[i] == "lt")
605 truth = State->GetParameter(iC->TestParam[i]) < iC->TestValue[i];
606 else if (iC->Comparison[i] == "le")
607 truth = State->GetParameter(iC->TestParam[i]) <= iC->TestValue[i];
608 else if (iC->Comparison[i] == "eq")
609 truth = State->GetParameter(iC->TestParam[i]) == iC->TestValue[i];
610 else if (iC->Comparison[i] == "ge")
611 truth = State->GetParameter(iC->TestParam[i]) >= iC->TestValue[i];
612 else if (iC->Comparison[i] == "gt")
613 truth = State->GetParameter(iC->TestParam[i]) > iC->TestValue[i];
614 else if (iC->Comparison[i] == "ne")
615 truth = State->GetParameter(iC->TestParam[i]) != iC->TestValue[i];
617 cerr << "Bad comparison" << endl;
619 if (i == 0) WholeTruth = truth;
620 else WholeTruth = WholeTruth && truth;
622 if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
625 // if the conditions are true, do the setting of the desired parameters
628 for (i=0; i<iC->SetValue.size(); i++) {
629 if ( ! iC->Triggered[i]) {
630 iC->OriginalValue[i] = State->GetParameter(iC->SetParam[i]);
631 switch (iC->Type[i]) {
633 iC->newValue[i] = iC->SetValue[i];
636 iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
641 cerr << "Invalid Type specified" << endl;
644 iC->Triggered[i] = true;
645 iC->StartTime[i] = currentTime;
648 switch (iC->Action[i]) {
650 newSetValue = (currentTime - iC->StartTime[i])/(iC->TC[i])
651 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
652 if (newSetValue > iC->newValue[i]) newSetValue = iC->newValue[i];
655 newSetValue = iC->newValue[i];
658 newSetValue = (1 - exp(-(currentTime - iC->StartTime[i])/(iC->TC[i])))
659 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
662 cerr << "Invalid Action specified" << endl;
665 State->SetParameter(iC->SetParam[i], newSetValue);
672 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 void FGFDMExec::Debug(void)
676 //TODO: Add your source code here