-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle_filter.cpp
297 lines (249 loc) · 9.23 KB
/
particle_filter.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* particle_filter.cpp
*
* Created on: September 06, 2017
* Author: Tanuj Kush
*/
#include <random>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <math.h>
#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include "particle_filter.h"
using namespace std;
void ParticleFilter::init(double x, double y, double theta, double std[]) {
// Set the number of particles. Initialize all particles to first position (based on estimates of
// x, y, theta and their uncertainties from GPS) and all weights to 1.
// Add random Gaussian noise to each particle.
// Set up a random generator
default_random_engine gen;
// Set up a normal distribution for x, y and theta
normal_distribution<double> dist_x(x, std[0]);
normal_distribution<double> dist_y(y, std[1]);
normal_distribution<double> dist_theta(theta, std[2]);
// Number of particles in the filter
num_particles = 100;
// Add particles, initialized to initial GPS coordinates plus some noise
for (int i=0;i<num_particles;i++)
{
Particle p;
p.x = dist_x(gen);
p.y = dist_y(gen);
//p.weight = 1.;
p.theta = dist_theta(gen);
weights.push_back(1.);
//theta is measured positive CCW and is bound by [0,2*pi)
//if theta is out of this range, it must be wrapped back
wrap_theta(p.theta);
particles.push_back(p);
}
//initialization is done
is_initialized = true;
}
void ParticleFilter::prediction(double delta_t, double std_pos[], double velocity, double yaw_rate) {
// Predict the next location for each particle based on motion model and GPS sensor noise
// Setup a random generator
default_random_engine gen;
double x, y, theta;
double x_new, y_new, theta_new;
// update the positions and heading based on motion model + sensor noise
for (int i=0;i<num_particles;i++)
{
x = particles[i].x;
y = particles[i].y;
theta = particles[i].theta;
// EOM for almost zero yaw rate
if ( fabs(yaw_rate) < EPSILON )
{
x_new = x + ( velocity*delta_t )*cos( theta );
y_new = y + ( velocity*delta_t )*sin( theta );
theta_new = theta;
}
// EOM for non zero yaw rate
else
{
x_new = x + ( velocity/yaw_rate )*( sin( theta + yaw_rate*delta_t ) - sin( theta ) );
y_new = y + ( velocity/yaw_rate )*( -cos( theta + yaw_rate*delta_t ) + cos( theta ) );
theta_new = theta + yaw_rate*delta_t;
wrap_theta(theta_new);
}
// Generate Gaussian noise for GPS sensor noise
normal_distribution<double> dist_x(x_new, std_pos[0]);
normal_distribution<double> dist_y(y_new, std_pos[1]);
normal_distribution<double> dist_theta(theta_new, std_pos[2]);
// Set particle data
particles[i].x = dist_x(gen);
particles[i].y = dist_y(gen);
particles[i].theta = dist_theta(gen);
wrap_theta(particles[i].theta);
}
}
void ParticleFilter::dataAssociation(std::vector<LandmarkObs>& predicted, Map map_lm) {
// Find the predicted measurement that is closest to each observed measurement and assign the
// observed measurement to this particular landmark.
// loop over all the transformed observations
for (int i=0;i<predicted.size();i++)
{
double minDist = 1.e10;
double dist = 1.e10;
int minId = -1;
// loop over all the landmark information
for (int j=0;j<map_lm.landmark_list.size();j++)
{
dist = (map_lm.landmark_list[j].x_f - predicted[i].x)*(map_lm.landmark_list[j].x_f - predicted[i].x) \
+ (map_lm.landmark_list[j].y_f - predicted[i].y)*(map_lm.landmark_list[j].y_f - predicted[i].y);
// find minimum distance (no need for sqrt here, this speeds things up a bit!)
if ( dist < minDist )
{
minDist = dist;
minId = j;
}
}
//associate transformed observation with landmark id minId
predicted[i].id = map_lm.landmark_list[minId].id_i;
}
}
void ParticleFilter::updateWeights(double sensor_range, double std_landmark[],
std::vector<LandmarkObs> observations, Map map_landmarks) {
// Update the weights of each particle using a mult-variate Gaussian distribution. You can read
// more about this distribution here: https://en.wikipedia.org/wiki/Multivariate_normal_distribution
// NOTE: The observations are given in the VEHICLE'S coordinate system. Your particles are located
// according to the MAP'S coordinate system. You will need to transform between the two systems.
// Keep in mind that this transformation requires both rotation AND translation (but no scaling).
// The following is a good resource for the theory:
// https://www.willamette.edu/~gorr/classes/GeneralGraphics/Transforms/transforms2d.htm
// and the following is a good resource for the actual equation to implement (look at equation
// 3.33
// http://planning.cs.uiuc.edu/node99.html
double dist = 0.;
double weight_sum = 0.;
double exponent = 0.;
double divisor = 2 * M_PI * std_landmark[0] * std_landmark[1];
double x, y;
double mu_x, mu_y;
// loop over all particles
for (int i=0;i<num_particles;i++)
{
double weight_prod = 1.;
// create a vector of transformed obs
std::vector<LandmarkObs> transformed_obs;
for (int j=0;j<observations.size();j++)
{
LandmarkObs lm_pred;
// transform car's observations to particle observations
lm_pred.x = particles[i].x + ( observations[j].x * cos( particles[i].theta ) - observations[j].y * sin ( particles[i].theta ) );
lm_pred.y = particles[i].y + ( observations[j].x * sin( particles[i].theta ) + observations[j].y * cos ( particles[i].theta ) );
transformed_obs.push_back(lm_pred);
}
// if no measurement is in range, then don't update weights
// associate transformed observations with map landmarks
dataAssociation(transformed_obs, map_landmarks);
// update weights based on observations and associations
for (int j=0;j<transformed_obs.size();j++)
{
x = transformed_obs[j].x;
y = transformed_obs[j].y;
mu_x = map_landmarks.landmark_list[transformed_obs[j].id-1].x_f;
mu_y = map_landmarks.landmark_list[transformed_obs[j].id-1].y_f;
exponent = -( ( x - mu_x )*( x - mu_x ) / ( 2*std_landmark[0]*std_landmark[0] ) +
( y - mu_y )*( y - mu_y ) / ( 2*std_landmark[1]*std_landmark[1] ) );
// avoid division by zero
if ( fabs( divisor ) > EPSILON )
weight_prod *= exp( exponent ) / divisor;
else
//ignore zero variance
continue;
}
// update particle weight
particles[i].weight = weight_prod;
weights[i] = particles[i].weight;
// calculate weight sum
weight_sum += weights[i];
}
// normalize weights for resampling
// avoid division by zero
if ( weight_sum < EPSILON*EPSILON*EPSILON )
cout << "Sum of weights for resampling is almost zero! ("<<weight_sum<<")\n";
else
{
for (int i=0;i<num_particles;i++)
weights[i] /= weight_sum;
}
}
void ParticleFilter::resample() {
// Resample particles with replacement with probability proportional to their weight.
// NOTE: You may find std::discrete_distribution helpful here.
// http://en.cppreference.com/w/cpp/numeric/random/discrete_distribution
// new vector for particles
std::vector<Particle> resampled;
// random generator
std::default_random_engine gen;
// discrete distribution based on weights vector
std::discrete_distribution<> distribution( weights.begin(), weights.end() );
int sample_id = -1;
// pick particles based on weights with replacement
for (int i=0;i<num_particles;i++)
{
sample_id = distribution(gen);
resampled.push_back(particles[sample_id]);
}
// update particles vector with the sampled one
particles.clear();
particles = resampled;
}
Particle ParticleFilter::SetAssociations(Particle particle, std::vector<int> associations, std::vector<double> sense_x, std::vector<double> sense_y)
{
//particle: the particle to assign each listed association, and association's (x,y) world coordinates mapping to
// associations: The landmark id that goes along with each listed association
// sense_x: the associations x mapping already converted to world coordinates
// sense_y: the associations y mapping already converted to world coordinates
//Clear the previous associations
particle.associations.clear();
particle.sense_x.clear();
particle.sense_y.clear();
particle.associations= associations;
particle.sense_x = sense_x;
particle.sense_y = sense_y;
return particle;
}
string ParticleFilter::getAssociations(Particle best)
{
vector<int> v = best.associations;
stringstream ss;
copy( v.begin(), v.end(), ostream_iterator<int>(ss, " "));
string s = ss.str();
s = s.substr(0, s.length()-1); // get rid of the trailing space
return s;
}
string ParticleFilter::getSenseX(Particle best)
{
vector<double> v = best.sense_x;
stringstream ss;
copy( v.begin(), v.end(), ostream_iterator<float>(ss, " "));
string s = ss.str();
s = s.substr(0, s.length()-1); // get rid of the trailing space
return s;
}
string ParticleFilter::getSenseY(Particle best)
{
vector<double> v = best.sense_y;
stringstream ss;
copy( v.begin(), v.end(), ostream_iterator<float>(ss, " "));
string s = ss.str();
s = s.substr(0, s.length()-1); // get rid of the trailing space
return s;
}
void ParticleFilter::printDebugInfo(string method, Particle p)
{
cout << "In " << method <<"\n";
cout << "Particle " << ":\n";
cout << "X:\t " << p.x << "\n";
cout << "Y:\t " << p.y << "\n";
cout << "Th:\t " << p.theta << "\n";
cout << "Wt:\t " << p.weight << "\n";
cout << "***\n";
}