Nav2 Navigation Stack - lyrical  lyrical
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.hpp"
21 #include "nav2_ros_common/tf2_factories.hpp"
22 
23 #include "nav2_util/robot_utils.hpp"
24 
25 namespace nav2_collision_monitor
26 {
27 
29  const nav2::LifecycleNode::WeakPtr & node,
30  const std::string & source_name,
31  const nav2::TransformBuffer::SharedPtr tf_buffer,
32  const std::string & base_frame_id,
33  const std::string & global_frame_id,
34  const tf2::Duration & transform_tolerance,
35  const rclcpp::Duration & source_timeout,
36  const bool base_shift_correction)
37 : Source(
38  node, source_name, tf_buffer, base_frame_id, global_frame_id,
39  transform_tolerance, source_timeout, base_shift_correction),
40  data_(nullptr)
41 {
42  RCLCPP_INFO(logger_, "[%s]: Creating Scan", source_name_.c_str());
43 }
44 
46 {
47  RCLCPP_INFO(logger_, "[%s]: Destroying Scan", source_name_.c_str());
48  data_sub_.reset();
49 }
50 
52 {
53  if (!Source::configure()) {
54  return false;
55  }
56  auto node = node_.lock();
57  if (!node) {
58  throw std::runtime_error{"Failed to lock node"};
59  }
60 
61  std::string source_topic;
62 
63  // Laser scanner has no own parameters
64  getCommonParameters(source_topic);
65 
66  data_sub_ = node->create_subscription<sensor_msgs::msg::LaserScan>(
67  source_topic,
68  std::bind(&Scan::dataCallback, this, std::placeholders::_1),
70 
71  return true;
72 }
73 
75  const rclcpp::Time & curr_time,
76  std::vector<Point> & data)
77 {
78  // Ignore data from the source if it is not being published yet or
79  // not being published for a long time
80  if (data_ == nullptr) {
81  return false;
82  }
83  if (!sourceValid(data_->header.stamp, curr_time)) {
84  return false;
85  }
86 
87  tf2::Transform tf_transform;
88  if (!getTransform(curr_time, data_->header, tf_transform)) {
89  return false;
90  }
91 
92  // Calculate poses and refill data array
93  float angle = data_->angle_min;
94  for (size_t i = 0; i < data_->ranges.size(); i++) {
95  if (data_->ranges[i] >= data_->range_min && data_->ranges[i] <= data_->range_max) {
96  // Transform point coordinates from source frame -> to base frame
97  tf2::Vector3 p_v3_s(
98  data_->ranges[i] * std::cos(angle),
99  data_->ranges[i] * std::sin(angle),
100  0.0);
101  tf2::Vector3 p_v3_b = tf_transform * p_v3_s;
102 
103  // Refill data array
104  data.push_back({p_v3_b.x(), p_v3_b.y(), p_v3_b.z(), source_name_});
105  }
106  angle += data_->angle_increment;
107  }
108  return true;
109 }
110 
111 void Scan::dataCallback(sensor_msgs::msg::LaserScan::ConstSharedPtr msg)
112 {
113  data_ = msg;
114 }
115 
116 } // namespace nav2_collision_monitor
A QoS profile for best-effort sensor data with a history of 10 messages.
Scan(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)
Scan constructor.
Definition: scan.cpp:28
nav2::Subscription< sensor_msgs::msg::LaserScan >::SharedPtr data_sub_
Laser scanner data subscriber.
Definition: scan.hpp:90
bool configure()
Data source configuration routine. Obtains ROS-parameters and creates laser scanner subscriber.
Definition: scan.cpp:51
void dataCallback(sensor_msgs::msg::LaserScan::ConstSharedPtr msg)
Laser scanner data callback.
Definition: scan.cpp:111
sensor_msgs::msg::LaserScan::ConstSharedPtr data_
Latest data obtained from laser scanner.
Definition: scan.hpp:93
~Scan()
Scan destructor.
Definition: scan.cpp:45
bool getSourceData(const rclcpp::Time &curr_time, std::vector< Point > &data) override
Adds latest data from laser scanner to the data array.
Definition: scan.cpp:74
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
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 getCommonParameters(std::string &source_topic)
Supporting routine obtaining ROS-parameters common for all data sources.
Definition: source.cpp:160
bool configure()
Source configuration routine.
Definition: source.cpp:62
std::string source_name_
Name of data source.
Definition: source.hpp:215
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