Git Product home page Git Product logo

carnd-project-06-bicycle-tracker-with-extended-kalman-filter's Introduction

CarND-Project-06-Bicycle-Tracker-with-Extended-Kalman-Filter

Udacity Self-Driving Car Engineer Nanodegree: Project 5 - Extended Kalman Filter

Further reading

Sim

Files in the Github src Folder

  • main.cpp - communicates with the Term 2 Simulator receiving data measurements, calls a function to run the Kalman filter, calls a function to calculate RMSE

  • FusionEKF.cpp - initializes the filter, calls the predict function, calls the update function

  • kalman_filter.cpp - defines the predict function, the update function for lidar, and the update function for radar

  • tools.cpp - function to calculate RMSE and the Jacobian matrix

Data File

data file: obj_pose-laser-radar-synthetic-input.txt

Whereas radar has three measurements (rho, phi, rhodot), lidar has two measurements (x, y).

Variable Definitions

  • x is the state vector.

  • z is measurement vector.

    • For a lidar sensor, the z vector contains the position−x and position−y measurements.
  • H is the measurement matrix, which projects your belief about the object's current state into the measurement space of the sensor.

  • Multiplying Hx allows us to compare x, our belief, with z, the sensor measurement.

  • R is the Measurement Noise Covariance Matrix, which represents the uncertainty in our sensor measurements.

    • will be provided by the sensor manufacturer

    • the off-diagonal 0 in RR indicate that the noise processes are uncorrelated.

  • Q is the process covariance matrix.

Kalman Filter

1. Initialize

  1. Initialize the state ekf_.x_ with the first measurement.

  2. Create the covariance matrix.

ekf_.P_ = MatrixXd(4, 4);
ekf_.P_ << 1, 0, 0, 0,
          0, 1, 0, 0,
          0, 0, 1000, 0,
          0, 0, 0, 1000;
  1. Convert radar from polar to cartesian coordinates.
if (measurement_pack.sensor_type_ == MeasurementPackage::RADAR) {
  float rho     = measurement_pack.raw_measurements_(0);
  float phi     = measurement_pack.raw_measurements_(1);
  float rho_dot = measurement_pack.raw_measurements_(2);
  ekf_.x_(0) = rho     * cos(phi);
  ekf_.x_(1) = rho     * sin(phi);      
  ekf_.x_(2) = rho_dot * cos(phi);
  ekf_.x_(3) = rho_dot * sin(phi);
}
else if (measurement_pack.sensor_type_ == MeasurementPackage::LASER) {
  ekf_.x_(0) = measurement_pack.raw_measurements_(0);
  ekf_.x_(1) = measurement_pack.raw_measurements_(1);
  ekf_.x_(2) = 0.0;
  ekf_.x_(3) = 0.0;
}

2. Predict

  1. Update State transition matrix with dt
ekf_.F_ = MatrixXd(4, 4);
ekf_.F_ << 1, 0, dt, 0,
   0, 1, 0, dt,
   0, 0, 1, 0,
   0, 0, 0, 1;
  1. set the process covariance matrix Q with dt
ekf_.Q_ = MatrixXd(4, 4);
ekf_.Q_ << dt_4 / 4 * noise_ax, 0, dt_3 / 2 * noise_ax, 0,
   0, dt_4 / 4 * noise_ay, 0, dt_3 / 2 * noise_ay,
   dt_3 / 2 * noise_ax, 0, dt_2*noise_ax, 0,
   0, dt_3 / 2 * noise_ay, 0, dt_2*noise_ay;
  1. Call kalman filter predict.
ekf_.Predict();

3. Update

if (measurement_pack.sensor_type_ == MeasurementPackage::RADAR) {
  // Radar updates
  // Use Jacobian instead of H
  ekf_.H_ = tools.CalculateJacobian(ekf_.x_);
  ekf_.R_ = R_radar_;
  ekf_.UpdateEKF(measurement_pack.raw_measurements_);
} else {
  // Laser updates
  ekf_.H_ = H_laser_;
  ekf_.R_ = R_laser_;
  ekf_.Update(measurement_pack.raw_measurements_);
}
void KalmanFilter::UpdateEKF(const VectorXd &z) {

  // update the state by using Extended Kalman Filter equations
  // Recalculate x object state to rho, theta, rho_dot coordinates
  double px = x_(0);
  double py = x_(1);
  double vx = x_(2);
  double vy = x_(3);
  double rho = sqrt(px * px + py * py);
  double theta = atan2(py, px);
  double rho_dot = (px * vx + py * vy) / rho;

  VectorXd h = VectorXd(3);
  h << rho, theta, rho_dot;
  VectorXd y = z - h;

  if (y(1) > M_PI) {
    y(1) = 2*M_PI - y(1);
  } else if (y(1) < -M_PI){
    y(1) = 2*M_PI + y(1);
  }
  cout << "theta" << y(1) << endl<<endl;
  UpdateWithY(y);
}
void KalmanFilter::Update(const VectorXd &z) {

  // update the state by using Kalman Filter equations
  VectorXd y = z - H_ * x_;
  UpdateWithY(y);
}
void KalmanFilter::UpdateWithY(const VectorXd &y){

  // Kalman gain:
  MatrixXd Ht_ = H_.transpose();
  MatrixXd K = P_ * Ht_ * (H_ * P_ * Ht_ + R_).inverse();

  //new estimate
  x_ = x_ + (K * y);
  int x_size = x_.size();
  MatrixXd I = MatrixXd::Identity(x_size, x_size);
  P_ = (I - K * H_) * P_;
}

carnd-project-06-bicycle-tracker-with-extended-kalman-filter's People

Contributors

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