Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
zone_parameter_filter.hpp
1 // Copyright (c) 2026 Komada (aki1770-del)
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 #ifndef NAV2_COSTMAP_2D__COSTMAP_FILTERS__ZONE_PARAMETER_FILTER_HPP_
16 #define NAV2_COSTMAP_2D__COSTMAP_FILTERS__ZONE_PARAMETER_FILTER_HPP_
17 
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "geometry_msgs/msg/pose.hpp"
24 #include "nav_msgs/msg/occupancy_grid.hpp"
25 #include "rclcpp/parameter.hpp"
26 #include "rclcpp/parameter_client.hpp"
27 #include "std_msgs/msg/u_int8.hpp"
28 
29 #include "nav2_costmap_2d/costmap_filters/costmap_filter.hpp"
30 #include "nav2_msgs/msg/costmap_filter_info.hpp"
31 
32 namespace nav2_costmap_2d
33 {
34 
45 {
46 public:
48 
53  void initializeFilter(const std::string & filter_info_topic) override;
54 
60  void process(
61  nav2_costmap_2d::Costmap2D & master_grid,
62  int min_i, int min_j, int max_i, int max_j,
63  const geometry_msgs::msg::Pose & pose) override;
64 
69  void resetFilter() override;
70 
74  bool isActive();
75 
76 protected:
80  void filterInfoCallback(
81  const nav2_msgs::msg::CostmapFilterInfo::ConstSharedPtr & msg);
82 
86  void maskCallback(
87  const nav_msgs::msg::OccupancyGrid::ConstSharedPtr & msg);
88 
93  void loadStateConfig();
94 
99  void applyState(uint8_t new_state);
100 
105  void resetToNominal();
106 
111  const std::string & target_node,
112  const std::vector<rclcpp::Parameter> & params);
113 
120 
121  nav2::Subscription<nav2_msgs::msg::CostmapFilterInfo>::SharedPtr filter_info_sub_;
122  nav2::Subscription<nav_msgs::msg::OccupancyGrid>::SharedPtr mask_sub_;
123  nav2::Publisher<std_msgs::msg::UInt8>::SharedPtr state_event_pub_;
124 
125  nav_msgs::msg::OccupancyGrid::ConstSharedPtr filter_mask_;
126  std::string global_frame_;
127 
128  uint8_t current_state_{0};
129  bool state_initialized_{false};
130 
131  // One per-state-override or per-nominal-default entry.
133  {
134  std::string target_node;
135  rclcpp::Parameter param;
136  };
137  std::map<uint8_t, std::vector<StateParamEntry>> state_param_map_;
138  // Keyed by target_node; values are bare-named Parameters to restore.
139  std::map<std::string, std::vector<rclcpp::Parameter>> nominal_defaults_;
140  std::map<std::string, rclcpp::AsyncParametersClient::SharedPtr> param_clients_;
141 
142  std::vector<std::shared_future<
143  std::vector<rcl_interfaces::msg::SetParametersResult>>> pending_futures_;
144 
145  std::string state_event_topic_;
146 
147  bool filter_info_received_{false};
148 };
149 
150 } // namespace nav2_costmap_2d
151 
152 #endif // NAV2_COSTMAP_2D__COSTMAP_FILTERS__ZONE_PARAMETER_FILTER_HPP_
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
: CostmapFilter basic class. It is inherited from Layer in order to avoid hidden problems when the sh...
Costmap filter that applies a configured set of ROS parameters based on the mask value at the robot's...
void loadStateConfig()
Parse the per-state parameter map and nominal_defaults from YAML overrides.
void process(nav2_costmap_2d::Costmap2D &master_grid, int min_i, int min_j, int max_i, int max_j, const geometry_msgs::msg::Pose &pose) override
Sample the mask at the robot pose; if the state changed, apply the new state's parameter set via asyn...
void applyState(uint8_t new_state)
Apply the parameter set associated with the given state. State 0 restores nominal_defaults; throws on...
void resetFilter() override
Reset filter — drop subscriptions, reset publisher, clear nominal-defaults capture.
void resetToNominal()
Restore all overridden parameters to their nominal_defaults values via async set_parameters.
void maskCallback(const nav_msgs::msg::OccupancyGrid::ConstSharedPtr &msg)
Subscriber callback for the filter mask topic.
bool isActive()
Whether the filter has received its mask and is operational.
void initializeFilter(const std::string &filter_info_topic) override
Initialise filter, subscribe to filter info / mask, build per-target-node async parameter clients.
void issueAsyncSetParameters(const std::string &target_node, const std::vector< rclcpp::Parameter > &params)
Issue an async set_parameters call to the named target node.
void filterInfoCallback(const nav2_msgs::msg::CostmapFilterInfo::ConstSharedPtr &msg)
Subscriber callback for the filter info topic.
void checkPendingParameterUpdates()
Process completed set_parameters results non-blockingly. Called at the start of every process(); a fa...