-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFusionEKF.cpp
139 lines (116 loc) · 3.45 KB
/
FusionEKF.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "FusionEKF.h"
#include <iostream>
#include "Eigen/Dense"
#include "tools.h"
using Eigen::MatrixXd;
using Eigen::VectorXd;
using std::cout;
using std::endl;
using std::vector;
/**
* Constructor.
*/
FusionEKF::FusionEKF() {
is_initialized_ = false;
previous_timestamp_ = 0;
// initializing matrices
R_laser_ = MatrixXd(2, 2);
R_radar_ = MatrixXd(3, 3);
H_laser_ = MatrixXd(2, 4);
Hj_ = MatrixXd(3, 4);
F_ = MatrixXd(4, 4);
//measurement covariance matrix - laser
R_laser_ << 0.0225, 0,
0, 0.0225;
//measurement covariance matrix - radar
R_radar_ << 0.09, 0, 0,
0, 0.0009, 0,
0, 0, 0.09;
H_laser_ << 1, 0, 0, 0,
0, 1, 0, 0;
//the initial transition matrix F_
F_ << 1, 0, 1, 0,
0, 1, 0, 1,
0, 0, 1, 0,
0, 0, 0, 1;
// set the acceleration noise components
noise_ax_ = 9;
noise_ay_ = 9;
}
/**
* Destructor.
*/
FusionEKF::~FusionEKF() {}
void FusionEKF::ProcessMeasurement(const MeasurementPackage &measurement_pack) {
/**
* Initialization
*/
if (!is_initialized_) {
cout << "First measurement detected! Initializing EFK." << endl;
// Initialize the state ekf_.x_.
ekf_.x_ = VectorXd(4);
ekf_.x_ << 1, 1, 1, 1;
// Initialize 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;
// Initialize the state transition matrix
ekf_.F_ = F_;
if (measurement_pack.sensor_type_ == MeasurementPackage::RADAR) {
// convert Radar measurements from polar to catersian using trigonometry.
float rho = measurement_pack.raw_measurements_[0];
float phi = measurement_pack.raw_measurements_[1];
float rho_dot = measurement_pack.raw_measurements_[2];
ekf_.x_ << rho * cos(phi),
rho * sin(phi),
rho_dot * cos(phi),
rho_dot * sin(phi);
}
else if (measurement_pack.sensor_type_ == MeasurementPackage::LASER) {
ekf_.x_ << measurement_pack.raw_measurements_[0],
measurement_pack.raw_measurements_[1],
0,
0;
}
// done initializing, no need to predict or update
is_initialized_ = true;
previous_timestamp_ = measurement_pack.timestamp_;
return;
}
/**
* Prediction
*/
// compute the time elapsed between the current and previous measurements
// dt - expressed in seconds
float dt = (measurement_pack.timestamp_ - previous_timestamp_) / 1000000.0;
previous_timestamp_ = measurement_pack.timestamp_;
float dt_2 = dt * dt;
float dt_3 = dt * dt * dt;
float dt_4 = dt * dt * dt * dt;
// Modify the F matrix with computed time
ekf_.F_(0, 2) = dt;
ekf_.F_(1, 3) = dt;
// Set the process covariance matrix Q
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_;
ekf_.Predict();
/**
* Update
*/
if (measurement_pack.sensor_type_ == MeasurementPackage::RADAR) {
ekf_.R_ = R_radar_;
ekf_.UpdateEKF(measurement_pack.raw_measurements_);
} else {
ekf_.H_ = H_laser_;
ekf_.R_ = R_laser_;
ekf_.Update(measurement_pack.raw_measurements_);
}
// print the output
cout << "x_ = " << ekf_.x_ << endl;
cout << "P_ = " << ekf_.P_ << endl;
}