Skip to content

Commit 5423eba

Browse files
Docking backwards as plugin param (#5079)
* Replace dock_backwards_ param with dock plugin function Signed-off-by: Alberto Tudela <[email protected]> * Deprecated dock_backwards warning Signed-off-by: Alberto Tudela <[email protected]> * Convert dockDirection from bool to enum Signed-off-by: Alberto Tudela <[email protected]> * Minor fixes Signed-off-by: Alberto Tudela <[email protected]> * Improve deprecated param handling Signed-off-by: Alberto Tudela <[email protected]> * Set default to forward Signed-off-by: Alberto Tudela <[email protected]> * Added tests Signed-off-by: Alberto Tudela <[email protected]> * Update nav2_docking/README.md Co-authored-by: Steve Macenski <[email protected]> Signed-off-by: Alberto Tudela <[email protected]> * Upper and others Signed-off-by: Alberto Tudela <[email protected]> * Revert declare_parameters_from_overrides Signed-off-by: Alberto Tudela <[email protected]> * Added throw on configure plugin Signed-off-by: Alberto Tudela <[email protected]> * Remove node_utils Signed-off-by: Alberto Tudela <[email protected]> --------- Signed-off-by: Alberto Tudela <[email protected]> Co-authored-by: Steve Macenski <[email protected]>
1 parent fbd3084 commit 5423eba

File tree

20 files changed

+286
-57
lines changed

20 files changed

+286
-57
lines changed

nav2_bringup/params/nav2_params.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ docking_server:
439439
max_retries: 3
440440
base_frame: "base_link"
441441
fixed_frame: "odom"
442-
dock_backwards: false
443442
dock_prestaging_tolerance: 0.5
444443
service_introspection_mode: "disabled"
445444

@@ -452,6 +451,7 @@ docking_server:
452451
use_external_detection_pose: true
453452
use_battery_status: false # true
454453
use_stall_detection: false # true
454+
dock_direction: "forward"
455455

456456
external_detection_timeout: 1.0
457457
external_detection_translation_x: -0.18

nav2_docking/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ For debugging purposes, there are several publishers which can be used with RVIZ
207207
| max_retries | Maximum number of retries to attempt | int | 3 |
208208
| base_frame | Robot's base frame for control law | string | "base_link" |
209209
| fixed_frame | Fixed frame to use, recommended to be a smooth odometry frame **not** map | string | "odom" |
210-
| dock_backwards | Whether the robot is docking with the dock forward or backward in motion | bool | false |
211210
| dock_prestaging_tolerance | L2 distance in X,Y,Theta from the staging pose to bypass navigation | double | 0.5 |
212211
| dock_plugins | A set of dock plugins to load | vector<string> | N/A |
213212
| dock_database | The filepath to the dock database to use for this environment | string | N/A |
@@ -251,6 +250,7 @@ Note: `dock_plugins` and either `docks` or `dock_database` are required.
251250
| docking_threshold | If not using stall detection, the pose threshold to the docking pose where `isDocked() = true` | double | 0.05 |
252251
| staging_x_offset | Staging pose offset forward (negative) of dock pose (m) | double | -0.7 |
253252
| staging_yaw_offset | Staging pose angle relative to dock pose (rad) | double | 0.0 |
253+
| dock_direction | Whether the robot is docking with the dock forward or backward in motion | string | "forward" or "backward" |
254254

255255
Note: The external detection rotation angles are setup to work out of the box with Apriltags detectors in `image_proc` and `isaac_ros`.
256256

nav2_docking/opennav_docking/CMakeLists.txt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ add_library(controller SHARED
3333
)
3434
target_include_directories(controller
3535
PUBLIC
36-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
37-
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
36+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
37+
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
3838
)
3939
target_link_libraries(controller PUBLIC
4040
${geometry_msgs_TARGETS}
@@ -51,8 +51,8 @@ add_library(${library_name} SHARED
5151
)
5252
target_include_directories(${library_name}
5353
PUBLIC
54-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
55-
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
54+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
55+
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
5656
)
5757
target_link_libraries(${library_name} PUBLIC
5858
angles::angles
@@ -79,8 +79,8 @@ add_library(pose_filter SHARED
7979
)
8080
target_include_directories(pose_filter
8181
PUBLIC
82-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
83-
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
82+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
83+
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
8484
)
8585
target_link_libraries(pose_filter PUBLIC
8686
${geometry_msgs_TARGETS}
@@ -95,8 +95,8 @@ add_executable(${executable_name}
9595
)
9696
target_include_directories(${executable_name}
9797
PUBLIC
98-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
99-
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
98+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
99+
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
100100
)
101101
target_link_libraries(${executable_name} PRIVATE ${library_name} rclcpp::rclcpp)
102102

@@ -107,8 +107,8 @@ add_library(simple_charging_dock SHARED
107107
)
108108
target_include_directories(simple_charging_dock
109109
PUBLIC
110-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
111-
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
110+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
111+
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
112112
)
113113
target_link_libraries(simple_charging_dock PUBLIC
114114
${geometry_msgs_TARGETS}
@@ -121,6 +121,7 @@ target_link_libraries(simple_charging_dock PUBLIC
121121
tf2_ros::tf2_ros
122122
)
123123
target_link_libraries(simple_charging_dock PRIVATE
124+
angles::angles
124125
nav2_util::nav2_util_core
125126
pluginlib::pluginlib
126127
tf2::tf2
@@ -131,8 +132,8 @@ add_library(simple_non_charging_dock SHARED
131132
)
132133
target_include_directories(simple_non_charging_dock
133134
PUBLIC
134-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
135-
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
135+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
136+
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>"
136137
)
137138
target_link_libraries(simple_non_charging_dock PUBLIC
138139
${geometry_msgs_TARGETS}
@@ -145,6 +146,7 @@ target_link_libraries(simple_non_charging_dock PUBLIC
145146
tf2_ros::tf2_ros
146147
)
147148
target_link_libraries(simple_non_charging_dock PRIVATE
149+
angles::angles
148150
nav2_util::nav2_util_core
149151
pluginlib::pluginlib
150152
tf2::tf2

nav2_docking/opennav_docking/include/opennav_docking/docking_server.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
#ifndef OPENNAV_DOCKING__DOCKING_SERVER_HPP_
1616
#define OPENNAV_DOCKING__DOCKING_SERVER_HPP_
1717

18-
#include <vector>
18+
#include <functional>
1919
#include <memory>
20-
#include <string>
2120
#include <mutex>
22-
#include <functional>
21+
#include <optional>
22+
#include <string>
23+
#include <vector>
2324

2425
#include "rclcpp/rclcpp.hpp"
2526
#include "nav2_util/lifecycle_node.hpp"
@@ -87,10 +88,11 @@ class DockingServer : public nav2_util::LifecycleNode
8788
* @brief Use control law and dock perception to approach the charge dock.
8889
* @param dock Dock instance, gets queried for refined pose and docked state.
8990
* @param dock_pose Initial dock pose, will be refined by perception.
91+
* @param backward If true, the robot will drive backwards.
9092
* @returns True if dock successfully approached, False if cancelled. For
9193
* any internal error, will throw.
9294
*/
93-
bool approachDock(Dock * dock, geometry_msgs::msg::PoseStamped & dock_pose);
95+
bool approachDock(Dock * dock, geometry_msgs::msg::PoseStamped & dock_pose, bool backward);
9496

9597
/**
9698
* @brief Wait for charging to begin.
@@ -102,9 +104,10 @@ class DockingServer : public nav2_util::LifecycleNode
102104
/**
103105
* @brief Reset the robot for another approach by controlling back to staging pose.
104106
* @param staging_pose The target pose that will reset for another approach.
107+
* @param backward If true, the robot will drive backwards.
105108
* @returns True if reset is successful.
106109
*/
107-
bool resetApproach(const geometry_msgs::msg::PoseStamped & staging_pose);
110+
bool resetApproach(const geometry_msgs::msg::PoseStamped & staging_pose, bool backward);
108111

109112
/**
110113
* @brief Run a single iteration of the control loop to approach a pose.
@@ -242,7 +245,7 @@ class DockingServer : public nav2_util::LifecycleNode
242245
// This is our fixed frame for controlling - typically "odom"
243246
std::string fixed_frame_;
244247
// Does the robot drive backwards onto the dock? Default is forwards
245-
bool dock_backwards_;
248+
std::optional<bool> dock_backwards_;
246249
// The tolerance to the dock's staging pose not requiring navigation
247250
double dock_prestaging_tolerance_;
248251

nav2_docking/opennav_docking/include/opennav_docking/utils.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "nav2_util/geometry_utils.hpp"
2525
#include "angles/angles.h"
2626
#include "opennav_docking/types.hpp"
27+
#include "opennav_docking_core/charging_dock.hpp"
2728
#include "tf2/utils.hpp"
2829

2930
namespace utils
@@ -33,6 +34,7 @@ using rclcpp::ParameterType::PARAMETER_STRING;
3334
using rclcpp::ParameterType::PARAMETER_STRING_ARRAY;
3435
using rclcpp::ParameterType::PARAMETER_DOUBLE_ARRAY;
3536
using nav2_util::geometry_utils::orientationAroundZAxis;
37+
using opennav_docking_core::DockDirection;
3638

3739
/**
3840
* @brief Parse a yaml file to obtain docks
@@ -183,6 +185,21 @@ inline double l2Norm(const geometry_msgs::msg::Pose & a, const geometry_msgs::ms
183185
delta_angle * delta_angle);
184186
}
185187

188+
inline DockDirection getDockDirectionFromString(const std::string & direction)
189+
{
190+
auto upper_direction = direction;
191+
std::transform(
192+
upper_direction.begin(), upper_direction.end(), upper_direction.begin(), ::toupper);
193+
194+
if (upper_direction == "FORWARD") {
195+
return DockDirection::FORWARD;
196+
} else if (upper_direction == "BACKWARD") {
197+
return DockDirection::BACKWARD;
198+
} else {
199+
return DockDirection::UNKNOWN;
200+
}
201+
}
202+
186203
} // namespace utils
187204

188205
#endif // OPENNAV_DOCKING__UTILS_HPP_

nav2_docking/opennav_docking/src/docking_server.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ DockingServer::DockingServer(const rclcpp::NodeOptions & options)
3838
declare_parameter("max_retries", 3);
3939
declare_parameter("base_frame", "base_link");
4040
declare_parameter("fixed_frame", "odom");
41-
declare_parameter("dock_backwards", false);
41+
declare_parameter("dock_backwards", rclcpp::PARAMETER_BOOL);
4242
declare_parameter("dock_prestaging_tolerance", 0.5);
4343
}
4444

@@ -57,10 +57,19 @@ DockingServer::on_configure(const rclcpp_lifecycle::State & state)
5757
get_parameter("max_retries", max_retries_);
5858
get_parameter("base_frame", base_frame_);
5959
get_parameter("fixed_frame", fixed_frame_);
60-
get_parameter("dock_backwards", dock_backwards_);
6160
get_parameter("dock_prestaging_tolerance", dock_prestaging_tolerance_);
6261
RCLCPP_INFO(get_logger(), "Controller frequency set to %.4fHz", controller_frequency_);
6362

63+
bool dock_backwards;
64+
try {
65+
if (get_parameter("dock_backwards", dock_backwards)) {
66+
dock_backwards_ = dock_backwards;
67+
RCLCPP_WARN(get_logger(), "Parameter dock_backwards is deprecated. "
68+
"Please use the dock_direction parameter in your dock plugin instead.");
69+
}
70+
} catch (rclcpp::exceptions::ParameterUninitializedException & ex) {
71+
}
72+
6473
vel_publisher_ = std::make_unique<nav2_util::TwistPublisher>(node, "cmd_vel", 1);
6574
tf2_buffer_ = std::make_shared<tf2_ros::Buffer>(node->get_clock());
6675

@@ -155,6 +164,7 @@ DockingServer::on_cleanup(const rclcpp_lifecycle::State & /*state*/)
155164
curr_dock_type_.clear();
156165
controller_.reset();
157166
vel_publisher_.reset();
167+
dock_backwards_.reset();
158168
return nav2_util::CallbackReturn::SUCCESS;
159169
}
160170

@@ -274,12 +284,17 @@ void DockingServer::dockRobot()
274284
doInitialPerception(dock, dock_pose);
275285
RCLCPP_INFO(get_logger(), "Successful initial dock detection");
276286

287+
// Get the direction of the movement
288+
bool dock_backward = dock_backwards_.has_value() ?
289+
dock_backwards_.value() :
290+
(dock->plugin->getDockDirection() == opennav_docking_core::DockDirection::BACKWARD);
291+
277292
// Docking control loop: while not docked, run controller
278293
rclcpp::Time dock_contact_time;
279294
while (rclcpp::ok()) {
280295
try {
281296
// Approach the dock using control law
282-
if (approachDock(dock, dock_pose)) {
297+
if (approachDock(dock, dock_pose, dock_backward)) {
283298
// We are docked, wait for charging to begin
284299
RCLCPP_INFO(
285300
get_logger(), "Made contact with dock, waiting for charge to start (if applicable).");
@@ -312,7 +327,7 @@ void DockingServer::dockRobot()
312327
}
313328

314329
// Reset to staging pose to try again
315-
if (!resetApproach(dock->getStagingPose())) {
330+
if (!resetApproach(dock->getStagingPose(), dock_backward)) {
316331
// Cancelled, preempted, or shutting down
317332
stashDockData(goal->use_dock_id, dock, false);
318333
publishZeroVelocity();
@@ -409,11 +424,13 @@ void DockingServer::doInitialPerception(Dock * dock, geometry_msgs::msg::PoseSta
409424
}
410425
}
411426

412-
bool DockingServer::approachDock(Dock * dock, geometry_msgs::msg::PoseStamped & dock_pose)
427+
bool DockingServer::approachDock(
428+
Dock * dock, geometry_msgs::msg::PoseStamped & dock_pose, bool backward)
413429
{
414430
rclcpp::Rate loop_rate(controller_frequency_);
415431
auto start = this->now();
416432
auto timeout = rclcpp::Duration::from_seconds(dock_approach_timeout_);
433+
417434
while (rclcpp::ok()) {
418435
publishDockingFeedback(DockRobot::Feedback::CONTROLLING);
419436

@@ -440,7 +457,7 @@ bool DockingServer::approachDock(Dock * dock, geometry_msgs::msg::PoseStamped &
440457

441458
// Make sure that the target pose is pointing at the robot when moving backwards
442459
// This is to ensure that the robot doesn't try to dock from the wrong side
443-
if (dock_backwards_) {
460+
if (backward) {
444461
target_pose.pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(
445462
tf2::getYaw(target_pose.pose.orientation) + M_PI);
446463
}
@@ -458,9 +475,7 @@ bool DockingServer::approachDock(Dock * dock, geometry_msgs::msg::PoseStamped &
458475
// Compute and publish controls
459476
auto command = std::make_unique<geometry_msgs::msg::TwistStamped>();
460477
command->header.stamp = now();
461-
if (!controller_->computeVelocityCommand(target_pose.pose, command->twist, true,
462-
dock_backwards_))
463-
{
478+
if (!controller_->computeVelocityCommand(target_pose.pose, command->twist, true, backward)) {
464479
throw opennav_docking_core::FailedToControl("Failed to get control");
465480
}
466481
vel_publisher_->publish(std::move(command));
@@ -507,7 +522,8 @@ bool DockingServer::waitForCharge(Dock * dock)
507522
return false;
508523
}
509524

510-
bool DockingServer::resetApproach(const geometry_msgs::msg::PoseStamped & staging_pose)
525+
bool DockingServer::resetApproach(
526+
const geometry_msgs::msg::PoseStamped & staging_pose, bool backward)
511527
{
512528
rclcpp::Rate loop_rate(controller_frequency_);
513529
auto start = this->now();
@@ -527,7 +543,7 @@ bool DockingServer::resetApproach(const geometry_msgs::msg::PoseStamped & stagin
527543
command->header.stamp = now();
528544
if (getCommandToPose(
529545
command->twist, staging_pose, undock_linear_tolerance_, undock_angular_tolerance_, false,
530-
!dock_backwards_))
546+
!backward))
531547
{
532548
return true;
533549
}
@@ -653,9 +669,13 @@ void DockingServer::undockRobot()
653669
// Get command to approach staging pose
654670
auto command = std::make_unique<geometry_msgs::msg::TwistStamped>();
655671
command->header.stamp = now();
672+
bool dock_backward = dock_backwards_.has_value() ?
673+
dock_backwards_.value() :
674+
(dock->getDockDirection() == opennav_docking_core::DockDirection::BACKWARD);
675+
656676
if (getCommandToPose(
657677
command->twist, staging_pose, undock_linear_tolerance_, undock_angular_tolerance_, false,
658-
!dock_backwards_))
678+
!dock_backward))
659679
{
660680
RCLCPP_INFO(get_logger(), "Robot has reached staging pose");
661681
// Have reached staging_pose

nav2_docking/opennav_docking/src/simple_charging_dock.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "nav2_util/node_utils.hpp"
1818
#include "opennav_docking/simple_charging_dock.hpp"
19+
#include "opennav_docking/utils.hpp"
1920

2021
namespace opennav_docking
2122
{
@@ -78,6 +79,10 @@ void SimpleChargingDock::configure(
7879
nav2_util::declare_parameter_if_not_declared(
7980
node_, name + ".staging_yaw_offset", rclcpp::ParameterValue(0.0));
8081

82+
// Direction of docking
83+
nav2_util::declare_parameter_if_not_declared(
84+
node_, name + ".dock_direction", rclcpp::ParameterValue(std::string("forward")));
85+
8186
node_->get_parameter(name + ".use_battery_status", use_battery_status_);
8287
node_->get_parameter(name + ".use_external_detection_pose", use_external_detection_pose_);
8388
node_->get_parameter(name + ".external_detection_timeout", external_detection_timeout_);
@@ -98,6 +103,13 @@ void SimpleChargingDock::configure(
98103
node_->get_parameter(name + ".staging_x_offset", staging_x_offset_);
99104
node_->get_parameter(name + ".staging_yaw_offset", staging_yaw_offset_);
100105

106+
std::string dock_direction;
107+
node_->get_parameter(name + ".dock_direction", dock_direction);
108+
dock_direction_ = utils::getDockDirectionFromString(dock_direction);
109+
if (dock_direction_ == opennav_docking_core::DockDirection::UNKNOWN) {
110+
throw std::runtime_error{"Dock direction is not valid. Valid options are: forward or backward"};
111+
}
112+
101113
// Setup filter
102114
double filter_coef;
103115
node_->get_parameter(name + ".filter_coef", filter_coef);

0 commit comments

Comments
 (0)