Self-Driving Car Engineer Nanodegree Program
- Define the length of the trajectory, N, and duration of each timestep, dt.
- Define vehicle dynamics and actuator limitations along with other constraints.
- Define the cost function.
- We pass the current state as the initial state to the model predictive controller.
- We call the optimization solver. Given the initial state, the solver will return the vector of control inputs that minimizes the cost function. The solver we'll use is called Ipopt.
- We apply the first control input to the vehicle.
- Back to first step.
- N is the number of timesteps the model predicts ahead. Higher N means further the prediction.
- dt is the length of each timestep i.e,. it is the frequency in which steering and accelation re-evaluates.
- Below values are best output of multiple experiments I did: We set the number of timesteps to 10 and the timestep evaluation frequency or evaluation period to 0.1.
size_t N = 10; //20 //50 // Higher N causes to drift the car of track very soon
double dt = 0.1; // 0.01, .03
const double Lf = 2.67;
3. Both the reference cross track and orientation errors are 0. The reference velocity is set to 50 mph.
double ref_cte = 0;
double ref_epsi = 0;
double ref_v = 50; //40
Played around with these coeff to get best driving expereince
constexpr double coeff_cte = 1.; // cross track error
constexpr double coeff_epsi = 1.; // angle
constexpr double coeff_v = 1.; // velocity
constexpr double coeff_penalize_delta = 100.; // Best = 100,200.
6. Minimizes the use of acceleration. With value as 100 - car drives smoothly and accurately but was steady and slow at 25 kmph
constexpr double coeff_penalize_a = 20.;
constexpr double coeff_derivative_delta = 10.; // / Best = 10, even 100 works but 10 seems more smooth
constexpr double coeff_derivative_a = 100.; // Best = 10
- Reference state cost - influences car to follow a reference (CTE) , orienation and velocity
for (int i = 0; i < N; i++) {
fg[0] += coeff_cte * CppAD::pow(vars[cte_start + i] - ref_cte, 2);
fg[0] += coeff_epsi * CppAD::pow(vars[epsi_start + i] - ref_epsi, 2);
fg[0] += coeff_v * CppAD::pow(vars[v_start + i] - ref_v, 2);
}
- Minimize the use of actuators - Steering and Accelerator.
//
for (int i = 0; i < N - 1; i++) {
fg[0] += coeff_penalize_delta * CppAD::pow(vars[delta_start + i], 2);
fg[0] += coeff_penalize_a * CppAD::pow(vars[a_start + i], 2);
}
- Minimize the value gap between sequential actuations.
for (int i = 0; i < N - 2; i++) {
fg[0] += coeff_derivative_delta * CppAD::pow(vars[delta_start + i + 1] - vars[delta_start + i], 2);
fg[0] += coeff_derivative_a * CppAD::pow(vars[a_start + i + 1] - vars[a_start + i], 2);
}
- The MPC model was used/initialized in main.cpp.
- Reference way points provided by simulator are converted to vehicle coordinate system (from world space to car space)
- Converted reference points were used to fit a polynomial
- ptsx (Array) - The global x positions of the waypoints.
- ptsy (Array) - The global y positions of the waypoints. This corresponds to the z coordinate in Unity since y is the up-down direction.
- psi (float) - The orientation of the vehicle in radians converted from the Unity format to the standard format expected in most mathemetical functions (more details below).
- psi_unity (float) - The orientation of the vehicle in radians. This is an orientation commonly used in navigation.
- x (float) - The global x position of the vehicle.
- y (float) - The global y position of the vehicle.
- steering_angle (float) - The current steering angle in radians.
- throttle (float) - The current throttle value [-1, 1].
- speed (float) - The current velocity in mph.
Most of the code in main.cpp were from classroom questions:
-
Find CTE: Ref: https://github.com/udacity/CarND-MPC-Quizzes/blob/master/polyfit/solution/main.cpp#L47
-
Find orientation error: Ref: https://github.com/udacity/CarND-MPC-Quizzes/blob/master/mpc_to_line/solution/MPC.cpp#L323
-
Get data from MPC: Ref: https://github.com/udacity/CarND-MPC-Quizzes/blob/master/mpc_to_line/solution/MPC.cpp#L342
https://youtu.be/LCD1_OBZSG8
- 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 == 0.14, but the master branch will probably work just fine
- Follow the instructions in the uWebSockets README to get setup for your platform. You can download the zip of the appropriate version from the releases page. Here's a link to the v0.14 zip.
- If you have MacOS and have Homebrew installed you can just run the ./install-mac.sh script to install this.
- 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 - Linux
- You will need a version of Ipopt 3.12.1 or higher. The version available through
apt-getis 3.11.x. If you can get that version to work great but if not there's a scriptinstall_ipopt.shthat will install Ipopt. You just need to download the source from the Ipopt releases page or the Github releases page. - Then call
install_ipopt.shwith the source directory as the first argument, ex: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 cppador 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.csvfile 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.
We've purposefully kept editor configuration files out of this repo in order to keep it as simple and environment agnostic as possible. However, we recommend using the following settings:
- indent using spaces
- set tab width to 2 spaces (keeps the matrices in source code aligned)
Please (do your best to) stick to Google's C++ style guide.
Note: regardless of the changes you make, your project must be buildable using cmake and make!
More information is only accessible by people who are already enrolled in Term 2 of CarND. If you are enrolled, see the project page for instructions and the project rubric.
- You don't have to follow this directory structure, but if you do, your work will span all of the .cpp files here. Keep an eye out for TODOs.
Help your fellow students!
We decided to create Makefiles with cmake to keep this project as platform agnostic as possible. Similarly, we omitted IDE profiles in order to we ensure that students don't feel pressured to use one IDE or another.
However! I'd love to help people get up and running with their IDEs of choice. If you've created a profile for an IDE that you think other students would appreciate, we'd love to have you add the requisite profile files and instructions to ide_profiles/. For example if you wanted to add a VS Code profile, you'd add:
- /ide_profiles/vscode/.vscode
- /ide_profiles/vscode/README.md
The README should explain what the profile does, how to take advantage of it, and how to install it.
Frankly, I've never been involved in a project with multiple IDE profiles before. I believe the best way to handle this would be to keep them out of the repo root to avoid clutter. My expectation is that most profiles will include instructions to copy files to a new location to get picked up by the IDE, but that's just a guess.
One last note here: regardless of the IDE used, every submitted project must still be compilable with cmake and make./