Giter Site home page Giter Site logo

chrasmus / sdcn_may18_t2_p5_mpc_controller Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 16.65 MB

Udacity Self-Driving Car Nanodegree, Term 2, Project 5 : Implementation of a MPC controller in C++

License: MIT License

CMake 1.84% C++ 83.02% Shell 0.18% C 2.03% Fortran 11.48% Ruby 0.12% Cuda 1.14% Python 0.08% JavaScript 0.07% CSS 0.05%

sdcn_may18_t2_p5_mpc_controller's Introduction

MPC controller

Writeup


Udacity Course, October 2017 cohort

Self-Driving Car Engineer Nanodegree Program

Project 'MPC Controller', May 2018

Claus H. Rasmussen


Build a Model Prediction Controller in C++ and maneuver the vehicle round the lake track in the Udacity simulator.

The Model The Model Prediction Controller is build around the concept of 'simple' kinematic models that ignore tire forces, gravity, and mass. The model has a state consisting of position (x,y), an orientation angle (psi) and a velocity (v). The implemented model tracks the evolving state over time and two actuators/control inputs, steering angle (d) and throttle (a), that allows us to control the vehicle state.

At time t+1, where dt is the time step, the formulas for calculating the new state are:

x(t+1)   = x(t) + v(t) * cos(psi) * dt
y(t+1)   = y(t) + v(t) * sin(psi) * dt
psi(t+1) = psi(t) + v(t) * d(t) * dt / Lf
v(t+1)   = v(t) + a(t) * dt

The simulator sends a trajectory in form of a series of waypoints to the model, that fits them to a 3rd order polynomial, using Eigen. Given a fixed duration and timesteps (see next section), a number of discrete paths between the actuations to be used by the car, makes the car following the predicted trajectory. A cost function is used to ensure that the actuators receive values, that makes the car drive as good as possible, making the errors between the actual path and the predicted as small as possible.

Elapsed Duration and Timestep Length (N & dt) With a value for Timestep Length fixed at 0.1 seconds, I tried out a couple of values for Elapsed duration (N) and found that the car crashed using a value of 20, while setting N to 10 (equals to a time horisont of one second), the car is able to do several lapses around the lake track. I believe it is possible to fine tune N and dt a little further, e.g. N=14 and dt=0.08 may give a better overall result, but I haven't gone deeper into this in this project.

Polynomial Fitting and MPC Preprocessing The waypoints, send from the simulator, are processed with the provided polyfit(...) function in main.cpp, line 111. The result is the coefficients to the fitted polynomial, which first are used to calculate the cross track error and the orientation error values, cte and epsi, again using a provided function, polyeval(...) in main.cpp, line 114 and 117. After calculating the state vector, considering a latency of 100 milliseconds, the coefficients are used in the central MPC.Solve(...) function, along with the state vector (main.cpp, line 155).

The MPC object, from MPC.cpp and MPC.h, is set up using a cost function, in MPC.cpp, line 53-74. This happens in a FGeval object, that is used by the MPC.solve(...) function. In the FGeval object, the errors (cte and epsi) are added to the cost function using a high value multiplicator (= 2000) to ensure that these variables gets 'high attention'. Furthermore values in the power of two for speed, steering angle and throttle values are added, including a 'difference cost' for steering and throttle, in order to make sure that they don't grow or shrink too fast during the drive. Next the constraints are added to the fg vector.

In the MPC.Solve(...) function (MPC.cpp, line 134-268), the upper and lower limits for the variables for the states and actuators are set. Values for the initial state are added last. The are comments in the code for these variables.

After solving the problem, using the function CppAD::ipopt::solve(...), MPC.cpp, line 238, the result is processed before it is returned to the simulator and used to actuate steering and throttle.

Model Predictive Control with Latency In main.cpp, lines 126-145, a latency (delay_t = 100 milliseconds) is added to the state vector:

if (consider_latency) {
  // State values reflects latency
  double delay_x = v * delay_t;
  double delay_y = 0;
  double delay_psi = v * -steer_value / Lf * delay_t;
  double delay_v = v + throttle_value * delay_t;
  double delay_cte = cte + v * sin(epsi) * delay_t;
  double delay_epsi = epsi + v * -steer_value /Lf * delay_t;
  state << delay_x, delay_y, delay_psi, delay_v, delay_cte, delay_epsi;
}
else {
  // State values without latency corrections
  state << 0, 0, 0, v, cte, epsi;
}

And it eventually was a success, it took a lot of debugging and testing, sometimes it was fun, but it turned out well with a car that actually drives quite nicely :-)




This section was provided by Udacity to help set up the coding environment etc.

CarND-Controls-MPC

Self-Driving Car Engineer Nanodegree Program


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.

sdcn_may18_t2_p5_mpc_controller's People

Contributors

chrasmus avatar

Watchers

James Cloos avatar  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.