{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "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",
    "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",
    "* $\\psi$ heading error = $\\psi = \\theta_{ref} - \\theta$\n",
    "* $cte$ crosstrack error = lateral distance of the robot from the track w.r.t robot frame, cte is a function of the track and x\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} = v\\cos{\\theta}$ \n",
    "* $\\dot{y} = v\\sin{\\theta}$\n",
    "* $\\dot{\\theta} = w$\n",
    "* $\\dot{\\psi} = -w$\n",
    "* $\\dot{cte} = v\\sin{\\psi}$\n",
    "\n",
    "The **Continuous** State Space Model is\n",
    "\n",
    "$ {\\dot{x}} = Ax + Bu $\n",
    "\n",
    "with:\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} & \\frac{\\partial f(x,u)}{\\partial \\psi} & \\frac{\\partial f(x,u)}{\\partial cte} \\\\\n",
    "\\end{bmatrix}\n",
    "\\quad\n",
    "=\n",
    "\\quad\n",
    "\\begin{bmatrix}\n",
    "0 & 0 & -vsin(\\theta) & 0 & 0 \\\\\n",
    "0 & 0 & vcos(\\theta) & 0 & 0 \\\\\n",
    "0 & 0 & 0 & 0 & 0 \\\\\n",
    "0 & 0 & 0 & 0 & 0 \\\\\n",
    "0 & 0 & 0 & vcos(\\psi) & 0 \n",
    "\\end{bmatrix}\n",
    "\\quad $\n",
    "\n",
    "\n",
    "$ B = \\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_t} & 0 \\\\\n",
    "\\sin{\\theta_t} & 0 \\\\\n",
    "0 & 1 \\\\\n",
    "0 & -1 \\\\\n",
    "\\sin{(\\psi_t)} & 0  \n",
    "\\end{bmatrix}\n",
    "\\quad $\n",
    "\n",
    "discretize with forward Euler Integration for time step 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",
    "* ${\\psi_{t+1}} = \\psi_{t} - w_t*dt$\n",
    "* ${cte_{t+1}} = cte_{t} + v_t\\sin{\\psi}*dt$\n",
    "\n",
    "The **Discrete** State Space Model is then:\n",
    "\n",
    "${x_{t+1}} = Ax_t + Bu_t $\n",
    "\n",
    "with:\n",
    "\n",
    "$\n",
    "A = \\quad\n",
    "\\begin{bmatrix}\n",
    "1 & 0 & -v\\sin{\\theta}dt & 0 & 0 \\\\\n",
    "0 & 1 & v\\cos{\\theta}dt & 0 & 0 \\\\\n",
    "0 & 0 & 1 & 0 & 0 \\\\\n",
    "0 & 0 & 0 & 1 & 0 \\\\\n",
    "0 & 0 & 0 & vcos{(\\psi)}dt & 1 \n",
    "\\end{bmatrix}\n",
    "\\quad\n",
    "$\n",
    "\n",
    "$\n",
    "B = \\quad\n",
    "\\begin{bmatrix}\n",
    "\\cos{\\theta_t}dt & 0 \\\\\n",
    "\\sin{\\theta_t}dt & 0 \\\\\n",
    "0 & dt \\\\\n",
    "0 & -dt \\\\\n",
    "\\sin{\\psi_t}dt & 0  \n",
    "\\end{bmatrix}\n",
    "\\quad\n",
    "$\n",
    "\n",
    "This State Space Model is **non-linear** (A,B are time changing), to linearize it the **Taylor's series expansion** is used around $\\bar{x}$ and $\\bar{u}$:\n",
    "\n",
    "$ \\dot{x} = f(x,u) \\approx f(\\bar{x},\\bar{u}) +  A(x-\\bar{x}) + B(u-\\bar{u})$\n",
    "\n",
    "So:\n",
    "\n",
    "$ x_{t+1} = x_t + (f(\\bar{x},\\bar{u}) +  A(x_t-\\bar{x}) + B(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 Discrete linearized kinematics model is\n",
    "\n",
    "$ x_{t+1} = A'x_t + B' u_t + C' $\n",
    "\n",
    "with:\n",
    "\n",
    "$ A' = I+dtA $\n",
    "\n",
    "$ B' = dtB $\n",
    "\n",
    "$ C' = dt(f(\\bar{x},\\bar{u}) - A\\bar{x} - B\\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": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Control problem statement.\n",
    "\n",
    "N = 5 #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": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_linear_model(x_bar,u_bar):\n",
    "    \"\"\"\n",
    "    \"\"\"\n",
    "    \n",
    "    x = x_bar[0]\n",
    "    y = x_bar[1]\n",
    "    theta = x_bar[2]\n",
    "    psi = x_bar[3]\n",
    "    cte = x_bar[4]\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[4,3]=v*np.cos(psi)\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[3,1]=-1\n",
    "    B[4,0]=np.sin(psi)\n",
    "    B_lin=dt*B\n",
    "    \n",
    "    f_xu=np.array([v*np.cos(theta),v*np.sin(theta),w,-w,v*np.sin(-psi)]).reshape(N,1)\n",
    "    C_lin = dt*(f_xu - np.dot(A,x_bar.reshape(N,1)) - np.dot(B,u_bar.reshape(M,1)))\n",
    "    \n",
    "    return A_lin,B_lin,C_lin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Motion Prediction: using scipy intergration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define process model\n",
    "def kinematics_model(x,t,u):\n",
    "    \"\"\"\n",
    "    \"\"\"\n",
    "\n",
    "    dxdt = u[0]*np.cos(x[2])\n",
    "    dydt = u[0]*np.sin(x[2])\n",
    "    dthetadt = u[1]\n",
    "    dpsidt = -u[1]\n",
    "    dctedt = u[0]*np.sin(-x[3])\n",
    "\n",
    "    dqdt = [dxdt,\n",
    "            dydt,\n",
    "            dthetadt,\n",
    "            dpsidt,\n",
    "            dctedt]\n",
    "\n",
    "    return dqdt\n",
    "\n",
    "def predict(x0,u):\n",
    "    \"\"\"\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,\n",
    "                         x0,\n",
    "                         tspan,\n",
    "                         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": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 3.97 ms, sys: 0 ns, total: 3.97 ms\n",
      "Wall time: 3.46 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",
    "x0[3] = 0\n",
    "x0[4] = 1\n",
    "\n",
    "x_bar=predict(x0,u_bar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check the model prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27044 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 4 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",
    "plt.subplot(2, 2, 3)\n",
    "plt.plot(np.degrees(x_bar[3,:]))\n",
    "plt.ylabel('psi(t) [deg]')\n",
    "#plt.xlabel('time')\n",
    "\n",
    "plt.subplot(2, 2, 4)\n",
    "plt.plot(x_bar[4,:])\n",
    "plt.ylabel('cte(t)')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "the results seems valid:\n",
    "* the cte is correct\n",
    "* theta error is correct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Motion Prediction: using the state space model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1.78 ms, sys: 679 µs, total: 2.46 ms\n",
      "Wall time: 1.63 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",
    "x0[3] = 0\n",
    "x0[4] = 1\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(5,1)\n",
    "    ut=u_bar[:,t-1].reshape(2,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": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 25292 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 4 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(x_bar[2,:])\n",
    "plt.ylabel('theta(t)')\n",
    "#plt.xlabel('time')\n",
    "\n",
    "plt.subplot(2, 2, 3)\n",
    "plt.plot(x_bar[3,:])\n",
    "plt.ylabel('psi(t)')\n",
    "#plt.xlabel('time')\n",
    "\n",
    "plt.subplot(2, 2, 4)\n",
    "plt.plot(x_bar[4,:])\n",
    "plt.ylabel('cte(t)')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The results are the same 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": 9,
   "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(np.sqrt(np.power(np.diff(start_xp[idx:idx+2]),2)+np.power(np.diff(start_yp[idx:idx+2]),2)))\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",
    "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 = [path[0,nn_idx+1] - path[0,nn_idx],\n",
    "             path[1,nn_idx+1] - path[1,nn_idx]]   \n",
    "        v /= np.linalg.norm(v)\n",
    "\n",
    "        d = [path[0,nn_idx] - state[0],\n",
    "             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",
    "def road_curve(state,track):\n",
    "    \n",
    "    #given vehicle pos find lookahead waypoints\n",
    "    nn_idx=get_nn_idx(state,track)\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(( dx * np.cos(-state[2]) - dy * np.sin(-state[2]),\n",
    "                                   dy * np.cos(-state[2]) + dx * np.sin(-state[2]) ))\n",
    "\n",
    "    #fit poly\n",
    "    return np.polyfit(wp_vehicle_frame[0,:], wp_vehicle_frame[1,:], 3, rcond=None, full=False, w=None, cov=False)\n",
    "\n",
    "def f(x,coeff):\n",
    "    return round(coeff[0]*x**3 + coeff[1]*x**2 + coeff[2]*x**1 + coeff[3]*x**0,6)\n",
    "\n",
    "def df(x,coeff):\n",
    "    return round(3*coeff[0]*x**2 + 2*coeff[1]*x**1 + coeff[2]*x**0,6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "test it"
   ]
  },
  {
   "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} $\n",
    "\n",
    "#### Non-Linear MPC Formulation\n",
    "\n",
    "In general cases, the objective function is any non-differentiable non-linear function of states and inputs over a finite horizon T. In this case the constrains include nonlinear dynamics of motion.\n",
    "\n",
    "$\n",
    "\\begin{equation}\n",
    "\\begin{aligned}\n",
    "\\min_{} \\quad &  \\sum^{t+T}_{j=t} C(x_{j|t},{j|t})\\\\\n",
    "\\textrm{s.t.} \\quad &  x(0) = x0\\\\\n",
    "  & x_{j+1|t} = f(x_{j|t},u_{j|t}) \\quad  \\textrm{for} \\quad t<j<t+T-1    \\\\\n",
    "\\end{aligned}\n",
    "\\end{equation}\n",
    "$\n",
    "\n",
    "Other nonlinear constrains may be applied:\n",
    "\n",
    "$ g(x_{j|t},{j|t})<0 \\quad  \\textrm{for} \\quad t<j<t+T-1 $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.          0.          0.         -0.17453294 -1.015427  ]\n"
     ]
    }
   ],
   "source": [
    "track = compute_path_from_wp([0,5,10],[0,0,2])\n",
    "\n",
    "# Constrains\n",
    "MAX_SPEED = 2.5\n",
    "MIN_SPEED = 0.5\n",
    "MAX_STEER_SPEED = 1.57/2\n",
    "\n",
    "# Starting Condition\n",
    "x0 = np.zeros(N)\n",
    "x0[0] = 0\n",
    "x0[1] = 1\n",
    "x0[2] = np.radians(-10)\n",
    "\n",
    "#X0 is needed ONLY to initialize road curve\n",
    "K=road_curve(x0,track)\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.0\n",
    "\n",
    "# Linearized Model for Prediction##############\n",
    "x_bar=np.zeros((N,T+1))\n",
    "\n",
    "#starting state is 0,0,0,psi,cte\n",
    "x_bar[0,0]=0\n",
    "x_bar[1,0]=0\n",
    "x_bar[2,0]=0\n",
    "# x_bar[3,0]=x_bar[2,0]-np.arctan2(df(x_bar[0,0],K),x_bar[0,0])\n",
    "x_bar[3,0]=x_bar[2,0]-np.arctan(df(x_bar[0,0],K))\n",
    "x_bar[4,0]=f(x_bar[0,0],K)-x_bar[1,0]\n",
    "\n",
    "print(x_bar[:,0])\n",
    "\n",
    "for t in range (1,T+1):\n",
    "    xt=x_bar[:,t-1].reshape(5,1)\n",
    "    ut=u_bar[:,t-1].reshape(2,1)\n",
    "    \n",
    "    A,B,C=get_linear_model(xt,ut)\n",
    "    \n",
    "    xt_plus_one = np.squeeze(np.dot(A,xt)+np.dot(B,ut)+C)\n",
    "#     xt_plus_one[3]=xt_plus_one[2]-np.arctan2(df(xt_plus_one[0],K),xt_plus_one[0])\n",
    "    xt_plus_one[3]=xt_plus_one[2]-np.arctan(df(xt_plus_one[0],K))\n",
    "    xt_plus_one[4]=f(xt_plus_one[0],K)-xt_plus_one[1]\n",
    "    \n",
    "    x_bar[:,t]= xt_plus_one\n",
    "#################################################\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "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 = 283, constraints m = 323\n",
      "          nnz(P) + nnz(A) = 857\n",
      "settings: linear system solver = qdldl,\n",
      "          eps_abs = 1.0e-05, eps_rel = 1.0e-05,\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.02e+00   1.21e+02   1.00e-01   3.34e-04s\n",
      " 125   1.6631e+03   2.07e-05   3.42e-04   7.12e+01   1.30e-03s\n",
      "\n",
      "status:               solved\n",
      "solution polish:      unsuccessful\n",
      "number of iterations: 125\n",
      "optimal objective:    1663.1222\n",
      "run time:             1.65e-03s\n",
      "optimal rho estimate: 7.91e+01\n",
      "\n",
      "CPU times: user 224 ms, sys: 7.49 ms, total: 232 ms\n",
      "Wall time: 223 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": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22320 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 4 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",
    "psi_mpc=np.array(x.value[3, :]).flatten()\n",
    "cte_mpc=np.array(x.value[4, :]).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": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/marcello/.local/lib/python3.6/site-packages/ipykernel_launcher.py:29: RuntimeWarning: invalid value encountered in true_divide\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CVXPY Optimization Time: Avrg: 0.1798s Max: 0.2277s Min: 0.1655s\n"
     ]
    }
   ],
   "source": [
    "track = compute_path_from_wp([0,3,4,6,10],\n",
    "                             [0,0,2,4,4])\n",
    "\n",
    "sim_duration = 50 #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/2\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\n",
    "    x_bar=np.zeros((N,T+1))\n",
    "    x_bar[3,0]=x_bar[2,0]-np.arctan2(df(x_bar[0,0],K),x_bar[0,0])\n",
    "    x_bar[4,0]=f(x_bar[0,0],K)-x_bar[1,0]\n",
    "    \n",
    "    #x_bar[:,0]=x_sim[:,sim_time]\n",
    "    for t in range (1,T+1):\n",
    "        xt=x_bar[:,t-1].reshape(5,1)\n",
    "        ut=u_bar[:,t-1].reshape(2,1)\n",
    "\n",
    "        A,B,C=get_linear_model(xt,ut)\n",
    "\n",
    "        xt_plus_one = np.squeeze(np.dot(A,xt)+np.dot(B,ut)+C)\n",
    "\n",
    "        xt_plus_one[3]=xt_plus_one[2]-np.arctan2(df(xt_plus_one[0],K),xt_plus_one[0])\n",
    "        xt_plus_one[4]=f(xt_plus_one[0],K)-xt_plus_one[1]\n",
    "        \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",
    "    for t in range(T):\n",
    "\n",
    "        # Tracking\n",
    "        #cost += 10*cp.sum_squares( x[3, t]) # psi\n",
    "        #cost += 50*cp.sum_squares( x[4, t]) # cte\n",
    "        cost += 10*cp.sum_squares(x[2,t]-np.arctan(df(x_bar[0,t],K))) # psi\n",
    "        cost += 50*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], 10*np.eye(M))\n",
    "        \n",
    "        # Actuation effort\n",
    "        cost += cp.quad_form( u[:, t],10*np.eye(M))\n",
    "        \n",
    "        # 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",
    "    # 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((np.array(u.value[0, :]).flatten(),\n",
    "                    (np.array(u.value[1, :]).flatten())))\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(kinematics_model,\n",
    "                                 x_sim[:,sim_time],\n",
    "                                 tspan,\n",
    "                                 args=(u_bar[:,0],))[1]\n",
    "    \n",
    "print(\"CVXPY Optimization Time: Avrg: {:.4f}s Max: {:.4f}s Min: {:.4f}s\".format(np.mean(opt_time),\n",
    "                                                                                np.max(opt_time),\n",
    "                                                                                np.min(opt_time)))   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 81012 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": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
