Skip to content

Commit 43199c9

Browse files
authored
nav2_collision_monitor dynamic parameters polygon and source enabled for Humble (#4615)
* Copy modification from c2d84df into humble collision_monitor for dynamic parameter enabled in polygon * Add the enabled dynamic parameter for source. Signed-off-by: Enzo Ghisoni <enzo.ghisoni@hotmail.fr> * Start backport action_state_ declaration in collision_monitor_node_test.cpp Signed-off-by: EnzoGhisoni <enzo.ghisoni@botronics.be> --------- Signed-off-by: EnzoGhisoni <enzo.ghisoni@botronics.be> Co-authored-by: EnzoGhisoni <enzo.ghisoni@botronics.be>
1 parent 7df7cc7 commit 43199c9

File tree

13 files changed

+324
-4
lines changed

13 files changed

+324
-4
lines changed

‎nav2_collision_monitor/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ find_package(tf2_geometry_msgs REQUIRED)
1414
find_package(nav2_common REQUIRED)
1515
find_package(nav2_util REQUIRED)
1616
find_package(nav2_costmap_2d REQUIRED)
17+
find_package(nav2_msgs REQUIRED)
1718

1819
### Header ###
1920

@@ -35,6 +36,7 @@ set(dependencies
3536
tf2_geometry_msgs
3637
nav2_util
3738
nav2_costmap_2d
39+
nav2_msgs
3840
)
3941

4042
set(executable_name collision_monitor)

‎nav2_collision_monitor/include/nav2_collision_monitor/polygon.hpp‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class Polygon
8686
* @return Action type for current polygon
8787
*/
8888
ActionType getActionType() const;
89+
/**
90+
* @brief Obtains polygon enabled state
91+
* @return Whether polygon is enabled
92+
*/
93+
bool getEnabled() const;
8994
/**
9095
* @brief Obtains polygon maximum points to enter inside polygon causing no action
9196
* @return Maximum points to enter to current polygon and take no action
@@ -161,6 +166,14 @@ class Polygon
161166
* @param point Given point to check
162167
* @return True if given point is inside polygon, otherwise false
163168
*/
169+
170+
/**
171+
* @brief Callback executed when a parameter change is detected
172+
* @param event ParameterEvent message
173+
*/
174+
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(
175+
std::vector<rclcpp::Parameter> parameters);
176+
164177
bool isPointInside(const Point & point) const;
165178

166179
// ----- Variables -----
@@ -169,7 +182,9 @@ class Polygon
169182
nav2_util::LifecycleNode::WeakPtr node_;
170183
/// @brief Collision monitor node logger stored for further usage
171184
rclcpp::Logger logger_{rclcpp::get_logger("collision_monitor")};
172-
185+
/// @brief Dynamic parameters handler
186+
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_;
187+
173188
// Basic parameters
174189
/// @brief Name of polygon
175190
std::string polygon_name_;
@@ -185,6 +200,8 @@ class Polygon
185200
double simulation_time_step_;
186201
/// @brief Footprint subscriber
187202
std::unique_ptr<nav2_costmap_2d::FootprintSubscriber> footprint_sub_;
203+
/// @brief Whether polygon is enabled
204+
bool enabled_;
188205

189206
// Global variables
190207
/// @brief TF buffer

‎nav2_collision_monitor/include/nav2_collision_monitor/source.hpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,19 @@ class Source
7474
const rclcpp::Time & curr_time,
7575
std::vector<Point> & data) const = 0;
7676

77+
/**
78+
* @brief Obtains source enabled state
79+
* @return Whether source is enabled
80+
*/
81+
bool getEnabled() const;
82+
7783
protected:
84+
/**
85+
* @brief Source configuration routine.
86+
* @return True in case of everything is configured correctly, or false otherwise
87+
*/
88+
bool configure();
89+
7890
/**
7991
* @brief Supporting routine obtaining ROS-parameters common for all data sources
8092
* @param source_topic Output name of source subscription topic
@@ -91,12 +103,21 @@ class Source
91103
const rclcpp::Time & source_time,
92104
const rclcpp::Time & curr_time) const;
93105

106+
/**
107+
* @brief Callback executed when a parameter change is detected
108+
* @param event ParameterEvent message
109+
*/
110+
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(
111+
std::vector<rclcpp::Parameter> parameters);
112+
94113
// ----- Variables -----
95114

