Skip to content

Commit

Permalink
fix(cr2sumo): traffic light cycle time offset is not considered durin…
Browse files Browse the repository at this point in the history
…g conversion
  • Loading branch information
jrester committed Oct 16, 2024
1 parent 03497d2 commit ca92e4f
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions crdesigner/map_conversion/sumo_map/cr2sumo/traffic_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
from commonroad.scenario.traffic_light import (
TrafficLight,
TrafficLightCycle,
TrafficLightCycleElement,
TrafficLightState,
)
Expand Down Expand Up @@ -111,7 +112,7 @@ def _sync_traffic_light_cycles(traffic_lights: List[TrafficLight]) -> List[List[
)
states = np.array(
[
_cycles_to_states(traffic_light.traffic_light_cycle.cycle_elements, time_steps)
_sample_traffic_light_cycle_to_states(traffic_light.traffic_light_cycle, time_steps)
for traffic_light in traffic_lights
]
).T
Expand All @@ -130,18 +131,16 @@ def _sync_traffic_light_cycles(traffic_lights: List[TrafficLight]) -> List[List[
return res


def _cycles_to_states(cycles: List[TrafficLightCycleElement], max_time: int) -> List[TrafficLightState]:
def _sample_traffic_light_cycle_to_states(cycle: TrafficLightCycle, max_time: int) -> List[TrafficLightState]:
"""
Sample TrafficLightCycleElements for each timestep in max_time
:param cycles:
:param max_time:
:return:
Sample the states of `cycle` for each timestep from 0 to `max_time`.
:param cycle: The traffic light cycle that should be resampled.
:param max_time: The time step until which the cycle should be sampled.
:return: Sampled traffic light states.
"""
states: List[TrafficLightState] = []
cycle_idx = 0
length = sum(c.duration for c in cycles)
for i in range(max_time):
if (i % length) >= sum(c.duration for c in cycles[: cycle_idx + 1]):
cycle_idx = (cycle_idx + 1) % len(cycles)
states.append(cycles[cycle_idx].state)
states.append(cycle.get_state_at_time_step(i))
return states

0 comments on commit ca92e4f

Please sign in to comment.