Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
pointcloud.cpp
1 // Copyright (c) 2022 Samsung R&D Institute Russia
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "nav2_collision_monitor/pointcloud.hpp"
16 
17 #include <functional>
18 
19 #include "sensor_msgs/point_cloud2_iterator.hpp"
20 
21 #include "nav2_util/node_utils.hpp"
22 
23 namespace nav2_collision_monitor
24 {
25 
27  const nav2_util::LifecycleNode::WeakPtr & node,
28  const std::string & source_name,
29  const std::shared_ptr<tf2_ros::Buffer> tf_buffer,
30  const std::string & base_frame_id,
31  const std::string & global_frame_id,
32  const tf2::Duration & transform_tolerance,
33  const rclcpp::Duration & source_timeout,
34  const bool base_shift_correction)
35 : Source(
36  node, source_name, tf_buffer, base_frame_id, global_frame_id,
37  transform_tolerance, source_timeout, base_shift_correction),
38  data_(nullptr)
39 {
40  RCLCPP_INFO(logger_, "[%s]: Creating PointCloud", source_name_.c_str());
41 }
42 
44 {
45  RCLCPP_INFO(logger_, "[%s]: Destroying PointCloud", source_name_.c_str());
46  data_sub_.reset();
47 }
48 
50 {
52  auto node = node_.lock();
53  if (!node) {
54  throw std::runtime_error{"Failed to lock node"};
55  }
56 
57  std::string source_topic;
58 
59  getParameters(source_topic);
60 
61  rclcpp::QoS pointcloud_qos = rclcpp::SensorDataQoS(); // set to default
62  data_sub_ = node->create_subscription<sensor_msgs::msg::PointCloud2>(
63  source_topic, pointcloud_qos,
64  std::bind(&PointCloud::dataCallback, this, std::placeholders::_1));
65 }
66 
68  const rclcpp::Time & curr_time,
69  std::vector<Point> & data) const
70 {
71  // Ignore data from the source if it is not being published yet or
72  // not published for a long time
73  if (data_ == nullptr) {
74  return;
75  }
76  if (!sourceValid(data_->header.stamp, curr_time)) {
77  return;
78  }
79 
80  tf2::Transform tf_transform;
82  // Obtaining the transform to get data from source frame and time where it was received
83  // to the base frame and current time
84  if (
85  !nav2_util::getTransform(
86  data_->header.frame_id, data_->header.stamp,
87  base_frame_id_, curr_time, global_frame_id_,
88  transform_tolerance_, tf_buffer_, tf_transform))
89  {
90  return;
91  }
92  } else {
93  // Obtaining the transform to get data from source frame to base frame without time shift
94  // considered. Less accurate but much more faster option not dependent on state estimation
95  // frames.
96  if (
97  !nav2_util::getTransform(
98  data_->header.frame_id, base_frame_id_,
99  transform_tolerance_, tf_buffer_, tf_transform))
100  {
101  return;
102  }
103  }
104 
105  sensor_msgs::PointCloud2ConstIterator<float> iter_x(*data_, "x");
106  sensor_msgs::PointCloud2ConstIterator<float> iter_y(*data_, "y");
107  sensor_msgs::PointCloud2ConstIterator<float> iter_z(*data_, "z");
108 
109  // Refill data array with PointCloud points in base frame
110  for (; iter_x != iter_x.end(); ++iter_x, ++iter_y, ++iter_z) {
111  // Transform point coordinates from source frame -> to base frame
112  tf2::Vector3 p_v3_s(*iter_x, *iter_y, *iter_z);
113  tf2::Vector3 p_v3_b = tf_transform * p_v3_s;
114 
115  // Refill data array
116  if (p_v3_b.z() >= min_height_ && p_v3_b.z() <= max_height_) {
117  data.push_back({p_v3_b.x(), p_v3_b.y()});
118  }
119  }
120 }
121 
122 void PointCloud::getParameters(std::string & source_topic)
123 {
124  auto node = node_.lock();
125  if (!node) {
126  throw std::runtime_error{"Failed to lock node"};
127  }
128 
129  getCommonParameters(source_topic);
130 
131  nav2_util::declare_parameter_if_not_declared(
132  node, source_name_ + ".min_height", rclcpp::ParameterValue(0.05));
133  min_height_ = node->get_parameter(source_name_ + ".min_height").as_double();
134  nav2_util::declare_parameter_if_not_declared(
135  node, source_name_ + ".max_height", rclcpp::ParameterValue(0.5));
136  max_height_ = node->get_parameter(source_name_ + ".max_height").as_double();
137 }
138 
139 void PointCloud::dataCallback(sensor_msgs::msg::PointCloud2::ConstSharedPtr msg)
140 {
141  data_ = msg;
142 }
143 
144 } // namespace nav2_collision_monitor
void getParameters(std::string &source_topic)
Getting sensor-specific ROS-parameters.
Definition: pointcloud.cpp:122
sensor_msgs::msg::PointCloud2::ConstSharedPtr data_
Latest data obtained from pointcloud.
Definition: pointcloud.hpp:100
void configure()
Data source configuration routine. Obtains pointcloud related ROS-parameters and creates pointcloud s...
Definition: pointcloud.cpp:49
void dataCallback(sensor_msgs::msg::PointCloud2::ConstSharedPtr msg)
PointCloud data callback.
Definition: pointcloud.cpp:139
rclcpp::Subscription< sensor_msgs::msg::PointCloud2 >::SharedPtr data_sub_
PointCloud data subscriber.
Definition: pointcloud.hpp:94
PointCloud(const nav2_util::LifecycleNode::WeakPtr &node, const std::string &source_name, const std::shared_ptr< tf2_ros::Buffer > tf_buffer, const std::string &base_frame_id, const std::string &global_frame_id, const tf2::Duration &transform_tolerance, const rclcpp::Duration &source_timeout, const bool base_shift_correction)
PointCloud constructor.
Definition: pointcloud.cpp:26
void getData(const rclcpp::Time &curr_time, std::vector< Point > &data) const
Adds latest data from pointcloud source to the data array.
Definition: pointcloud.cpp:67
~PointCloud()
PointCloud destructor.
Definition: pointcloud.cpp:43
Basic data source class.
Definition: source.hpp:38
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
Definition: source.hpp:118
std::string base_frame_id_
Robot base frame ID.
Definition: source.hpp:130
tf2::Duration transform_tolerance_
Transform tolerance.
Definition: source.hpp:134
void getCommonParameters(std::string &source_topic)
Supporting routine obtaining ROS-parameters common for all data sources.
Definition: source.cpp:60
bool configure()
Source configuration routine.
Definition: source.cpp:49
std::string global_frame_id_
Global frame ID for correct transform calculation.
Definition: source.hpp:132
std::string source_name_
Name of data source.
Definition: source.hpp:124
nav2_util::LifecycleNode::WeakPtr node_
Collision Monitor node.
Definition: source.hpp:116
std::shared_ptr< tf2_ros::Buffer > tf_buffer_
TF buffer.
Definition: source.hpp:128
bool sourceValid(const rclcpp::Time &source_time, const rclcpp::Time &curr_time) const
Checks whether the source data might be considered as valid.
Definition: source.cpp:77
bool base_shift_correction_
Whether to correct source data towards to base frame movement, considering the difference between cur...
Definition: source.hpp:139