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 unsigned int FGFDMExec::FDMctr = 0;
107 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
113 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 GroundReactions = new FGGroundReactions(this);
181 Aircraft = new FGAircraft(this);
182 Translation = new FGTranslation(this);
183 Rotation = new FGRotation(this);
184 Position = new FGPosition(this);
185 Auxiliary = new FGAuxiliary(this);
186 Output = new FGOutput(this);
188 State = new FGState(this); // This must be done here, as the FGState
189 // class needs valid pointers to the above
192 // Initialize models so they can communicate with each other
194 if (!Atmosphere->InitModel()) {
195 cerr << fgred << "Atmosphere model init failed" << fgdef << endl;
197 if (!FCS->InitModel()) {
198 cerr << fgred << "FCS model init failed" << fgdef << endl;
200 if (!Propulsion->InitModel()) {
201 cerr << fgred << "FGPropulsion model init failed" << fgdef << endl;
203 if (!MassBalance->InitModel()) {
204 cerr << fgred << "FGMassBalance model init failed" << fgdef << endl;
206 if (!Aerodynamics->InitModel()) {
207 cerr << fgred << "FGAerodynamics model init failed" << fgdef << endl;
209 if (!Inertial->InitModel()) {
210 cerr << fgred << "FGInertial model init failed" << fgdef << endl;
212 if (!GroundReactions->InitModel()) {
213 cerr << fgred << "Ground Reactions model init failed" << fgdef << endl;
215 if (!Aircraft->InitModel()) {
216 cerr << fgred << "Aircraft model init failed" << fgdef << endl;
218 if (!Translation->InitModel()){
219 cerr << fgred << "Translation model init failed" << fgdef << endl;
221 if (!Rotation->InitModel()) {
222 cerr << fgred << "Rotation model init failed" << fgdef << endl;
224 if (!Position->InitModel()) {
225 cerr << fgred << "Position model init failed" << fgdef << endl;
227 if (!Auxiliary->InitModel()) {
228 cerr << fgred << "Auxiliary model init failed" << fgdef << endl;
230 if (!Output->InitModel()) {
231 cerr << fgred << "Output model init failed" << fgdef << endl;
234 if (Error > 0) result = false;
236 // Schedule a model. The second arg (the integer) is the pass number. For
237 // instance, the atmosphere model gets executed every fifth pass it is called
238 // by the executive. Everything else here gets executed each pass.
240 Schedule(Atmosphere, 1);
242 Schedule(Propulsion, 1);
243 Schedule(MassBalance, 1);
244 Schedule(Aerodynamics, 1);
245 Schedule(Inertial, 1);
246 Schedule(GroundReactions, 1);
247 Schedule(Aircraft, 1);
248 Schedule(Rotation, 1);
249 Schedule(Translation, 1);
250 Schedule(Position, 1);
251 Schedule(Auxiliary, 1);
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 bool FGFDMExec::DeAllocate(void) {
263 if ( Atmosphere != 0 ) delete Atmosphere;
264 if ( FCS != 0 ) delete FCS;
265 if ( Propulsion != 0) delete Propulsion;
266 if ( MassBalance != 0) delete MassBalance;
267 if ( Aerodynamics != 0) delete Aerodynamics;
268 if ( Inertial != 0) delete Inertial;
269 if ( GroundReactions != 0) delete GroundReactions;
270 if ( Aircraft != 0 ) delete Aircraft;
271 if ( Translation != 0 ) delete Translation;
272 if ( Rotation != 0 ) delete Rotation;
273 if ( Position != 0 ) delete Position;
274 if ( Auxiliary != 0 ) delete Auxiliary;
275 if ( Output != 0 ) delete Output;
276 if ( State != 0 ) delete State;
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 int FGFDMExec::Schedule(FGModel* model, int rate)
304 FGModel* model_iterator;
306 model_iterator = FirstModel;
308 if (model_iterator == 0L) { // this is the first model
311 FirstModel->NextModel = 0L;
312 FirstModel->SetRate(rate);
314 } else { // subsequent model
316 while (model_iterator->NextModel != 0L) {
317 model_iterator = model_iterator->NextModel;
319 model_iterator->NextModel = model;
320 model_iterator->NextModel->SetRate(rate);
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 bool FGFDMExec::Run(void)
331 FGModel* model_iterator;
333 if (frozen) return true;
335 model_iterator = FirstModel;
336 if (model_iterator == 0L) return false;
340 if (State->Getsim_time() >= EndTime) return false;
344 cout << "================== Frame: " << Frame << " Time: "
345 << State->Getsim_time() << endl;
347 while (!model_iterator->Run()) {
348 model_iterator = model_iterator->NextModel;
349 if (model_iterator == 0L) break;
358 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 bool FGFDMExec::RunIC(FGInitialCondition *fgic)
363 State->Initialize(fgic);
369 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371 bool FGFDMExec::LoadModel(string APath, string EPath, string model)
375 string aircraftCfgFileName;
377 AircraftPath = APath;
381 aircraftCfgFileName = AircraftPath + "/" + model + "/" + model + ".xml";
383 aircraftCfgFileName = AircraftPath + ";" + model + ";" + model + ".xml";
386 FGConfigFile AC_cfg(aircraftCfgFileName);
387 if (!AC_cfg.IsOpen()) return false;
394 result = Aircraft->Load(&AC_cfg);
398 if (debug_lvl > 0) cout << "\n\nJSBSim startup complete\n\n";
401 << " FGFDMExec: Failed to load aircraft and/or engine model"
408 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 bool FGFDMExec::LoadScript(string script)
412 FGConfigFile Script(script);
415 string initialize="";
419 struct condition *newCondition;
421 if (!Script.IsOpen()) return false;
423 Script.GetNextConfigLine();
424 ScriptName = Script.GetValue("name");
426 if (debug_lvl > 0) cout << "Reading Script File " << ScriptName << endl;
428 while (Script.GetNextConfigLine() != string("EOF") && Script.GetValue() != string("/runscript")) {
429 token = Script.GetValue();
430 if (token == "use") {
431 if ((token = Script.GetValue("aircraft")) != string("")) {
433 if (debug_lvl > 0) cout << " Use aircraft: " << token << endl;
434 } else if ((token = Script.GetValue("initialize")) != string("")) {
436 if (debug_lvl > 0) cout << " Use reset file: " << token << endl;
438 cerr << "Unknown 'use' keyword: \"" << token << "\"" << endl;
440 } else if (token == "run") {
441 StartTime = strtod(Script.GetValue("start").c_str(), NULL);
442 State->Setsim_time(StartTime);
443 EndTime = strtod(Script.GetValue("end").c_str(), NULL);
444 dt = strtod(Script.GetValue("dt").c_str(), NULL);
446 Script.GetNextConfigLine();
447 token = Script.GetValue();
448 while (token != string("/run")) {
450 if (token == "when") {
451 Script.GetNextConfigLine();
452 token = Script.GetValue();
453 newCondition = new struct condition();
454 while (token != string("/when")) {
455 if (token == "parameter") {
456 newCondition->TestParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
457 newCondition->TestValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
458 newCondition->Comparison.push_back(Script.GetValue("comparison"));
459 } else if (token == "set") {
460 newCondition->SetParam.push_back(State->GetParameterIndex(Script.GetValue("name")));
461 newCondition->SetValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
462 newCondition->Triggered.push_back(false);
463 newCondition->OriginalValue.push_back(0.0);
464 newCondition->newValue.push_back(0.0);
465 newCondition->StartTime.push_back(0.0);
466 newCondition->EndTime.push_back(0.0);
467 string tempCompare = Script.GetValue("type");
468 if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
469 else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
470 else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
471 else newCondition->Type.push_back((eType)0);
472 tempCompare = Script.GetValue("action");
473 if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
474 else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
475 else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
476 else newCondition->Action.push_back((eAction)0);
478 if (Script.GetValue("persistent") == "true")
479 newCondition->Persistent.push_back(true);
481 newCondition->Persistent.push_back(false);
483 newCondition->TC.push_back(strtod(Script.GetValue("tc").c_str(), NULL));
486 cerr << "Unrecognized keyword in script file: \" [when] " << token << "\"" << endl;
488 Script.GetNextConfigLine();
489 token = Script.GetValue();
491 Conditions.push_back(*newCondition);
492 Script.GetNextConfigLine();
493 token = Script.GetValue();
496 cerr << "Error reading script file: expected \"when\", got \"" << token << "\"" << endl;
501 cerr << "Unrecognized keyword in script file: \"" << token << "\" [runscript] " << endl;
505 if (aircraft == "") {
506 cerr << "Aircraft file not loaded in script" << endl;
510 // print out conditions for double-checking if requested
513 vector <struct condition>::iterator iterConditions = Conditions.begin();
516 cout << "\n Script goes from " << StartTime << " to " << EndTime
517 << " with dt = " << dt << endl << endl;
519 while (iterConditions < Conditions.end()) {
520 cout << " Condition: " << count++ << endl;
523 for (i=0; i<iterConditions->TestValue.size(); i++) {
524 if (i>0) cout << " and" << endl << " ";
525 cout << "(" << State->paramdef[iterConditions->TestParam[i]]
526 << iterConditions->Comparison[i] << " "
527 << iterConditions->TestValue[i] << ")";
531 for (i=0; i<iterConditions->SetValue.size(); i++) {
532 cout << endl << " set" << State->paramdef[iterConditions->SetParam[i]]
533 << "to " << iterConditions->SetValue[i];
535 switch (iterConditions->Type[i]) {
537 cout << " (constant";
546 cout << " (unspecified type";
549 switch (iterConditions->Action[i]) {
557 cout << " via exponential approach";
560 cout << " via unspecified action";
563 if (!iterConditions->Persistent[i]) cout << endl
568 if (iterConditions->Action[i] == FG_RAMP ||
569 iterConditions->Action[i] == FG_EXP) cout << endl
570 << " with time constant "
571 << iterConditions->TC[i];
573 cout << ")" << endl << " }" << endl << endl;
581 result = LoadModel("aircraft", "engine", aircraft);
583 cerr << "Aircraft file " << aircraft << " was not found" << endl;
586 if ( ! State->Reset("aircraft", aircraft, initialize))
587 State->Initialize(2000,0,0,0,0,0,0.5,0.5,40000,0,0,0);
592 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
594 void FGFDMExec::RunScript(void)
596 vector <struct condition>::iterator iC = Conditions.begin();
603 double currentTime = State->Getsim_time();
606 while (iC < Conditions.end()) {
607 // determine whether the set of conditional tests for this condition equate
609 for (i=0; i<iC->TestValue.size(); i++) {
610 if (iC->Comparison[i] == "lt")
611 truth = State->GetParameter(iC->TestParam[i]) < iC->TestValue[i];
612 else if (iC->Comparison[i] == "le")
613 truth = State->GetParameter(iC->TestParam[i]) <= iC->TestValue[i];
614 else if (iC->Comparison[i] == "eq")
615 truth = State->GetParameter(iC->TestParam[i]) == iC->TestValue[i];
616 else if (iC->Comparison[i] == "ge")
617 truth = State->GetParameter(iC->TestParam[i]) >= iC->TestValue[i];
618 else if (iC->Comparison[i] == "gt")
619 truth = State->GetParameter(iC->TestParam[i]) > iC->TestValue[i];
620 else if (iC->Comparison[i] == "ne")
621 truth = State->GetParameter(iC->TestParam[i]) != iC->TestValue[i];
623 cerr << "Bad comparison" << endl;
625 if (i == 0) WholeTruth = truth;
626 else WholeTruth = WholeTruth && truth;
628 if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
631 // if the conditions are true, do the setting of the desired parameters
634 for (i=0; i<iC->SetValue.size(); i++) {
635 if ( ! iC->Triggered[i]) {
636 iC->OriginalValue[i] = State->GetParameter(iC->SetParam[i]);
637 switch (iC->Type[i]) {
639 iC->newValue[i] = iC->SetValue[i];
642 iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
647 cerr << "Invalid Type specified" << endl;
650 iC->Triggered[i] = true;
651 iC->StartTime[i] = currentTime;
654 switch (iC->Action[i]) {
656 newSetValue = (currentTime - iC->StartTime[i])/(iC->TC[i])
657 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
658 if (newSetValue > iC->newValue[i]) newSetValue = iC->newValue[i];
661 newSetValue = iC->newValue[i];
664 newSetValue = (1 - exp(-(currentTime - iC->StartTime[i])/(iC->TC[i])))
665 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
668 cerr << "Invalid Action specified" << endl;
671 State->SetParameter(iC->SetParam[i], newSetValue);
672 if ((unsigned long int)Propulsion->GetTank(0) == 0) {
673 cout << "Param # getting set: " << iC->SetParam[i] << " Value: " << newSetValue << endl;
681 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683 void FGFDMExec::Debug(void)
685 //TODO: Add your source code here