Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
pf_draw.c
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: Particle filter; drawing routines
23  * Author: Andrew Howard
24  * Date: 10 Dec 2002
25  * CVS: $Id: pf_draw.c 7057 2008-10-02 00:44:06Z gbiggs $
26  *************************************************************************/
27 
28 #pragma GCC diagnostic ignored "-Wpedantic"
29 #ifdef INCLUDE_RTKGUI
30 
31 #include <assert.h>
32 #include <math.h>
33 #include <stdlib.h>
34 
35 
36 #include <rtk.h>
37 
38 #include "nav2_amcl/pf/pf.hpp"
39 #include "nav2_amcl/pf/pf_pdf.hpp"
40 #include "nav2_amcl/pf/pf_kdtree.hpp"
41 
42 // Draw the statistics
43 void pf_draw_statistics(pf_t * pf, rtk_fig_t * fig);
44 
45 
46 // Draw the sample set
47 void pf_draw_samples(pf_t * pf, rtk_fig_t * fig, int max_samples)
48 {
49  int i;
50  double px, py, pa;
51  pf_sample_set_t * set;
52  pf_sample_t * sample;
53 
54  set = pf->sets + pf->current_set;
55  max_samples = MIN(max_samples, set->sample_count);
56 
57  for (i = 0; i < max_samples; i++) {
58  sample = set->samples + i;
59 
60  px = sample->pose.v[0];
61  py = sample->pose.v[1];
62  pa = sample->pose.v[2];
63 
64  // printf("%f %f\n", px, py);
65 
66  rtk_fig_point(fig, px, py);
67  rtk_fig_arrow(fig, px, py, pa, 0.1, 0.02);
68  // rtk_fig_rectangle(fig, px, py, 0, 0.1, 0.1, 0);
69  }
70 }
71 
72 
73 // Draw the histogram (kd tree)
74 void pf_draw_hist(pf_t * pf, rtk_fig_t * fig)
75 {
76  pf_sample_set_t * set;
77 
78  set = pf->sets + pf->current_set;
79 
80  rtk_fig_color(fig, 0.0, 0.0, 1.0);
81  pf_kdtree_draw(set->kdtree, fig);
82 }
83 
84 
85 // Draw the CEP statistics
86 // void pf_draw_cep_stats(pf_t * pf, rtk_fig_t * fig)
87 // {
88 // pf_vector_t mean;
89 // double var;
90 
91 // pf_get_cep_stats(pf, &mean, &var);
92 // var = sqrt(var);
93 
94 // rtk_fig_color(fig, 0, 0, 1);
95 // rtk_fig_ellipse(fig, mean.v[0], mean.v[1], mean.v[2], 3 * var, 3 * var, 0);
96 // }
97 
98 // Draw the cluster statistics
99 void pf_draw_cluster_stats(pf_t * pf, rtk_fig_t * fig)
100 {
101  int i;
102  pf_cluster_t * cluster;
103  pf_sample_set_t * set;
104  pf_vector_t mean;
105  pf_matrix_t cov;
106  pf_matrix_t r, d;
107  double weight, o, d1, d2;
108 
109  set = pf->sets + pf->current_set;
110 
111  for (i = 0; i < set->cluster_count; i++) {
112  cluster = set->clusters + i;
113 
114  weight = cluster->weight;
115  mean = cluster->mean;
116  cov = cluster->cov;
117 
118  // Compute unitary representation S = R D R^T
119  pf_matrix_unitary(&r, &d, cov);
120 
121  /* Debugging
122  printf("mean = \n");
123  pf_vector_fprintf(mean, stdout, "%e");
124  printf("cov = \n");
125  pf_matrix_fprintf(cov, stdout, "%e");
126  printf("r = \n");
127  pf_matrix_fprintf(r, stdout, "%e");
128  printf("d = \n");
129  pf_matrix_fprintf(d, stdout, "%e");
130  */
131 
132  // Compute the orientation of the error ellipse (first eigenvector)
133  o = atan2(r.m[1][0], r.m[0][0]);
134  d1 = 6 * sqrt(d.m[0][0]);
135  d2 = 6 * sqrt(d.m[1][1]);
136 
137  if (d1 > 1e-3 && d2 > 1e-3) {
138  // Draw the error ellipse
139  rtk_fig_ellipse(fig, mean.v[0], mean.v[1], o, d1, d2, 0);
140  rtk_fig_line_ex(fig, mean.v[0], mean.v[1], o, d1);
141  rtk_fig_line_ex(fig, mean.v[0], mean.v[1], o + M_PI / 2, d2);
142  }
143 
144  // Draw a direction indicator
145  rtk_fig_arrow(fig, mean.v[0], mean.v[1], mean.v[2], 0.50, 0.10);
146  rtk_fig_arrow(fig, mean.v[0], mean.v[1], mean.v[2] + 3 * sqrt(cov.m[2][2]), 0.50, 0.10);
147  rtk_fig_arrow(fig, mean.v[0], mean.v[1], mean.v[2] - 3 * sqrt(cov.m[2][2]), 0.50, 0.10);
148  }
149 }
150 #endif
Definition: pf.hpp:112