96115
/// @brief Collision Monitor node
97116
nav2_util::LifecycleNode::WeakPtr node_;
98117
/// @brief Collision monitor node logger stored for further usage
99118
rclcpp::Logger logger_{rclcpp::get_logger("collision_monitor")};
119+
/// @brief Dynamic parameters handler
120+
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_;
100121

101122
// Basic parameters
102123
/// @brief Name of data source
@@ -116,6 +137,8 @@ class Source
116137
/// @brief Whether to correct source data towards to base frame movement,
117138
/// considering the difference between current time and latest source time
118139
bool base_shift_correction_;
140+
/// @brief Whether source is enabled
141+
bool enabled_;
119142
}; // class Source
120143

121144
} // namespace nav2_collision_monitor

‎nav2_collision_monitor/params/collision_monitor_params.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ collision_monitor:
2222
max_points: 3
2323
visualize: True
2424
polygon_pub_topic: "polygon_stop"
25+
enabled: True
2526
PolygonSlow:
2627
type: "polygon"
2728
points: [0.4, 0.4, 0.4, -0.4, -0.4, -0.4, -0.4, 0.4]
@@ -30,6 +31,7 @@ collision_monitor:
3031
slowdown_ratio: 0.3
3132
visualize: True
3233
polygon_pub_topic: "polygon_slowdown"
34+
enabled: True
3335
FootprintApproach:
3436
type: "polygon"
3537
action_type: "approach"
@@ -38,12 +40,15 @@ collision_monitor:
3840
simulation_time_step: 0.1
3941
max_points: 5
4042
visualize: False
43+
enabled: True
4144
observation_sources: ["scan"]
4245
scan:
4346
type: "scan"
4447
topic: "/scan"
48+
enabled: True
4549
pointcloud:
4650
type: "pointcloud"
4751
topic: "/intel_realsense_r200_depth/points"
4852
min_height: 0.1
4953
max_height: 0.5
54+
enabled: True

‎nav2_collision_monitor/src/collision_monitor_node.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ void CollisionMonitor::process(const Velocity & cmd_vel_in)
355355

356356
// Fill collision_points array from different data sources
357357
for (std::shared_ptr<Source> source : sources_) {
358-
source->getData(curr_time, collision_points);
358+
if (source->getEnabled()) {
359+
source->getData(curr_time, collision_points);
360+
}
359361
}
360362

361363
// By default - there is no action
@@ -364,6 +366,9 @@ void CollisionMonitor::process(const Velocity & cmd_vel_in)
364366
std::shared_ptr<Polygon> action_polygon;
365367

