15 #include "nav2_collision_monitor/pointcloud.hpp"
19 #include "sensor_msgs/point_cloud2_iterator.hpp"
20 #include "tf2/transform_datatypes.hpp"
21 #include "nav2_ros_common/tf2_factories.hpp"
23 #include "nav2_ros_common/node_utils.hpp"
24 #include "nav2_util/robot_utils.hpp"
26 namespace nav2_collision_monitor
30 const nav2::LifecycleNode::WeakPtr & node,
31 const std::string & source_name,
32 const nav2::TransformBuffer::SharedPtr tf_buffer,
33 const std::string & base_frame_id,
34 const std::string & global_frame_id,
35 const tf2::Duration & transform_tolerance,
36 const rclcpp::Duration & source_timeout,
37 const bool base_shift_correction)
39 node, source_name, tf_buffer, base_frame_id, global_frame_id,
40 transform_tolerance, source_timeout, base_shift_correction),
49 #if RCLCPP_VERSION_GTE(30, 0, 0)
54 auto node =
node_.lock();
55 if (post_set_params_handler_ && node) {
56 node->remove_post_set_parameters_callback(post_set_params_handler_.get());
58 post_set_params_handler_.reset();
59 if (on_set_params_handler_ && node) {
60 node->remove_on_set_parameters_callback(on_set_params_handler_.get());
62 on_set_params_handler_.reset();
70 auto node =
node_.lock();
72 throw std::runtime_error{
"Failed to lock node"};
75 std::string source_topic;
79 #if RCLCPP_VERSION_GTE(30, 0, 0)
80 const point_cloud_transport::TransportHints hint(transport_type_);
81 pct_ = std::make_shared<point_cloud_transport::PointCloudTransport>(*node);
88 data_sub_ = node->create_subscription<sensor_msgs::msg::PointCloud2>(
95 post_set_params_handler_ = node->add_post_set_parameters_callback(
98 this, std::placeholders::_1));
99 on_set_params_handler_ = node->add_on_set_parameters_callback(
102 this, std::placeholders::_1));
108 const rclcpp::Time & curr_time,
109 std::vector<Point> & data)
111 std::lock_guard<std::mutex> lock_reinit(
mutex_);
114 if (
data_ ==
nullptr) {
121 tf2::Transform tf_transform;
126 sensor_msgs::PointCloud2ConstIterator<float> iter_x(*
data_,
"x");
127 sensor_msgs::PointCloud2ConstIterator<float> iter_y(*
data_,
"y");
128 sensor_msgs::PointCloud2ConstIterator<float> iter_z(*
data_,
"z");
130 bool height_present =
false;
131 for (
const auto & field :
data_->fields) {
132 if (field.name ==
"height") {
133 height_present =
true;
138 std::string height_field{
"z"};
140 height_field =
"height";
143 logger_,
"[%s]: 'use_global_height' parameter true but height field not in cloud",
147 sensor_msgs::PointCloud2ConstIterator<float> iter_height(*
data_, height_field);
150 for (; iter_x != iter_x.end(); ++iter_x, ++iter_y, ++iter_z) {
152 tf2::Vector3 p_v3_s(*iter_x, *iter_y, *iter_z);
154 double data_height = *iter_z;
156 data_height = *iter_height;
161 double range = p_v3_s.length();
162 if (range < min_range_) {
166 tf2::Vector3 p_v3_b = tf_transform * p_v3_s;
170 data_height = p_v3_b.z();
174 if (data_height >= min_height_ && data_height <= max_height_) {
175 data.push_back({p_v3_b.x(), p_v3_b.y(), p_v3_b.z(),
source_name_});
183 auto node =
node_.lock();
185 throw std::runtime_error{
"Failed to lock node"};
190 min_height_ = node->declare_or_get_parameter(
source_name_ +
".min_height", 0.05);
191 max_height_ = node->declare_or_get_parameter(
source_name_ +
".max_height", 0.5);
192 min_range_ = node->declare_or_get_parameter(
source_name_ +
".min_range", 0.0);
195 transport_type_ = node->declare_or_get_parameter(
205 const std::vector<rclcpp::Parameter> & parameters)
207 rcl_interfaces::msg::SetParametersResult result;
208 result.successful =
true;
209 for (
const auto & parameter : parameters) {
210 const auto & param_type = parameter.get_type();
211 const auto & param_name = parameter.get_name();
215 if (param_type == ParameterType::PARAMETER_DOUBLE) {
216 if (parameter.as_double() < 0.0) {
218 logger_,
"The value of parameter '%s' is incorrectly set to %f, "
219 "it should be >=0. Ignoring parameter update.",
220 param_name.c_str(), parameter.as_double());
221 result.successful =
false;
229 const std::vector<rclcpp::Parameter> & parameters)
231 std::lock_guard<std::mutex> lock_reinit(
mutex_);
233 for (
auto parameter : parameters) {
234 const auto & param_type = parameter.get_type();
235 const auto & param_name = parameter.get_name();
239 if (param_type == rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE) {
241 min_height_ = parameter.as_double();
242 }
else if (param_name ==
source_name_ +
"." +
"max_height") {
243 max_height_ = parameter.as_double();
244 }
else if (param_name ==
source_name_ +
"." +
"min_range") {
245 min_range_ = parameter.as_double();
247 }
else if (param_type == rcl_interfaces::msg::ParameterType::PARAMETER_BOOL) {
250 }
else if (param_name ==
source_name_ +
"." +
"use_global_height") {
A QoS profile for best-effort sensor data with a history of 10 messages.
bool configure()
Data source configuration routine. Obtains pointcloud related ROS-parameters and creates pointcloud s...
void getParameters(std::string &source_topic)
Getting sensor-specific ROS-parameters.
sensor_msgs::msg::PointCloud2::ConstSharedPtr data_
Latest data obtained from pointcloud.
PointCloud(const nav2::LifecycleNode::WeakPtr &node, const std::string &source_name, const nav2::TransformBuffer::SharedPtr 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.
rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(const std::vector< rclcpp::Parameter > ¶meters)
Validate incoming parameter updates before applying them. This callback is triggered when one or more...
bool getSourceData(const rclcpp::Time &curr_time, std::vector< Point > &data) override
Adds latest data from pointcloud source to the data array.
nav2::Subscription< sensor_msgs::msg::PointCloud2 >::SharedPtr data_sub_
PointCloud data subscriber.
void dataCallback(sensor_msgs::msg::PointCloud2::ConstSharedPtr msg)
PointCloud data callback.
void updateParametersCallback(const std::vector< rclcpp::Parameter > ¶meters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
~PointCloud()
PointCloud destructor.
nav2::LifecycleNode::WeakPtr node_
Collision Monitor node.
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
bool getTransform(const rclcpp::Time &curr_time, const std_msgs::msg::Header &data_header, tf2::Transform &tf_transform) const
Obtain the transform to get data from source frame and time where it was received to the base frame a...
bool enabled_
Whether source is enabled.
void getCommonParameters(std::string &source_topic)
Supporting routine obtaining ROS-parameters common for all data sources.
bool configure()
Source configuration routine.
std::string source_name_
Name of data source.
bool sourceValid(const rclcpp::Time &source_time, const rclcpp::Time &curr_time) const
Checks whether the source data might be considered as valid.
std::mutex mutex_
Dynamic parameters handler.