path2d.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, the neonavigation authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef TRAJECTORY_TRACKER_PATH2D_H
31 #define TRAJECTORY_TRACKER_PATH2D_H
32 
33 #include <vector>
34 #include <limits>
35 
36 #include <Eigen/Core>
37 #include <Eigen/Geometry>
38 
39 #include <geometry_msgs/Pose.h>
40 #include <tf2/utils.h>
41 
44 
45 namespace trajectory_tracker
46 {
47 class Pose2D
48 {
49 public:
50  Eigen::Vector2d pos_;
51  float yaw_;
52  float velocity_;
53 
54  inline Pose2D()
55  : pos_(0, 0)
56  , yaw_(0)
57  , velocity_(0)
58  {
59  }
60  inline Pose2D(const Eigen::Vector2d& p, float y, float velocity)
61  : pos_(p)
62  , yaw_(y)
63  , velocity_(velocity)
64  {
65  }
66  inline explicit Pose2D(const geometry_msgs::Pose& pose, float velocity)
67  : pos_(Eigen::Vector2d(pose.position.x, pose.position.y))
68  , yaw_(tf2::getYaw(pose.orientation))
69  , velocity_(velocity)
70  {
71  }
72 };
73 class Path2D : public std::vector<Pose2D>
74 {
75 private:
76  using Super = std::vector<Pose2D>;
77 
78 public:
79  using Iterator = std::vector<Pose2D>::iterator;
80  using ConstIterator = std::vector<Pose2D>::const_iterator;
81 
82  inline float length() const
83  {
84  double l = 0;
85  for (size_t i = 1; i < size(); i++)
86  l += ((*this)[i - 1].pos_ - (*this)[i].pos_).norm();
87  return l;
88  }
90  const ConstIterator& begin,
91  const ConstIterator& end,
92  const bool allow_backward_motion) const
93  {
94  float sign_vel_prev = 0;
95  ConstIterator it_prev = begin;
96  for (ConstIterator it = begin + 1; it < end; ++it)
97  {
98  const Eigen::Vector2d inc = it->pos_ - it_prev->pos_;
99 
100  // stop reading forward if the path is switching back
101  const float angle = atan2(inc[1], inc[0]);
102  const float angle_pose = allow_backward_motion ? it->yaw_ : angle;
103  const float sign_vel_req = std::cos(angle) * std::cos(angle_pose) + std::sin(angle) * std::sin(angle_pose);
104  if (sign_vel_prev * sign_vel_req < 0)
105  return it;
106  sign_vel_prev = sign_vel_req;
107  it_prev = it;
108  }
109  return end;
110  }
112  const ConstIterator& begin,
113  const ConstIterator& end,
114  const Eigen::Vector2d& target,
115  const float max_search_range = 0) const
116  {
117  float distance_path_search = 0;
118  float min_dist = std::numeric_limits<float>::max();
119  ConstIterator it_nearest = Super::end();
120 
121  ConstIterator it_prev = begin;
122  for (ConstIterator it = begin + 1; it < end; ++it)
123  {
124  const Eigen::Vector2d inc = it->pos_ - it_prev->pos_;
125  distance_path_search += inc.norm();
126  if (max_search_range > 0 && distance_path_search > max_search_range)
127  break;
128 
129  const float d =
130  trajectory_tracker::lineStripDistance(it_prev->pos_, it->pos_, target);
131  if (d <= min_dist)
132  {
133  min_dist = d;
134  it_nearest = it;
135  }
136  it_prev = it;
137  }
138  return it_nearest;
139  }
140  inline double remainedDistance(
141  const ConstIterator& begin,
142  const ConstIterator& nearest,
143  const ConstIterator& end,
144  const Eigen::Vector2d& target_on_line) const
145  {
146  double remain = (nearest->pos_ - target_on_line).norm();
147  if (nearest + 1 >= end)
148  {
149  const ConstIterator last = end - 1;
150  const ConstIterator last_pre = end - 2;
151  if (last_pre < begin || last < begin)
152  {
153  // no enough points: orientation control mode
154  return 0;
155  }
156  const Eigen::Vector2d vec_path = last->pos_ - last_pre->pos_;
157  const Eigen::Vector2d vec_remain = last->pos_ - target_on_line;
158  if (vec_path.dot(vec_remain) >= 0)
159  {
160  // ongoing
161  return remain;
162  }
163  // overshoot
164  return -remain;
165  }
166  ConstIterator it_prev = nearest;
167  for (ConstIterator it = nearest + 1; it < end; ++it)
168  {
169  remain += (it_prev->pos_ - it->pos_).norm();
170  it_prev = it;
171  }
172  return remain;
173  }
174  inline float getCurvature(
175  const ConstIterator& begin,
176  const ConstIterator& end,
177  const Eigen::Vector2d& target_on_line,
178  const float max_search_range) const
179  {
180  const float max_search_range_sq = max_search_range * max_search_range;
182  ConstIterator it_prev2 = begin;
183  ConstIterator it_prev1 = begin + 1;
184  for (ConstIterator it = begin + 2; it < end; ++it)
185  {
186  curv += trajectory_tracker::curv3p(it_prev2->pos_, it_prev1->pos_, it->pos_);
187  if ((it->pos_ - target_on_line).squaredNorm() > max_search_range_sq)
188  break;
189  it_prev2 = it_prev1;
190  it_prev1 = it;
191  }
192  return curv;
193  }
194 };
195 } // namespace trajectory_tracker
196 
197 #endif // TRAJECTORY_TRACKER_PATH2D_H
d
double remainedDistance(const ConstIterator &begin, const ConstIterator &nearest, const ConstIterator &end, const Eigen::Vector2d &target_on_line) const
Definition: path2d.h:140
std::vector< Pose2D >::const_iterator ConstIterator
Definition: path2d.h:80
ConstIterator findLocalGoal(const ConstIterator &begin, const ConstIterator &end, const bool allow_backward_motion) const
Definition: path2d.h:89
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
float getCurvature(const ConstIterator &begin, const ConstIterator &end, const Eigen::Vector2d &target_on_line, const float max_search_range) const
Definition: path2d.h:174
std::vector< Pose2D > Super
Definition: path2d.h:76
std::vector< Pose2D >::iterator Iterator
Definition: path2d.h:79
float curv3p(const Eigen::Vector2d &a, const Eigen::Vector2d &b, const Eigen::Vector2d &c)
Definition: eigen_line.h:40
TFSIMD_FORCE_INLINE const tfScalar & x() const
double getYaw(const tf2::Quaternion &q)
float length() const
Definition: path2d.h:82
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
Pose2D(const Eigen::Vector2d &p, float y, float velocity)
Definition: path2d.h:60
Eigen::Vector2d pos_
Definition: path2d.h:50
float lineStripDistance(const Eigen::Vector2d &a, const Eigen::Vector2d &b, const Eigen::Vector2d &c)
Definition: eigen_line.h:66
Pose2D(const geometry_msgs::Pose &pose, float velocity)
Definition: path2d.h:66
ConstIterator findNearest(const ConstIterator &begin, const ConstIterator &end, const Eigen::Vector2d &target, const float max_search_range=0) const
Definition: path2d.h:111


trajectory_tracker
Author(s): Atsushi Watanabe
autogenerated on Tue Jul 9 2019 05:00:09