Git Product home page Git Product logo

carnd-project-10-mpc-model-predictive-control's Introduction

CarND-Project-10-MPC-Model-Predictive-Control

Udacity Self-Driving Car Engineer Nanodegree: Project 10 - Model Predictive Control

Reference

udacity/CarND-MPC-Project

Simulation

main.cpp

Input

double px = j[1]["x"];
double py = j[1]["y"];
double psi = j[1]["psi"];
double v = j[1]["speed"];
double delta = j[1]["steering_angle"];
double a = j[1]["throttle"];

Transform the points to the vehicle's orientation

for (int i = 0; i < ptsx.size(); i++) {
  double x = ptsx[i] - px;
  double y = ptsy[i] - py;
  ptsx_car[i] = x * cos(-psi) - y * sin(-psi);
  ptsy_car[i] = x * sin(-psi) + y * cos(-psi);
}

Fits a 3rd-order polynomial to the vehicle's x and y coordinates

auto coeffs = polyfit(ptsx_car, ptsy_car, 3);

Calculates the cross track error

Because points were transformed to vehicle coordinates, x & y equal 0 below.

double cte = polyeval(coeffs, 0);

Calculate the orientation error

Derivative of the polyfit goes in atan() below.

Because x = 0 in the vehicle coordinates, the higher orders are zero, leaves only coeffs[1].

double epsi = -atan(coeffs[1]);

Predict state after latency

// Latency for predicting time at actuation
const double dt = 0.1;

// Predict state after latency
// x, y and psi are all zero after transformation above
double pred_px = 0.0 + v * dt; // Since psi is zero, cos(0) = 1, can leave out
const double pred_py = 0.0; // Since sin(0) = 0, y stays as 0 (y + v * 0 * dt)
double pred_psi = 0.0 + v * -delta / Lf * dt;
double pred_v = v + a * dt;
double pred_cte = cte + v * sin(epsi) * dt;
double pred_epsi = epsi + v * -delta / Lf * dt;

Solve for new actuations

// Feed in the predicted state values
Eigen::VectorXd state(6);
state << pred_px, pred_py, pred_psi, pred_v, pred_cte, pred_epsi;

// Solve for new actuations (and to show predicted x and y in the future)
auto vars = mpc.Solve(state, coeffs);

Calculate steering and throttle

Steering must be divided by deg2rad(25) to normalize within [-1, 1].

Multiplying by Lf takes into account vehicle's turning ability

double steer_value = vars[0] / (deg2rad(25) * Lf);
double throttle_value = vars[1];

mpc.cpp

-> CarND-18-Motion-Planning-MPC-Model-Predictive-Control

carnd-project-10-mpc-model-predictive-control's People

Contributors

chenbohan avatar

Stargazers

 avatar

Watchers

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