Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature negative speed #3

Merged
merged 5 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions srunner/scenariomanager/actorcontrols/actor_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import carla

from srunner.scenariomanager.actorcontrols.external_control import ExternalControl
from srunner.scenariomanager.actorcontrols.npc_vehicle_control import NpcVehicleControl
from srunner.scenariomanager.actorcontrols.simple_vehicle_control import SimpleVehicleControl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this simple controller affect any other scenarios? I.e. will it always work as intended?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that none of the controllers are optimal but this simple controller is the better of the two imo. The previous controller uses a PID controller to steer the vehicle both lateral and longitudinal which results in some response time (suboptimal for a scenariorunner imo). The new controller sets the velocity and angular velocity of the vehicle directly and thus makes the ScenarioRunner focus more on the scenario itself rather than following a trajectory to the best of its ability. The PID controller might be more "realistic" in a sense, but demands a lot of tuning to work half as good as the real controllers in a car. So in the end it's not really more realistic anyway.

from srunner.scenariomanager.actorcontrols.pedestrian_control import PedestrianControl


Expand Down Expand Up @@ -69,7 +69,7 @@ def __init__(self, actor, control_py_module, args, scenario_file_path):
if isinstance(actor, carla.Walker):
self.control_instance = PedestrianControl(actor)
elif isinstance(actor, carla.Vehicle):
self.control_instance = NpcVehicleControl(actor)
self.control_instance = SimpleVehicleControl(actor)
else:
# use ExternalControl for all misc objects to handle all actors the same way
self.control_instance = ExternalControl(actor)
Expand Down
22 changes: 19 additions & 3 deletions srunner/scenariomanager/actorcontrols/simple_vehicle_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, actor, args=None):
self._consider_traffic_lights = False
self._consider_obstacles = False
self._proximity_threshold = float('inf')
self._waypoint_reached_threshold = 4.0
self._waypoint_reached_threshold = 0.2 # don't know if this is a reasonable value but it's better than 4m (previous).
self._max_deceleration = None
self._max_acceleration = None

Expand Down Expand Up @@ -321,31 +321,47 @@ def _set_new_velocity(self, next_location):
velocity.x = direction.x / direction_norm * target_speed
velocity.y = direction.y / direction_norm * target_speed

if target_speed < 0:
velocity.x = -velocity.x
velocity.y = -velocity.y

self._actor.set_target_velocity(velocity)

# set new angular velocity
current_yaw = CarlaDataProvider.get_transform(self._actor).rotation.yaw
# print(f"Current yaw: {current_yaw}")
# When we have a waypoint list, use the direction between the waypoints to calculate the heading (change)
# otherwise use the waypoint heading directly
if self._waypoints:
delta_yaw = math.degrees(math.atan2(direction.y, direction.x)) - current_yaw
if target_speed < 0:
new_yaw = math.degrees(math.atan2(direction.y, direction.x)) - 180
# print(f"New yaw: {new_yaw}")
delta_yaw = new_yaw - current_yaw
else:
delta_yaw = math.degrees(math.atan2(direction.y, direction.x)) - current_yaw
else:
new_yaw = CarlaDataProvider.get_map().get_waypoint(next_location).transform.rotation.yaw
delta_yaw = new_yaw - current_yaw

# print(f"Delta yaw: {delta_yaw}")
if math.fabs(delta_yaw) > 360:
delta_yaw = delta_yaw % 360

# print(f"Delta yaw after modulo function: {delta_yaw}")

if delta_yaw > 180:
delta_yaw = delta_yaw - 360
elif delta_yaw < -180:
delta_yaw = delta_yaw + 360
# print(f"Delta yaw after half circle function: {delta_yaw}")

angular_velocity = carla.Vector3D(0, 0, 0)
if target_speed == 0:
angular_velocity.z = 0
elif target_speed < 0:
angular_velocity.z = delta_yaw / (direction_norm / -target_speed)
else:
angular_velocity.z = delta_yaw / (direction_norm / target_speed)
# print(f"Angular velocity: {angular_velocity.z}")
self._actor.set_target_angular_velocity(angular_velocity)

self._last_update = current_time
Expand Down
Loading