예제 #1
0
## adds the fsds package located the parent directory to the pyhthon path
sys.path.insert(0, os.path.abspath(os.path.join(os.getenv("HOME"), "Formula-Student-Driverless-Simulator/python")))
import fsds


import qlearn
import SimEnv

import datetime
import time
import argparse

parser = argparse.ArgumentParser(description='Q Learning based Optimal Control')
parser.add_argument("--name", help="Name of the checkpoints folder, defaults to timestamp", default=datetime.datetime.fromtimestamp(time.time()).isoformat())
args = parser.parse_args()
print(args)

# connect to the simulator 
client = fsds.FSDSClient()

# Check network connection, exit if not connected
client.confirmConnection()

# After enabling setting trajectory setpoints via the api. 
client.enableApiControl(True)

env = SimEnv.Env(client)

qlearn.start_learning(env, args.name)
    def launch_simulator(self):
        # Abort if simulator is already running
        if self.simulation_process is not None:
            print(400, 'Simulation already running.')

        # Get team config
        self.mission = MISSIONS[0]  # request.json['mission']
        self.track = TRACKS[0]  # request.json['track']

        # Create log file. Create logs directory if it does not exist
        filename = 'logs/{}_{}_{}.txt'.format(
            str(datetime.now().strftime("%d-%m-%Y_%H%M%S")), "DDQ",
            self.mission)
        simfilename = 'logs/{}_{}_{}_SIM.txt'.format(
            str(datetime.now().strftime("%d-%m-%Y_%H%M%S")), "DDQ",
            self.mission)
        if not os.path.exists(os.path.dirname(filename)):
            try:
                os.makedirs(os.path.dirname(filename))
            except OSError as exc:  # Guard against race condition
                if exc.errno != errno.EEXIST:
                    raise
        # Write to log file
        self.log_file = open(filename, 'w')
        self.log("created logfile " + filename)

        self.finished_signal_received = False

        # Write team specific car settings to settings.json
        #filename = os.path.realpath(os.path.dirname(__file__)) + '/../settings.json'
        #with open(filename, 'w') as file:
        #    json.dump(self.team['car_settings'], file, sort_keys=True, indent=4, separators=(',', ': '))

        proc = None
        try:
            # Launch Unreal Engine simulator
            #proc = subprocess.Popen(['../simulator/FSDS.exe', '/Game/'+self.track+'?listen'])
            simfile = open(simfilename, 'w')
            proc = subprocess.Popen(
                [self.executable, '/Game/' + self.track + '?listen'],
                stdout=simfile)

            time.sleep(15)

            # Create connection with airsim car client
            self.client = fsds.FSDSClient()
            self.client.confirmConnection()
            self.client.enableApiControl(True)

            self.getAllSimData()

            self.compute_track_boundaries()

            # Get referee state
            self.doo_count = self.referee_state.doo_counter
            self.lap_times = self.referee_state.laps

            # Start referee state listener
            #self.referee_state_timer = Timer(1.5, self.referee_state_listener)
            #self.referee_state_timer.start()

            self.simulation_process = proc

            self.log('Launched simulator. {} {} {}'.format(
                "DDQ", self.track, self.mission))

            return {}
        except Exception as e:
            #e = sys.exc_info()[0]
            print("Error while launching simulator", e)
            print(e)
            self.shutdown_process(proc)
            exit(1)
            raise