366368
for (std::shared_ptr<Polygon> polygon : polygons_) {
369+
if (!polygon->getEnabled()) {
370+
continue;
371+
}
367372
if (robot_action.action_type == STOP) {
368373
// If robot already should stop, do nothing
369374
break;
@@ -481,7 +486,9 @@ void CollisionMonitor::printAction(
481486
void CollisionMonitor::publishPolygons() const
482487
{
483488
for (std::shared_ptr<Polygon> polygon : polygons_) {
484-
polygon->publish();
489+
if (polygon->getEnabled()) {
490+
polygon->publish();
491+
}
485492
}
486493
}
487494

‎nav2_collision_monitor/src/pointcloud.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ PointCloud::~PointCloud()
4848

4949
void PointCloud::configure()
5050
{
51+
Source::configure();
5152
auto node = node_.lock();
5253
if (!node) {
5354
throw std::runtime_error{"Failed to lock node"};

‎nav2_collision_monitor/src/polygon.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Polygon::~Polygon()
4444
{
4545
RCLCPP_INFO(logger_, "[%s]: Destroying Polygon", polygon_name_.c_str());
4646
poly_.clear();
47+
dyn_params_handler_.reset();
4748
}
4849

4950
bool Polygon::configure()
@@ -82,6 +83,10 @@ bool Polygon::configure()
8283
polygon_pub_topic, polygon_qos);
8384
}
8485

86+
// Add callback for dynamic parameters
87+
dyn_params_handler_ = node->add_on_set_parameters_callback(
88+
std::bind(&Polygon::dynamicParametersCallback, this, std::placeholders::_1));
89+
8590
return true;
8691
}
8792

@@ -109,6 +114,11 @@ ActionType Polygon::getActionType() const
109114
return action_type_;
110115
}
111116

117+
bool Polygon::getEnabled() const
118+
{
119+
return enabled_;
120+
}
121+
112122
int Polygon::getMaxPoints() const
113123
{
114124
return max_points_;
@@ -244,6 +254,10 @@ bool Polygon::getCommonParameters(std::string & polygon_pub_topic)
244254
return false;
245255
}
246256

257+
nav2_util::declare_parameter_if_not_declared(
258+
node, polygon_name_ + ".enabled", rclcpp::ParameterValue(true));
259+
enabled_ = node->get_parameter(polygon_name_ + ".enabled").as_bool();
260+
247261
nav2_util::declare_parameter_if_not_declared(
248262
node, polygon_name_ + ".max_points", rclcpp::ParameterValue(3));
249263
max_points_ = node->get_parameter(polygon_name_ + ".max_points").as_int();
@@ -350,6 +364,26 @@ bool Polygon::getParameters(std::string & polygon_pub_topic, std::string & footp
350364
return true;
351365
}
352366

367+
rcl_interfaces::msg::SetParametersResult
368+
Polygon::dynamicParametersCallback(
369+
std::vector<rclcpp::Parameter> parameters)
370+
{
371+
rcl_interfaces::msg::SetParametersResult result;
372+
373+
for (auto parameter : parameters) {
374+
const auto & param_type = parameter.get_type();
375+
const auto & param_name = parameter.get_name();
376+
377+
if (param_type == rcl_interfaces::msg::ParameterType::PARAMETER_BOOL) {
378+
if (param_name == polygon_name_ + "." + "enabled") {
379+
enabled_ = parameter.as_bool();
380+
}
381+
}
382+
}
383+
result.successful = true;
384+
return result;
385+
}
386+
353387
inline bool Polygon::isPointInside(const Point & point) const
354388
{
355389
// Adaptation of Shimrat, Moshe. "Algorithm 112: position of point relative to polygon."

‎nav2_collision_monitor/src/range.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Range::~Range()
4848

4949
void Range::configure()
5050
{
51+
Source::configure();
5152
auto node = node_.lock();
5253
if (!node) {
5354
throw std::runtime_error{"Failed to lock node"};

‎nav2_collision_monitor/src/scan.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Scan::~Scan()
4545

4646
void Scan::configure()
4747
{
48+
Source::configure();
4849
auto node = node_.lock();
4950
if (!node) {
5051
throw std::runtime_error{"Failed to lock node"};

‎nav2_collision_monitor/src/source.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ Source::~Source()
4646
{
4747
}
4848

49+
bool Source::configure()
50+
{
51+
auto node = node_.lock();
52+
53+
// Add callback for dynamic parameters
54+
dyn_params_handler_ = node->add_on_set_parameters_callback(
55+
std::bind(&Source::dynamicParametersCallback, this, std::placeholders::_1));
56+
57+
return true;
58+
}
59+
4960
void Source::getCommonParameters(std::string & source_topic)
5061
{
5162
auto node = node_.lock();
@@ -57,6 +68,10 @@ void Source::getCommonParameters(std::string & source_topic)
5768
node, source_name_ + ".topic",
5869
rclcpp::ParameterValue("scan")); // Set deafult topic for laser scanner
5970
source_topic = node->get_parameter(source_name_ + ".topic").as_string();
71+
72+
nav2_util::declare_parameter_if_not_declared(
73+
node, source_name_ + ".enabled", rclcpp::ParameterValue(true));
74+
enabled_ = node->get_parameter(source_name_ + ".enabled").as_bool();
6075
}
6176

6277
bool Source::sourceValid(
@@ -78,4 +93,29 @@ bool Source::sourceValid(
7893
return true;
7994
}
8095

96+
bool Source::getEnabled() const
97+
{
98+
return enabled_;
99+
}
100+
101+
rcl_interfaces::msg::SetParametersResult
102+
Source::dynamicParametersCallback(
103+
std::vector<rclcpp::Parameter> parameters)
104+
{
105+
rcl_interfaces::msg::SetParametersResult result;
106+
107+
for (auto parameter : parameters) {
108+
const auto & param_type = parameter.get_type();
109+
const auto & param_name = parameter.get_name();
110+
111+
if (param_type == rcl_interfaces::msg::ParameterType::PARAMETER_BOOL) {
112+
if (param_name == source_name_ + "." + "enabled") {
113+
enabled_ = parameter.as_bool();
114+
}
115+
}
116+
}
117+
result.successful = true;
118+
return result;
119+
}
120+
81121
} // namespace nav2_collision_monitor

0 commit comments

Comments
 (0)