Skip to content

RyosukeHonda/Model_Predictive_Control

Repository files navigation

CarND-Controls-MPC

Self-Driving Car Engineer Nanodegree Program


Model

The model I used in this project is as follows. The state of the vehicle is defined as [x,y,psi,v], and the actuator is [delta, a]. "delta" which means the steering angle takes the value between and including -25(deg) and 25(deg).(from -0.436332 to 0.436332 in radian) "a" which means the steering angle takes the value between and including -1.0 and 1.0.

Constraint

Below is the cost function to minimize.

  // The part of the cost based on the reference state.
    for (int i = 0; i < N - 1; i++) {
      fg[0] += 2000*CppAD::pow(vars[cte_start + i] - ref_cte, 2);
      fg[0] += 1800*CppAD::pow(vars[epsi_start + i] - ref_epsi, 2);
      fg[0] += CppAD::pow(vars[v_start + i] - ref_v, 2);
    }

    // Minimize the use of actuators.
    for (int i = 0; i < N - 1; i++) {
      fg[0] += 3*CppAD::pow(vars[delta_start + i], 2);
      fg[0] += 5*CppAD::pow(vars[a_start + i], 2);
    }

    // Minimize the value gap between sequential actuations.
    for (int i = 0; i < N - 2; i++) {
      fg[0] += 100*CppAD::pow(vars[delta_start + i + 1] - vars[delta_start + i], 2);
      fg[0] += 10*CppAD::pow(vars[a_start + i + 1] - vars[a_start + i], 2);
    }

Timestep Length and Frequency In this project, the latency is set to 100ms. We have to overcome this latency. We need to predict at least 100ms ahead to control the vehicle appropriately. Therefore, I set dt as 100ms. Then, we have to decide how much we predict the future. The environment surrounding the vehicle will change within a few minutes, so predicting long time ahead isn't so meaningful and of course, prediction accuracy isn't so good. I set 1000ms as the horizon which is not so short and not so long. That means I set N as 10 in this project. In short, the model predicts every 100ms for 10 times.

Polynomial Fitting and MPC preprocessing.

A polynomial is fitted to waypoints. I used 3rd order of polynomial fit. At each timestep, we get 6 waypoints that is global coordinate system. I preprocess those waypoints to the car coordinate system. To covert from global coordinate system to car coordinate system, each of waypoints are subtracted from the car position (px,py) and then rotate by -psi. At each timestep, the car is always located in (0,0) so, the calculation for polynomial fitting is easy.

Rotation

Model Predictive Control with Latency.

As I mentioned above, there exists 100ms latency. Therefore, we need to predict at least 100ms ahead. Predicting further future(more than 100ms) was hard to predict. Therefore, I set it as 100ms.

The result of this project is available from here. https://www.youtube.com/watch?v=YElSbNqtxEw

Dependencies

  • cmake >= 3.5
  • All OSes: click here for installation instructions
  • make >= 4.1
  • gcc/g++ >= 5.4
  • 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.
  • Ipopt
    • Mac: brew install ipopt --with-openblas
    • 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 script install_ipopt.sh that will install Ipopt. You just need to download the source from here.
      • Then call install_ipopt.sh with the source directory as the first argument, ex: bash install_ipopt.sh Ipopt-3.12.1.
    • 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.
  • 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.

Basic Build Instructions

  1. Clone this repo.
  2. Make a build directory: mkdir build && cd build
  3. Compile: cmake .. && make
  4. Run it: ./mpc.

Tips

  1. 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.
  2. 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.
  3. For visualization this C++ matplotlib wrapper could be helpful.

Editor Settings

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)

Code Style

Please (do your best to) stick to Google's C++ style guide.

Project Instructions and Rubric

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.

Hints!

  • 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.

Call for IDE Profiles Pull Requests

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./

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published