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 "FGXMLParse.h"
59 #include <initialization/FGTrim.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 string aircraft="", initialize="", comparison = "", prop_name="";
95 Element *document=0, *element=0, *run_element=0, *when_element=0;
96 Element *parameter_element=0, *set_element=0;
98 double dt = 0.0, value = 0.0;
99 FGXMLParse script_file_parser;
100 struct condition *newCondition;
101 ifstream script_file(script.c_str());
103 if ( !script_file ) return false;
105 readXML(script_file, script_file_parser);
106 document = script_file_parser.GetDocument();
108 if (document->GetName() != string("runscript")) {
109 cerr << "File: " << script << " is not a script file" << endl;
113 // read aircraft and initialization files
115 element = document->FindElement("use");
117 aircraft = element->GetAttributeValue("aircraft");
118 if (!aircraft.empty()) {
119 result = FDMExec->LoadModel(aircraft);
120 if (!result) return false;
122 cerr << "Aircraft must be specified first in script file." << endl;
126 element = document->FindNextElement("use");
127 initialize = element->GetAttributeValue("initialize");
130 cerr << "No \"use\" directives in the script file." << endl;
134 run_element = document->FindElement("run");
137 cerr << "No \"run\" element found in script." << endl;
143 StartTime = run_element->GetAttributeValueAsNumber("start");
144 State->Setsim_time(StartTime);
145 EndTime = run_element->GetAttributeValueAsNumber("end");
146 dt = run_element->GetAttributeValueAsNumber("dt");
149 // read "when" tests from script
151 when_element = run_element->FindElement("when");
152 while (when_element) { // "when" processing
153 newCondition = new struct condition();
156 parameter_element = when_element->FindElement("parameter");
157 while (parameter_element) {
158 prop_name = parameter_element->GetAttributeValue("name");
159 newCondition->TestParam.push_back( PropertyManager->GetNode(prop_name) );
160 value = parameter_element->GetAttributeValueAsNumber("value");
161 newCondition->TestValue.push_back(value);
162 comparison = parameter_element->GetAttributeValue("comparison");
163 newCondition->Comparison.push_back(comparison);
164 parameter_element = when_element->FindNextElement("parameter");
167 // read set definitions
168 set_element = when_element->FindElement("set");
169 while (set_element) {
170 prop_name = set_element->GetAttributeValue("name");
171 newCondition->SetParam.push_back( PropertyManager->GetNode(prop_name) );
172 value = set_element->GetAttributeValueAsNumber("value");
173 newCondition->SetValue.push_back(value);
174 newCondition->Triggered.push_back(false);
175 newCondition->OriginalValue.push_back(0.0);
176 newCondition->newValue.push_back(0.0);
177 newCondition->StartTime.push_back(0.0);
178 newCondition->EndTime.push_back(0.0);
179 string tempCompare = set_element->GetAttributeValue("type");
180 if (tempCompare == "FG_DELTA") newCondition->Type.push_back(FG_DELTA);
181 else if (tempCompare == "FG_BOOL") newCondition->Type.push_back(FG_BOOL);
182 else if (tempCompare == "FG_VALUE") newCondition->Type.push_back(FG_VALUE);
183 else newCondition->Type.push_back(FG_VALUE); // DEFAULT
184 tempCompare = set_element->GetAttributeValue("action");
185 if (tempCompare == "FG_RAMP") newCondition->Action.push_back(FG_RAMP);
186 else if (tempCompare == "FG_STEP") newCondition->Action.push_back(FG_STEP);
187 else if (tempCompare == "FG_EXP") newCondition->Action.push_back(FG_EXP);
188 else newCondition->Action.push_back(FG_STEP); // DEFAULT
190 if (set_element->GetAttributeValue("persistent") == "true")
191 newCondition->Persistent.push_back(true);
193 newCondition->Persistent.push_back(false); // DEFAULT
195 if (!set_element->GetAttributeValue("tc").empty())
196 newCondition->TC.push_back(set_element->GetAttributeValueAsNumber("tc"));
198 newCondition->TC.push_back(1.0); // DEFAULT
200 set_element = when_element->FindNextElement("set");
202 Conditions.push_back(*newCondition);
203 when_element = run_element->FindNextElement("when");
208 FGInitialCondition *IC=FDMExec->GetIC();
209 if ( ! IC->Load( initialize )) {
210 cerr << "Initialization unsuccessful" << endl;
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 bool FGScript::RunScript(void)
221 vector <struct condition>::iterator iC = Conditions.begin();
223 bool WholeTruth = false;
226 double currentTime = State->Getsim_time();
227 double newSetValue = 0;
229 if (currentTime > EndTime) return false;
231 while (iC < Conditions.end()) {
232 // determine whether the set of conditional tests for this condition equate
234 for (i=0; i<iC->TestValue.size(); i++) {
235 if (iC->Comparison[i] == "lt")
236 truth = iC->TestParam[i]->getDoubleValue() < iC->TestValue[i];
237 else if (iC->Comparison[i] == "le")
238 truth = iC->TestParam[i]->getDoubleValue() <= iC->TestValue[i];
239 else if (iC->Comparison[i] == "eq")
240 truth = iC->TestParam[i]->getDoubleValue() == iC->TestValue[i];
241 else if (iC->Comparison[i] == "ge")
242 truth = iC->TestParam[i]->getDoubleValue() >= iC->TestValue[i];
243 else if (iC->Comparison[i] == "gt")
244 truth = iC->TestParam[i]->getDoubleValue() > iC->TestValue[i];
245 else if (iC->Comparison[i] == "ne")
246 truth = iC->TestParam[i]->getDoubleValue() != iC->TestValue[i];
248 cerr << "Bad comparison" << endl;
250 if (i == 0) WholeTruth = truth;
251 else WholeTruth = WholeTruth && truth;
253 if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
256 // if the conditions are true, do the setting of the desired parameters
259 for (i=0; i<iC->SetValue.size(); i++) {
260 if ( ! iC->Triggered[i]) {
261 iC->OriginalValue[i] = iC->SetParam[i]->getDoubleValue();
262 switch (iC->Type[i]) {
264 iC->newValue[i] = iC->SetValue[i];
267 iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
270 iC->newValue[i] = iC->SetValue[i];
273 cerr << "Invalid Type specified" << endl;
276 iC->Triggered[i] = true;
277 iC->StartTime[i] = currentTime;
280 double time_span = currentTime - iC->StartTime[i];
281 double value_span = iC->newValue[i] - iC->OriginalValue[i];
283 switch (iC->Action[i]) {
285 if (time_span <= iC->TC[i])
286 newSetValue = time_span/iC->TC[i] * value_span + iC->OriginalValue[i];
288 newSetValue = iC->newValue[i];
291 newSetValue = iC->newValue[i];
294 newSetValue = (1 - exp( -time_span/iC->TC[i] )) * value_span + iC->OriginalValue[i];
297 cerr << "Invalid Action specified" << endl;
300 iC->SetParam[i]->setDoubleValue(newSetValue);
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 // The bitmasked value choices are as follows:
310 // unset: In this case (the default) JSBSim would only print
311 // out the normally expected messages, essentially echoing
312 // the config files as they are read. If the environment
313 // variable is not set, debug_lvl is set to 1 internally
314 // 0: This requests JSBSim not to output any messages
316 // 1: This value explicity requests the normal JSBSim
318 // 2: This value asks for a message to be printed out when
319 // a class is instantiated
320 // 4: When this value is set, a message is displayed when a
321 // FGModel object executes its Run() method
322 // 8: When this value is set, various runtime state variables
323 // are printed out periodically
324 // 16: When set various parameters are sanity checked and
325 // a message is printed out when they go out of bounds
327 void FGScript::Debug(int from)
331 if (debug_lvl <= 0) return;
333 if (debug_lvl & 1) { // Standard console startup message output
334 if (from == 0) { // Constructor
335 } else if (from == 3) {
336 } else if (from == 4) { // print out script data
337 vector <struct condition>::iterator iterConditions = Conditions.begin();
340 cout << "\n Script goes from " << StartTime << " to " << EndTime
341 << " with dt = " << State->Getdt() << endl << endl;
343 while (iterConditions < Conditions.end()) {
344 cout << " Condition: " << count++ << endl;
347 for (i=0; i<iterConditions->TestValue.size(); i++) {
348 if (i>0) cout << " and" << endl << " ";
349 cout << "(" << iterConditions->TestParam[i]->GetName()
350 << " " << iterConditions->Comparison[i] << " "
351 << iterConditions->TestValue[i] << ")";
355 for (i=0; i<iterConditions->SetValue.size(); i++) {
356 cout << endl << " set " << iterConditions->SetParam[i]->GetName()
357 << " to " << iterConditions->SetValue[i];
359 switch (iterConditions->Type[i]) {
361 cout << " (constant";
370 cout << " (unspecified type";
373 switch (iterConditions->Action[i]) {
381 cout << " via exponential approach";
384 cout << " via unspecified action";
387 if (!iterConditions->Persistent[i]) cout << endl
392 if (iterConditions->Action[i] == FG_RAMP ||
393 iterConditions->Action[i] == FG_EXP) cout << endl
394 << " with time constant "
395 << iterConditions->TC[i];
397 cout << ")" << endl << " }" << endl << endl;
405 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
406 if (from == 0) cout << "Instantiated: FGScript" << endl;
407 if (from == 1) cout << "Destroyed: FGScript" << endl;
409 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
411 if (debug_lvl & 8 ) { // Runtime state variables
413 if (debug_lvl & 16) { // Sanity checking
415 if (debug_lvl & 64) {
416 if (from == 0) { // Constructor
417 cout << IdSrc << endl;
418 cout << IdHdr << endl;