Ejemplo n.º 1
0
    def restart(self):

        if self.external_actor:
            # Check whether there is already an actor with defined role name
            for actor in self.world.get_actors():
                if actor.attributes.get('role_name') == self.actor_role_name:
                    self.player = actor
                    break
        else:
            # Get a random blueprint.
            blueprint = random.choice(
                self.world.get_blueprint_library().filter(self._actor_filter))
            blueprint.set_attribute('role_name', self.actor_role_name)
            if blueprint.has_attribute('color'):
                color = random.choice(
                    blueprint.get_attribute('color').recommended_values)
                blueprint.set_attribute('color', color)
            if blueprint.has_attribute('driver_id'):
                driver_id = random.choice(
                    blueprint.get_attribute('driver_id').recommended_values)
                blueprint.set_attribute('driver_id', driver_id)
            if blueprint.has_attribute('is_invincible'):
                blueprint.set_attribute('is_invincible', 'true')
            # Spawn the player.
            if self.player is not None:
                spawn_point = self.player.get_transform()
                spawn_point.location.z += 2.0
                spawn_point.rotation.roll = 0.0
                spawn_point.rotation.pitch = 0.0
                self.destroy()
                self.player = self.world.try_spawn_actor(
                    blueprint, spawn_point)
            while self.player is None:
                if not self.map.get_spawn_points():
                    print(
                        'There are no spawn points available in your map/town.'
                    )
                    print(
                        'Please add some Vehicle Spawn Point to your UE4 scene.'
                    )
                    sys.exit(1)
                spawn_points = self.map.get_spawn_points()
                spawn_point = random.choice(
                    spawn_points) if spawn_points else carla.Transform()
                self.player = self.world.try_spawn_actor(
                    blueprint, spawn_point)

        if self.external_actor:
            ego_sensors = []
            for actor in self.world.get_actors():
                if actor.parent == self.player:
                    ego_sensors.append(actor)

            for ego_sensor in ego_sensors:
                if ego_sensor is not None:
                    ego_sensor.destroy()

        # Set up the sensors.
        self.camera = Camera(self.player, self.dim)
        self.rss_unstructured_scene_visualizer = RssUnstructuredSceneVisualizer(
            self.player, self.world, self.dim)
        self.rss_bounding_box_visualizer = RssBoundingBoxVisualizer(
            self.dim, self.world, self.camera.sensor)
        self.rss_sensor = RssSensor(self.player, self.world,
                                    self.rss_unstructured_scene_visualizer,
                                    self.rss_bounding_box_visualizer,
                                    self.hud.rss_state_visualizer)
