Files
dragonpilot/selfdrive/debug/count_events.py

79 lines
2.2 KiB
Python
Raw Normal View History

2021-04-13 15:07:46 -07:00
#!/usr/bin/env python3
import sys
2022-06-23 15:07:34 -07:00
import math
import datetime
2021-04-13 15:07:46 -07:00
from collections import Counter
from pprint import pprint
from tqdm import tqdm
2023-05-14 11:24:48 -07:00
from typing import List, Tuple, cast
2021-04-13 15:07:46 -07:00
from cereal.services import service_list
from openpilot.tools.lib.route import Route
from openpilot.tools.lib.logreader import LogReader
2021-04-13 15:07:46 -07:00
if __name__ == "__main__":
r = Route(sys.argv[1])
2021-08-23 12:02:08 -07:00
cnt_valid: Counter = Counter()
cnt_events: Counter = Counter()
cams = [s for s in service_list if s.endswith('CameraState')]
cnt_cameras = dict.fromkeys(cams, 0)
2023-05-14 11:24:48 -07:00
alerts: List[Tuple[float, str]] = []
2022-06-23 15:07:34 -07:00
start_time = math.inf
end_time = -math.inf
ignition_off = None
2021-04-13 15:07:46 -07:00
for q in tqdm(r.qlog_paths()):
2021-10-25 10:24:30 -07:00
if q is None:
continue
2021-08-23 12:02:08 -07:00
lr = list(LogReader(q))
for msg in lr:
2023-05-14 11:24:48 -07:00
end_time = max(end_time, msg.logMonoTime)
start_time = min(start_time, msg.logMonoTime)
2021-08-23 12:02:08 -07:00
if msg.which() == 'carEvents':
for e in msg.carEvents:
cnt_events[e.name] += 1
2023-05-14 11:24:48 -07:00
elif msg.which() == 'controlsState':
if len(alerts) == 0 or alerts[-1][1] != msg.controlsState.alertType:
t = (msg.logMonoTime - start_time) / 1e9
alerts.append((t, msg.controlsState.alertType))
elif msg.which() == 'pandaStates':
if ignition_off is None:
ign = any(ps.ignitionLine or ps.ignitionCan for ps in msg.pandaStates)
if not ign:
ignition_off = msg.logMonoTime
elif msg.which() in cams:
cnt_cameras[msg.which()] += 1
2021-08-23 12:02:08 -07:00
if not msg.valid:
cnt_valid[msg.which()] += 1
2022-06-23 15:07:34 -07:00
duration = (end_time - start_time) / 1e9
2021-08-23 12:02:08 -07:00
print("Events")
pprint(cnt_events)
print("\n")
2021-08-23 12:02:08 -07:00
print("Not valid")
pprint(cnt_valid)
print("\n")
print("Cameras")
for k, v in cnt_cameras.items():
2022-06-23 15:07:34 -07:00
s = service_list[k]
expected_frames = int(s.frequency * duration / cast(float, s.decimation))
print(" ", k.ljust(20), f"{v}, {v/expected_frames:.1%} of expected")
2023-05-14 11:24:48 -07:00
print("\n")
print("Alerts")
for t, a in alerts:
print(f"{t:8.2f} {a}")
if ignition_off is not None:
ignition_off = round((ignition_off - start_time) / 1e9, 2)
print("Ignition off at", ignition_off)
2023-05-14 11:24:48 -07:00
2022-06-23 15:07:34 -07:00
print("\n")
print("Route duration", datetime.timedelta(seconds=duration))