Self-Driving Car Engineer Nanodegree Program
This project implement Model Predictive Control in C++ that automatically drive a vehicle smoothly at high speed in simulated environment. The program/application receives live data about the vehicle current position, steer direction, and speed. The vehicle information is fetch through a model that will calculate the optimal driving path and driving commands(actuators), which the application will send back to the vehicle to control/update the vehicle acceleration and steering. Then, the process is repeat with vehicle send back latest information to the application for another model iteration.
The vehicle's state is respresent as a vector consist of the car's position in "x" and "y" coordinate in meters, heading direction notated as "ψ" (psi) in radian value, speed/velocity "v" in meters/second, cross track error "cte" in meter, and heading error e\psi (radians) at time t.
Steering angle, acceleration, and brake are actuators that being modeled and store as vector. For simplicity acceleration and brake are combine as single actuator of throttle with range value of 1 and -1, which -1 produce a complete stop/brake. Steering is noted as delta and throttle is noted as "a" through out the codes.
To model the physical constraint of the real world, steering is limited to turning angel of 25° and -25°.
The current state will be passed to following equations calculate vehicle future state.
x_[t+1] = x[t] + v[t] * cos(psi[t]) * dt
y_[t+1] = y[t] + v[t] * sin(psi[t]) * dt
psi_[t+1] = psi[t] + v[t] / Lf * delta[t] * dt
v_[t+1] = v[t] + a[t] * dt
cte[t+1] = f(x[t]) - y[t] + v[t] * sin(epsi[t]) * dt
epsi[t+1] = psi[t] - psides[t] + v[t] * delta[t] / Lf * dt
The vehicle's position and traveling path data are passed to the application as world's coordinate perspective. The application must convert the given world's coordinate to the car's coordinate system/perspective, then pass these converted coordinate values to Polynomial fitting function.
Coordinates conversion is implemented on "main.cpp" between line 121 to 131.
The application is also rely on two optimal parameters "N" and "dt", which are manualy set and tune. "N" repesent number of forward traveling points to calculate/predict, in other words, number of timesteps in the horizon. "dt" represent time delay factor that occur betwen each control actuation.
The product result of "N" and "dt" will far(in time) in future horizon the application should calculate. Optimally this value should be a few a seconds at most.
Initially, "N" was set at low value of 5 while "dt" was set at high value of 0.5. But, these values produced undesirable result. "N" was finally tuned at 10 and "dt" was at 0.1.
"N" and "dt" are set on "MPC.pp" at line 9 and 10;
http://www.youtube.com/watch?v=ETqwuQWn_E8
- cmake >= 3.5
- All OSes: click here for installation instructions
- make >= 4.1(mac, linux), 3.81(Windows)
- 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
- If challenges to installation are encountered (install script fails). Please review this thread for tips on installing 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. - 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.
- 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.
- Clone this repo.
- Make a build directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./mpc
.
- 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.)
- Tips for setting up your environment are available here