/MPC-Project

Submission repository for MPC project for Udacity Self-Driving Car Engineer Nanodegree program

Primary LanguageC++MIT LicenseMIT

CarND-Controls-MPC

Self-Driving Car Engineer Nanodegree Program


Project Goal

The goal of the project is to model a self-driving car using a Model Predictive Controller. The model is tested on a simulated car in the Udacity simulator. The following are the expectations of the project:

  • The car should stay within the lane and not veer too far off the center.
  • The car should drive smoothly minimizing any jerky movements.
  • The car should drive at or below the desired speed.

Vehicle Model

The model used for the vehicle is the kinematic bicycle model. This model takes into account the dynamics of the system, like velocity, heading and ignores other factors like friction, mass and various forces. The following set of equations describe the state of a vehicle at any given time:

Here represents the current state of the vehicle at time . is the cross-track error, the distance of the vehicle's center from the trajectory. is the difference in the heading and desired heading . The above set of equations will estimate the state of vehicle at time . Also, is a constant that represents the distance between the center of mass of the vehicle and it's front wheels. Without it, the above model is only true for a point particle, which is not a realistic reflection of our vehicle. is defined in the code here.

Furthermore, and represent the actuators of the system. is the change in heading, which can be assumed as the amount of steering to apply. is the acceleration and is used as an approximation of the throttle to be applied.

The model updates can be found at MPC.cpp#L124-129.

Model Predictive Control

Once we have the kinematic model of our vehicle, we can use MPC to estimate our future trajectory. In MPC, an optimal control problem is "solved" for a certain number of steps, called the horizon, based on certain frequency. The horizon and frequency are represented in the code by variables N and dt in file MPC.cpp#L9-10 .

The optimal control problem referred above is a nonlinear optimization problem that tries to minimize a certain cost given certain constraints.

The cost in our case is given by:

The terms in the first summation penalize , and that diverge from the desired values. The first two terms in the second summmation penalize high values of actuators. The term penalizes high speeds around curves. denotes the curvature of the road in this case, which is approximated using the highest polynomial coefficent in the code. Finally, the third summation penalizes high differences of previous and current actuators values. This makes turns and acceleration smoother. specifies how much weight we assign to each of the cost terms in the equation, effectively allowing us to tweak the amount of contribution of each. The code for the costs is at MPC.cpp#L58-80

The constraints in our case are given by:

The constraints specify the range of values the variables can take. The code for these constraints is at MPC.cpp#L182-203

Reference frame

The computations for the model are done in the reference frame of the vehicle. Since the waypoints are received in global coordinates, they are converted into vehicle coordinates at main.cpp#L112-120

Trajectory representation

The trajectory in our case consists of waypoints that are pre-defined along the route of travel. These waypoints are represented by the coefficients of a third degree polynomial computed by polyfit here

Latency

The simulation adds a latency of about 100ms. This is to simulate the delay between actuation and effect. We need to account for this latency accurately otherwise the computed and reference trajectories will keep diverging. We do this by setting the initial state for MPC to be the one we estimate after accounting for latency. The code for this is at main.cpp#L134-139. Additionally, dt also plays a big role in this. The best results are obtained by setting dt to the value of the expected latency.

Demo

A demo of the program can be found here

IMAGE ALT TEXT

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.