Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add velocity based polygon #3708

Merged
merged 36 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a2622f7
add velocity based polygon
kaichie Jul 23, 2023
ac4045f
fix header, copyright and variable name change
kaichie Jul 24, 2023
763b432
optimise polygon update
kaichie Jul 24, 2023
489392a
optimise duplicated code with setPolygonShape
kaichie Jul 24, 2023
727060c
add warning log for uncovered speed
kaichie Jul 24, 2023
3d9e979
Merge remote-tracking branch 'upstream/main' into add_polygon_velocity
kaichie Sep 2, 2023
50fd22c
update feedback
kaichie Sep 3, 2023
b6a05e2
rename polygon velocity to velocity polygon
kaichie Sep 3, 2023
b0a1093
cleanup
kaichie Sep 3, 2023
f1de5f9
fix typo
kaichie Sep 3, 2023
43c28d0
add dynamic support for velocity polygon
kaichie Sep 24, 2023
c2098b6
Merge remote-tracking branch 'upstream/main' into add_polygon_velocity
kaichie Sep 24, 2023
f6b2b19
wrap try catch for getting parameters
kaichie Sep 24, 2023
7f9f55a
update naming and linting
kaichie Sep 24, 2023
1230ede
use switch case
kaichie Sep 25, 2023
cd1af8a
Revert "use switch case"
kaichie Sep 26, 2023
2c4181e
fix proper return for invalid parameters
kaichie Sep 26, 2023
400d45e
remove topic parameter for velocity polygon
kaichie Sep 26, 2023
b97cbb7
fix formatting manually
kaichie Sep 26, 2023
5dc403b
continue if points are not defined
kaichie Sep 26, 2023
3db0146
Merge branch 'main' into add_polygon_velocity
kaichie Nov 16, 2023
8491ed4
Merge branch 'main' into add_polygon_velocity
kaichie Dec 5, 2023
a5c48a7
Merge branch 'main' into add_polygon_velocity
kaichie Jan 2, 2024
abab066
rewrite velocity polygon with polygon base class
kaichie Jan 7, 2024
620c7c2
update review comments and description
kaichie Jan 7, 2024
898d8c0
add VelocityPolygon to detector node
kaichie Jan 7, 2024
940225c
review update
kaichie Jan 16, 2024
0a7fe8c
fix cpplint
kaichie Jan 16, 2024
680f7ca
Merge remote-tracking branch 'upstream/main' into add_polygon_velocity
kaichie Jan 16, 2024
5c5d184
Update nav2_collision_monitor/src/velocity_polygon.cpp
kaichie Jan 17, 2024
49d7b71
add velocity polygon tests
kaichie Jan 17, 2024
ee69105
fix cpplint
kaichie Jan 17, 2024
10d59e1
add in-line comment
kaichie Jan 17, 2024
bbcb930
fix push back
kaichie Jan 17, 2024
df2cb95
minor change and update README
kaichie Jan 20, 2024
e150e85
update README
kaichie Jan 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nav2_collision_monitor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ set(library_name ${executable_name}_core)
add_library(${library_name} SHARED
src/collision_monitor_node.cpp
src/polygon.cpp
src/polygon_velocity.cpp
src/circle.cpp
src/source.cpp
src/scan.cpp
Expand Down
14 changes: 14 additions & 0 deletions nav2_collision_monitor/include/nav2_collision_monitor/polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "nav2_util/lifecycle_node.hpp"
#include "nav2_costmap_2d/footprint_subscriber.hpp"

#include "nav2_collision_monitor/polygon_velocity.hpp"
#include "nav2_collision_monitor/types.hpp"

namespace nav2_collision_monitor
Expand Down Expand Up @@ -127,6 +128,16 @@ class Polygon
*/
virtual bool isShapeSet();

/**
* @brief Returns true if using velocity based polygon
*/
bool isUsingPolygonVelocitySelector();

/**
* @brief Updates polygon by velocity (if any)
*/
void updatePolygonByVelocity(const Velocity & cmd_vel_in);

/**
* @brief Updates polygon from footprint subscriber (if any)
*/
Expand Down Expand Up @@ -243,6 +254,9 @@ class Polygon
/// @brief Polygon publisher for visualization purposes
rclcpp_lifecycle::LifecyclePublisher<geometry_msgs::msg::PolygonStamped>::SharedPtr polygon_pub_;

/// @brief Polygon velocity (if any)
std::vector<std::shared_ptr<PolygonVelocity>> polygon_velocity_;
kaichie marked this conversation as resolved.
Show resolved Hide resolved

/// @brief Polygon points (vertices) in a base_frame_id_
std::vector<Point> poly_;
}; // class Polygon
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) 2022 Samsung R&D Institute Russia
kaichie marked this conversation as resolved.
Show resolved Hide resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef NAV2_COLLISION_MONITOR__POLYGON_VELOCITY_HPP_
#define NAV2_COLLISION_MONITOR__POLYGON_VELOCITY_HPP_

