Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
source.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/source.hpp"
16 
17 #include <exception>
18 
19 #include "geometry_msgs/msg/transform_stamped.hpp"
20 
21 #include "nav2_util/node_utils.hpp"
22 #include "nav2_util/robot_utils.hpp"
23 
24 namespace nav2_collision_monitor
25 {
26 
28  const nav2_util::LifecycleNode::WeakPtr & node,
29  const std::string & source_name,
30  const std::shared_ptr<tf2_ros::Buffer> tf_buffer,
31  const std::string & base_frame_id,
32  const std::string & global_frame_id,
33  const tf2::Duration & transform_tolerance,
34  const rclcpp::Duration & source_timeout,
35  const bool base_shift_correction)
36 : node_(node), source_name_(source_name), tf_buffer_(tf_buffer),
37  base_frame_id_(base_frame_id), global_frame_id_(global_frame_id),
38  transform_tolerance_(transform_tolerance), source_timeout_(source_timeout),
39  base_shift_correction_(base_shift_correction)
40 {
41 }
42 
44 {
45 }
46 
48 {
49  auto node = node_.lock();
50 
51  // Add callback for dynamic parameters
52  dyn_params_handler_ = node->add_on_set_parameters_callback(
53  std::bind(&Source::dynamicParametersCallback, this, std::placeholders::_1));
54 
55  return true;
56 }
57 
58 void Source::getCommonParameters(std::string & source_topic)
59 {
60  auto node = node_.lock();
61  if (!node) {
62  throw std::runtime_error{"Failed to lock node"};
63  }
64 
65  nav2_util::declare_parameter_if_not_declared(
66  node, source_name_ + ".topic",
67  rclcpp::ParameterValue("scan")); // Set default topic for laser scanner
68  source_topic = node->get_parameter(source_name_ + ".topic").as_string();
69 
70  nav2_util::declare_parameter_if_not_declared(
71  node, source_name_ + ".enabled", rclcpp::ParameterValue(true));
72  enabled_ = node->get_parameter(source_name_ + ".enabled").as_bool();
73 
74  nav2_util::declare_parameter_if_not_declared(
75  node, source_name_ + ".source_timeout",
76  rclcpp::ParameterValue(source_timeout_.seconds())); // node source_timeout by default
77  source_timeout_ = rclcpp::Duration::from_seconds(
78  node->get_parameter(source_name_ + ".source_timeout").as_double());
79 }
80 
82  const rclcpp::Time & source_time,
83  const rclcpp::Time & curr_time) const
84 {
85  // Source is considered as not valid, if latest received data timestamp is earlier
86  // than current time by source_timeout_ interval
87  const rclcpp::Duration dt = curr_time - source_time;
88  if (source_timeout_.seconds() != 0.0 && dt > source_timeout_) {
89  RCLCPP_WARN(
90  logger_,
91  "[%s]: Latest source and current collision monitor node timestamps differ on %f seconds. "
92  "Ignoring the source.",
93  source_name_.c_str(), dt.seconds());
94  return false;
95  }
96 
97  return true;
98 }
99 
100 bool Source::getEnabled() const
101 {
102  return enabled_;
103 }
104 
105 std::string Source::getSourceName() const
106 {
107  return source_name_;
108 }
109 
110 rclcpp::Duration Source::getSourceTimeout() const
111 {
112  return source_timeout_;
113 }
114 
115 rcl_interfaces::msg::SetParametersResult
117  std::vector<rclcpp::Parameter> parameters)
118 {
119  rcl_interfaces::msg::SetParametersResult result;
120 
121  for (auto parameter : parameters) {
122  const auto & param_type = parameter.get_type();
123  const auto & param_name = parameter.get_name();
124  if(param_name.find(source_name_ + ".") != 0) {
125  continue;
126  }
127  if (param_type == rcl_interfaces::msg::ParameterType::PARAMETER_BOOL) {
128  if (param_name == source_name_ + "." + "enabled") {
129  enabled_ = parameter.as_bool();
130  }
131  }
132  }
133  result.successful = true;
134  return result;
135 }
136 
138  const rclcpp::Time & curr_time,
139  const std_msgs::msg::Header & data_header,
140  tf2::Transform & tf_transform) const
141 {
143  if (
144  !nav2_util::getTransform(
145  data_header.frame_id, data_header.stamp,
146  base_frame_id_, curr_time, global_frame_id_,
147  transform_tolerance_, tf_buffer_, tf_transform))
148  {
149  return false;
150  }
151  } else {
152  if (
153  !nav2_util::getTransform(
154  data_header.frame_id, base_frame_id_,
155  transform_tolerance_, tf_buffer_, tf_transform))
156  {
157  return false;
158  }
159  }
160  return true;
161 }
162 
163 } // namespace nav2_collision_monitor
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
Definition: source.hpp:146
std::string base_frame_id_
Robot base frame ID.
Definition: source.hpp:158
virtual ~Source()
Source destructor.
Definition: source.cpp:43
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...
Definition: source.cpp:137
bool enabled_
Whether source is enabled.
Definition: source.hpp:169
bool getEnabled() const
Obtains source enabled state.
Definition: source.cpp:100
std::string getSourceName() const
Obtains the name of the data source.
Definition: source.cpp:105
tf2::Duration transform_tolerance_
Transform tolerance.
Definition: source.hpp:162
void getCommonParameters(std::string &source_topic)
Supporting routine obtaining ROS-parameters common for all data sources.
Definition: source.cpp:58
bool configure()
Source configuration routine.
Definition: source.cpp:47
Source(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)
Source constructor.
Definition: source.cpp:27
std::string global_frame_id_
Global frame ID for correct transform calculation.
Definition: source.hpp:160
std::string source_name_
Name of data source.
Definition: source.hpp:152
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(std::vector< rclcpp::Parameter > parameters)
Callback executed when a parameter change is detected.
Definition: source.cpp:116
nav2_util::LifecycleNode::WeakPtr node_
Collision Monitor node.
Definition: source.hpp:144
std::shared_ptr< tf2_ros::Buffer > tf_buffer_
TF buffer.
Definition: source.hpp:156
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:81
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_
Dynamic parameters handler.
Definition: source.hpp:148
bool base_shift_correction_
Whether to correct source data towards to base frame movement, considering the difference between cur...
Definition: source.hpp:167
rclcpp::Duration source_timeout_
Maximum time interval in which data is considered valid.
Definition: source.hpp:164
rclcpp::Duration getSourceTimeout() const
Obtains the source_timeout parameter of the data source.
Definition: source.cpp:110