Nav2 Navigation Stack - lyrical  lyrical
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 "behaviortree_cpp/decorator_node.h"
25 #include "behaviortree_cpp/json_export.h"
26 #include "nav_msgs/msg/odometry.hpp"
27 #include "nav2_util/odometry_utils.hpp"
28 #include "nav2_behavior_tree/bt_utils.hpp"
29 #include "nav2_behavior_tree/json_utils.hpp"
30 
31 
32 namespace nav2_behavior_tree
33 {
34 
48 class SpeedController : public BT::DecoratorNode
49 {
50 public:
57  const std::string & name,
58  const BT::NodeConfiguration & conf);
59 
64  static BT::PortsList providedPorts()
65  {
66  // Register JSON definitions for the types used in the ports
67  BT::RegisterJsonDefinition<geometry_msgs::msg::PoseStamped>();
68  BT::RegisterJsonDefinition<nav_msgs::msg::Goals>();
69 
70  return {
71  BT::InputPort<double>("min_rate", 0.1, "Minimum rate"),
72  BT::InputPort<double>("max_rate", 1.0, "Maximum rate"),
73  BT::InputPort<double>("min_speed", 0.0, "Minimum speed"),
74  BT::InputPort<double>("max_speed", 0.5, "Maximum speed"),
75  BT::InputPort<nav_msgs::msg::Goals>(
76  "goals", "Vector of navigation goals"),
77  BT::InputPort<geometry_msgs::msg::PoseStamped>(
78  "goal", "Navigation goal"),
79  };
80  }
81 
82 private:
87  BT::NodeStatus tick() override;
88 
93  inline double getScaledRate(const double & speed)
94  {
95  return std::max(
96  std::min(
97  (((speed - min_speed_) / d_speed_) * d_rate_) + min_rate_,
98  max_rate_), min_rate_);
99  }
100 
104  inline void updatePeriod()
105  {
106  auto velocity = odom_smoother_->getTwist();
107  double speed = std::hypot(velocity.linear.x, velocity.linear.y);
108  double rate = getScaledRate(speed);
109  period_ = 1.0 / rate;
110  }
111 
112  nav2::LifecycleNode::SharedPtr node_;
113 
114  // To keep track of time to reset
115  rclcpp::Time start_;
116 
117  // To get a smoothed velocity
118  std::shared_ptr<nav2_util::OdomSmoother> odom_smoother_;
119 
120  bool first_tick_;
121 
122  // Time period after which child node should be ticked
123  double period_;
124 
125  // Rates thresholds to tick child node
126  double min_rate_;
127  double max_rate_;
128  double d_rate_;
129 
130  // Speed thresholds
131  double min_speed_;
132  double max_speed_;
133  double d_speed_;
134 
135  // current goal
136  geometry_msgs::msg::PoseStamped goal_;
137  nav_msgs::msg::Goals goals_;
138 };
139 
140 } // namespace nav2_behavior_tree
141 
142 #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.