BerkeleyLearnVerify / Scenic

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

Following a fixed distance along a curve #204

Open jschiefele opened 7 months ago

jschiefele commented 7 months ago

Versions: Scenic 2.1.0, Python 3.11

Hi.

My goal is to model a scenario where the ego vehicle follows a curved track, and there is an obstacle on this track. I want to be able to specify the distance between ego and obstacle, not as the crow flies, but measured along the track (like a braking distance on a curved road).

I am using the following specifier to obtain this, like in the minimal example below, but do not get the intended results:

from scenic import scenarioFromString

scenario_str = """
track = PolylineRegion([(0., -6.), (0. ,0.), (4., 3.)])
ego = Object at (0. @ -5.)
obstacle = Object following track.orientation from ego.position for 10.
"""
scenario = scenarioFromString(scenario_str)
scene, _ = scenario.generate()
print([obj.position for obj in scene.objects])
# Result:   [(0.0 @ -5.0), (1.9999999999999998 @ 4.0)]
# Expected: [(0.0 @ -5.0), (4.0 @ 3.0)]

The obstacle was not placed on the track, and the ego-obstacle-distance is not 10. Am I misunderstanding something here? Or is there any other syntax to specify a distance along a curve?

Eric-Vin commented 3 months ago

For your first question, the inaccuracy is due to the forward Euler approximation being done internally. You might be able to get better results with a smaller step size (see the note here), but with this method there will always be some noise.

As to your second question, to my knowledge there's no direct way to calculate that distance in Scenic right now, though this has come up in the past and it's something we'd like to add/review a PR adding.

jschiefele commented 3 months ago

Thanks @Eric-Vin. Thinking about your hint with the Euler approximation, I guess the VectorField which defines the orientation of a PolylineRegion is simply not intended to support my specific use-case:

follow_PolylineRegion

My plan now is to modify one of the Regions implemented in Scenic 3 (PathRegion?) by defining something like a 1D VectorField to serve as the track's orientation. Its followFrom() should stay strictly on the linear segments, somehow wrapping the incremental step around to the next linear segment when a corner is reached.

Please let me know if you think this is a reasonable idea (and if this might be of general interest as a PR).

Eric-Vin commented 1 month ago

Apologies for the late response, just saw that this comment was posted! This seems like a reasonable idea to me overall, and the idea for the default vector field for PathRegion seems similar to the one for PolylineRegion.