/home/runner/work/beluga/beluga/beluga_ros/include/beluga_ros/likelihood_field.hpp Source File

/home/runner/work/beluga/beluga/beluga_ros/include/beluga_ros/likelihood_field.hpp Source File#

Beluga ROS: /home/runner/work/beluga/beluga/beluga_ros/include/beluga_ros/likelihood_field.hpp Source File
Beluga ROS
likelihood_field.hpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
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 BELUGA_ROS_LIKELIHOOD_FIELD_HPP
16 #define BELUGA_ROS_LIKELIHOOD_FIELD_HPP
17 
18 #include <cstdint>
19 
22 #include <nav_msgs/msg/occupancy_grid.hpp>
23 
29 namespace beluga_ros {
30 
32 template <typename T>
34  const beluga::ValueGrid2<T>& likelihood_field,
35  const Sophus::SE2d& origin,
36  nav_msgs::msg::OccupancyGrid& message) {
37  // Set metadata
38  message.info.width = static_cast<unsigned int>(likelihood_field.width());
39  message.info.height = static_cast<unsigned int>(likelihood_field.height());
40  message.info.resolution = static_cast<float>(likelihood_field.resolution());
41  tf2::toMsg(origin, message.info.origin); // origin -> Pose: [x,y,z],[w,x,y,z]
42 
43  // Populate the data field with the grid data
44  const auto& grid_data = likelihood_field.data();
45  message.data.resize(likelihood_field.size());
46 
47  // Find min and max values for normalization
48  const auto [min_it, max_it] = std::minmax_element(grid_data.begin(), grid_data.end());
49  const float min_val = *min_it;
50  const float max_val = *max_it;
51  const float range = max_val - min_val;
52 
53  // Handle degenerate case (flat grid) by filling the occupancy grid with zeros
54  if (range <= std::numeric_limits<float>::epsilon()) {
55  // All values are the same; treat as unknown or flat
56  std::fill(message.data.begin(), message.data.end(), 0);
57  return;
58  }
59 
60  // Normalizing each cell to [0, 100] - To be consistent with nav2:
61  // navigation2/nav2_costmap_2d/src/costmap_2d_publisher.cpp
62  for (std::size_t i = 0; i < grid_data.size(); ++i) {
63  const float normalized = (grid_data[i] - min_val) / range;
64  message.data[i] = static_cast<int8_t>(normalized * 100.0f); // Scale to [0, 100]
65  }
66 }
67 
68 } // namespace beluga_ros
69 
70 #endif // BELUGA_ROS_LIKELIHOOD_FIELD_HPP
double resolution() const
std::size_t size() const
std::size_t height() const
std::size_t width() const
const std::vector< T > & data() const
The main Beluga ROS namespace.
Definition: amcl.hpp:46
void assign_likelihood_field(const beluga::ValueGrid2< T > &likelihood_field, const Sophus::SE2d &origin, nav_msgs::msg::OccupancyGrid &message)
Convert from likelihood data to ROS2 message.
Definition: likelihood_field.hpp:33
beluga_ros::msg::Point toMsg(const Eigen::Matrix< Scalar, 2, 1 > &in)
Converts an Eigen Vector2 type to a Point message.
Definition: tf2_eigen.hpp:46
Message conversion API overloads for Sophus types.