Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
adjust_speed_limit.cpp
1 // Copyright (c) 2025, Open Navigation LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #include <memory>
17 #include <string>
18 
19 #include "nav2_route/plugins/route_operations/adjust_speed_limit.hpp"
20 
21 namespace nav2_route
22 {
23 
25  const rclcpp_lifecycle::LifecycleNode::SharedPtr node,
26  std::shared_ptr<nav2_costmap_2d::CostmapSubscriber>/* costmap_subscriber */,
27  const std::string & name)
28 {
29  RCLCPP_INFO(node->get_logger(), "Configuring Adjust speed limit operation.");
30  name_ = name;
31  logger_ = node->get_logger();
32  nav2_util::declare_parameter_if_not_declared(
33  node, getName() + ".speed_tag", rclcpp::ParameterValue("speed_limit"));
34  speed_tag_ = node->get_parameter(getName() + ".speed_tag").as_string();
35 
36 
37  nav2_util::declare_parameter_if_not_declared(
38  node, getName() + ".speed_limit_topic", rclcpp::ParameterValue("speed_limit"));
39  std::string topic = node->get_parameter(getName() + ".speed_tag").as_string();
40 
41  speed_limit_pub_ = node->create_publisher<nav2_msgs::msg::SpeedLimit>(topic, 1);
42  speed_limit_pub_->on_activate();
43 }
44 
46  NodePtr /*node_achieved*/,
47  EdgePtr edge_entered,
48  EdgePtr /*edge_exited*/,
49  const Route & /*route*/,
50  const geometry_msgs::msg::PoseStamped & /*curr_pose*/,
51  const Metadata * /*mdata*/)
52 {
53  OperationResult result;
54  if (!edge_entered) {
55  return result;
56  }
57 
58  float speed_limit = 100.0;
59  speed_limit = edge_entered->metadata.getValue<float>(speed_tag_, speed_limit);
60  RCLCPP_DEBUG(logger_, "Setting speed limit to %.2f%% of maximum.", speed_limit);
61 
62  auto msg = std::make_unique<nav2_msgs::msg::SpeedLimit>();
63  msg->percentage = true;
64  msg->speed_limit = speed_limit;
65  speed_limit_pub_->publish(std::move(msg));
66  return result;
67 }
68 
69 } // namespace nav2_route
70 
71 #include "pluginlib/class_list_macros.hpp"
A route operation to process on state changes to evaluate if speed limits should be adjusted based on...
std::string getName() override
Get name of the plugin for parameter scope mapping.
void configure(const rclcpp_lifecycle::LifecycleNode::SharedPtr node, std::shared_ptr< nav2_costmap_2d::CostmapSubscriber > costmap_subscriber, const std::string &name) override
Configure.
OperationResult perform(NodePtr node_achieved, EdgePtr edge_entered, EdgePtr edge_exited, const Route &route, const geometry_msgs::msg::PoseStamped &curr_pose, const Metadata *mdata=nullptr) override
The main speed limit operation to adjust the maximum speed of the vehicle.
A plugin interface to perform an operation while tracking the route such as triggered from the graph ...
An object representing edges between nodes.
Definition: types.hpp:134
An object to store arbitrary metadata regarding nodes from the graph file.
Definition: types.hpp:35
An object to store the nodes in the graph file.
Definition: types.hpp:183
a struct to hold return from an operation
An ordered set of nodes and edges corresponding to the planned route.
Definition: types.hpp:211