class DonkeyUnitySimContoller:
    """
    Wrapper for communicating with unity simulation.

    :param level: (int) Level index
    :param port: (int) Port to use for communicating with the simulator
    :param max_cte_error: (float) Max cross track error before reset
    """
    def __init__(self, level, port=9090, max_cte_error=3.0):
        self.level = level
        self.verbose = False

        # sensor size - height, width, depth
        self.camera_img_size = INPUT_DIM

        self.address = ('0.0.0.0', port)

        # Socket message handler
        self.handler = DonkeyUnitySimHandler(level,
                                             max_cte_error=max_cte_error)
        # Create the server to which the unity sim will connect
        self.server = SimServer(self.address, self.handler)
        # Start the Asynchronous socket handler thread
        self.thread = Thread(target=asyncore.loop)
        self.thread.daemon = True
        self.thread.start()

    def close_connection(self):
        return self.server.handle_close()

    def wait_until_loaded(self):
        while not self.handler.loaded:
            print("Waiting for sim to start...")
            time.sleep(3.0)

    def reset(self):
        self.handler.reset()

    def get_sensor_size(self):
        return self.handler.get_sensor_size()

    def take_action(self, action):
        self.handler.take_action(action)

    def get_original_image(self):
        return self.handler.original_image

    def observe(self):
        return self.handler.observe()

    def quit(self):
        pass

    def render(self, mode):
        pass

    def is_game_over(self):
        return self.handler.is_game_over()

    def calc_reward(self, done):
        return self.handler.calc_reward(done)
Beispiel #2
0
class DonkeyUnitySimContoller:
    """
    Wrapper for communicating with unity simulation.

    :param level: (int) Level index
    :param port: (int) Port to use for communicating with the simulator
    :param max_cte_error: (float) Max cross track error before reset
    :param road_style: (int)
    :param seed: (int)
    :param turn_increment: (float)
    """
    def __init__(self,
                 level,
                 port=9090,
                 max_cte_error=3.0,
                 road_style=0,
                 seed=0,
                 turn_increment=1.0):

        self.level = level
        self.verbose = False
        # sensor size - height, width, depth
        self.camera_img_size = INPUT_DIM

        self.address = ('0.0.0.0', port)

        # Socket message handler
        self.handler = DonkeyUnitySimHandler(level,
                                             max_cte_error=max_cte_error,
                                             road_style=road_style,
                                             seed=seed,
                                             turn_increment=turn_increment)
        # Create the server to which the unity sim will connect
        self.server = SimServer(self.address, self.handler)
        # Start the Asynchronous socket handler thread
        self.thread = Thread(target=asyncore.loop)
        self.thread.daemon = True
        self.thread.start()

    def close_connection(self):
        return self.server.handle_close()

    def wait_until_loaded(self):
        """
        Wait for a client (Unity simulator).
        """
        while not self.handler.loaded:
            print(
                "Waiting for sim to start..."
                "if the simulation is running, press EXIT to go back to the menu"
            )
            time.sleep(3.0)
        self.regen_road()

    def reset(self):
        self.handler.reset()

    def regen_road(self):
        self.handler.send_regen_road()

    def seed(self, seed):
        self.handler.seed = seed

    def get_sensor_size(self):
        """
        :return: (int, int, int)
        """
        return self.handler.get_sensor_size()

    def take_action(self, action):
        self.handler.take_action(action)

    def observe(self):
        """
        :return: (np.ndarray)
        """
        return self.handler.observe()

    def quit(self):
        pass

    def render(self, mode):
        pass

    def is_game_over(self):
        return self.handler.is_game_over()

    def calc_reward(self, done):
        return self.handler.calc_reward(done)