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 start(self): # You start with some configurationpath self.carla = CARLA(self._host, self._port) self.positions = self.carla.loadConfigurationFile(self._config_path) self._current_goal = random.randint(0, len(self.positions) - 1) position_reset = random.randint(0, len(self.positions) - 1) self.carla.newEpisode(position_reset) self._target = random.randint(0, len(self.positions)) self._start_time = time.time()
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()
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_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()
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()
def use_example(ini_file, port=2000, host='127.0.0.1', print_measurements=True, images_to_disk=False): frequency = 10 rate = rospy.Rate(frequency) # We assume the CARLA server is already waiting for a client to connect at # host:port. To create a connection we can use the CARLA # constructor, it creates a CARLA client object and starts the # connection. It will throw an exception if something goes wrong. carla = CARLA(host, port) """ As a first step, Carla must have a configuration file loaded. This will load a map in the server with the properties specified by the ini file. It returns all the posible starting positions on the map in a vector. """ positions = carla.loadConfigurationFile(ini_file) """ Ask Server for a new episode starting on position of index zero in the positions vector """ carla.newEpisode(0) capture = time.time() # General iteratior i = 1 # Iterator that will go over the positions on the map after each episode iterator_start_positions = 1 while not rospy.is_shutdown(): try: """ User get the measurements dictionary from the server. Measurements contains: * WallTime: Current time on Wall from server machine. * GameTime: Current time on Game. Restarts at every episode * PlayerMeasurements : All information and events that happens to player * Agents : All non-player agents present on the map information such as cars positions, traffic light states * BRGA : BGRA optical images * Depth : Depth Images * Labels : Images containing the semantic segmentation. NOTE: the semantic segmentation must be previously activated on the server. See documentation for that. """ measurements = carla.getMeasurements() ego_x = measurements['PlayerMeasurements'].transform.location.x ego_y = measurements['PlayerMeasurements'].transform.location.y ego_z = measurements['PlayerMeasurements'].transform.location.z ego_ox = measurements['PlayerMeasurements'].transform.orientation.x ego_oy = measurements['PlayerMeasurements'].transform.orientation.y ego_oz = measurements['PlayerMeasurements'].transform.orientation.z # Print all the measurements... Will write images to disk if print_measurements: print_pack(measurements, i, images_to_disk) """ Sends a control command to the server This control structue contains the following fields: * throttle : goes from 0 to -1 * steer : goes from -1 to 1 * brake : goes from 0 to 1 * hand_brake : Activate or desactivate the Hand Brake. * reverse: Activate or desactive the reverse gear. """ control = Control() control.throttle = 0.9 control.steer = (random.random() * 2) - 1 carla.sendCommand(control) rate.sleep() i += 1 if i % RESET_FREQUENCY == 0: print('Fps for this episode : ', (1.0 / ((time.time() - capture) / 100.0))) """ Starts another new episode, the episode will have the same configuration as the previous one. In order to change configuration, the loadConfigurationFile could be called at any time. """ if iterator_start_positions < len(positions): carla.newEpisode(iterator_start_positions) iterator_start_positions += 1 else: carla.newEpisode(0) iterator_start_positions = 1 print("Now Starting on Position: ", iterator_start_positions - 1) capture = time.time() except Exception as e: logging.exception('Exception raised to the top') time.sleep(1)
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 VirtualElektraMachine(Driver): def __init__(self,gpu_number="0",experiment_name ='None',driver_conf=None,memory_fraction=0.9,\ trained_manager =None,session =None,config_input=None): Driver.__init__(self) if trained_manager == None: conf_module = __import__(experiment_name) self._config = conf_module.configInput() config_gpu = tf.ConfigProto() config_gpu.gpu_options.visible_device_list = gpu_number config_gpu.gpu_options.per_process_gpu_memory_fraction = memory_fraction self._sess = tf.Session(config=config_gpu) self._train_manager = load_system(conf_module.configTrain()) self._sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(tf.global_variables()) cpkt = restore_session(self._sess, saver, self._config.models_path) else: self._train_manager = trained_manager self._sess = session self._config = config_input if self._train_manager._config.control_mode == 'goal': self._select_goal = conf_module.configTrain().select_goal self._control_function = getattr( machine_output_functions, self._train_manager._config.control_mode) self._image_cut = driver_conf.image_cut # load a manager to deal with test data self.use_planner = driver_conf.use_planner if driver_conf.use_planner: self.planner = Planner('drive_interfaces/carla_interface/' + driver_conf.city_name + '.txt',\ 'drive_interfaces/carla_interface/' + driver_conf.city_name + '.png') self._host = driver_conf.host self._port = driver_conf.port self._config_path = driver_conf.carla_config self._resolution = driver_conf.resolution self._straight_button = False self._left_button = False self._right_button = False self._recording = False def start(self): # You start with some configurationpath self.carla = CARLA(self._host, self._port) self.positions = self.carla.loadConfigurationFile(self._config_path) self._current_goal = random.randint(0, len(self.positions) - 1) position_reset = random.randint(0, len(self.positions) - 1) self.carla.newEpisode(position_reset) self._target = random.randint(0, len(self.positions)) self._start_time = time.time() def _get_direction_buttons(self): #with suppress_stdout():if keys[K_LEFT]: keys = pygame.key.get_pressed() if (keys[K_s]): self._left_button = False self._right_button = False self._straight_button = False if (keys[K_a]): self._left_button = True self._right_button = False self._straight_button = False if (keys[K_d]): self._right_button = True self._left_button = False self._straight_button = False if (keys[K_w]): self._straight_button = True self._left_button = False self._right_button = False return [self._left_button, self._right_button, self._straight_button] def compute_goal(self, pos, ori): #Return the goal selected pos, point = self.planner.get_defined_point( pos, ori, (self.positions[self._target][0], self.positions[self._target][1], 22), (1.0, 0.02, -0.001), 1 + self._select_goal) return convert_to_car_coord(point[0], point[1], pos[0], pos[1], ori[0], ori[1]) def compute_direction( self, pos, ori): # This should have maybe some global position... GPS stuff if self._train_manager._config.control_mode == 'goal': return self.compute_goal(pos, ori) elif self.use_planner: command, made_turn, completed = self.planner.get_next_command( pos, ori, (self.positions[self._target].location.x, self.positions[self._target].location.y, 22), (1.0, 0.02, -0.001)) return command else: # BUtton 3 has priority if 'Control' not in set(self._config.inputs_names): return None button_vec = self._get_direction_buttons() if sum(button_vec) == 0: # Nothing return 2 elif button_vec[0] == True: # Left return 3 elif button_vec[1] == True: # RIght return 4 else: return 5 def get_recording(self): return False def get_reset(self): return False def new_episode(self, initial_pos, target, cars, pedestrians, weather): config = ConfigParser() config.read(self._config_path) config.set('CARLA/LevelSettings', 'NumberOfVehicles', cars) config.set('CARLA/LevelSettings', 'NumberOfPedestrians', pedestrians) config.set('CARLA/LevelSettings', 'WeatherId', weather) # Write down a temporary init_file to be used on the experiments temp_f_name = 's' +str(initial_pos)+'_e'+ str(target) + "_p" +\ str(pedestrians)+'_c' + str(cars)+"_w" + str(weather) +\ '.ini' with open(temp_f_name, 'w') as configfile: config.write(configfile) positions = self.carla.requestNewEpisode(temp_f_name) self.carla.newEpisode(initial_pos) self._target = target def get_all_turns(self, data, target): rewards = data[0] sensor = data[2][0] speed = rewards.speed return self.planner.get_all_commands((rewards.player_x,rewards.player_y,22),(rewards.ori_x,rewards.ori_y,rewards.ori_z),\ (target[0],target[1],22),(1.0,0.02,-0.001)) #### TO BE DEPRECATED , WE DONT NEED TWO FUNCTIONS .... def run_step(self, data, target): rewards = data[0] sensor = data[2][0] speed = rewards.speed direction,made_turn,completed = self.planner.get_next_command((rewards.player_x,rewards.player_y,22),(rewards.ori_x,rewards.ori_y,rewards.ori_z),\ (target[0],target[1],22),(1.0,0.02,-0.001)) #pos = (rewards.player_x,rewards.player_y,22) #ori =(rewards.ori_x,rewards.ori_y,rewards.ori_z) #pos,point = self.planner.get_defined_point(pos,ori,(target[0],target[1],22),(1.0,0.02,-0.001),self._select_goal) #direction = convert_to_car_coord(point[0],point[1],pos[0],pos[1],ori[0],ori[1]) capture_time = time.time() sensor = sensor[self._image_cut[0]:self._image_cut[1], :, :] sensor = scipy.misc.imresize(sensor, [ self._config.network_input_size[0], self._config.network_input_size[1] ]) image_input = sensor.astype(np.float32) #print future_image #print direction #print "2" image_input = np.multiply(image_input, 1.0 / 255.0) steer, acc, brake = self._control_function(image_input, speed, direction, self._config, self._sess, self._train_manager) if brake < 0.1: brake = 0.0 if acc > 2 * brake: brake = 0.0 control = Control() control.steer = steer control.throttle = acc control.brake = brake control.hand_brake = 0 control.reverse = 0 #### DELETE THIS VERSION NOT COMMITABLE made_turn = 0 completed = 0 return control, made_turn, completed def compute_action(self, sensor, speed, direction=None): capture_time = time.time() if capture_time - self._start_time > 400: self._target = random.randint(0, len(self.positions)) self._start_time = time.time() if direction == None: direction = self.compute_direction((0, 0, 0), (0, 0, 0)) sensor = sensor[self._image_cut[0]:self._image_cut[1], :, :3] sensor = sensor[:, :, ::-1] sensor = scipy.misc.imresize(sensor, [ self._config.network_input_size[0], self._config.network_input_size[1] ]) image_input = sensor.astype(np.float32) #print future_image #print "2" image_input = np.multiply(image_input, 1.0 / 255.0) steer, acc, brake = self._control_function(image_input, speed, direction, self._config, self._sess, self._train_manager) control = Control() control.steer = steer if control.steer > 0.3: control.steer = 1.0 elif control.steer < -0.3: control.steer = -1.0 else: control.steer = 0.0 control.throttle = 1.0 control.brake = 0.0 # print brake control.hand_brake = 0 control.reverse = 0 return control # The augmentation should be dependent on speed def get_sensor_data(self): measurements = self.carla.getMeasurements() self._latest_measurements = measurements player_data = measurements['PlayerMeasurements'] pos = [ player_data.transform.location.x, player_data.transform.location.y, 22 ] ori = [ player_data.transform.orientation.x, player_data.transform.orientation.y, player_data.transform.orientation.z ] direction = 2.0 return measurements, direction def compute_perception_activations(self, sensor, speed): sensor = sensor[self._image_cut[0]:self._image_cut[1], :, :] sensor = scipy.misc.imresize(sensor, [ self._config.network_input_size[0], self._config.network_input_size[1] ]) image_input = sensor.astype(np.float32) #print future_image image_input = image_input - self._mean_image #print "2" image_input = np.multiply(image_input, 1.0 / 127.0) vbp_image = machine_output_functions.vbp(image_input, speed, self._config, self._sess, self._train_manager) min_max_scaler = preprocessing.MinMaxScaler() vbp_image = min_max_scaler.fit_transform(np.squeeze(vbp_image)) # print vbp_image #print vbp_image #print grayscale_colormap(np.squeeze(vbp_image),'jet') vbp_image_3 = np.copy(image_input) vbp_image_3[:, :, 0] = vbp_image vbp_image_3[:, :, 1] = vbp_image vbp_image_3[:, :, 2] = vbp_image #print vbp_image return 0.4 * grayscale_colormap(np.squeeze(vbp_image), 'inferno') + 0.6 * image_input def act(self, action): self.carla.sendCommand(action) def stop(self): self.carla.stop()
def use_example(ini_file,port = 2000, host ='127.0.0.1',print_measurements =False,images_to_disk=False): # We assume the CARLA server is already waiting for a client to connect at # host:port. To create a connection we can use the CARLA # constructor, it creates a CARLA client object and starts the # connection. It will throw an exception if something goes wrong. carla =CARLA(host,port) """ As a first step, Carla must have a configuration file loaded. This will load a map in the server with the properties specified by the ini file. It returns all the posible starting positions on the map in a vector. """ positions = carla.loadConfigurationFile(ini_file) """ Ask Server for a new episode starting on position of index zero in the positions vector """ carla.newEpisode(0) capture = time.time() # General iteratior i = 1 # Iterator that will go over the positions on the map after each episode iterator_start_positions = 1 while True: try: """ User get the measurements dictionary from the server. Measurements contains: * WallTime: Current time on Wall from server machine. * GameTime: Current time on Game. Restarts at every episode * PlayerMeasurements : All information and events that happens to player * Agents : All non-player agents present on the map information such as cars positions, traffic light states * BRGA : BGRA optical images * Depth : Depth Images * Labels : Images containing the semantic segmentation. NOTE: the semantic segmentation must be previously activated on the server. See documentation for that. """ measurements = carla.getMeasurements() # Print all the measurements... Will write images to disk if print_measurements: print_pack(measurements,i,images_to_disk) """ Sends a control command to the server This control structue contains the following fields: * throttle : goes from 0 to -1 * steer : goes from -1 to 1 * brake : goes from 0 to 1 * hand_brake : Activate or desactivate the Hand Brake. * reverse: Activate or desactive the reverse gear. """ control = Control() control.throttle = 0.9 control.steer = (random.random() * 2) - 1 carla.sendCommand(control) i+=1 if i % RESET_FREQUENCY ==0: print ('Fps for this episode : ',(1.0/((time.time() -capture)/100.0))) """ Starts another new episode, the episode will have the same configuration as the previous one. In order to change configuration, the loadConfigurationFile could be called at any time. """ if iterator_start_positions < len(positions): carla.newEpisode(iterator_start_positions) iterator_start_positions+=1 else : carla.newEpisode(0) iterator_start_positions = 1 print("Now Starting on Position: ",iterator_start_positions-1) capture = time.time() except Exception as e: logging.exception('Exception raised to the top') time.sleep(1)
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()
def main_loop(self): # Carla Setup carla = CARLA(self.host, self.port) positions = carla.loadConfigurationFile(self.ini_file) carla.newEpisode(0) capture = time.time() i = 1 # Iterator that will go over the positions on the map after each episode iterator_start_positions = 1 rate = rospy.Rate(ROS_FREQUENCY) while not rospy.is_shutdown(): try: """ User get the measurements dictionary from the server. Measurements contains: * WallTime: Current time on Wall from server machine. * GameTime: Current time on Game. Restarts at every episode * PlayerMeasurements : All information and events that happens to player * Agents : All non-player agents present on the map information such as cars positions, traffic light states * BRGA : BGRA optical images * Depth : Depth Images * Labels : Images containing the semantic segmentation. NOTE: the semantic segmentation must be previously activated on the server. See documentation for that. """ measurements = carla.getMeasurements() self.measurements_process(measurements) self.measurements_publish() """ Sends a control command to the server This control structue contains the following fields: * throttle : goes from 0 to 1 * steer : goes from -1 to 1 * brake : goes from 0 to 1 * hand_brake : Activate or desactivate the Hand Brake. * reverse: Activate or desactive the reverse gear. """ control = Control() control.throttle = 0.9 control.steer = 0 carla.sendCommand(control) rate.sleep() i += 1 if i % RESET_FREQUENCY == 0: print('Fps for this episode : ', (1.0 / ((time.time() - capture) / 100.0))) """ Starts another new episode, the episode will have the same configuration as the previous one. In order to change configuration, the loadConfigurationFile could be called at any time. """ if iterator_start_positions < len(positions): carla.newEpisode(iterator_start_positions) iterator_start_positions += 1 else: carla.newEpisode(0) iterator_start_positions = 1 print("Now Starting on Position: ", iterator_start_positions - 1) capture = time.time() except KeyboardInterrupt: pass