passing_layer.cpp
Go to the documentation of this file.
1 // Copyright 2018 David V. Lu!!
3 #include <geometry_msgs/PointStamped.h>
6 #include <angles/angles.h>
7 #include <algorithm>
8 #include <list>
9 #include <string>
10 
12 {
14 {
15 public:
17  {
18  }
19 
20  virtual void updateBounds(double origin_x, double origin_y, double origin_z, double* min_x, double* min_y,
21  double* max_x, double* max_y)
22  {
23  boost::recursive_mutex::scoped_lock lock(lock_);
24 
25  std::string global_frame = layered_costmap_->getGlobalFrameID();
26  transformed_people_.clear();
27 
28  for (unsigned int i = 0; i < people_list_.people.size(); i++)
29  {
30  people_msgs::Person& person = people_list_.people[i];
31  people_msgs::Person tpt;
32  geometry_msgs::PointStamped pt, opt;
33 
34  try
35  {
36  pt.point.x = person.position.x;
37  pt.point.y = person.position.y;
38  pt.point.z = person.position.z;
39  pt.header.frame_id = people_list_.header.frame_id;
40  pt.header.stamp = people_list_.header.stamp;
41  tf_->transform(pt, opt, global_frame);
42  tpt.position.x = opt.point.x;
43  tpt.position.y = opt.point.y;
44  tpt.position.z = opt.point.z;
45 
46  pt.point.x += person.velocity.x;
47  pt.point.y += person.velocity.y;
48  pt.point.z += person.velocity.z;
49  tf_->transform(pt, opt, global_frame);
50 
51  tpt.velocity.x = tpt.position.x - opt.point.x;
52  tpt.velocity.y = tpt.position.y - opt.point.y;
53  tpt.velocity.z = tpt.position.z - opt.point.z;
54 
55  transformed_people_.push_back(tpt);
56 
57  double mag = sqrt(pow(tpt.velocity.x, 2) + pow(person.velocity.y, 2));
58  double factor = 1.0 + mag * factor_;
59  double point = get_radius(cutoff_, amplitude_, covar_ * factor);
60 
61  *min_x = std::min(*min_x, tpt.position.x - point);
62  *min_y = std::min(*min_y, tpt.position.y - point);
63  *max_x = std::max(*max_x, tpt.position.x + point);
64  *max_y = std::max(*max_y, tpt.position.y + point);
65  }
66  catch (tf2::LookupException& ex)
67  {
68  ROS_ERROR("No Transform available Error: %s\n", ex.what());
69  continue;
70  }
71  catch (tf2::ConnectivityException& ex)
72  {
73  ROS_ERROR("Connectivity Error: %s\n", ex.what());
74  continue;
75  }
76  catch (tf2::ExtrapolationException& ex)
77  {
78  ROS_ERROR("Extrapolation Error: %s\n", ex.what());
79  continue;
80  }
81  }
82  }
83 
84  virtual void updateCosts(costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j)
85  {
86  boost::recursive_mutex::scoped_lock lock(lock_);
87  if (!enabled_) return;
88 
89  if (people_list_.people.size() == 0)
90  return;
91  if (cutoff_ >= amplitude_)
92  return;
93 
94  std::list<people_msgs::Person>::iterator p_it;
96  double res = costmap->getResolution();
97 
98  for (p_it = transformed_people_.begin(); p_it != transformed_people_.end(); ++p_it)
99  {
100  people_msgs::Person person = *p_it;
101  double angle = atan2(person.velocity.y, person.velocity.x) + 1.51;
102  double mag = sqrt(pow(person.velocity.x, 2) + pow(person.velocity.y, 2));
103  double factor = 1.0 + mag * factor_;
104  double base = get_radius(cutoff_, amplitude_, covar_);
105  double point = get_radius(cutoff_, amplitude_, covar_ * factor);
106 
107  unsigned int width = std::max(1, static_cast<int>((base + point) / res)),
108  height = std::max(1, static_cast<int>((base + point) / res));
109 
110  double cx = person.position.x, cy = person.position.y;
111 
112  double ox, oy;
113  if (sin(angle) > 0)
114  oy = cy - base;
115  else
116  oy = cy + (point - base) * sin(angle) - base;
117 
118  if (cos(angle) >= 0)
119  ox = cx - base;
120  else
121  ox = cx + (point - base) * cos(angle) - base;
122 
123 
124  int dx, dy;
125  costmap->worldToMapNoBounds(ox, oy, dx, dy);
126 
127  int start_x = 0, start_y = 0, end_x = width, end_y = height;
128  if (dx < 0)
129  start_x = -dx;
130  else if (dx + width > costmap->getSizeInCellsX())
131  end_x = std::max(0, static_cast<int>(costmap->getSizeInCellsX()) - dx);
132 
133  if (static_cast<int>(start_x + dx) < min_i)
134  start_x = min_i - dx;
135  if (static_cast<int>(end_x + dx) > max_i)
136  end_x = max_i - dx;
137 
138  if (dy < 0)
139  start_y = -dy;
140  else if (dy + height > costmap->getSizeInCellsY())
141  end_y = std::max(0, static_cast<int>(costmap->getSizeInCellsY()) - dy);
142 
143  if (static_cast<int>(start_y + dy) < min_j)
144  start_y = min_j - dy;
145  if (static_cast<int>(end_y + dy) > max_j)
146  end_y = max_j - dy;
147 
148  double bx = ox + res / 2,
149  by = oy + res / 2;
150  for (int i = start_x; i < end_x; i++)
151  {
152  for (int j = start_y; j < end_y; j++)
153  {
154  unsigned char old_cost = costmap->getCost(i + dx, j + dy);
155  if (old_cost == costmap_2d::NO_INFORMATION)
156  continue;
157 
158  double x = bx + i * res, y = by + j * res;
159  double ma = atan2(y - cy, x - cx);
160  double diff = angles::shortest_angular_distance(angle, ma);
161  double a;
162  if (fabs(diff) < M_PI / 2)
163  a = gaussian(x, y, cx, cy, amplitude_, covar_ * factor, covar_, angle);
164  else
165  continue;
166 
167  if (a < cutoff_)
168  continue;
169  unsigned char cvalue = (unsigned char) a;
170  costmap->setCost(i + dx, j + dy, std::max(cvalue, old_cost));
171  }
172  }
173  }
174  }
175 };
176 }; // namespace social_navigation_layers
177 
double gaussian(double x, double y, double x0, double y0, double A, double varx, double vary, double skew)
void setCost(unsigned int mx, unsigned int my, unsigned char cost)
unsigned int getSizeInCellsX() const
LayeredCostmap * layered_costmap_
std::list< people_msgs::Person > transformed_people_
Definition: social_layer.h:37
T & transform(const T &in, T &out, const std::string &target_frame, ros::Duration timeout=ros::Duration(0.0)) const
unsigned int getSizeInCellsY() const
std::string getGlobalFrameID() const
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
tf2_ros::Buffer * tf_
virtual void updateBounds(double origin_x, double origin_y, double origin_z, double *min_x, double *min_y, double *max_x, double *max_y)
double get_radius(double cutoff, double A, double var)
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
static const unsigned char NO_INFORMATION
virtual void updateCosts(costmap_2d::Costmap2D &master_grid, int min_i, int min_j, int max_i, int max_j)
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
double getResolution() const
void worldToMapNoBounds(double wx, double wy, int &mx, int &my) const
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
#define ROS_ERROR(...)
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
def shortest_angular_distance(from_angle, to_angle)
unsigned char getCost(unsigned int mx, unsigned int my) const


social_navigation_layers
Author(s): David V. Lu!!
autogenerated on Mon Feb 28 2022 22:55:31