46 double alpha_slow,
double alpha_fast,
56 pf = calloc(1,
sizeof(
pf_t));
74 for (j = 0; j < 2; j++)
79 set->samples = calloc(max_samples,
sizeof(
pf_sample_t));
81 for (i = 0; i <
set->sample_count; i++)
83 sample =
set->samples + i;
84 sample->
pose.
v[0] = 0.0;
85 sample->
pose.
v[1] = 0.0;
86 sample->
pose.
v[2] = 0.0;
87 sample->
weight = 1.0 / max_samples;
93 set->cluster_count = 0;
94 set->cluster_max_count = max_samples;
118 for (i = 0; i < 2; i++)
147 for (i = 0; i <
set->sample_count; i++)
149 sample =
set->samples + i;
186 for (i = 0; i <
set->sample_count; i++)
188 sample =
set->samples + i;
190 sample->
pose = (*init_fn) (init_data);
222 double mean_x = 0, mean_y = 0;
224 for (i = 0; i <
set->sample_count; i++){
225 sample =
set->samples + i;
227 mean_x += sample->
pose.
v[0];
228 mean_y += sample->
pose.
v[1];
230 mean_x /=
set->sample_count;
231 mean_y /=
set->sample_count;
233 for (i = 0; i <
set->sample_count; i++){
234 sample =
set->samples + i;
254 (*action_fn) (action_data,
set);
272 total = (*sensor_fn) (sensor_data,
set);
278 for (i = 0; i <
set->sample_count; i++)
280 sample =
set->samples + i;
285 w_avg /=
set->sample_count;
300 for (i = 0; i <
set->sample_count; i++)
302 sample =
set->samples + i;
303 sample->
weight = 1.0 /
set->sample_count;
332 c = (
double*)malloc(
sizeof(
double)*(set_a->
sample_count+1));
363 if(drand48() < w_diff)
396 if((c[i] <= r) && (r < c[i+1]))
399 assert(i<set_a->sample_count);
403 assert(sample_a->
weight > 0);
410 total += sample_b->
weight;
430 sample_b->
weight /= total;
457 b = 2 / (9 * ((double) k - 1));
458 c = sqrt(2 / (9 * ((
double) k - 1))) * pf->
pop_z;
461 n = (int) ceil((k - 1) / (2 * pf->
pop_err) * x * x * x);
463 if (n < pf->min_samples)
480 double m[4], c[2][2];
488 set->cluster_count = 0;
490 for (i = 0; i <
set->cluster_max_count; i++)
492 cluster =
set->clusters + i;
498 for (j = 0; j < 4; j++)
500 for (j = 0; j < 2; j++)
501 for (k = 0; k < 2; k++)
502 cluster->
c[j][k] = 0.0;
510 for (j = 0; j < 4; j++)
512 for (j = 0; j < 2; j++)
513 for (k = 0; k < 2; k++)
517 for (i = 0; i <
set->sample_count; i++)
519 sample =
set->samples + i;
526 if (cidx >=
set->cluster_max_count)
528 if (cidx + 1 >
set->cluster_count)
529 set->cluster_count = cidx + 1;
531 cluster =
set->clusters + cidx;
542 cluster->
m[2] += sample->
weight * cos(sample->
pose.
v[2]);
543 cluster->
m[3] += sample->
weight * sin(sample->
pose.
v[2]);
551 for (j = 0; j < 2; j++)
552 for (k = 0; k < 2; k++)
560 for (i = 0; i <
set->cluster_count; i++)
562 cluster =
set->clusters + i;
566 cluster->
mean.
v[2] = atan2(cluster->
m[3], cluster->
m[2]);
571 for (j = 0; j < 2; j++)
572 for (k = 0; k < 2; k++)
573 cluster->
cov.
m[j][k] = cluster->
c[j][k] / cluster->
weight -
578 cluster->
cov.
m[2][2] = -2 * log(sqrt(cluster->
m[2] * cluster->
m[2] +
579 cluster->
m[3] * cluster->
m[3]));
587 set->mean.v[0] = m[0] / weight;
588 set->mean.v[1] = m[1] / weight;
589 set->mean.v[2] = atan2(m[3], m[2]);
592 for (j = 0; j < 2; j++)
593 for (k = 0; k < 2; k++)
594 set->cov.m[j][k] = c[j][k] / weight -
set->mean.v[j] *
set->mean.v[k];
598 set->cov.m[2][2] = -2 * log(sqrt(m[2] * m[2] + m[3] * m[3]));
608 double mn, mx, my, mrr;
619 for (i = 0; i <
set->sample_count; i++)
621 sample =
set->samples + i;
630 mean->
v[0] = mx / mn;
631 mean->
v[1] = my / mn;
634 *var = mrr / mn - (mx * mx / (mn * mn) + my * my / (mn * mn));
649 if (clabel >=
set->cluster_count)
651 cluster =
set->clusters + clabel;
653 *weight = cluster->
weight;
654 *mean = cluster->
mean;
pf_init_model_fn_t random_pose_fn
double(* pf_sensor_model_fn_t)(void *sensor_data, struct _pf_sample_set_t *set)
static int pf_resample_limit(pf_t *pf, int k)
void pf_update_sensor(pf_t *pf, pf_sensor_model_fn_t sensor_fn, void *sensor_data)
void pf_get_cep_stats(pf_t *pf, pf_vector_t *mean, double *var)
void pf_kdtree_clear(pf_kdtree_t *self)
void pf_kdtree_insert(pf_kdtree_t *self, pf_vector_t pose, double value)
pf_vector_t(* pf_init_model_fn_t)(void *init_data)
void pf_init_converged(pf_t *pf)
pf_vector_t pf_vector_zero()
void pf_update_resample(pf_t *pf)
pf_t * pf_alloc(int min_samples, int max_samples, double alpha_slow, double alpha_fast, pf_init_model_fn_t random_pose_fn, void *random_pose_data)
void pf_init_model(pf_t *pf, pf_init_model_fn_t init_fn, void *init_data)
void pf_kdtree_free(pf_kdtree_t *self)
ROSCPP_DECL void set(const std::string &key, const XmlRpc::XmlRpcValue &v)
void pf_pdf_gaussian_free(pf_pdf_gaussian_t *pdf)
void pf_update_action(pf_t *pf, pf_action_model_fn_t action_fn, void *action_data)
int pf_update_converged(pf_t *pf)
pf_matrix_t pf_matrix_zero()
TFSIMD_FORCE_INLINE const tfScalar & x() const
pf_pdf_gaussian_t * pf_pdf_gaussian_alloc(pf_vector_t x, pf_matrix_t cx)
void pf_cluster_stats(pf_t *pf, pf_sample_set_t *set)
void(* pf_action_model_fn_t)(void *action_data, struct _pf_sample_set_t *set)
int pf_get_cluster_stats(pf_t *pf, int clabel, double *weight, pf_vector_t *mean, pf_matrix_t *cov)
void pf_init(pf_t *pf, pf_vector_t mean, pf_matrix_t cov)
pf_kdtree_t * pf_kdtree_alloc(int max_size)
int pf_kdtree_get_cluster(pf_kdtree_t *self, pf_vector_t pose)
void pf_kdtree_cluster(pf_kdtree_t *self)
pf_vector_t pf_pdf_gaussian_sample(pf_pdf_gaussian_t *pdf)