Skip to content

Carla Bridge

CarlaBridge ¤

convert_control_from_agent_to_source(self, control) ¤

Converts control to carla.VehicleControl

Source code in Bridges/carla_bridge.py
def convert_control_from_agent_to_source(
        self, control: VehicleControl
) -> carla.VehicleControl:
    """Converts control to carla.VehicleControl"""
    return carla.VehicleControl(
        throttle=abs(control.throttle),
        steer=control.steering,
        brake=0,
        hand_brake=False,
        reverse=True if control.throttle < 0 else False,
        manual_gear_shift=False,
        gear=1,
    )

convert_control_from_source_to_agent(self, source) ¤

Convert CARLA raw vehicle control to VehicleControl(throttle,steering).

Source code in Bridges/carla_bridge.py
def convert_control_from_source_to_agent(
        self, source: carla.VehicleControl
) -> VehicleControl:
    """Convert CARLA raw vehicle control to VehicleControl(throttle,steering)."""

    return VehicleControl(
        throttle=-1 * source.throttle if source.reverse else source.throttle,
        steering=source.steer,
    )

convert_depth_from_source_to_agent(self, source) ¤

Convert CARLA raw Image to a Union with Depth numpy array.

Source code in Bridges/carla_bridge.py
def convert_depth_from_source_to_agent(
        self, source: carla.Image
) -> Union[DepthData, None]:
    """Convert CARLA raw Image to a Union with Depth numpy array."""
    try:
        array = np.frombuffer(source.raw_data, dtype=np.dtype("uint8"))
        array = np.reshape(array, (source.height, source.width, 4))  # BGRA
        array = array[:, :, :3]  # BGR
        array = array[:, :, ::-1]  # RGB
        array = png_to_depth(array)
        return DepthData(data=array)
    except:
        return None

convert_imu_from_source_to_agent(self, source) ¤

Convert CARLA raw IMUData to IMUData(accelerometer, gyroscope).

Source code in Bridges/carla_bridge.py
def convert_imu_from_source_to_agent(self, source: IMUSensor) -> IMUData:
    """Convert CARLA raw IMUData to IMUData(accelerometer, gyroscope)."""
    return IMUData(
        accelerometer=Vector3D(
            x=source.accelerometer[0],
            y=source.accelerometer[1],
            z=source.accelerometer[2],
        ),
        gyroscope=Vector3D(
            x=source.gyroscope[0], y=source.gyroscope[1], z=source.gyroscope[2]
        ),
    )

convert_location_from_agent_to_source(self, source) ¤

Convert Agent's Location to a Carla.Location.

Source code in Bridges/carla_bridge.py
def convert_location_from_agent_to_source(self, source: Location) -> carla.Location:
    """Convert Agent's Location to a Carla.Location."""
    return carla.Location(x=source.x, y=-source.z, z=source.y)

convert_location_from_source_to_agent(self, source) ¤

Convert Location data from Carla.location to Agent's location data type invert the Z axis to make it into right hand coordinate system

Parameters:

Name Type Description Default
source Location

carla.location

required

Returns:

Type Description
Location
Source code in Bridges/carla_bridge.py
def convert_location_from_source_to_agent(self, source: carla.Location) -> Location:
    """
    Convert Location data from Carla.location to Agent's location data type
    invert the Z axis to make it into right hand coordinate system
    Args:
        source: carla.location

    Returns:

    """
    return Location(x=source.x, y=source.z, z=source.y)

convert_rgb_from_source_to_agent(self, source) ¤

Convert CARLA raw Image to a Union with RGB numpy array.

Source code in Bridges/carla_bridge.py
def convert_rgb_from_source_to_agent(
        self, source: carla.Image
) -> Union[RGBData, None]:
    """Convert CARLA raw Image to a Union with RGB numpy array."""

    try:
        source.convert(cc.Raw)
        rgb_image = self._to_rgb_array(source)
        return RGBData(data=rgb_image)
    except:
        return None

convert_rotation_from_agent_to_source(self, source) ¤

Convert Agent's Rotation to a Carla.Rotation.

Source code in Bridges/carla_bridge.py
def convert_rotation_from_agent_to_source(self, source: Rotation) -> carla.Rotation:
    """Convert Agent's Rotation to a Carla.Rotation."""
    return carla.Rotation(pitch=source.yaw, yaw=source.pitch, roll=source.roll)

