Skip to content

Commit 1230ede

Browse files
committed
use switch case
1 parent 7f9f55a commit 1230ede

File tree

3 files changed

+100
-85
lines changed

3 files changed

+100
-85
lines changed

nav2_collision_monitor/include/nav2_collision_monitor/polygon.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ class Polygon
128128
*/
129129
virtual bool isShapeSet();
130130

131-
/**
132-
* @brief Returns true if using velocity based polygon
133-
*/
134-
bool isUsingVelocityPolygonSelector();
135-
136131
/**
137132
* @brief Updates polygon from footprint subscriber (if any)
138133
*/
@@ -262,6 +257,9 @@ class Polygon
262257

263258
/// @brief Polygon points (vertices) in a base_frame_id_
264259
std::vector<Point> poly_;
260+
261+
/// @brief Source for setting the polygon
262+
PolygonSource polygon_source;
265263
}; // class Polygon
266264

267265
} // namespace nav2_collision_monitor

nav2_collision_monitor/include/nav2_collision_monitor/types.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ struct Action
7979
std::string polygon_name;
8080
};
8181

82+
/// @brief Source for setting the polygon
83+
enum PolygonSource
84+
{
85+
POLYGON_SOURCE_UNKNOWN = 0, // Default
86+
STATIC_POINTS = 1, // Set from points
87+
DYNAMIC_SUB = 2, // Set from topic subscription
88+
VELOCITY_POLYGON = 3 // Set based on current twists
89+
};
90+
8291
} // namespace nav2_collision_monitor
8392

8493
#endif // NAV2_COLLISION_MONITOR__TYPES_HPP_

nav2_collision_monitor/src/polygon.cpp

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Polygon::Polygon(
3838
: node_(node), polygon_name_(polygon_name), action_type_(DO_NOTHING),
3939
slowdown_ratio_(0.0), linear_limit_(0.0), angular_limit_(0.0),
4040
footprint_sub_(nullptr), tf_buffer_(tf_buffer),
41-
base_frame_id_(base_frame_id), transform_tolerance_(transform_tolerance)
41+
base_frame_id_(base_frame_id), transform_tolerance_(transform_tolerance),
42+
polygon_source(POLYGON_SOURCE_UNKNOWN)
4243
{
4344
RCLCPP_INFO(logger_, "[%s]: Creating Polygon", polygon_name_.c_str());
4445
}
@@ -169,89 +170,95 @@ bool Polygon::isShapeSet()
169170
return true;
170171
}
171172

