largemap_to_map.cpp
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 #include <algorithm>
31 #include <cmath>
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 #include <ros/ros.h>
37 
38 #include <nav_msgs/OccupancyGrid.h>
41 
43 
45 {
46 private:
52 
53  nav_msgs::OccupancyGrid::ConstPtr large_map_;
56 
57  std::string robot_frame_;
58 
59  int width_;
62  std::map<size_t, std::vector<size_t>> occlusion_table_;
63 
64 public:
66  : pnh_("~")
67  , nh_()
68  , tfl_(tfbuf_)
69  {
71  pnh_.param("robot_frame", robot_frame_, std::string("base_link"));
72 
73  pub_map_ = neonavigation_common::compat::advertise<nav_msgs::OccupancyGrid>(
74  nh_, "map_local",
75  pnh_, "map", 1, true);
76  sub_largemap_ = nh_.subscribe("map", 2, &LargeMapToMapNode::cbLargeMap, this);
77 
78  pnh_.param("width", width_, 30);
79  pnh_.param("round_local_map", round_local_map_, false);
80  pnh_.param("simulate_occlusion", simulate_occlusion_, false);
81 
82  for (size_t addr = 0; addr < static_cast<size_t>(width_ * width_); ++addr)
83  {
84  const int ux = addr % width_;
85  const int uy = addr / width_;
86  const float x = ux - width_ / 2.0;
87  const float y = uy - width_ / 2.0;
88  const float l = std::sqrt(x * x + y * y);
89  for (float r = 1.0; r < l - 1.0; r += 0.5)
90  {
91  const float x2 = x * r / l;
92  const float y2 = y * r / l;
93  const int ux2 = x2 + width_ / 2.0;
94  const int uy2 = y2 + width_ / 2.0;
95  const int addr2 = uy2 * width_ + ux2;
96  occlusion_table_[addr2].push_back(addr);
97  }
98  }
99  for (auto& cell : occlusion_table_)
100  {
101  std::sort(cell.second.begin(), cell.second.end());
102  cell.second.erase(std::unique(cell.second.begin(), cell.second.end()), cell.second.end());
103  const auto self_it = std::find(cell.second.begin(), cell.second.end(), cell.first);
104  if (self_it != cell.second.end())
105  cell.second.erase(self_it);
106  }
107 
108  double hz;
109  pnh_.param("hz", hz, 1.0);
110  timer_ = nh_.createTimer(ros::Duration(1.0 / hz), &LargeMapToMapNode::cbTimer, this);
111  }
112 
113 private:
114  void cbTimer(const ros::TimerEvent& event)
115  {
116  publishMap();
117  }
118  void cbLargeMap(const nav_msgs::OccupancyGrid::ConstPtr& msg)
119  {
120  large_map_ = msg;
121  }
122  void publishMap()
123  {
124  if (!large_map_)
125  return;
127  try
128  {
129  tf2::fromMsg(tfbuf_.lookupTransform(large_map_->header.frame_id, robot_frame_, ros::Time(0)), trans);
130  }
131  catch (tf2::TransformException& e)
132  {
133  return;
134  }
135 
136  nav_msgs::OccupancyGrid map;
137  map.header.frame_id = large_map_->header.frame_id;
138  map.header.stamp = ros::Time::now();
139  map.info = large_map_->info;
140  map.info.width = width_;
141  map.info.height = width_;
142 
143  const auto pos = trans.getOrigin();
144  const float x = static_cast<int>(pos.x() / map.info.resolution) * map.info.resolution;
145  const float y = static_cast<int>(pos.y() / map.info.resolution) * map.info.resolution;
146  map.info.origin.position.x = x - map.info.width * map.info.resolution * 0.5;
147  map.info.origin.position.y = y - map.info.height * map.info.resolution * 0.5;
148  map.info.origin.position.z = 0.0;
149  map.info.origin.orientation.w = 1.0;
150  map.data.resize(width_ * width_);
151 
152  const int gx =
153  std::lround((map.info.origin.position.x - large_map_->info.origin.position.x) / map.info.resolution);
154  const int gy =
155  std::lround((map.info.origin.position.y - large_map_->info.origin.position.y) / map.info.resolution);
156  const float half_width = width_ / 2.0;
157 
158  for (int y = gy; y < gy + width_; ++y)
159  {
160  for (int x = gx; x < gx + width_; ++x)
161  {
162  const int lx = x - gx;
163  const int ly = y - gy;
164  const size_t addr = ly * width_ + lx;
165  const size_t addr_large = y * large_map_->info.width + x;
166  if (round_local_map_ &&
167  std::pow(lx - half_width, 2) + std::pow(ly - half_width, 2) > std::pow(half_width, 2))
168  {
169  map.data[addr] = -1;
170  }
171  else if (x < 0 || y < 0 ||
172  x >= static_cast<int>(large_map_->info.width) ||
173  y >= static_cast<int>(large_map_->info.height))
174  {
175  map.data[addr] = -1;
176  }
177  else
178  {
179  map.data[addr] = large_map_->data[addr_large];
180  }
181  }
182  }
183  if (simulate_occlusion_)
184  {
185  for (size_t addr = 0; addr < static_cast<size_t>(width_ * width_); ++addr)
186  {
187  if (map.data[addr] == 100)
188  {
189  for (auto a : occlusion_table_[addr])
190  {
191  map.data[a] = -1;
192  }
193  }
194  }
195  }
196 
197  pub_map_.publish(map);
198  }
199 };
200 
201 int main(int argc, char** argv)
202 {
203  ros::init(argc, argv, "largemap_to_map");
204 
205  LargeMapToMapNode conv;
206  ros::spin();
207 
208  return 0;
209 }
tf2_ros::TransformListener tfl_
void cbTimer(const ros::TimerEvent &event)
void publish(const boost::shared_ptr< M > &message) const
void cbLargeMap(const nav_msgs::OccupancyGrid::ConstPtr &msg)
ros::Subscriber sub_largemap_
virtual geometry_msgs::TransformStamped lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration timeout) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
TFSIMD_FORCE_INLINE const tfScalar & y() const
nav_msgs::OccupancyGrid::ConstPtr large_map_
ROSCPP_DECL void spin(Spinner &spinner)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void fromMsg(const A &, B &b)
TFSIMD_FORCE_INLINE const tfScalar & x() const
ros::NodeHandle nh_
tf2_ros::Buffer tfbuf_
std::string robot_frame_
int main(int argc, char **argv)
static Time now()
std::map< size_t, std::vector< size_t > > occlusion_table_
ros::NodeHandle pnh_
ros::Publisher pub_map_


costmap_cspace
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:20:29