예제 #1
0
    def _Radar_callback(weak_self, radar_data):
        self = weak_self()
        if not self:
            return
        # To get a numpy [[vel, altitude, azimuth, depth],...[,,,]]:
        # points = np.frombuffer(radar_data.raw_data, dtype=np.dtype('f4'))
        # points = np.reshape(points, (len(radar_data), 4))

        current_rot = radar_data.transform.rotation
        for detect in radar_data:
            azi = math.degrees(detect.azimuth)
            alt = math.degrees(detect.altitude)
            # The 0.25 adjusts a bit the distance so the dots can
            # be properly seen
            fw_vec = Carla.Vector3D(x=detect.depth - 0.25)
            Carla.Transform(
                Carla.Location(),
                Carla.Rotation(
                    pitch=current_rot.pitch + alt,
                    yaw=current_rot.yaw + azi,
                    roll=current_rot.roll)).transform(fw_vec)

            def clamp(min_v, max_v, value):
                return max(min_v, min(value, max_v))

            norm_velocity = detect.velocity / self.velocity_range # range [-1, 1]
            r = int(clamp(0.0, 1.0, 1.0 - norm_velocity) * 255.0)
            g = int(clamp(0.0, 1.0, 1.0 - abs(norm_velocity)) * 255.0)
            b = int(abs(clamp(- 1.0, 0.0, - 1.0 - norm_velocity)) * 255.0)
            self.debug.draw_point(
                radar_data.transform.location + fw_vec,
                size=0.075,
                life_time=0.06,
                persistent_lines=False,
                color=Carla.Color(r, g, b))
예제 #2
0
    def __init__(self, world):

        self.world = world

        #Variables related to simulated vehicle
        self.sensors = []
        self.speed = 0
        self.steeringangle = 0
        self.throttle = 0
        self.steering = 0
        self.braking = 0

        #Load Carlas blueprint library
        self.blueprint_library = self.world.get_blueprint_library()

        #Grab all the car blueprints from library
        vehicle = self.blueprint_library.find('vehicle.bmw.isetta')

        #Spawns trike at spawn point
        spawn = Carla.Transform(Carla.Location(x=34, y=7, z=1),
                                Carla.Rotation(yaw=0))
        self.actor = self.world.spawn_actor(vehicle, spawn)

        # Set up the sensors.
        self.GNSSSensor = GNSSSensor(self.world, self.actor)
        self.IMUSensor = IMUSensor(self.world, self.actor)
예제 #3
0
 def __init__(self, parent_actor):
     self.sensor = None
     self._parent = parent_actor
     self.lat = 0.0
     self.lon = 0.0
     world = self._parent.get_world()
     bp = world.get_blueprint_library().find('sensor.other.gnss')
     self.sensor = world.spawn_actor(bp, Carla.Transform(Carla.Location(x=1.0, z=2.8)), attach_to=self._parent)
     # We need to pass the lambda a weak reference to self to avoid circular
     # reference.
     weak_self = weakref.ref(self)
     self.sensor.listen(lambda event: GnssSensor._on_gnss_event(weak_self, event))
예제 #4
0
    def __init__(self, world, actor):

        bp = world.get_blueprint_library().find('sensor.camera.rgb')
        bp.set_attribute('image_size_x', str(IMG_WIDTH))
        bp.set_attribute('image_size_y', str(IMG_HEIGHT))
        bp.set_attribute('fov', '110')
        self.sensor = world.spawn_actor(bp,
                                        Carla.Transform(
                                            Carla.Location(x=2.5, z=0.7)),
                                        attach_to=actor)

        weak_self = weakref.ref(self)
        self.sensor.listen(
            lambda raw_data: RGBSensor._process_rgb(weak_self, raw_data))
예제 #5
0
 def __init__(self, world, start_in_autopilot):
     self._autopilot_enabled = start_in_autopilot
     if isinstance(world.player, Carla.Vehicle):
         self._control = Carla.VehicleControl()
         self._lights = Carla.VehicleLightState.NONE
         world.player.set_autopilot(self._autopilot_enabled)
         world.player.set_light_state(self._lights)
     elif isinstance(world.player, Carla.Walker):
         self._control = Carla.WalkerControl()
         self._autopilot_enabled = False
         self._rotation = world.player.get_transform().rotation
     else:
         raise NotImplementedError("Actor type not supported")
     self._steer_cache = 0.0
     world.hud.notification("Press 'H' or '?' for help.", seconds=4.0)
