Skip to content

Commit f8d2bef

Browse files
author
asarazin
committed
Collision monitor: select specific observation sources for polygon
Signed-off-by: asarazin <[email protected]>
1 parent 63b6d90 commit f8d2bef

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

nav2_collision_monitor/include/nav2_collision_monitor/polygon.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ class Polygon
126126
*/
127127
virtual void getPolygon(std::vector<Point> & poly) const;
128128

129+
/**
130+
* @brief Obtains the name of the observation sources for current polygon.
131+
* @return Names of the observation sources
132+
*/
133+
std::vector<std::string> getSourcesNames() const;
134+
129135
/**
130136
* @brief Returns true if polygon points were set.
131137
* Othewise, prints a warning and returns false.
@@ -252,6 +258,8 @@ class Polygon
252258
rclcpp::Subscription<geometry_msgs::msg::PolygonStamped>::SharedPtr polygon_sub_;
253259
/// @brief Footprint subscriber
254260
std::unique_ptr<nav2_costmap_2d::FootprintSubscriber> footprint_sub_;
261+
/// @brief Name of the observation sources to check for polygon
262+
std::vector<std::string> sources_names_;
255263

256264
// Global variables
257265
/// @brief TF buffer

nav2_collision_monitor/src/collision_monitor_node.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,17 @@ bool CollisionMonitor::getParameters(
270270
stop_pub_timeout_ =
271271
rclcpp::Duration::from_seconds(get_parameter("stop_pub_timeout").as_double());
272272

273-
if (!configurePolygons(base_frame_id, transform_tolerance)) {
274-
return false;
275-
}
276-
277273
if (
278274
!configureSources(
279275
base_frame_id, odom_frame_id, transform_tolerance, source_timeout, base_shift_correction))
280276
{
281277
return false;
282278
}
283279

280+
if (!configurePolygons(base_frame_id, transform_tolerance)) {
281+
return false;
282+
}
283+
284284
return true;
285285
}
286286

@@ -405,7 +405,14 @@ void CollisionMonitor::process(const Velocity & cmd_vel_in, const std_msgs::msg:
405405
}
406406

407407
// Points array collected from different data sources in a robot base frame
408-
std::vector<Point> collision_points;
408+
std::unordered_map<std::string, std::vector<Point>> sources_collision_points_map;
409+
410+
// Fill collision_points array from different data sources
411+
for (std::shared_ptr<Source> source : sources_) {
412+
std::vector<Point> collision_points;
413+
source->getData(curr_time, collision_points);
414+
sources_collision_points_map.insert({source->getSourceName(), collision_points});
415+
}
409416

410417
// By default - there is no action
411418
Action robot_action{DO_NOTHING, cmd_vel_in, ""};
@@ -468,6 +475,28 @@ void CollisionMonitor::process(const Velocity & cmd_vel_in, const std_msgs::msg:
468475
// Update polygon coordinates
469476
polygon->updatePolygon(cmd_vel_in);
470477

478+
// Get collision points for sources associated to polygon
479+
std::vector<std::string> sources_names = polygon->getSourcesNames();
480+
std::vector<Point> collision_points;
481+
for (auto source_name : sources_names) {
482+
try {
483+
// Get vector for source
484+
auto source_collision_points = sources_collision_points_map.at(source_name);
485+
// Concatenate vectors
486+
collision_points.insert(
487+
collision_points.end(),
488+
source_collision_points.begin(),
489+
source_collision_points.end());
490+
} catch (std::exception & e) {
491+
RCLCPP_ERROR_STREAM_THROTTLE(
492+
get_logger(),
493+
*get_clock(),
494+
1000,
495+
"Observation source [" << source_name <<
496+
"] configured for polygon [" << polygon->getName() << "] is not defined!");
497+
}
498+
}
499+
471500
const ActionType at = polygon->getActionType();
472501
if (at == STOP || at == SLOWDOWN || at == LIMIT) {
473502
// Process STOP/SLOWDOWN for the selected polygon

nav2_collision_monitor/src/polygon.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ double Polygon::getTimeBeforeCollision() const
168168
return time_before_collision_;
169169
}
170170

171+
std::vector<std::string> Polygon::getSourcesNames() const
172+
{
173+
return sources_names_;
174+
}
175+
171176
void Polygon::getPolygon(std::vector<Point> & poly) const
172177
{
173178
poly = poly_;
@@ -372,6 +377,27 @@ bool Polygon::getCommonParameters(std::string & polygon_pub_topic)
372377
node, polygon_name_ + ".polygon_pub_topic", rclcpp::ParameterValue(polygon_name_));
373378
polygon_pub_topic = node->get_parameter(polygon_name_ + ".polygon_pub_topic").as_string();
374379
}
380+
381+
// By default, use all observation sources for polygon
382+
nav2_util::declare_parameter_if_not_declared(
383+
node, "observation_sources", rclcpp::PARAMETER_STRING_ARRAY);
384+
std::vector<std::string> source_names =
385+
node->get_parameter("observation_sources").as_string_array();
386+
nav2_util::declare_parameter_if_not_declared(
387+
node, polygon_name_ + ".sources_names", rclcpp::ParameterValue(source_names));
388+
sources_names_ = node->get_parameter(polygon_name_ + ".sources_names").as_string_array();
389+
390+
// Check the observation sources configured for polygon are defined
391+
for (auto source_name : sources_names_) {
392+
if (std::find(source_names.begin(), source_names.end(), source_name) == source_names.end()) {
393+
RCLCPP_ERROR_STREAM(
394+
logger_,
395+
"Observation source [" << source_name <<
396+
"] configured for polygon [" << getName() <<
397+
"] is not defined!");
398+
return false;
399+
}
400+
}
375401
} catch (const std::exception & ex) {
376402
RCLCPP_ERROR(
377403
logger_,

0 commit comments

Comments
 (0)