Nav2 Navigation Stack - lyrical  lyrical
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 nav2::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  speed_tag_ = node->declare_or_get_parameter(
33  getName() + ".speed_tag", std::string("speed_limit"));
34 
35  std::string topic = node->declare_or_get_parameter(
36  getName() + ".speed_limit_topic", std::string("speed_limit"));
37 
38  speed_limit_pub_ = node->create_publisher<nav2_msgs::msg::SpeedLimit>(topic);
39  speed_limit_pub_->on_activate();
40 }
41 
43  NodePtr /*node_achieved*/,
44  EdgePtr edge_entered,
45  EdgePtr /*edge_exited*/,
46  const Route & /*route*/,
47  const geometry_msgs::msg::PoseStamped & /*curr_pose*/,
48  const Metadata * /*mdata*/)
49 {
50  OperationResult result;
51  if (!edge_entered) {
52  return result;
53  }
54 
55  float speed_limit = 100.0;
56  speed_limit = edge_entered->metadata.getValue<float>(speed_tag_, speed_limit);
57  RCLCPP_DEBUG(logger_, "Setting speed limit to %.2f%% of maximum.", speed_limit);
58 
59  auto msg = std::make_unique<nav2_msgs::msg::SpeedLimit>();
60  msg->percentage = true;
61  msg->speed_limit = speed_limit;
62  speed_limit_pub_->publish(std::move(msg));
63  return result;
64 }
65 
66 } // namespace nav2_route
67 
68 #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 nav2::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