moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
pointcloud_octomap_updater.cpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: Jon Binney, Ioan Sucan */
36
37#include <cmath>
40#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
41// TODO: Remove conditional includes when released to all active distros.
42#if __has_include(<tf2/LinearMath/Vector3.hpp>)
43#include <tf2/LinearMath/Vector3.hpp>
44#else
45#include <tf2/LinearMath/Vector3.h>
46#endif
47#if __has_include(<tf2/LinearMath/Transform.hpp>)
48#include <tf2/LinearMath/Transform.hpp>
49#else
50#include <tf2/LinearMath/Transform.h>
51#endif
52#include <sensor_msgs/point_cloud2_iterator.hpp>
53// For Rolling, Kilted, and newer
54#if RCLCPP_VERSION_GTE(29, 6, 0)
55#include <tf2_ros/create_timer_interface.hpp>
56#include <tf2_ros/create_timer_ros.hpp>
57// For Jazzy and older
58#else
59#include <tf2_ros/create_timer_interface.h>
60#include <tf2_ros/create_timer_ros.h>
61#endif
63#include <rclcpp/version.h>
64
65#include <memory>
66
68{
70 : OccupancyMapUpdater("PointCloudUpdater")
71 , scale_(1.0)
72 , padding_(0.0)
73 , max_range_(std::numeric_limits<double>::infinity())
74 , point_subsample_(1)
75 , max_update_rate_(0)
76 , point_cloud_subscriber_(nullptr)
77 , point_cloud_filter_(nullptr)
78 , logger_(moveit::getLogger("moveit.ros.pointcloud_octomap_updater"))
79{
80}
81
82bool PointCloudOctomapUpdater::setParams(const std::string& name_space)
83{
84 auto check_required = [this, &name_space](const std::string& key, auto& target,
85 std::vector<std::string>& missing_keys) {
86 if (!this->node_->get_parameter(name_space + "." + key, target))
87 {
88 missing_keys.push_back(key);
89 }
90 };
91 // This parameter is optional
92 node_->get_parameter_or(name_space + ".ns", ns_, std::string());
93
94 std::vector<std::string> missing_keys;
95
96 check_required("point_cloud_topic", point_cloud_topic_, missing_keys);
97 check_required("max_range", max_range_, missing_keys);
98 check_required("padding_offset", padding_, missing_keys);
99 check_required("padding_scale", scale_, missing_keys);
100 check_required("point_subsample", point_subsample_, missing_keys);
101 check_required("max_update_rate", max_update_rate_, missing_keys);
102 check_required("filtered_cloud_topic", filtered_cloud_topic_, missing_keys);
103
104 if (missing_keys.empty())
105 {
106 return true;
107 }
108 std::ostringstream oss;
109 for (const auto& name : missing_keys)
110 {
111 oss << ", "
112 << "'" << name << "'";
113 }
114 RCLCPP_ERROR(node_->get_logger(), "Missing parameters under '%s': %s", name_space.c_str(), oss.str().c_str());
115 return false;
116}
117
118bool PointCloudOctomapUpdater::initialize(const rclcpp::Node::SharedPtr& node)
119{
120 node_ = node;
121 tf_buffer_ = std::make_shared<tf2_ros::Buffer>(node_->get_clock());
122 auto create_timer_interface =
123 std::make_shared<tf2_ros::CreateTimerROS>(node->get_node_base_interface(), node->get_node_timers_interface());
124 tf_buffer_->setCreateTimerInterface(create_timer_interface);
125 tf_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_buffer_);
126 shape_mask_ = std::make_unique<point_containment_filter::ShapeMask>();
127 shape_mask_->setTransformCallback(
128 [this](ShapeHandle shape, Eigen::Isometry3d& tf) { return getShapeTransform(shape, tf); });
129
130 return true;
131}
132
134{
135 std::string prefix = "";
136 if (!ns_.empty())
137 prefix = ns_ + "/";
138
139 if (!filtered_cloud_topic_.empty())
140 {
141 filtered_cloud_publisher_ =
142 node_->create_publisher<sensor_msgs::msg::PointCloud2>(prefix + filtered_cloud_topic_, rclcpp::SensorDataQoS());
143 }
144
145 if (point_cloud_subscriber_)
146 return;
147
148 rclcpp::SubscriptionOptions options;
149 options.callback_group = node_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
150 /* subscribe to point cloud topic using tf filter*/
151 auto qos_profile =
152#if RCLCPP_VERSION_GTE(28, 3, 0)
153 rclcpp::SensorDataQoS();
154#else
155 rmw_qos_profile_sensor_data;
156#endif
157 point_cloud_subscriber_ =
158 new message_filters::Subscriber<sensor_msgs::msg::PointCloud2>(node_, point_cloud_topic_, qos_profile, options);
159 if (tf_listener_ && tf_buffer_ && !monitor_->getMapFrame().empty())
160 {
161// For Rolling, L-turtle, and newer
162#if RCLCPP_VERSION_GTE(30, 0, 0)
163 using MessageFilterPointCloud2 = tf2_ros::MessageFilter<sensor_msgs::msg::PointCloud2>;
164
165 MessageFilterPointCloud2::RequiredInterfaces required_interfaces{ node_->get_node_logging_interface(),
166 node_->get_node_clock_interface() };
167
168 point_cloud_filter_ = new tf2_ros::MessageFilter<sensor_msgs::msg::PointCloud2>(
169 *point_cloud_subscriber_, *tf_buffer_, monitor_->getMapFrame(), 5, std::move(required_interfaces));
170#else
171 point_cloud_filter_ = new tf2_ros::MessageFilter<sensor_msgs::msg::PointCloud2>(
172 *point_cloud_subscriber_, *tf_buffer_, monitor_->getMapFrame(), 5, node_);
173#endif
174 point_cloud_filter_->registerCallback(
175 [this](const sensor_msgs::msg::PointCloud2::ConstSharedPtr& cloud) { cloudMsgCallback(cloud); });
176 RCLCPP_INFO(logger_, "Listening to '%s' using message filter with target frame '%s'", point_cloud_topic_.c_str(),
177 point_cloud_filter_->getTargetFramesString().c_str());
178 }
179 else
180 {
181 point_cloud_subscriber_->registerCallback(
182 [this](const sensor_msgs::msg::PointCloud2::ConstSharedPtr& cloud) { cloudMsgCallback(cloud); });
183 RCLCPP_INFO(logger_, "Listening to '%s'", point_cloud_topic_.c_str());
184 }
185}
186
188{
189 delete point_cloud_filter_;
190 delete point_cloud_subscriber_;
191 point_cloud_filter_ = nullptr;
192 point_cloud_subscriber_ = nullptr;
193}
194
195ShapeHandle PointCloudOctomapUpdater::excludeShape(const shapes::ShapeConstPtr& shape)
196{
197 ShapeHandle h = 0;
198 if (shape_mask_)
199 {
200 h = shape_mask_->addShape(shape, scale_, padding_);
201 }
202 else
203 {
204 RCLCPP_ERROR(logger_, "Shape filter not yet initialized!");
205 }
206 return h;
207}
208
210{
211 if (shape_mask_)
212 shape_mask_->removeShape(handle);
213}
214
215bool PointCloudOctomapUpdater::getShapeTransform(ShapeHandle h, Eigen::Isometry3d& transform) const
216{
217 ShapeTransformCache::const_iterator it = transform_cache_.find(h);
218 if (it != transform_cache_.end())
219 {
220 transform = it->second;
221 }
222 return it != transform_cache_.end();
223}
224
225void PointCloudOctomapUpdater::updateMask(const sensor_msgs::msg::PointCloud2& /*cloud*/,
226 const Eigen::Vector3d& /*sensor_origin*/, std::vector<int>& /*mask*/)
227{
228}
229
230void PointCloudOctomapUpdater::cloudMsgCallback(const sensor_msgs::msg::PointCloud2::ConstSharedPtr& cloud_msg)
231{
232 RCLCPP_DEBUG(logger_, "Received a new point cloud message");
233 rclcpp::Time start = rclcpp::Clock(RCL_ROS_TIME).now();
234
235 if (max_update_rate_ > 0)
236 {
237 // ensure we are not updating the octomap representation too often
238 if ((node_->now() - last_update_time_) <= rclcpp::Duration::from_seconds(1.0 / max_update_rate_))
239 return;
240 last_update_time_ = node_->now();
241 }
242
243 if (monitor_->getMapFrame().empty())
244 monitor_->setMapFrame(cloud_msg->header.frame_id);
245
246 /* get transform for cloud into map frame */
247 tf2::Stamped<tf2::Transform> map_h_sensor;
248 if (monitor_->getMapFrame() == cloud_msg->header.frame_id)
249 {
250 map_h_sensor.setIdentity();
251 }
252 else
253 {
254 if (tf_buffer_)
255 {
256 try
257 {
258 tf2::fromMsg(tf_buffer_->lookupTransform(monitor_->getMapFrame(), cloud_msg->header.frame_id,
259 cloud_msg->header.stamp),
260 map_h_sensor);
261 }
262 catch (tf2::TransformException& ex)
263 {
264 RCLCPP_ERROR_STREAM(logger_, "Transform error of sensor data: " << ex.what() << "; quitting callback");
265 return;
266 }
267 }
268 else
269 return;
270 }
271
272 /* compute sensor origin in map frame */
273 const tf2::Vector3& sensor_origin_tf = map_h_sensor.getOrigin();
274 octomap::point3d sensor_origin(sensor_origin_tf.getX(), sensor_origin_tf.getY(), sensor_origin_tf.getZ());
275 Eigen::Vector3d sensor_origin_eigen(sensor_origin_tf.getX(), sensor_origin_tf.getY(), sensor_origin_tf.getZ());
276
277 if (!updateTransformCache(cloud_msg->header.frame_id, cloud_msg->header.stamp))
278 return;
279
280 /* mask out points on the robot */
281 shape_mask_->maskContainment(*cloud_msg, sensor_origin_eigen, 0.0, max_range_, mask_);
282 updateMask(*cloud_msg, sensor_origin_eigen, mask_);
283
284 octomap::KeySet free_cells, occupied_cells, model_cells, clip_cells;
285 std::unique_ptr<sensor_msgs::msg::PointCloud2> filtered_cloud;
286
287 // We only use these iterators if we are creating a filtered_cloud for
288 // publishing. We cannot default construct these, so we use unique_ptr's
289 // to defer construction
290 std::unique_ptr<sensor_msgs::PointCloud2Iterator<float>> iter_filtered_x;
291 std::unique_ptr<sensor_msgs::PointCloud2Iterator<float>> iter_filtered_y;
292 std::unique_ptr<sensor_msgs::PointCloud2Iterator<float>> iter_filtered_z;
293
294 if (!filtered_cloud_topic_.empty())
295 {
296 filtered_cloud = std::make_unique<sensor_msgs::msg::PointCloud2>();
297 filtered_cloud->header = cloud_msg->header;
298 sensor_msgs::PointCloud2Modifier pcd_modifier(*filtered_cloud);
299 pcd_modifier.setPointCloud2FieldsByString(1, "xyz");
300 pcd_modifier.resize(cloud_msg->width * cloud_msg->height);
301
302 // we have created a filtered_out, so we can create the iterators now
303 iter_filtered_x = std::make_unique<sensor_msgs::PointCloud2Iterator<float>>(*filtered_cloud, "x");
304 iter_filtered_y = std::make_unique<sensor_msgs::PointCloud2Iterator<float>>(*filtered_cloud, "y");
305 iter_filtered_z = std::make_unique<sensor_msgs::PointCloud2Iterator<float>>(*filtered_cloud, "z");
306 }
307 size_t filtered_cloud_size = 0;
308
309 tree_->lockRead();
310
311 try
312 {
313 /* do ray tracing to find which cells this point cloud indicates should be free, and which it indicates
314 * should be occupied */
315 for (unsigned int row = 0; row < cloud_msg->height; row += point_subsample_)
316 {
317 unsigned int row_c = row * cloud_msg->width;
318 sensor_msgs::PointCloud2ConstIterator<float> pt_iter(*cloud_msg, "x");
319 // set iterator to point at start of the current row
320 pt_iter += row_c;
321
322 for (unsigned int col = 0; col < cloud_msg->width; col += point_subsample_, pt_iter += point_subsample_)
323 {
324 // if (mask_[row_c + col] == point_containment_filter::ShapeMask::CLIP)
325 // continue;
326
327 /* check for NaN */
328 if (!std::isnan(pt_iter[0]) && !std::isnan(pt_iter[1]) && !std::isnan(pt_iter[2]))
329 {
330 /* occupied cell at ray endpoint if ray is shorter than max range and this point
331 isn't on a part of the robot*/
332 if (mask_[row_c + col] == point_containment_filter::ShapeMask::INSIDE)
333 {
334 // transform to map frame
335 tf2::Vector3 point_tf = map_h_sensor * tf2::Vector3(pt_iter[0], pt_iter[1], pt_iter[2]);
336 model_cells.insert(tree_->coordToKey(point_tf.getX(), point_tf.getY(), point_tf.getZ()));
337 }
338 else if (mask_[row_c + col] == point_containment_filter::ShapeMask::CLIP)
339 {
340 tf2::Vector3 clipped_point_tf =
341 map_h_sensor * (tf2::Vector3(pt_iter[0], pt_iter[1], pt_iter[2]).normalize() * max_range_);
342 clip_cells.insert(
343 tree_->coordToKey(clipped_point_tf.getX(), clipped_point_tf.getY(), clipped_point_tf.getZ()));
344 }
345 else
346 {
347 tf2::Vector3 point_tf = map_h_sensor * tf2::Vector3(pt_iter[0], pt_iter[1], pt_iter[2]);
348 occupied_cells.insert(tree_->coordToKey(point_tf.getX(), point_tf.getY(), point_tf.getZ()));
349 // build list of valid points if we want to publish them
350 if (filtered_cloud)
351 {
352 **iter_filtered_x = pt_iter[0];
353 **iter_filtered_y = pt_iter[1];
354 **iter_filtered_z = pt_iter[2];
355 ++filtered_cloud_size;
356 ++*iter_filtered_x;
357 ++*iter_filtered_y;
358 ++*iter_filtered_z;
359 }
360 }
361 }
362 }
363 }
364
365 /* compute the free cells along each ray that ends at an occupied cell */
366 for (const octomap::OcTreeKey& occupied_cell : occupied_cells)
367 {
368 if (tree_->computeRayKeys(sensor_origin, tree_->keyToCoord(occupied_cell), key_ray_))
369 free_cells.insert(key_ray_.begin(), key_ray_.end());
370 }
371
372 /* compute the free cells along each ray that ends at a model cell */
373 for (const octomap::OcTreeKey& model_cell : model_cells)
374 {
375 if (tree_->computeRayKeys(sensor_origin, tree_->keyToCoord(model_cell), key_ray_))
376 free_cells.insert(key_ray_.begin(), key_ray_.end());
377 }
378
379 /* compute the free cells along each ray that ends at a clipped cell */
380 for (const octomap::OcTreeKey& clip_cell : clip_cells)
381 {
382 free_cells.insert(clip_cell);
383 if (tree_->computeRayKeys(sensor_origin, tree_->keyToCoord(clip_cell), key_ray_))
384 free_cells.insert(key_ray_.begin(), key_ray_.end());
385 }
386 }
387 catch (...)
388 {
389 tree_->unlockRead();
390 return;
391 }
392
393 tree_->unlockRead();
394
395 /* cells that overlap with the model are not occupied */
396 for (const octomap::OcTreeKey& model_cell : model_cells)
397 occupied_cells.erase(model_cell);
398
399 /* occupied cells are not free */
400 for (const octomap::OcTreeKey& occupied_cell : occupied_cells)
401 free_cells.erase(occupied_cell);
402
403 tree_->lockWrite();
404
405 try
406 {
407 /* mark free cells only if not seen occupied in this cloud */
408 for (const octomap::OcTreeKey& free_cell : free_cells)
409 tree_->updateNode(free_cell, false);
410
411 /* now mark all occupied cells */
412 for (const octomap::OcTreeKey& occupied_cell : occupied_cells)
413 tree_->updateNode(occupied_cell, true);
414
415 // set the logodds to the minimum for the cells that are part of the model
416 const float lg = tree_->getClampingThresMinLog() - tree_->getClampingThresMaxLog();
417 for (const octomap::OcTreeKey& model_cell : model_cells)
418 tree_->updateNode(model_cell, lg);
419 }
420 catch (...)
421 {
422 RCLCPP_ERROR(logger_, "Internal error while updating octree");
423 }
424 tree_->unlockWrite();
425 RCLCPP_DEBUG(logger_, "Processed point cloud in %lf ms", (node_->now() - start).seconds() * 1000.0);
426 tree_->triggerUpdateCallback();
427
428 if (filtered_cloud)
429 {
430 sensor_msgs::PointCloud2Modifier pcd_modifier(*filtered_cloud);
431 pcd_modifier.resize(filtered_cloud_size);
432 filtered_cloud_publisher_->publish(*filtered_cloud);
433 }
434}
435} // namespace occupancy_map_monitor
const std::string & getMapFrame() const
Gets the map frame (this is set either by the constor or a parameter).
void setMapFrame(const std::string &frame)
Sets the map frame.
Base class for classes which update the occupancy map.
bool updateTransformCache(const std::string &target_frame, const rclcpp::Time &target_time)
bool setParams(const std::string &name_space) override
Set updater params using struct that comes from parsing a yaml string. This must be called after setM...
virtual void updateMask(const sensor_msgs::msg::PointCloud2 &cloud, const Eigen::Vector3d &sensor_origin, std::vector< int > &mask)
bool initialize(const rclcpp::Node::SharedPtr &node) override
Do any necessary setup (subscribe to ros topics, etc.). This call assumes setMonitor() and setParams(...
ShapeHandle excludeShape(const shapes::ShapeConstPtr &shape) override
Main namespace for MoveIt.