Git Product home page Git Product logo

mpc-project's Introduction

CarND-Controls-MPC

Self-Driving Car Engineer Nanodegree Program


MPC

Model Predicr Cotrol uses an optimizer to find the control input and minimize the cost function. Here are the state vector $ state = \left[ x, y, \psi, v, cte, e\psi \right]$

Duration

Set up everything required for the MPC loop. This consist of defining the duration of the trajectory, $ T = N * dt $, Here I set N = 10 and dt = 0.1. the duration T = 1s.

Cost Function

void operator()(ADvector& fg, const ADvector& vars)

fg is the cost function and vehicle model/constrain is defined. vars contains all variables used by the cost functin and model. This si all one lont vector, I set N = 10, so the indices are show as follows: $$ vars[0],\dots, vars[9] -> \left[ x_1,\dots,x_{10}\right] $$ $$ vars[10],\dots, vars[19] -> \left[ y_1,\dots,y_{10}\right] $$ $$ vars[20],\dots, vars[29] -> \left[ \psi_1,\dots,\psi_{10}\right] $$ $$ vars[30],\dots, vars[19] -> \left[ v_1,\dots,v_{10}\right] $$ $$ vars[40],\dots, vars[49] -> \left[ cte_1,\dots,cte_{10}\right] $$ $$ vars[50],\dots, vars[59] -> \left[ e\psi_1,\dots,e\psi_{10}\right] $$ $$ vars[60],\dots, vars[69] -> \left[ \delta_1,\dots,\delta_{9}\right] $$ $$ vars[70],\dots, vars[79] -> \left[ a_1,\dots,a_{9}\right] $$

fg[0] store the cost value, I sum all the components of the cost and store them at fg[0]. $$ J = \sum_{t=1}^{N}(cte_t - cte_{ref})^2 + (e\psi_t - e\psi_{ref})^2 + \dots $$

for (int i = 0; i < N; ++i)
    {
      fg[0] += 3500 * CppAD::pow(vars[cte_start + i], 2);
      fg[0] += 3500 * CppAD::pow(vars[epsi_start + i], 2);
      fg[0] += CppAD::pow(vars[v_start + i] - ref_v, 2);
    }

    for (int i = 0; i < N - 1; ++i)
    {
      fg[0] += CppAD::pow(vars[delta_start + i], 2);
      fg[0] += CppAD::pow(vars[a_start + i], 2);
      // penalty weight for steering and speed
      fg[0] += 500 * CppAD::pow(vars[delta_start + i] * vars[v_start + i], 2);
    }

    for (int i = 0; i < N - 2; ++i)
    {
      fg[0] += 200 * CppAD::pow(vars[delta_start + i + 1] - vars[delta_start + i], 2);
      fg[0] += CppAD::pow(vars[a_start + i + 1] - vars[a_start + i], 2);
    }

Define the vehicle model and constraints $$ x_{t + 1} = x_t + v_t * \cos(\psi_t) * dt$$ $$ y_{t+1} = y_t + x_t * \sin(psi_t) * dt $$ $$ \psi_{t+1} = \psi_t - \frac{v_t}{L_f} * \delta_f * dt $$ $$ v_{t+1} = v_t a_t * dt $$ $$ cte_{t+1} = f(x_t) - y_t + v_t \sin(e\psi_t)dt $$ $$ e\psi_t = \psi_t - \psi_{des_t} - \frac{v_t}{L_f}\delta_tdt $$

$$\delta \in \left[ -25^\circ, 25^\circ \right]$$ $$ a \in \left[ -1, 1\right] $$

Note that the $\delta$ is positive we rotate counter-clockwise, or turn left. In the simulator however, a positive value implies a right turn and a negative value implies a left turn.

Latency

In a real car, an actuation command won't execute instantly, so there will be a delay as the command propagtes throuth the system. a relaistic delay might be ont he order of 100ms. dt = 0.1(100ms), so I set previous $\delta$(steering value) and $\alpha$(throttle value) state to the constraints instead of delay.

  previous_delta = steering_value
  previous_a = throttle_value
  //take previous state for delaing 100ms.
  vars_lowerbound[delta_start + 1] = previous_delta;
  vars_upperbound[delta_start + 1] = previous_delta;
  vars_lowerbound[a_start + 1] = previous_a;
  vars_upperbound[a_start + 1] = previous_a;

Dependencies

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.)
  4. Tips for setting up your environment are available here
  5. VM Latency: Some students have reported differences in behavior using VM's ostensibly a result of latency. Please let us know if issues arise as a result of a VM environment.

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

How to write a README

A well written README file can enhance your project and portfolio. Develop your abilities to create professional README files by completing this free course.

mpc-project's People

Contributors

aaronmayue avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.