Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
speed_controller.hpp
1 // Copyright (c) 2018 Intel Corporation
2 // Copyright (c) 2020 Sarthak Mittal
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef NAV2_BEHAVIOR_TREE__PLUGINS__DECORATOR__SPEED_CONTROLLER_HPP_
17 #define NAV2_BEHAVIOR_TREE__PLUGINS__DECORATOR__SPEED_CONTROLLER_HPP_
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 #include <deque>
23 
24 #include "nav_msgs/msg/odometry.hpp"
25 #include "nav2_util/odometry_utils.hpp"
26 
27 #include "behaviortree_cpp/decorator_node.h"
28 #include "behaviortree_cpp/json_export.h"
29 #include "nav2_behavior_tree/bt_utils.hpp"
30 #include "nav2_behavior_tree/json_utils.hpp"
31 
32 namespace nav2_behavior_tree
33 {
34 
47 class SpeedController : public BT::DecoratorNode
48 {
49 public:
56  const std::string & name,
57  const BT::NodeConfiguration & conf);
58 
63  static BT::PortsList providedPorts()
64  {
65  // Register JSON definitions for the types used in the ports
66  BT::RegisterJsonDefinition<geometry_msgs::msg::PoseStamped>();
67  BT::RegisterJsonDefinition<std::vector<geometry_msgs::msg::PoseStamped>>();
68 
69  return {
70  BT::InputPort<double>("min_rate", 0.1, "Minimum rate"),
71  BT::InputPort<double>("max_rate", 1.0, "Maximum rate"),
72  BT::InputPort<double>("min_speed", 0.0, "Minimum speed"),
73  BT::InputPort<double>("max_speed", 0.5, "Maximum speed"),
74  BT::InputPort<std::vector<geometry_msgs::msg::PoseStamped>>(
75  "goals", "Vector of navigation goals"),
76  BT::InputPort<geometry_msgs::msg::PoseStamped>(
77  "goal", "Navigation goal"),
78  };
79  }
80 
81 private:
86  BT::NodeStatus tick() override;
87 
92  inline double getScaledRate(const double & speed)
93  {
94  return std::max(
95  std::min(
96  (((speed - min_speed_) / d_speed_) * d_rate_) + min_rate_,
97  max_rate_), min_rate_);
98  }
99 
103  inline void updatePeriod()
104  {
105  auto velocity = odom_smoother_->getTwist();
106  double speed = std::hypot(velocity.linear.x, velocity.linear.y);
107  double rate = getScaledRate(speed);
108  period_ = 1.0 / rate;
109  }
110 
111  rclcpp::Node::SharedPtr node_;
112 
113  // To keep track of time to reset
114  rclcpp::Time start_;
115 
116  // To get a smoothed velocity
117  std::shared_ptr<nav2_util::OdomSmoother> odom_smoother_;
118 
119  bool first_tick_;
120 
121  // Time period after which child node should be ticked
122  double period_;
123 
124  // Rates thresholds to tick child node
125  double min_rate_;
126  double max_rate_;
127  double d_rate_;
128 
129  // Speed thresholds
130  double min_speed_;
131  double max_speed_;
132  double d_speed_;
133 
134  // current goal
135  geometry_msgs::msg::PoseStamped goal_;
136  std::vector<geometry_msgs::msg::PoseStamped> goals_;
137 };
138 
139 } // namespace nav2_behavior_tree
140 
141 #endif // NAV2_BEHAVIOR_TREE__PLUGINS__DECORATOR__SPEED_CONTROLLER_HPP_
A BT::DecoratorNode that ticks its child every at a rate proportional to the speed of the robot....
static BT::PortsList providedPorts()
Creates list of BT ports.
SpeedController(const std::string &name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::SpeedController.