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__)
50 # include <iostream.h>
58 #include "FGConfigFile.h"
60 static const char *IdSrc = "$Id$";
61 static const char *IdHdr = ID_FGSCRIPT;
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73 FGScript::FGScript(FGFDMExec* fgex) : FDMExec(fgex)
75 State = FDMExec->GetState();
76 PropertyManager=FDMExec->GetPropertyManager();
80 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 bool FGScript::LoadScript(string script)
91 FGConfigFile Script(script);
98 struct condition *newCondition;
100 if (!Script.IsOpen()) return false;
102 Script.GetNextConfigLine();
103 if (Script.GetValue("runscript").length() <= 0) {
104 cerr << "File: " << script << " is not a script file" << endl;
108 ScriptName = Script.GetValue("name");
111 if (debug_lvl > 0) cout << "Reading Script File " << ScriptName << endl;
113 while (Script.GetNextConfigLine() != string("EOF") && Script.GetValue() != string("/runscript")) {
114 token = Script.GetValue();
115 if (token == "use") {
116 if ((token = Script.GetValue("aircraft")) != string("")) {
118 result = FDMExec->LoadModel("aircraft", "engine", aircraft);
120 cerr << "Aircraft file " << aircraft << " was not found" << endl;
123 if (debug_lvl > 0) cout << " Use aircraft: " << token << endl;
124 } else if ((token = Script.GetValue("initialize")) != string("")) {
126 if (debug_lvl > 0) cout << " Use reset file: " << token << endl;
128 cerr << "Unknown 'use' keyword: \"" << token << "\"" << endl;
130 } else if (token == "run") {
131 StartTime = strtod(Script.GetValue("start").c_str(), NULL);
132 State->Setsim_time(StartTime);
133 EndTime = strtod(Script.GetValue("end").c_str(), NULL);
134 dt = strtod(Script.GetValue("dt").c_str(), NULL);
136 Script.GetNextConfigLine();
137 token = Script.GetValue();
138 while (token != string("/run")) {
140 if (token == "when") {
141 Script.GetNextConfigLine();
142 token = Script.GetValue();
143 newCondition = new struct condition();
144 while (token != string("/when")) {
145 if (token == "parameter") {
146 prop_name = State->GetPropertyName( Script.GetValue("name") );
147 newCondition->TestParam.push_back( PropertyManager->GetNode(prop_name) );
148 newCondition->TestValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
149 newCondition->Comparison.push_back(Script.GetValue("comparison"));
150 } else if (token == "set") {
151 prop_name = State->GetPropertyName( Script.GetValue("name") );
152 newCondition->SetParam.push_back( PropertyManager->GetNode(prop_name) );
153 newCondition->SetValue.push_back(strtod(Script.GetValue("value").c_str(), NULL));
154 newCondition->Triggered.push_back(false);
155 newCondition->OriginalValue.push_back(0.0);
156 newCondition->newValue.push_back(0.0);
157 newCondition->StartTime.push_back(0.0);
158 newCondition->EndTime.push_back(0.0);
159 string tempCompare = Script.GetValue("type");
160 if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
161 else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
162 else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
163 else newCondition->Type.push_back((eType)0);
164 tempCompare = Script.GetValue("action");
165 if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
166 else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
167 else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
168 else newCondition->Action.push_back((eAction)0);
170 if (Script.GetValue("persistent") == "true")
171 newCondition->Persistent.push_back(true);
173 newCondition->Persistent.push_back(false);
175 newCondition->TC.push_back(strtod(Script.GetValue("tc").c_str(), NULL));
178 cerr << "Unrecognized keyword in script file: \" [when] " << token << "\"" << endl;
180 Script.GetNextConfigLine();
181 token = Script.GetValue();
183 Conditions.push_back(*newCondition);
184 Script.GetNextConfigLine();
185 token = Script.GetValue();
188 cerr << "Error reading script file: expected \"when\", got \"" << token << "\"" << endl;
192 } else if (token.empty()) {
195 cerr << "Unrecognized keyword in script file: \"" << token << "\" [runscript] " << endl;
199 if (aircraft == "") {
200 cerr << "Aircraft file not loaded in script" << endl;
207 FGInitialCondition IC(FDMExec);
208 if ( ! IC.Load("aircraft", aircraft, initialize)) {
209 cerr << "Initialization unsuccessful" << endl;
216 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 bool FGScript::RunScript(void)
220 vector <struct condition>::iterator iC = Conditions.begin();
222 bool WholeTruth = false;
225 double currentTime = State->Getsim_time();
226 double newSetValue = 0;
228 if (currentTime > EndTime) return false;
230 while (iC < Conditions.end()) {
231 // determine whether the set of conditional tests for this condition equate
233 for (i=0; i<iC->TestValue.size(); i++) {
234 if (iC->Comparison[i] == "lt")
235 truth = iC->TestParam[i]->getDoubleValue() < iC->TestValue[i];
236 else if (iC->Comparison[i] == "le")
237 truth = iC->TestParam[i]->getDoubleValue() <= iC->TestValue[i];
238 else if (iC->Comparison[i] == "eq")
239 truth = iC->TestParam[i]->getDoubleValue() == iC->TestValue[i];
240 else if (iC->Comparison[i] == "ge")
241 truth = iC->TestParam[i]->getDoubleValue() >= iC->TestValue[i];
242 else if (iC->Comparison[i] == "gt")
243 truth = iC->TestParam[i]->getDoubleValue() > iC->TestValue[i];
244 else if (iC->Comparison[i] == "ne")
245 truth = iC->TestParam[i]->getDoubleValue() != iC->TestValue[i];
247 cerr << "Bad comparison" << endl;
249 if (i == 0) WholeTruth = truth;
250 else WholeTruth = WholeTruth && truth;
252 if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
255 // if the conditions are true, do the setting of the desired parameters
258 for (i=0; i<iC->SetValue.size(); i++) {
259 if ( ! iC->Triggered[i]) {
260 iC->OriginalValue[i] = iC->SetParam[i]->getDoubleValue();
261 switch (iC->Type[i]) {
263 iC->newValue[i] = iC->SetValue[i];
266 iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
269 iC->newValue[i] = iC->SetValue[i];
272 cerr << "Invalid Type specified" << endl;
275 iC->Triggered[i] = true;
276 iC->StartTime[i] = currentTime;
279 switch (iC->Action[i]) {
281 newSetValue = (currentTime - iC->StartTime[i])/(iC->TC[i])
282 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
283 if (newSetValue > iC->newValue[i]) newSetValue = iC->newValue[i];
286 newSetValue = iC->newValue[i];
289 newSetValue = (1 - exp(-(currentTime - iC->StartTime[i])/(iC->TC[i])))
290 * (iC->newValue[i] - iC->OriginalValue[i]) + iC->OriginalValue[i];
293 cerr << "Invalid Action specified" << endl;
296 iC->SetParam[i]->setDoubleValue(newSetValue);
304 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 // The bitmasked value choices are as follows:
306 // unset: In this case (the default) JSBSim would only print
307 // out the normally expected messages, essentially echoing
308 // the config files as they are read. If the environment
309 // variable is not set, debug_lvl is set to 1 internally
310 // 0: This requests JSBSim not to output any messages
312 // 1: This value explicity requests the normal JSBSim
314 // 2: This value asks for a message to be printed out when
315 // a class is instantiated
316 // 4: When this value is set, a message is displayed when a
317 // FGModel object executes its Run() method
318 // 8: When this value is set, various runtime state variables
319 // are printed out periodically
320 // 16: When set various parameters are sanity checked and
321 // a message is printed out when they go out of bounds
323 void FGScript::Debug(int from)
327 if (debug_lvl <= 0) return;
329 if (debug_lvl & 1) { // Standard console startup message output
330 if (from == 0) { // Constructor
331 } else if (from == 3) {
332 } else if (from == 4) { // print out script data
333 vector <struct condition>::iterator iterConditions = Conditions.begin();
336 cout << "\n Script goes from " << StartTime << " to " << EndTime
337 << " with dt = " << State->Getdt() << endl << endl;
339 while (iterConditions < Conditions.end()) {
340 cout << " Condition: " << count++ << endl;
343 for (i=0; i<iterConditions->TestValue.size(); i++) {
344 if (i>0) cout << " and" << endl << " ";
345 cout << "(" << iterConditions->TestParam[i]->GetName()
346 << " " << iterConditions->Comparison[i] << " "
347 << iterConditions->TestValue[i] << ")";
351 for (i=0; i<iterConditions->SetValue.size(); i++) {
352 cout << endl << " set " << iterConditions->SetParam[i]->GetName()
353 << " to " << iterConditions->SetValue[i];
355 switch (iterConditions->Type[i]) {
357 cout << " (constant";
366 cout << " (unspecified type";
369 switch (iterConditions->Action[i]) {
377 cout << " via exponential approach";
380 cout << " via unspecified action";
383 if (!iterConditions->Persistent[i]) cout << endl
388 if (iterConditions->Action[i] == FG_RAMP ||
389 iterConditions->Action[i] == FG_EXP) cout << endl
390 << " with time constant "
391 << iterConditions->TC[i];
393 cout << ")" << endl << " }" << endl << endl;
401 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
402 if (from == 0) cout << "Instantiated: FGScript" << endl;
403 if (from == 1) cout << "Destroyed: FGScript" << endl;
405 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
407 if (debug_lvl & 8 ) { // Runtime state variables
409 if (debug_lvl & 16) { // Sanity checking
411 if (debug_lvl & 64) {
412 if (from == 0) { // Constructor
413 cout << IdSrc << endl;
414 cout << IdHdr << endl;