예제 #1
0
def RunScenario(scenario,
                verbose=0,
                fasttime=True,
                use_python=False,
                out=None,
                output_dir="sim_output"):
    """
    Run an ICAROUS scenario
    :param scenario: dictionary defining scenario inputs
    :param verbose: control printouts (0: none, 1: some, 2+: more)
    :param fasttime: run simulation in fasttime (pycarous only)
    :param use_python: run simulation with pycarous (override scenario inputs)
    :param out: port number to forward MAVLink data for visualization
                (use out=None to turn off MAVLink output)
    :param output_dir: location to save log files
    """
    # Clear out existing logs
    ClearLogs(pycarous_log_dir)
    ClearLogs(cFS_log_dir)

    # Set python option for each vehicle
    for v in scenario["vehicles"]:
        if use_python:
            v["python"] = True
        if not v.get("python", scenario.get("python", False)):
            fasttime = False

    # Create simulation environment
    sim_time_limit = scenario.get("time_limit", 1000)
    sim = SimEnvironment(verbose=verbose,
                         fasttime=fasttime,
                         time_limit=sim_time_limit)
    if "merge_fixes" in scenario:
        sim.InputMergeFixes(os.path.join(icarous_home,
                                         scenario["merge_fixes"]))
    sim.AddWind(scenario.get("wind", [(0, 0)]))
    sitl_running = False

    # Add Icarous instances to simulation environment
    num_vehicles = len(scenario["vehicles"])
    for v in scenario["vehicles"]:
        v = dict(list(scenario.items()) + list(v.items()))
        cpu_id = v.get("cpu_id", len(sim.icInstances) + 1)
        spacecraft_id = cpu_id - 1
        callsign = v.get("name", "vehicle%d" % spacecraft_id)
        fp_file = os.path.join(icarous_home, v["waypoint_file"])
        HomePos = GetHomePosition(fp_file)
        daa_file = os.path.join(icarous_home,
                                v.get("daa_file", default_daa_file))

        # Initialize Icarous class
        python = v.get("python", False)
        if python:
            os.chdir("../pycarous")
            ic = Icarous(HomePos,
                         simtype="UAM_VTOL",
                         vehicleID=spacecraft_id,
                         callsign=callsign,
                         verbose=verbose,
                         fasttime=fasttime,
                         daaConfig=daa_file)
        else:
            sim.fasttime = False
            try:
                shutil.copyfile(daa_file,
                                icarous_exe + "/../ram/DaidalusQuadConfig.txt")
            except shutil.SameFileError:
                pass
            apps = v.get("apps", default_apps)
            sitl = v.get("sitl", False)
            if sitl:
                if "arducopter" not in apps:
                    apps.append("arducopter")
                if "rotorsim" in apps:
                    apps.remove("rotorsim")
            sitl_running |= sitl
            ic = IcarousRunner(HomePos,
                               vehicleID=spacecraft_id,
                               callsign=callsign,
                               verbose=verbose,
                               apps=apps,
                               sitl=sitl,
                               out=out)

        # Set parameters
        param_file = os.path.join(icarous_home, v["parameter_file"])
        params = LoadIcarousParams(param_file)
        if v.get("param_adjustments"):
            params.update(v["param_adjustments"])
        params["RESSPEED"] = params["DEF_WP_SPEED"]
        ic.SetParameters(params)

        # Input flight plan
        eta = v.get("eta", False)
        ic.InputFlightplanFromFile(fp_file, eta=eta)

        # Input geofences
        if v.get("geofence_file"):
            gf_file = os.path.join(icarous_home, v["geofence_file"])
            ic.InputGeofence(gf_file)

        # Input simulated traffic
        for tf in v["traffic"]:
            traf_id = num_vehicles + len(sim.tfList)
            sim.AddTraffic(traf_id, HomePos, *tf)

        delay = v.get("delay", 0)
        time_limit = min(sim_time_limit, v.get("time_limit", sim_time_limit))
        sim.AddIcarousInstance(ic, delay, time_limit)

    # Run the simulation
    sim.RunSimulation()

    # Shut down SITL if necessary
    if sitl_running:
        subprocess.call(["pkill", "-9", "arducopter"])
        subprocess.call(["pkill", "-9", "mavproxy"])

    # Collect the log files
    os.chdir(output_dir)
    sim.WriteLog()
    os.chdir(sim_home)
    CollectLogs(pycarous_log_dir, output_dir)
    CollectLogs(cFS_log_dir, output_dir)
예제 #2
0
    args.fasttime = False

# Create log folder for module logs
try:
    os.mkdir(os.path.join(os.getcwd(), 'log'))
except FileExistsError:
    pass
except:
    raise

# Initialize simulation environment
sim = SimEnvironment(fasttime=args.fasttime, verbose=args.verbosity)
sim.AddWind([args.wind])

# Set the home position for the simulation
HomePos = GetHomePosition(args.flightplan)

# Add traffic inputs
if args.traffic != '':
    tfinputs = ReadTrafficInput(args.traffic)
    for tf in tfinputs:
        sim.AddTraffic(tf[0], HomePos, *tf[1:-1], delay=tf[-1])

# Initialize Icarous class
if args.cfs:
    ic = IcarousRunner(HomePos, verbose=args.verbosity)
else:
    ic = Icarous(HomePos,
                 simtype=args.simtype,
                 monitor=args.daaType,
                 verbose=args.verbosity,