]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGScript.cpp
sync. with JSBSim v. 2.0
[flightgear.git] / src / FDM / JSBSim / input_output / FGScript.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGScript.cpp
4  Author:       Jon S. Berndt
5  Date started: 12/21/01
6  Purpose:      Loads and runs JSBSim scripts.
7
8  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
9
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
13  version.
14
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
18  details.
19
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.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29
30 This class wraps up the simulation scripting routines.
31
32 HISTORY
33 --------------------------------------------------------------------------------
34 12/21/01   JSB   Created
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES,  and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 INCLUDES
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43
44 #ifdef FGFS
45 #  include <simgear/compiler.h>
46 #  include STL_IOSTREAM
47 #  include STL_ITERATOR
48 #else
49 #  if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
50 #    include <iostream.h>
51 #  else
52 #    include <iostream>
53 #  endif
54 #  include <iterator>
55 #endif
56
57 #include "FGScript.h"
58 #include "FGXMLParse.h"
59 #include <initialization/FGTrim.h>
60
61 namespace JSBSim {
62
63 static const char *IdSrc = "$Id$";
64 static const char *IdHdr = ID_FGSCRIPT;
65
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 GLOBAL DECLARATIONS
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
69
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 CLASS IMPLEMENTATION
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73
74 // Constructor
75
76 FGScript::FGScript(FGFDMExec* fgex) : FDMExec(fgex)
77 {
78   State = FDMExec->GetState();
79   PropertyManager=FDMExec->GetPropertyManager();
80   Debug(0);
81 }
82
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84
85 FGScript::~FGScript()
86 {
87   Debug(1);
88 }
89
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91
92 bool FGScript::LoadScript( string script )
93 {
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;
97   bool result = false;
98   double dt = 0.0, value = 0.0;
99   FGXMLParse script_file_parser;
100   struct condition *newCondition;
101   ifstream script_file(script.c_str());
102
103   if ( !script_file ) return false;
104
105   readXML(script_file, script_file_parser);
106   document = script_file_parser.GetDocument();
107
108   if (document->GetName() != string("runscript")) {
109     cerr << "File: " << script << " is not a script file" << endl;
110     return false;
111   }
112
113   // read aircraft and initialization files
114
115   element = document->FindElement("use");
116   if (element) {
117     aircraft = element->GetAttributeValue("aircraft");
118     if (!aircraft.empty()) {
119       result = FDMExec->LoadModel(aircraft);
120       if (!result) return false;
121     } else {
122       cerr << "Aircraft must be specified first in script file." << endl;
123       return false;
124     }
125
126     element = document->FindNextElement("use");
127     initialize = element->GetAttributeValue("initialize");
128
129   } else {
130     cerr << "No \"use\" directives in the script file." << endl;
131     return false;
132   }
133
134   run_element = document->FindElement("run");
135
136   if (!run_element) {
137     cerr << "No \"run\" element found in script." << endl;
138     return false;
139   }
140
141   // Set sim timing
142
143   StartTime = run_element->GetAttributeValueAsNumber("start");
144   State->Setsim_time(StartTime);
145   EndTime   = run_element->GetAttributeValueAsNumber("end");
146   dt        = run_element->GetAttributeValueAsNumber("dt");
147   State->Setdt(dt);
148
149   // read "when" tests from script
150
151   when_element = run_element->FindElement("when");
152   while (when_element) { // "when" processing
153     newCondition = new struct condition();
154
155     // read parameters
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");
165     }
166
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
189
190       if (set_element->GetAttributeValue("persistent") == "true")
191         newCondition->Persistent.push_back(true);
192       else
193         newCondition->Persistent.push_back(false); // DEFAULT
194
195       if (!set_element->GetAttributeValue("tc").empty())
196         newCondition->TC.push_back(set_element->GetAttributeValueAsNumber("tc"));
197       else
198         newCondition->TC.push_back(1.0); // DEFAULT
199
200       set_element = when_element->FindNextElement("set");
201     }
202     Conditions.push_back(*newCondition);
203     when_element = run_element->FindNextElement("when");
204   }
205
206   Debug(4);
207
208   FGInitialCondition *IC=FDMExec->GetIC();
209   if ( ! IC->Load( initialize )) {
210     cerr << "Initialization unsuccessful" << endl;
211     exit(-1);
212   }
213
214   return true;
215 }
216
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218
219 bool FGScript::RunScript(void)
220 {
221   vector <struct condition>::iterator iC = Conditions.begin();
222   bool truth = false;
223   bool WholeTruth = false;
224   unsigned i;
225
226   double currentTime = State->Getsim_time();
227   double newSetValue = 0;
228
229   if (currentTime > EndTime) return false;
230
231   while (iC < Conditions.end()) {
232     // determine whether the set of conditional tests for this condition equate
233     // to true
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];
247       else
248               cerr << "Bad comparison" << endl;
249
250       if (i == 0) WholeTruth = truth;
251       else        WholeTruth = WholeTruth && truth;
252
253       if (!truth && iC->Persistent[i] && iC->Triggered[i]) iC->Triggered[i] = false;
254     }
255
256     // if the conditions are true, do the setting of the desired parameters
257
258     if (WholeTruth) {
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]) {
263           case FG_VALUE:
264             iC->newValue[i] = iC->SetValue[i];
265             break;
266           case FG_DELTA:
267             iC->newValue[i] = iC->OriginalValue[i] + iC->SetValue[i];
268             break;
269           case FG_BOOL:
270             iC->newValue[i] = iC->SetValue[i];
271             break;
272           default:
273             cerr << "Invalid Type specified" << endl;
274             break;
275           }
276           iC->Triggered[i] = true;
277           iC->StartTime[i] = currentTime;
278         }
279
280         double time_span = currentTime - iC->StartTime[i];
281         double value_span = iC->newValue[i] - iC->OriginalValue[i];
282
283         switch (iC->Action[i]) {
284         case FG_RAMP:
285           if (time_span <= iC->TC[i])
286             newSetValue = time_span/iC->TC[i] * value_span + iC->OriginalValue[i];
287           else
288             newSetValue = iC->newValue[i];
289           break;
290         case FG_STEP:
291           newSetValue = iC->newValue[i];
292           break;
293         case FG_EXP:
294           newSetValue = (1 - exp( -time_span/iC->TC[i] )) * value_span + iC->OriginalValue[i];
295           break;
296         default:
297           cerr << "Invalid Action specified" << endl;
298           break;
299         }
300         iC->SetParam[i]->setDoubleValue(newSetValue);
301       }
302     }
303     iC++;
304   }
305   return true;
306 }
307
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
315 //       whatsoever.
316 //    1: This value explicity requests the normal JSBSim
317 //       startup messages
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
326
327 void FGScript::Debug(int from)
328 {
329   unsigned int i;
330
331   if (debug_lvl <= 0) return;
332
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();
338       int count=0;
339
340       cout << "\n  Script goes from " << StartTime << " to " << EndTime
341            << " with dt = " << State->Getdt() << endl << endl;
342
343       while (iterConditions < Conditions.end()) {
344         cout << "  Condition: " << count++ << endl;
345         cout << "    if (";
346
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] << ")";
352         }
353         cout << ") then {";
354
355         for (i=0; i<iterConditions->SetValue.size(); i++) {
356           cout << endl << "      set " << iterConditions->SetParam[i]->GetName()
357                << " to " << iterConditions->SetValue[i];
358
359           switch (iterConditions->Type[i]) {
360           case FG_VALUE:
361             cout << " (constant";
362             break;
363           case FG_DELTA:
364             cout << " (delta";
365             break;
366           case FG_BOOL:
367             cout << " (boolean";
368             break;
369           default:
370             cout << " (unspecified type";
371           }
372
373           switch (iterConditions->Action[i]) {
374           case FG_RAMP:
375             cout << " via ramp";
376             break;
377           case FG_STEP:
378             cout << " via step";
379             break;
380           case FG_EXP:
381             cout << " via exponential approach";
382             break;
383           default:
384             cout << " via unspecified action";
385           }
386
387           if (!iterConditions->Persistent[i]) cout << endl
388                              << "                              once";
389           else cout << endl
390                              << "                              repeatedly";
391
392           if (iterConditions->Action[i] == FG_RAMP ||
393               iterConditions->Action[i] == FG_EXP) cout << endl
394                              << "                              with time constant "
395                              << iterConditions->TC[i];
396         }
397         cout << ")" << endl << "    }" << endl << endl;
398
399         iterConditions++;
400       }
401
402       cout << endl;
403     }
404   }
405   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
406     if (from == 0) cout << "Instantiated: FGScript" << endl;
407     if (from == 1) cout << "Destroyed:    FGScript" << endl;
408   }
409   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
410   }
411   if (debug_lvl & 8 ) { // Runtime state variables
412   }
413   if (debug_lvl & 16) { // Sanity checking
414   }
415   if (debug_lvl & 64) {
416     if (from == 0) { // Constructor
417       cout << IdSrc << endl;
418       cout << IdHdr << endl;
419     }
420   }
421 }
422 }