15 #include "nav2_collision_monitor/velocity_polygon.hpp"
17 #include "nav2_ros_common/node_utils.hpp"
18 #include "nav2_ros_common/tf2_factories.hpp"
20 namespace nav2_collision_monitor
24 const nav2::LifecycleNode::WeakPtr & node,
const std::string & polygon_name,
25 const nav2::TransformBuffer::SharedPtr tf_buffer,
const std::string & base_frame_id,
26 const tf2::Duration & transform_tolerance)
27 :
Polygon::
Polygon(node, polygon_name, tf_buffer, base_frame_id, transform_tolerance)
38 std::string & polygon_sub_topic,
39 std::string & polygon_pub_topic,
40 std::string & footprint_topic)
42 auto node =
node_.lock();
44 throw std::runtime_error{
"Failed to lock node"};
46 clock_ = node->get_clock();
54 std::vector<std::string> velocity_polygons =
55 node->declare_or_get_parameter<std::vector<std::string>>(
62 for (std::string velocity_polygon_name : velocity_polygons) {
64 std::vector<Point> poly;
65 std::string poly_string =
66 node->declare_or_get_parameter<std::string>(
74 double linear_min = node->declare_or_get_parameter<
double>(
78 double linear_max = node->declare_or_get_parameter<
double>(
82 double theta_min = node->declare_or_get_parameter<
double>(
86 double theta_max = node->declare_or_get_parameter<
double>(
90 double direction_end_angle = 0.0;
91 double direction_start_angle = 0.0;
93 direction_end_angle = node->declare_or_get_parameter(
94 polygon_name_ +
"." + velocity_polygon_name +
".direction_end_angle", M_PI);
96 direction_start_angle = node->declare_or_get_parameter(
97 polygon_name_ +
"." + velocity_polygon_name +
".direction_start_angle", -M_PI);
101 poly, velocity_polygon_name, linear_min, linear_max, theta_min,
102 theta_max, direction_end_angle, direction_start_angle};
105 }
catch (
const std::exception & ex) {
117 if (
isInRange(cmd_vel_in, sub_polygon)) {
119 poly_ = sub_polygon.poly_;
124 geometry_msgs::msg::Point32 p_s;
128 polygon_.polygon.points.push_back(p_s);
135 RCLCPP_WARN_THROTTLE(
137 "Velocity is not covered by any of the velocity polygons. x: %.3f y: %.3f tw: %.3f ",
138 cmd_vel_in.x, cmd_vel_in.y, cmd_vel_in.tw);
147 (cmd_vel_in.tw <= sub_polygon.theta_max_ &&
148 cmd_vel_in.tw >= sub_polygon.theta_min_);
152 const double magnitude = std::hypot(cmd_vel_in.x, cmd_vel_in.y);
154 const double direction = (magnitude > 0.0) ? std::atan2(cmd_vel_in.y, cmd_vel_in.x) : 0.0;
157 in_range &= (magnitude <= sub_polygon.linear_max_ &&
158 magnitude >= sub_polygon.linear_min_);
161 if (sub_polygon.direction_start_angle_ <= sub_polygon.direction_end_angle_) {
163 (direction >= sub_polygon.direction_start_angle_ &&
164 direction <= sub_polygon.direction_end_angle_);
167 (direction >= sub_polygon.direction_start_angle_ ||
168 direction <= sub_polygon.direction_end_angle_);
173 (cmd_vel_in.x <= sub_polygon.linear_max_ &&
174 cmd_vel_in.x >= sub_polygon.linear_min_);
Basic polygon shape class. For STOP/SLOWDOWN/LIMIT model it represents zone around the robot while fo...
bool getCommonParameters(std::string &polygon_sub_topic, std::string &polygon_pub_topic, std::string &footprint_topic, bool use_dynamic_sub=false)
Supporting routine obtaining ROS-parameters common for all shapes.
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
geometry_msgs::msg::PolygonStamped polygon_
Polygon, used for: 1. visualization; 2. storing latest dynamic polygon message.
std::vector< Point > poly_
Polygon points (vertices) in a base_frame_id_.
std::string polygon_name_
Name of polygon.
nav2::LifecycleNode::WeakPtr node_
Collision Monitor node.
bool getPolygonFromString(std::string &poly_string, std::vector< Point > &polygon)
Extracts Polygon points from a string with of the form [[x1,y1],[x2,y2],[x3,y3]......
std::vector< SubPolygonParameter > sub_polygons_
Vector to store the parameters of the sub-polygon.
bool getParameters(std::string &, std::string &polygon_pub_topic, std::string &) override
Overridden getParameters function for VelocityPolygon parameters.
bool isInRange(const Velocity &cmd_vel_in, const SubPolygonParameter &sub_polygon_param)
Check if the velocities and direction is in expected range.
virtual ~VelocityPolygon()
VelocityPolygon destructor.
void updatePolygon(const Velocity &cmd_vel_in) override
Overridden updatePolygon function for VelocityPolygon.
bool holonomic_
Flag to indicate if the robot is holonomic.
VelocityPolygon(const nav2::LifecycleNode::WeakPtr &node, const std::string &polygon_name, const nav2::TransformBuffer::SharedPtr tf_buffer, const std::string &base_frame_id, const tf2::Duration &transform_tolerance)
VelocityPolygon constructor.
Point with 2D collision-check coordinates and optional z from the source.
Custom struct to store the parameters of the sub-polygon.
Velocity for 2D model of motion.