Skip to content

Commit b759ea5

Browse files
SteveMacenskiTony Najjartonynajjar
authored andcommitted
Collision monitor: Add polygon source (ros-navigation#4275)
* Rename PushRosNamespace to PushROSNamespace * Fix min_points checking * Add polygon source * Revert "Merge branch 'add-collision-points-debug' into add-polygon-source" This reverts commit 15c261c, reversing changes made to 5a63584. * . * fix * fix * fix lint * PR comments fixes * fixes * add test * fix space * use standard msg Polygonstamped * draft * . * fixes * fixes * fixes * fix * revert * fixes * add detector test * rebasing fixes * Adding polygon source * adjusting tests * linting --------- Co-authored-by: Tony Najjar <tony.najjar@logivations.com> Co-authored-by: Tony Najjar <tony.najjar.1997@gmail.com>
1 parent 0de3e83 commit b759ea5

20 files changed

+670
-11
lines changed

nav2_collision_monitor/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ add_library(${monitor_library_name} SHARED
5656
src/source.cpp
5757
src/scan.cpp
5858
src/pointcloud.cpp
59+
src/polygon_source.cpp
5960
src/range.cpp
6061
src/kinematics.cpp
6162
)
@@ -67,6 +68,7 @@ add_library(${detector_library_name} SHARED
6768
src/source.cpp
6869
src/scan.cpp
6970
src/pointcloud.cpp
71+
src/polygon_source.cpp
7072
src/range.cpp
7173
src/kinematics.cpp
7274
)

nav2_collision_monitor/include/nav2_collision_monitor/collision_detector_node.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "nav2_collision_monitor/scan.hpp"
3939
#include "nav2_collision_monitor/pointcloud.hpp"
4040
#include "nav2_collision_monitor/range.hpp"
41+
#include "nav2_collision_monitor/polygon_source.hpp"
4142

4243
namespace nav2_collision_monitor
4344
{

nav2_collision_monitor/include/nav2_collision_monitor/collision_monitor_node.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "nav2_collision_monitor/scan.hpp"
4242
#include "nav2_collision_monitor/pointcloud.hpp"
4343
#include "nav2_collision_monitor/range.hpp"
44+
#include "nav2_collision_monitor/polygon_source.hpp"
4445

4546
namespace nav2_collision_monitor
4647
{

nav2_collision_monitor/include/nav2_collision_monitor/pointcloud.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class PointCloud : public Source
7373
*/
7474
bool getData(
7575
const rclcpp::Time & curr_time,
76-
std::vector<Point> & data) const;
76+
std::vector<Point> & data);
7777

7878
protected:
7979
/**
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) 2023 Pixel Robotics GmbH
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+
#ifndef NAV2_COLLISION_MONITOR__POLYGON_SOURCE_HPP_
16+
#define NAV2_COLLISION_MONITOR__POLYGON_SOURCE_HPP_
17+
18+
#include <memory>
19+
#include <vector>
20+
#include <string>
21+
22+
#include "geometry_msgs/msg/polygon_instance_stamped.hpp"
23+
#include "geometry_msgs/msg/polygon_stamped.hpp"
24+
25+
#include "nav2_collision_monitor/source.hpp"
26+
27+
namespace nav2_collision_monitor
28+
{
29+
30+
/**
31+
* @brief Implementation for polygon source
32+
*/
33+
class PolygonSource : public Source
34+
{
35+
public:
36+
/**
37+
* @brief PolygonSource constructor
38+
* @param node Collision Monitor node pointer
39+
* @param source_name Name of data source
40+
* @param tf_buffer Shared pointer to a TF buffer
41+
* @param base_frame_id Robot base frame ID. The output data will be transformed into this frame.
42+
* @param global_frame_id Global frame ID for correct transform calculation
43+
* @param transform_tolerance Transform tolerance
44+
* @param source_timeout Maximum time interval in which data is considered valid
45+
* @param base_shift_correction Whether to correct source data towards to base frame movement,
46+
* considering the difference between current time and latest source time
47+
*/
48+
PolygonSource(
49+
const nav2_util::LifecycleNode::WeakPtr & node,
50+
const std::string & source_name,
51+
const std::shared_ptr<tf2_ros::Buffer> tf_buffer,
52+
const std::string & base_frame_id,
53+
const std::string & global_frame_id,
54+
const tf2::Duration & transform_tolerance,
55+
const rclcpp::Duration & source_timeout,
56+
const bool base_shift_correction);
57+
/**
58+
* @brief PolygonSource destructor
59+
*/
60+
~PolygonSource();
61+
62+
/**
63+
* @brief Data source configuration routine. Obtains ROS-parameters
64+
* and creates subscriber.
65+
*/
66+
void configure();
67+
68+
/**
69+
* @brief Adds latest data from polygon source to the data array.
70+
* @param curr_time Current node time for data interpolation
71+
* @param data Array where the data from source to be added.
72+
* Added data is transformed to base_frame_id_ coordinate system at curr_time.
73+
* @return false if an invalid source should block the robot
74+
*/
75+
bool getData(
76+
const rclcpp::Time & curr_time,
77+
std::vector<Point> & data);
78+
79+
/**
80+
* @brief Converts a PolygonInstanceStamped to a std::vector<Point>
81+
* @param polygon Input Polygon to be converted
82+
* @param data Output vector of Point
83+
*/
84+
void convertPolygonStampedToPoints(
85+
const geometry_msgs::msg::PolygonStamped & polygon,
86+
std::vector<Point> & data) const;
87+
88+
protected:
89+
/**
90+
* @brief Getting sensor-specific ROS-parameters
91+
* @param source_topic Output name of source subscription topic
92+
*/
93+
void getParameters(std::string & source_topic);
94+
95+
/**
96+
* @brief PolygonSource data callback
97+
* @param msg Shared pointer to PolygonSource message
98+
*/
99+
void dataCallback(geometry_msgs::msg::PolygonInstanceStamped::ConstSharedPtr msg);
100+
101+
// ----- Variables -----
102+
103+
/// @brief PolygonSource data subscriber
104+
rclcpp::Subscription<geometry_msgs::msg::PolygonInstanceStamped>::SharedPtr data_sub_;
105+
106+
/// @brief Latest data obtained
107+
std::vector<geometry_msgs::msg::PolygonInstanceStamped> data_;
108+
109+
/// @brief distance between sampled points on polygon edges
110+
double sampling_distance_;
111+
}; // class PolygonSource
112+
113+
} // namespace nav2_collision_monitor
114+
115+
#endif // NAV2_COLLISION_MONITOR__POLYGON_SOURCE_HPP_

nav2_collision_monitor/include/nav2_collision_monitor/range.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Range : public Source
7373
*/
7474
bool getData(
7575
const rclcpp::Time & curr_time,
76-
std::vector<Point> & data) const;
76+
std::vector<Point> & data);
7777