#include <memory>
kaichie marked this conversation as resolved.
Show resolved Hide resolved
#include <string>
#include <vector>

#include "rclcpp/rclcpp.hpp"

#include "nav2_collision_monitor/types.hpp"

namespace nav2_collision_monitor
{

/**
* @brief Polygon velocity class.
* This class contains all the points of the polygon and
* the expected condition of the velocity based polygon.
*/
class PolygonVelocity
SteveMacenski marked this conversation as resolved.
Show resolved Hide resolved
{
public:
/**
* @brief PolygonVelocity constructor
* @param poly Points of the polygon
* @param polygon_name Name of polygon
* @param linear_max Maximum twist linear velocity
* @param linear_min Minimum twist linear velocity
* @param direction_max Maximum twist direction angle
* @param direction_min Minimum twist direction angle
* @param theta_max Maximum twist rotational speed
* @param theta_min Minimum twist rotational speed
*/
PolygonVelocity(
const std::vector<Point> & poly,
const std::string & polygon_name,
const double & linear_max,
const double & linear_min,
const double & direction_max,
const double & direction_min,
const double & theta_max,
const double & theta_min);
/**
* @brief PolygonVelocity destructor
*/
virtual ~PolygonVelocity();

/**
* @brief Check if the velocities and direction is in expected range.
* @param cmd_vel_in Robot twist command input
* @return True if speed and direction is within the condition
*/
bool isInRange(const Velocity & cmd_vel_in);

/**
* @brief Check if the velocities and direction is in expected range.
* @param cmd_vel_in Robot twist command input
* @return True if speed and direction is within the condition
*/
std::vector<Point> getPolygon();

protected:
// ----- Variables -----

/// @brief Collision monitor node logger stored for further usage
rclcpp::Logger logger_{rclcpp::get_logger("collision_monitor")};

// Basic parameters
/// @brief Points of the polygon
std::vector<Point> poly_;
/// @brief Name of polygon
std::string polygon_name_;
/// @brief Maximum twist linear velocity
double linear_max_;
/// @brief Minimum twist linear velocity
double linear_min_;
/// @brief Maximum twist direction angle
double direction_max_;
/// @brief Minimum twist direction angle
double direction_min_;
/// @brief Maximum twist rotational speed
double theta_max_;
/// @brief Minimum twist rotational speed
double theta_min_;
}; // class PolygonVelocity

} // namespace nav2_collision_monitor