172-
bool Polygon::isUsingVelocityPolygonSelector()
173-
{
174-
return !velocity_polygons_.empty();
175-
}
176-
177173
void Polygon::updatePolygon(const Velocity & cmd_vel_in)
178174
{
179-
if (isUsingVelocityPolygonSelector()) {
180-
for (auto & velocity_polygon : velocity_polygons_) {
181-
182-
if (velocity_polygon->isInRange(cmd_vel_in)) {
183-
// Set the polygon that is within the speed range
184-
poly_ = velocity_polygon->getPolygon();
185-
186-
// Update visualization polygon
187-
polygon_.polygon.points.clear();
188-
for (const Point & p : poly_) {
189-
geometry_msgs::msg::Point32 p_s;
190-
p_s.x = p.x;
191-
p_s.y = p.y;
192-
// p_s.z will remain 0.0
193-
polygon_.polygon.points.push_back(p_s);
175+
switch (polygon_source) {
176+
case STATIC_POINTS:
177+
{
178+
if (!polygon_.header.frame_id.empty() && polygon_.header.frame_id != base_frame_id_) {
179+
// Polygon is published in another frame: correct poly_ vertices to the latest frame state
180+
std::size_t new_size = polygon_.polygon.points.size();
181+
182+
// Get the transform from PolygonStamped frame to base_frame_id_
183+
tf2::Transform tf_transform;
184+
if (
185+
!nav2_util::getTransform(
186+
polygon_.header.frame_id, base_frame_id_,
187+
transform_tolerance_, tf_buffer_, tf_transform))
188+
{
189+
return;
190+
}
191+
192+
// Correct main poly_ vertices
193+
poly_.resize(new_size);
194+
for (std::size_t i = 0; i < new_size; i++) {
195+
// Transform point coordinates from PolygonStamped frame -> to base frame
196+
tf2::Vector3 p_v3_s(polygon_.polygon.points[i].x, polygon_.polygon.points[i].y, 0.0);
197+
tf2::Vector3 p_v3_b = tf_transform * p_v3_s;
198+
199+
// Fill poly_ array
200+
poly_[i] = {p_v3_b.x(), p_v3_b.y()};
201+
}
194202
}
195-
196-
return;
203+
break;
197204
}
198-
}
199-
200-
// Log for uncovered velocity
201-
auto node = node_.lock();
202-
if (!node) {
203-
throw std::runtime_error{"Failed to lock node"};
204-
}
205-
RCLCPP_WARN_THROTTLE(
206-
logger_,
207-
*node->get_clock(), 2.0, "Velocity is not covered by any of the velocity polygons. x: %.3f y: %.3f tw: %.3f ",
208-
cmd_vel_in.x, cmd_vel_in.y, cmd_vel_in.tw);
209-
return;
210-
}
211-
212-
if (footprint_sub_ != nullptr) {
213-
// Get latest robot footprint from footprint subscriber
214-
std::vector<geometry_msgs::msg::Point> footprint_vec;
215-
std_msgs::msg::Header footprint_header;
216-
footprint_sub_->getFootprintInRobotFrame(footprint_vec, footprint_header);
217-
218-
std::size_t new_size = footprint_vec.size();
219-
poly_.resize(new_size);
220-
polygon_.header.frame_id = base_frame_id_;
221-
polygon_.polygon.points.resize(new_size);
222-
223-
geometry_msgs::msg::Point32 p_s;
224-
for (std::size_t i = 0; i < new_size; i++) {
225-
poly_[i] = {footprint_vec[i].x, footprint_vec[i].y};
226-
p_s.x = footprint_vec[i].x;
227-
p_s.y = footprint_vec[i].y;
228-
polygon_.polygon.points[i] = p_s;
229-
}
230-
} else if (!polygon_.header.frame_id.empty() && polygon_.header.frame_id != base_frame_id_) {
231-
// Polygon is published in another frame: correct poly_ vertices to the latest frame state
232-
std::size_t new_size = polygon_.polygon.points.size();
233-
234-
// Get the transform from PolygonStamped frame to base_frame_id_
235-
tf2::Transform tf_transform;
236-
if (
237-
!nav2_util::getTransform(
238-
polygon_.header.frame_id, base_frame_id_,
239-
transform_tolerance_, tf_buffer_, tf_transform))
240-
{
241-
return;
242-
}
243-
244-
// Correct main poly_ vertices
245-
poly_.resize(new_size);
246-
for (std::size_t i = 0; i < new_size; i++) {
247-
// Transform point coordinates from PolygonStamped frame -> to base frame
248-
tf2::Vector3 p_v3_s(polygon_.polygon.points[i].x, polygon_.polygon.points[i].y, 0.0);
249-
tf2::Vector3 p_v3_b = tf_transform * p_v3_s;
205+
case DYNAMIC_SUB:
206+
{
207+
// Get latest robot footprint from footprint subscriber
208+
std::vector<geometry_msgs::msg::Point> footprint_vec;
209+
std_msgs::msg::Header footprint_header;
210+
footprint_sub_->getFootprintInRobotFrame(footprint_vec, footprint_header);
211+
212+
std::size_t new_size = footprint_vec.size();
213+
poly_.resize(new_size);
214+
polygon_.header.frame_id = base_frame_id_;
215+
polygon_.polygon.points.resize(new_size);
216+
217+
geometry_msgs::msg::Point32 p_s;
218+
for (std::size_t i = 0; i < new_size; i++) {
219+
poly_[i] = {footprint_vec[i].x, footprint_vec[i].y};
220+
p_s.x = footprint_vec[i].x;
221+
p_s.y = footprint_vec[i].y;
222+
polygon_.polygon.points[i] = p_s;
223+
}
224+
break;
225+
}
226+
case VELOCITY_POLYGON:
227+
{
228+
for (auto & velocity_polygon : velocity_polygons_) {
229+
if (velocity_polygon->isInRange(cmd_vel_in)) {
230+
// Set the polygon that is within the speed range
231+
poly_ = velocity_polygon->getPolygon();
232+
233+
// Update visualization polygon
234+
polygon_.polygon.points.clear();
235+
for (const Point & p : poly_) {
236+
geometry_msgs::msg::Point32 p_s;
237+
p_s.x = p.x;
238+
p_s.y = p.y;
239+
// p_s.z will remain 0.0
240+
polygon_.polygon.points.push_back(p_s);
241+
}
242+
return;
243+
}
244+
}
250245

251-
// Fill poly_ array
252-
poly_[i] = {p_v3_b.x(), p_v3_b.y()};
253-
}
246+
// Log for uncovered velocity
247+
auto node = node_.lock();
248+
if (!node) {
249+
throw std::runtime_error{"Failed to lock node"};
250+
}
251+
RCLCPP_WARN_THROTTLE(
252+
logger_,
253+
*node->get_clock(), 2.0, "Velocity is not covered by any of the velocity polygons. x: %.3f y: %.3f tw: %.3f ",
254+
cmd_vel_in.x, cmd_vel_in.y, cmd_vel_in.tw);
255+
break;
256+
}
257+
case POLYGON_SOURCE_UNKNOWN:
258+
default:
259+
break;
254260
}
261+
return;
255262
}
256263

257264
int Polygon::getPointsInside(const std::vector<Point> & points) const
@@ -438,6 +445,7 @@ bool Polygon::getParameters(
438445
polygon_name_.c_str());
439446
return false;
440447
}
448+
polygon_source = STATIC_POINTS;
441449
return true;
442450
} catch (const rclcpp::exceptions::ParameterUninitializedException &) {
443451
RCLCPP_INFO(
@@ -462,6 +470,7 @@ bool Polygon::getParameters(
462470
node, polygon_name_ + ".footprint_topic", rclcpp::PARAMETER_STRING);
463471
footprint_topic =
464472
node->get_parameter(polygon_name_ + ".footprint_topic").as_string();
473+
polygon_source = DYNAMIC_SUB;
465474
return true;
466475
}
467476
} catch (const rclcpp::exceptions::ParameterUninitializedException &) {
@@ -486,9 +495,8 @@ bool Polygon::getParameters(
486495
if (velocity_polygon->getParameters()) {
487496
velocity_polygons_.emplace_back(velocity_polygon);
488497
}
489-
490498
}
491-
499+
polygon_source = VELOCITY_POLYGON;
492500
} catch (const std::exception & ex) {
493501
RCLCPP_ERROR(
494502
logger_,

0 commit comments

Comments
 (0)