예제 #6
0
    def __init__(self, world, actor):

        # Init default values
        self.latitude = 0
        self.longitude = 0

        # Attach nmeaSensor to trike (speed directly taken from trike actor)
        blueprint = world.get_blueprint_library().find('sensor.other.gnss')
        transform = Carla.Transform(Carla.Location(x=1.0, z=2.8))
        self.sensor = world.spawn_actor(blueprint, transform, attach_to=actor)

        #To avoid circular ref we are going to use a weak reference.
        weak_self = weakref.ref(self)
        self.sensor.listen(
            lambda event: GNSSSensor._on_gnss_event(weak_self, event))
예제 #7
0
    def updateSteering(self, s):

        self.steering = s
        self.actor.apply_control(
            Carla.VehicleControl(throttle=self.throttle,
                                 steer=self.steering,
                                 brake=self.braking))
예제 #8
0
 def updateReverse(self, r):
     self.reverse = r
     self.actor.apply_control(
         Carla.VehicleControl(throttle=self.throttle,
                              steer=self.steering,
                              brake=self.braking,
                              reverse=self.reverse))
예제 #9
0
def game_loop(args):
    pygame.init()
    pygame.font.init()
    world = None

    try:
        client = Carla.Client(args.host, args.port)
        client.set_timeout(2.0)

        display = pygame.display.set_mode(
            (args.width, args.height),
            pygame.HWSURFACE | pygame.DOUBLEBUF)

        hud = HUD(args.width, args.height)
        world = World(client.get_world(), hud, args)
        controller = KeyboardControl(world, args.autopilot)

        clock = pygame.time.Clock()
        while True:
            clock.tick_busy_loop(60)
            if controller.parse_events(client, world, clock):
                return
            world.tick(clock)
            world.render(display)
            pygame.display.flip()

    finally:

        if (world and world.recording_enabled):
            client.stop_recorder()

        if world is not None:
            world.destroy()

        pygame.quit()
예제 #10
0
    def __init__(self, host, port):

        # Initialize some variables
        self.camera_manager = None
        self.vehicle = None

        # Attempt to connect to the Carla server, timeout after 5 seconds
        self.client = Carla.Client(host, port)
        self.client.set_timeout(5.0)

        # Start pygame and build window and hud
        pygame.init()
        pygame.font.init()

        self.display = pygame.display.set_mode(
            (1280, 720), pygame.HWSURFACE | pygame.DOUBLEBUF)
        self.clock = pygame.time.Clock()
        self.hud = HUD(1280, 720)

        # Get the world from the server
        self.world = self.client.get_world()
        self.world.on_tick(self.hud.on_world_tick)

        #vehicle states
        self.states = {'autopilot_engaged' : False,
                         'reverse_engaged' : False,
                         'destination_set' : False}
예제 #11
0
 def __init__(self, parent_actor):
     self.sensor = None
     self._parent = parent_actor
     self.velocity_range = 7.5 # m/s
     world = self._parent.get_world()
     self.debug = world.debug
     bp = world.get_blueprint_library().find('sensor.other.radar')
     bp.set_attribute('horizontal_fov', str(35))
     bp.set_attribute('vertical_fov', str(20))
     self.sensor = world.spawn_actor(
         bp,
         Carla.Transform(
             Carla.Location(x=2.8, z=1.0),
             Carla.Rotation(pitch=5)),
         attach_to=self._parent)
     # We need a weak reference to self to avoid circular reference.
     weak_self = weakref.ref(self)
     self.sensor.listen(
         lambda radar_data: RadarSensor._Radar_callback(weak_self, radar_data))
예제 #12
0
 def __init__(self, parent_actor, hud):
     self.sensor = None
     self._parent = parent_actor
     self.hud = hud
     world = self._parent.get_world()
     bp = world.get_blueprint_library().find('sensor.other.lane_invasion')
     self.sensor = world.spawn_actor(bp, Carla.Transform(), attach_to=self._parent)
     # We need to pass the lambda a weak reference to self to avoid circular
     # reference.
     weak_self = weakref.ref(self)
     self.sensor.listen(lambda event: LaneInvasionSensor._on_invasion(weak_self, event))