convert_rotation_from_source_to_agent(self, source) ¤

Convert a CARLA raw rotation to Rotation(pitch=float,yaw=float,roll=float).

Source code in Bridges/carla_bridge.py
def convert_rotation_from_source_to_agent(self, source: carla.Rotation) -> Rotation:
    """Convert a CARLA raw rotation to Rotation(pitch=float,yaw=float,roll=float)."""
    roll, pitch, yaw = source.roll, source.pitch, source.yaw
    if yaw <= 90:
        yaw = yaw+90
    else:
        yaw = yaw - 270
    return Rotation(roll=roll, pitch=pitch, yaw=-yaw)

convert_sensor_data_from_source_to_agent(self, source) ¤

Returns CARLA Sensors Data from raw front RGB, rear RGB, front depth, and IMU Data.

Source code in Bridges/carla_bridge.py
def convert_sensor_data_from_source_to_agent(self, source: dict) -> SensorsData:
    """Returns CARLA Sensors Data from raw front RGB, rear RGB, front depth, and IMU Data."""
    return SensorsData(
        front_rgb=self.convert_rgb_from_source_to_agent(
            source=source.get("front_rgb", None)
        ),
        rear_rgb=self.convert_rgb_from_source_to_agent(
            source=source.get("rear_rgb", None)
        ),
        front_depth=self.convert_depth_from_source_to_agent(
            source=source.get("front_depth", None)
        ),
        imu_data=self.convert_imu_from_source_to_agent(
            source=source.get("imu", None)
        ),
    )

convert_transform_from_agent_to_source(self, source) ¤

Convert Agent's Transform to a Carla.Transform.

Source code in Bridges/carla_bridge.py
def convert_transform_from_agent_to_source(
        self, source: Transform
) -> carla.Transform:
    """Convert Agent's Transform to a Carla.Transform."""
    return carla.Transform(
        location=self.convert_location_from_agent_to_source(source=source.location),
        rotation=self.convert_rotation_from_agent_to_source(source=source.rotation),
    )

convert_transform_from_source_to_agent(self, source) ¤

Convert CARLA raw location and rotation to Transform(location,rotation).

Source code in Bridges/carla_bridge.py
def convert_transform_from_source_to_agent(
        self, source: carla.Transform
) -> Transform:
    """Convert CARLA raw location and rotation to Transform(location,rotation)."""
    return Transform(
        location=self.convert_location_from_source_to_agent(source=source.location),
        rotation=self.convert_rotation_from_source_to_agent(source=source.rotation),
    )

convert_vector3d_from_agent_to_source(self, vector3d) ¤

Convert Vector3D Object to a CARLA raw Vector3d Data.

Source code in Bridges/carla_bridge.py
def convert_vector3d_from_agent_to_source(
        self, vector3d: Vector3D
) -> carla.Vector3D:
    """Convert Vector3D Object to a CARLA raw Vector3d Data."""
    return carla.Vector3D(x=vector3d.x, y=vector3d.y, z=vector3d.z)

convert_vector3d_from_source_to_agent(self, source) ¤

Convert CARLA raw Vector3d Data to a Vector3D Object.

Source code in Bridges/carla_bridge.py
def convert_vector3d_from_source_to_agent(self, source: carla.Vector3D) -> Vector3D:
    """Convert CARLA raw Vector3d Data to a Vector3D Object."""
    return Vector3D(x=source.x, y=source.y, z=source.z)

convert_vehicle_from_source_to_agent(self, source) ¤

Converts Velocity, Transform, and Control of carla.Vehicle

Source code in Bridges/carla_bridge.py
def convert_vehicle_from_source_to_agent(self, source: carla.Vehicle) -> Vehicle:
    """Converts Velocity, Transform, and Control of carla.Vehicle"""
    control: VehicleControl = self.convert_control_from_source_to_agent(
        source.get_control()
    )
    # this is cheating here, vehicle does not know its own location
    transform: Transform = self.convert_transform_from_source_to_agent(
        source.get_transform()
    )
    velocity: Vector3D = self.convert_vector3d_from_source_to_agent(
        source.get_velocity()
    )
    return Vehicle(velocity=velocity, transform=transform, control=control)