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

ScenarioManager tick reorganized #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
65 changes: 44 additions & 21 deletions leaderboard/scenarios/scenario_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def cleanup(self):
self.end_system_time = None
self.end_game_time = None

self._spectator = None

def load_scenario(self, scenario, agent, rep_number):
"""
Load a new scenario
Expand All @@ -109,6 +111,8 @@ def load_scenario(self, scenario, agent, rep_number):
self.other_actors = scenario.other_actors
self.repetition_number = rep_number

self._spectator = CarlaDataProvider.get_world().get_spectator()

# To print the scenario tree uncomment the next line
# py_trees.display.render_dot_tree(self.scenario_tree)

Expand All @@ -133,13 +137,45 @@ def run_scenario(self):
if snapshot:
timestamp = snapshot.timestamp
if timestamp:
self._tick_scenario(timestamp)
self._tick(timestamp)

def _tick_scenario(self, timestamp):
def _tick_agent(self):
"""
Run next tick of scenario and the agent and tick the world.
Tick the agent.
"""
try:
self._agent_watchdog.resume()
self._agent_watchdog.update()
ego_action = self._agent()
self._agent_watchdog.pause()

# Special exception inside the agent that isn't caused by the agent
except SensorReceivedNoData as e:
raise RuntimeError(e)

except Exception as e:
raise AgentError(e)

self.ego_vehicles[0].apply_control(ego_action)

def _tick_scenario(self):
"""
Tick the scenario tree.
"""
self.scenario_tree.tick_once()

def _tick_carla(self):
"""
Tick the CARLA server.
"""
CarlaDataProvider.get_world().tick(self._timeout)

def _tick(self, timestamp):
"""
Run next tick of scenario and the agent and tick the world.

The tick is separated into different methods to ease profiler annotations.
"""
if self._timestamp_last_run < timestamp.elapsed_seconds and self._running:
self._timestamp_last_run = timestamp.elapsed_seconds

Expand All @@ -149,24 +185,12 @@ def _tick_scenario(self, timestamp):
CarlaDataProvider.on_carla_tick()
self._watchdog.pause()

try:
self._agent_watchdog.resume()
self._agent_watchdog.update()
ego_action = self._agent()
self._agent_watchdog.pause()

# Special exception inside the agent that isn't caused by the agent
except SensorReceivedNoData as e:
raise RuntimeError(e)

except Exception as e:
raise AgentError(e)
self._tick_agent()

self._watchdog.resume()
self.ego_vehicles[0].apply_control(ego_action)

# Tick scenario
self.scenario_tree.tick_once()
self._tick_scenario()

if self._debug_mode:
print("\n")
Expand All @@ -177,13 +201,12 @@ def _tick_scenario(self, timestamp):
if self.scenario_tree.status != py_trees.common.Status.RUNNING:
self._running = False

spectator = CarlaDataProvider.get_world().get_spectator()
ego_trans = self.ego_vehicles[0].get_transform()
spectator.set_transform(carla.Transform(ego_trans.location + carla.Location(z=50),
carla.Rotation(pitch=-90)))
self._spectator.set_transform(carla.Transform(ego_trans.location + carla.Location(z=50),
carla.Rotation(pitch=-90)))

if self._running and self.get_running_status():
CarlaDataProvider.get_world().tick(self._timeout)
self._tick_carla()

def get_running_status(self):
"""
Expand Down