/CarND-MPC-Project

CarND Term 2 Model Predictive Control (MPC) Project

Primary LanguageC++

CarND-Controls-MPC

Self-Driving Car Engineer Nanodegree Program

The Car Model

The bicycle model is implemented in this project to approximate the dynamics of the vehicle, given its simplicity. The relevant states are the x,y position of the vehicle, its heading psi and longitudinal velocity magnitude v. The actuations of the vehicle are represented by the acceleration a, which is the first derivative of the velocity v, and delta, the steering angle of the car.

The position update of the vehicle is given by the discretized integral of an object moving at speed v in the heading psi. The speed v is updated by the discretized integral of the acceleration a and the heading psi is updated by integrating the angular rotation rate induced by the front axle of the vehicle moving at speed v at an angle delta from the longitudinal direction, with a moment arm of Lf from the center of mass of the car.

Mathematically, we state that: x(t+1) = x(t) + delta_t * cos(psi(t)) v(t) y(t+1) = y(t) + delta_t * sin(psi(t)) v(t) v(t+1) = v(t) + delta_t * a(t) psi(t+1) = psi(t) + delta_t * v(t) / Lf * delta(t)

The Time Horizon Choice

The time horizon, N, of value 10 was chosen because on my local machine it runs without a considerable lag (This wasn't the case when N was set to 50). Given the reference speed of 40mph and the time horizon of 10 steps, a reasonable time step length of 0.1 seconds was chosen. At higher values, e.g. 0.5 when the MPC controller had to predict long trajectories, the controller did not consistently return a valid trajectory.

Waypoint Following

The waypoints in the global map coordinates are transformed into the vehicle's coordinates. Thereafter, the trajectory is approximated by a third order polynomial(or lower depending on the number of waypoints).

Accounting for Actuator Latency

In reality, the desired actuations output by the MPC controller is not enforced instantaneously. In this project, it is assumed that it takes 0.1 seconds before the vehicle reaches the desired setpoint steering angle and throttle acceleration. To account for this artifact, the initial state of the vehicle at the first time horizon, is projected forward by the delay of 0.1 seconds. Since in the vehicle frame, the car's position and orientation is given by the identity affine transformation, the resultant drift due to the time delay is given by the following.

x(0) = 0 + delay * cos(psi(0)) v(0) y(0) = 0 + delay * sin(psi(0)) v(0) v(0) = v(0) + delay * a(0) psi(0) = 0 + delay * v(0) / Lf * delta(0)

Note that psi(0) = 0


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.