{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "228a6736-de31-4255-9d72-a6ff391b968d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 6 qualifying vehicles:\n",
      "  KIA_EV6\n",
      "  HYUNDAI_KONA_EV_2ND_GEN\n",
      "  HYUNDAI_IONIQ_5\n",
      "  KIA_NIRO_EV_2ND_GEN\n",
      "  HYUNDAI_IONIQ_6\n",
      "  GENESIS_GV60_EV_1ST_GEN\n"
     ]
    }
   ],
   "source": [
    "from opendbc.car import structs\n",
    "from opendbc.car.hyundai.values import CAR, HyundaiFlags\n",
    "from opendbc.car.hyundai.fingerprints import FW_VERSIONS\n",
    "\n",
    "TEST_PLATFORMS = set(CAR.with_flags(HyundaiFlags.CANFD)) & set(CAR.with_flags(HyundaiFlags.EV))  # CAN-FD electric vehicles only\n",
    "#TEST_PLATFORMS = set(CAR.with_flags(HyundaiFlags.CANFD)) - set(CAR.with_flags(HyundaiFlags.EV))  # CAN-FD hybrid and ICE vehicles only\n",
    "\n",
    "print(f\"Found {len(TEST_PLATFORMS)} qualifying vehicles:\")\n",
    "for platform in TEST_PLATFORMS:\n",
    "  print(f\"  {platform}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "ed1c8aec-c274-4c61-b83d-711ea194bf86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting segments from commaCarSegments dataset:\n",
      "Got 1300 segments for platform KIA_EV6, sampling 5 segments\n",
      "Got 9 segments for platform HYUNDAI_KONA_EV_2ND_GEN, sampling 5 segments\n",
      "Got 1570 segments for platform HYUNDAI_IONIQ_5, sampling 5 segments\n",
      "Got 34 segments for platform KIA_NIRO_EV_2ND_GEN, sampling 5 segments\n",
      "Got 974 segments for platform HYUNDAI_IONIQ_6, sampling 5 segments\n",
      "Got 157 segments for platform GENESIS_GV60_EV_1ST_GEN, sampling 5 segments\n",
      "Collected 30 segments for analysis\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "from openpilot.tools.lib.logreader import LogReader\n",
    "from openpilot.tools.lib.comma_car_segments import get_comma_car_segments_database\n",
    "from opendbc.car.hyundai.values import CAR\n",
    "\n",
    "database = get_comma_car_segments_database()\n",
    "TEST_SEGMENTS = []\n",
    "\n",
    "MAX_SEGS_PER_PLATFORM = 5  # TODO: Increase this to search more segments\n",
    "\n",
    "print(\"Collecting segments from commaCarSegments dataset:\")\n",
    "for platform in TEST_PLATFORMS:\n",
    "  assert(platform in database)\n",
    "  #if platform not in database:\n",
    "  #  print(f\"Skipping platform: {platform}, no data available\")\n",
    "  #  continue\n",
    "\n",
    "  all_segments = database[platform]\n",
    "\n",
    "  NUM_SEGMENTS = min(len(all_segments), MAX_SEGS_PER_PLATFORM)\n",
    "\n",
    "  print(f\"Got {len(all_segments)} segments for platform {platform}, sampling {NUM_SEGMENTS} segments\")\n",
    "\n",
    "  TEST_SEGMENTS.extend(random.sample(all_segments, NUM_SEGMENTS))\n",
    "\n",
    "print(f\"Collected {len(TEST_SEGMENTS)} segments for analysis\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "0c75e8f2-4f5f-4f89-b8db-5223a6534a9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Analyzing segment ff2bd20623fcaeaa/2023-11-26--16-27-04/5/s    for KIA EV6 2022\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 3f1a6480f940cf9a/2024-01-10--23-06-11/16/s   for KIA EV6 2022\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment b0a9998109ed0053/2023-12-15--11-10-18/12/s   for KIA EV6 2022\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 6e14aa2ed85025df/2023-11-15--13-18-12/24/s   for KIA EV6 2022\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment a43f21df3a1ca12d/2024-01-25--08-56-22/16/s   for KIA EV6 2022\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 1618132d68afc876/2023-12-05--13-49-24/11/s   for HYUNDAI KONA ELECTRIC 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 1618132d68afc876/2023-11-26--12-31-18/17/s   for HYUNDAI KONA ELECTRIC 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 1618132d68afc876/2023-12-05--11-51-44/3/s    for HYUNDAI KONA ELECTRIC 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 1618132d68afc876/2023-08-27--09-32-14/13/s   for HYUNDAI KONA 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 1618132d68afc876/2024-01-25--15-07-04/24/s   for HYUNDAI KONA ELECTRIC 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 223780ed74116bc2/2023-11-16--09-44-56/15/s   for HYUNDAI IONIQ 5 2022\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment ba9951252624f37d/2024-01-20--22-33-23/118/s  for HYUNDAI IONIQ 5 2022\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 8379b28e51ceb3b1/2023-11-09--23-21-58/92/s   for HYUNDAI IONIQ 5 2022\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 26fac43e27cd6091/2023-11-06--12-23-21/9/s    for HYUNDAI IONIQ 5 2022\n",
      "  GEAR_SHIFTER    gear=1.0\n",
      "  ACCELERATOR     gear=0.0\n",
      "Analyzing segment 5edb897a0ec7a477/2024-01-13--20-41-36/101/s  for HYUNDAI IONIQ 5 2022\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 66cf8ea23b7c2789/2023-12-04--13-48-53/5/s    for KIA NIRO EV 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment b153671049a867b3/2023-12-10--20-31-37/2/s    for KIA NIRO EV 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment b153671049a867b3/2023-12-03--21-08-30/14/s   for KIA NIRO EV 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment b153671049a867b3/2023-11-07--19-52-23/0/s    for KIA NIRO EV 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment b153671049a867b3/2023-07-12--19-25-18/6/s    for KIA NIRO EV 2ND GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 9ea4578ee2b1abcb/2023-11-18--07-59-26/11/s   for HYUNDAI IONIQ 6 2023\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 0ad7facc77922c3e/2023-12-21--17-47-25/18/s   for HYUNDAI IONIQ 6 2023\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 26968f888e7330d3/2024-01-02--11-18-37/8/s    for HYUNDAI IONIQ 6 2023\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 9ea4578ee2b1abcb/2023-11-27--21-03-24/33/s   for HYUNDAI IONIQ 6 2023\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment df7fdd56970d90fe/2024-01-07--01-04-39/26/s   for HYUNDAI IONIQ 6 2023\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 94542b2d06f7a9a6/2023-12-11--14-45-44/0/s    for GENESIS GV60 ELECTRIC 1ST GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 94542b2d06f7a9a6/2023-12-11--20-57-09/8/s    for GENESIS GV60 ELECTRIC 1ST GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 94542b2d06f7a9a6/2024-01-03--12-52-38/5/s    for GENESIS GV60 ELECTRIC 1ST GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 94542b2d06f7a9a6/2024-01-19--19-57-52/47/s   for GENESIS GV60 ELECTRIC 1ST GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analyzing segment 94542b2d06f7a9a6/2024-01-03--13-01-23/1/s    for GENESIS GV60 ELECTRIC 1ST GEN\n",
      "  GEAR_SHIFTER    gear=4.0\n",
      "  ACCELERATOR     gear=5.0\n",
      "Analysis finished\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from opendbc.can.parser import CANParser\n",
    "from opendbc.car.hyundai.values import DBC\n",
    "from opendbc.car.hyundai.hyundaicanfd import CanBus\n",
    "\n",
    "from openpilot.selfdrive.pandad import can_capnp_to_list\n",
    "from openpilot.tools.lib.logreader import LogReader, comma_car_segments_source\n",
    "\n",
    "message_names = [\"GEAR_SHIFTER\", \"ACCELERATOR\", \"GEAR\", \"GEAR_ALT\", \"GEAR_ALT_2\"]\n",
    "\n",
    "for segment in TEST_SEGMENTS:\n",
    "  lr = LogReader(segment, sources=[comma_car_segments_source])\n",
    "  CP = lr.first(\"carParams\")\n",
    "  if CP is None:\n",
    "    continue\n",
    "\n",
    "  can_msgs = [msg for msg in lr if msg.which() == \"can\"]\n",
    "  parser_messages = []\n",
    "  for name in message_names:\n",
    "    parser_messages.append((name, 0))\n",
    "  cp = CANParser(DBC[platform][\"pt\"], parser_messages, CanBus(CP).ECAN)\n",
    "\n",
    "  parsed_message_history = []\n",
    "  examples = []\n",
    "\n",
    "  for msg in can_msgs:\n",
    "    cp.update_strings(can_capnp_to_list([msg.as_builder().to_bytes()]))\n",
    "    parsed_message_history.append(copy.copy(cp.vl))\n",
    "\n",
    "  print(f\"Analyzing segment {segment:<44} for {CP.carFingerprint}\")\n",
    "  for name in message_names:\n",
    "    if parsed_message_history[0][name][\"CHECKSUM\"] != 0:  # Message is present for this segment\n",
    "      gear_prev = parsed_message_history[0][name][\"GEAR\"]\n",
    "      print(f\"  {name:<15} gear={gear_prev}\")\n",
    "      for i, parsed_messages in enumerate(parsed_message_history):\n",
    "        gear = parsed_messages[name][\"GEAR\"]\n",
    "        if gear != gear_prev:\n",
    "          print(f\"  *** Signal transition found! ***\")\n",
    "          examples.append(i)\n",
    "        gear_prev = gear\n",
    "\n",
    "print(f\"Analysis finished\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7724dd97-f62e-4fd3-9f64-63d49be669d2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f393e00-8efd-40fb-a41e-d312531a83e8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
