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


social_navigation_layers
Author(s): David V. Lu!!
autogenerated on Thu Mar 4 2021 04:02:57