예제 #13
0
 def restart(self):
     self.player_max_speed = 1.589
     self.player_max_speed_fast = 3.713
     # Keep same camera config if the camera manager exists.
     cam_index = self.camera_manager.index if self.camera_manager is not None else 0
     cam_pos_index = self.camera_manager.transform_index if self.camera_manager is not None else 0
     # 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')
     # set the max speed
     if blueprint.has_attribute('speed'):
         self.player_max_speed = float(blueprint.get_attribute('speed').recommended_values[1])
         self.player_max_speed_fast = float(blueprint.get_attribute('speed').recommended_values[2])
     else:
         print("No recommended values for 'speed' attribute")
     # 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)
     # Set up the sensors.
     self.collision_sensor = CollisionSensor(self.player, self.hud)
     self.lane_invasion_sensor = LaneInvasionSensor(self.player, self.hud)
     self.gnss_sensor = GnssSensor(self.player)
     self.imu_sensor = IMUSensor(self.player)
     self.camera_manager = CameraManager(self.player, self.hud, self._gamma)
     self.camera_manager.transform_index = cam_pos_index
     self.camera_manager.set_sensor(cam_index, notify=False)
     actor_type = get_actor_display_name(self.player)
     self.hud.notification(actor_type)
예제 #14
0
 def __init__(self, parent_actor):
     self.sensor = None
     self._parent = parent_actor
     self.accelerometer = (0.0, 0.0, 0.0)
     self.gyroscope = (0.0, 0.0, 0.0)
     self.compass = 0.0
     world = self._parent.get_world()
     bp = world.get_blueprint_library().find('sensor.other.imu')
     self.sensor = world.spawn_actor(
         bp, Carla.Transform(), attach_to=self._parent)
     # We need to pass the lambda a weak reference to self to avoid circular
     # reference.
     weak_self = weakref.ref(self)
     self.sensor.listen(
         lambda sensor_data: IMUSensor._IMU_callback(weak_self, sensor_data))
예제 #15
0
    def __init__(self, world, actor):

        #Default values
        self.accelerometer = (0.0, 0.0, 0.0)
        self.gyroscope = (0.0, 0.0, 0.0)
        self.compass = 0.0
        self.radiansCompass = 0.0

        # Attach the sensor to the vehicle
        bp = world.get_blueprint_library().find('sensor.other.imu')
        self.sensor = world.spawn_actor(bp, Carla.Transform(), attach_to=actor)

        # We need to pass the lambda a weak reference to self to avoid circular
        # reference.
        weak_self = weakref.ref(self)
        self.sensor.listen(lambda sensor_data: IMUSensor._IMU_callback(
            weak_self, sensor_data))
