{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "segments = [\n",
    "  \"d9df6f87e8feff94|2023-03-28--17-41-10/1:12\"\n",
    "]\n",
    "platform = \"SUBARU_OUTBACK\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "import numpy as np\n",
    "\n",
    "from opendbc.can.parser import CANParser\n",
    "from opendbc.car.subaru.values import DBC\n",
    "\n",
    "from openpilot.selfdrive.pandad import can_capnp_to_list\n",
    "from openpilot.tools.lib.logreader import LogReader\n",
    "\n",
    "\"\"\"\n",
    "In this example, we plot the relationship between Cruise_Brake and Acceleration for stock eyesight.\n",
    "\"\"\"\n",
    "\n",
    "for segment in segments:\n",
    "  lr = LogReader(segment)\n",
    "\n",
    "  messages = [\n",
    "    (\"ES_Distance\", 20),\n",
    "    (\"ES_Brake\", 20),\n",
    "    (\"ES_Status\", 20),\n",
    "  ]\n",
    "\n",
    "  cp = CANParser(DBC[platform][\"pt\"], messages, 1)\n",
    "\n",
    "  es_distance_history = []\n",
    "  es_status_history = []\n",
    "  es_brake_history = []\n",
    "  acceleration_history = []\n",
    "\n",
    "  last_acc = 0\n",
    "\n",
    "  for msg in lr:\n",
    "    if msg.which() == \"can\":\n",
    "      cp.update_strings(can_capnp_to_list([msg.as_builder().to_bytes()]))\n",
    "      es_distance_history.append(copy.copy(cp.vl[\"ES_Distance\"]))\n",
    "      es_brake_history.append(copy.copy(cp.vl[\"ES_Brake\"]))\n",
    "      es_status_history.append(copy.copy(cp.vl[\"ES_Status\"]))\n",
    "\n",
    "      acceleration_history.append(last_acc)\n",
    "\n",
    "    if msg.which() == \"carState\":\n",
    "      last_acc = msg.carState.aEgo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process(history, func):\n",
    "  return np.array([func(h) for h in history])\n",
    "\n",
    "cruise_activated = process(es_status_history, lambda es_status: es_status[\"Cruise_Activated\"])\n",
    "cruise_throttle = process(es_distance_history, lambda es_distance: es_distance[\"Cruise_Throttle\"])\n",
    "cruise_rpm = process(es_status_history, lambda es_status: es_status[\"Cruise_RPM\"])\n",
    "cruise_brake = process(es_brake_history, lambda es_brake: es_brake[\"Brake_Pressure\"])\n",
    "acceleration = process(acceleration_history, lambda acc: acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "valid_brake = (cruise_activated==1) & (cruise_brake>0) # only when cruise is activated and eyesight is braking\n",
    "\n",
    "ax = plt.figure().add_subplot()\n",
    "\n",
    "ax.set_title(\"Brake_Pressure vs Acceleration\")\n",
    "ax.set_xlabel(\"Brake_Pessure\")\n",
    "ax.set_ylabel(\"Acceleration\")\n",
    "ax.scatter(cruise_brake[valid_brake], -acceleration[valid_brake])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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": 4
}
