示例#1
0
void jsbsim_init(void) {

    // *** SET UP JSBSIM *** //

    char* root = getenv("PAPARAZZI_HOME");
    if (root == NULL) {
        cerr << "PAPARAZZI_HOME is not defined" << endl;
        exit(0);
    }
    string pprzRoot = string(root);

#ifdef JSBSIM_MODEL
    AircraftName = JSBSIM_MODEL;
#endif
#ifdef JSBSIM_INIT
    ICName = JSBSIM_INIT;
#endif

    FDMExec = new JSBSim::FGFDMExec();

    /* Set simulation time step */
    FDMExec->Setsim_time(0.);
    FDMExec->Setdt(DT);
    cout << "Simulation delta " << FDMExec->GetDeltaT() << endl;

    FDMExec->DisableOutput();
    FDMExec->SetDebugLevel(0); // No DEBUG messages

    if (!AircraftName.empty()) {

        if ( ! FDMExec->LoadModel( pprzRoot + "/conf/simulator",
                                   pprzRoot + "/conf/simulator",
                                   pprzRoot + "/conf/simulator",
                                   AircraftName)) {
            cerr << "  JSBSim could not be started" << endl << endl;
            delete FDMExec;
            exit(-1);
        }

        JSBSim::FGInitialCondition *IC = FDMExec->GetIC();
        if(!ICName.empty()) {
            if (!IC->Load(ICName)) {
                delete FDMExec;
                cerr << "Initialization from file unsuccessful" << endl;
                exit(-1);
            }
        }
        else {

            // FGInitialCondition::SetAltitudeASLFtIC
            // requires this function to be called
            // before itself
            IC->SetVgroundFpsIC(0.);

            // Use flight plan initial conditions
            IC->SetLatitudeDegIC(NAV_LAT0 / 1e7);
            IC->SetLongitudeDegIC(NAV_LON0 / 1e7);

            IC->SetAltitudeASLFtIC((GROUND_ALT + 2.0) / FT2M);
            IC->SetTerrainElevationFtIC(GROUND_ALT / FT2M);
            IC->SetPsiDegIC(QFU);
            IC->SetVgroundFpsIC(0.);

            //initRunning for all engines
            FDMExec->GetPropulsion()->InitRunning(-1);
            if (!FDMExec->RunIC()) {
                cerr << "Initialization from flight plan unsuccessful" << endl;
                exit(-1);
            }
        }
        //FDMExec->GetGroundReactions()->InitModel();

    } else {
        cerr << "  No Aircraft given" << endl << endl;
        delete FDMExec;
        exit(-1);
    }

    //if (FDMExec->Run()) cout << "Made Initial Run" << endl;
    //else {
    //  cerr << "Initial run failed " << endl;
    //  exit(-1);
    //}

}
示例#2
0
/**
 * Initializes JSBSim.
 *
 * Sets up the JSBSim executive and loads initial conditions
 * Exits NPS with -1 if models or ICs fail to load
 *
 * @param dt   The desired simulation timestep
 *
 * @warning Needs PAPARAZZI_HOME defined to find the config files
 */
