1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Loads and runs JSBSim scripts.
8 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
30 This class wraps up the simulation scripting routines.
33 --------------------------------------------------------------------------------
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES, and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 # include <simgear/compiler.h>
46 # include STL_IOSTREAM
47 # include STL_ITERATOR
49 # if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
50 # include <iostream.h>
58 #include "FGConfigFile.h"
63 static const char *IdSrc = "$Id$";
64 static const char *IdHdr = ID_FGSCRIPT;
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76 FGScript::FGScript(FGFDMExec* fgex) : FDMExec(fgex)
78 State = FDMExec->GetState();
79 PropertyManager=FDMExec->GetPropertyManager();
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 bool FGScript::LoadScript( string script )
94 FGConfigFile Script(script);
101 struct condition *newCondition;
103 if (!Script.IsOpen()) return false;
105 Script.GetNextConfigLine();
106 if (Script.GetValue("runscript").length() <= 0) {
107 cerr << "File: " << script << " is not a script file" << endl;
111 ScriptName = Script.GetValue("name");
114 if (debug_lvl > 0) cout << "Reading and running from script file " << ScriptName << endl << endl;
116 while (Script.GetNextConfigLine() != string("EOF") && Script.GetValue() != string("/runscript")) {
117 token = Script.GetValue();
118 if (token == "use") {
119 if ((token = Script.GetValue("aircraft")) != string("")) {
121 result = FDMExec->LoadModel(aircraft);
123 cerr << "Aircraft file " << aircraft << " was not found" << endl;
126 if (debug_lvl > 0) cout << " Use aircraft: " << token << endl;
127 } else if ((token = Script.GetValue("initialize")) != string("")) {
129 if (debug_lvl > 0) cout << " Use reset file: " << token << endl;
131 cerr << "Unknown 'use' keyword: \"" << token << "\"" << endl;
133 } else if (token == "run") {
134 StartTime = strtod(Script.GetValue("start").c_str(), NULL);
135 State->Setsim_time(StartTime);
136 EndTime = strtod(Script.GetValue("end").c_str(), NULL);
137 dt = strtod(Script.GetValue("dt").c_str(), NULL);
139 Script.GetNextConfigLine();
140 token = Script.GetValue();
141 while (token != string("/run")) {
143 if (token == "when") {
144 Script.GetNextConfigLine();
145 token = Script.GetValue();
146 newCondition = new struct condition();
147 while (token != string("/when")) {
148 if (token == "parameter") {
149 prop_name = Script.GetValue("name");
150 newCondition->TestParam.push_back( PropertyManager->GetNode(prop_name) );
151 newCondition->TestValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
152 newCondition->Comparison.push_back(Script.GetValue("comparison"));
153 } else if (token == "set") {
154 prop_name = Script.GetValue("name");
155 newCondition->SetParam.push_back( PropertyManager->GetNode(prop_name) );
156 newCondition->SetValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
157 newCondition->Triggered.push_back(false);
158 newCondition->OriginalValue.push_back(0.0);
159 newCondition->newValue.push_back(0.0);
160 newCondition->StartTime.push_back(0.0);
161 newCondition->EndTime.push_back(0.0);
162 string tempCompare = Script.GetValue("type");
163 if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
164 else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
165 else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
166 else newCondition->Type.push_back((eType)0);
167 tempCompare = Script.GetValue("action");
168 if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
169 else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
170 else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
171 else newCondition->Action.push_back((eAction)0);
173 if (Script.GetValue("persistent") == "true")
174 newCondition->Persistent.push_back(true);
176 newCondition->Persistent.push_back(false);
178 newCondition->TC.push_back(strtod(Script.GetValue("tc").c_str(), NULL));
181 cerr << "Unrecognized keyword in script file: \" [when] " << token << "\"" << endl;
183 Script.GetNextConfigLine();
184 token = Script.GetValue();
186 Conditions.push_back(*newCondition);
187 Script.GetNextConfigLine();
188 token = Script.GetValue();
191 cerr << "Error reading script file: expected \"when\", got \"" << token << "\"" << endl;
195 } else if (token.empty()) {
198 cerr << "Unrecognized keyword in script file: \"" << token << "\" [runscript] " << endl;
202 if (aircraft == "") {
203 cerr << "Aircraft file not loaded in script" << endl;
210 FGInitialCondition *IC=FDMExec->GetIC();
211 if ( ! IC->Load( initialize )) {
212 cerr << "Initialization unsuccessful" << endl;
215 /* comment this out for conversion capability
216 FGTrim fgt(FDMExec, tFull);
217 if ( !fgt.DoTrim() ) {
218 cout << "Trim Failed" << endl;
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 bool FGScript::RunScript(void)
229 vector <struct condition>::iterator iC = Conditions.begin();
231 bool WholeTruth = false;
234 double currentTime = State->Getsim_time();
235 double newSetValue = 0;
237 if (currentTime > EndTime) return false;
239 while (iC < Conditions.end()) {
240 // determine whether the set of conditional tests for this condition equate
242 for (i=0; i<iC->TestValue.size(); i++) {
243 if (iC->Comparison[i] == "lt")
244 truth = iC->TestParam[i]->getDoubleValue() < iC->TestValue[i];
245 else if (iC->Comparison[i] == "le")
246 truth = iC->TestParam[i]->getDoubleValue() <= iC->TestValue[i];
247 else if (iC->Comparison[i] == "eq")
248 truth = iC->TestParam[i]->getDoubleValue() == iC->TestValue[i];
249 else if (iC->Comparison[i] == "ge")
250 truth = iC->TestParam[i]->getDoubleValue() >= iC->TestValue[i];
251 else if (iC->Comparison[i] == "gt")
252 truth = iC->TestParam[i]->getDoubleValue() > iC->TestValue[i];
253 else if (iC->Comparison[i] == "ne")
254 truth = iC->TestParam[i]->getDoubleValue() != iC->TestValue[i];
256 cerr << "Bad comparison" << endl;
258 if (i == 0) WholeTruth = truth;
259 else WholeTruth = WholeTruth && truth;
261 if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
264 // if the conditions are true, do the setting of the desired parameters
267 for (i=0; i<iC->SetValue.size(); i++) {
268 if ( ! iC->Triggered[i]) {
269 iC->OriginalValue[i] = iC->SetParam[i]->getDoubleValue();
270 switch (iC->Type[i]) {
272 iC->newValue[i] = iC->SetValue[i];
275 iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
278 iC->newValue[i] = iC->SetValue[i];
281 cerr << "Invalid Type specified" << endl;
284 iC->Triggered[i] = true;
285 iC->StartTime[i] = currentTime;
288 switch (iC->Action[i]) {
290 newSetValue = (currentTime - iC->StartTime[i])/(iC->TC[i])
291 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
292 if (newSetValue > iC->newValue[i]) newSetValue = iC->newValue[i];
295 newSetValue = iC->newValue[i];
298 newSetValue = (1 - exp(-(currentTime - iC->StartTime[i])/(iC->TC[i])))
299 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
302 cerr << "Invalid Action specified" << endl;
305 iC->SetParam[i]->setDoubleValue(newSetValue);
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 // The bitmasked value choices are as follows:
315 // unset: In this case (the default) JSBSim would only print
316 // out the normally expected messages, essentially echoing
317 // the config files as they are read. If the environment
318 // variable is not set, debug_lvl is set to 1 internally
319 // 0: This requests JSBSim not to output any messages
321 // 1: This value explicity requests the normal JSBSim
323 // 2: This value asks for a message to be printed out when
324 // a class is instantiated
325 // 4: When this value is set, a message is displayed when a
326 // FGModel object executes its Run() method
327 // 8: When this value is set, various runtime state variables
328 // are printed out periodically
329 // 16: When set various parameters are sanity checked and
330 // a message is printed out when they go out of bounds
332 void FGScript::Debug(int from)
336 if (debug_lvl <= 0) return;
338 if (debug_lvl & 1) { // Standard console startup message output
339 if (from == 0) { // Constructor
340 } else if (from == 3) {
341 } else if (from == 4) { // print out script data
342 vector <struct condition>::iterator iterConditions = Conditions.begin();
345 cout << "\n Script goes from " << StartTime << " to " << EndTime
346 << " with dt = " << State->Getdt() << endl << endl;
348 while (iterConditions < Conditions.end()) {
349 cout << " Condition: " << count++ << endl;
352 for (i=0; i<iterConditions->TestValue.size(); i++) {
353 if (i>0) cout << " and" << endl << " ";
354 cout << "(" << iterConditions->TestParam[i]->GetName()
355 << " " << iterConditions->Comparison[i] << " "
356 << iterConditions->TestValue[i] << ")";
360 for (i=0; i<iterConditions->SetValue.size(); i++) {
361 cout << endl << " set " << iterConditions->SetParam[i]->GetName()
362 << " to " << iterConditions->SetValue[i];
364 switch (iterConditions->Type[i]) {
366 cout << " (constant";
375 cout << " (unspecified type";
378 switch (iterConditions->Action[i]) {
386 cout << " via exponential approach";
389 cout << " via unspecified action";
392 if (!iterConditions->Persistent[i]) cout << endl
397 if (iterConditions->Action[i] == FG_RAMP ||
398 iterConditions->Action[i] == FG_EXP) cout << endl
399 << " with time constant "
400 << iterConditions->TC[i];
402 cout << ")" << endl << " }" << endl << endl;
410 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
411 if (from == 0) cout << "Instantiated: FGScript" << endl;
412 if (from == 1) cout << "Destroyed: FGScript" << endl;
414 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
416 if (debug_lvl & 8 ) { // Runtime state variables
418 if (debug_lvl & 16) { // Sanity checking
420 if (debug_lvl & 64) {
421 if (from == 0) { // Constructor
422 cout << IdSrc << endl;
423 cout << IdHdr << endl;