Nav2 Navigation Stack - lyrical  lyrical
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/rclcpp.hpp"
26 #include "std_msgs/msg/u_int8.hpp"
27 
28 #include "nav2_costmap_2d/costmap_filters/costmap_filter.hpp"
29 #include "nav2_msgs/msg/costmap_filter_info.hpp"
30 
31 namespace nav2_costmap_2d
32 {
33 
44 {
45 public:
47 
52  void initializeFilter(const std::string & filter_info_topic) override;
53 
59  void process(
60  nav2_costmap_2d::Costmap2D & master_grid,
61  int min_i, int min_j, int max_i, int max_j,
62  const geometry_msgs::msg::Pose & pose) override;
63 
68  void resetFilter() override;
69 
73  bool isActive();
74 
75 protected:
79  void filterInfoCallback(
80  const nav2_msgs::msg::CostmapFilterInfo::ConstSharedPtr & msg);
81 
85  void maskCallback(
86  const nav_msgs::msg::OccupancyGrid::ConstSharedPtr & msg);
87 
92  void loadStateConfig();
93 
98  void applyState(uint8_t new_state);
99 
104  void resetToNominal();
105 
110  const std::string & target_node,
111  const std::vector<rclcpp::Parameter> & params);
112 
119 
120  nav2::Subscription<nav2_msgs::msg::CostmapFilterInfo>::SharedPtr filter_info_sub_;
121  nav2::Subscription<nav_msgs::msg::OccupancyGrid>::SharedPtr mask_sub_;
122  nav2::Publisher<std_msgs::msg::UInt8>::SharedPtr state_event_pub_;
123 
124  nav_msgs::msg::OccupancyGrid::ConstSharedPtr filter_mask_;
125  std::string global_frame_;
126 
127  uint8_t current_state_{0};
128  bool state_initialized_{false};
129 
130  // One per-state-override or per-nominal-default entry.
132  {
133  std::string target_node;
134  rclcpp::Parameter param;
135  };
136  std::map<uint8_t, std::vector<StateParamEntry>> state_param_map_;
137  // Keyed by target_node; values are bare-named Parameters to restore.
138  std::map<std::string, std::vector<rclcpp::Parameter>> nominal_defaults_;
139  std::map<std::string, rclcpp::AsyncParametersClient::SharedPtr> param_clients_;
140 
141  std::vector<std::shared_future<
142  std::vector<rcl_interfaces::msg::SetParametersResult>>> pending_futures_;
143 
144  std::string state_event_topic_;
145 
146  bool filter_info_received_{false};
147 };
148 
149 } // namespace nav2_costmap_2d
150 
151 #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...