Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
circle.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/circle.hpp"
16 
17 #include <math.h>
18 #include <cmath>
19 #include <exception>
20 
21 #include "nav2_util/node_utils.hpp"
22 
23 namespace nav2_collision_monitor
24 {
25 
27  const nav2_util::LifecycleNode::WeakPtr & node,
28  const std::string & polygon_name,
29  const std::shared_ptr<tf2_ros::Buffer> tf_buffer,
30  const std::string & base_frame_id,
31  const tf2::Duration & transform_tolerance)
32 : Polygon::Polygon(node, polygon_name, tf_buffer, base_frame_id, transform_tolerance)
33 {
34  RCLCPP_INFO(logger_, "[%s]: Creating Circle", polygon_name_.c_str());
35 }
36 
38 {
39  RCLCPP_INFO(logger_, "[%s]: Destroying Circle", polygon_name_.c_str());
40 }
41 
42 void Circle::getPolygon(std::vector<Point> & poly) const
43 {
44  // Number of polygon points. More edges means better approximation.
45  const double polygon_edges = 16;
46  // Increment of angle during points position calculation
47  double angle_increment = 2 * M_PI / polygon_edges;
48 
49  // Clear polygon before filling
50  poly.clear();
51 
52  // Making new polygon looks like a circle
53  Point p;
54  for (double angle = 0.0; angle < 2 * M_PI; angle += angle_increment) {
55  p.x = radius_ * std::cos(angle);
56  p.y = radius_ * std::sin(angle);
57  poly.push_back(p);
58  }
59 }
60 
61 int Circle::getPointsInside(const std::vector<Point> & points) const
62 {
63  int num = 0;
64  for (Point point : points) {
65  if (point.x * point.x + point.y * point.y < radius_squared_) {
66  num++;
67  }
68  }
69 
70  return num;
71 }
72 
73 bool Circle::getParameters(std::string & polygon_pub_topic, std::string & footprint_topic)
74 {
75  auto node = node_.lock();
76  if (!node) {
77  throw std::runtime_error{"Failed to lock node"};
78  }
79 
80  if (!getCommonParameters(polygon_pub_topic)) {
81  return false;
82  }
83 
84  // There is no footprint subscription for the Circle. Thus, set string as empty.
85  footprint_topic.clear();
86 
87  try {
88  // Leave it not initialized: the will cause an error if it will not set
89  nav2_util::declare_parameter_if_not_declared(
90  node, polygon_name_ + ".radius", rclcpp::PARAMETER_DOUBLE);
91  radius_ = node->get_parameter(polygon_name_ + ".radius").as_double();
93  } catch (const std::exception & ex) {
94  RCLCPP_ERROR(
95  logger_,
96  "[%s]: Error while getting circle parameters: %s",
97  polygon_name_.c_str(), ex.what());
98  return false;
99  }
100 
101  return true;
102 }
103 
104 } // namespace nav2_collision_monitor
int getPointsInside(const std::vector< Point > &points) const override
Gets number of points inside circle.
Definition: circle.cpp:61
void getPolygon(std::vector< Point > &poly) const override
Gets polygon points, approximated to the circle. To be used in visualization purposes.
Definition: circle.cpp:42
double radius_squared_
(radius * radius) value. Stored for optimization.
Definition: circle.hpp:84
bool getParameters(std::string &polygon_pub_topic, std::string &footprint_topic) override
Supporting routine obtaining polygon-specific ROS-parameters.
Definition: circle.cpp:73
~Circle()
Circle class destructor.
Definition: circle.cpp:37
double radius_
Radius of the circle.
Definition: circle.hpp:82
Circle(const nav2_util::LifecycleNode::WeakPtr &node, const std::string &polygon_name, const std::shared_ptr< tf2_ros::Buffer > tf_buffer, const std::string &base_frame_id, const tf2::Duration &transform_tolerance)
Circle class constructor.
Definition: circle.cpp:26
Basic polygon shape class. For STOP/SLOWDOWN model it represents zone around the robot while for APPR...
Definition: polygon.hpp:43
nav2_util::LifecycleNode::WeakPtr node_
Collision Monitor node.
Definition: polygon.hpp:182
bool getCommonParameters(std::string &polygon_pub_topic)
Supporting routine obtaining ROS-parameters common for all shapes.
Definition: polygon.cpp:232
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
Definition: polygon.hpp:184
std::string polygon_name_
Name of polygon.
Definition: polygon.hpp:190