{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.integrate import odeint\n",
    "from scipy.interpolate import interp1d\n",
    "import cvxpy as cp\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.style.use(\"ggplot\")\n",
    "\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### kinematics model equations\n",
    "\n",
    "The variables of the model are:\n",
    "\n",
    "* $x$ coordinate of the robot\n",
    "* $y$ coordinate of the robot\n",
    "* $\\theta$ heading of the robot\n",
    "\n",
    "The inputs of the model are:\n",
    "\n",
    "* $v$ linear velocity of the robot\n",
    "* $w$ angular velocity of the robot\n",
    "\n",
    "These are the differential equations f(x,u) of the model:\n",
    "\n",
    "$\\dot{x} = f(x,u)$\n",
    "\n",
    "* $\\dot{x} = v\\cos{\\theta}$ \n",
    "* $\\dot{y} = v\\sin{\\theta}$\n",
    "* $\\dot{\\theta} = w$\n",
    "\n",
    "Discretize with forward Euler Integration for time step dt:\n",
    "\n",
    "${x_{t+1}} = x_{t} + f(x,u)dt$\n",
    "\n",
    "* ${x_{t+1}} = x_{t} + v_t\\cos{\\theta}dt$\n",
    "* ${y_{t+1}} = y_{t} + v_t\\sin{\\theta}dt$\n",
    "* ${\\theta_{t+1}} = \\theta_{t} + w_t dt$\n",
    "\n",
    "----------------------\n",
    "\n",
    "The Model is **non-linear** and **time variant**, but the Numerical Optimizer requires a Linear sets of equations. To approximate the equivalent **LTI** State space model, the **Taylor's series expansion** is used around $\\bar{x}$ and $\\bar{u}$ (at each time step):\n",
    "\n",
    "$ f(x,u) \\approx f(\\bar{x},\\bar{u}) +  \\frac{\\partial f(x,u)}{\\partial x}|_{x=\\bar{x},u=\\bar{u}}(x-\\bar{x}) + \\frac{\\partial f(x,u)}{\\partial u}|_{x=\\bar{x},u=\\bar{u}}(u-\\bar{u})$\n",
    "\n",
    "This can be rewritten usibg the State Space model form Ax+Bu :\n",
    "\n",
    "$ f(\\bar{x},\\bar{u}) +  A|_{x=\\bar{x},u=\\bar{u}}(x-\\bar{x}) + B|_{x=\\bar{x},u=\\bar{u}}(u-\\bar{u})$\n",
    "\n",
    "Where:\n",
    "\n",
    "$\n",
    "A =\n",
    "\\quad\n",
    "\\begin{bmatrix}\n",
    "\\frac{\\partial f(x,u)}{\\partial x} & \\frac{\\partial f(x,u)}{\\partial y} & \\frac{\\partial f(x,u)}{\\partial \\theta} \\\\\n",
    "\\end{bmatrix}\n",
    "\\quad\n",
    "=\n",
    "\\quad\n",
    "\\begin{bmatrix}\n",
    "0 & 0 & -v\\sin{\\theta} \\\\\n",
    "0 & 0 & v\\cos{\\theta} \\\\\n",
    "0 & 0 & 0\\\\\n",
    "\\end{bmatrix}\n",
    "\\quad\n",
    "$\n",
    "\n",
    "and\n",
    "\n",
    "$\n",
    "B = \n",
    "\\quad\n",
    "\\begin{bmatrix}\n",
    "\\frac{\\partial f(x,u)}{\\partial v} & \\frac{\\partial f(x,u)}{\\partial w} \\\\\n",
    "\\end{bmatrix}\n",
    "\\quad\n",
    "= \n",
    "\\quad\n",
    "\\begin{bmatrix}\n",
    "\\cos{\\theta} & 0 \\\\\n",
    "\\sin{\\theta} & 0 \\\\\n",
    "0 & 1\n",
    "\\end{bmatrix}\n",
    "\\quad\n",
    "$\n",
    "\n",
    "are the *Jacobians*.\n",
    "\n",
    "\n",
    "\n",
    "So the discretized model is given by:\n",
    "\n",
    "$ x_{t+1} = x_t + (f(\\bar{x},\\bar{u}) +  A|_{x=\\bar{x}}(x_t-\\bar{x}) + B|_{u=\\bar{u}}(u_t-\\bar{u}) )dt $\n",
    "\n",
    "$ x_{t+1} = (I+dtA)x_t + dtBu_t +dt(f(\\bar{x},\\bar{u}) - A\\bar{x} - B\\bar{u}))$\n",
    "\n",
    "The LTI-equivalent kinematics model is:\n",
    "\n",
    "$ x_{t+1} = A'x_t + B' u_t + C' $\n",
    "\n",
    "with:\n",
    "\n",
    "$ A' = I+dtA|_{x=\\bar{x},u=\\bar{u}} $\n",
    "\n",
    "$ B' = dtB|_{x=\\bar{x},u=\\bar{u}} $\n",
    "\n",
    "$ C' = dt(f(\\bar{x},\\bar{u}) - A|_{x=\\bar{x},u=\\bar{u}}\\bar{x} - B|_{x=\\bar{x},u=\\bar{u}}\\bar{u}) $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-----------------\n",
    "[About Taylor Series Expansion](https://courses.engr.illinois.edu/ece486/fa2017/documents/lecture_notes/state_space_p2.pdf):\n",
    "\n",
    "In order to linearize general nonlinear systems, we will use the Taylor Series expansion of functions.\n",
    "\n",
    "Typically it is possible to assume that the system is operating about some nominal\n",
    "state solution $\\bar{x}$ (possibly requires a nominal input $\\bar{u}$) called **equilibrium point**.\n",
    "\n",
    "Recall that the Taylor Series expansion of f(x) around the\n",
    "point $\\bar{x}$ is given by:\n",
    "\n",
    "$f(x)=f(\\bar{x}) + \\frac{df(x)}{dx}|_{x=\\bar{x}}(x-\\bar{x})$ + higher order terms...\n",
    "\n",
    "For x sufficiently close to $\\bar{x}$, these higher order terms will be very close to zero, and so we can drop them.\n",
    "\n",
    "The extension to functions of multiple states and inputs is very similar to the above procedure.Suppose the evolution of state x\n",
    "is given by:\n",
    "\n",
    "$\\dot{x} = f(x1, x2, . . . , xn, u1, u2, . . . , um) = Ax+Bu$\n",
    "\n",
    "Where:\n",
    "\n",
    "$ A =\n",
    "\\quad\n",
    "\\begin{bmatrix}\n",
    "\\frac{\\partial f(x,u)}{\\partial x1} & ... & \\frac{\\partial f(x,u)}{\\partial xn} \\\\\n",
    "\\end{bmatrix}\n",
    "\\quad\n",
    "$ and $ B = \\quad\n",
    "\\begin{bmatrix}\n",
    "\\frac{\\partial f(x,u)}{\\partial u1} & ... & \\frac{\\partial f(x,u)}{\\partial um} \\\\\n",
    "\\end{bmatrix}\n",
    "\\quad $\n",
    "\n",
    "Then:\n",
    "\n",
    "$f(x,u)=f(\\bar{x},\\bar{u}) + \\frac{df(x,u)}{dx}|_{x=\\bar{x}}(x-\\bar{x}) + \\frac{df(x,u)}{du}|_{u=\\bar{u}}(u-\\bar{u}) = f(\\bar{x},\\bar{u}) + A_{x=\\bar{x}}(x-\\bar{x}) + B_{u=\\bar{u}}(u-\\bar{u})$\n",
    "\n",
    "-----------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Kinematics Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Control problem statement.\n",
    "\n",
    "N = 3  # number of state variables\n",
    "M = 2  # number of control variables\n",
    "T = 20  # Prediction Horizon\n",
    "dt = 0.25  # discretization step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_linear_model(x_bar, u_bar):\n",
    "    \"\"\" \"\"\"\n",
    "\n",
    "    x = x_bar[0]\n",
    "    y = x_bar[1]\n",
    "    theta = x_bar[2]\n",
    "\n",
    "    v = u_bar[0]\n",
    "    w = u_bar[1]\n",
    "\n",
    "    A = np.zeros((N, N))\n",
    "    A[0, 2] = -v * np.sin(theta)\n",
    "    A[1, 2] = v * np.cos(theta)\n",
    "    A_lin = np.eye(N) + dt * A\n",
    "\n",
    "    B = np.zeros((N, M))\n",
    "    B[0, 0] = np.cos(theta)\n",
    "    B[1, 0] = np.sin(theta)\n",
    "    B[2, 1] = 1\n",
    "    B_lin = dt * B\n",
    "\n",
    "    f_xu = np.array([v * np.cos(theta), v * np.sin(theta), w]).reshape(N, 1)\n",
    "    C_lin = dt * (\n",
    "        f_xu - np.dot(A, x_bar.reshape(N, 1)) - np.dot(B, u_bar.reshape(M, 1))\n",
    "    )\n",
    "\n",
    "    return A_lin, B_lin, C_lin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Motion Prediction: using scipy intergration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define process model\n",
    "# This uses the continuous model\n",
    "def kinematics_model(x, t, u):\n",
    "    \"\"\" \"\"\"\n",
    "\n",
    "    dxdt = u[0] * np.cos(x[2])\n",
    "    dydt = u[0] * np.sin(x[2])\n",
    "    dthetadt = u[1]\n",
    "\n",
    "    dqdt = [dxdt, dydt, dthetadt]\n",
    "\n",
    "    return dqdt\n",
    "\n",
    "\n",
    "def predict(x0, u):\n",
    "    \"\"\" \"\"\"\n",
    "\n",
    "    x_bar = np.zeros((N, T + 1))\n",
    "\n",
    "    x_bar[:, 0] = x0\n",
    "\n",
    "    # solve ODE\n",
    "    for t in range(1, T + 1):\n",
    "\n",
    "        tspan = [0, dt]\n",
    "        x_next = odeint(kinematics_model, x0, tspan, args=(u[:, t - 1],))\n",
    "\n",
    "        x0 = x_next[1]\n",
    "        x_bar[:, t] = x_next[1]\n",
    "\n",
    "    return x_bar"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Validate the model, here the status w.r.t a straight line with constant heading 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 8.17 ms, sys: 10 µs, total: 8.18 ms\n",
      "Wall time: 7.26 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "u_bar = np.zeros((M, T))\n",
    "u_bar[0, :] = 1  # m/s\n",
    "u_bar[1, :] = np.radians(-10)  # rad/s\n",
    "\n",
    "x0 = np.zeros(N)\n",
    "x0[0] = 0\n",
    "x0[1] = 1\n",
    "x0[2] = np.radians(0)\n",
    "\n",
    "x_bar = predict(x0, u_bar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check the model prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12948 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot trajectory\n",
    "plt.subplot(2, 2, 1)\n",
    "plt.plot(x_bar[0, :], x_bar[1, :])\n",
    "plt.plot(np.linspace(0, 10, T + 1), np.zeros(T + 1), \"b-\")\n",
    "plt.axis(\"equal\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.xlabel(\"x\")\n",
    "\n",
    "plt.subplot(2, 2, 2)\n",
    "plt.plot(np.degrees(x_bar[2, :]))\n",
    "plt.ylabel(\"theta(t) [deg]\")\n",
    "# plt.xlabel('time')\n",
    "\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Motion Prediction: using the state space model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1.72 ms, sys: 0 ns, total: 1.72 ms\n",
      "Wall time: 1.2 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "u_bar = np.zeros((M, T))\n",
    "u_bar[0, :] = 1  # m/s\n",
    "u_bar[1, :] = np.radians(-10)  # rad/s\n",
    "\n",
    "x0 = np.zeros(N)\n",
    "x0[0] = 0\n",
    "x0[1] = 1\n",
    "x0[2] = np.radians(0)\n",
    "\n",
    "x_bar = np.zeros((N, T + 1))\n",
    "x_bar[:, 0] = x0\n",
    "\n",
    "for t in range(1, T + 1):\n",
    "    xt = x_bar[:, t - 1].reshape(N, 1)\n",
    "    ut = u_bar[:, t - 1].reshape(M, 1)\n",
    "\n",
    "    A, B, C = get_linear_model(xt, ut)\n",
    "\n",
    "    xt_plus_one = np.dot(A, xt) + np.dot(B, ut) + C\n",
    "\n",
    "    x_bar[:, t] = np.squeeze(xt_plus_one)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12052 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot trajectory\n",
    "plt.subplot(2, 2, 1)\n",
    "plt.plot(x_bar[0, :], x_bar[1, :])\n",
    "plt.plot(np.linspace(0, 10, T + 1), np.zeros(T + 1), \"b-\")\n",
    "plt.axis(\"equal\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.xlabel(\"x\")\n",
    "\n",
    "plt.subplot(2, 2, 2)\n",
    "plt.plot(np.degrees(x_bar[2, :]))\n",
    "plt.ylabel(\"theta(t)\")\n",
    "# plt.xlabel('time')\n",
    "\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The results are the same as expected, so the linearized model is equivalent as expected."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "------------------\n",
    "\n",
    "the kinematics model predictits psi and cte for constant heading references. So, for non-constant paths appropriate functions have to be developed.\n",
    "\n",
    "-----------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PRELIMINARIES"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compute_path_from_wp(start_xp, start_yp, step=0.1):\n",
    "    final_xp = []\n",
    "    final_yp = []\n",
    "    delta = step  # [m]\n",
    "\n",
    "    for idx in range(len(start_xp) - 1):\n",
    "        section_len = np.sum(\n",
    "            np.sqrt(\n",
    "                np.power(np.diff(start_xp[idx : idx + 2]), 2)\n",
    "                + np.power(np.diff(start_yp[idx : idx + 2]), 2)\n",
    "            )\n",
    "        )\n",
    "\n",
    "        interp_range = np.linspace(0, 1, np.floor(section_len / delta).astype(int))\n",
    "\n",
    "        fx = interp1d(np.linspace(0, 1, 2), start_xp[idx : idx + 2], kind=1)\n",
    "        fy = interp1d(np.linspace(0, 1, 2), start_yp[idx : idx + 2], kind=1)\n",
    "\n",
    "        final_xp = np.append(final_xp, fx(interp_range))\n",
    "        final_yp = np.append(final_yp, fy(interp_range))\n",
    "\n",
    "    return np.vstack((final_xp, final_yp))\n",
    "\n",
    "\n",
    "def get_nn_idx(state, path):\n",
    "\n",
    "    dx = state[0] - path[0, :]\n",
    "    dy = state[1] - path[1, :]\n",
    "    dist = np.sqrt(dx**2 + dy**2)\n",
    "    nn_idx = np.argmin(dist)\n",
    "\n",
    "    try:\n",
    "        v = [\n",
    "            path[0, nn_idx + 1] - path[0, nn_idx],\n",
    "            path[1, nn_idx + 1] - path[1, nn_idx],\n",
    "        ]\n",
    "        v /= np.linalg.norm(v)\n",
    "\n",
    "        d = [path[0, nn_idx] - state[0], path[1, nn_idx] - state[1]]\n",
    "\n",
    "        if np.dot(d, v) > 0:\n",
    "            target_idx = nn_idx\n",
    "        else:\n",
    "            target_idx = nn_idx + 1\n",
    "\n",
    "    except IndexError as e:\n",
    "        target_idx = nn_idx\n",
    "\n",
    "    return target_idx\n",
    "\n",
    "\n",
    "def road_curve(state, track):\n",
    "\n",
    "    # given vehicle pos find lookahead waypoints\n",
    "    nn_idx = get_nn_idx(state, track) - 1\n",
    "    LOOKAHED = 6\n",
    "    lk_wp = track[:, nn_idx : nn_idx + LOOKAHED]\n",
    "\n",
    "    # trasform lookahead waypoints to vehicle ref frame\n",
    "    dx = lk_wp[0, :] - state[0]\n",
    "    dy = lk_wp[1, :] - state[1]\n",
    "\n",
    "    wp_vehicle_frame = np.vstack(\n",
    "        (\n",
    "            dx * np.cos(-state[2]) - dy * np.sin(-state[2]),\n",
    "            dy * np.cos(-state[2]) + dx * np.sin(-state[2]),\n",
    "        )\n",
    "    )\n",
    "\n",
    "    # fit poly\n",
    "    return np.polyfit(\n",
    "        wp_vehicle_frame[0, :],\n",
    "        wp_vehicle_frame[1, :],\n",
    "        3,\n",
    "        rcond=None,\n",
    "        full=False,\n",
    "        w=None,\n",
    "        cov=False,\n",
    "    )\n",
    "\n",
    "\n",
    "def f(x, coeff):\n",
    "    return round(\n",
    "        coeff[0] * x**3 + coeff[1] * x**2 + coeff[2] * x**1 + coeff[3] * x**0, 6\n",
    "    )\n",
    "\n",
    "\n",
    "# def f(x,coeff):\n",
    "#     return  round(coeff[0]*x**5+coeff[1]*x**4+coeff[2]*x**3+coeff[3]*x**2+coeff[4]*x**1+coeff[5]*x**0,6)\n",
    "\n",
    "\n",
    "def df(x, coeff):\n",
    "    return round(3 * coeff[0] * x**2 + 2 * coeff[1] * x**1 + coeff[2] * x**0, 6)\n",
    "\n",
    "\n",
    "# def df(x,coeff):\n",
    "#     return round(5*coeff[0]*x**4 + 4*coeff[1]*x**3 +3*coeff[2]*x**2 + 2*coeff[3]*x**1 + coeff[4]*x**0,6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MPC Problem formulation\n",
    "\n",
    "**Model Predictive Control** refers to the control approach of **numerically** solving a optimization problem at each time step. \n",
    "\n",
    "The controller generates a control signal over a fixed lenght T (Horizon) at each time step."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![mpc](img/mpc_block_diagram.png)\n",
    "\n",
    "![mpc](img/mpc_t.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Linear MPC Formulation\n",
    "\n",
    "Linear MPC makes use of the **LTI** (Linear time invariant) discrete state space model, wich represents a motion model used for Prediction.\n",
    "\n",
    "$x_{t+1} = Ax_t + Bu_t$\n",
    "\n",
    "The LTI formulation means that **future states** are linearly related to the current state and actuator signal. Hence, the MPC seeks to find a **control policy** U over a finite lenght horizon.\n",
    "\n",
    "$U={u_{t|t}, u_{t+1|t}, ...,u_{t+T|t}}$\n",
    "\n",
    "The objective function used minimize (drive the state to 0) is:\n",
    "\n",
    "$\n",
    "\\begin{equation}\n",
    "\\begin{aligned}\n",
    "\\min_{} \\quad &  \\sum^{t+T-1}_{j=t} x^T_{j|t}Qx_{j|t} + u^T_{j|t}Ru_{j|t}\\\\\n",
    "\\textrm{s.t.} \\quad &  x(0) = x0\\\\\n",
    "  & x_{j+1|t} = Ax_{j|t}+Bu_{j|t}) \\quad  \\textrm{for} \\quad t<j<t+T-1    \\\\\n",
    "\\end{aligned}\n",
    "\\end{equation}\n",
    "$\n",
    "\n",
    "Other linear constrains may be applied,for instance on the control variable:\n",
    "\n",
    "$ U_{MIN} < u_{j|t} < U_{MAX}    \\quad  \\textrm{for} \\quad t<j<t+T-1 $\n",
    "\n",
    "The objective fuction accounts for quadratic error on deviation from 0 of the state and the control inputs sequences. Q and R are the **weight matrices** and are used to tune the response.\n",
    "\n",
    "Because the goal is tracking a **reference signal** such as a trajectory, the objective function is rewritten as:\n",
    "\n",
    "$\n",
    "\\begin{equation}\n",
    "\\begin{aligned}\n",
    "\\min_{} \\quad &  \\sum^{t+T-1}_{j=t} \\delta x^T_{j|t}Q\\delta x_{j|t} + u^T_{j|t}Ru_{j|t}\n",
    "\\end{aligned}\n",
    "\\end{equation}\n",
    "$\n",
    "\n",
    "where the error w.r.t desired state is accounted for:\n",
    "\n",
    "$ \\delta x = x_{j,t,ref} - x_{j,t} $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Problem Formulation: Study case\n",
    "\n",
    "In this case, the objective function to minimize is given the sum of the **cross-track error** plus **heading error**\n",
    "\n",
    "$\n",
    "\\begin{equation}\n",
    "\\begin{aligned}\n",
    "\\min_{} \\quad &  \\sum^{t+T-1}_{j=t} cte_{j|t,ref}^TQcte_{j,t,ref} + \\psi_{j|t,ref}^TP\\psi_{j|t,ref} + u^T_{j|t}Ru_{j|t} \\\\\n",
    "\\textrm{s.t.} \\quad &  x(0) = x0\\\\\n",
    "  & x_{j+1|t} = Ax_{j|t}+Bu_{j|t}) \\quad  \\textrm{for} \\quad t<j<t+T-1    \\\\\n",
    "\\end{aligned}\n",
    "\\end{equation}\n",
    "$\n",
    "\n",
    "where R,P,Q are the cost matrices used to tune the response.\n",
    "\n",
    "## Error Formulation\n",
    "\n",
    "The track can be represented by fitting a curve trough its waypoints, using the vehicle position as reference\n",
    "\n",
    "![mpc](img/fitted_poly.png)\n",
    "\n",
    "A fitted cubic poly has the form:\n",
    "\n",
    "$\n",
    "f = K_0 * x^3 + K_1 * x^2 + K_2 * x + K_3\n",
    "$\n",
    "\n",
    "The derivative of a fitted cubic poly has the form:\n",
    "\n",
    "$\n",
    "f' = 3.0 * K_0 * x^2 + 2.0 * K_1 * x + K_2\n",
    "$\n",
    "\n",
    "\n",
    "* **crosstrack error** cte: desired y-position - y-position of vehicle -> this is the value of the fitted polynomial\n",
    "\n",
    "* **heading error** epsi:  desired heading - heading of vehicle -> is the inclination of  tangent to the  fitted polynomial\n",
    "\n",
    "Then using the fitted polynomial representation in vehicle frame the errors can be easily computed as:\n",
    "\n",
    "$\n",
    "cte = f(px) \\\\\n",
    "\\psi = -atan(f`(px)) \\\\\n",
    "$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------------------------------------------------------------\n",
      "           OSQP v0.6.0  -  Operator Splitting QP Solver\n",
      "              (c) Bartolomeo Stellato,  Goran Banjac\n",
      "        University of Oxford  -  Stanford University 2019\n",
      "-----------------------------------------------------------------\n",
      "problem:  variables n = 241, constraints m = 281\n",
      "          nnz(P) + nnz(A) = 753\n",
      "settings: linear system solver = qdldl,\n",
      "          eps_abs = 1.0e-04, eps_rel = 1.0e-04,\n",
      "          eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,\n",
      "          rho = 1.00e-01 (adaptive),\n",
      "          sigma = 1.00e-06, alpha = 1.60, max_iter = 10000\n",
      "          check_termination: on (interval 25),\n",
      "          scaling: on, scaled_termination: off\n",
      "          warm start: on, polish: on, time_limit: off\n",
      "\n",
      "iter   objective    pri res    dua res    rho        time\n",
      "   1   0.0000e+00   1.00e+00   1.06e+02   1.00e-01   2.96e-04s\n",
      " 125   1.8119e+03   6.13e-05   5.69e-04   4.76e+01   1.06e-03s\n",
      "\n",
      "status:               solved\n",
      "solution polish:      unsuccessful\n",
      "number of iterations: 125\n",
      "optimal objective:    1811.9387\n",
      "run time:             1.27e-03s\n",
      "optimal rho estimate: 4.81e+01\n",
      "\n",
      "CPU times: user 237 ms, sys: 4 ms, total: 241 ms\n",
      "Wall time: 236 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "x = cp.Variable((N, T + 1))\n",
    "u = cp.Variable((M, T))\n",
    "\n",
    "# CVXPY Linear MPC problem statement\n",
    "cost = 0\n",
    "constr = []\n",
    "\n",
    "for t in range(T):\n",
    "\n",
    "    # Cost function\n",
    "    # cost += 10*cp.sum_squares( x[3, t]) # psi\n",
    "    # cost += 500*cp.sum_squares( x[4, t]) # cte\n",
    "\n",
    "    cost += 10 * cp.sum_squares(x[2, t] - np.arctan(df(x_bar[0, t], K)))  # psi\n",
    "    cost += 500 * cp.sum_squares(f(x_bar[0, t], K) - x[1, t])  # cte\n",
    "    # Actuation effort\n",
    "    cost += cp.quad_form(u[:, t], 1 * np.eye(M))\n",
    "\n",
    "    # Actuation rate of change\n",
    "    if t < (T - 1):\n",
    "        cost += cp.quad_form(u[:, t + 1] - u[:, t], 100 * np.eye(M))\n",
    "\n",
    "    # KINEMATICS constrains\n",
    "    A, B, C = get_linear_model(x_bar[:, t], u_bar[:, t])\n",
    "    constr += [x[:, t + 1] == A @ x[:, t] + B @ u[:, t] + C.flatten()]\n",
    "\n",
    "# sums problem objectives and concatenates constraints.\n",
    "constr += [x[:, 0] == x_bar[:, 0]]  # <--watch out the start condition\n",
    "constr += [u[0, :] <= MAX_SPEED]\n",
    "constr += [u[0, :] >= MIN_SPEED]\n",
    "constr += [cp.abs(u[1, :]) <= MAX_STEER_SPEED]\n",
    "\n",
    "prob = cp.Problem(cp.Minimize(cost), constr)\n",
    "solution = prob.solve(solver=cp.OSQP, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12068 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x_mpc = np.array(x.value[0, :]).flatten()\n",
    "y_mpc = np.array(x.value[1, :]).flatten()\n",
    "theta_mpc = np.array(x.value[2, :]).flatten()\n",
    "v_mpc = np.array(u.value[0, :]).flatten()\n",
    "w_mpc = np.array(u.value[1, :]).flatten()\n",
    "\n",
    "# simulate robot state trajectory for optimized U\n",
    "x_traj = predict(x0, np.vstack((v_mpc, w_mpc)))\n",
    "\n",
    "# plt.figure(figsize=(15,10))\n",
    "# plot trajectory\n",
    "plt.subplot(2, 2, 1)\n",
    "plt.plot(track[0, :], track[1, :], \"b+\")\n",
    "plt.plot(x_traj[0, :], x_traj[1, :])\n",
    "plt.axis(\"equal\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.xlabel(\"x\")\n",
    "\n",
    "# plot v(t)\n",
    "plt.subplot(2, 2, 2)\n",
    "plt.plot(v_mpc)\n",
    "plt.ylabel(\"v(t)\")\n",
    "# plt.xlabel('time')\n",
    "\n",
    "# plot w(t)\n",
    "# plt.subplot(2, 2, 3)\n",
    "# plt.plot(w_mpc)\n",
    "# plt.ylabel('w(t)')\n",
    "# plt.xlabel('time')\n",
    "\n",
    "# plt.subplot(2, 2, 3)\n",
    "# plt.plot(psi_mpc)\n",
    "# plt.ylabel('psi(t)')\n",
    "\n",
    "# plt.subplot(2, 2, 4)\n",
    "# plt.plot(cte_mpc)\n",
    "# plt.ylabel('cte(t)')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "full track demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-25-0756f6b412cb>:29: RuntimeWarning: invalid value encountered in true_divide\n",
      "  v /= np.linalg.norm(v)\n",
      "<ipython-input-28-79710852001d>:33: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n",
      "<ipython-input-28-79710852001d>:33: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CVXPY Optimization Time: Avrg: 0.1897s Max: 0.2940s Min: 0.1666s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-28-79710852001d>:33: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n"
     ]
    }
   ],
   "source": [
    "track = compute_path_from_wp([0, 3, 4, 6, 10, 13], [0, 0, 2, 4, 3, 3], 0.25)\n",
    "\n",
    "# track = compute_path_from_wp([0,5,7.5,10,12,13,13,10],\n",
    "#                              [0,0,2.5,2.5,0,0,5,10],0.5)\n",
    "\n",
    "sim_duration = 80  # time steps\n",
    "opt_time = []\n",
    "\n",
    "x_sim = np.zeros((N, sim_duration))\n",
    "u_sim = np.zeros((M, sim_duration - 1))\n",
    "\n",
    "MAX_SPEED = 1.25\n",
    "MIN_SPEED = 0.75\n",
    "MAX_STEER_SPEED = 1.57\n",
    "\n",
    "# Starting Condition\n",
    "x0 = np.zeros(N)\n",
    "x0[0] = 0\n",
    "x0[1] = -0.25\n",
    "x0[2] = np.radians(-0)\n",
    "x_sim[:, 0] = x0\n",
    "\n",
    "# starting guess\n",
    "u_bar = np.zeros((M, T))\n",
    "u_bar[0, :] = 0.5 * (MAX_SPEED + MIN_SPEED)\n",
    "u_bar[1, :] = 0.00\n",
    "\n",
    "for sim_time in range(sim_duration - 1):\n",
    "\n",
    "    iter_start = time.time()\n",
    "\n",
    "    K = road_curve(x_sim[:, sim_time], track)\n",
    "\n",
    "    # dynamics starting state w.r.t vehicle frame\n",
    "    x_bar = np.zeros((N, T + 1))\n",
    "\n",
    "    # prediction for linearization of costrains\n",
    "    for t in range(1, T + 1):\n",
    "        xt = x_bar[:, t - 1].reshape(N, 1)\n",
    "        ut = u_bar[:, t - 1].reshape(M, 1)\n",
    "        A, B, C = get_linear_model(xt, ut)\n",
    "        xt_plus_one = np.squeeze(np.dot(A, xt) + np.dot(B, ut) + C)\n",
    "        x_bar[:, t] = xt_plus_one\n",
    "\n",
    "    # CVXPY Linear MPC problem statement\n",
    "    cost = 0\n",
    "    constr = []\n",
    "    x = cp.Variable((N, T + 1))\n",
    "    u = cp.Variable((M, T))\n",
    "\n",
    "    # Prediction Horizon\n",
    "    for t in range(T):\n",
    "\n",
    "        # cost += 30*cp.sum_squares(x[2,t]-np.arctan(df(x_bar[0,t],K))) # psi\n",
    "        cost += 50 * cp.sum_squares(\n",
    "            x[2, t] - np.arctan2(df(x_bar[0, t], K), x_bar[0, t])\n",
    "        )  # psi\n",
    "        cost += 20 * cp.sum_squares(f(x_bar[0, t], K) - x[1, t])  # cte\n",
    "\n",
    "        # Actuation rate of change\n",
    "        if t < (T - 1):\n",
    "            cost += cp.quad_form(u[:, t + 1] - u[:, t], 100 * np.eye(M))\n",
    "\n",
    "        # Actuation effort\n",
    "        cost += cp.quad_form(u[:, t], 1 * np.eye(M))\n",
    "\n",
    "        # Kinrmatics Constrains (Linearized model)\n",
    "        A, B, C = get_linear_model(x_bar[:, t], u_bar[:, t])\n",
    "        constr += [x[:, t + 1] == A @ x[:, t] + B @ u[:, t] + C.flatten()]\n",
    "\n",
    "    # sums problem objectives and concatenates constraints.\n",
    "    constr += [x[:, 0] == x_bar[:, 0]]  # <--watch out the start condition\n",
    "    constr += [u[0, :] <= MAX_SPEED]\n",
    "    constr += [u[0, :] >= MIN_SPEED]\n",
    "    constr += [cp.abs(u[1, :]) <= MAX_STEER_SPEED]\n",
    "\n",
    "    # Solve\n",
    "    prob = cp.Problem(cp.Minimize(cost), constr)\n",
    "    solution = prob.solve(solver=cp.OSQP, verbose=False)\n",
    "\n",
    "    # retrieved optimized U and assign to u_bar to linearize in next step\n",
    "    u_bar = np.vstack(\n",
    "        (np.array(u.value[0, :]).flatten(), (np.array(u.value[1, :]).flatten()))\n",
    "    )\n",
    "\n",
    "    u_sim[:, sim_time] = u_bar[:, 0]\n",
    "\n",
    "    # Measure elpased time to get results from cvxpy\n",
    "    opt_time.append(time.time() - iter_start)\n",
    "\n",
    "    # move simulation to t+1\n",
    "    tspan = [0, dt]\n",
    "    x_sim[:, sim_time + 1] = odeint(\n",
    "        kinematics_model, x_sim[:, sim_time], tspan, args=(u_bar[:, 0],)\n",
    "    )[1]\n",
    "\n",
    "print(\n",
    "    \"CVXPY Optimization Time: Avrg: {:.4f}s Max: {:.4f}s Min: {:.4f}s\".format(\n",
    "        np.mean(opt_time), np.max(opt_time), np.min(opt_time)\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 61116 characters]\n",
      "text/plain": [
       "<Figure size 1080x720 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot trajectory\n",
    "grid = plt.GridSpec(2, 3)\n",
    "\n",
    "plt.figure(figsize=(15, 10))\n",
    "\n",
    "plt.subplot(grid[0:2, 0:2])\n",
    "plt.plot(track[0, :], track[1, :], \"b+\")\n",
    "plt.plot(x_sim[0, :], x_sim[1, :])\n",
    "plt.axis(\"equal\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.xlabel(\"x\")\n",
    "\n",
    "plt.subplot(grid[0, 2])\n",
    "plt.plot(u_sim[0, :])\n",
    "plt.ylabel(\"v(t) [m/s]\")\n",
    "\n",
    "plt.subplot(grid[1, 2])\n",
    "plt.plot(np.degrees(u_sim[1, :]))\n",
    "plt.ylabel(\"w(t) [deg/s]\")\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## OBSTACLE AVOIDANCE\n",
    "see dccp paper for reference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-25-0756f6b412cb>:29: RuntimeWarning: invalid value encountered in true_divide\n",
      "  v /= np.linalg.norm(v)\n",
      "<ipython-input-51-a1c8be923656>:44: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n",
      "<ipython-input-51-a1c8be923656>:44: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n",
      "<ipython-input-51-a1c8be923656>:44: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n",
      "<ipython-input-51-a1c8be923656>:44: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n",
      "<ipython-input-51-a1c8be923656>:44: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n",
      "<ipython-input-51-a1c8be923656>:44: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n",
      "<ipython-input-51-a1c8be923656>:44: RankWarning: Polyfit may be poorly conditioned\n",
      "  K=road_curve(x_sim[:,sim_time],track)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CVXPY Optimization Time: Avrg: 21.4061s Max: 49.1719s Min: 1.2366s\n"
     ]
    }
   ],
   "source": [
    "import dccp\n",
    "\n",
    "track = compute_path_from_wp([0, 3, 4, 6, 10, 13], [0, 0, 2, 4, 3, 3], 0.25)\n",
    "\n",
    "obstacles = np.array([[4, 6], [2, 4]])\n",
    "obstacle_radius = 0.5\n",
    "\n",
    "\n",
    "def to_vehic_frame(pt, pos_x, pos_y, theta):\n",
    "    dx = pt[0] - pos_x\n",
    "    dy = pt[1] - pos_y\n",
    "\n",
    "    return [\n",
    "        dx * np.cos(-theta) - dy * np.sin(-theta),\n",
    "        dy * np.cos(-theta) + dx * np.sin(-theta),\n",
    "    ]\n",
    "\n",
    "\n",
    "# track = compute_path_from_wp([0,5,7.5,10,12,13,13,10],\n",
    "#                              [0,0,2.5,2.5,0,0,5,10],0.5)\n",
    "\n",
    "sim_duration = 80  # time steps\n",
    "opt_time = []\n",
    "\n",
    "x_sim = np.zeros((N, sim_duration))\n",
    "u_sim = np.zeros((M, sim_duration - 1))\n",
    "\n",
    "MAX_SPEED = 1.25\n",
    "MIN_SPEED = 0.75\n",
    "MAX_STEER_SPEED = 1.57\n",
    "\n",
    "# Starting Condition\n",
    "x0 = np.zeros(N)\n",
    "x0[0] = 0\n",
    "x0[1] = -0.25\n",
    "x0[2] = np.radians(-0)\n",
    "x_sim[:, 0] = x0\n",
    "\n",
    "# starting guess\n",
    "u_bar = np.zeros((M, T))\n",
    "u_bar[0, :] = 0.5 * (MAX_SPEED + MIN_SPEED)\n",
    "u_bar[1, :] = 0.00\n",
    "\n",
    "for sim_time in range(sim_duration - 1):\n",
    "\n",
    "    iter_start = time.time()\n",
    "\n",
    "    # compute coefficients\n",
    "    K = road_curve(x_sim[:, sim_time], track)\n",
    "\n",
    "    # compute opstacles in ref frame\n",
    "    o_ = []\n",
    "    for j in range(2):\n",
    "        o_.append(\n",
    "            to_vehic_frame(\n",
    "                obstacles[:, j],\n",
    "                x_sim[0, sim_time],\n",
    "                x_sim[1, sim_time],\n",
    "                x_sim[2, sim_time],\n",
    "            )\n",
    "        )\n",
    "\n",
    "    # dynamics starting state w.r.t vehicle frame\n",
    "    x_bar = np.zeros((N, T + 1))\n",
    "\n",
    "    # prediction for linearization of costrains\n",
    "    for t in range(1, T + 1):\n",
    "        xt = x_bar[:, t - 1].reshape(N, 1)\n",
    "        ut = u_bar[:, t - 1].reshape(M, 1)\n",
    "        A, B, C = get_linear_model(xt, ut)\n",
    "        xt_plus_one = np.squeeze(np.dot(A, xt) + np.dot(B, ut) + C)\n",
    "        x_bar[:, t] = xt_plus_one\n",
    "\n",
    "    # CVXPY Linear MPC problem statement\n",
    "    cost = 0\n",
    "    constr = []\n",
    "    x = cp.Variable((N, T + 1))\n",
    "    u = cp.Variable((M, T))\n",
    "\n",
    "    # Prediction Horizon\n",
    "    for t in range(T):\n",
    "\n",
    "        # cost += 30*cp.sum_squares(x[2,t]-np.arctan(df(x_bar[0,t],K))) # psi\n",
    "        cost += 50 * cp.sum_squares(\n",
    "            x[2, t] - np.arctan2(df(x_bar[0, t], K), x_bar[0, t])\n",
    "        )  # psi\n",
    "        cost += 20 * cp.sum_squares(f(x_bar[0, t], K) - x[1, t])  # cte\n",
    "\n",
    "        # Actuation rate of change\n",
    "        if t < (T - 1):\n",
    "            cost += cp.quad_form(u[:, t + 1] - u[:, t], 100 * np.eye(M))\n",
    "\n",
    "        # Actuation effort\n",
    "        cost += cp.quad_form(u[:, t], 1 * np.eye(M))\n",
    "\n",
    "        # Kinrmatics Constrains (Linearized model)\n",
    "        A, B, C = get_linear_model(x_bar[:, t], u_bar[:, t])\n",
    "        constr += [x[:, t + 1] == A @ x[:, t] + B @ u[:, t] + C.flatten()]\n",
    "\n",
    "        # Obstacle Avoidance Contrains\n",
    "        for j in range(2):\n",
    "            constr += [cp.norm(x[0:2, t] - o_[j], 2) >= obstacle_radius]\n",
    "\n",
    "    # sums problem objectives and concatenates constraints.\n",
    "    constr += [x[:, 0] == x_bar[:, 0]]  # <--watch out the start condition\n",
    "    constr += [u[0, :] <= MAX_SPEED]\n",
    "    constr += [u[0, :] >= MIN_SPEED]\n",
    "    constr += [cp.abs(u[1, :]) <= MAX_STEER_SPEED]\n",
    "\n",
    "    # Solve\n",
    "    prob = cp.Problem(cp.Minimize(cost), constr)\n",
    "    solution = prob.solve(method=\"dccp\", verbose=False)\n",
    "\n",
    "    # retrieved optimized U and assign to u_bar to linearize in next step\n",
    "    u_bar = np.vstack(\n",
    "        (np.array(u.value[0, :]).flatten(), (np.array(u.value[1, :]).flatten()))\n",
    "    )\n",
    "\n",
    "    u_sim[:, sim_time] = u_bar[:, 0]\n",
    "\n",
    "    # Measure elpased time to get results from cvxpy\n",
    "    opt_time.append(time.time() - iter_start)\n",
    "\n",
    "    # move simulation to t+1\n",
    "    tspan = [0, dt]\n",
    "    x_sim[:, sim_time + 1] = odeint(\n",
    "        kinematics_model, x_sim[:, sim_time], tspan, args=(u_bar[:, 0],)\n",
    "    )[1]\n",
    "\n",
    "print(\n",
    "    \"CVXPY Optimization Time: Avrg: {:.4f}s Max: {:.4f}s Min: {:.4f}s\".format(\n",
    "        np.mean(opt_time), np.max(opt_time), np.min(opt_time)\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 85696 characters]\n",
      "text/plain": [
       "<Figure size 1080x720 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot trajectory\n",
    "grid = plt.GridSpec(2, 3)\n",
    "\n",
    "plt.figure(figsize=(15, 10))\n",
    "\n",
    "ax = plt.subplot(grid[0:2, 0:2])\n",
    "plt.plot(track[0, :], track[1, :], \"b+\")\n",
    "plt.plot(x_sim[0, :], x_sim[1, :])\n",
    "# obstacles\n",
    "circle1 = plt.Circle((obstacles[0, 0], obstacles[1, 0]), obstacle_radius, color=\"r\")\n",
    "circle2 = plt.Circle((obstacles[0, 1], obstacles[1, 1]), obstacle_radius, color=\"r\")\n",
    "plt.axis(\"equal\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.xlabel(\"x\")\n",
    "\n",
    "ax.add_artist(circle1)\n",
    "ax.add_artist(circle2)\n",
    "\n",
    "plt.subplot(grid[0, 2])\n",
    "plt.plot(u_sim[0, :])\n",
    "plt.ylabel(\"v(t) [m/s]\")\n",
    "\n",
    "plt.subplot(grid[1, 2])\n",
    "plt.plot(np.degrees(u_sim[1, :]))\n",
    "plt.ylabel(\"w(t) [deg/s]\")\n",
    "\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:jupyter] *",
   "language": "python",
   "name": "conda-env-jupyter-py"
  },
  "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.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
