Skip to content

Commit

Permalink
Correction of merge
Browse files Browse the repository at this point in the history
Signed-off-by: Jakubach <jakubach@gmail.com>
  • Loading branch information
Jakubach committed Nov 9, 2024
1 parent 945acf1 commit 9ea7849
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ class Controller
* @returns True if command is valid, false otherwise.
*/
bool computeVelocityCommand(
const geometry_msgs::msg::Pose & pose, geometry_msgs::msg::Twist & cmd,
const geometry_msgs::msg::Pose & pose,const geometry_msgs::msg::Pose & robot_pose, geometry_msgs::msg::Twist & cmd,
bool backward = false);


/**
* @brief Callback executed when a parameter change is detected
Expand All @@ -69,6 +68,7 @@ class Controller

protected:
std::unique_ptr<nav2_graceful_controller::SmoothControlLaw> control_law_;

double k_phi_, k_delta_, beta_, lambda_;
double slowdown_radius_, v_linear_min_, v_linear_max_, v_angular_max_,v_angular_min_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ class DockingServer : public nav2_util::LifecycleNode
double initial_rotation_min_angle_;
// Enable a rotation in place to the goal before starting the path. The control law may generate large sweeping arcs to the goal pose, depending on the initial robot orientation and k_phi, k_delta.
bool initial_rotation_;
// Enable aproaching a docking station only with initial detection without updates
bool dock_backwards_without_sensor_;

// This is a class member so it can be accessed in publish feedback
rclcpp::Time action_start_time_;
Expand Down
11 changes: 8 additions & 3 deletions nav2_docking/opennav_docking/src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ Controller::Controller(const rclcpp_lifecycle::LifecycleNode::SharedPtr & node)
node->get_parameter("controller.v_angular_min", v_angular_min_);
node->get_parameter("controller.v_angular_max", v_angular_max_);
node->get_parameter("controller.slowdown_radius", slowdown_radius_);

control_law_ = std::make_unique<nav2_graceful_controller::SmoothControlLaw>(
k_phi_, k_delta_, beta_, lambda_, slowdown_radius_, v_linear_min_, v_linear_max_, v_angular_max_);
k_phi_, k_delta_, beta_, lambda_, slowdown_radius_, v_linear_min_, v_linear_max_,
v_angular_max_);

// Add callback for dynamic parameters
dyn_params_handler_ = node->add_on_set_parameters_callback(
std::bind(&Controller::dynamicParametersCallback, this, std::placeholders::_1));

}

bool Controller::computeVelocityCommand(
Expand All @@ -72,10 +73,12 @@ rcl_interfaces::msg::SetParametersResult
Controller::dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters)
{
std::lock_guard<std::mutex> lock(dynamic_params_lock_);

rcl_interfaces::msg::SetParametersResult result;
for (auto parameter : parameters) {
const auto & type = parameter.get_type();
const auto & name = parameter.get_name();

if (type == rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE) {
if (name == "controller.k_phi") {
k_phi_ = parameter.as_double();
Expand All @@ -96,12 +99,14 @@ Controller::dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters)
} else if (name == "controller.slowdown_radius") {
slowdown_radius_ = parameter.as_double();
}

// Update the smooth control law with the new params
control_law_->setCurvatureConstants(k_phi_, k_delta_, beta_, lambda_);
control_law_->setSlowdownRadius(slowdown_radius_);
control_law_->setSpeedLimit(v_linear_min_, v_linear_max_, v_angular_max_);
}
}

result.successful = true;
return result;
}
Expand Down
30 changes: 23 additions & 7 deletions nav2_docking/opennav_docking/src/docking_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ DockingServer::DockingServer(const rclcpp::NodeOptions & options)
declare_parameter("dock_backwards", false);
declare_parameter("dock_prestaging_tolerance", 0.5);
declare_parameter("initial_rotation", true);
declare_parameter("initial_rotation_min_angle", 0.3);
declare_parameter("initial_rotation_min_angle", 0.5);
declare_parameter("dock_backwards_without_sensor", true);
}

nav2_util::CallbackReturn
Expand All @@ -63,6 +64,7 @@ DockingServer::on_configure(const rclcpp_lifecycle::State & state)
get_parameter("dock_prestaging_tolerance", dock_prestaging_tolerance_);
get_parameter("initial_rotation", initial_rotation_);
get_parameter("initial_rotation_min_angle", initial_rotation_min_angle_);
get_parameter("dock_backwards_without_sensor", dock_backwards_without_sensor_);
RCLCPP_INFO(get_logger(), "Controller frequency set to %.4fHz", controller_frequency_);

vel_publisher_ = std::make_unique<nav2_util::TwistPublisher>(node, "cmd_vel", 1);
Expand Down Expand Up @@ -389,6 +391,17 @@ void DockingServer::doInitialPerception(Dock * dock, geometry_msgs::msg::PoseSta
publishDockingFeedback(DockRobot::Feedback::INITIAL_PERCEPTION);
rclcpp::Rate loop_rate(controller_frequency_);
auto start = this->now();
// If docking without sensors, stop the robot for short time to get a stable and quality detection of docking pose
if(dock_backwards_without_sensor_){
publishZeroVelocity();
while (rclcpp::ok()) {
if (this->now() - start > rclcpp::Duration::from_seconds(1.0)) {
break;
}
}
}

start = this->now();
auto timeout = rclcpp::Duration::from_seconds(initial_perception_timeout_);
while (!dock->plugin->getRefinedPose(dock_pose, dock->id)) {
if (this->now() - start > timeout) {
Expand Down Expand Up @@ -426,7 +439,7 @@ bool DockingServer::approachDock(Dock * dock, geometry_msgs::msg::PoseStamped &
}

// Update perception
if (!dock->plugin->getRefinedPose(dock_pose, dock->id)) {
if (!dock_backwards_without_sensor_ && !dock->plugin->getRefinedPose(dock_pose, dock->id)) {
throw opennav_docking_core::FailedToDetectDock("Failed dock detection");
}

Expand All @@ -442,16 +455,16 @@ bool DockingServer::approachDock(Dock * dock, geometry_msgs::msg::PoseStamped &
}

// Compute and publish controls
geometry_msgs::msg::Twist command;
auto command = std::make_unique<geometry_msgs::msg::TwistStamped>();

geometry_msgs::msg::PoseStamped robot_pose = getRobotPoseInFrame(target_pose.header.frame_id);
const double angle_to_goal = angles::shortest_angular_distance(
tf2::getYaw(robot_pose.pose.orientation), atan2(robot_pose.pose.position.y - target_pose.pose.position.y, robot_pose.pose.position.x - target_pose.pose.position.x));

// Compute and publish controls
auto command = std::make_unique<geometry_msgs::msg::TwistStamped>();
command->header.stamp = now();
if (!controller_->computeVelocityCommand(target_pose.pose, command->twist, dock_backwards_)) {
if(initial_rotation_ && fabs(angle_to_goal) > initial_rotation_min_angle_){
command->twist = controller_->rotateToTarget(angle_to_goal);
}
else if (!controller_->computeVelocityCommand(target_pose.pose, robot_pose.pose, command->twist, dock_backwards_)) {
throw opennav_docking_core::FailedToControl("Failed to get control");
}
vel_publisher_->publish(std::move(command));
Expand Down Expand Up @@ -751,6 +764,9 @@ DockingServer::dynamicParametersCallback(std::vector<rclcpp::Parameter> paramete
if (name == "initial_rotation") {
initial_rotation_ = parameter.as_bool();
}
if (name == "dock_backwards_without_sensor") {
dock_backwards_without_sensor_ = parameter.as_bool();
}
}
}

Expand Down

0 comments on commit 9ea7849

Please sign in to comment.