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/rclcpp.hpp"
23 
24 #include "tf2/time.hpp"
25 #include "nav2_ros_common/tf2_factories.hpp"
26 
27 #include "nav2_msgs/srv/add_exclusion_zone.hpp"
28 #include "nav2_msgs/srv/remove_exclusion_zone.hpp"
29 
30 #include "nav2_collision_monitor/types.hpp"
31 #include "nav2_collision_monitor/exclusion_zone.hpp"
32 #include "nav2_ros_common/lifecycle_node.hpp"
33 #include "std_msgs/msg/header.hpp"
34 
35 using rcl_interfaces::msg::ParameterType;
36 
37 namespace nav2_collision_monitor
38 {
39 
43 class Source
44 {
45 public:
58  Source(
59  const nav2::LifecycleNode::WeakPtr & node,
60  const std::string & source_name,
61  const nav2::TransformBuffer::SharedPtr tf_buffer,
62  const std::string & base_frame_id,
63  const std::string & global_frame_id,
64  const tf2::Duration & transform_tolerance,
65  const rclcpp::Duration & source_timeout,
66  const bool base_shift_correction);
70  virtual ~Source();
71 
80  bool getData(
81  const rclcpp::Time & curr_time,
82  std::vector<Point> & data);
83 
87  void activate();
88 
92  void deactivate();
93 
97  void publishExclusionZones() const;
98 
103  bool getEnabled() const;
104 
109  std::string getSourceName() const;
110 
115  rclcpp::Duration getSourceTimeout() const;
116 
117 protected:
126  virtual bool getSourceData(
127  const rclcpp::Time & curr_time,
128  std::vector<Point> & data) = 0;
129 
134  bool configure();
135 
140  void getCommonParameters(std::string & source_topic);
141 
148  bool sourceValid(
149  const rclcpp::Time & source_time,
150  const rclcpp::Time & curr_time) const;
151 
156  const std::shared_ptr<rmw_request_id_t> request_header,
157  const std::shared_ptr<nav2_msgs::srv::AddExclusionZone::Request> request,
158  std::shared_ptr<nav2_msgs::srv::AddExclusionZone::Response> response);
159 
164  const std::shared_ptr<rmw_request_id_t> request_header,
165  const std::shared_ptr<nav2_msgs::srv::RemoveExclusionZone::Request> request,
166  std::shared_ptr<nav2_msgs::srv::RemoveExclusionZone::Response> response);
167 
176  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
177  const std::vector<rclcpp::Parameter> & parameters);
178 
185  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
186 
197  bool getTransform(
198  const rclcpp::Time & curr_time,
199  const std_msgs::msg::Header & data_header,
200  tf2::Transform & tf_transform) const;
201 
202  // ----- Variables -----
203 
205  nav2::LifecycleNode::WeakPtr node_;
207  rclcpp::Logger logger_{rclcpp::get_logger("collision_monitor")};
209  mutable std::mutex mutex_;
210  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
211  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
212 
213  // Basic parameters
215  std::string source_name_;
216 
217  // Global variables
219  nav2::TransformBuffer::SharedPtr tf_buffer_;
221  std::string base_frame_id_;
223  std::string global_frame_id_;
225  tf2::Duration transform_tolerance_;
227  rclcpp::Duration source_timeout_;
232  bool enabled_;
234  std::vector<std::shared_ptr<ExclusionZone>> exclusion_zones_;
236  nav2::ServiceServer<nav2_msgs::srv::AddExclusionZone>::SharedPtr add_ez_service_;
238  nav2::ServiceServer<nav2_msgs::srv::RemoveExclusionZone>::SharedPtr remove_ez_service_;
239 }; // class Source
240 
241 } // namespace nav2_collision_monitor
242 
243 #endif // NAV2_COLLISION_MONITOR__SOURCE_HPP_
Basic data source class.
Definition: source.hpp:44
nav2::LifecycleNode::WeakPtr node_
Collision Monitor node.
Definition: source.hpp:205
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
Definition: source.hpp:207
std::vector< std::shared_ptr< ExclusionZone > > exclusion_zones_
Exclusion zones masking out points from this source.
Definition: source.hpp:234
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:221
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:232
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:236
tf2::Duration transform_tolerance_
Transform tolerance.
Definition: source.hpp:225
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:223
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:215
nav2::ServiceServer< nav2_msgs::srv::RemoveExclusionZone >::SharedPtr remove_ez_service_
Service to remove an exclusion zone at runtime.
Definition: source.hpp:238
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:230
std::mutex mutex_
Dynamic parameters handler.
Definition: source.hpp:209
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:227
nav2::TransformBuffer::SharedPtr tf_buffer_
TF buffer.
Definition: source.hpp:219
rclcpp::Duration getSourceTimeout() const
Obtains the source_timeout parameter of the data source.
Definition: source.cpp:209