class App(object): def __init__( self, port=2000, host='127.0.0.1', config='./CarlaSettings.ini', resolution=(2400, 600), verbose=True): self._running = True self._display_surf = None self.port = port self.host = host self.config = config self.verbose = verbose self.resolution = resolution self.size = self.weight, self.height = resolution self.reverse_gear = False def on_init(self): pygame.init() print(__doc__) time.sleep(3) self._display_surf = pygame.display.set_mode( self.size, pygame.HWSURFACE | pygame.DOUBLEBUF) logging.debug('Started the PyGame Library') self._running = True self.step = 0 self.prev_step = 0 self.prev_time = time.time() self.carla = CARLA(self.host, self.port) positions = self.carla.loadConfigurationFile(self.config) self.num_pos = len(positions) print("Staring Episode on Position ", self.num_pos) self.carla.newEpisode(np.random.randint(self.num_pos)) self.prev_restart_time = time.time() # Adding Joystick controls here self.js = pygame.joystick.Joystick(0) self.js.init() axis = self.js.get_axis(1) jsInit = self.js.get_init() jsId = self.js.get_id() print("Joystick ID: %d Init status: %s Axis(1): %d" % (jsId, jsInit, axis)) def on_event(self, event): if event.type == pygame.QUIT: self._running = False def on_loop(self): self.step += 1 keys = pygame.key.get_pressed() numAxes = self.js.get_numaxes() jsInputs = [ float(self.js.get_axis(i)) for i in range(numAxes)] print('Js inputs [%s]' % ', '.join(map(str, jsInputs))) restart = False control = Control() pressed_keys = [] if keys[K_LEFT] or keys[K_a]: control.steer = -1.0 pressed_keys.append('left') if keys[K_RIGHT] or keys[K_d]: control.steer = 1.0 pressed_keys.append('right') if keys[K_UP] or keys[K_w]: control.throttle = 1.0 pressed_keys.append('reverse' if self.reverse_gear else 'forward') if keys[K_DOWN] or keys[K_s]: control.brake = 1.0 pressed_keys.append('brake') if keys[K_SPACE]: control.hand_brake = True pressed_keys.append('hand-brake') if keys[K_q]: self.reverse_gear = not self.reverse_gear pressed_keys.append('toggle reverse') if keys[K_r]: pressed_keys.append('reset') if time.time() - self.prev_restart_time > 2.: self.prev_restart_time = time.time() restart = True if time.time() - self.prev_restart_time < 2.: control.throttle = 0.0 control.steer = 0.0 control.steer = jsInputs[0] brakeCmd = (((jsInputs[1] - (-1)) * (1.0 - 0)) / (1.0 - (-1.0))) + 0 throttleCmd = (((jsInputs[2] - (-1)) * (1.0 - 0)) / (1.0 - (-1.0))) + 0 control.brake = brakeCmd control.throttle = throttleCmd control.reverse = self.reverse_gear self.carla.sendCommand(control) measurements = self.carla.getMeasurements() pack = measurements['PlayerMeasurements'] self.img_vec = measurements['BGRA'] self.depth_vec = measurements['Depth'] self.labels_vec = measurements['Labels'] if time.time() - self.prev_time > 1.: message = 'Step {step} ({fps:.1f} FPS): ' message += '{speed:.2f} km/h, ' message += '{other_lane:.0f}% other lane, {offroad:.0f}% off-road' message += ': pressed [%s]' % ', '.join(pressed_keys) message = message.format( step=self.step, fps=float(self.step - self.prev_step) / (time.time() - self.prev_time), speed=pack.forward_speed, other_lane=100 * pack.intersection_otherlane, offroad=100 * pack.intersection_offroad) empty_space = max(0, get_terminal_width() - len(message)) sys.stdout.write('\r' + message + empty_space * ' ') sys.stdout.flush() self.prev_step = self.step self.prev_time = time.time() if restart: print('\n *** RESTART *** \n') player_pos = np.random.randint(self.num_pos) print(' Player pos %d \n' % (player_pos)) self.carla.newEpisode(player_pos) def on_render(self): """ The render method plots the First RGB, the First Depth and First Semantic Segmentation Camera. """ pos_x = 0 if self.depth_vec: self.depth_vec[0] = self.depth_vec[0][:, :, :3] self.depth_vec[0] = self.depth_vec[0][:, :, ::-1] self.depth_vec[0] = convert_depth(self.depth_vec[0]) surface = pygame.surfarray.make_surface( np.transpose(self.depth_vec[0], (1, 0, 2))) self._display_surf.blit(surface, (pos_x, 0)) pos_x += self.depth_vec[0].shape[1] if self.img_vec: self.img_vec[0] = self.img_vec[0][:, :, :3] self.img_vec[0] = self.img_vec[0][:, :, ::-1] surface = pygame.surfarray.make_surface( np.transpose(self.img_vec[0], (1, 0, 2))) self._display_surf.blit(surface, (pos_x, 0)) pos_x += self.img_vec[0].shape[1] if self.labels_vec: self.labels_vec[0] = join_classes(self.labels_vec[0][:, :, 2]) surface = pygame.surfarray.make_surface( np.transpose(self.labels_vec[0], (1, 0, 2))) self._display_surf.blit(surface, (pos_x, 0)) pos_x += self.labels_vec[0].shape[1] pygame.display.flip() def on_cleanup(self): self.carla.closeConections() pygame.quit() def on_execute(self): if self.on_init() == False: self._running = False while(self._running): try: for event in pygame.event.get(): self.on_event(event) self.on_loop() self.on_render() except Exception as e: logging.exception(e) self._running = False break self.on_cleanup()
class App: def __init__(self, port=2000, host='127.0.0.1', config='./CarlaSettings.ini',\ resolution=(2400,600),verbose=True): self._running = True self._display_surf = None self.port = port self.host = host self.config = config self.verbose = verbose self.resolution = resolution self.size = self.weight, self.height = resolution def on_init(self): pygame.init() print (" \n \n \n Welcome to CARLA manual control \n USE ARROWS for control \n Press R for reset \n"\ +"STARTING in a few seconds...") time.sleep(3) self._display_surf = pygame.display.set_mode( self.size, pygame.HWSURFACE | pygame.DOUBLEBUF) logging.debug('Started the PyGame Library') self._running = True self.step = 0 self.prev_step = 0 self.prev_time = time.time() self.carla = CARLA(self.host, self.port) positions = self.carla.loadConfigurationFile(self.config) self.num_pos = len(positions) print("Staring Episode on Position ", self.num_pos) self.carla.newEpisode(np.random.randint(self.num_pos)) self.prev_restart_time = time.time() def on_event(self, event): if event.type == pygame.QUIT: self._running = False def on_loop(self): self.step += 1 keys = pygame.key.get_pressed() gas = 0 steer = 0 restart = False pressed_keys = [] if keys[K_LEFT]: steer = -1. pressed_keys.append('left') if keys[K_RIGHT]: pressed_keys.append('right') steer = 1. if keys[K_UP]: pressed_keys.append('up') gas = 1. if keys[K_DOWN]: pressed_keys.append('down') gas = -1. if keys[K_r]: pressed_keys.append('r') if time.time() - self.prev_restart_time > 2.: self.prev_restart_time = time.time() restart = True if time.time() - self.prev_restart_time < 2.: gas = 0. steer = 0. control = Control() control.throttle = gas control.steer = steer self.carla.sendCommand(control) measurements = self.carla.getMeasurements() pack = measurements['PlayerMeasurements'] self.img_vec = measurements['BGRA'] self.depth_vec = measurements['Depth'] self.labels_vec = measurements['Labels'] if time.time() - self.prev_time > 1.: print( 'Step', self.step, 'FPS', float(self.step - self.prev_step) / (time.time() - self.prev_time)) print('speed', pack.forward_speed, 'collision', pack.collision_other, \ 'collision_car', pack.collision_vehicles, 'colision_ped', pack.collision_pedestrians, 'pressed:', pressed_keys) self.prev_step = self.step self.prev_time = time.time() if restart: print('\n *** RESTART *** \n') player_pos = np.random.randint(self.num_pos) print(' Player pos %d \n' % (player_pos)) self.carla.newEpisode(player_pos) """ The render method plots the First RGB, the First Depth and First Semantic Segmentation Camera """ def on_render(self): pos_x = 0 if len(self.depth_vec) > 0: self.depth_vec[0] = self.depth_vec[0][:, :, :3] self.depth_vec[0] = self.depth_vec[0][:, :, ::-1] self.depth_vec[0] = convert_depth(self.depth_vec[0]) surface = pygame.surfarray.make_surface( np.transpose(self.depth_vec[0], (1, 0, 2))) self._display_surf.blit(surface, (pos_x, 0)) pos_x += self.depth_vec[0].shape[1] if len(self.img_vec) > 0: self.img_vec[0] = self.img_vec[0][:, :, :3] self.img_vec[0] = self.img_vec[0][:, :, ::-1] surface = pygame.surfarray.make_surface( np.transpose(self.img_vec[0], (1, 0, 2))) self._display_surf.blit(surface, (pos_x, 0)) pos_x += self.img_vec[0].shape[1] if len(self.labels_vec) > 0: self.labels_vec[0] = join_classes(self.labels_vec[0][:, :, 2]) surface = pygame.surfarray.make_surface( np.transpose(self.labels_vec[0], (1, 0, 2))) self._display_surf.blit(surface, (pos_x, 0)) pos_x += self.labels_vec[0].shape[1] pygame.display.flip() def on_cleanup(self): self.carla.closeConections() pygame.quit() def on_execute(self): if self.on_init() == False: self._running = False frequency = 10 rate = rospy.Rate(frequency) while not rospy.is_shutdown(): try: for event in pygame.event.get(): self.on_event(event) self.on_loop() self.on_render() rate.sleep() except Exception as e: logging.exception(e) self._running = False break self.on_cleanup()
class App: def __init__(self, port=2000, host='127.0.0.1', config='./CarlaSettings.ini',\ resolution=(2400,600),verbose=True): self._running = True self._display_surf = None self.port = port self.host = host self.config = config self.verbose = verbose self.resolution = resolution self.size = self.weight, self.height = resolution def on_init(self): pygame.init() print (" \n \n \n Welcome to CARLA manual control \n USE ARROWS for control \n Press R for reset \n"\ +"STARTING in a few seconds...") time.sleep(3) self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF) logging.debug('Started the PyGame Library') self._running = True self.step = 0 self.prev_step = 0 self.prev_time = time.time() self.carla =CARLA(self.host, self.port) positions = self.carla.loadConfigurationFile(self.config) self.num_pos = len(positions) print ("Staring Episode on Position ",self.num_pos) self.carla.newEpisode(np.random.randint(self.num_pos)) self.prev_restart_time = time.time() def on_event(self, event): if event.type == pygame.QUIT: self._running = False def on_loop(self): self.step += 1 keys=pygame.key.get_pressed() gas = 0 steer = 0 restart = False pressed_keys = [] if keys[K_LEFT]: steer = -1. pressed_keys.append('left') if keys[K_RIGHT]: pressed_keys.append('right') steer = 1. if keys[K_UP]: pressed_keys.append('up') gas = 1. if keys[K_DOWN]: pressed_keys.append('down') gas = -1. if keys[K_r]: pressed_keys.append('r') if time.time() - self.prev_restart_time > 2.: self.prev_restart_time = time.time() restart = True if time.time() - self.prev_restart_time < 2.: gas = 0. steer = 0. control = Control() control.throttle = gas control.steer = steer self.carla.sendCommand(control) measurements = self.carla.getMeasurements() pack = measurements['PlayerMeasurements'] self.img_vec = measurements['BGRA'] self.depth_vec = measurements['Depth'] self.labels_vec = measurements['Labels'] if time.time() - self.prev_time > 1.: print('Step', self.step, 'FPS', float(self.step - self.prev_step) / (time.time() - self.prev_time)) print('speed', pack.forward_speed, 'collision', pack.collision_other, \ 'collision_car', pack.collision_vehicles, 'colision_ped', pack.collision_pedestrians, 'pressed:', pressed_keys) self.prev_step = self.step self.prev_time = time.time() if restart: print('\n *** RESTART *** \n') player_pos = np.random.randint(self.num_pos) print(' Player pos %d \n' % (player_pos)) self.carla.newEpisode(player_pos) """ The render method plots the First RGB, the First Depth and First Semantic Segmentation Camera """ def on_render(self): pos_x =0 if len(self.depth_vec) > 0: self.depth_vec[0] = self.depth_vec[0][:,:,:3] self.depth_vec[0] = self.depth_vec[0][:,:,::-1] self.depth_vec[0] = convert_depth(self.depth_vec[0]) surface = pygame.surfarray.make_surface(np.transpose(self.depth_vec[0], (1,0,2))) self._display_surf.blit(surface,(pos_x,0)) pos_x += self.depth_vec[0].shape[1] if len(self.img_vec) > 0: self.img_vec[0] = self.img_vec[0][:,:,:3] self.img_vec[0] = self.img_vec[0][:,:,::-1] surface = pygame.surfarray.make_surface(np.transpose(self.img_vec[0], (1,0,2))) self._display_surf.blit(surface,(pos_x,0)) pos_x += self.img_vec[0].shape[1] if len(self.labels_vec) > 0: self.labels_vec[0] = join_classes(self.labels_vec[0][:,:,2]) surface = pygame.surfarray.make_surface(np.transpose(self.labels_vec[0], (1,0,2))) self._display_surf.blit(surface,(pos_x,0)) pos_x += self.labels_vec[0].shape[1] pygame.display.flip() def on_cleanup(self): self.carla.closeConections() pygame.quit() def on_execute(self): if self.on_init() == False: self._running = False while( self._running ): try: for event in pygame.event.get(): self.on_event(event) self.on_loop() self.on_render() except Exception as e: logging.exception(e) self._running = False break self.on_cleanup()