-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
202 lines (167 loc) · 6.05 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
#include <uWS/uWS.h>
#include <iostream>
#include <fstream>
#include "json.hpp"
#include <math.h>
#include "FusionEKF.h"
#include "tools.h"
using namespace std;
// for convenience
using json = nlohmann::json;
// Checks if the SocketIO event has JSON data.
// If there is data the JSON object in string format will be returned,
// else the empty string "" will be returned.
std::string hasData(std::string s) {
auto found_null = s.find("null");
auto b1 = s.find_first_of("[");
auto b2 = s.find_first_of("]");
if (found_null != std::string::npos) {
return "";
}
else if (b1 != std::string::npos && b2 != std::string::npos) {
return s.substr(b1, b2 - b1 + 1);
}
return "";
}
int main()
{
uWS::Hub h;
// Create a Kalman Filter instance
FusionEKF fusionEKF;
// used to compute the RMSE later
Tools tools;
vector<VectorXd> estimations;
vector<VectorXd> ground_truth;
ofstream myfile;
myfile.open("obj_pose-laser-radar-ekf-output.txt");
h.onMessage([&fusionEKF,&tools,&estimations,&ground_truth, &myfile](uWS::WebSocket<uWS::SERVER> ws, char *data, size_t length, uWS::OpCode opCode) {
// "42" at the start of the message means there's a websocket message event.
// The 4 signifies a websocket message
// The 2 signifies a websocket event
if (length && length > 2 && data[0] == '4' && data[1] == '2')
{
auto s = hasData(std::string(data));
if (s != "") {
auto j = json::parse(s);
std::string event = j[0].get<std::string>();
if (event == "telemetry") {
// j[1] is the data JSON object
string sensor_measurment = j[1]["sensor_measurement"];
MeasurementPackage meas_package;
istringstream iss(sensor_measurment);
long long timestamp;
// reads first element from the current line
string sensor_type;
iss >> sensor_type;
if (sensor_type.compare("L") == 0) {
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;
} else if (sensor_type.compare("R") == 0) {
meas_package.sensor_type_ = MeasurementPackage::RADAR;
meas_package.raw_measurements_ = VectorXd(3);
float ro;
float theta;
float ro_dot;
iss >> ro;
iss >> theta;
iss >> ro_dot;
meas_package.raw_measurements_ << ro,theta, ro_dot;
iss >> timestamp;
meas_package.timestamp_ = timestamp;
}
float x_gt;
float y_gt;
float vx_gt;
float vy_gt;
iss >> x_gt;
iss >> y_gt;
iss >> vx_gt;
iss >> vy_gt;
VectorXd gt_values(4);
gt_values(0) = x_gt;
gt_values(1) = y_gt;
gt_values(2) = vx_gt;
gt_values(3) = vy_gt;
ground_truth.push_back(gt_values);
//Call ProcessMeasurment(meas_package) for Kalman filter
fusionEKF.ProcessMeasurement(meas_package);
//Push the current estimated x,y positon from the Kalman filter's state vector
VectorXd estimate(4);
double p_x = fusionEKF.ekf_.x_(0);
double p_y = fusionEKF.ekf_.x_(1);
double v1 = fusionEKF.ekf_.x_(2);
double v2 = fusionEKF.ekf_.x_(3);
estimate(0) = p_x;
estimate(1) = p_y;
estimate(2) = v1;
estimate(3) = v2;
// Write to txt file for visualisation
myfile << p_x << '\t' << p_y << '\t' << v1 << '\t' << v2 << '\t';
if (meas_package.sensor_type_ == MeasurementPackage::LASER) {
myfile << meas_package.raw_measurements_[0] << '\t' << meas_package.raw_measurements_[1] << '\t';
} else if (meas_package.sensor_type_ == MeasurementPackage::RADAR) {
double rho = meas_package.raw_measurements_[0];
double phi = meas_package.raw_measurements_[1];
myfile << rho*cos(phi) << '\t' << rho*sin(phi) << '\t';
}
myfile << gt_values(0) << '\t' << gt_values(1) << '\t' << gt_values(2) << '\t' << gt_values(3) << endl;
estimations.push_back(estimate);
VectorXd RMSE = tools.CalculateRMSE(estimations, ground_truth);
json msgJson;
msgJson["estimate_x"] = p_x;
msgJson["estimate_y"] = p_y;
msgJson["rmse_x"] = RMSE(0);
msgJson["rmse_y"] = RMSE(1);
msgJson["rmse_vx"] = RMSE(2);
msgJson["rmse_vy"] = RMSE(3);
auto msg = "42[\"estimate_marker\"," + msgJson.dump() + "]";
// std::cout << msg << std::endl;
ws.send(msg.data(), msg.length(), uWS::OpCode::TEXT);
}
} else {
std::string msg = "42[\"manual\",{}]";
ws.send(msg.data(), msg.length(), uWS::OpCode::TEXT);
}
}
});
// We don't need this since we're not using HTTP but if it's removed the program
// doesn't compile :-(
h.onHttpRequest([](uWS::HttpResponse *res, uWS::HttpRequest req, char *data, size_t, size_t) {
const std::string s = "<h1>Hello world!</h1>";
if (req.getUrl().valueLength == 1)
{
res->end(s.data(), s.length());
}
else
{
// i guess this should be done more gracefully?
res->end(nullptr, 0);
}
});
h.onConnection([&h](uWS::WebSocket<uWS::SERVER> ws, uWS::HttpRequest req) {
std::cout << "Connected!!!" << std::endl;
});
h.onDisconnection([&h](uWS::WebSocket<uWS::SERVER> ws, int code, char *message, size_t length) {
ws.close();
std::cout << "Disconnected" << std::endl;
});
int port = 4567;
if (h.listen(port))
{
std::cout << "Listening to port " << port << std::endl;
}
else
{
std::cerr << "Failed to listen to port" << std::endl;
return -1;
}
h.run();
myfile.close();
}