src/ukf.cpp
/**
 * Updates the state and the state covariance matrix using a radar measurement.
 * @param {MeasurementPackage} meas_package
 */
Updates the state and the state covariance matrix using a radar measurement.
Updates the state and the state covariance matrix using a radar measurement .
void UKF::UpdateRadar(MeasurementPackage meas_package) {
  int n_z = 3;
  MatrixXd Zsig = MatrixXd(n_z, 2 * n_aug_ + 1);
  VectorXd z = VectorXd(n_z);
  z(0) = meas_package.raw_measurements_(0);
  z(1) = meas_package.raw_measurements_(1);
  z(2) = meas_package.raw_measurements_(2);
  for (int i = 0; i < 2*n_aug_+1; i++) {
    double px  = Xsig_pred_(0,i);
    double py  = Xsig_pred_(1,i);
    double v   = Xsig_pred_(2,i);
    double yaw = Xsig_pred_(3,i);
    double v1 = cos(yaw) * v;
    double v2 = sin(yaw) * v;
    Zsig(0,i) = sqrt(px*px + py*py);
    Zsig(1,i) = atan2(py, px);
    Zsig(2,i) = ((px*v1) + (py*v2)) / sqrt(px*px + py*py);
  }
  VectorXd z_pred = VectorXd(n_z);
  z_pred.fill(0.0);
  for (int i=0; i < 2*n_aug_+1; i++) {
      z_pred = z_pred + weights_(i) * Zsig.col(i);
  }
  MatrixXd S = MatrixXd(n_z,n_z);
  S.fill(0.0);
  for (int i = 0; i < 2*n_aug_+1; i++) {
    VectorXd z_diff = Zsig.col(i) - z_pred;
    while (z_diff(1) > M_PI)  z_diff(1) -= 2.*M_PI;
    while (z_diff(1) < -M_PI) z_diff(1) += 2.*M_PI;
    S = S + weights_(i) * z_diff * z_diff.transpose();
  }
  MatrixXd R = MatrixXd(n_z,n_z);
  R <<    std_radr_*std_radr_, 0, 0,
          0, std_radphi_*std_radphi_, 0,
          0, 0,std_radrd_*std_radrd_;
  S = S + R;
  MatrixXd Tc = MatrixXd(n_x_, n_z);
  Tc.fill(0.0);
  for (int i = 0; i < 2*n_aug_+1; i++) {  
    VectorXd z_diff = Zsig.col(i) - z_pred;
    while (z_diff(1) > M_PI)  z_diff(1) -= 2.*M_PI;
    while (z_diff(1) < -M_PI) z_diff(1) += 2.*M_PI;
    VectorXd x_diff = Xsig_pred_.col(i) - x_;
    while (x_diff(3) > M_PI)  x_diff(3) -= 2.*M_PI;
    while (x_diff(3) < -M_PI) x_diff(3) += 2.*M_PI;
    Tc = Tc + weights_(i) * x_diff * z_diff.transpose();
  }
  MatrixXd K = Tc * S.inverse();
  VectorXd z_diff = z - z_pred;
  while (z_diff(1) > M_PI)   z_diff(1) -= 2.*M_PI;
  while (z_diff(1) < -M_PI)  z_diff(1) += 2.*M_PI;
  radar_nis_ = z_diff.transpose() * S.inverse() * z_diff;
  x_ = x_ + K * z_diff;
  P_ = P_ - K * S * K.transpose();
}
void UKF :: UpdateRadar ( MeasurementPackage meas_package ) { int n_z = 3 ; MatrixXd Zsig = MatrixXd ( n_z , 2 * n_aug_ + 1 ) ; VectorXd z = VectorXd ( n_z ) ; z ( 0 ) = meas_package . raw_measurements_ ( 0 ) ; z ( 1 ) = meas_package . raw_measurements_ ( 1 ) ; z ( 2 ) = meas_package . raw_measurements_ ( 2 ) ; for ( int i = 0 ; i < 2 * n_aug_ + 1 ; i ++ ) { double px = Xsig_pred_ ( 0 , i ) ; double py = Xsig_pred_ ( 1 , i ) ; double v = Xsig_pred_ ( 2 , i ) ; double yaw = Xsig_pred_ ( 3 , i ) ; double v1 = cos ( yaw ) * v ; double v2 = sin ( yaw ) * v ; Zsig ( 0 , i ) = sqrt ( px * px + py * py ) ; Zsig ( 1 , i ) = atan2 ( py , px ) ; Zsig ( 2 , i ) = ( ( px * v1 ) + ( py * v2 ) ) / sqrt ( px * px + py * py ) ; } VectorXd z_pred = VectorXd ( n_z ) ; z_pred . fill ( 0.0 ) ; for ( int i = 0 ; i < 2 * n_aug_ + 1 ; i ++ ) { z_pred = z_pred + weights_ ( i ) * Zsig . col ( i ) ; } MatrixXd S = MatrixXd ( n_z , n_z ) ; S . fill ( 0.0 ) ; for ( int i = 0 ; i < 2 * n_aug_ + 1 ; i ++ ) { VectorXd z_diff = Zsig . col ( i ) - z_pred ; while ( z_diff ( 1 ) > M_PI ) z_diff ( 1 ) -= 2. * M_PI ; while ( z_diff ( 1 ) < - M_PI ) z_diff ( 1 ) += 2. * M_PI ; S = S + weights_ ( i ) * z_diff * z_diff . transpose ( ) ; } MatrixXd R = MatrixXd ( n_z , n_z ) ; R << std_radr_ * std_radr_ , 0 , 0 , 0 , std_radphi_ * std_radphi_ , 0 , 0 , 0 , std_radrd_ * std_radrd_ ; S = S + R ; MatrixXd Tc = MatrixXd ( n_x_ , n_z ) ; Tc . fill ( 0.0 ) ; for ( int i = 0 ; i < 2 * n_aug_ + 1 ; i ++ ) { VectorXd z_diff = Zsig . col ( i ) - z_pred ; while ( z_diff ( 1 ) > M_PI ) z_diff ( 1 ) -= 2. * M_PI ; while ( z_diff ( 1 ) < - M_PI ) z_diff ( 1 ) += 2. * M_PI ; VectorXd x_diff = Xsig_pred_ . col ( i ) - x_ ; while ( x_diff ( 3 ) > M_PI ) x_diff ( 3 ) -= 2. * M_PI ; while ( x_diff ( 3 ) < - M_PI ) x_diff ( 3 ) += 2. * M_PI ; Tc = Tc + weights_ ( i ) * x_diff * z_diff . transpose ( ) ; } MatrixXd K = Tc * S . inverse ( ) ; VectorXd z_diff = z - z_pred ; while ( z_diff ( 1 ) > M_PI ) z_diff ( 1 ) -= 2. * M_PI ; while ( z_diff ( 1 ) < - M_PI ) z_diff ( 1 ) += 2. * M_PI ; radar_nis_ = z_diff . transpose ( ) * S . inverse ( ) * z_diff ; x_ = x_ + K * z_diff ; P_ = P_ - K * S * K . transpose ( ) ; }
Updates the state and the state covariance matrix using a radar measurement.
Updates the state and the state covariance matrix using a radar measurement .
/**
  TODO:

  Complete this function! Use radar data to update the belief about the object's
  position. Modify the state vector, x_, and covariance, P_.

  You'll also need to calculate the radar NIS.
  */ // Convert sigma points into measurement space /// extract values /// measurement model: r, rho, rho_dot // Calculate mean predicted measurement // Calculate innovation covariance matrix S /// angle normalization // Add measurement noise covariance matrix //calculate cross correlation matrix //2n+1 simga points //angle normalization // state difference //angle normalization //Kalman gain K; //residual //angle normalization //update state mean and covariance matrix