~ubuntu-branches/ubuntu/precise/flightgear/precise

« back to all changes in this revision

Viewing changes to src/FDM/JSBSim/input_output/FGScript.cpp

  • Committer: Package Import Robot
  • Author(s): Ove Kaaven
  • Date: 2011-09-03 22:16:12 UTC
  • mfrom: (3.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20110903221612-2cjy0z7ztj5nkln5
Tags: 2.4.0-1
* New upstream release. Closes: #638588.
* Build-Depend on OpenSceneGraph 3.0, and the Subversion library.
* Recommend fgfs-scenery-base.
* Enable parallel builds (shorter compile times on multicore CPUs).
* Removed hack that tried to build without optimizations if
  building with optimizations fails.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43
43
 
44
44
#include "FGScript.h"
 
45
#include "input_output/FGXMLElement.h"
45
46
#include "input_output/FGXMLParse.h"
46
47
#include "initialization/FGTrim.h"
47
48
 
48
49
#include <iostream>
49
50
#include <cstdlib>
 
51
#include <iomanip>
50
52
 
51
53
using namespace std;
52
54
 
53
55
namespace JSBSim {
54
56
 
55
 
static const char *IdSrc = "$Id$";
 
57
static const char *IdSrc = "$Id: FGScript.cpp,v 1.46 2011/02/18 12:44:16 jberndt Exp $";
56
58
static const char *IdHdr = ID_FGSCRIPT;
57
59
 
58
60
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67
69
 
68
70
FGScript::FGScript(FGFDMExec* fgex) : FDMExec(fgex)
69
71
{
70
 
  State = FDMExec->GetState();
71
72
  PropertyManager=FDMExec->GetPropertyManager();
 
73
 
72
74
  Debug(0);
73
75
}
74
76
 
76
78
 
77
79
FGScript::~FGScript()
78
80
{
79
 
  unsigned int i;
 
81
  unsigned int i, j;
80
82
 
81
 
  for (i=0; i<local_properties.size(); i++) delete local_properties[i];
 
83
  for (i=0; i<local_properties.size(); i++) {
 
84
    delete local_properties[i]->value;
 
85
    delete local_properties[i];
 
86
  }
82
87
  local_properties.clear();
83
88
 
84
 
  for (i=0; i<Events.size(); i++) delete Events[i].Condition;
 
89
  for (i=0; i<Events.size(); i++) {
 
90
    delete Events[i].Condition;
 
91
    for (j=0; j<Events[i].Functions.size(); j++)
 
92
      delete Events[i].Functions[j];
 
93
  }
85
94
  Events.clear();
86
95
 
87
96
  Debug(1);
89
98
 
90
99
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91
100
 
92
 
bool FGScript::LoadScript( string script )
 
101
bool FGScript::LoadScript(string script, double deltaT)
93
102
{
94
103
  string aircraft="", initialize="", comparison = "", prop_name="";
95
104
  string notifyPropertyName="";
135
144
  // Set sim timing
136
145
 
137
146
  StartTime = run_element->GetAttributeValueAsNumber("start");
138
 
  State->Setsim_time(StartTime);
 
147
  FDMExec->Setsim_time(StartTime);
139
148
  EndTime   = run_element->GetAttributeValueAsNumber("end");
140
 
  dt        = run_element->GetAttributeValueAsNumber("dt");
141
 
  State->Setdt(dt);
 
149
  // Make sure that the desired time is reached and executed.
 
150
  EndTime += 0.99*FDMExec->GetDeltaT();
 
151
 
 
152
  if (deltaT == 0.0)
 
153
    dt = run_element->GetAttributeValueAsNumber("dt");
 
154
  else {
 
155
    dt = deltaT;
 
156
    cout << endl << "Overriding simulation step size from the command line. New step size is: "
 
157
         << deltaT << " seconds (" << 1/deltaT << " Hz)" << endl << endl;
 
158
  }
 
159
 
 
160
  FDMExec->Setdt(dt);
142
161
  
143
162
  // read aircraft and initialization files
144
163
 
175
194
    if (output_file.empty()) {
176
195
      cerr << "No logging directives file was specified." << endl;
177
196
    } else {
178
 
      FDMExec->SetOutputDirectives(output_file);
 
197
      if (!FDMExec->SetOutputDirectives(output_file)) return false;
179
198
    }
180
199
  }
181
200
 
226
245
    // Process the conditions
227
246
    condition_element = event_element->FindElement("condition");
228
247
    if (condition_element != 0) {
229
 
      newCondition = new FGCondition(condition_element, PropertyManager);
 
248
      try {
 
249
        newCondition = new FGCondition(condition_element, PropertyManager);
 
250
      } catch(string str) {
 
251
        cout << endl << fgred << str << reset << endl << endl;
 
252
        delete newEvent;
 
253
        return false;
 
254
      }
230
255
      newEvent->Condition = newCondition;
231
256
    } else {
232
257
      cerr << "No condition specified in script event " << newEvent->Name << endl;
 
258
      delete newEvent;
233
259
      return false;
234
260
    }
235
261
 
243
269
    // Notify about when this event is triggered?
244
270
    if ((notify_element = event_element->FindElement("notify")) != 0) {
245
271
      newEvent->Notify = true;
 
272
      // Check here for new <description> tag that gets echoed
 
273
      string notify_description = notify_element->FindElementValue("description");
 
274
      if (!notify_description.empty()) {
 
275
        newEvent->Description = notify_description;
 
276
      }
246
277
      notify_property_element = notify_element->FindElement("property");
247
278
      while (notify_property_element) {
248
279
        notifyPropertyName = notify_property_element->GetDataLine();
249
280
        if (PropertyManager->GetNode(notifyPropertyName)) {
250
281
          newEvent->NotifyProperties.push_back( PropertyManager->GetNode(notifyPropertyName) );
 
282
          string caption_attribute = notify_property_element->GetAttributeValue("caption");
 
283
          if (caption_attribute.empty()) {
 
284
            newEvent->DisplayString.push_back(notifyPropertyName);
 
285
          } else {
 
286
            newEvent->DisplayString.push_back(caption_attribute);
 
287
          }
251
288
        } else {
252
289
          cout << endl << fgred << "  Could not find the property named "
253
290
               << notifyPropertyName << " in script" << endl << "  \""
254
 
               << ScriptName << "\". This unknown property will not be "
255
 
               << "echoed for notification." << reset << endl;
 
291
               << ScriptName << "\". Execution is aborted. Please recheck "
 
292
               << "your input files and scripts." << reset << endl;
 
293
          delete newEvent->Condition;
 
294
          delete newEvent;
 
295
          return false;
256
296
        }
257
297
        notify_property_element = notify_element->FindNextElement("property");
258
298
      }
320
360
  unsigned i, j;
321
361
  unsigned event_ctr = 0;
322
362
 
323
 
  double currentTime = State->Getsim_time();
 
363
  double currentTime = FDMExec->GetSimTime();
324
364
  double newSetValue = 0;
325
365
 
326
 
  if (currentTime > EndTime) return false; //Script done!
 
366
  if (currentTime > EndTime) return false;
327
367
 
328
368
  // Iterate over all events.
329
369
  for (unsigned int ev_ctr=0; ev_ctr < Events.size(); ev_ctr++) {
361
401
      Events[ev_ctr].Triggered = true;
362
402
 
363
403
    } else if (Events[ev_ctr].Persistent) { // If the event is persistent, reset the trigger.
364
 
 
 
404
      Events[ev_ctr].Triggered = false; // Reset the trigger for persistent events
 
405
      Events[ev_ctr].Notified = false;  // Also reset the notification flag
 
406
    } else if (Events[ev_ctr].Continuous) { // If the event is continuous, reset the trigger.
365
407
      Events[ev_ctr].Triggered = false; // Reset the trigger for persistent events
366
408
      Events[ev_ctr].Notified = false;  // Also reset the notification flag
367
409
    }
408
450
      if (Events[ev_ctr].Notify && !Events[ev_ctr].Notified) {
409
451
        cout << endl << "  Event " << event_ctr << " (" << Events[ev_ctr].Name << ")"
410
452
             << " executed at time: " << currentTime << endl;
 
453
        if (!Events[ev_ctr].Description.empty()) {
 
454
          cout << "    " << Events[ev_ctr].Description << endl;
 
455
        }
411
456
        for (j=0; j<Events[ev_ctr].NotifyProperties.size();j++) {
412
 
          cout << "    " << Events[ev_ctr].NotifyProperties[j]->GetName()
 
457
//          cout << "    " << Events[ev_ctr].NotifyProperties[j]->GetRelativeName()
 
458
          cout << "    " << Events[ev_ctr].DisplayString[j]
413
459
               << " = " << Events[ev_ctr].NotifyProperties[j]->getDoubleValue() << endl;
414
460
        }
415
461
        cout << endl;
453
499
      cout << endl;
454
500
      cout << "Script: \"" << ScriptName << "\"" << endl;
455
501
      cout << "  begins at " << StartTime << " seconds and runs to " << EndTime
456
 
           << " seconds with dt = " << State->Getdt() << endl;
 
502
        << " seconds with dt = " << setprecision(6) << FDMExec->GetDeltaT() << " (" <<
 
503
        ceil(1.0/FDMExec->GetDeltaT()) << " Hz)" << endl;
457
504
      cout << endl;
458
505
 
459
506
      for (unsigned int i=0; i<local_properties.size(); i++) {
470
517
        cout << ":" << endl;
471
518
 
472
519
        if (Events[i].Persistent)
473
 
          cout << "  " << "Always executes";
 
520
          cout << "  " << "Whenever triggered, executes once";
 
521
        else if (Events[i].Continuous)
 
522
          cout << "  " << "While true, always executes";
474
523
        else
475
 
          cout << "  " << "Executes once";
 
524
          cout << "  " << "When first triggered, executes once";
476
525
 
477
526
        Events[i].Condition->PrintCondition();
478
527
 
479
 
        cout << endl << "  Actions taken:" << endl << "    {";
 
528
        cout << endl << "  Actions taken";
 
529
        if (Events[i].Delay > 0.0)
 
530
          cout << " (after a delay of " << Events[i].Delay << " secs)";
 
531
        cout << ":" << endl << "    {";
480
532
        for (unsigned j=0; j<Events[i].SetValue.size(); j++) {
481
533
          if (Events[i].SetValue[j] == 0.0 && Events[i].Functions[j] != 0L) {
482
534
            if (Events[i].SetParam[j] == 0) {
486
538
                   << reset << endl;
487
539
              exit(-1);
488
540
            }
489
 
            cout << endl << "      set " << Events[i].SetParam[j]->GetName()
 
541
            cout << endl << "      set " << Events[i].SetParam[j]->GetRelativeName("/fdm/jsbsim/")
490
542
                 << " to function value";
491
543
          } else {
492
544
            if (Events[i].SetParam[j] == 0) {
496
548
                   << reset << endl;
497
549
              exit(-1);
498
550
            }
499
 
            cout << endl << "      set " << Events[i].SetParam[j]->GetName()
 
551
            cout << endl << "      set " << Events[i].SetParam[j]->GetRelativeName("/fdm/jsbsim/")
500
552
                 << " to " << Events[i].SetValue[j];
501
553
          }
502
554
 
529
581
          if (Events[i].Action[j] == FG_RAMP || Events[i].Action[j] == FG_EXP)
530
582
            cout << " with time constant " << Events[i].TC[j] << ")";
531
583
        }
532
 
        cout << endl << "    }" << endl << endl;
 
584
        cout << endl << "    }" << endl;
533
585
 
 
586
        // Print notifications
 
587
        if (Events[i].Notify) {
 
588
          if (Events[i].NotifyProperties.size() > 0) {
 
589
            cout << "  Notifications" << ":" << endl << "    {" << endl;
 
590
            for (unsigned j=0; j<Events[i].NotifyProperties.size();j++) {
 
591
              cout << "      "
 
592
                   << Events[i].NotifyProperties[j]->GetRelativeName("/fdm/jsbsim/")
 
593
                   << endl;
 
594
            }
 
595
            cout << "    }" << endl;
 
596
          }
 
597
        }
 
598
        cout << endl;
534
599
      }
535
600
    }
536
601
  }