15 #include "nav2_collision_monitor/polygon.hpp"
20 #include "geometry_msgs/msg/point.hpp"
21 #include "geometry_msgs/msg/point32.hpp"
23 #include "nav2_util/node_utils.hpp"
25 #include "nav2_collision_monitor/kinematics.hpp"
27 namespace nav2_collision_monitor
31 const nav2_util::LifecycleNode::WeakPtr & node,
32 const std::string & polygon_name,
33 const std::shared_ptr<tf2_ros::Buffer> tf_buffer,
34 const std::string & base_frame_id,
35 const tf2::Duration & transform_tolerance)
36 : node_(node), polygon_name_(polygon_name), action_type_(DO_NOTHING),
37 slowdown_ratio_(0.0), footprint_sub_(nullptr), tf_buffer_(tf_buffer),
38 base_frame_id_(base_frame_id), transform_tolerance_(transform_tolerance)
52 auto node =
node_.lock();
54 throw std::runtime_error{
"Failed to lock node"};
57 std::string polygon_pub_topic, footprint_topic;
63 if (!footprint_topic.empty()) {
64 footprint_sub_ = std::make_unique<nav2_costmap_2d::FootprintSubscriber>(
71 std::vector<Point> poly;
73 for (
const Point & p : poly) {
74 geometry_msgs::msg::Point32 p_s;
81 rclcpp::QoS polygon_qos = rclcpp::SystemDefaultsQoS();
82 polygon_pub_ = node->create_publisher<geometry_msgs::msg::PolygonStamped>(
83 polygon_pub_topic, polygon_qos);
146 std::vector<geometry_msgs::msg::Point> footprint_vec;
147 std_msgs::msg::Header footprint_header;
148 footprint_sub_->getFootprintInRobotFrame(footprint_vec, footprint_header);
150 std::size_t new_size = footprint_vec.size();
151 poly_.resize(new_size);
154 geometry_msgs::msg::Point32 p_s;
155 for (std::size_t i = 0; i < new_size; i++) {
156 poly_[i] = {footprint_vec[i].x, footprint_vec[i].y};
157 p_s.x = footprint_vec[i].x;
158 p_s.y = footprint_vec[i].y;
167 for (
const Point & point : points) {
168 if (isPointInside(point)) {
176 const std::vector<Point> & collision_points,
180 Pose pose = {0.0, 0.0, 0.0};
184 std::vector<Point> points_transformed = collision_points;
197 points_transformed = collision_points;
198 transformPoints(pose, points_transformed);
216 auto node =
node_.lock();
218 throw std::runtime_error{
"Failed to lock node"};
222 std::unique_ptr<geometry_msgs::msg::PolygonStamped> poly_s =
223 std::make_unique<geometry_msgs::msg::PolygonStamped>();
224 poly_s->header.stamp = node->now();
234 auto node =
node_.lock();
236 throw std::runtime_error{
"Failed to lock node"};
242 nav2_util::declare_parameter_if_not_declared(
243 node,
polygon_name_ +
".action_type", rclcpp::PARAMETER_STRING);
244 const std::string at_str =
245 node->get_parameter(
polygon_name_ +
".action_type").as_string();
246 if (at_str ==
"stop") {
248 }
else if (at_str ==
"slowdown") {
250 }
else if (at_str ==
"approach") {
257 nav2_util::declare_parameter_if_not_declared(
258 node,
polygon_name_ +
".enabled", rclcpp::ParameterValue(
true));
261 nav2_util::declare_parameter_if_not_declared(
262 node,
polygon_name_ +
".max_points", rclcpp::ParameterValue(3));
266 nav2_util::declare_parameter_if_not_declared(
267 node,
polygon_name_ +
".slowdown_ratio", rclcpp::ParameterValue(0.5));
272 nav2_util::declare_parameter_if_not_declared(
273 node,
polygon_name_ +
".time_before_collision", rclcpp::ParameterValue(2.0));
275 node->get_parameter(
polygon_name_ +
".time_before_collision").as_double();
276 nav2_util::declare_parameter_if_not_declared(
277 node,
polygon_name_ +
".simulation_time_step", rclcpp::ParameterValue(0.1));
279 node->get_parameter(
polygon_name_ +
".simulation_time_step").as_double();
282 nav2_util::declare_parameter_if_not_declared(
283 node,
polygon_name_ +
".visualize", rclcpp::ParameterValue(
false));
287 nav2_util::declare_parameter_if_not_declared(
289 polygon_pub_topic = node->get_parameter(
polygon_name_ +
".polygon_pub_topic").as_string();
291 }
catch (
const std::exception & ex) {
294 "[%s]: Error while getting common polygon parameters: %s",
304 auto node =
node_.lock();
306 throw std::runtime_error{
"Failed to lock node"};
316 nav2_util::declare_parameter_if_not_declared(
318 rclcpp::ParameterValue(
"local_costmap/published_footprint"));
320 node->get_parameter(
polygon_name_ +
".footprint_topic").as_string();
327 footprint_topic.clear();
331 nav2_util::declare_parameter_if_not_declared(
332 node,
polygon_name_ +
".points", rclcpp::PARAMETER_DOUBLE_ARRAY);
333 std::vector<double> poly_row =
334 node->get_parameter(
polygon_name_ +
".points").as_double_array();
336 if (poly_row.size() <= 6 || poly_row.size() % 2 != 0) {
339 "[%s]: Polygon has incorrect points description",
347 for (
double val : poly_row) {
352 poly_.push_back(point);
356 }
catch (
const std::exception & ex) {
359 "[%s]: Error while getting polygon parameters: %s",
367 rcl_interfaces::msg::SetParametersResult
369 std::vector<rclcpp::Parameter> parameters)
371 rcl_interfaces::msg::SetParametersResult result;
373 for (
auto parameter : parameters) {
374 const auto & param_type = parameter.get_type();
375 const auto & param_name = parameter.get_name();
377 if (param_type == rcl_interfaces::msg::ParameterType::PARAMETER_BOOL) {
383 result.successful =
true;
387 inline bool Polygon::isPointInside(
const Point & point)
const
394 const int poly_size =
poly_.size();
400 for (j = 0; j < poly_size; j++) {
404 if ((point.y <=
poly_[i].y) == (point.y >
poly_[j].y)) {
406 const double x_inter =
poly_[i].x +
410 if (x_inter > point.x) {
nav2_util::LifecycleNode::WeakPtr node_
Collision Monitor node.
double getTimeBeforeCollision() const
Obtains required time before collision for current polygon. Applicable for APPROACH model.
int max_points_
Maximum number of data readings within a zone to not trigger the action.
bool getCommonParameters(std::string &polygon_pub_topic)
Supporting routine obtaining ROS-parameters common for all shapes.
virtual int getPointsInside(const std::vector< Point > &points) const
Gets number of points inside given polygon.
double time_before_collision_
Time before collision in seconds.
bool enabled_
Whether polygon is enabled.
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
ActionType action_type_
Action type for the polygon.
double getCollisionTime(const std::vector< Point > &collision_points, const Velocity &velocity) const
Obtains estimated (simulated) time before a collision. Applicable for APPROACH model.
int getMaxPoints() const
Obtains polygon maximum points to enter inside polygon causing no action.
void publish() const
Publishes polygon message into a its own topic.
void updatePolygon()
Updates polygon from footprint subscriber (if any)
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_
Dynamic parameters handler.
double simulation_time_step_
Time step for robot movement simulation.
void activate()
Activates polygon lifecycle publisher.
bool configure()
Shape configuration routine. Obtains ROS-parameters related to shape object and creates polygon lifec...
geometry_msgs::msg::Polygon polygon_
Polygon points stored for later publishing.
std::string getName() const
Returns the name of polygon.
virtual void getPolygon(std::vector< Point > &poly) const
Gets polygon points.
tf2::Duration transform_tolerance_
Transform tolerance.
std::shared_ptr< tf2_ros::Buffer > tf_buffer_
TF buffer.
ActionType getActionType() const
Obtains polygon action type.
std::vector< Point > poly_
Polygon points (vertices)
Polygon(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)
Polygon constructor.
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(std::vector< rclcpp::Parameter > parameters)
Checks if point is inside polygon.
void deactivate()
Deactivates polygon lifecycle publisher.
std::unique_ptr< nav2_costmap_2d::FootprintSubscriber > footprint_sub_
Footprint subscriber.
double getSlowdownRatio() const
Obtains speed slowdown ratio for current polygon. Applicable for SLOWDOWN model.
std::string polygon_name_
Name of polygon.
virtual ~Polygon()
Polygon destructor.
bool getEnabled() const
Obtains polygon enabled state.
rclcpp_lifecycle::LifecyclePublisher< geometry_msgs::msg::PolygonStamped >::SharedPtr polygon_pub_
Polygon publisher for visualization purposes.
bool visualize_
Whether to publish the polygon.
std::string base_frame_id_
Base frame ID.
virtual bool getParameters(std::string &polygon_pub_topic, std::string &footprint_topic)
Supporting routine obtaining polygon-specific ROS-parameters.
double slowdown_ratio_
Robot slowdown (share of its actual speed)
Velocity for 2D model of motion.