def __init__(self, config=None):
        if config is None:
            config = ENV_CONFIG
        self.environment_config = config
        carla_server_binary = add_carla_path(
            ENV_CONFIG["CARLA_PATH_CONFIG_FILE"])
        self.environment_config.update({"SERVER_BINARY": carla_server_binary})
        module = __import__("experiments.{}".format(
            self.environment_config["Experiment"]))
        exec("self.experiment = module.{}.Experiment()".format(
            self.environment_config["Experiment"]))
        self.action_space = self.experiment.get_action_space()
        self.observation_space = self.experiment.get_observation_space()
        self.experiment_config = self.experiment.get_experiment_config()

        self.core = CarlaCore(self.environment_config, self.experiment_config)
        self.experiment.spawn_actors(self.core)
        self.experiment.initialize_reward(self.core)
        self.reset()
def main():
    actor_list = []
    pygame.init()

    display = pygame.display.set_mode((900, 1200),
                                      pygame.HWSURFACE | pygame.DOUBLEBUF)
    font = get_font()
    clock = pygame.time.Clock()

    env_config = ENV_CONFIG
    carla_server_binary = add_carla_path(ENV_CONFIG["CARLA_PATH_CONFIG_FILE"])
    env_config.update({"SERVER_BINARY": carla_server_binary})

    experiment = Experiment()
    action_space = experiment.get_action_space()
    observation_space = experiment.get_observation_space()
    exp_config = experiment.get_experiment_config()
    CarlaCore(environment_config=env_config,
              experiment_config=exp_config,
              core_config=None)
    time.sleep(10)

    client = carla.Client('localhost', 2000)
    client.set_timeout(2.0)
    print(client)
    print("server_version", client.get_server_version())
    print("client_version", client.get_client_version())

    world = client.get_world()

    try:
        m = world.get_map()
        start_pose = random.choice(m.get_spawn_points())
        waypoint = m.get_waypoint(start_pose.location)

        blueprint_library = world.get_blueprint_library()

        vehicle = world.spawn_actor(
            random.choice(blueprint_library.filter('vehicle.*')), start_pose)
        actor_list.append(vehicle)
        vehicle.set_simulate_physics(False)

        camera_rgb = world.spawn_actor(
            blueprint_library.find('sensor.camera.rgb'),
            carla.Transform(carla.Location(x=-5.5, z=2.8),
                            carla.Rotation(pitch=-15)),
            attach_to=vehicle)
        actor_list.append(camera_rgb)

        camera_semseg = world.spawn_actor(
            blueprint_library.find('sensor.camera.semantic_segmentation'),
            carla.Transform(carla.Location(x=-5.5, z=2.8),
                            carla.Rotation(pitch=-15)),
            attach_to=vehicle)
        actor_list.append(camera_semseg)

        # Create a synchronous mode context.
        with CarlaSyncMode(world, camera_rgb, camera_semseg,
                           fps=50) as sync_mode:
            while True:
                if should_quit():
                    return
                clock.tick()
                t = time.time()

                # Advance the simulation and wait for the data.
                snapshot, image_rgb, image_semseg = sync_mode.tick(timeout=2.0)

                # Choose the next waypoint and update the car location.
                waypoint = random.choice(waypoint.next(1.5))
                vehicle.set_transform(waypoint.transform)

                image_semseg.convert(carla.ColorConverter.CityScapesPalette)
                fps = round(1.0 / snapshot.timestamp.delta_seconds)

                # Draw the display.
                draw_image(display, image_rgb)
                draw_image(display, image_semseg, blend=True)
                display.blit(
                    font.render('% 5d FPS (real)' % clock.get_fps(), True,
                                (255, 255, 255)), (8, 10))
                display.blit(
                    font.render('% 5d FPS (simulated)' % fps, True,
                                (255, 255, 255)), (8, 28))
                pygame.display.flip()
                elapsed = time.time() - t
                print("Elapsed (ms):{:0.2f}".format(elapsed * 1000))

    finally:

        print('destroying actors.')
        for actor in actor_list:
            actor.destroy()

        pygame.quit()
        print('done.')
# For a copy, see <https://opensource.org/licenses/MIT>.

import glob
import os
import sys
from helper.CarlaHelper import add_carla_path
import time

ENV_CONFIG = {
    "RAY": True,  # True if you are  running an experiment in Ray
    "DEBUG_MODE": True,
    "CARLA_PATH_CONFIG_FILE":
    "CARLA_PATH.txt",  # IN this file, put the path to your CARLA FOLDER
}

CARLA_SERVER_BINARY = add_carla_path(ENV_CONFIG["CARLA_PATH_CONFIG_FILE"])
ENV_CONFIG.update({"SERVER_BINARY": CARLA_SERVER_BINARY})

import carla

from core.CarlaCore2 import CarlaCore
from experiments.experiment1 import Experiment

import random

try:
    import pygame
except ImportError:
    raise RuntimeError(
        'cannot import pygame, make sure pygame package is installed')
Example #4
0
def main():

    env_config = ENV_CONFIG
    carla_server_binary = add_carla_path(ENV_CONFIG["CARLA_PATH_CONFIG_FILE"])
    env_config.update({"SERVER_BINARY": carla_server_binary})

    experiment = Experiment()
    action_space = experiment.get_action_space()
    observation_space = experiment.get_observation_space()
    exp_config = experiment.get_experiment_config()
    CarlaCore(environment_config=env_config,
              experiment_config=exp_config,
              core_config=None)
    time.sleep(5)

    argparser = argparse.ArgumentParser(
        description='CARLA Manual Control Client')
    argparser.add_argument('-v',
                           '--verbose',
                           action='store_true',
                           dest='debug',
                           help='print debug information')
    argparser.add_argument('--host',
                           metavar='H',
                           default='127.0.0.1',
                           help='IP of the host server (default: 127.0.0.1)')
    argparser.add_argument('-p',
                           '--port',
                           metavar='P',
                           default=2000,
                           type=int,
                           help='TCP port to listen to (default: 2000)')
    argparser.add_argument('-a',
                           '--autopilot',
                           action='store_true',
                           help='enable autopilot')
    argparser.add_argument('--res',
                           metavar='WIDTHxHEIGHT',
                           default='1280x720',
                           help='window resolution (default: 1280x720)')
    argparser.add_argument('--filter',
                           metavar='PATTERN',
                           default='vehicle.*',
                           help='actor filter (default: "vehicle.*")')
    argparser.add_argument('--rolename',
                           metavar='NAME',
                           default='hero',
                           help='actor role name (default: "hero")')
    argparser.add_argument(
        '--gamma',
        default=2.2,
        type=float,
        help='Gamma correction of the camera (default: 2.2)')
    args = argparser.parse_args()

    args.width, args.height = [int(x) for x in args.res.split('x')]

    log_level = logging.DEBUG if args.debug else logging.INFO
    logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)

    logging.info('listening to server %s:%s', args.host, args.port)

    print(__doc__)

    try:

        game_loop(args)

    except KeyboardInterrupt:
        print('\nCancelled by user. Bye!')