Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
scan.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/scan.hpp"
16 
17 #include <cmath>
18 #include <functional>
19 
20 #include "tf2/transform_datatypes.h"
21 
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 : Source(
37  node, source_name, tf_buffer, base_frame_id, global_frame_id,
38  transform_tolerance, source_timeout, base_shift_correction),
39  data_(nullptr)
40 {
41  RCLCPP_INFO(logger_, "[%s]: Creating Scan", source_name_.c_str());
42 }
43 
45 {
46  RCLCPP_INFO(logger_, "[%s]: Destroying Scan", source_name_.c_str());
47  data_sub_.reset();
48 }
49 
51 {
53  auto node = node_.lock();
54  if (!node) {
55  throw std::runtime_error{"Failed to lock node"};
56  }
57 
58  std::string source_topic;
59 
60  // Laser scanner has no own parameters
61  getCommonParameters(source_topic);
62 
63  rclcpp::QoS scan_qos = rclcpp::SensorDataQoS(); // set to default
64  data_sub_ = node->create_subscription<sensor_msgs::msg::LaserScan>(
65  source_topic, scan_qos,
66  std::bind(&Scan::dataCallback, this, std::placeholders::_1));
67 }
68 
70  const rclcpp::Time & curr_time,
71  std::vector<Point> & data)
72 {
73  // Ignore data from the source if it is not being published yet or
74  // not being published for a long time
75  if (data_ == nullptr) {
76  return false;
77  }
78  if (!sourceValid(data_->header.stamp, curr_time)) {
79  return false;
80  }
81 
82  tf2::Transform tf_transform;
83  if (!getTransform(curr_time, data_->header, tf_transform)) {
84  return false;
85  }
86 
87  // Calculate poses and refill data array
88  float angle = data_->angle_min;
89  for (size_t i = 0; i < data_->ranges.size(); i++) {
90  if (data_->ranges[i] >= data_->range_min && data_->ranges[i] <= data_->range_max) {
91  // Transform point coordinates from source frame -> to base frame
92  tf2::Vector3 p_v3_s(
93  data_->ranges[i] * std::cos(angle),
94  data_->ranges[i] * std::sin(angle),
95  0.0);
96  tf2::Vector3 p_v3_b = tf_transform * p_v3_s;
97 
98  // Refill data array
99  data.push_back({p_v3_b.x(), p_v3_b.y()});
100  }
101  angle += data_->angle_increment;
102  }
103  return true;
104 }
105 
106 void Scan::dataCallback(sensor_msgs::msg::LaserScan::ConstSharedPtr msg)
107 {
108  data_ = msg;
109 }
110 
111 } // namespace nav2_collision_monitor
bool getData(const rclcpp::Time &curr_time, std::vector< Point > &data)
Adds latest data from laser scanner to the data array.
Definition: scan.cpp:69
rclcpp::Subscription< sensor_msgs::msg::LaserScan >::SharedPtr data_sub_
Laser scanner data subscriber.
Definition: scan.hpp:88
void dataCallback(sensor_msgs::msg::LaserScan::ConstSharedPtr msg)
Laser scanner data callback.
Definition: scan.cpp:106
sensor_msgs::msg::LaserScan::ConstSharedPtr data_
Latest data obtained from laser scanner.
Definition: scan.hpp:91
Scan(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)
Scan constructor.
Definition: scan.cpp:27
~Scan()
Scan destructor.
Definition: scan.cpp:44
void configure()
Data source configuration routine. Obtains ROS-parameters and creates laser scanner subscriber.
Definition: scan.cpp:50
Basic data source class.
Definition: source.hpp:38
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
Definition: source.hpp:146
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:135
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
std::string source_name_
Name of data source.
Definition: source.hpp:152
nav2_util::LifecycleNode::WeakPtr node_
Collision Monitor node.
Definition: source.hpp:144
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