]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGScript.cpp
Sync w. JSBSim CVS (merge from PRE_OSG_PLIB_20061029 branch)
[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 Lesser 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 Lesser General Public License for more
18  details.
19
20  You should have received a copy of the GNU Lesser 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 Lesser 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 <input_output/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   unsigned int i;
88   for (i=0; i<local_properties.size(); i++)
89     PropertyManager->Untie(local_properties[i]->title);
90   
91   for (i=0; i<local_properties.size(); i++)
92     delete local_properties[i];
93
94   local_properties.clear();
95   Debug(1);
96 }
97
98 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99
100 bool FGScript::LoadScript( string script )
101 {
102   string aircraft="", initialize="", comparison = "", prop_name="";
103   string notifyPropertyName="";
104   Element *element=0, *run_element=0, *event_element=0;
105   Element *condition_element=0, *set_element=0, *delay_element=0;
106   Element *notify_element = 0L, *notify_property_element = 0L;
107   Element *property_element = 0L;
108   bool result = false;
109   double dt = 0.0, value = 0.0;
110   struct event *newEvent;
111   FGCondition *newCondition;
112
113   document = LoadXMLDocument(script);
114
115   if (document->GetName() != string("runscript")) {
116     cerr << "File: " << script << " is not a script file" << endl;
117     return false;
118   }
119
120   ScriptName = document->GetAttributeValue("name");
121
122  // First, find "run" element and set delta T
123
124   run_element = document->FindElement("run");
125
126   if (!run_element) {
127     cerr << "No \"run\" element found in script." << endl;
128     return false;
129   }
130
131   // Set sim timing
132
133   StartTime = run_element->GetAttributeValueAsNumber("start");
134   State->Setsim_time(StartTime);
135   EndTime   = run_element->GetAttributeValueAsNumber("end");
136   dt        = run_element->GetAttributeValueAsNumber("dt");
137   State->Setdt(dt);
138
139   // read aircraft and initialization files
140
141   element = document->FindElement("use");
142   if (element) {
143     aircraft = element->GetAttributeValue("aircraft");
144     if (!aircraft.empty()) {
145       result = FDMExec->LoadModel(aircraft);
146       if (!result) return false;
147     } else {
148       cerr << "Aircraft must be specified in use element." << endl;
149       return false;
150     }
151
152     initialize = element->GetAttributeValue("initialize");
153     if (initialize.empty()) {
154       cerr << "Initialization file must be specified in use element." << endl;
155       return false;
156     }
157
158   } else {
159     cerr << "No \"use\" directives in the script file." << endl;
160     return false;
161   }
162
163   // Read local property declarations
164   property_element = run_element->FindElement("property");
165   while (property_element) {
166     LocalProps *localProp = new LocalProps();
167     localProp->title = property_element->GetDataLine();
168     local_properties.push_back(localProp);
169     PropertyManager->Tie(localProp->title, (local_properties.back())->value);
170     property_element = run_element->FindNextElement("property");
171   }
172
173   // Read "events" from script
174
175   event_element = run_element->FindElement("event");
176   while (event_element) { // event processing
177
178     // Create the event structure
179     newEvent = new struct event();
180
181     // Retrieve the event name if given
182     newEvent->Name = event_element->GetAttributeValue("name");
183
184     // Is this event persistent? That is, does it execute repeatedly as long as the
185     // condition is true, or does it execute as a one-shot event, only?
186     if (event_element->GetAttributeValue("persistent") == string("true")) {
187       newEvent->Persistent = true;
188     }
189
190     // Process the conditions
191     condition_element = event_element->FindElement("condition");
192     if (condition_element != 0) {
193       newCondition = new FGCondition(condition_element, PropertyManager);
194       newEvent->Condition = newCondition;
195     } else {
196       cerr << "No condition specified in script event " << newEvent->Name << endl;
197       return false;
198     }
199
200     // Is there a delay between the time this event is triggered, and when the event
201     // actions are executed?
202
203     delay_element = event_element->FindElement("delay");
204     if (delay_element) newEvent->Delay = event_element->FindElementValueAsNumber("delay");
205     else newEvent->Delay = 0.0;
206
207     // Notify about when this event is triggered?
208     if ((notify_element = event_element->FindElement("notify")) != 0) {
209       newEvent->Notify = true;
210       notify_property_element = notify_element->FindElement("property");
211       while (notify_property_element) {
212         notifyPropertyName = notify_property_element->GetDataLine();
213         newEvent->NotifyProperties.push_back( PropertyManager->GetNode(notifyPropertyName) );
214         notify_property_element = notify_element->FindNextElement("property");
215       }
216     }
217
218     // Read set definitions (these define the actions to be taken when the event is triggered).
219     set_element = event_element->FindElement("set");
220     while (set_element) {
221       prop_name = set_element->GetAttributeValue("name");
222       newEvent->SetParam.push_back( PropertyManager->GetNode(prop_name) );
223       value = set_element->GetAttributeValueAsNumber("value");
224       newEvent->SetValue.push_back(value);
225       newEvent->OriginalValue.push_back(0.0);
226       newEvent->newValue.push_back(0.0);
227       newEvent->ValueSpan.push_back(0.0);
228       string tempCompare = set_element->GetAttributeValue("type");
229       if      (tempCompare == "FG_DELTA") newEvent->Type.push_back(FG_DELTA);
230       else if (tempCompare == "FG_BOOL")  newEvent->Type.push_back(FG_BOOL);
231       else if (tempCompare == "FG_VALUE") newEvent->Type.push_back(FG_VALUE);
232       else                                newEvent->Type.push_back(FG_VALUE); // DEFAULT
233       tempCompare = set_element->GetAttributeValue("action");
234       if      (tempCompare == "FG_RAMP") newEvent->Action.push_back(FG_RAMP);
235       else if (tempCompare == "FG_STEP") newEvent->Action.push_back(FG_STEP);
236       else if (tempCompare == "FG_EXP")  newEvent->Action.push_back(FG_EXP);
237       else                               newEvent->Action.push_back(FG_STEP); // DEFAULT
238
239       if (!set_element->GetAttributeValue("tc").empty())
240         newEvent->TC.push_back(set_element->GetAttributeValueAsNumber("tc"));
241       else
242         newEvent->TC.push_back(1.0); // DEFAULT
243
244       newEvent->Transiting.push_back(false);
245
246       set_element = event_element->FindNextElement("set");
247     }
248     Events.push_back(*newEvent);
249     event_element = run_element->FindNextElement("event");
250   }
251
252   Debug(4);
253
254   FGInitialCondition *IC=FDMExec->GetIC();
255   if ( ! IC->Load( initialize )) {
256     cerr << "Initialization unsuccessful" << endl;
257     exit(-1);
258   }
259
260   return true;
261 }
262
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264
265 bool FGScript::RunScript(void)
266 {
267   vector <struct event>::iterator iEvent = Events.begin();
268   unsigned i, j;
269   unsigned event_ctr = 0;
270
271   double currentTime = State->Getsim_time();
272   double newSetValue = 0;
273
274   if (currentTime > EndTime) return false; //Script done!
275
276   // Iterate over all events.
277   while (iEvent < Events.end()) {
278     iEvent->PrevTriggered = iEvent->Triggered;
279     // Determine whether the set of conditional tests for this condition equate
280     // to true and should cause the event to execute.
281     if (iEvent->Condition->Evaluate()) {
282       if (!iEvent->Triggered) {
283
284         // The conditions are true, do the setting of the desired Event parameters
285         for (i=0; i<iEvent->SetValue.size(); i++) {
286           iEvent->OriginalValue[i] = iEvent->SetParam[i]->getDoubleValue();
287           switch (iEvent->Type[i]) {
288           case FG_VALUE:
289           case FG_BOOL:
290             iEvent->newValue[i] = iEvent->SetValue[i];
291             break;
292           case FG_DELTA:
293             iEvent->newValue[i] = iEvent->OriginalValue[i] + iEvent->SetValue[i];
294             break;
295           default:
296             cerr << "Invalid Type specified" << endl;
297             break;
298           }
299           iEvent->StartTime = currentTime + iEvent->Delay;
300           iEvent->ValueSpan[i] = iEvent->newValue[i] - iEvent->OriginalValue[i];
301           iEvent->Transiting[i] = true;
302         }
303       }
304       iEvent->Triggered = true;
305     } else if (iEvent->Persistent) {
306       iEvent->Triggered = false; // Reset the trigger for persistent events
307       iEvent->Notified = false;  // Also reset the notification flag
308     }
309
310     if ((currentTime >= iEvent->StartTime) && iEvent->Triggered) {
311
312       for (i=0; i<iEvent->SetValue.size(); i++) {
313         if (iEvent->Transiting[i]) {
314           iEvent->TimeSpan = currentTime - iEvent->StartTime;
315           switch (iEvent->Action[i]) {
316           case FG_RAMP:
317             if (iEvent->TimeSpan <= iEvent->TC[i]) {
318               newSetValue = iEvent->TimeSpan/iEvent->TC[i] * iEvent->ValueSpan[i] + iEvent->OriginalValue[i];
319             } else {
320               newSetValue = iEvent->newValue[i];
321               iEvent->Transiting[i] = false;
322             }
323             break;
324           case FG_STEP:
325             newSetValue = iEvent->newValue[i];
326             iEvent->Transiting[i] = false;
327             break;
328           case FG_EXP:
329             newSetValue = (1 - exp( -iEvent->TimeSpan/iEvent->TC[i] )) * iEvent->ValueSpan[i] + iEvent->OriginalValue[i];
330             break;
331           default:
332             cerr << "Invalid Action specified" << endl;
333             break;
334           }
335           iEvent->SetParam[i]->setDoubleValue(newSetValue);
336         }
337       }
338
339       // Print notification values after setting them
340       if (iEvent->Notify && !iEvent->Notified) {
341         cout << endl << "  Event " << event_ctr << " (" << iEvent->Name << ")"
342              << " executed at time: " << currentTime << endl;
343         for (j=0; j<iEvent->NotifyProperties.size();j++) {
344           cout << "    " << iEvent->NotifyProperties[j]->GetName()
345                << " = " << iEvent->NotifyProperties[j]->getDoubleValue() << endl;
346         }
347         cout << endl;
348         iEvent->Notified = true;
349       }
350
351     }
352
353     iEvent++;
354     event_ctr++;
355   }
356   return true;
357 }
358
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 //    The bitmasked value choices are as follows:
361 //    unset: In this case (the default) JSBSim would only print
362 //       out the normally expected messages, essentially echoing
363 //       the config files as they are read. If the environment
364 //       variable is not set, debug_lvl is set to 1 internally
365 //    0: This requests JSBSim not to output any messages
366 //       whatsoever.
367 //    1: This value explicity requests the normal JSBSim
368 //       startup messages
369 //    2: This value asks for a message to be printed out when
370 //       a class is instantiated
371 //    4: When this value is set, a message is displayed when a
372 //       FGModel object executes its Run() method
373 //    8: When this value is set, various runtime state variables
374 //       are printed out periodically
375 //    16: When set various parameters are sanity checked and
376 //       a message is printed out when they go out of bounds
377
378 void FGScript::Debug(int from)
379 {
380   if (debug_lvl <= 0) return;
381
382   if (debug_lvl & 1) { // Standard console startup message output
383     if (from == 0) { // Constructor
384     } else if (from == 3) {
385     } else if (from == 4)  { // print out script data
386       cout << endl;
387       cout << "Script: \"" << ScriptName << "\"" << endl;
388       cout << "  begins at " << StartTime << " seconds and runs to " << EndTime
389            << " seconds with dt = " << State->Getdt() << endl;
390       cout << endl;
391
392       for (unsigned i=0; i<Events.size(); i++) {
393         cout << "Event " << i;
394         if (!Events[i].Name.empty()) cout << " (" << Events[i].Name << ")";
395         cout << ":" << endl;
396
397         if (Events[i].Persistent)
398           cout << "  " << "Always executes";
399         else
400           cout << "  " << "Executes once";
401
402         Events[i].Condition->PrintCondition();
403
404         cout << endl << "  Actions taken:" << endl << "    {";
405         for (unsigned j=0; j<Events[i].SetValue.size(); j++) {
406           cout << endl << "      set " << Events[i].SetParam[j]->GetName()
407                << " to " << Events[i].SetValue[j];
408
409           switch (Events[i].Type[j]) {
410           case FG_VALUE:
411           case FG_BOOL:
412             cout << " (constant";
413             break;
414           case FG_DELTA:
415             cout << " (delta";
416             break;
417           default:
418             cout << " (unspecified type";
419           }
420
421           switch (Events[i].Action[j]) {
422           case FG_RAMP:
423             cout << " via ramp";
424             break;
425           case FG_STEP:
426             cout << " via step)";
427             break;
428           case FG_EXP:
429             cout << " via exponential approach";
430             break;
431           default:
432             cout << " via unspecified action)";
433           }
434
435           if (Events[i].Action[j] == FG_RAMP || Events[i].Action[j] == FG_EXP)
436             cout << " with time constant " << Events[i].TC[j] << ")";
437         }
438         cout << endl << "    }" << endl << endl;
439
440       }
441     }
442   }
443   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
444     if (from == 0) cout << "Instantiated: FGScript" << endl;
445     if (from == 1) cout << "Destroyed:    FGScript" << endl;
446   }
447   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
448   }
449   if (debug_lvl & 8 ) { // Runtime state variables
450   }
451   if (debug_lvl & 16) { // Sanity checking
452   }
453   if (debug_lvl & 64) {
454     if (from == 0) { // Constructor
455       cout << IdSrc << endl;
456       cout << IdHdr << endl;
457     }
458   }
459 }
460 }