Skip to content

Commit f4c1e41

Browse files
glopezdiestglopezdiest
andauthored
[OSC2.0] Trajectory fixes for slow speeds (#1150)
* Fixes for slow speeds * misspelling Co-Authored-By: glopezdiest <[email protected]>
1 parent ab5996d commit f4c1e41

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

srunner/examples/follow_trajectory.osc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ scenario top:
1212
do parallel(duration: 18s): # Execute the following lines in parallel
1313

1414
ego_vehicle.drive(path) with:
15-
follow_trajectory(input_file: "srunner/examples/resources/ego_logs.csv", control:"physics")
15+
follow_trajectory(input_file: "srunner/examples/resources/ego_logs.csv", control: "physics")
1616

1717
npc1.drive(path) with:
18-
follow_trajectory(input_file: "srunner/examples/resources/npc1_logs.csv", control:"physics")
18+
follow_trajectory(input_file: "srunner/examples/resources/npc1_logs.csv", control: "physics")
1919

2020
npc2.drive(path) with:
21-
follow_trajectory(input_file: "srunner/examples/resources/npc2_logs.csv", control:"physics")
21+
follow_trajectory(input_file: "srunner/examples/resources/npc2_logs.csv", control: "physics")

srunner/osc2_stdlib/path.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ def check(cls, pos) -> bool:
139139
len_ok = carla_data.CarlaDataProvider.check_road_length(wp, cls._length)
140140
if not len_ok:
141141
return False
142+
142143
# Check if the test road is signposted
143144
if cls.sign_type:
144145
is_sign = PathTrafficSign.path_has_traffic_sign(
145146
wp, cls.sign_type, cls._length
146147
)
147148
if not is_sign:
148149
return False
150+
149151
# Restricted test roads cannot be signposted
150152
if cls.sign_types:
151153
no_sign = PathTrafficSign.path_has_no_traffic_signs(

srunner/scenariomanager/actorcontrols/vehicle_teleport_control.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class VehicleTeleportControl(BasicControl):
3030

3131

3232
def __init__(self, actor, args=None):
33+
self._prev_yaw = None
34+
self._distance_threshold = 0.01
3335
super(VehicleTeleportControl, self).__init__(actor)
3436

3537
def reset(self):
@@ -49,9 +51,17 @@ def run_step(self):
4951
return
5052
if not self._start_time:
5153
self._start_time = GameTime.get_time()
54+
if not self._prev_yaw:
55+
self._prev_yaw = self._actor.get_transform().rotation.yaw
5256

5357
transform = self._waypoints.pop(0)
5458
vec = self._waypoints[0].location - transform.location
55-
yaw = math.degrees(math.atan2(vec.y, vec.x))
59+
60+
if abs(vec.x) < self._distance_threshold and abs(vec.y) < self._distance_threshold:
61+
yaw = self._prev_yaw # Vehicle is stopped, avoid changing the yaw
62+
else:
63+
yaw = math.degrees(math.atan2(vec.y, vec.x))
5664

5765
self._actor.set_transform(carla.Transform(transform.location, carla.Rotation(yaw=yaw)))
66+
67+
self._prev_yaw = yaw

srunner/scenariomanager/actorcontrols/vehicle_velocity_control.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self, actor, args=None):
4848
# This is the maximum amount of time used to aim the vehicle towards the next point.
4949
# Used to avoid weird lateral slides if the time between point is high.
5050
self._max_yaw_time = 1
51+
self._distance_threshold = 1
5152

5253
def reset(self):
5354
"""
@@ -62,6 +63,7 @@ def run_step(self):
6263
Teleports the vehicle to a given waypoint on each tick. Use the location from the waypoints
6364
but calculate the transform so that it is always pointing towards the next waypoint.
6465
"""
66+
6567
if not self._waypoints:
6668
return
6769
if not self._start_time:
@@ -88,17 +90,22 @@ def run_step(self):
8890
linear_speed = (target_transform.location.distance(current_transform.location)) / delta_time
8991
linear_velocity = linear_speed * target_vec
9092

91-
delta_yaw = math.degrees(math.atan2(target_vec.y, target_vec.x)) - current_transform.rotation.yaw
93+
self._actor.set_target_velocity(linear_velocity)
9294

93-
if math.fabs(delta_yaw) > 360:
94-
delta_yaw = delta_yaw % 360
95+
96+
for i in range(len(self._waypoints)):
97+
target_transform = self._waypoints[i]
98+
target_time = self._times[i]
99+
if current_transform.location.distance(target_transform.location) > self._distance_threshold:
100+
break
101+
102+
delta_time = target_time + self._start_time - current_time
103+
delta_yaw = math.degrees(math.atan2(target_vec.y, target_vec.x)) - current_transform.rotation.yaw
104+
delta_yaw = delta_yaw % 360
95105
if delta_yaw > 180:
96106
delta_yaw = delta_yaw - 360
97-
elif delta_yaw < -180:
98-
delta_yaw = delta_yaw + 360
99107
angular_speed = delta_yaw / min(delta_time, self._max_yaw_time)
100108
angular_velocity = carla.Vector3D(0, 0, angular_speed)
101109

102-
self._actor.set_target_velocity(linear_velocity)
103110
self._actor.set_target_angular_velocity(angular_velocity)
104111
self._actor.apply_control(carla.VehicleControl(throttle=0.3)) # Make the wheels turn

0 commit comments

Comments
 (0)