-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SpeedController nodes to adjust replanning rate according to speed (
#1744) * Add SpeedController decorator node and update default BTs * Fix lint errors * Add OdomSmoother class and fix SpeedController to use smoothed velocity Signed-off-by: Sarthak Mittal <[email protected]> * Add duration input port to tree nodes xml for groot Signed-off-by: Sarthak Mittal <[email protected]> * Revert BT images Signed-off-by: Sarthak Mittal <[email protected]> * Address reviewer's comments Signed-off-by: Sarthak Mittal <[email protected]> * Throw BT exception when rate <= 0 Signed-off-by: Sarthak Mittal <[email protected]> * Add simple behavior tree using speed controller Signed-off-by: Sarthak Mittal <[email protected]> * Set default value to maximum rate on IDLE * Add test for speed controller Signed-off-by: Sarthak Mittal <[email protected]> * Use smart pointers in tests Signed-off-by: Sarthak Mittal <[email protected]> * Fix test Signed-off-by: Sarthak Mittal <[email protected]> * Move speed controller header file Signed-off-by: Sarthak Mittal <[email protected]> * Update speed controller to reset only on new goal Signed-off-by: Sarthak Mittal <[email protected]> * Fix test Signed-off-by: Sarthak Mittal <[email protected]> * Add params to parameter list Signed-off-by: Sarthak Mittal <[email protected]> * Fix memory leak Signed-off-by: Sarthak Mittal <[email protected]>
- Loading branch information
Showing
21 changed files
with
750 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
nav2_behavior_tree/include/nav2_behavior_tree/plugins/speed_controller.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// Copyright (c) 2020 Sarthak Mittal | ||
// | ||
// 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_BEHAVIOR_TREE__PLUGINS__SPEED_CONTROLLER_HPP_ | ||
#define NAV2_BEHAVIOR_TREE__PLUGINS__SPEED_CONTROLLER_HPP_ | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <deque> | ||
|
||
#include "nav_msgs/msg/odometry.hpp" | ||
#include "nav2_util/odometry_utils.hpp" | ||
|
||
#include "behaviortree_cpp_v3/decorator_node.h" | ||
|
||
namespace nav2_behavior_tree | ||
{ | ||
|
||
class SpeedController : public BT::DecoratorNode | ||
{ | ||
public: | ||
SpeedController( | ||
const std::string & name, | ||
const BT::NodeConfiguration & conf); | ||
|
||
// Any BT node that accepts parameters must provide a requiredNodeParameters method | ||
static BT::PortsList providedPorts() | ||
{ | ||
return { | ||
BT::InputPort<double>("min_rate", 0.1, "Minimum rate"), | ||
BT::InputPort<double>("max_rate", 1.0, "Maximum rate"), | ||
BT::InputPort<double>("min_speed", 0.0, "Minimum speed"), | ||
BT::InputPort<double>("max_speed", 0.5, "Maximum speed"), | ||
BT::InputPort<double>("filter_duration", 0.3, "Duration (secs) for velocity smoothing filter") | ||
}; | ||
} | ||
|
||
private: | ||
BT::NodeStatus tick() override; | ||
|
||
// Scale the rate based speed | ||
inline double getScaledRate(const double & speed) | ||
{ | ||
return std::max( | ||
std::min( | ||
(((speed - min_speed_) / d_speed_) * d_rate_) + min_rate_, | ||
max_rate_), min_rate_); | ||
} | ||
|
||
// Update period based on current smoothed speed and reset timer | ||
inline void updatePeriod() | ||
{ | ||
auto velocity = odom_smoother_->getTwist(); | ||
double speed = std::hypot(velocity.linear.x, velocity.linear.y); | ||
double rate = getScaledRate(speed); | ||
period_ = 1.0 / rate; | ||
} | ||
|
||
rclcpp::Node::SharedPtr node_; | ||
|
||
// To keep track of time to reset | ||
rclcpp::Time start_; | ||
|
||
// To get a smoothed velocity | ||
std::shared_ptr<nav2_util::OdomSmoother> odom_smoother_; | ||
|
||
bool first_tick_; | ||
|
||
// Time period after which child node should be ticked | ||
double period_; | ||
|
||
// Rates thresholds to tick child node | ||
double min_rate_; | ||
double max_rate_; | ||
double d_rate_; | ||
|
||
// Speed thresholds | ||
double min_speed_; | ||
double max_speed_; | ||
double d_speed_; | ||
|
||
// current goal | ||
geometry_msgs::msg::PoseStamped goal_; | ||
}; | ||
|
||
} // namespace nav2_behavior_tree | ||
|
||
#endif // NAV2_BEHAVIOR_TREE__PLUGINS__SPEED_CONTROLLER_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
nav2_behavior_tree/plugins/decorator/speed_controller.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// Copyright (c) 2020 Sarthak Mittal | ||
// | ||
// 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. | ||
|
||
#include <string> | ||
#include <memory> | ||
|
||
#include "nav2_util/geometry_utils.hpp" | ||
|
||
#include "nav2_behavior_tree/plugins/speed_controller.hpp" | ||
|
||
namespace nav2_behavior_tree | ||
{ | ||
|
||
SpeedController::SpeedController( | ||
const std::string & name, | ||
const BT::NodeConfiguration & conf) | ||
: BT::DecoratorNode(name, conf), | ||
first_tick_(false), | ||
period_(1.0), | ||
min_rate_(0.1), | ||
max_rate_(1.0), | ||
min_speed_(0.0), | ||
max_speed_(0.5) | ||
{ | ||
node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node"); | ||
|
||
getInput("min_rate", min_rate_); | ||
getInput("max_rate", max_rate_); | ||
getInput("min_speed", min_speed_); | ||
getInput("max_speed", max_speed_); | ||
|
||
if (min_rate_ <= 0.0 || max_rate_ <= 0.0) { | ||
std::string err_msg = "SpeedController node cannot have rate <= 0.0"; | ||
RCLCPP_FATAL(node_->get_logger(), err_msg); | ||
throw BT::BehaviorTreeException(err_msg); | ||
} | ||
|
||
d_rate_ = max_rate_ - min_rate_; | ||
d_speed_ = max_speed_ - min_speed_; | ||
|
||
double duration = 0.3; | ||
getInput("filter_duration", duration); | ||
std::string odom_topic; | ||
node_->get_parameter_or("odom_topic", odom_topic, std::string("odom")); | ||
odom_smoother_ = std::make_shared<nav2_util::OdomSmoother>(node_, duration, odom_topic); | ||
} | ||
|
||
inline BT::NodeStatus SpeedController::tick() | ||
{ | ||
auto current_goal = config().blackboard->get<geometry_msgs::msg::PoseStamped>("goal"); | ||
|
||
if (goal_ != current_goal) { | ||
// Reset state and set period to max since we have a new goal | ||
period_ = 1.0 / max_rate_; | ||
start_ = node_->now(); | ||
first_tick_ = true; | ||
goal_ = current_goal; | ||
} | ||
|
||
setStatus(BT::NodeStatus::RUNNING); | ||
|
||
auto elapsed = node_->now() - start_; | ||
|
||
// The child gets ticked the first time through and any time the period has | ||
// expired. In addition, once the child begins to run, it is ticked each time | ||
// 'til completion | ||
if (first_tick_ || (child_node_->status() == BT::NodeStatus::RUNNING) || | ||
elapsed.seconds() >= period_) | ||
{ | ||
first_tick_ = false; | ||
|
||
// update period if the last period is exceeded | ||
if (elapsed.seconds() >= period_) { | ||
updatePeriod(); | ||
start_ = node_->now(); | ||
} | ||
|
||
const BT::NodeStatus child_state = child_node_->executeTick(); | ||
|
||
switch (child_state) { | ||
case BT::NodeStatus::RUNNING: | ||
return BT::NodeStatus::RUNNING; | ||
|
||
case BT::NodeStatus::SUCCESS: | ||
return BT::NodeStatus::SUCCESS; | ||
|
||
case BT::NodeStatus::FAILURE: | ||
default: | ||
return BT::NodeStatus::FAILURE; | ||
} | ||
} | ||
|
||
return status(); | ||
} | ||
|
||
} // namespace nav2_behavior_tree | ||
|
||
#include "behaviortree_cpp_v3/bt_factory.h" | ||
BT_REGISTER_NODES(factory) | ||
{ | ||
factory.registerNodeType<nav2_behavior_tree::SpeedController>("SpeedController"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.