Self-Driving Car Engineer Nanodegree Program
The purpose of this project is to use Model Predictive Control (MPC) to drive a lap around the track while staying on the drivable portion of the track surface.
- Clone this repo. (see original udacity repo here)
- Make a build directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./mpc
.
The MPC consists of:
- Vehicle trajectory with number of timesteps
N
and timestep durationdt
. - Vehicle state and actuation variables, along with lower/upper constraints on the variables. State and actuation consists of:
x,y
positionspsi
yaw anglev
velocitycte
cross track errorepsi
yaw errordelta
steering anglea
acceleration
- Cost function to optimize actuation for 2 objectives: (1) Speed close to desired speed
ref_v
(set to 128 MPH), (2) Trajectory close to polynomial line of reference path. The cost function utilizes state & actuation values as well as the value gap between sequential actuator cost (deltadot
,adot
).
The cost function weights are taken from the project Q&A video after some experimentation with other weighting factors that heavily penalized delta
& deltadot
rather than the current heavy weighting on cte
& epsi
seen in MPC.cpp
lines 28-34:
cte_wt = 2000; // cross-track error
epsi_wt = 2000; // psi error
v_wt = 1; // reference velocity
delta_wt = 5; // steering delta
a_wt = 5; // acceleration
deltadot_wt = 200; // steering delta change
adot_wt = 10; // acceleration change
The state is updated with the following equations:
x = x + v * cos(psi) * dt
y = y + v * sin(psi) * dt
psi = psi + (v/Lf) * delta * dt
v = v + a * dt
cte = cte + v * sin(epsi) * dt
epsi = epsi + (v/Lf) * delta * dt
Discuss the reasoning behind the chosen N (timestep length) and dt (elapsed duration between timesteps) values. Provide details of previous values tried.
The values were initially set to N=10
and dt=0.1
, but the timestep length N
value was later increased to 16 to account for additional points projected into the future. The elapsed duration dt
value was increased to 0.11 to be larger than the 0.1s latency period.
After these values failed to impact the controller's results, the values were returned to the original N=10
and dt=0.1
settings from the Q&A video.
A polynomial is fitted to waypoints. If preprocess waypoints, the vehicle state, and/or actuators prior to the MPC procedure it is described.
The yellow reference line in the video indicates a 3rd degree polynomial fitted with waypoints received from telemetry, while the green line indicates the MPC predicted trajectory.
Prior to the MPC updates, the waypoints are in global coordinates and need to be shifted to adopt the vehicle's frame of reference. The vehicle's location is transformed and the yaw angle rotated in main.cpp
lines 104-110.
Implement Model Predictive Control that handles a 100 millisecond latency. Provides details on how to deal with latency.
The MPC handles a 100 millisecond latency (i.e., the car reacts to actuation after 100ms) by using a predicted state that is calculated 100ms into the future.
- This "future" state uses the same update equations in the MPC and adjusts the
x, y, psi, v
variables inmain.cpp
lines 128-135. - The new state is passed to
mpc.Solve
and returns actuation variables that will properly match a state 100ms in the future.
The vehicle can successfully drive around the track at speeds nearing 100 MPH, and two completed laps can be seen here:
- It's recommended to test the MPC on basic examples to see if your implementation behaves as desired. One possible example is the vehicle starting offset of a straight line (reference). If the MPC implementation is correct, after some number of timesteps (not too many) it should find and track the reference line.
- The
lake_track_waypoints.csv
file has the waypoints of the lake track. You could use this to fit polynomials and points and see of how well your model tracks curve. NOTE: This file might be not completely in sync with the simulator so your solution should NOT depend on it. - For visualization this C++ matplotlib wrapper could be helpful.
- cmake >= 3.5
- All OSes: click here for installation instructions
- make >= 4.1
- Linux: make is installed by default on most Linux distros
- Mac: install Xcode command line tools to get make
- Windows: Click here for installation instructions
- gcc/g++ >= 5.4
- Linux: gcc / g++ is installed by default on most Linux distros
- Mac: same deal as make - [install Xcode command line tools]((https://developer.apple.com/xcode/features/)
- Windows: recommend using MinGW
- uWebSockets
- Run either
install-mac.sh
orinstall-ubuntu.sh
. - If you install from source, checkout to commit
e94b6e1
, i.e.Some function signatures have changed in v0.14.x. See this PR for more details.git clone https://github.com/uWebSockets/uWebSockets cd uWebSockets git checkout e94b6e1
- Run either
- Fortran Compiler
- Mac:
brew install gcc
(might not be required) - Linux:
sudo apt-get install gfortran
. Additionall you have also have to install gcc and g++,sudo apt-get install gcc g++
. Look in this Dockerfile for more info.
- Mac:
- Ipopt
- Mac:
brew install ipopt
- Some Mac users have experienced the following error:
This error has been resolved by updrading ipopt withListening to port 4567 Connected!!! mpc(4561,0x7ffff1eed3c0) malloc: *** error for object 0x7f911e007600: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug
brew upgrade ipopt --with-openblas
per this forum post. - Linux
- You will need a version of Ipopt 3.12.1 or higher. The version available through
apt-get
is 3.11.x. If you can get that version to work great but if not there's a scriptinstall_ipopt.sh
that will install Ipopt. You just need to download the source from the Ipopt releases page or the Github releases page. - Then call
install_ipopt.sh
with the source directory as the first argument, ex:sudo bash install_ipopt.sh Ipopt-3.12.1
.
- You will need a version of Ipopt 3.12.1 or higher. The version available through
- Windows: TODO. If you can use the Linux subsystem and follow the Linux instructions.
- Mac:
- CppAD
- Mac:
brew install cppad
- Linux
sudo apt-get install cppad
or equivalent. - Windows: TODO. If you can use the Linux subsystem and follow the Linux instructions.
- Mac:
- Eigen. This is already part of the repo so you shouldn't have to worry about it.
- Simulator. You can download these from the releases tab.
- Not a dependency but read the DATA.md for a description of the data sent back from the simulator.