static void init_jsbsim(double dt) {

  char buf[1024];
  string rootdir;
  string jsbsim_ic_name;

  sprintf(buf,"%s/conf/simulator/jsbsim/",getenv("PAPARAZZI_HOME"));
  rootdir = string(buf);

  /* if jsbsim initial conditions are defined, use them
   * otherwise use flightplan location
   */
#ifdef NPS_JSBSIM_INIT
  jsbsim_ic_name = NPS_JSBSIM_INIT;
#endif

  FDMExec = new FGFDMExec();

  FDMExec->Setsim_time(0.);
  FDMExec->Setdt(dt);

  FDMExec->DisableOutput();
  FDMExec->SetDebugLevel(0); // No DEBUG messages

  if ( ! FDMExec->LoadModel( rootdir + "aircraft",
                             rootdir + "engine",
                             rootdir + "systems",
                             NPS_JSBSIM_MODEL,
                             false)){
#ifdef DEBUG
    cerr << "  JSBSim could not be started" << endl << endl;
#endif
    delete FDMExec;
    exit(-1);
  }

  //initRunning for all engines
  FDMExec->GetPropulsion()->InitRunning(-1);

  JSBSim::FGInitialCondition *IC = FDMExec->GetIC();
  if(!jsbsim_ic_name.empty()) {
    if ( ! IC->Load(jsbsim_ic_name)) {
#ifdef DEBUG
      cerr << "Initialization unsuccessful" << endl;
#endif
      delete FDMExec;
      exit(-1);
    }
  }
  else {
    // FGInitialCondition::SetAltitudeASLFtIC
    // requires this function to be called
    // before itself
    IC->SetVgroundFpsIC(0.);

    // Use flight plan initial conditions
    // convert geodetic lat from flight plan to geocentric
    double gd_lat = RadOfDeg(NAV_LAT0 / 1e7);
    double gc_lat = gc_of_gd_lat_d(gd_lat, GROUND_ALT);
    IC->SetLatitudeDegIC(DegOfRad(gc_lat));
    IC->SetLongitudeDegIC(NAV_LON0 / 1e7);


    IC->SetAltitudeASLFtIC(FeetOfMeters(GROUND_ALT + 2.0));
    IC->SetTerrainElevationFtIC(FeetOfMeters(GROUND_ALT));
    IC->SetPsiDegIC(QFU);
    IC->SetVgroundFpsIC(0.);

    //initRunning for all engines
    FDMExec->GetPropulsion()->InitRunning(-1);
    if (!FDMExec->RunIC()) {
      cerr << "Initialization from flight plan unsuccessful" << endl;
      exit(-1);
    }

    // compute offset between geocentric and geodetic ecef
    struct LlaCoor_d lla0 = { RadOfDeg(NAV_LON0 / 1e7), gd_lat, (double)(NAV_ALT0+NAV_MSL0)/1000. };
    ecef_of_lla_d(&offset, &lla0);
    struct EcefCoor_d ecef0 = {
      MetersOfFeet(FDMExec->GetPropagate()->GetLocation().Entry(1)),
      MetersOfFeet(FDMExec->GetPropagate()->GetLocation().Entry(2)),
      MetersOfFeet(FDMExec->GetPropagate()->GetLocation().Entry(3))
    };
    VECT3_DIFF(offset, offset, ecef0);
  }

  // calculate vehicle max radius in m
  vehicle_radius_max = 0.01; // specify not 0.0 in case no gear
  int num_gear = FDMExec->GetGroundReactions()->GetNumGearUnits();
  int i;
  for(i = 0; i < num_gear; i++) {
    FGColumnVector3 gear_location = FDMExec->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation();
    double radius = MetersOfFeet(gear_location.Magnitude());
    if (radius > vehicle_radius_max) vehicle_radius_max = radius;
  }

}
示例#3
0
//------------------------------------------------------------------------------
// reset() -- 
//------------------------------------------------------------------------------
void JSBSimModel::reset()
{
    BaseClass::reset();

    pitchTrimPos  = (LCreal)0.0;
    pitchTrimRate = (LCreal)0.1;
    pitchTrimSw   = (LCreal)0.0;
    rollTrimPos   = (LCreal)0.0;
    rollTrimRate  = (LCreal)0.1;
    rollTrimSw    = (LCreal)0.0;

    // Get our Player (must have one!)
    Simulation::Player* p = static_cast<Simulation::Player*>( findContainerByType(typeid(Simulation::Player)) );
    if (p == 0) return;

    // must have strings set
    if (rootDir == 0 || model == 0) return;

    // Must also have the JSBSim object
    if (fdmex == 0) {
        // must have a JSBSim property manager
        if (propMgr == 0) {
            propMgr = new JSBSim::FGPropertyManager();
        }
        fdmex = new JSBSim::FGFDMExec(propMgr);

        std::string RootDir(rootDir->getString());
        fdmex->SetAircraftPath(RootDir + "aircraft");
        fdmex->SetEnginePath(RootDir + "engine");
        fdmex->SetSystemsPath(RootDir + "systems"); // JSBSim-1.0 or after only

        fdmex->LoadModel(model->getString());
        JSBSim::FGPropertyManager* propMgr = fdmex->GetPropertyManager();
        if (propMgr != 0) {
            hasHeadingHold = propMgr->HasNode("ap/heading_hold") && propMgr->HasNode("ap/heading_setpoint");
            hasVelocityHold = propMgr->HasNode("ap/airspeed_hold") && propMgr->HasNode("ap/airspeed_setpoint");
            hasAltitudeHold = propMgr->HasNode("ap/altitude_hold") && propMgr->HasNode("ap/altitude_setpoint");
#if 0
            // CGB this isn't working for some reason. I set the values directly in "dynamics" for now.
            if (hasHeadingHold) {
                propMgr->Tie("ap/heading_hold", this, &JSBSimModel::isHeadingHoldOn);
                propMgr->Tie("ap/heading_setpoint", this, &JSBSimModel::getCommandedHeadingD);
            }
            if (hasVelocityHold) {
                propMgr->Tie("ap/airspeed_hold", this, &JSBSimModel::isVelocityHoldOn);
                propMgr->Tie("ap/airspeed_setpoint", this, &JSBSimModel::getCommandedVelocityKts);
            }
            if (hasAltitudeHold) {
                propMgr->Tie("ap/altitude_hold", this, &JSBSimModel::isAltitudeHoldOn);
                propMgr->Tie("ap/altitude_setpoint", this, &JSBSimModel::getCommandedAltitude * Basic::Distance::M2FT);
            }
#endif
        }
    }
    
#if 0
    // CGB TBD
    reset = 0;
    freeze = 0;
#endif
    JSBSim::FGInitialCondition* fgic = fdmex->GetIC();
    if (fgic == 0) return;

    fgic->SetAltitudeASLFtIC(Basic::Distance::M2FT * p->getAltitude());

#if 0
    fgic->SetTrueHeadingDegIC(Basic::Angle::R2DCC * p->getHeading());
    fgic->SetRollAngleDegIC(Basic::Angle::R2DCC * p->getRoll());
    fgic->SetPitchAngleDegIC(Basic::Angle::R2DCC * p->getPitch());
#else
    fgic->SetPsiDegIC(Basic::Angle::R2DCC * p->getHeading());
    fgic->SetPhiDegIC(Basic::Angle::R2DCC * p->getRoll());
    fgic->SetThetaDegIC(Basic::Angle::R2DCC * p->getPitch());
#endif
    fgic->SetVtrueKtsIC(Basic::Distance::M2NM * p->getTotalVelocity() * 3600.0f);
    fgic->SetLatitudeDegIC(p->getInitLatitude());
    fgic->SetLongitudeDegIC(p->getInitLongitude());

    JSBSim::FGPropulsion* Propulsion = fdmex->GetPropulsion();
    JSBSim::FGFCS* FCS = fdmex->GetFCS();
    if (Propulsion != 0 && FCS != 0) {
        Propulsion->SetMagnetos(3);
        for (unsigned int i=0; i < Propulsion->GetNumEngines(); i++) {
            FCS->SetMixtureCmd(i, 1.0);
            FCS->SetThrottleCmd(i, 1.0);
            FCS->SetPropAdvanceCmd(i, 1.0);
            FCS->SetMixturePos(i, 1.0);
            FCS->SetThrottlePos(i, 1.0);
            FCS->SetPropAdvance(i, 1.0);
            JSBSim::FGEngine* eng = Propulsion->GetEngine(i);
            eng->SetRunning(true);
            JSBSim::FGThruster* thruster = eng->GetThruster();
            thruster->SetRPM(1000.0);
        }
        Propulsion->SetFuelFreeze(p->isFuelFrozen());
        Propulsion->InitRunning(-1);     // -1 refers to "All Engines"
        Propulsion->GetSteadyState();
   }
   fdmex->RunIC();
}