#32 Switch carla_manual_control to new API

This commit is contained in:
nsubiron 2017-11-18 11:51:23 +01:00
parent 55ae048582
commit 1b9029b056
1 changed files with 35 additions and 15 deletions

View File

@ -39,9 +39,12 @@ import matplotlib.pyplot as plt
import pygame import pygame
from pygame.locals import * from pygame.locals import *
from carla import CARLA from carla.client import CarlaClient
from carla import Control
try:
from carla.carla_server_pb2 import Control
except ImportError:
raise RuntimeError('cannot import "carla_server_pb2.py", run the protobuf compiler to generate this file')
def join_classes(labels_image): def join_classes(labels_image):
@ -84,6 +87,12 @@ def convert_depth(depth):
color_depth = grayscale_colormap(gray_depth, 'jet') * 255 color_depth = grayscale_colormap(gray_depth, 'jet') * 255
return color_depth return color_depth
def get_image_array(image):
new_image = np.frombuffer(image.raw, dtype=np.dtype("uint8"))
return [np.reshape(new_image, (image.height, image.width, 4))]
if sys.version_info >= (3, 3): if sys.version_info >= (3, 3):
import shutil import shutil
@ -109,7 +118,8 @@ class App(object):
self._display_surf = None self._display_surf = None
self.port = port self.port = port
self.host = host self.host = host
self.config = config with open(config, 'r') as fp:
self.config = fp.read()
self.verbose = verbose self.verbose = verbose
self.resolution = resolution self.resolution = resolution
self.size = self.weight, self.height = resolution self.size = self.weight, self.height = resolution
@ -127,12 +137,20 @@ class App(object):
self.prev_step = 0 self.prev_step = 0
self.prev_time = time.time() self.prev_time = time.time()
self.carla = CARLA(self.host, self.port) self.carla = CarlaClient(self.host, self.port)
while True:
try:
self.carla.connect()
break
except Exception as e:
logging.error("Cannot connect: %s", e)
time.sleep(1)
positions = self.carla.loadConfigurationFile(self.config) scene = self.carla.request_new_episode(self.config)
self.num_pos = len(positions) self.num_pos = len(scene.player_start_spots)
print("Staring Episode on Position ", self.num_pos) player_start = np.random.randint(self.num_pos)
self.carla.newEpisode(np.random.randint(self.num_pos)) print("Starting episode...")
self.carla.start_episode(player_start)
self.prev_restart_time = time.time() self.prev_restart_time = time.time()
def on_event(self, event): def on_event(self, event):
@ -176,13 +194,15 @@ class App(object):
control.steer = 0.0 control.steer = 0.0
control.reverse = self.reverse_gear control.reverse = self.reverse_gear
self.carla.sendCommand(control)
measurements = self.carla.getMeasurements() measurements, images = self.carla.read_measurements()
pack = measurements['PlayerMeasurements']
self.img_vec = measurements['BGRA'] self.carla.send_control(control)
self.depth_vec = measurements['Depth']
self.labels_vec = measurements['Labels'] pack = measurements.player_measurements
self.img_vec = get_image_array(images[0])
self.depth_vec = get_image_array(images[1])
self.labels_vec = get_image_array(images[2])
if time.time() - self.prev_time > 1.: if time.time() - self.prev_time > 1.:
message = 'Step {step} ({fps:.1f} FPS): ' message = 'Step {step} ({fps:.1f} FPS): '
@ -255,7 +275,7 @@ class App(object):
pygame.display.flip() pygame.display.flip()
def on_cleanup(self): def on_cleanup(self):
self.carla.closeConections() self.carla.disconnect()
pygame.quit() pygame.quit()
def on_execute(self): def on_execute(self):