|
| 1 | +/********************************************************************* |
| 2 | + * |
| 3 | + * Software License Agreement (BSD License) |
| 4 | + * |
| 5 | + * Copyright (c) 2025, User |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions |
| 10 | + * are met: |
| 11 | + * |
| 12 | + * * Redistributions of source code must retain the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer. |
| 14 | + * * Redistributions in binary form must reproduce the above |
| 15 | + * copyright notice, this list of conditions and the following |
| 16 | + * disclaimer in the documentation and/or other materials provided |
| 17 | + * with the distribution. |
| 18 | + * * Neither the name of Willow Garage, Inc. nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived |
| 20 | + * from this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 25 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 26 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 28 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 29 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 30 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 32 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 | + * POSSIBILITY OF SUCH DAMAGE. |
| 34 | + * |
| 35 | + *********************************************************************/ |
| 36 | + |
| 37 | +#ifndef NAV2_COSTMAP_2D__PREDICTIVE_INFLATION_LAYER_HPP_ |
| 38 | +#define NAV2_COSTMAP_2D__PREDICTIVE_INFLATION_LAYER_HPP_ |
| 39 | + |
| 40 | +#include <cmath> |
| 41 | +#include <mutex> |
| 42 | +#include <vector> |
| 43 | + |
| 44 | +#include "rclcpp/rclcpp.hpp" |
| 45 | +#include "nav2_costmap_2d/inflation_layer.hpp" |
| 46 | + |
| 47 | +namespace nav2_costmap_2d |
| 48 | +{ |
| 49 | + |
| 50 | +/** |
| 51 | + * @class PredictiveInflationLayer |
| 52 | + * @brief Inflation layer that modulates costs based on robot heading and nominal speed |
| 53 | + */ |
| 54 | +class PredictiveInflationLayer : public InflationLayer |
| 55 | +{ |
| 56 | +public: |
| 57 | + /** |
| 58 | + * @brief A constructor |
| 59 | + */ |
| 60 | + PredictiveInflationLayer(); |
| 61 | + |
| 62 | + /** |
| 63 | + * @brief A destructor |
| 64 | + */ |
| 65 | + virtual ~PredictiveInflationLayer() = default; |
| 66 | + |
| 67 | + /** |
| 68 | + * @brief Initialization process of layer on startup |
| 69 | + */ |
| 70 | + void onInitialize() override; |
| 71 | + |
| 72 | + /** |
| 73 | + * @brief Update the bounds of the master costmap by this layer's update dimensions |
| 74 | + * @param robot_x X pose of robot |
| 75 | + * @param robot_y Y pose of robot |
| 76 | + * @param robot_yaw Robot orientation |
| 77 | + * @param min_x X min map coord of the window to update |
| 78 | + * @param min_y Y min map coord of the window to update |
| 79 | + * @param max_x X max map coord of the window to update |
| 80 | + * @param max_y Y max map coord of the window to update |
| 81 | + */ |
| 82 | + void updateBounds( |
| 83 | + double robot_x, double robot_y, double robot_yaw, double * min_x, |
| 84 | + double * min_y, double * max_x, double * max_y) override; |
| 85 | + |
| 86 | + /** |
| 87 | + * @brief Update the costs in the master costmap in the window |
| 88 | + * @param master_grid The master costmap grid to update |
| 89 | + * @param min_i X min map coord of the window to update |
| 90 | + * @param min_j Y min map coord of the window to update |
| 91 | + * @param max_i X max map coord of the window to update |
| 92 | + * @param max_j Y max map coord of the window to update |
| 93 | + */ |
| 94 | + void updateCosts( |
| 95 | + nav2_costmap_2d::Costmap2D & master_grid, |
| 96 | + int min_i, int min_j, int max_i, int max_j) override; |
| 97 | + |
| 98 | +protected: |
| 99 | + /** |
| 100 | + * @brief Load and validate parameters |
| 101 | + */ |
| 102 | + void loadParameters(); |
| 103 | + |
| 104 | + /** |
| 105 | + * @brief Callback executed when a parameter change is detected |
| 106 | + * @param parameters List of changed parameters |
| 107 | + */ |
| 108 | + rcl_interfaces::msg::SetParametersResult |
| 109 | + dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters); |
| 110 | + |
| 111 | + /** |
| 112 | + * @brief Compute the directional weight based on the angle to the obstacle |
| 113 | + * @param angle Absolute angle between robot heading and obstacle vector |
| 114 | + * @return Weight factor (1.0 = standard, >1.0 = inflated, <1.0 = reduced) |
| 115 | + */ |
| 116 | + double computeDirectionalWeight(double angle) const; |
| 117 | + |
| 118 | + /** |
| 119 | + * @brief Compute the speed-based weight component |
| 120 | + * @return Speed weight factor |
| 121 | + */ |
| 122 | + double computeSpeedWeight() const; |
| 123 | + |
| 124 | + /** |
| 125 | + * @brief Compute the effective distance for cost calculation |
| 126 | + * @param distance Actual Euclidean distance |
| 127 | + * @param weight Combined directional and speed weight |
| 128 | + * @return Effective distance (smaller means closer/higher cost) |
| 129 | + */ |
| 130 | + double computeEffectiveDistance(double distance, double weight) const; |
| 131 | + |
| 132 | + /** |
| 133 | + * @brief Reverse lookup from cost to distance (inverse of computeCost) |
| 134 | + * @param cost Cost value [0-255] |
| 135 | + * @return Distance in cells |
| 136 | + */ |
| 137 | + double costToDistance(unsigned char cost) const; |
| 138 | + |
| 139 | + // Parameters |
| 140 | + bool predictive_mode_; |
| 141 | + double nominal_speed_; |
| 142 | + double speed_scale_; |
| 143 | + double max_inflation_scale_; |
| 144 | + |
| 145 | + // Angle thresholds (radians) |
| 146 | + double forward_angle_; |
| 147 | + double side_angle_; |
| 148 | + |
| 149 | + // Directional weights |
| 150 | + double forward_weight_; |
| 151 | + double side_weight_; |
| 152 | + double rear_weight_; |
| 153 | + |
| 154 | + // Cached state |
| 155 | + double base_inflation_radius_; // The "true" radius configured by user |
| 156 | + double last_robot_x_; |
| 157 | + double last_robot_y_; |
| 158 | + double last_robot_yaw_; |
| 159 | +}; |
| 160 | + |
| 161 | +} // namespace nav2_costmap_2d |
| 162 | + |
| 163 | +#endif // NAV2_COSTMAP_2D__PREDICTIVE_INFLATION_LAYER_HPP_ |
0 commit comments