Ejemplo n.º 2
0
class World(object):
    def __init__(self, carla_world, args):
        self.world = carla_world
        self.actor_role_name = args.rolename
        self.dim = (args.width, args.height)
        try:
            self.map = self.world.get_map()
        except RuntimeError as error:
            print('RuntimeError: {}'.format(error))
            print('  The server could not send the OpenDRIVE (.xodr) file:')
            print(
                '  Make sure it exists, has the same name of your town, and is correct.'
            )
            sys.exit(1)
        self.external_actor = args.externalActor

        self.hud = HUD(args.width, args.height, carla_world)
        self.recording_frame_num = 0
        self.recording = False
        self.recording_dir_num = 0
        self.player = None
        self.actors = []
        self.rss_sensor = None
        self.rss_unstructured_scene_visualizer = None
        self.rss_bounding_box_visualizer = None
        self._actor_filter = args.filter
        if not self._actor_filter.startswith("vehicle."):
            print('Error: RSS only supports vehicles as ego.')
            sys.exit(1)

        self.restart()
        self.world_tick_id = self.world.on_tick(self.on_world_tick)

    def on_world_tick(self, world_snapshot):
        self.hud.on_world_tick(world_snapshot)

    def toggle_pause(self):
        settings = self.world.get_settings()
        self.pause_simulation(not settings.synchronous_mode)

    def pause_simulation(self, pause):
        settings = self.world.get_settings()
        if pause and not settings.synchronous_mode:
            settings.synchronous_mode = True
            settings.fixed_delta_seconds = 0.05
            self.world.apply_settings(settings)
        elif not pause and settings.synchronous_mode:
            settings.synchronous_mode = False
            settings.fixed_delta_seconds = None
            self.world.apply_settings(settings)

    def restart(self):

        if self.external_actor:
            # Check whether there is already an actor with defined role name
            for actor in self.world.get_actors():
                if actor.attributes.get('role_name') == self.actor_role_name:
                    self.player = actor
                    break
        else:
            # Get a random blueprint.
            blueprint = random.choice(
                self.world.get_blueprint_library().filter(self._actor_filter))
            blueprint.set_attribute('role_name', self.actor_role_name)
            if blueprint.has_attribute('color'):
                color = random.choice(
                    blueprint.get_attribute('color').recommended_values)
                blueprint.set_attribute('color', color)
            if blueprint.has_attribute('driver_id'):
                driver_id = random.choice(
                    blueprint.get_attribute('driver_id').recommended_values)
                blueprint.set_attribute('driver_id', driver_id)
            if blueprint.has_attribute('is_invincible'):
                blueprint.set_attribute('is_invincible', 'true')
            # Spawn the player.
            if self.player is not None:
                spawn_point = self.player.get_transform()
                spawn_point.location.z += 2.0
                spawn_point.rotation.roll = 0.0
                spawn_point.rotation.pitch = 0.0
                self.destroy()
                self.player = self.world.try_spawn_actor(
                    blueprint, spawn_point)
            while self.player is None:
                if not self.map.get_spawn_points():
                    print(
                        'There are no spawn points available in your map/town.'
                    )
                    print(
                        'Please add some Vehicle Spawn Point to your UE4 scene.'
                    )
                    sys.exit(1)
                spawn_points = self.map.get_spawn_points()
                spawn_point = random.choice(
                    spawn_points) if spawn_points else carla.Transform()
                self.player = self.world.try_spawn_actor(
                    blueprint, spawn_point)

        if self.external_actor:
            ego_sensors = []
            for actor in self.world.get_actors():
                if actor.parent == self.player:
                    ego_sensors.append(actor)

            for ego_sensor in ego_sensors:
                if ego_sensor is not None:
                    ego_sensor.destroy()

        # Set up the sensors.
        self.camera = Camera(self.player, self.dim)
        self.rss_unstructured_scene_visualizer = RssUnstructuredSceneVisualizer(
            self.player, self.world, self.dim)
        self.rss_bounding_box_visualizer = RssBoundingBoxVisualizer(
            self.dim, self.world, self.camera.sensor)
        self.rss_sensor = RssSensor(self.player, self.world,
                                    self.rss_unstructured_scene_visualizer,
                                    self.rss_bounding_box_visualizer,
                                    self.hud.rss_state_visualizer)

    def tick(self, clock):
        self.hud.tick(self.player, clock)

    def toggle_recording(self):
        if not self.recording:
            dir_name = "_out%04d" % self.recording_dir_num
            while os.path.exists(dir_name):
                self.recording_dir_num += 1
                dir_name = "_out%04d" % self.recording_dir_num
            self.recording_frame_num = 0
            os.mkdir(dir_name)
        else:
            self.hud.notification('Recording finished (folder: _out%04d)' %
                                  self.recording_dir_num)

        self.recording = not self.recording

    def render(self, display):
        self.camera.render(display)
        self.rss_bounding_box_visualizer.render(display,
                                                self.camera.current_frame)
        self.rss_unstructured_scene_visualizer.render(display)
        self.hud.render(display)

        if self.recording:
            pygame.image.save(
                display, "_out%04d/%08d.bmp" %
                (self.recording_dir_num, self.recording_frame_num))
            self.recording_frame_num += 1

    def destroy(self):
        # stop from ticking
        if self.world_tick_id:
            self.world.remove_on_tick(self.world_tick_id)

        if self.camera:
            self.camera.destroy()
        if self.rss_sensor:
            self.rss_sensor.destroy()
        if self.rss_unstructured_scene_visualizer:
            self.rss_unstructured_scene_visualizer.destroy()
        if self.player:
            self.player.destroy()