### Implement RL Training Loop with ROAR_PY and CARLA Source: https://context7.com/augcog/roar_py/llms.txt This Python code demonstrates setting up a CARLA simulation environment using ROAR_PY, spawning a vehicle with a camera sensor, retrieving Gymnasium-compatible action and observation spaces, and executing a basic training loop. The loop involves receiving observations, sampling a random action, applying the action, and stepping the simulation. It is designed for training RL agents and requires CARLA and ROAR_PY to be installed. ```python import roar_py_interface import numpy as np import asyncio import carla import roar_py_carla async def rl_training_loop(): # Setup environment carla_client = carla.Client('localhost', 2000) carla_client.set_timeout(5.0) roar_py_instance = roar_py_carla.RoarPyCarlaInstance(carla_client) carla_world = roar_py_instance.world carla_world.set_asynchronous(False) carla_world.set_control_steps(0.05, 0.01) # Spawn vehicle with sensors vehicle = carla_world.spawn_vehicle( "vehicle.tesla.model3", carla_world.spawn_points[0][0], carla_world.spawn_points[0][1] ) camera = vehicle.attach_camera_sensor( roar_py_interface.RoarPyCameraSensorDataRGB, np.array([-2.0, 0.0, 1.5]), np.array([0, 0, 0]), image_width=84, image_height=84 ) # Get Gymnasium spaces action_space = vehicle.get_action_spec() observation_space = vehicle.get_gym_observation_spec() print(f"Action space: {action_space}") print(f"Observation space: {observation_space}") # Training loop for episode in range(100): # Reset (in real use, reset vehicle position) await carla_world.step() for step in range(1000): # Get observation obs = await vehicle.receive_observation() gym_obs = vehicle.convert_obs_to_gym_obs(obs) # Sample action (replace with your RL agent) action = { "throttle": np.array([0.5]), "steer": np.array([np.random.uniform(-0.3, 0.3)]), "brake": np.array([0.0]), "hand_brake": np.array([0]), "reverse": np.array([0]) } # Apply action await vehicle.apply_action(action) await carla_world.step() # Calculate reward, check done, etc. # Your RL training logic here roar_py_instance.close() if __name__ == '__main__': asyncio.run(rl_training_loop()) ``` -------------------------------- ### Get and Use CARLA Spawn Points Source: https://context7.com/augcog/roar_py/llms.txt Retrieve all available spawn points on the current CARLA map and use them to place vehicles. Spawn points are provided as location and rotation tuples. Random selection is demonstrated for vehicle placement. ```python # Get all spawn points spawn_points = carla_world.spawn_points print(f"Available spawn points: {len(spawn_points)}") # Each spawn point is a tuple of (location, rotation) for i, (location, rotation) in enumerate(spawn_points[:5]): print(f"Spawn {i}:") print(f" Location: {location}") # np.ndarray [x, y, z] print(f" Rotation: {rotation}") # np.ndarray [roll, pitch, yaw] in radians # Spawn at random point random_idx = np.random.randint(len(spawn_points)) spawn_loc, spawn_rot = spawn_points[random_idx] vehicle = carla_world.spawn_vehicle( "vehicle.tesla.model3", spawn_loc + np.array([0, 0, 2.0]), # Add 2m height for safety spawn_rot ) ``` -------------------------------- ### Generate Native Waypoints using Global Route Planner (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Iterates through the sparse waypoints (`dat_np_sparse`) to generate a series of native CARLA waypoints. It creates `carla.Location` objects for start and end points and uses the `GlobalRoutePlanner` to trace routes between them. Handles potential `nx.NetworkXNoPath` exceptions by skipping the pair. ```python for i in range(len(dat_np_sparse)): curr_start = dat_np_sparse[i] curr_end = dat_np_sparse[(i+1)%len(dat_np_sparse)] loc_1 = carla.Location(curr_start[0], curr_start[1], curr_start[2]) loc_2 = carla.Location(curr_end[0], curr_end[1], curr_end[2]) try: native_ws += grp.trace_route(loc_1, loc_2) except nx.NetworkXNoPath: continue ``` -------------------------------- ### Initialize CARLA and ROAR Py Instances (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Sets up connections to a CARLA simulator and initializes the ROAR Py interface. It establishes a CARLA client connection, creates a ROAR Py CARLA instance, obtains the CARLA world object, and initializes a `GlobalRoutePlanner`. ```python import carla import roar_py_carla import roar_py_interface import networkx as nx carla_client = carla.Client('localhost', 2000) carla_client.set_timeout(5.0) roar_py_instance = roar_py_carla.RoarPyCarlaInstance(carla_client) roar_py_world = roar_py_instance.world grp = GlobalRoutePlanner(roar_py_world._native_carla_map, 2.0) native_ws = [] ``` -------------------------------- ### Initialize ROAR-Py and CARLA Client Source: https://github.com/augcog/roar_py/blob/main/utilities/trace_waypoints.ipynb Establishes a connection to the CARLA server and initializes the ROAR-Py CARLA instance. It configures asynchronous mode and control step timings for the simulation world. ```python import roar_py_carla import roar_py_interface import carla import numpy as np from typing import List, Tuple import transforms3d as tr3d carla_client = carla.Client('localhost', 2000) carla_client.set_timeout(15.0) roar_py_instance = roar_py_carla.RoarPyCarlaInstance(carla_client) roar_py_world = roar_py_instance.world roar_py_world.set_asynchronous(True) roar_py_world.set_control_steps(0.00, 0.005) ``` -------------------------------- ### Initialize Carla Instance and World in ROAR_PY (Python) Source: https://context7.com/augcog/roar_py/llms.txt Connects to a Carla simulator instance and configures the simulation world. It sets up the ROAR_PY-Carla integration, configures synchronous/asynchronous modes, and cleans up previous actors. Requires `carla`, `roar_py_carla`, and `asyncio`. ```python import carla import roar_py_carla import asyncio async def main(): # Connect to Carla server carla_client = carla.Client('localhost', 2000) carla_client.set_timeout(5.0) # Create ROAR_PY instance roar_py_instance = roar_py_carla.RoarPyCarlaInstance(carla_client) # Get world and configure simulation mode carla_world = roar_py_instance.world carla_world.set_asynchronous(True) # or False for synchronous mode carla_world.set_control_steps(0.0, 0.005) # timestep, substep # Step the world await carla_world.step() # Clean up unregistered actors from previous runs roar_py_instance.clean_actors_not_registered() try: # Your simulation code here pass finally: roar_py_instance.close() if __name__ == '__main__': asyncio.run(main()) ``` -------------------------------- ### Create CARLA Actor with Custom Sensors Source: https://context7.com/augcog/roar_py/llms.txt Spawn a vehicle and attach various custom sensors, including RGB cameras, collision detectors, accelerometers, and gyroscopes. The code demonstrates how to configure sensor parameters and retrieve sensor data. ```python # Spawn vehicle vehicle = carla_world.spawn_vehicle( "vehicle.audi.a2", spawn_points[0][0] + np.array([0, 0, 1]), spawn_points[0][1], name="custom_vehicle" ) # Attach front camera front_camera = vehicle.attach_camera_sensor( roar_py_interface.RoarPyCameraSensorDataRGB, np.array([2.5, 0.0, 1.0]), # Front bumper np.array([0, 0, 0]), image_width=800, image_height=600, name="front_cam" ) # Attach rear camera rear_camera = vehicle.attach_camera_sensor( roar_py_interface.RoarPyCameraSensorDataRGB, np.array([-2.5, 0.0, 1.0]), # Rear np.array([0, 0, np.pi]), # 180 degree rotation image_width=800, image_height=600, name="rear_cam" ) # Attach collision sensor collision_sensor = vehicle.attach_collision_sensor(name="collision_detector") # Attach IMU sensors accel_sensor = vehicle.attach_accelerometer_sensor(name="accelerometer") gyro_sensor = vehicle.attach_gyroscope_sensor(name="gyroscope") # Get all sensors all_sensors = list(vehicle.get_sensors()) print(f"Total sensors: {len(all_sensors)}") # Collect all observations observations = await vehicle.receive_observation() front_image = observations["front_cam"] rear_image = observations["rear_cam"] collision_data = observations["collision_detector"] accel_data = observations["accelerometer"] gyro_data = observations["gyroscope"] ``` -------------------------------- ### Configure CARLA World Timestep Source: https://context7.com/augcog/roar_py/llms.txt Set the CARLA world to synchronous or asynchronous mode with configurable timesteps. This allows precise control over physics simulation speed and frequency. Asynchronous mode uses variable timesteps, while synchronous mode uses fixed timesteps. ```python # Synchronous mode with fixed timestep carla_world.set_asynchronous(False) carla_world.set_control_steps( control_timestep=0.05, # 50ms per tick (20 FPS) control_substimestep=0.01 # 10ms physics substeps ) # Variable timestep (asynchronous mode) carla_world.set_asynchronous(True) carla_world.set_control_steps(0.0, 0.005) # Step the world and get elapsed time dt = await carla_world.step() print(f"Time delta: {dt} seconds") print(f"Total elapsed: {carla_world.last_tick_elapsed_seconds} seconds") # Check mode is_async = carla_world.is_asynchronous timestep = carla_world.control_timestep ``` -------------------------------- ### Import Libraries for Waypoint Processing (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Imports necessary libraries including NumPy for numerical operations, Pandas for data manipulation, and specific modules from ROAR Py and CARLA for interacting with the simulation environment. ```python import numpy as np import pandas as pd from roar_py_carla.carla_agents.navigation.global_route_planner import GlobalRoutePlanner ``` -------------------------------- ### Implement Waypoint Following Controller in Python Source: https://context7.com/augcog/roar_py/llms.txt Implements a proportional controller for autonomous vehicle waypoint following. It includes functions for normalizing radian angles and filtering waypoints to find the closest one to the vehicle's current location. The controller calculates steering and throttle based on the vehicle's speed and heading towards the target waypoint. ```python def normalize_rad(rad: float): return (rad + np.pi) % (2 * np.pi) - np.pi def filter_waypoints(location: np.ndarray, current_idx: int, waypoints: list) -> int: """Find closest waypoint to current location""" def dist_to_waypoint(waypoint): return np.linalg.norm(location[:2] - waypoint.location[:2]) for i in range(current_idx, len(waypoints) + current_idx): if dist_to_waypoint(waypoints[i % len(waypoints)]) < 3: return i % len(waypoints) return current_idx # Main control loop current_waypoint_idx = 10 waypoints = carla_world.maneuverable_waypoints while True: await carla_world.step() # Get vehicle state vehicle_location = vehicle.get_3d_location() vehicle_rotation = vehicle.get_roll_pitch_yaw() vehicle_velocity = vehicle.get_linear_3d_velocity() # Find target waypoint current_waypoint_idx = filter_waypoints( vehicle_location, current_waypoint_idx, waypoints ) target_waypoint = waypoints[(current_waypoint_idx + 3) % len(waypoints)] # Calculate steering angle vector_to_waypoint = (target_waypoint.location - vehicle_location)[:2] heading_to_waypoint = np.arctan2(vector_to_waypoint[1], vector_to_waypoint[0]) delta_heading = normalize_rad(heading_to_waypoint - vehicle_rotation[2]) # Proportional steering controller speed = np.linalg.norm(vehicle_velocity) steer_control = -8.0 / np.sqrt(speed) * delta_heading / np.pi if speed > 1e-2 else -np.sign(delta_heading) steer_control = np.clip(steer_control, -1.0, 1.0) # Speed controller targeting 20 m/s throttle_control = 0.05 * (20 - speed) # Apply control control = { "throttle": np.clip(throttle_control, 0.0, 1.0), "steer": steer_control, "brake": np.clip(-throttle_control, 0.0, 1.0), "hand_brake": 0.0, "reverse": 0 } await vehicle.apply_action(control) ``` -------------------------------- ### Spawn Vehicle in Carla World using ROAR_PY (Python) Source: https://context7.com/augcog/roar_py/llms.txt Spawns a vehicle actor in the Carla simulation world at a specified location and rotation. It allows for customization of the vehicle blueprint, color, and automatic gear shifting. Requires `numpy` and access to `carla_world` object. ```python import numpy as np # Get spawn points from the map spawn_points = carla_world.spawn_points spawn_point, spawn_rpy = spawn_points[1] # Spawn vehicle with automatic gear shifting vehicle = carla_world.spawn_vehicle( "vehicle.tesla.model3", # Blueprint ID spawn_point + np.array([0, 0, 2.0]), # Location [x, y, z] spawn_rpy, # Rotation [roll, pitch, yaw] in radians auto_gear=True, # Enable automatic gear shifting name="my_vehicle", rgba=np.array([255, 0, 0, 255]) # Optional: Red color ) # Access vehicle properties bounding_box = vehicle.bounding_box print(f"Vehicle extent: {bounding_box.extent}") print(f"Vehicle location: {vehicle.get_3d_location()}") print(f"Vehicle velocity: {vehicle.get_linear_3d_velocity()}") print(f"Vehicle rotation: {vehicle.get_roll_pitch_yaw()}") ``` -------------------------------- ### Load and Reload CARLA Maps Source: https://context7.com/augcog/roar_py/llms.txt Manage CARLA maps by listing available maps, loading specific ones, or reloading the current map. This operation destroys all existing actors. It also retrieves CARLA client and server version information. ```python # Get available maps available_maps = roar_py_instance.get_available_maps() print("Available maps:", available_maps) # Load a specific map (destroys all actors) roar_py_instance.load_world("Town05", reset_settings=True) # Reload current map roar_py_instance.reload_world(reset_settings=True) # Get map name map_name = carla_world.map_name print(f"Current map: {map_name}") # Get version info client_version = roar_py_instance.get_client_version() server_version = roar_py_instance.get_server_version() print(f"Client: {client_version}, Server: {server_version}") ``` -------------------------------- ### Apply Vehicle Control Actions with ROAR_PY (Python) Source: https://context7.com/augcog/roar_py/llms.txt Applies control commands (throttle, steer, brake, gear) to a vehicle actor using a Gymnasium-compatible action dictionary. This enables interaction with the simulated vehicle. Requires `numpy` and the `vehicle` object. ```python # Define control action control = { "throttle": np.array([0.5]), # 0.0 to 1.0 "steer": np.array([0.0]), # -1.0 to 1.0 "brake": np.array([0.0]), # 0.0 to 1.0 "hand_brake": np.array([0]), # 0 or 1 "reverse": np.array([0]) # 0 or 1 } # For manual gear vehicles, add: # control["target_gear"] = 3 # Apply action asynchronously await vehicle.apply_action(control) # Get vehicle action specification (Gymnasium Space) action_spec = vehicle.get_action_spec() print(action_spec) # Dict space with throttle, steer, brake, etc. ``` -------------------------------- ### Generate and Save Waypoints Source: https://github.com/augcog/roar_py/blob/main/utilities/trace_waypoints.ipynb Generates a list of waypoints from CARLA's lane data, converts them into ROAR-Py waypoint objects, and saves them to a compressed NPZ file. Asserts that the number of waypoints per lane is even. ```python waypoint_list : List[roar_py_interface.RoarPyWaypoint] = [] trace_waypoint_laneid = 1 print("LaneIDS: ", roar_py_world.comprehensive_waypoints.keys()) for lane_id in [0, 1]: lane_waypoints = roar_py_world.comprehensive_waypoints[lane_id] assert len(lane_waypoints) % 2 == 0 for i in range(len(lane_waypoints)//2): first_waypoint = lane_waypoints[2*i] second_waypoint = lane_waypoints[2*i+1] real_waypoint = roar_py_interface.RoarPyWaypoint.from_line_representation( first_waypoint.location, second_waypoint.location, first_waypoint.roll_pitch_yaw ) # print(real_waypoint) waypoint_list.append(real_waypoint) roar_py_instance.close() np.savez_compressed("Monza.npz", **roar_py_interface.RoarPyWaypoint.save_waypoint_list(waypoint_list)) ``` -------------------------------- ### Generate and Save Spawn Points (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Calls the `generate_spawn_points` function with the previously generated waypoints to obtain arrays of spawn point locations and rotations. It then prints the shape of the spawn point locations array and saves these arrays into a compressed NumPy `.npz` file named 'final_major_map_spawn_points.npz'. ```python spawn_point_locations, spawn_rotations = generate_spawn_points(waypoints) print(spawn_point_locations.shape) np.savez_compressed("final_major_map_spawn_points.npz", locations=spawn_point_locations, rotations=spawn_rotations) ``` -------------------------------- ### Visualize Waypoints with Debug Arrows Source: https://github.com/augcog/roar_py/blob/main/utilities/trace_waypoints.ipynb Iterates through a list of waypoints and draws debug arrows in the CARLA world to visualize the forward direction of each waypoint. Arrows are green and persist indefinitely. ```python for waypoint in waypoint_list: origin_loc = waypoint.location forward_loc = waypoint.location + tr3d.euler.euler2mat(*waypoint.roll_pitch_yaw) @ np.array([0.5,0.0,0.0]) roar_py_world.carla_world.debug.draw_arrow( roar_py_carla.location_to_carla(origin_loc), roar_py_carla.location_to_carla(forward_loc), thickness=0.1, arrow_size=0.1, color=carla.Color(0,255,0), life_time=-1.0 ) ``` -------------------------------- ### Load and Access Maneuverable Waypoints in Python Source: https://context7.com/augcog/roar_py/llms.txt Retrieves pre-computed or dynamically traced waypoints for path planning and navigation. It allows access to waypoint properties such as location, roll-pitch-yaw, and lane width, and provides functionality to spawn a vehicle at a specific waypoint. ```python # Get maneuverable waypoints (racing line or road center line) waypoints = carla_world.maneuverable_waypoints if waypoints is not None: print(f"Total waypoints: {len(waypoints)}") # Access waypoint properties for i, waypoint in enumerate(waypoints[:10]): location = waypoint.location # np.ndarray [x, y, z] rpy = waypoint.roll_pitch_yaw # np.ndarray [roll, pitch, yaw] lane_width = waypoint.lane_width # float print(f"Waypoint {i}: pos={location}, rotation={rpy}, width={lane_width}") # Spawn vehicle at specific waypoint vehicle = carla_world.spawn_vehicle( "vehicle.audi.a2", waypoints[10].location + np.array([0, 0, 1]), waypoints[10].roll_pitch_yaw ) ``` -------------------------------- ### Display DataFrame Content (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Prints the content of the Pandas DataFrame `dat` to the console. This is useful for inspecting the loaded waypoint data and understanding its structure before further processing. ```python print(dat) ``` -------------------------------- ### Attach LiDAR Sensor to Vehicle in Python Source: https://context7.com/augcog/roar_py/llms.txt Attaches a LiDAR sensor to a vehicle to capture 3D point cloud data. Configuration options include location, number of lasers, maximum distance, and field of view. It allows for receiving and processing point cloud data and converting it to a Gymnasium observation. ```python lidar = carla_world.attach_lidar_sensor( location=np.array([0.0, 0.0, 2.5]), # Relative to vehicle roll_pitch_yaw=np.array([0.0, 0.0, 0.0]), num_lasers=32, max_distance=50.0, points_per_second=56000, rotation_frequency=10.0, upper_fov=10.0, lower_fov=-30.0, horizontal_fov=360.0, control_timestep=0.0, name="lidar_sensor", bind_to=vehicle ) # Receive LiDAR data lidar_data = await lidar.receive_observation() # Access point cloud: shape (N, 4) where columns are [x, y, z, intensity] points = lidar_data.lidar_points_data print(f"Number of points: {points.shape[0]}") print(f"Channels: {lidar_data.channels}") print(f"Horizontal angle: {lidar_data.horizontal_angle}") # Convert to Gymnasium observation gym_obs = lidar_data.convert_obs_to_gym_obs() ``` -------------------------------- ### Convert CARLA Waypoints to ROAR Py Waypoints (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Transforms the generated native CARLA waypoints (`native_ws`) into ROAR Py `RoarPyWaypoint` objects. This involves converting CARLA transforms to ROAR Py format, adjusting orientation, and creating new `RoarPyWaypoint` instances with associated lane width. ```python real_ws = [] for native_ww in native_ws: w = native_ww[0] transform_w = roar_py_carla.transform_from_carla(w.transform) transform_w[1][2] += np.pi/2 real_w = roar_py_interface.RoarPyWaypoint( transform_w[0], transform_w[1], w.lane_width ) real_ws.append(real_w) ``` -------------------------------- ### Define Function to Generate Spawn Points (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Defines a Python function `generate_spawn_points` that takes a list of `RoarPyWaypoint` objects. It selects a subset of these waypoints (e.g., every 10th waypoint) to serve as spawn points, extracting their locations and rotations. These are then stacked into NumPy arrays. ```python from roar_py_interface import RoarPyWaypoint from typing import List, Tuple from roar_py_carla.utils import * import carla def generate_spawn_points(waypoints : List[RoarPyWaypoint]) -> Tuple[np.ndarray, np.ndarray]: spawn_point_locations = [] spawn_rotations = [] for spawn_point in waypoints[::len(waypoints)//10]: spawn_point_locations.append(spawn_point.location) spawn_rotations.append(spawn_point.roll_pitch_yaw) return np.stack(spawn_point_locations, axis=0), np.stack(spawn_rotations, axis=0) ``` -------------------------------- ### Receive Multi-Sensor Observations in Python Source: https://context7.com/augcog/roar_py/llms.txt Enables the collection of observations from multiple sensors (camera, collision, location, velocimeter) attached to an actor in a single asynchronous call. It facilitates accessing individual sensor data and retrieving the Gymnasium observation specification for all sensors. ```python # Attach multiple sensors camera = vehicle.attach_camera_sensor( roar_py_interface.RoarPyCameraSensorDataRGB, np.array([-2.0, 0.0, 1.5]), np.array([0, 0, 0]) ) collision_sensor = vehicle.attach_collision_sensor( name="collision" ) location_sensor = vehicle.attach_location_in_world_sensor( name="gps" ) velocimeter = vehicle.attach_velocimeter_sensor( name="speed" ) # Receive all observations at once await carla_world.step() observations = await vehicle.receive_observation() # Access individual sensor data camera_obs = observations["camera"] collision_obs = observations["collision"] location_obs = observations["gps"] velocity_obs = observations["speed"] # Get Gymnasium observation specification obs_spec = vehicle.get_gym_observation_spec() print(obs_spec) # Dict space containing all sensor specs ``` -------------------------------- ### Convert CARLA World Coordinates to Geolocation Source: https://context7.com/augcog/roar_py/llms.txt Transform 3D world coordinates obtained from a vehicle's location into GPS latitude and longitude. This is useful for integrating simulator data with real-world geographic systems or for GPS sensor simulation. ```python # Get vehicle location in world coordinates vehicle_location = vehicle.get_3d_location() # [x, y, z] # Convert to geolocation geolocation = carla_world.get_geolocation(vehicle_location) latitude = geolocation[0] # degrees longitude = geolocation[1] # degrees altitude = geolocation[2] # meters print(f"GPS: {latitude:.6f}, {longitude:.6f}, {altitude:.2f}m") # Useful for GPS sensor simulation or geographic visualization ``` -------------------------------- ### Load Waypoint Data from CSV (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Reads waypoint data from a 'new_right_side_waypoints.txt' file into a Pandas DataFrame. The file is expected to have no header row. The data is assumed to be in a format readable by `pd.read_csv`. ```python dat = pd.read_csv("new_right_side_waypoints.txt", header=None) ``` -------------------------------- ### Attach Camera Sensor to Vehicle in ROAR_PY (Python) Source: https://context7.com/augcog/roar_py/llms.txt Attaches a camera sensor (RGB, Depth, Grayscale, Segmentation) to a vehicle, specifying its relative position, rotation, and camera parameters (FOV, resolution). It allows receiving and processing sensor data, including converting it to PIL images or NumPy arrays. Requires `roar_py_interface` and the `vehicle` object. ```python import roar_py_interface # Attach RGB camera relative to vehicle camera = vehicle.attach_camera_sensor( roar_py_interface.RoarPyCameraSensorDataRGB, # Data type np.array([-2.0 * vehicle.bounding_box.extent[0], 0.0, 3.0 * vehicle.bounding_box.extent[2]]), # Relative position np.array([0, 10/180.0*np.pi, 0]), # Relative rotation [roll, pitch, yaw] fov=90.0, image_width=1024, image_height=768 ) # Receive camera observation await carla_world.step() camera_data = await camera.receive_observation() # Access image data pil_image = camera_data.get_image() # PIL Image numpy_array = camera_data.to_gym() # numpy array (H, W, 3) image_size = camera_data.get_size() # (width, height) # Save image pil_image.save("capture.png") # Other camera types: # RoarPyCameraSensorDataGreyscale - Grayscale images # RoarPyCameraSensorDataDepth - Depth maps # RoarPyCameraSensorDataSemanticSegmentation - Segmentation masks ``` -------------------------------- ### Manipulate and Subset NumPy Waypoint Array (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Performs several operations on the NumPy array `dat_np`: prints its shape, reorders columns (0, 2, 1), and then selects every 50th row to create a sparse waypoint array `dat_np_sparse`. Finally, it prints the shape of the sparse array. ```python print(dat_np.shape) dat_np = dat_np[:,[0,2,1]] dat_np_sparse = dat_np[::50,:] print(dat_np_sparse.shape) ``` -------------------------------- ### Save ROAR Py Waypoints to NPZ File (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Saves the processed list of `RoarPyWaypoint` objects (`real_ws`) into a compressed NumPy `.npz` file named 'final_major_map_waypoints.npz'. It uses the `save_waypoint_list` method from `RoarPyWaypoint` for serialization. ```python np.savez_compressed("final_major_map_waypoints.npz", **roar_py_interface.RoarPyWaypoint.save_waypoint_list(real_ws)) ``` -------------------------------- ### Generate and Save Waypoints using Custom Function (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Calls the `generate_waypoints` function with processed data (`dat_np`) and saves the resulting waypoints to 'final_major_map_waypoints.npz'. It also prints the total number of generated waypoints and the location of the first waypoint. ```python waypoints = generate_waypoints(dat_np, 2, 3.5) print("final length",len(waypoints)) np.savez_compressed("final_major_map_waypoints.npz", **RoarPyWaypoint.save_waypoint_list(waypoints)) print(waypoints[0].location) ``` -------------------------------- ### Define Function to Generate ROAR Py Waypoints (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Defines a Python function `generate_waypoints` that takes a list of 3D locations (as NumPy arrays), a desired distance between waypoints, and a lane width. It processes the input locations to create a list of `RoarPyWaypoint` objects, calculating rotations based on the path's direction. ```python from roar_py_interface import RoarPyWaypoint from typing import List, Tuple from roar_py_carla.utils import * import carla def generate_waypoints(locations : List[np.ndarray], distance_between_waypoints : float, lane_width : float): locations = [location_from_carla(carla.Location(x=location[0], y=location[1], z=location[2])) for location in locations] waypoint_locations = [locations[0]] waypoint_rotations = [] last_location_idx = 0 for i in range(1,len(locations)): next_location = locations[i] dist_to_last_location = np.linalg.norm(next_location - locations[last_location_idx]) if dist_to_last_location > distance_between_waypoints: waypoint_locations.append(next_location) waypoint_rotations.append(np.arctan2(next_location[1] - locations[last_location_idx][1], next_location[0] - locations[last_location_idx][0])) last_location_idx = i waypoint_rotations.append(np.arctan2(locations[0][1] - waypoint_locations[-1][1], locations[0][0] - waypoint_locations[-1][0])) waypoints = [] for i in range(len(waypoint_locations)): waypoints.append(RoarPyWaypoint( waypoint_locations[i], np.array([0,0,waypoint_rotations[i]]), lane_width )) return waypoints ``` -------------------------------- ### Convert DataFrame to NumPy Array (Python) Source: https://github.com/augcog/roar_py/blob/main/utilities/transform_waypoints.ipynb Converts the Pandas DataFrame `dat` into a NumPy array named `dat_np`. This allows for efficient numerical operations and array manipulation. ```python dat_np = np.asarray(dat) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.