⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fgscript.cpp

📁 6 DOF Missle Simulation
💻 CPP
📖 第 1 页 / 共 2 页
字号:
      newEvent->ValueSpan.push_back(0.0);      string tempCompare = set_element->GetAttributeValue("type");      if      (tempCompare == "FG_DELTA") newEvent->Type.push_back(FG_DELTA);      else if (tempCompare == "FG_BOOL")  newEvent->Type.push_back(FG_BOOL);      else if (tempCompare == "FG_VALUE") newEvent->Type.push_back(FG_VALUE);      else                                newEvent->Type.push_back(FG_VALUE); // DEFAULT      tempCompare = set_element->GetAttributeValue("action");      if      (tempCompare == "FG_RAMP") newEvent->Action.push_back(FG_RAMP);      else if (tempCompare == "FG_STEP") newEvent->Action.push_back(FG_STEP);      else if (tempCompare == "FG_EXP")  newEvent->Action.push_back(FG_EXP);      else                               newEvent->Action.push_back(FG_STEP); // DEFAULT      if (!set_element->GetAttributeValue("tc").empty())        newEvent->TC.push_back(set_element->GetAttributeValueAsNumber("tc"));      else        newEvent->TC.push_back(1.0); // DEFAULT      newEvent->Transiting.push_back(false);      set_element = event_element->FindNextElement("set");    }    Events.push_back(*newEvent);    delete newEvent;    event_element = run_element->FindNextElement("event");  }  Debug(4);  FGInitialCondition *IC=FDMExec->GetIC();  if ( ! IC->Load( initialize )) {    cerr << "Initialization unsuccessful" << endl;    exit(-1);  }  return true;}//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bool FGScript::RunScript(void){  vector <struct event>::iterator iEvent = Events.begin();  unsigned i, j;  unsigned event_ctr = 0;  double currentTime = State->Getsim_time();  double newSetValue = 0;  if (currentTime > EndTime) return false; //Script done!  // Iterate over all events.  while (iEvent < Events.end()) {    iEvent->PrevTriggered = iEvent->Triggered;    // Determine whether the set of conditional tests for this condition equate    // to true and should cause the event to execute.    if (iEvent->Condition->Evaluate()) {      if (!iEvent->Triggered) {        // The conditions are true, do the setting of the desired Event parameters        for (i=0; i<iEvent->SetValue.size(); i++) {          iEvent->OriginalValue[i] = iEvent->SetParam[i]->getDoubleValue();          if (iEvent->Functions[i] != 0) { // Parameter should be set to a function value            iEvent->SetValue[i] = iEvent->Functions[i]->GetValue();          }          switch (iEvent->Type[i]) {          case FG_VALUE:          case FG_BOOL:            iEvent->newValue[i] = iEvent->SetValue[i];            break;          case FG_DELTA:            iEvent->newValue[i] = iEvent->OriginalValue[i] + iEvent->SetValue[i];            break;          default:            cerr << "Invalid Type specified" << endl;            break;          }          iEvent->StartTime = currentTime + iEvent->Delay;          iEvent->ValueSpan[i] = iEvent->newValue[i] - iEvent->OriginalValue[i];          iEvent->Transiting[i] = true;        }      }      iEvent->Triggered = true;    } else if (iEvent->Persistent) {      iEvent->Triggered = false; // Reset the trigger for persistent events      iEvent->Notified = false;  // Also reset the notification flag    }    if ((currentTime >= iEvent->StartTime) && iEvent->Triggered) {      for (i=0; i<iEvent->SetValue.size(); i++) {        if (iEvent->Transiting[i]) {          iEvent->TimeSpan = currentTime - iEvent->StartTime;          if (iEvent->Functions[i] == 0) {            switch (iEvent->Action[i]) {            case FG_RAMP:              if (iEvent->TimeSpan <= iEvent->TC[i]) {                newSetValue = iEvent->TimeSpan/iEvent->TC[i] * iEvent->ValueSpan[i] + iEvent->OriginalValue[i];              } else {                newSetValue = iEvent->newValue[i];                iEvent->Transiting[i] = false;              }              break;            case FG_STEP:              newSetValue = iEvent->newValue[i];              iEvent->Transiting[i] = false;              break;            case FG_EXP:              newSetValue = (1 - exp( -iEvent->TimeSpan/iEvent->TC[i] )) * iEvent->ValueSpan[i] + iEvent->OriginalValue[i];              break;            default:              cerr << "Invalid Action specified" << endl;              break;            }          } else { // Set the new value based on a function            newSetValue = iEvent->Functions[i]->GetValue();          }          iEvent->SetParam[i]->setDoubleValue(newSetValue);        }      }      // Print notification values after setting them      if (iEvent->Notify && !iEvent->Notified) {        cout << endl << "  Event " << event_ctr << " (" << iEvent->Name << ")"             << " executed at time: " << currentTime << endl;        for (j=0; j<iEvent->NotifyProperties.size();j++) {          cout << "    " << iEvent->NotifyProperties[j]->GetName()               << " = " << iEvent->NotifyProperties[j]->getDoubleValue() << endl;        }        cout << endl;        iEvent->Notified = true;      }    }    iEvent++;    event_ctr++;  }  return true;}//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//    The bitmasked value choices are as follows://    unset: In this case (the default) JSBSim would only print//       out the normally expected messages, essentially echoing//       the config files as they are read. If the environment//       variable is not set, debug_lvl is set to 1 internally//    0: This requests JSBSim not to output any messages//       whatsoever.//    1: This value explicity requests the normal JSBSim//       startup messages//    2: This value asks for a message to be printed out when//       a class is instantiated//    4: When this value is set, a message is displayed when a//       FGModel object executes its Run() method//    8: When this value is set, various runtime state variables//       are printed out periodically//    16: When set various parameters are sanity checked and//       a message is printed out when they go out of boundsvoid FGScript::Debug(int from){  if (debug_lvl <= 0) return;  if (debug_lvl & 1) { // Standard console startup message output    if (from == 0) { // Constructor    } else if (from == 3) {    } else if (from == 4)  { // print out script data      cout << endl;      cout << "Script: \"" << ScriptName << "\"" << endl;      cout << "  begins at " << StartTime << " seconds and runs to " << EndTime           << " seconds with dt = " << State->Getdt() << endl;      cout << endl;      for (unsigned i=0; i<Events.size(); i++) {        cout << "Event " << i;        if (!Events[i].Name.empty()) cout << " (" << Events[i].Name << ")";        cout << ":" << endl;        if (Events[i].Persistent)          cout << "  " << "Always executes";        else          cout << "  " << "Executes once";        Events[i].Condition->PrintCondition();        cout << endl << "  Actions taken:" << endl << "    {";        for (unsigned j=0; j<Events[i].SetValue.size(); j++) {          if (Events[i].SetValue[j] == 0.0 && Events[i].Functions[j] != 0L) {            if (Events[i].SetParam[j] == 0) {              cerr << fgred << highint << endl                   << "  An attempt has been made to access a non-existent property" << endl                   << "  in this event. Please check the property names used, spelling, etc."                   << reset << endl;              exit(-1);            }            cout << endl << "      set " << Events[i].SetParam[j]->GetName()                 << " to function value";          } else {            if (Events[i].SetParam[j] == 0) {              cerr << fgred << highint << endl                   << "  An attempt has been made to access a non-existent property" << endl                   << "  in this event. Please check the property names used, spelling, etc."                   << reset << endl;              exit(-1);            }            cout << endl << "      set " << Events[i].SetParam[j]->GetName()                 << " to " << Events[i].SetValue[j];          }          switch (Events[i].Type[j]) {          case FG_VALUE:          case FG_BOOL:            cout << " (constant";            break;          case FG_DELTA:            cout << " (delta";            break;          default:            cout << " (unspecified type";          }          switch (Events[i].Action[j]) {          case FG_RAMP:            cout << " via ramp";            break;          case FG_STEP:            cout << " via step)";            break;          case FG_EXP:            cout << " via exponential approach";            break;          default:            cout << " via unspecified action)";          }          if (Events[i].Action[j] == FG_RAMP || Events[i].Action[j] == FG_EXP)            cout << " with time constant " << Events[i].TC[j] << ")";        }        cout << endl << "    }" << endl << endl;      }    }  }  if (debug_lvl & 2 ) { // Instantiation/Destruction notification    if (from == 0) cout << "Instantiated: FGScript" << endl;    if (from == 1) cout << "Destroyed:    FGScript" << endl;  }  if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects  }  if (debug_lvl & 8 ) { // Runtime state variables  }  if (debug_lvl & 16) { // Sanity checking  }  if (debug_lvl & 64) {    if (from == 0) { // Constructor      cout << IdSrc << endl;      cout << IdHdr << endl;    }  }}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -