def visualize_pedestrian(): config = parse_arguments() client = carla.Client(config.host, config.port) client.set_timeout(5.0) world = client.get_world() carla_map = world.get_map() walker_blueprints = world.get_blueprint_library().filter("walker.*") spawn_point = carla_map.get_spawn_points()[0] pedestrian = None spectator = world.get_spectator() for blueprint in walker_blueprints: try: print("Showing pedestrian", blueprint) pedestrian = world.spawn_actor(blueprint, spawn_point) world.wait_for_tick() iterations = 40 theta = np.linspace(0, 1*math.pi, iterations) phi = (5/6 / 2)*math.pi r = 2 for jdx in range(iterations): transform = carlautil.spherical_to_camera_watcher_transform( r, theta[jdx], phi, location=pedestrian.get_location()) # transform = carlautil.strafe_transform( # transform, right=0, above=0) spectator.set_transform(transform) world.wait_for_tick() finally: if pedestrian: pedestrian.destroy()
def __set_random_frame(self, spectator, transform, vehicle=None, strafe_camera=False, xy_shift_camera=True): """ TODO: the camera transform should be relative to the yaw of vehicle transform, not just vehicle location """ r = random.uniform(*self.range_r) theta = random.uniform(*self.range_theta) phi = random.uniform(*self.range_phi) _transform = carla.Transform( transform.location, carla.Rotation(yaw=transform.rotation.yaw)) if xy_shift_camera: # NOTE: the Audi A2 has dimensions (3.70 m, 1.79 m) x_shift = random.uniform(-self.max_shift, self.max_shift) y_shift = random.uniform(-self.max_shift, self.max_shift) _transform.location += carla.Location(x=x_shift, y=y_shift) _transform = carlautil.spherical_to_camera_watcher_transform( r, theta, phi, pin=_transform) if strafe_camera and vehicle: # NOTE: GIRAFFE does not do camera strafing, it does camera shifting near_frac = vehicle.bounding_box.extent.x ratio_f = near_frac / self.range_r[0] strafe_amt = r * ratio_f - near_frac right_strafe = random.uniform(-strafe_amt, strafe_amt) above_strafe = random.uniform(-strafe_amt, strafe_amt) _transform = carlautil.strafe_transform(_transform, right=right_strafe, above=above_strafe) spectator.set_transform(_transform)
def demo_night_lights(self, spectator, vehicle): """ NOTE: for CARLA 0.9.11/12, not all cars have lights """ iterations = 300 theta = np.linspace(-math.pi / 2, (3 / 2) * math.pi, iterations) phi = (1 / 3) * math.pi r = (self.near_r + self.far_r) / 2. original_lightstate = vehicle.get_light_state() vls = carla.VehicleLightState light_state = vls(vls.LowBeam | vls.Interior | vls.Reverse | vls.Position) vehicle.set_light_state(light_state) weather = self.world.get_weather() weather.sun_altitude_angle = -15 self.world.set_weather(weather) for jdx in range(iterations): # set spectator orientation and position transform = carlautil.spherical_to_camera_watcher_transform( r, theta[jdx], phi, location=vehicle.get_location()) transform = carlautil.strafe_transform(transform, right=0, above=0) spectator.set_transform(transform) # wait for server before continuing if jdx == 0 or jdx == iterations - 1: for _ in range(50): self.world.wait_for_tick() else: self.world.wait_for_tick() vehicle.set_light_state(original_lightstate)
def demo_all_lights(self, spectator, vehicle): iterations = 150 theta = np.linspace(-math.pi/2, math.pi/2, iterations) phi = math.pi / 4 r = 5 sun_altitude_angles = np.linspace(1, -19, iterations) original_lightstate = vehicle.get_light_state() light_types = ["Position", "LowBeam", "HighBeam", "Brake", "RightBlinker", "LeftBlinker", "Reverse", "Fog", "Interior", "Special1", "Special2", "All"] for light_type in light_types: print(f"Demonstrating vehicle {light_type} lights.") vehicle.set_light_state(getattr(carla.VehicleLightState, light_type)) for jdx in range(iterations): # set spectator orientation and position transform = carlautil.spherical_to_camera_watcher_transform( r, theta[jdx], phi, location=vehicle.get_location()) transform = carlautil.strafe_transform(transform, right=0, above=0) spectator.set_transform(transform) # set weather weather = self.world.get_weather() weather.sun_altitude_angle = sun_altitude_angles[jdx] self.world.set_weather(weather) # wait for server before continuing if jdx == 0 or jdx == iterations - 1: for _ in range(50): self.world.wait_for_tick() else: self.world.wait_for_tick() vehicle.set_light_state(original_lightstate)
def inspect_vehicle(): config = parse_arguments() client = carla.Client(config.host, config.port) client.set_timeout(10.0) world = client.get_world() carla_map = world.get_map() blueprint = world.get_blueprint_library().find("vehicle.audi.a2") spawn_point = carla_map.get_spawn_points()[0] vehicle = None try: vehicle = world.spawn_actor(blueprint, spawn_point) world.wait_for_tick() transform = carlautil.spherical_to_camera_watcher_transform( 3, math.pi, math.pi*(1/6), location=vehicle.get_location()) world.get_spectator().set_transform(transform) world.wait_for_tick() def get_forward_vector(): """ The vehicle's forward vector is a unit vector """ f = vehicle.get_transform().get_forward_vector() f = np.array([f.x, f.y, f.z]) print( np.linalg.norm(f) ) def get_bounding_box(): bb = vehicle.bounding_box e = bb.extent print(np.array([e.x, e.y, e.z])*2) # get_bounding_box() finally: if vehicle: vehicle.destroy()
def __set_random_frame(self, spectator, vehicle, strafe_camera=False, xy_shift_camera=True): r = random.uniform(self.near_r, self.far_r) theta = random.uniform(0, 2 * math.pi) phi = random.uniform((5 / 14) * math.pi, (1 / 6) * math.pi) location = vehicle.get_location() if xy_shift_camera: # NOTE: the Audi A2 has dimensions (3.70 m, 1.79 m) x_shift = random.uniform(self.min_shift, self.max_shift) y_shift = random.uniform(self.min_shift, self.max_shift) location += carla.Location(x=x_shift, y=y_shift) transform = carlautil.spherical_to_camera_watcher_transform( r, theta, phi, location=location) if strafe_camera: # NOTE: GIRAFFE does not do camera strafing, it does camera shifting near_frac = vehicle.bounding_box.extent.x ratio_f = near_frac / self.near_r strafe_amt = r * ratio_f - near_frac right_strafe = random.uniform(-strafe_amt, strafe_amt) above_strafe = random.uniform(-strafe_amt, strafe_amt) transform = carlautil.strafe_transform(transform, right=right_strafe, above=above_strafe) spectator.set_transform(transform)
def visualize_distances(): config = parse_arguments() client = carla.Client(config.host, config.port) client.set_timeout(10.0) world = client.get_world() carla_map = world.get_map() blueprints = world.get_blueprint_library().filter("vehicle.*") blueprints = [x for x in blueprints if int(x.get_attribute("number_of_wheels")) == 4] blueprints = [x for x in blueprints if not x.id.endswith('isetta')] blueprints = [x for x in blueprints if not x.id.endswith('carlacola')] blueprints = [x for x in blueprints if not x.id.endswith('firetruck')] blueprints = [x for x in blueprints if not x.id.endswith('cybertruck')] blueprints = [x for x in blueprints if not x.id.endswith('ambulance')] blueprints = [x for x in blueprints if not x.id.endswith('sprinter')] blueprints = [x for x in blueprints if not x.id.endswith('t2')] # blueprint = world.get_blueprint_library().find("vehicle.audi.a2") blueprint = random.choice(blueprints) spawn_point = carla_map.get_spawn_points()[0] vehicle = None try: vehicle = world.spawn_actor(blueprint, spawn_point) transform = carlautil.spherical_to_camera_watcher_transform( 3, math.pi, math.pi*(1/6), pin=spawn_point.location) world.get_spectator().set_transform(transform) thickness = 0.2 color = carla.Color(r=255, g=0, b=0, a=100) life_time = 6 loc = carlautil.to_location_ndarray(spawn_point.location) scale = 2. z = loc[2] + 0.5 box = np.array([[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]]) box = (box*scale) + loc[:2] world.debug.draw_line( carla.Location(box[0, 0], box[0, 1], z), carla.Location(box[1, 0], box[1, 1], z), thickness=thickness, color=color, life_time=life_time) world.debug.draw_line( carla.Location(box[1, 0], box[1, 1], z), carla.Location(box[2, 0], box[2, 1], z), thickness=thickness, color=color, life_time=life_time) world.debug.draw_line( carla.Location(box[2, 0], box[2, 1], z), carla.Location(box[3, 0], box[3, 1], z), thickness=thickness, color=color, life_time=life_time) world.debug.draw_line( carla.Location(box[3, 0], box[3, 1], z), carla.Location(box[0, 0], box[0, 1], z), thickness=thickness, color=color, life_time=life_time) time.sleep(life_time) world.wait_for_tick() finally: if vehicle: vehicle.destroy()
def visualize_wheel_turning_2(): """ Q: is the steering immediate? A: no it takes time. Small steering rate (i.e. setting steer=0.2) is fast. The larger the change in steering, the longer it takes the wheels to change from the current angle to the intended angle. https://github.com/carla-simulator/carla/issues/4655 """ config = parse_arguments() client = carla.Client(config.host, config.port) client.set_timeout(10.0) world = client.get_world() carla_map = world.get_map() blueprint = world.get_blueprint_library().find("vehicle.audi.a2") spawn_point = carla_map.get_spawn_points()[0] vehicle = None try: vehicle = world.spawn_actor(blueprint, spawn_point) world.wait_for_tick() phys_control = vehicle.get_physics_control() wheel = phys_control.wheels[1] transform = carlautil.spherical_to_camera_watcher_transform( 3, math.pi, math.pi*(1/6), location=vehicle.get_location()) world.get_spectator().set_transform(transform) for _ in range(100): world.wait_for_tick() world.wait_for_tick() vehicle.apply_control(carla.VehicleControl(steer=0.2)) world.wait_for_tick() time.sleep(0.5) world.wait_for_tick() vehicle.apply_control(carla.VehicleControl(steer=0.0)) world.wait_for_tick() time.sleep(0.5) world.wait_for_tick() vehicle.apply_control(carla.VehicleControl(steer=0.5)) world.wait_for_tick() time.sleep(0.5) world.wait_for_tick() vehicle.apply_control(carla.VehicleControl(steer=0.0)) world.wait_for_tick() time.sleep(0.5) world.wait_for_tick() vehicle.apply_control(carla.VehicleControl(steer=1.0)) world.wait_for_tick() time.sleep(0.5) for _ in range(200): world.wait_for_tick() finally: if vehicle: vehicle.destroy()
def demo_azimuthal_rotation(world, spectator, vehicle, iterations=300): theta = np.linspace(0, 2*math.pi, iterations) phi = math.pi / 4 r = 4 for jdx in range(iterations): transform = carlautil.spherical_to_camera_watcher_transform( r, theta[jdx], phi, location=vehicle.get_location()) transform = carlautil.strafe_transform(transform, right=0, above=0) spectator.set_transform(transform) if jdx == 0 or jdx == iterations - 1: for _ in range(50): world.wait_for_tick() else: world.wait_for_tick()
def visualize_wheel_turning_1(): """This method shows that setting steer = 1.0 makes the vehicle turn to the max steering angle.""" config = parse_arguments() client = carla.Client(config.host, config.port) client.set_timeout(10.0) world = client.get_world() carla_map = world.get_map() blueprint = world.get_blueprint_library().find("vehicle.audi.a2") spawn_point = carla_map.get_spawn_points()[0] vehicle = None try: vehicle = world.spawn_actor(blueprint, spawn_point) world.wait_for_tick() phys_control = vehicle.get_physics_control() wheel = phys_control.wheels[1] transform = carlautil.spherical_to_camera_watcher_transform( 3, math.pi, math.pi*(1/6), location=vehicle.get_location()) world.get_spectator().set_transform(transform) # Q1: does applying steering control once persist across many ticks? world.wait_for_tick() vehicle.apply_control(carla.VehicleControl(steer=1.0)) world.wait_for_tick() phys_control = vehicle.get_physics_control() wheel = phys_control.wheels[1] location = vehicle.get_location() transform = vehicle.get_transform() end = location + 5*transform.get_forward_vector() world.debug.draw_line(location, end, thickness=0.1, life_time=15.0) # Q2: does steer=1.0 correspond with wheel.max_steer_angle? location = vehicle.get_location() transform = vehicle.get_transform() theta = math.radians(90 - wheel.max_steer_angle) direction = math.sin(theta)*transform.get_forward_vector() \ + math.cos(theta)*transform.get_right_vector() end = location + 5*direction world.debug.draw_line(location, end, thickness=0.1, life_time=15.0) # A1: yes it does. for _ in range(800): world.wait_for_tick() finally: if vehicle: vehicle.destroy()
def demo_radius(self, spectator, vehicle): iterations = 100 theta = math.pi / 2 phi = math.pi / 4 r = np.linspace(self.near_r, 5, iterations) for jdx in range(iterations): transform = carlautil.spherical_to_camera_watcher_transform( r[jdx], theta, phi, location=vehicle.get_location()) transform = carlautil.strafe_transform(transform, right=0, above=0) spectator.set_transform(transform) if jdx == 0 or jdx == iterations - 1: for _ in range(50): self.world.wait_for_tick() else: self.world.wait_for_tick()
def demo_polar_rotation(self, spectator, vehicle): iterations = 200 theta = math.pi / 3 phi = np.linspace((5/14)*math.pi, (1/6)*math.pi, iterations) r = 4 for jdx in range(iterations): transform = carlautil.spherical_to_camera_watcher_transform( r, theta, phi[jdx], location=vehicle.get_location()) transform = carlautil.strafe_transform(transform, right=0, above=0) spectator.set_transform(transform) if jdx == 0 or jdx == iterations - 1: for _ in range(50): self.world.wait_for_tick() else: self.world.wait_for_tick()
def __set_random_frame(self, spectator, vehicle): r = random.uniform(self.near_r, 4.5) theta = random.uniform(0, 2*math.pi) phi = random.uniform((5/14)*math.pi, (1/6)*math.pi) near_frac = vehicle.bounding_box.extent.x ratio_f = near_frac / self.near_r strafe_amt = r*ratio_f - near_frac right_strafe = random.uniform(-strafe_amt, strafe_amt) above_strafe = random.uniform(-strafe_amt, strafe_amt) transform = carlautil.spherical_to_camera_watcher_transform( r, theta, phi, location=vehicle.get_location()) transform = carlautil.strafe_transform(transform, right=right_strafe, above=above_strafe) spectator.set_transform(transform)
def demo_shift(self, spectator, vehicle): iterations = 100 theta = math.pi / 2 phi = math.pi / 4 r = np.linspace(self.near_r, 5, iterations) near_frac = vehicle.bounding_box.extent.x ratio_f = near_frac / self.near_r for jdx in range(iterations): transform = carlautil.spherical_to_camera_watcher_transform( r[jdx], theta, phi, location=vehicle.get_location()) transform = carlautil.strafe_transform(transform, right=r[jdx]*ratio_f - near_frac, above=r[jdx]*ratio_f - near_frac) spectator.set_transform(transform) if jdx == 0 or jdx == iterations - 1: for _ in range(50): self.world.wait_for_tick() else: self.world.wait_for_tick()
def inspect_entity_dimensions(): config = parse_arguments() client = carla.Client(config.host, config.port) client.set_timeout(5.0) world = client.get_world() carla_map = world.get_map() spawn_point = carla_map.get_spawn_points()[0] # vehicle blueprints blueprints = world.get_blueprint_library().filter("vehicle.*") blueprints = [x for x in blueprints if int(x.get_attribute("number_of_wheels")) == 4] blueprints = [x for x in blueprints if not x.id.endswith('isetta')] blueprints = [x for x in blueprints if not x.id.endswith('carlacola')] blueprints = [x for x in blueprints if not x.id.endswith('firetruck')] blueprints = [x for x in blueprints if not x.id.endswith('cybertruck')] blueprints = [x for x in blueprints if not x.id.endswith('ambulance')] blueprints = [x for x in blueprints if not x.id.endswith('sprinter')] blueprints = [x for x in blueprints if not x.id.endswith('t2')] # walker blueprints walker_blueprints = world.get_blueprint_library().filter("walker.*") vehicle = None pedestrian = None spectator = world.get_spectator() logging = util.AttrDict( vehicle=util.AttrDict( xs=[], d2s=[], d3s=[] ), pedestrian=util.AttrDict( xs=[], d2s=[], d3s=[] ) ) for blueprint in blueprints: try: vehicle = world.spawn_actor(blueprint, spawn_point) world.wait_for_tick() transform = carlautil.spherical_to_camera_watcher_transform( 5, math.pi, math.pi*(1/6), pin=spawn_point.location) spectator.set_transform(transform) world.wait_for_tick() x = carlautil.actor_to_bbox_ndarray(vehicle) d2 = np.linalg.norm(x[:2] / 2) d3 = np.linalg.norm(x / 2) logging.vehicle.xs.append(x) logging.vehicle.d2s.append(d2) logging.vehicle.d3s.append(d3) print(blueprint.id) print(x) time.sleep(0.1) finally: if vehicle: vehicle.destroy() vehicle = None world.wait_for_tick() for blueprint in walker_blueprints: try: pedestrian = world.spawn_actor(blueprint, spawn_point) world.wait_for_tick() transform = carlautil.spherical_to_camera_watcher_transform( 5, math.pi, math.pi*(1/6), pin=spawn_point.location) spectator.set_transform(transform) world.wait_for_tick() x = carlautil.actor_to_bbox_ndarray(pedestrian) d2 = np.linalg.norm(x[:2] / 2) d3 = np.linalg.norm(x / 2) logging.pedestrian.xs.append(x) logging.pedestrian.d2s.append(d2) logging.pedestrian.d3s.append(d3) finally: if pedestrian: pedestrian.destroy() pedestrian = None for k, entity in logging.items(): entity.xs = np.stack(entity.xs) entity.d2s = np.stack(entity.d2s) entity.d3s = np.stack(entity.d3s) print(f"Vehicle dimensions { np.max(logging.vehicle.xs, 0) }") print(f" max 2D distance from origin { np.max(logging.vehicle.d2s) }") print(f" max 3D distance from origin { np.max(logging.vehicle.d3s) }") print(f"Pedestrian dimensions { np.mean(logging.pedestrian.xs, 0) }") print(f" max 2D distance from origin { np.max(logging.pedestrian.d2s) }") print(f" max 3D distance from origin { np.max(logging.pedestrian.d3s) }")
def visualize_vehicles_in_the_dark(): """Cars with lights: audi.tt mercedes.sprinter dodge.charger_police mercedes.coupe_2020 dodge.charger_police_2020 mini.cooper_s_2021 bh crossbike lincoln.mkz_2020 lincoln.mkz_2017 ford.mustnge tesla ybertruck ford ambulance nissan patrol_2021 yamaha yzf chevrolet impala volkswagen t2 diamondback century tesla model3 carlamotos firetruck audi etron dodge charger_2020 gazelle omafiets harley-deavidson low_rider """ config = parse_arguments() client = carla.Client(config.host, config.port) client.set_timeout(5.0) world = client.get_world() carla_map = world.get_map() weather = world.get_weather() weather.sun_altitude_angle = -15 world.set_weather(weather) vehicle_blueprints = world.get_blueprint_library().filter("vehicle.*") spawn_point = carla_map.get_spawn_points()[0] vehicle = None pedestrian = None spectator = world.get_spectator() for blueprint in vehicle_blueprints: try: print("Showing vehicle", blueprint) vehicle = world.spawn_actor(blueprint, spawn_point) world.wait_for_tick() vls = carla.VehicleLightState light_state = vls(vls.LowBeam | vls.Interior | vls.Reverse | vls.Position) vehicle.set_light_state(light_state) iterations = 100 theta = np.linspace(0, 1*math.pi, iterations) phi = math.pi / 4 r = 5 for jdx in range(iterations): transform = carlautil.spherical_to_camera_watcher_transform( r, theta[jdx], phi, location=vehicle.get_location()) # transform = carlautil.strafe_transform( # transform, right=0, above=0) spectator.set_transform(transform) world.wait_for_tick() finally: if vehicle: vehicle.destroy()
def visualize_wheel_turning_3(): config = parse_arguments() client = carla.Client(config.host, config.port) client.set_timeout(10.0) world = client.get_world() carla_map = world.get_map() blueprint = world.get_blueprint_library().find("vehicle.audi.a2") spawn_point = carla_map.get_spawn_points()[0] vehicle = None vwl = carla.VehicleWheelLocation try: vehicle = world.spawn_actor(blueprint, spawn_point) for _ in range(20): world.wait_for_tick() transform = carlautil.spherical_to_camera_watcher_transform( 3, math.pi, math.pi*(1/6), location=vehicle.get_location()) world.get_spectator().set_transform(transform) for _ in range(20): world.wait_for_tick() fl_angles = dict() fr_angles = dict() def plot(snapshot): fl_angle = vehicle.get_wheel_steer_angle(vwl.FL_Wheel) fr_angle = vehicle.get_wheel_steer_angle(vwl.FR_Wheel) fl_angles[snapshot.timestamp.elapsed_seconds] = fl_angle fr_angles[snapshot.timestamp.elapsed_seconds] = fr_angle def derivative(x, y): assert len(x) == len(y) dy = [None]*(len(x) - 1) for i in range(len(x) - 1): dy[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]) assert len(x[:-1]) == len(dy) return x[:-1], dy ## Block 1 callback_id = world.on_tick(plot) """get_wheel_steer_angle() returns angle in degrees between 45 and 70 deg so the actual turning is more like 57.5 deg. The wheel on the side the car is turning turns a higher angle. Wheels reach max turns from 0 in 0.2 seconds so 287.5 deg/s.""" steering_choices = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] # steering_choices = [0.6] for steer in steering_choices: for _ in range(6): world.wait_for_tick() time.sleep(0.8) for _ in range(6): world.wait_for_tick() vehicle.apply_control(carla.VehicleControl(steer=steer)) for _ in range(6): world.wait_for_tick() time.sleep(0.8) for _ in range(6): world.wait_for_tick() vehicle.apply_control(carla.VehicleControl(steer=0)) for _ in range(6): world.wait_for_tick() time.sleep(0.8) world.remove_on_tick(callback_id) world.wait_for_tick() fig, axes = plt.subplots(1, 2, figsize=(20,10)) _time, _angles = util.unzip(fl_angles.items()) axes[0].plot(_time, _angles, "-bo", label="FL_Wheel") _time, _dangles = derivative(_time, _angles) axes[1].plot(_time, _dangles, "-bo", label="FL_Wheel") _time, _angles = util.unzip(fr_angles.items()) axes[0].plot(_time, _angles, "-ro", label="FR_Wheel") _time, _dangles = derivative(_time, _angles) axes[1].plot(_time, _dangles, "-ro", label="FR_Wheel") axes[0].legend() fig.savefig("out/steer") # plt.show() finally: if vehicle: vehicle.destroy() vehicle = None