7878
protected:
7979
/**

nav2_collision_monitor/include/nav2_collision_monitor/scan.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Scan : public Source
7373
*/
7474
bool getData(
7575
const rclcpp::Time & curr_time,
76-
std::vector<Point> & data) const;
76+
std::vector<Point> & data);
7777

7878
protected:
7979
/**

nav2_collision_monitor/include/nav2_collision_monitor/source.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Source
7373
*/
7474
virtual bool getData(
7575
const rclcpp::Time & curr_time,
76-
std::vector<Point> & data) const = 0;
76+
std::vector<Point> & data) = 0;
7777

7878
/**
7979
* @brief Obtains source enabled state

nav2_collision_monitor/src/collision_detector_node.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ bool CollisionDetector::configureSources(
287287
r->configure();
288288

289289
sources_.push_back(r);
290+
} else if (source_type == "polygon") {
291+
std::shared_ptr<PolygonSource> ps = std::make_shared<PolygonSource>(
292+
node, source_name, tf_buffer_, base_frame_id, odom_frame_id,
293+
transform_tolerance, source_timeout, base_shift_correction);
294+
ps->configure();
295+
296+
sources_.push_back(ps);
290297
} else { // Error if something else
291298
RCLCPP_ERROR(
292299
get_logger(),
@@ -345,6 +352,7 @@ void CollisionDetector::process()
345352
marker.color.r = 1.0;
346353
marker.color.a = 1.0;
347354
marker.lifetime = rclcpp::Duration(0, 0);
355+
marker.frame_locked = true;
348356

349357
for (const auto & point : collision_points) {
350358
geometry_msgs::msg::Point p;

nav2_collision_monitor/src/collision_monitor_node.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,13 @@ bool CollisionMonitor::configureSources(
378378
r->configure();
379379

380380
sources_.push_back(r);
381+
} else if (source_type == "polygon") {
382+
std::shared_ptr<PolygonSource> ps = std::make_shared<PolygonSource>(
383+
node, source_name, tf_buffer_, base_frame_id, odom_frame_id,
384+
transform_tolerance, source_timeout, base_shift_correction);
385+
ps->configure();
386+
387+
sources_.push_back(ps);
381388
} else { // Error if something else
382389
RCLCPP_ERROR(
383390
get_logger(),
@@ -444,6 +451,7 @@ void CollisionMonitor::process(const Velocity & cmd_vel_in, const std_msgs::msg:
444451
marker.color.r = 1.0;
445452
marker.color.a = 1.0;
446453
marker.lifetime = rclcpp::Duration(0, 0);
454+
marker.frame_locked = true;
447455

448456
for (const auto & point : collision_points) {
449457
geometry_msgs::msg::Point p;

0 commit comments

Comments
 (0)