def reset(self): self._destroy_actors() self.collision_info = {} self.collision_hist = [] self.time_step = 0 self._set_synchronous_mode(False) if self.config['verbose']: print(colored("setting actors", "white")) # set actors ego_vehicle, _ = self.set_ego() self.ego = ego_vehicle self.last_position = get_pos(ego_vehicle) self.last_steer = 0 rgb_camera = self.set_camera(ego_vehicle, sensor_width=self.sensor_width, sensor_height=self.sensor_height, fov=110) collision_sensor = self.set_collision_sensor(ego_vehicle) obstacle_detector = self.set_obstacle_detector(ego_vehicle) sp_vehicles, sp_walkers, sp_walker_controllers = self.set_actors( vehicles=self.config['vehicles'], walkers=self.config['walkers']) # we need to store the pointers to avoid "out of scope" errors self.to_clean = dict( vehicles=[ego_vehicle, *sp_vehicles, *sp_walkers], sensors=[collision_sensor, rgb_camera, obstacle_detector], controllers=sp_walker_controllers) if self.config['verbose']: print( colored( f"spawned {len(sp_vehicles)} vehicles and {len(sp_walkers)} walkers", "green")) # attaching handlers weak_self = weakref.ref(self) rgb_camera.listen(lambda data: get_camera_img(weak_self, data)) collision_sensor.listen( lambda event: get_collision_hist(weak_self, event)) obstacle_detector.listen( lambda event: get_obstacle_hist(weak_self, event)) self._set_synchronous_mode(True) self.route_planner = RoutePlanner(ego_vehicle, self.config['max_waypt']) self.waypoints, self.red_light, self.distance_to_traffic_light, \ self.is_vehicle_hazard, self.vehicle_front, self.road_option = self.route_planner.run_step() return self._step([0, 0, 0])
def reset(self): # Clear sensor objects self.collision_sensor = None self.lidar_sensor = None self.camera_sensor = None # Delete sensors, vehicles and walkers self._clear_all_actors([ 'sensor.other.collision', 'sensor.lidar.ray_cast', 'sensor.camera.rgb', 'vehicle.*', 'controller.ai.walker', 'walker.*' ]) # Disable sync mode self._set_synchronous_mode(False) # Spawn surrounding vehicles random.shuffle(self.vehicle_spawn_points) count = self.number_of_vehicles if count > 0: for spawn_point in self.vehicle_spawn_points: if self._try_spawn_random_vehicle_at(spawn_point, number_of_wheels=[4]): count -= 1 if count <= 0: break while count > 0: if self._try_spawn_random_vehicle_at(random.choice( self.vehicle_spawn_points), number_of_wheels=[4]): count -= 1 # Spawn pedestrians random.shuffle(self.walker_spawn_points) count = self.number_of_walkers if count > 0: for spawn_point in self.walker_spawn_points: if self._try_spawn_random_walker_at(spawn_point): count -= 1 if count <= 0: break while count > 0: if self._try_spawn_random_walker_at( random.choice(self.walker_spawn_points)): count -= 1 # Get actors polygon list self.vehicle_polygons = [] vehicle_poly_dict = self._get_actor_polygons('vehicle.*') self.vehicle_polygons.append(vehicle_poly_dict) self.walker_polygons = [] walker_poly_dict = self._get_actor_polygons('walker.*') self.walker_polygons.append(walker_poly_dict) # Spawn the ego vehicle ego_spawn_times = 0 while True: if ego_spawn_times > self.max_ego_spawn_times: self.reset() if self.task_mode == 'random': transform = random.choice(self.vehicle_spawn_points) if self.task_mode == 'roundabout': self.start = [52.1 + np.random.uniform(-5, 5), -4.2, 178.66] # random # self.start=[52.1,-4.2, 178.66] # static transform = set_carla_transform(self.start) if self._try_spawn_ego_vehicle_at(transform): break else: ego_spawn_times += 1 time.sleep(0.1) # Add collision sensor self.collision_sensor = self.world.spawn_actor(self.collision_bp, carla.Transform(), attach_to=self.ego) self.collision_sensor.listen(lambda event: get_collision_hist(event)) def get_collision_hist(event): impulse = event.normal_impulse intensity = np.sqrt(impulse.x**2 + impulse.y**2 + impulse.z**2) self.collision_hist.append(intensity) if len(self.collision_hist) > self.collision_hist_l: self.collision_hist.pop(0) self.collision_hist = [] # Add lidar sensor self.lidar_sensor = self.world.spawn_actor(self.lidar_bp, self.lidar_trans, attach_to=self.ego) self.lidar_sensor.listen(lambda data: get_lidar_data(data)) def get_lidar_data(data): self.lidar_data = data # Add camera sensor self.camera_sensor = self.world.spawn_actor(self.camera_bp, self.camera_trans, attach_to=self.ego) self.camera_sensor.listen(lambda data: get_camera_img(data)) def get_camera_img(data): array = np.frombuffer(data.raw_data, dtype=np.dtype("uint8")) array = np.reshape(array, (data.height, data.width, 4)) array = array[:, :, :3] array = array[:, :, ::-1] self.camera_img = array # Update timesteps self.time_step = 0 self.reset_step += 1 # Enable sync mode self.settings.synchronous_mode = True self.world.apply_settings(self.settings) self.routeplanner = RoutePlanner(self.ego, self.max_waypt) self.waypoints, _, self.vehicle_front = self.routeplanner.run_step() # Set ego information for render self.birdeye_render.set_hero(self.ego, self.ego.id) return self._get_obs()
def reset(self): # Clear sensor objects self._clear_all_sensors() self.collision_sensor = None self.camera_sensor = None self.idle_timesteps = 0 # Delete sensors, vehicles and walkers self._clear_all_actors([ 'sensor.other.collision', 'sensor.lidar.ray_cast', 'sensor.camera.rgb', 'vehicle.*', 'controller.ai.walker', 'walker.*' ]) # Disable sync mode self._set_synchronous_mode(False) # Spawn vehicles in same lane as ego vehicle and ahead ego_vehicle_traffic_spawns_1 = get_spawn_points_for_traffic( 40, [-7, -6, -5, -4], self.map, self.number_of_vehicles) ego_vehicle_traffic_spawns_2 = get_spawn_points_for_traffic( 39, [-7, -6, -5, -4], self.map, self.number_of_vehicles, start=15) ego_vehicle_traffic_spawns = ego_vehicle_traffic_spawns_1 + ego_vehicle_traffic_spawns_2 random.shuffle(ego_vehicle_traffic_spawns) count = self.number_of_vehicles if count > 0: for spawn_point in ego_vehicle_traffic_spawns: if self._try_spawn_random_vehicle_at(spawn_point, number_of_wheels=[4]): count -= 1 if count <= 0: break while count > 0: if self._try_spawn_random_vehicle_at( random.choice(ego_vehicle_traffic_spawns), number_of_wheels=[4]): count -= 1 # set autopilot for all traffic vehicles: batch = [] for vehicle in self.traffic_vehicles: batch.append(carla.command.SetAutopilot(vehicle, True)) self.client.apply_batch_sync(batch) # Get actors polygon list self.vehicle_polygons = [] vehicle_poly_dict = self._get_actor_polygons('vehicle.*') self.vehicle_polygons.append(vehicle_poly_dict) self.walker_polygons = [] walker_poly_dict = self._get_actor_polygons('walker.*') self.walker_polygons.append(walker_poly_dict) # Spawn the ego vehicle ego_spawn_times = 0 while True: if ego_spawn_times > self.max_ego_spawn_times: self.reset() if self.task_mode == 'acc_1': wpt1 = get_waypoint_for_ego_spawn(road_id=39, lane_id=-5, s=0, map=self.map) transform = wpt1.transform transform.location.z += 2.0 if self._try_spawn_ego_vehicle_at(transform): break else: print('trying to spawn %d' % ego_spawn_times) ego_spawn_times += 1 time.sleep(0.1) # Add collision sensor self.collision_sensor = self.world.spawn_actor(self.collision_bp, carla.Transform(), attach_to=self.ego) self.collision_sensor.listen(lambda event: get_collision_hist(event)) def get_collision_hist(event): impulse = event.normal_impulse intensity = np.sqrt(impulse.x**2 + impulse.y**2 + impulse.z**2) self.collision_hist.append(intensity) if len(self.collision_hist) > self.collision_hist_l: self.collision_hist.pop(0) self.collision_hist = [] # Add camera sensor if self.use_rgb_camera: self.camera_sensor = self.world.spawn_actor(self.camera_bp, self.camera_trans, attach_to=self.ego) self.camera_sensor.listen(lambda data: get_camera_img(data)) def get_camera_img(data): array = np.frombuffer(data.raw_data, dtype=np.dtype("uint8")) array = np.reshape(array, (data.height, data.width, 4)) array = array[:, :, :3] array = array[:, :, ::-1] self.camera_img = array # Update timesteps self.time_step = 0 self.reset_step += 1 # Enable sync mode self.settings.synchronous_mode = False if not self.use_rgb_camera: self.settings.no_rendering_mode = True self.world.apply_settings(self.settings) self.routeplanner = RoutePlanner(self.ego, self.max_waypt) self.waypoints, _, self.vehicle_hazards = self.routeplanner.run_step() # Set ego information for render self.birdeye_render.set_hero(self.ego, self.ego.id) return self._get_obs()