-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmain.cpp
executable file
·237 lines (190 loc) · 6.82 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include "Eigen/Dense"
#include "ukf.h"
#include "ground_truth_package.h"
#include "measurement_package.h"
using namespace std;
using Eigen::MatrixXd;
using Eigen::VectorXd;
using std::vector;
void check_arguments(int argc, char* argv[]) {
string usage_instructions = "Usage instructions: ";
usage_instructions += argv[0];
usage_instructions += " path/to/input.txt output.txt";
bool has_valid_args = false;
// make sure the user has provided input and output files
if (argc == 1) {
cerr << usage_instructions << endl;
} else if (argc == 2) {
cerr << "Please include an output file.\n" << usage_instructions << endl;
} else if (argc == 3) {
has_valid_args = true;
} else if (argc > 3) {
cerr << "Too many arguments.\n" << usage_instructions << endl;
}
if (!has_valid_args) {
exit(EXIT_FAILURE);
}
}
void check_files(ifstream& in_file, string& in_name,
ofstream& out_file, string& out_name) {
if (!in_file.is_open()) {
cerr << "Cannot open input file: " << in_name << endl;
exit(EXIT_FAILURE);
}
if (!out_file.is_open()) {
cerr << "Cannot open output file: " << out_name << endl;
exit(EXIT_FAILURE);
}
}
int main(int argc, char* argv[]) {
check_arguments(argc, argv);
string in_file_name_ = argv[1];
ifstream in_file_(in_file_name_.c_str(), ifstream::in);
string out_file_name_ = argv[2];
ofstream out_file_(out_file_name_.c_str(), ofstream::out);
check_files(in_file_, in_file_name_, out_file_, out_file_name_);
/**********************************************
* Set Measurements *
**********************************************/
vector<MeasurementPackage> measurement_pack_list;
vector<GroundTruthPackage> gt_pack_list;
string line;
// prep the measurement packages (each line represents a measurement at a
// timestamp)
while (getline(in_file_, line)) {
string sensor_type;
MeasurementPackage meas_package;
GroundTruthPackage gt_package;
istringstream iss(line);
long long timestamp;
// reads first element from the current line
iss >> sensor_type;
if (sensor_type.compare("L") == 0) {
// laser measurement
// read measurements at this timestamp
meas_package.sensor_type_ = MeasurementPackage::LASER;
meas_package.raw_measurements_ = VectorXd(2);
float px;
float py;
iss >> px;
iss >> py;
meas_package.raw_measurements_ << px, py;
iss >> timestamp;
meas_package.timestamp_ = timestamp;
measurement_pack_list.push_back(meas_package);
} else if (sensor_type.compare("R") == 0) {
// radar measurement
// read measurements at this timestamp
meas_package.sensor_type_ = MeasurementPackage::RADAR;
meas_package.raw_measurements_ = VectorXd(3);
float ro;
float phi;
float ro_dot;
iss >> ro;
iss >> phi;
iss >> ro_dot;
meas_package.raw_measurements_ << ro, phi, ro_dot;
iss >> timestamp;
meas_package.timestamp_ = timestamp;
measurement_pack_list.push_back(meas_package);
}
// read ground truth data to compare later
float x_gt;
float y_gt;
float vx_gt;
float vy_gt;
iss >> x_gt;
iss >> y_gt;
iss >> vx_gt;
iss >> vy_gt;
gt_package.gt_values_ = VectorXd(4);
gt_package.gt_values_ << x_gt, y_gt, vx_gt, vy_gt;
gt_pack_list.push_back(gt_package);
}
// Create a UKF instance
UKF ukf;
// used to compute the RMSE later
vector<VectorXd> estimations;
vector<VectorXd> ground_truth;
// start filtering from the second frame (the speed is unknown in the first
// frame)
size_t number_of_measurements = measurement_pack_list.size();
// column names for output file
out_file_ << "time_stamp" << "\t";
out_file_ << "px_state" << "\t";
out_file_ << "py_state" << "\t";
out_file_ << "v_state" << "\t";
out_file_ << "yaw_angle_state" << "\t";
out_file_ << "yaw_rate_state" << "\t";
out_file_ << "sensor_type" << "\t";
out_file_ << "NIS" << "\t";
out_file_ << "px_measured" << "\t";
out_file_ << "py_measured" << "\t";
out_file_ << "px_ground_truth" << "\t";
out_file_ << "py_ground_truth" << "\t";
out_file_ << "vx_ground_truth" << "\t";
out_file_ << "vy_ground_truth" << "\n";
for (size_t k = 0; k < number_of_measurements; ++k) {
// Call the UKF-based fusion
ukf.ProcessMeasurement(measurement_pack_list[k]);
// timestamp
out_file_ << measurement_pack_list[k].timestamp_ << "\t"; // pos1 - est
// output the state vector
out_file_ << ukf.x_(0) << "\t"; // pos1 - est
out_file_ << ukf.x_(1) << "\t"; // pos2 - est
out_file_ << ukf.x_(2) << "\t"; // vel_abs -est
out_file_ << ukf.x_(3) << "\t"; // yaw_angle -est
out_file_ << ukf.x_(4) << "\t"; // yaw_rate -est
// output lidar and radar specific data
if (measurement_pack_list[k].sensor_type_ == MeasurementPackage::LASER) {
// sensor type
out_file_ << "lidar" << "\t";
// NIS value
out_file_ << ukf.NIS_laser_ << "\t";
// output the lidar sensor measurement px and py
out_file_ << measurement_pack_list[k].raw_measurements_(0) << "\t";
out_file_ << measurement_pack_list[k].raw_measurements_(1) << "\t";
} else if (measurement_pack_list[k].sensor_type_ == MeasurementPackage::RADAR) {
// sensor type
out_file_ << "radar" << "\t";
// NIS value
out_file_ << ukf.NIS_radar_ << "\t";
// output radar measurement in cartesian coordinates
float ro = measurement_pack_list[k].raw_measurements_(0);
float phi = measurement_pack_list[k].raw_measurements_(1);
out_file_ << ro * cos(phi) << "\t"; // px measurement
out_file_ << ro * sin(phi) << "\t"; // py measurement
}
// output the ground truth
out_file_ << gt_pack_list[k].gt_values_(0) << "\t";
out_file_ << gt_pack_list[k].gt_values_(1) << "\t";
out_file_ << gt_pack_list[k].gt_values_(2) << "\t";
out_file_ << gt_pack_list[k].gt_values_(3) << "\n";
// convert ukf x vector to cartesian to compare to ground truth
VectorXd ukf_x_cartesian_ = VectorXd(4);
float x_estimate_ = ukf.x_(0);
float y_estimate_ = ukf.x_(1);
float vx_estimate_ = ukf.x_(2) * cos(ukf.x_(3));
float vy_estimate_ = ukf.x_(2) * sin(ukf.x_(3));
ukf_x_cartesian_ << x_estimate_, y_estimate_, vx_estimate_, vy_estimate_;
estimations.push_back(ukf_x_cartesian_);
ground_truth.push_back(gt_pack_list[k].gt_values_);
}
// compute the accuracy (RMSE)
Tools tools;
cout << "RMSE" << endl << tools.CalculateRMSE(estimations, ground_truth) << endl;
// close files
if (out_file_.is_open()) {
out_file_.close();
}
if (in_file_.is_open()) {
in_file_.close();
}
cout << "Done!" << endl;
return 0;
}