Exemple #1
0
 def SetParametersFromFile(self, filename):
     """
     Input ICAROUS parameters from a .parm text file
     :param filename: path to .parm parameter file
     """
     params = LoadIcarousParams(filename)
     self.SetParameters(params)
Exemple #2
0
    def launch_arducopter(self):
        waypoints,_,_ = ReadFlightplanFile(os.path.join(icarous_home,
                                           self.scenario["waypoint_file"]))
        start_point = ','.join(str(x) for x in waypoints[0][0:3]+[0])

        # Set up parameters for SITL
        sitl_param_file = self.scenario.get("sitl_parameter_file", sitl_default)
        sitl_params = LoadIcarousParams(os.path.join(icarous_home, sitl_param_file))

        icarous_param_file = self.scenario["parameter_file"]
        icarous_params = LoadIcarousParams(os.path.join(icarous_home, icarous_param_file))
        icarous_params.update(self.scenario["param_adjustments"])

        sitl_params["WPNAV_SPEED"] = icarous_params["DEF_WP_SPEED"]*100
        filename = "sitlparams-%d" % self.spacecraft_id
        sitl_param_file = os.path.join(self.output_dir, filename)
        with open(sitl_param_file, 'w') as f:
            for param_id, param_value in sitl_params.items():
                f.write("%-16s %f\n" % (param_id, param_value))

        # Launch SITL
        arguments = ["sim_vehicle.py", "-v", "ArduCopter",
                     "-l", str(start_point),
                     "--add-param-file", sitl_param_file,
                     "--use-dir", "sitl_files",
                     "-I", str(self.spacecraft_id)]
        logname = "sitl-%d-%f.tlog" % (self.spacecraft_id, time.time())
        logfile = os.path.join(self.output_dir, logname)
        arguments += ["-m", "--logfile="+logfile]
        subprocess.Popen(arguments, stdout=subprocess.DEVNULL)

        if self.verbose:
            print("Waiting several seconds to allow ArduCopter to start up")
        time.sleep(60)
Exemple #3
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)
Exemple #4
0
 def SetParametersFromFile(self,filename):
     params = LoadIcarousParams(filename)
     self.SetParameters(params)