Skip to content

Commit

Permalink
add orientation filter, fix remaining TODOs
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Ferguson <[email protected]>
  • Loading branch information
mikeferguson committed Dec 13, 2024
1 parent d9ec380 commit 125a073
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ class GracefulController : public nav2_core::Controller
const std::vector<geometry_msgs::msg::PoseStamped> & poses,
std::vector<double> & distances);

/**
* @brief Control law requires proper orientations, not all planners provide them
* @param path Path to add orientations into
* @returns Path with orientations.
*/
std::vector<geometry_msgs::msg::PoseStamped> addOrientations(
const std::vector<geometry_msgs::msg::PoseStamped> & path);

std::shared_ptr<tf2_ros::Buffer> tf_buffer_;
std::string plugin_name_;
std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct Parameters
bool prefer_final_rotation;
double rotation_scaling_factor;
bool allow_backward;
bool add_orientations;
};

/**
Expand Down
38 changes: 36 additions & 2 deletions nav2_graceful_controller/src/graceful_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,16 @@ geometry_msgs::msg::TwistStamped GracefulController::computeVelocityCommands(
control_law_->setSlowdownRadius(params_->slowdown_radius);
control_law_->setSpeedLimit(params_->v_linear_min, params_->v_linear_max, params_->v_angular_max);

// Transform path to robot base frame and publish it
// Transform path to robot base frame
auto transformed_plan = path_handler_->transformGlobalPlan(
pose, params_->max_robot_pose_search_dist);

// Add proper orientations to plan, if desired
if (params_->add_orientations) {
transformed_plan.poses = addOrientations(transformed_plan.poses);

Check warning on line 141 in nav2_graceful_controller/src/graceful_controller.cpp

View check run for this annotation

Codecov / codecov/patch

nav2_graceful_controller/src/graceful_controller.cpp#L141

Added line #L141 was not covered by tests
}

// Publish plan for visualization
transformed_plan_pub_->publish(transformed_plan);

// Transform local frame to global frame to use in collision checking
Expand All @@ -148,7 +155,7 @@ geometry_msgs::msg::TwistStamped GracefulController::computeVelocityCommands(
logger_, "Could not transform %s to %s: %s",
costmap_ros_->getBaseFrameID().c_str(), costmap_ros_->getGlobalFrameID().c_str(),
ex.what());
return cmd_vel; // TODO(fergs): this seems bad?
throw nav2_core::NoValidControl("Could not transform");

Check warning on line 158 in nav2_graceful_controller/src/graceful_controller.cpp

View check run for this annotation

Codecov / codecov/patch

nav2_graceful_controller/src/graceful_controller.cpp#L158

Added line #L158 was not covered by tests
}

// Compute distance to goal as the path's integrated distance to account for path curvatures
Expand Down Expand Up @@ -422,6 +429,33 @@ void GracefulController::computeDistanceAlongPath(
}
}

std::vector<geometry_msgs::msg::PoseStamped> GracefulController::addOrientations(

Check warning on line 432 in nav2_graceful_controller/src/graceful_controller.cpp

View check run for this annotation

Codecov / codecov/patch

nav2_graceful_controller/src/graceful_controller.cpp#L432

Added line #L432 was not covered by tests
const std::vector<geometry_msgs::msg::PoseStamped> & path)
{
std::vector<geometry_msgs::msg::PoseStamped> oriented_path;
oriented_path.resize(path.size());
if (path.empty()) {

Check warning on line 437 in nav2_graceful_controller/src/graceful_controller.cpp

View check run for this annotation

Codecov / codecov/patch

nav2_graceful_controller/src/graceful_controller.cpp#L436-L437

Added lines #L436 - L437 were not covered by tests
// This really shouldn't happen
return oriented_path;
}

// The last pose will already be oriented since it is our goal
oriented_path.back() = path.back();

// For each pose, point at the next one
for (size_t i = 0; i < oriented_path.size() - 1; ++i) {

Check warning on line 446 in nav2_graceful_controller/src/graceful_controller.cpp

View check run for this annotation

Codecov / codecov/patch

nav2_graceful_controller/src/graceful_controller.cpp#L446

Added line #L446 was not covered by tests
// XY will be unchanged
oriented_path[i] = path[i];
// Get relative yaw angle
double dx = path[i + 1].pose.position.x - path[i].pose.position.x;
double dy = path[i + 1].pose.position.y - path[i].pose.position.y;
double yaw = std::atan2(dy, dx);
oriented_path[i].pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(yaw);

Check warning on line 453 in nav2_graceful_controller/src/graceful_controller.cpp

View check run for this annotation

Codecov / codecov/patch

nav2_graceful_controller/src/graceful_controller.cpp#L450-L453

Added lines #L450 - L453 were not covered by tests
}

return oriented_path;
}

} // namespace nav2_graceful_controller

// Register this controller as a nav2_core plugin
Expand Down
7 changes: 6 additions & 1 deletion nav2_graceful_controller/src/parameter_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ParameterHandler::ParameterHandler(
node, plugin_name_ + ".max_lookahead", rclcpp::ParameterValue(1.0));
declare_parameter_if_not_declared(
node, plugin_name_ + ".max_robot_pose_search_dist",
rclcpp::ParameterValue(costmap_size_x / 2.0)); // TODO(fergs): this is crazy
rclcpp::ParameterValue(costmap_size_x / 2.0));
declare_parameter_if_not_declared(node, plugin_name_ + ".k_phi", rclcpp::ParameterValue(3.0));
declare_parameter_if_not_declared(node, plugin_name_ + ".k_delta", rclcpp::ParameterValue(2.0));
declare_parameter_if_not_declared(node, plugin_name_ + ".beta", rclcpp::ParameterValue(0.2));
Expand All @@ -66,6 +66,8 @@ ParameterHandler::ParameterHandler(
node, plugin_name_ + ".rotation_scaling_factor", rclcpp::ParameterValue(0.5));
declare_parameter_if_not_declared(
node, plugin_name_ + ".allow_backward", rclcpp::ParameterValue(false));
declare_parameter_if_not_declared(
node, plugin_name_ + ".add_orientations", rclcpp::ParameterValue(false));

node->get_parameter(plugin_name_ + ".transform_tolerance", params_.transform_tolerance);
node->get_parameter(plugin_name_ + ".min_lookahead", params_.min_lookahead);
Expand Down Expand Up @@ -96,6 +98,7 @@ ParameterHandler::ParameterHandler(
node->get_parameter(plugin_name_ + ".prefer_final_rotation", params_.prefer_final_rotation);
node->get_parameter(plugin_name_ + ".rotation_scaling_factor", params_.rotation_scaling_factor);
node->get_parameter(plugin_name_ + ".allow_backward", params_.allow_backward);
node->get_parameter(plugin_name_ + ".add_orientations", params_.add_orientations);

if ((params_.initial_rotation_tolerance > 0.0) && params_.allow_backward) {
RCLCPP_WARN(
Expand Down Expand Up @@ -176,6 +179,8 @@ ParameterHandler::dynamicParametersCallback(std::vector<rclcpp::Parameter> param
continue;
}
params_.allow_backward = parameter.as_bool();
} else if (name == plugin_name_ + ".add_orientations") {
params_.add_orientations = parameter.as_bool();
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion nav2_graceful_controller/test/test_graceful_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ TEST(GracefulControllerTest, dynamicParameters) {
rclcpp::Parameter("test.prefer_final_rotation", false),
rclcpp::Parameter("test.rotation_scaling_factor", 13.0),
rclcpp::Parameter("test.v_angular_min_in_place", 14.0),
rclcpp::Parameter("test.allow_backward", true)});
rclcpp::Parameter("test.allow_backward", true),
rclcpp::Parameter("test.add_orientations", true)});

// Spin
rclcpp::spin_until_future_complete(node->get_node_base_interface(), results);
Expand All @@ -308,6 +309,7 @@ TEST(GracefulControllerTest, dynamicParameters) {
EXPECT_EQ(node->get_parameter("test.rotation_scaling_factor").as_double(), 13.0);
EXPECT_EQ(node->get_parameter("test.v_angular_min_in_place").as_double(), 14.0);
EXPECT_EQ(node->get_parameter("test.allow_backward").as_bool(), true);
EXPECT_EQ(node->get_parameter("test.add_orientations").as_bool(), true);

// Set initial rotation to true
results = params->set_parameters_atomically(
Expand Down

0 comments on commit 125a073

Please sign in to comment.