BerkeleyLearnVerify / Scenic

A compiler and scenario generator for the Scenic scenario description language.
https://scenic-lang.org/
Other
271 stars 93 forks source link

Incorrect roadDirection #176

Closed abol-karimi closed 4 months ago

abol-karimi commented 12 months ago

Example script:

param carla_map = 'Town05'
carla_map = globalParameters.carla_map
param map = f'/home/carla/CarlaUE4/Content/Carla/Maps/OpenDrive/{carla_map}.xodr'
param weather = 'CloudySunset'
model scenic.simulators.carla.model

intersection = network.elements['intersection396']
lane = intersection.incomingLanes[4]
point = new OrientedPoint at lane.centerline[0]

ego = new Car following roadDirection from point for 0

for d in (10, 20, 30, 40):
  car = new Car following roadDirection from point for d

which I run in python with:

import scenic
scenario = scenic.scenarioFromFile('road_direction.scenic',
                                   mode2D=True)
scene, _ = scenario.generate(maxIterations=1)
sim_result = scenario.getSimulator().simulate(scene, maxSteps=600)

produces this scene: Wrong_roadDirection

Platform:

Eric-Vin commented 12 months ago

Hi Abel, I'll take a look at this. It's possible that the map information is incorrect, but it could also be an issue on Scenic's side. Can you provide a seed to reproduce this, ideally using the internal visualizer? Something like:

scenic road_direction.scenic --2d --seed X

abol-karimi commented 12 months ago

scenic road_direction.scenic --2d produces correct results (with any seeds), for example:

roadDirection

so it seems that the bug is in the transformation to Carla's coordinates.

Eric-Vin commented 12 months ago

Hmm interesting. I'll investigate.

abol-karimi commented 11 months ago

There are two problems in scenicToCarlaRotation:

  1. Scenic and Carla use different order of rotations and default headings. When converting a rotation from scenic to Carla, we need to align Scenic's default heading (its y-axis) to Carla's default heading (its x-axis) before changing the order of rotations.
  2. Carla uses Unreal Engine's coordinate system, which has a strange definition as pointed out by AirSim: The system is left-handed and rotations are positive around the x-axis, but negative around the y- and z-axes! Therefore, after changing the order of rotations to what Carla expects, we need to pass the negation of the resulting pitch and roll to Carla.

Here is a diagram of the correspondence of the two frames: Scenic_vs_Carla

This code should fix it:

def scenicToCarlaRotation(orientation):
    yaw, pitch, roll = orientation.r.as_euler("ZXY", degrees=False)
    rot = Orientation.fromEuler(yaw=yaw+math.pi/2, pitch=pitch, roll=roll)
    pitch, yaw, roll = rot.r.as_euler("YZX", degrees=True)
    return carla.Rotation(pitch=-pitch, yaw=yaw, roll=-roll)

and it mostly does fix it, but I still sometimes see a car spawned in the opposite direction even though the output of the code above was correct. Maybe there is a bug in Carla?

abol-karimi commented 11 months ago

One thing I totally missed is that Euler angles in Scenic are intrinsic, not extrinsic! So I don't know what the bug is.

abol-karimi commented 6 months ago

This is my bugfix as we discussed over Slack:

def scenicToCarlaRotation(orientation):
    origin = (0, 0, 0)
    scenic_y_axis = (0, 1, 0)
    obj_3d_heading = orientation.r.apply(scenic_y_axis)
    scenic_yaw = headingOfSegment(origin, obj_3d_heading)
    carla_yaw = -90 - math.degrees(scenic_yaw)
    return carla.Rotation(pitch=0, yaw=carla_yaw, roll=0)