Here is some old code that was called once per simulation step to compute the gaps:
INDUCTION_LOOP_EXIT_TIMES_CACHE = {} # det_id : (veh_id, exitTime)
def inductionloop_timegap(id):
"""returns the largest netto-time gap between sucessive vehicles"""
vehicleData = traci.inductionloop.getVehicleData(id)
if len(vehicleData) == 0:
return traci.inductionloop.getTimeSinceDetection(id)
gaps = []
for veh_id, veh_length, entry_time, exit_time, vType in vehicleData:
last_veh, last_exit = INDUCTION_LOOP_EXIT_TIMES_CACHE.get(id, (None, None))
if last_veh != veh_id:
gaps.append((last_exit, entry_time))
if last_exit is not None and entry_time - last_exit <= 0:
print("negative time gap det=%s lastVeh=%s (exit=%s) veh_id=%s (entry=%s)" % (
id, last_veh, last_exit, veh_id, entry_time))
INDUCTION_LOOP_EXIT_TIMES_CACHE[id] = veh_id, exit_time
#if id == 'RD1': print vehicleData, gaps
gaps = [entry - exit for exit, entry in gaps if exit is not None]
if len(gaps) > 0:
#assert(min(gaps) > 0)
return max(gaps + [0])
else:
return traci.inductionloop.getTimeSinceDetection(id)