Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
pf.hpp
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2000 Brian Gerkey & Kasper Stoy
4  * gerkey@usc.edu kaspers@robotics.usc.edu
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 /**************************************************************************
22  * Desc: Simple particle filter for localization.
23  * Author: Andrew Howard
24  * Date: 10 Dec 2002
25  * CVS: $Id: pf.h 3293 2005-11-19 08:37:45Z gerkey $
26  *************************************************************************/
27 
28 #ifndef NAV2_AMCL__PF__PF_HPP_
29 #define NAV2_AMCL__PF__PF_HPP_
30 
31 #include <stdint.h>
32 
33 #include "nav2_amcl/pf/pf_vector.hpp"
34 #include "nav2_amcl/pf/pf_kdtree.hpp"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 // Forward declarations
41 struct _pf_t;
42 struct _rtk_fig_t;
43 struct _pf_sample_set_t;
44 
45 // Function prototype for the initialization model; generates a sample pose from
46 // an appropriate distribution.
47 typedef pf_vector_t (* pf_init_model_fn_t) (void * init_data);
48 
49 // Function prototype for the action model; generates a sample pose from
50 // an appropriate distribution
51 typedef void (* pf_action_model_fn_t) (
52  void * action_data,
53  struct _pf_sample_set_t * set);
54 
55 // Function prototype for the sensor model; determines the probability
56 // for the given set of sample poses.
57 typedef double (* pf_sensor_model_fn_t) (
58  void * sensor_data,
59  struct _pf_sample_set_t * set);
60 
61 
62 // Information for a single sample
63 typedef struct
64 {
65  // Pose represented by this sample
66  pf_vector_t pose;
67 
68  // Weight for this pose
69  double weight;
70 } pf_sample_t;
71 
72 
73 // Information for a cluster of samples
74 typedef struct
75 {
76  // Number of samples
77  int count;
78 
79  // Total weight of samples in this cluster
80  double weight;
81 
82  // Cluster statistics
83  pf_vector_t mean;
84  pf_matrix_t cov;
85 
86  // Workspace
87  double m[4], c[2][2];
88 } pf_cluster_t;
89 
90 
91 // Information for a set of samples
92 typedef struct _pf_sample_set_t
93 {
94  // The samples
95  int sample_count;
96  pf_sample_t * samples;
97 
98  // A kdtree encoding the histogram
99  pf_kdtree_t * kdtree;
100 
101  // Clusters
102  int cluster_count, cluster_max_count;
103  pf_cluster_t * clusters;
104 
105  // Filter statistics
106  pf_vector_t mean;
107  pf_matrix_t cov;
108  int converged;
110 
111 
112 // Information for an entire filter
113 typedef struct _pf_t
114 {
115  // This min and max number of samples
116  int min_samples, max_samples;
117 
118  // Population size parameters
119  double pop_err, pop_z;
120 
121  // The sample sets. We keep two sets and use [current_set]
122  // to identify the active set.
123  int current_set;
124  pf_sample_set_t sets[2];
125 
126  // Running averages, slow and fast, of likelihood
127  double w_slow, w_fast;
128 
129  // Decay rates for running averages
130  double alpha_slow, alpha_fast;
131 
132  // Function used to draw random pose samples
133  pf_init_model_fn_t random_pose_fn;
134 
135  double dist_threshold; // distance threshold in each axis over which the pf is considered to not
136  // be converged
137  int converged;
138 } pf_t;
139 
140 
141 // Create a new filter
142 pf_t * pf_alloc(
143  int min_samples, int max_samples,
144  double alpha_slow, double alpha_fast,
145  pf_init_model_fn_t random_pose_fn);
146 
147 // Free an existing filter
148 void pf_free(pf_t * pf);
149 
150 // Initialize the filter using a gaussian
151 void pf_init(pf_t * pf, pf_vector_t mean, pf_matrix_t cov);
152 
153 // Initialize the filter using some model
154 void pf_init_model(pf_t * pf, pf_init_model_fn_t init_fn, void * init_data);
155 
156 // Update the filter with some new action
157 // void pf_update_action(pf_t * pf, pf_action_model_fn_t action_fn, void * action_data);
158 
159 // Update the filter with some new sensor observation
160 void pf_update_sensor(pf_t * pf, pf_sensor_model_fn_t sensor_fn, void * sensor_data);
161 
162 // Resample the distribution
163 void pf_update_resample(pf_t * pf, void * random_pose_data);
164 
165 // Compute the CEP statistics (mean and variance).
166 // void pf_get_cep_stats(pf_t * pf, pf_vector_t * mean, double * var);
167 
168 // Compute the statistics for a particular cluster. Returns 0 if
169 // there is no such cluster.
170 int pf_get_cluster_stats(
171  pf_t * pf, int cluster, double * weight,
172  pf_vector_t * mean, pf_matrix_t * cov);
173 
174 // Re-compute the cluster statistics for a sample set
175 void pf_cluster_stats(pf_t * pf, pf_sample_set_t * set);
176 
177 
178 // Display the sample set
179 void pf_draw_samples(pf_t * pf, struct _rtk_fig_t * fig, int max_samples);
180 
181 // Draw the histogram (kdtree)
182 void pf_draw_hist(pf_t * pf, struct _rtk_fig_t * fig);
183 
184 // Draw the CEP statistics
185 // void pf_draw_cep_stats(pf_t * pf, struct _rtk_fig_t * fig);
186 
187 // Draw the cluster statistics
188 void pf_draw_cluster_stats(pf_t * pf, struct _rtk_fig_t * fig);
189 
190 // calculate if the particle filter has converged -
191 // and sets the converged flag in the current set and the pf
192 int pf_update_converged(pf_t * pf);
193 
194 // sets the current set and pf converged values to zero
195 void pf_init_converged(pf_t * pf);
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 
202 #endif // NAV2_AMCL__PF__PF_HPP_
Definition: pf.hpp:114