40#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
41#include <tf2/LinearMath/Vector3.h>
42#include <tf2/LinearMath/Transform.h>
43#include <sensor_msgs/point_cloud2_iterator.hpp>
44#include <tf2_ros/create_timer_interface.h>
45#include <tf2_ros/create_timer_ros.h>
47#include <rclcpp/version.h>
57 , max_range_(std::numeric_limits<double>::infinity())
60 , point_cloud_subscriber_(nullptr)
61 , point_cloud_filter_(nullptr)
62 , logger_(
moveit::getLogger(
"moveit.ros.pointcloud_octomap_updater"))
69 node_->get_parameter_or(name_space +
".ns", ns_, std::string());
70 return node_->get_parameter(name_space +
".point_cloud_topic", point_cloud_topic_) &&
71 node_->get_parameter(name_space +
".max_range", max_range_) &&
72 node_->get_parameter(name_space +
".padding_offset", padding_) &&
73 node_->get_parameter(name_space +
".padding_scale", scale_) &&
74 node_->get_parameter(name_space +
".point_subsample", point_subsample_) &&
75 node_->get_parameter(name_space +
".max_update_rate", max_update_rate_) &&
76 node_->get_parameter(name_space +
".filtered_cloud_topic", filtered_cloud_topic_);
82 tf_buffer_ = std::make_shared<tf2_ros::Buffer>(node_->get_clock());
83 auto create_timer_interface =
84 std::make_shared<tf2_ros::CreateTimerROS>(node->get_node_base_interface(), node->get_node_timers_interface());
85 tf_buffer_->setCreateTimerInterface(create_timer_interface);
86 tf_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_buffer_);
87 shape_mask_ = std::make_unique<point_containment_filter::ShapeMask>();
88 shape_mask_->setTransformCallback(
89 [
this](
ShapeHandle shape, Eigen::Isometry3d& tf) {
return getShapeTransform(shape, tf); });
96 std::string prefix =
"";
100 if (!filtered_cloud_topic_.empty())
102 filtered_cloud_publisher_ =
103 node_->create_publisher<sensor_msgs::msg::PointCloud2>(prefix + filtered_cloud_topic_, rclcpp::SensorDataQoS());
106 if (point_cloud_subscriber_)
109 point_cloud_subscriber_ =
new message_filters::Subscriber<sensor_msgs::msg::PointCloud2>(node_, point_cloud_topic_,
110#
if RCLCPP_VERSION_GTE(28, 3, 0)
111 rclcpp::SensorDataQoS());
113 rmw_qos_profile_sensor_data);
117 point_cloud_filter_ =
new tf2_ros::MessageFilter<sensor_msgs::msg::PointCloud2>(
119 point_cloud_filter_->registerCallback(
120 [
this](
const sensor_msgs::msg::PointCloud2::ConstSharedPtr& cloud) { cloudMsgCallback(cloud); });
121 RCLCPP_INFO(logger_,
"Listening to '%s' using message filter with target frame '%s'", point_cloud_topic_.c_str(),
122 point_cloud_filter_->getTargetFramesString().c_str());
126 point_cloud_subscriber_->registerCallback(
127 [
this](
const sensor_msgs::msg::PointCloud2::ConstSharedPtr& cloud) { cloudMsgCallback(cloud); });
128 RCLCPP_INFO(logger_,
"Listening to '%s'", point_cloud_topic_.c_str());
134 delete point_cloud_filter_;
135 delete point_cloud_subscriber_;
136 point_cloud_filter_ =
nullptr;
137 point_cloud_subscriber_ =
nullptr;
145 h = shape_mask_->addShape(shape, scale_, padding_);
149 RCLCPP_ERROR(logger_,
"Shape filter not yet initialized!");
157 shape_mask_->removeShape(handle);
160bool PointCloudOctomapUpdater::getShapeTransform(
ShapeHandle h, Eigen::Isometry3d& transform)
const
165 transform = it->second;
171 const Eigen::Vector3d& , std::vector<int>& )
175void PointCloudOctomapUpdater::cloudMsgCallback(
const sensor_msgs::msg::PointCloud2::ConstSharedPtr& cloud_msg)
177 RCLCPP_DEBUG(logger_,
"Received a new point cloud message");
178 rclcpp::Time
start = rclcpp::Clock(RCL_ROS_TIME).now();
180 if (max_update_rate_ > 0)
183 if ((node_->now() - last_update_time_) <= rclcpp::Duration::from_seconds(1.0 / max_update_rate_))
185 last_update_time_ = node_->now();
192 tf2::Stamped<tf2::Transform> map_h_sensor;
195 map_h_sensor.setIdentity();
203 tf2::fromMsg(tf_buffer_->lookupTransform(
monitor_->
getMapFrame(), cloud_msg->header.frame_id,
204 cloud_msg->header.stamp),
207 catch (tf2::TransformException& ex)
209 RCLCPP_ERROR_STREAM(logger_,
"Transform error of sensor data: " << ex.what() <<
"; quitting callback");
218 const tf2::Vector3& sensor_origin_tf = map_h_sensor.getOrigin();
219 octomap::point3d sensor_origin(sensor_origin_tf.getX(), sensor_origin_tf.getY(), sensor_origin_tf.getZ());
220 Eigen::Vector3d sensor_origin_eigen(sensor_origin_tf.getX(), sensor_origin_tf.getY(), sensor_origin_tf.getZ());
226 shape_mask_->maskContainment(*cloud_msg, sensor_origin_eigen, 0.0, max_range_, mask_);
227 updateMask(*cloud_msg, sensor_origin_eigen, mask_);
229 octomap::KeySet free_cells, occupied_cells, model_cells, clip_cells;
230 std::unique_ptr<sensor_msgs::msg::PointCloud2> filtered_cloud;
235 std::unique_ptr<sensor_msgs::PointCloud2Iterator<float>> iter_filtered_x;
236 std::unique_ptr<sensor_msgs::PointCloud2Iterator<float>> iter_filtered_y;
237 std::unique_ptr<sensor_msgs::PointCloud2Iterator<float>> iter_filtered_z;
239 if (!filtered_cloud_topic_.empty())
241 filtered_cloud = std::make_unique<sensor_msgs::msg::PointCloud2>();
242 filtered_cloud->header = cloud_msg->header;
243 sensor_msgs::PointCloud2Modifier pcd_modifier(*filtered_cloud);
244 pcd_modifier.setPointCloud2FieldsByString(1,
"xyz");
245 pcd_modifier.resize(cloud_msg->width * cloud_msg->height);
248 iter_filtered_x = std::make_unique<sensor_msgs::PointCloud2Iterator<float>>(*filtered_cloud,
"x");
249 iter_filtered_y = std::make_unique<sensor_msgs::PointCloud2Iterator<float>>(*filtered_cloud,
"y");
250 iter_filtered_z = std::make_unique<sensor_msgs::PointCloud2Iterator<float>>(*filtered_cloud,
"z");
252 size_t filtered_cloud_size = 0;
260 for (
unsigned int row = 0; row < cloud_msg->height; row += point_subsample_)
262 unsigned int row_c = row * cloud_msg->width;
263 sensor_msgs::PointCloud2ConstIterator<float> pt_iter(*cloud_msg,
"x");
267 for (
unsigned int col = 0; col < cloud_msg->width; col += point_subsample_, pt_iter += point_subsample_)
273 if (!std::isnan(pt_iter[0]) && !std::isnan(pt_iter[1]) && !std::isnan(pt_iter[2]))
280 tf2::Vector3 point_tf = map_h_sensor * tf2::Vector3(pt_iter[0], pt_iter[1], pt_iter[2]);
281 model_cells.insert(
tree_->coordToKey(point_tf.getX(), point_tf.getY(), point_tf.getZ()));
285 tf2::Vector3 clipped_point_tf =
286 map_h_sensor * (tf2::Vector3(pt_iter[0], pt_iter[1], pt_iter[2]).normalize() * max_range_);
288 tree_->coordToKey(clipped_point_tf.getX(), clipped_point_tf.getY(), clipped_point_tf.getZ()));
292 tf2::Vector3 point_tf = map_h_sensor * tf2::Vector3(pt_iter[0], pt_iter[1], pt_iter[2]);
293 occupied_cells.insert(
tree_->coordToKey(point_tf.getX(), point_tf.getY(), point_tf.getZ()));
297 **iter_filtered_x = pt_iter[0];
298 **iter_filtered_y = pt_iter[1];
299 **iter_filtered_z = pt_iter[2];
300 ++filtered_cloud_size;
311 for (
const octomap::OcTreeKey& occupied_cell : occupied_cells)
313 if (
tree_->computeRayKeys(sensor_origin,
tree_->keyToCoord(occupied_cell), key_ray_))
314 free_cells.insert(key_ray_.begin(), key_ray_.end());
318 for (
const octomap::OcTreeKey& model_cell : model_cells)
320 if (
tree_->computeRayKeys(sensor_origin,
tree_->keyToCoord(model_cell), key_ray_))
321 free_cells.insert(key_ray_.begin(), key_ray_.end());
325 for (
const octomap::OcTreeKey& clip_cell : clip_cells)
327 if (
tree_->computeRayKeys(sensor_origin,
tree_->keyToCoord(clip_cell), key_ray_))
328 free_cells.insert(key_ray_.begin(), key_ray_.end());
340 for (
const octomap::OcTreeKey& model_cell : model_cells)
341 occupied_cells.erase(model_cell);
344 for (
const octomap::OcTreeKey& occupied_cell : occupied_cells)
345 free_cells.erase(occupied_cell);
352 for (
const octomap::OcTreeKey& free_cell : free_cells)
353 tree_->updateNode(free_cell, false);
356 for (
const octomap::OcTreeKey& occupied_cell : occupied_cells)
357 tree_->updateNode(occupied_cell, true);
360 const float lg =
tree_->getClampingThresMinLog() -
tree_->getClampingThresMaxLog();
361 for (
const octomap::OcTreeKey& model_cell : model_cells)
362 tree_->updateNode(model_cell, lg);
366 RCLCPP_ERROR(logger_,
"Internal error while updating octree");
368 tree_->unlockWrite();
369 RCLCPP_DEBUG(logger_,
"Processed point cloud in %lf ms", (node_->now() -
start).seconds() * 1000.0);
370 tree_->triggerUpdateCallback();
374 sensor_msgs::PointCloud2Modifier pcd_modifier(*filtered_cloud);
375 pcd_modifier.resize(filtered_cloud_size);
376 filtered_cloud_publisher_->publish(*filtered_cloud);
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.
OccupancyMapMonitor * monitor_
collision_detection::OccMapTreePtr tree_
ShapeTransformCache transform_cache_
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...
PointCloudOctomapUpdater()
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(...
void forgetShape(ShapeHandle handle) override
ShapeHandle excludeShape(const shapes::ShapeConstPtr &shape) override
Main namespace for MoveIt.