예제 #16
0
    def __init__(self, actor, world, hud, gamma_correction):
        self.sensor = None
        self.surface = None
        self.actor = actor
        self.hud = hud
        self.recording = False

        bound_y = 0.5 + self.actor.bounding_box.extent.y
        Attachment = Carla.AttachmentType

        self._camera_transforms = [
            (Carla.Transform(Carla.Location(x=-5.5, z=2.5),
                             Carla.Rotation(pitch=8.0)), Attachment.SpringArm),
            (Carla.Transform(Carla.Location(x=1.6, z=1.7)), Attachment.Rigid),
            (Carla.Transform(Carla.Location(x=5.5, y=1.5, z=1.5)), Attachment.SpringArm),
            (Carla.Transform(Carla.Location(x=-8.0, z=6.0),
                             Carla.Rotation(pitch=6.0)), Attachment.SpringArm),
            (Carla.Transform(Carla.Location(x=-1, y=-bound_y, z=0.5)), Attachment.Rigid)]
        self.transform_index = 1
        self.sensors = [
            ['sensor.camera.rgb', cc.Raw, 'Camera RGB', {}],
            ['sensor.camera.depth', cc.Raw, 'Camera Depth (Raw)', {}],
            ['sensor.camera.depth', cc.Depth, 'Camera Depth (Gray Scale)', {}],
            ['sensor.camera.depth', cc.LogarithmicDepth,
                'Camera Depth (Logarithmic Gray Scale)', {}],
            ['sensor.camera.semantic_segmentation', cc.Raw,
                'Camera Semantic Segmentation (Raw)', {}],
            ['sensor.camera.semantic_segmentation', cc.CityScapesPalette,
                'Camera Semantic Segmentation (CityScapes Palette)', {}],
            ['sensor.lidar.ray_cast', None, 'Lidar (Ray-Cast)', {}],
            ['sensor.camera.rgb', cc.Raw, 'Camera RGB Distorted',
                {'lens_circle_multiplier': '3.0',
                 'lens_circle_falloff': '3.0',
                 'chromatic_aberration_intensity': '0.5',
                 'chromatic_aberration_offset': '0'}]]
        bp_library = world.get_blueprint_library()

        for item in self.sensors:
            bp = bp_library.find(item[0])
            if item[0].startswith('sensor.camera'):
                bp.set_attribute('image_size_x', str(hud.dim[0]))
                bp.set_attribute('image_size_y', str(hud.dim[1]))
                if bp.has_attribute('gamma'):
                    bp.set_attribute('gamma', str(gamma_correction))
                for attr_name, attr_value in item[3].items():
                    bp.set_attribute(attr_name, attr_value)
            elif item[0].startswith('sensor.lidar'):
                bp.set_attribute('range', '50')
            item.append(bp)

        self.index = None
