tzanis-anevlavis / CarND-MPC-Project

MPC for car trajectory following

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CarND-Controls-MPC

Disclaimer: The simulation environment in this repository is derived from the Car MPC project of Udacity. If you like it feel free to support it in the above link!

Description

System definition

The equations of motion are assumed as follows:

x[k+1] = x[k] + v[k] * cos(psi[k]) * dt
y[k+1] = y[k] + v[k] * sin(psi[k]) * dt
v[k+1] = v[k] + a[k] * dt
psi[k+1] = psi[k] + v[k] / L * delta[k] * dt
cte[k+1] = f(x[k]) - y[k] + v[k] * sin(epsi[k]) * dt
epsi[k+1] = psi[k] - psi_des[k] + v[k] / L * delta[k] * dt

where the state is x, y (position), v (velocity), psi (orientation), cte (cross-track error, i.e., the difference between the nominal trajectory and the current vehicle position y), and epsi (orientation error), and the input is a (acceleration) and delta (steering angle).

Actuation constraints

The input constraints are a \in [-1. 1], corresponding to full brake and full throttle, and delta \in [-25 deg, +25 deg].

Cost function

The goal here is to use MPC to perform trajectory following while adhering to the constraints. To this end, we design a cost function we seek to minimize over time k:

J = \sum_k (||cte[k]||2 + ||epsi[k]||2 + ||v[k]-v_des||2) + \sum_k (||a[k]||2 + ||delta[k]||2) + \sum_k (||a[k]-a[k-1]||2 + ||delta[k]-delta[k-1]||2)

The first sum asks to follow closely the desire trajectory by minimizing the magnitude of cte, epsi, and v-v_des. The second sum asks to keep the actuation low (efficiency). The third sum asks to not have abrupt changes in accelerating/decelerating or steering (comfort).

mpc_preview

Trajectory generation

The simulator provides waypoints that act as nominal trajectory. These way points have to be transformed to the car coordinate system:

// For waypoint i:
diffx = ptsx[i]-px;
diffy = ptsy[i]-py;
ptsx[i] = diffx * cos(psi) + diffy * sin(psi);
ptsy[i] = diffy * cos(psi) - diffx * sin(psi);

Then we fit a 3rd-degree polynomial to generate the reference trajectory for the MPC.

The DATA.md contains a description of the data sent back from the simulator.

Dependencies

  1. Udacity's Term 2 Simulator.
  2. uWebSocketIO, which can be installed using the provided bash scripts by Udacity for either Linux (install-ubuntu.sh) or Mac (install-mac.sh) systems.
  3. Eigen template library for linear algebra.
  4. cmake >= 3.5
  5. make >= 4.1 (Linux, Mac)
  6. gcc/g++ >= 5.4
  7. ipopt, an Interior Point OPTimizer.
  8. cppAD, for Algorithmic Differentiation.

Build and run

Once the dependencies are installed, the main program is built and run as follows:

mkdir build
cd build
cmake ..
make
./mpc

For more details on how main.cpp uses uWebSocketIO to communicate with the simulator see the original Udacity repository.

About

MPC for car trajectory following

License:MIT License


Languages

Language:C++ 99.3%Language:Shell 0.5%Language:CMake 0.2%Language:Dockerfile 0.1%