Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
source.hpp
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 #ifndef NAV2_COLLISION_MONITOR__SOURCE_HPP_
16 #define NAV2_COLLISION_MONITOR__SOURCE_HPP_
17 
18 #include <memory>
19 #include <vector>
20 #include <string>
21 
22 #include "rclcpp/duration.hpp"
23 #include "rclcpp/logger.hpp"
24 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
25 #include "rclcpp/parameter.hpp"
26 #include "rclcpp/time.hpp"
27 
28 #include "tf2/time.hpp"
29 #include "nav2_ros_common/tf2_factories.hpp"
30 
31 #include "nav2_msgs/srv/add_exclusion_zone.hpp"
32 #include "nav2_msgs/srv/remove_exclusion_zone.hpp"
33 
34 #include "nav2_collision_monitor/types.hpp"
35 #include "nav2_collision_monitor/exclusion_zone.hpp"
36 #include "nav2_ros_common/lifecycle_node.hpp"
37 #include "std_msgs/msg/header.hpp"
38 
39 using rcl_interfaces::msg::ParameterType;
40 
41 namespace nav2_collision_monitor
42 {
43 
47 class Source
48 {
49 public:
62  Source(
63  const nav2::LifecycleNode::WeakPtr & node,
64  const std::string & source_name,
65  const nav2::TransformBuffer::SharedPtr tf_buffer,
66  const std::string & base_frame_id,
67  const std::string & global_frame_id,
68  const tf2::Duration & transform_tolerance,
69  const rclcpp::Duration & source_timeout,
70  const bool base_shift_correction);
74  virtual ~Source();
75 
84  bool getData(
85  const rclcpp::Time & curr_time,
86  std::vector<Point> & data);
87 
91  void activate();
92 
96  void deactivate();
97 
101  void publishExclusionZones() const;
102 
107  bool getEnabled() const;
108 
113  std::string getSourceName() const;
114 
119  rclcpp::Duration getSourceTimeout() const;
120 
121 protected:
130  virtual bool getSourceData(
131  const rclcpp::Time & curr_time,
132  std::vector<Point> & data) = 0;
133 
138  bool configure();
139 
144  void getCommonParameters(std::string & source_topic);
145 
152  bool sourceValid(
153  const rclcpp::Time & source_time,
154  const rclcpp::Time & curr_time) const;
155 
160  const std::shared_ptr<rmw_request_id_t> request_header,
161  const std::shared_ptr<nav2_msgs::srv::AddExclusionZone::Request> request,
162  std::shared_ptr<nav2_msgs::srv::AddExclusionZone::Response> response);
163 
168  const std::shared_ptr<rmw_request_id_t> request_header,
169  const std::shared_ptr<nav2_msgs::srv::RemoveExclusionZone::Request> request,
170  std::shared_ptr<nav2_msgs::srv::RemoveExclusionZone::Response> response);
171 
180  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
181  const std::vector<rclcpp::Parameter> & parameters);
182 
189  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
190 
201  bool getTransform(
202  const rclcpp::Time & curr_time,
203  const std_msgs::msg::Header & data_header,
204  tf2::Transform & tf_transform) const;
205 
206  // ----- Variables -----
207 
209  nav2::LifecycleNode::WeakPtr node_;
211  rclcpp::Logger logger_{rclcpp::get_logger("collision_monitor")};
213  mutable std::mutex mutex_;
214  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
215  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
216 
217  // Basic parameters
219  std::string source_name_;
220 
221  // Global variables
223  nav2::TransformBuffer::SharedPtr tf_buffer_;
225  std::string base_frame_id_;
227  std::string global_frame_id_;
229  tf2::Duration transform_tolerance_;
231  rclcpp::Duration source_timeout_;
236  bool enabled_;
238  std::vector<std::shared_ptr<ExclusionZone>> exclusion_zones_;
240  nav2::ServiceServer<nav2_msgs::srv::AddExclusionZone>::SharedPtr add_ez_service_;
242  nav2::ServiceServer<nav2_msgs::srv::RemoveExclusionZone>::SharedPtr remove_ez_service_;
243 }; // class Source
244 
245 } // namespace nav2_collision_monitor
246 
247 #endif // NAV2_COLLISION_MONITOR__SOURCE_HPP_
Basic data source class.
Definition: source.hpp:48
nav2::LifecycleNode::WeakPtr node_
Collision Monitor node.
Definition: source.hpp:209
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
Definition: source.hpp:211
std::vector< std::shared_ptr< ExclusionZone > > exclusion_zones_
Exclusion zones masking out points from this source.
Definition: source.hpp:238
void removeExclusionZoneCallback(const std::shared_ptr< rmw_request_id_t > request_header, const std::shared_ptr< nav2_msgs::srv::RemoveExclusionZone::Request > request, std::shared_ptr< nav2_msgs::srv::RemoveExclusionZone::Response > response)
Service callback to remove an exclusion zone at runtime.
Definition: source.cpp:280
void activate()
Activates the exclusion zone visualization publishers (if any)
Definition: source.cpp:139
std::string base_frame_id_
Robot base frame ID.
Definition: source.hpp:225
virtual ~Source()
Source destructor.
Definition: source.cpp:47
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:325
void addExclusionZoneCallback(const std::shared_ptr< rmw_request_id_t > request_header, const std::shared_ptr< nav2_msgs::srv::AddExclusionZone::Request > request, std::shared_ptr< nav2_msgs::srv::AddExclusionZone::Response > response)
Service callback to add an exclusion zone at runtime.
Definition: source.cpp:241
bool enabled_
Whether source is enabled.
Definition: source.hpp:236
virtual bool getSourceData(const rclcpp::Time &curr_time, std::vector< Point > &data)=0
Adds latest data from source to the data array. Pure virtual method implemented by each concrete sour...
bool getEnabled() const
Obtains source enabled state.
Definition: source.cpp:198
std::string getSourceName() const
Obtains the name of the data source.
Definition: source.cpp:204
Source(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)
Source constructor.
Definition: source.cpp:31
nav2::ServiceServer< nav2_msgs::srv::AddExclusionZone >::SharedPtr add_ez_service_
Service to add an exclusion zone at runtime.
Definition: source.hpp:240
tf2::Duration transform_tolerance_
Transform tolerance.
Definition: source.hpp:229
void getCommonParameters(std::string &source_topic)
Supporting routine obtaining ROS-parameters common for all data sources.
Definition: source.cpp:160
bool getData(const rclcpp::Time &curr_time, std::vector< Point > &data)
Adds latest data from source to the data array, then removes any points falling inside an enabled exc...
Definition: source.cpp:112
bool configure()
Source configuration routine.
Definition: source.cpp:62
void updateParametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
Definition: source.cpp:222
void publishExclusionZones() const
Publishes the source's exclusion zones for visualization.
Definition: source.cpp:153
std::string global_frame_id_
Global frame ID for correct transform calculation.
Definition: source.hpp:227
void deactivate()
Deactivates the exclusion zone visualization publishers (if any)
Definition: source.cpp:146
std::string source_name_
Name of data source.
Definition: source.hpp:219
nav2::ServiceServer< nav2_msgs::srv::RemoveExclusionZone >::SharedPtr remove_ez_service_
Service to remove an exclusion zone at runtime.
Definition: source.hpp:242
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:179
bool base_shift_correction_
Whether to correct source data towards to base frame movement, considering the difference between cur...
Definition: source.hpp:234
std::mutex mutex_
Dynamic parameters handler.
Definition: source.hpp:213
rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(const std::vector< rclcpp::Parameter > &parameters)
Validate incoming parameter updates before applying them. This callback is triggered when one or more...
Definition: source.cpp:214
rclcpp::Duration source_timeout_
Maximum time interval in which data is considered valid.
Definition: source.hpp:231
nav2::TransformBuffer::SharedPtr tf_buffer_
TF buffer.
Definition: source.hpp:223
rclcpp::Duration getSourceTimeout() const
Obtains the source_timeout parameter of the data source.
Definition: source.cpp:209