예제 #17
0
    def parse_events(self, client, world, clock):
        if isinstance(self._control, Carla.VehicleControl):
            current_lights = self._lights
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return True
            elif event.type == pygame.KEYUP:
                if self._is_quit_shortcut(event.key):
                    return True
                elif event.key == K_BACKSPACE:
                    if self._autopilot_enabled:
                        world.player.set_autopilot(False)
                        world.restart()
                        world.player.set_autopilot(True)
                    else:
                        world.restart()
                elif event.key == K_F1:
                    world.hud.toggle_info()
                elif event.key == K_h or (event.key == K_SLASH and pygame.key.get_mods() & KMOD_SHIFT):
                    world.hud.help.toggle()
                elif event.key == K_TAB:
                    world.camera_manager.toggle_camera()
                elif event.key == K_c and pygame.key.get_mods() & KMOD_SHIFT:
                    world.next_weather(reverse=True)
                elif event.key == K_c:
                    world.next_weather()
                elif event.key == K_g:
                    world.toggle_radar()
                elif event.key == K_BACKQUOTE:
                    world.camera_manager.next_sensor()
                elif event.key == K_n:
                    world.camera_manager.next_sensor()
                elif event.key > K_0 and event.key <= K_9:
                    world.camera_manager.set_sensor(event.key - 1 - K_0)
                elif event.key == K_r and not (pygame.key.get_mods() & KMOD_CTRL):
                    world.camera_manager.toggle_recording()
                elif event.key == K_r and (pygame.key.get_mods() & KMOD_CTRL):
                    if (world.recording_enabled):
                        client.stop_recorder()
                        world.recording_enabled = False
                        world.hud.notification("Recorder is OFF")
                    else:
                        client.start_recorder("manual_recording.rec")
                        world.recording_enabled = True
                        world.hud.notification("Recorder is ON")
                elif event.key == K_p and (pygame.key.get_mods() & KMOD_CTRL):
                    # stop recorder
                    client.stop_recorder()
                    world.recording_enabled = False
                    # work around to fix camera at start of replaying
                    current_index = world.camera_manager.index
                    world.destroy_sensors()
                    # disable autopilot
                    self._autopilot_enabled = False
                    world.player.set_autopilot(self._autopilot_enabled)
                    world.hud.notification("Replaying file 'manual_recording.rec'")
                    # replayer
                    client.replay_file("manual_recording.rec", world.recording_start, 0, 0)
                    world.camera_manager.set_sensor(current_index)
                elif event.key == K_MINUS and (pygame.key.get_mods() & KMOD_CTRL):
                    if pygame.key.get_mods() & KMOD_SHIFT:
                        world.recording_start -= 10
                    else:
                        world.recording_start -= 1
                    world.hud.notification("Recording start time is %d" % (world.recording_start))
                elif event.key == K_EQUALS and (pygame.key.get_mods() & KMOD_CTRL):
                    if pygame.key.get_mods() & KMOD_SHIFT:
                        world.recording_start += 10
                    else:
                        world.recording_start += 1
                    world.hud.notification("Recording start time is %d" % (world.recording_start))
                if isinstance(self._control, Carla.VehicleControl):
                    if event.key == K_q:
                        self._control.gear = 1 if self._control.reverse else -1
                    elif event.key == K_m:
                        self._control.manual_gear_shift = not self._control.manual_gear_shift
                        self._control.gear = world.player.get_control().gear
                        world.hud.notification('%s Transmission' %
                                               ('Manual' if self._control.manual_gear_shift else 'Automatic'))
                    elif self._control.manual_gear_shift and event.key == K_COMMA:
                        self._control.gear = max(-1, self._control.gear - 1)
                    elif self._control.manual_gear_shift and event.key == K_PERIOD:
                        self._control.gear = self._control.gear + 1
                    elif event.key == K_p and not pygame.key.get_mods() & KMOD_CTRL:
                        self._autopilot_enabled = not self._autopilot_enabled
                        world.player.set_autopilot(self._autopilot_enabled)
                        world.hud.notification(
                            'Autopilot %s' % ('On' if self._autopilot_enabled else 'Off'))
                    elif event.key == K_l and pygame.key.get_mods() & KMOD_CTRL:
                        current_lights ^= Carla.VehicleLightState.Special1
                    elif event.key == K_l and pygame.key.get_mods() & KMOD_SHIFT:
                        current_lights ^= Carla.VehicleLightState.HighBeam
                    elif event.key == K_l:
                        # Use 'L' key to switch between lights:
                        # closed -> position -> low beam -> fog
                        if not self._lights & Carla.VehicleLightState.Position:
                            world.hud.notification("Position lights")
                            current_lights |= Carla.VehicleLightState.Position
                        else:
                            world.hud.notification("Low beam lights")
                            current_lights |= Carla.VehicleLightState.LowBeam
                        if self._lights & Carla.VehicleLightState.LowBeam:
                            world.hud.notification("Fog lights")
                            current_lights |= Carla.VehicleLightState.Fog
                        if self._lights & Carla.VehicleLightState.Fog:
                            world.hud.notification("Lights off")
                            current_lights ^= Carla.VehicleLightState.Position
                            current_lights ^= Carla.VehicleLightState.LowBeam
                            current_lights ^= Carla.VehicleLightState.Fog
                    elif event.key == K_i:
                        current_lights ^= Carla.VehicleLightState.Interior
                    elif event.key == K_z:
                        current_lights ^= Carla.VehicleLightState.LeftBlinker
                    elif event.key == K_x:
                        current_lights ^= Carla.VehicleLightState.RightBlinker

        if not self._autopilot_enabled:
            if isinstance(self._control, Carla.VehicleControl):
                self._parse_vehicle_keys(pygame.key.get_pressed(), clock.get_time())
                self._control.reverse = self._control.gear < 0
                # Set automatic control-related vehicle lights
                if self._control.brake:
                    current_lights |= Carla.VehicleLightState.Brake
                else: # Remove the Brake flag
                    current_lights &= Carla.VehicleLightState.All ^ Carla.VehicleLightState.Brake
                if self._control.reverse:
                    current_lights |= Carla.VehicleLightState.Reverse
                else: # Remove the Reverse flag
                    current_lights &= Carla.VehicleLightState.All ^ Carla.VehicleLightState.Reverse
                if current_lights != self._lights: # Change the light state only if necessary
                    self._lights = current_lights
                    world.player.set_light_state(Carla.VehicleLightState(self._lights))
            elif isinstance(self._control, Carla.WalkerControl):
                self._parse_walker_keys(pygame.key.get_pressed(), clock.get_time(), world)
            world.player.apply_control(self._control)
예제 #18
0
 def updateThrottle(self, t):
     self.throttle = t
     self.actor.apply_control(
         Carla.VehicleControl(throttle=self.throttle,
                              steer=self.steering,
                              brake=self.braking))