#endif // NAV2_COLLISION_MONITOR__POLYGON_VELOCITY_HPP_
38 changes: 36 additions & 2 deletions nav2_collision_monitor/params/collision_monitor_params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,45 @@ collision_monitor:
FootprintApproach:
type: "polygon"
action_type: "approach"
footprint_topic: "/local_costmap/published_footprint"
# footprint_topic: "/local_costmap/published_footprint"
SteveMacenski marked this conversation as resolved.
Show resolved Hide resolved
time_before_collision: 2.0
simulation_time_step: 0.1
min_points: 6
visualize: False
visualize: True
polygon_pub_topic: "polygon_approach"
polygon_velocity: ["stop", "rotation", "translation_forward", "translation_backward"] # third priority
stop:
points: [0.25, 0.25, 0.25, -0.25, -0.25, -0.25, -0.25, 0.25]
linear_min: 0.0
linear_max: 0.01
direction_min: -3.14159
direction_max: 3.14159
SteveMacenski marked this conversation as resolved.
Show resolved Hide resolved
theta_min: 0.0
theta_max: 0.01
rotation:
points: [0.3, 0.3, 0.3, -0.3, -0.3, -0.3, -0.3, 0.3]
linear_min: 0.0
linear_max: 0.05
direction_min: -3.14159
direction_max: 3.14159
theta_min: -1.0
theta_max: 1.0
translation_forward:
points: [0.35, 0.3, 0.35, -0.3, -0.2, -0.3, -0.2, 0.3]
linear_min: 0.0
linear_max: 1.0
direction_min: -1.5708
direction_max: 1.5708
theta_min: -1.0
theta_max: 1.0
translation_backward:
points: [0.2, 0.3, 0.2, -0.3, -0.35, -0.3, -0.35, 0.3]
linear_min: 0.0
linear_max: 1.0
direction_min: 1.5708
direction_max: -1.5708
theta_min: -1.0
theta_max: 1.0
observation_sources: ["scan"]
scan:
type: "scan"
Expand Down
8 changes: 8 additions & 0 deletions nav2_collision_monitor/src/collision_monitor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ bool CollisionMonitor::processStopSlowdownLimit(
const Velocity & velocity,
Action & robot_action) const
{
if (polygon->isUsingPolygonVelocitySelector()) {
polygon->updatePolygonByVelocity(velocity);
}

if (!polygon->isShapeSet()) {
return false;
}
Expand Down Expand Up @@ -478,6 +482,10 @@ bool CollisionMonitor::processApproach(
const Velocity & velocity,
Action & robot_action) const
{
if (polygon->isUsingPolygonVelocitySelector()) {
polygon->updatePolygonByVelocity(velocity);
}

kaichie marked this conversation as resolved.
Show resolved Hide resolved
if (!polygon->isShapeSet()) {
return false;
}
Expand Down
152 changes: 141 additions & 11 deletions nav2_collision_monitor/src/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,34 @@ bool Polygon::isShapeSet()
return true;
}

bool Polygon::isUsingPolygonVelocitySelector()
{
return !polygon_velocity_.empty();
}

void Polygon::updatePolygonByVelocity(const Velocity & cmd_vel_in)
{
for (auto & polygon : polygon_velocity_) {
kaichie marked this conversation as resolved.
Show resolved Hide resolved

if (polygon->isInRange(cmd_vel_in)) {
// Set the polygon that is within the speed range
poly_ = polygon->getPolygon();

// Update visualization polygon
polygon_.polygon.points.clear();
for (const Point & p : poly_) {
geometry_msgs::msg::Point32 p_s;
p_s.x = p.x;
p_s.y = p.y;
// p_s.z will remain 0.0
polygon_.polygon.points.push_back(p_s);
}

return;
AlexeyMerzlyakov marked this conversation as resolved.
Show resolved Hide resolved
}
}
kaichie marked this conversation as resolved.
Show resolved Hide resolved
}

void Polygon::updatePolygon()
{
if (footprint_sub_ != nullptr) {
Expand Down Expand Up @@ -420,19 +448,121 @@ bool Polygon::getParameters(
polygon_name_.c_str());
}

if (action_type_ == STOP || action_type_ == SLOWDOWN || action_type_ == LIMIT) {
// Dynamic polygon will be used
try {
SteveMacenski marked this conversation as resolved.
Show resolved Hide resolved
if (action_type_ == STOP || action_type_ == SLOWDOWN || action_type_ == LIMIT) {
// Dynamic polygon will be used
nav2_util::declare_parameter_if_not_declared(
node, polygon_name_ + ".polygon_sub_topic", rclcpp::PARAMETER_STRING);
polygon_sub_topic =
node->get_parameter(polygon_name_ + ".polygon_sub_topic").as_string();
return true;
} else if (action_type_ == APPROACH) {
// Obtain the footprint topic to make a footprint subscription for approach polygon
nav2_util::declare_parameter_if_not_declared(
node, polygon_name_ + ".footprint_topic", rclcpp::PARAMETER_STRING);
footprint_topic =
node->get_parameter(polygon_name_ + ".footprint_topic").as_string();
return true;
}
} catch (const rclcpp::exceptions::ParameterUninitializedException &) {
RCLCPP_INFO(
logger_,
"[%s]: Polygon topic are not defined. Using polygon velocity instead.",
polygon_name_.c_str());
}

// Polygon velocity will be used
nav2_util::declare_parameter_if_not_declared(
node, polygon_name_ + ".polygon_velocity", rclcpp::PARAMETER_STRING_ARRAY);
std::vector<std::string> polygon_velocity_array = node->get_parameter(
polygon_name_ + ".polygon_velocity").as_string_array();

for (std::string polygon_velocity_name : polygon_velocity_array) {

std::string polygon_name = polygon_velocity_name;

nav2_util::declare_parameter_if_not_declared(
node, polygon_name_ + ".polygon_sub_topic", rclcpp::PARAMETER_STRING);
polygon_sub_topic =
node->get_parameter(polygon_name_ + ".polygon_sub_topic").as_string();
} else if (action_type_ == APPROACH) {
// Obtain the footprint topic to make a footprint subscription for approach polygon
node, polygon_name_ + "." + polygon_velocity_name + ".points",
rclcpp::PARAMETER_DOUBLE_ARRAY);
std::vector<double> polygon_points = node->get_parameter(
polygon_name_ + "." + polygon_velocity_name + ".points").as_double_array();

// Check for points format correctness
if (polygon_points.size() <= 6 || polygon_points.size() % 2 != 0) {
RCLCPP_ERROR(
logger_,
"[%s]: Polygon has incorrect points description", polygon_name_.c_str());
return false;
}

// Obtain polygon vertices
Point point;
std::vector<nav2_collision_monitor::Point> poly;
bool first = true;
for (double val : polygon_points) {
if (first) {
point.x = val;
} else {
point.y = val;
poly.push_back(point);
}
first = !first;
}
kaichie marked this conversation as resolved.
Show resolved Hide resolved

// linear_max param
nav2_util::declare_parameter_if_not_declared(
SteveMacenski marked this conversation as resolved.
Show resolved Hide resolved
node, polygon_name_ + "." + polygon_velocity_name + ".linear_max",
rclcpp::ParameterValue(0.0));
double linear_max =
node->get_parameter(
polygon_name_ + "." + polygon_velocity_name +
".linear_max").as_double();

// linear_min param
nav2_util::declare_parameter_if_not_declared(
node, polygon_name_ + "." + polygon_velocity_name + ".linear_min",
rclcpp::ParameterValue(0.0));
double linear_min =
node->get_parameter(
polygon_name_ + "." + polygon_velocity_name +
".linear_min").as_double();

// direction_max param
nav2_util::declare_parameter_if_not_declared(
node, polygon_name_ + "." + polygon_velocity_name + ".direction_max",
rclcpp::ParameterValue(0.0));
double direction_max =
node->get_parameter(
polygon_name_ + "." + polygon_velocity_name +
".direction_max").as_double();

// direction_min param
nav2_util::declare_parameter_if_not_declared(
node, polygon_name_ + "." + polygon_velocity_name + ".direction_min",
rclcpp::ParameterValue(0.0));
double direction_min =
node->get_parameter(
polygon_name_ + "." + polygon_velocity_name +
".direction_min").as_double();

// theta_max param
nav2_util::declare_parameter_if_not_declared(
node, polygon_name_ + "." + polygon_velocity_name + ".theta_max", rclcpp::ParameterValue(
0.0));
double theta_max =
node->get_parameter(polygon_name_ + "." + polygon_velocity_name + ".theta_max").as_double();

// theta_min param
nav2_util::declare_parameter_if_not_declared(
node, polygon_name_ + ".footprint_topic",
rclcpp::ParameterValue("local_costmap/published_footprint"));
footprint_topic =
node->get_parameter(polygon_name_ + ".footprint_topic").as_string();
node, polygon_name_ + "." + polygon_velocity_name + ".theta_min", rclcpp::ParameterValue(
0.0));
double theta_min =
node->get_parameter(polygon_name_ + "." + polygon_velocity_name + ".theta_min").as_double();

polygon_velocity_.push_back(
std::make_shared<PolygonVelocity>(
poly, polygon_name, linear_max, linear_min, direction_max,
direction_min, theta_max, theta_min));
}
} catch (const std::exception & ex) {
RCLCPP_ERROR(
Expand Down
Loading