Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
differential_motion_model.cpp
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 #include <algorithm>
23 #include <cmath>
24 
25 #include "nav2_amcl/angleutils.hpp"
26 #include "nav2_amcl/motion_model/differential_motion_model.hpp"
27 #include "nav2_amcl/pf/pf_pdf.hpp"
28 
29 namespace nav2_amcl
30 {
31 
32 void
34  double alpha1, double alpha2, double alpha3, double alpha4,
35  double alpha5)
36 {
37  alpha1_ = alpha1;
38  alpha2_ = alpha2;
39  alpha3_ = alpha3;
40  alpha4_ = alpha4;
41  alpha5_ = alpha5;
42 }
43 
44 void
46  pf_t * pf, const pf_vector_t & pose,
47  const pf_vector_t & delta)
48 {
49  // Compute the new sample poses
50  pf_sample_set_t * set;
51 
52  set = pf->sets + pf->current_set;
53  pf_vector_t old_pose = pf_vector_sub(pose, delta);
54 
55  // Implement sample_motion_odometry (Prob Rob p 136)
56  double delta_rot1, delta_trans, delta_rot2;
57  double delta_rot1_hat, delta_trans_hat, delta_rot2_hat;
58  double delta_rot1_noise, delta_rot2_noise;
59 
60  // Avoid computing a bearing from two poses that are extremely near each
61  // other (happens on in-place rotation).
62  if (sqrt(
63  delta.v[1] * delta.v[1] +
64  delta.v[0] * delta.v[0]) < 0.01)
65  {
66  delta_rot1 = 0.0;
67  } else {
68  delta_rot1 = angleutils::angle_diff(
69  atan2(delta.v[1], delta.v[0]),
70  old_pose.v[2]);
71  }
72  delta_trans = sqrt(
73  delta.v[0] * delta.v[0] +
74  delta.v[1] * delta.v[1]);
75  delta_rot2 = angleutils::angle_diff(delta.v[2], delta_rot1);
76 
77  // We want to treat backward and forward motion symmetrically for the
78  // noise model to be applied below. The standard model seems to assume
79  // forward motion.
80  delta_rot1_noise = std::min(
81  fabs(angleutils::angle_diff(delta_rot1, 0.0)),
82  fabs(angleutils::angle_diff(delta_rot1, M_PI)));
83  delta_rot2_noise = std::min(
84  fabs(angleutils::angle_diff(delta_rot2, 0.0)),
85  fabs(angleutils::angle_diff(delta_rot2, M_PI)));
86 
87  for (int i = 0; i < set->sample_count; i++) {
88  pf_sample_t * sample = set->samples + i;
89 
90  // Sample pose differences
91  delta_rot1_hat = angleutils::angle_diff(
92  delta_rot1,
93  pf_ran_gaussian(
94  sqrt(
95  alpha1_ * delta_rot1_noise * delta_rot1_noise +
96  alpha2_ * delta_trans * delta_trans)));
97  delta_trans_hat = delta_trans -
98  pf_ran_gaussian(
99  sqrt(
100  alpha3_ * delta_trans * delta_trans +
101  alpha4_ * delta_rot1_noise * delta_rot1_noise +
102  alpha4_ * delta_rot2_noise * delta_rot2_noise));
103  delta_rot2_hat = angleutils::angle_diff(
104  delta_rot2,
105  pf_ran_gaussian(
106  sqrt(
107  alpha1_ * delta_rot2_noise * delta_rot2_noise +
108  alpha2_ * delta_trans * delta_trans)));
109 
110  // Apply sampled update to particle pose
111  sample->pose.v[0] += delta_trans_hat *
112  cos(sample->pose.v[2] + delta_rot1_hat);
113  sample->pose.v[1] += delta_trans_hat *
114  sin(sample->pose.v[2] + delta_rot1_hat);
115  sample->pose.v[2] += delta_rot1_hat + delta_rot2_hat;
116  }
117 }
118 
119 } // namespace nav2_amcl
120 
121 #include <pluginlib/class_list_macros.hpp>
virtual void initialize(double alpha1, double alpha2, double alpha3, double alpha4, double alpha5)
An factory to create motion models.
virtual void odometryUpdate(pf_t *pf, const pf_vector_t &pose, const pf_vector_t &delta)
Update on new odometry data.
An abstract motion model class.
Definition: pf.hpp:112