Skip to content

Commit 4823597

Browse files
authored
fix(probabilistic_occupancy_grid_map): build and runtime on non-cuda environments (#10061)
* feat: improved data handling in the ogm. When using the full concatenated pointcloud the processing time decreases from 8ms to 4ms Signed-off-by: Kenzo Lobos-Tsunekawa <kenzo.lobos@tier4.jp> * feat: added header blocks for non-cuda environments Signed-off-by: Kenzo Lobos-Tsunekawa <kenzo.lobos@tier4.jp> * chore: removed the cuda includes from the global includes Signed-off-by: Kenzo Lobos-Tsunekawa <kenzo.lobos@tier4.jp> * fix: missed one header include Signed-off-by: Kenzo Lobos-Tsunekawa <kenzo.lobos@tier4.jp> * chore: added more cuda related macros Signed-off-by: Kenzo Lobos-Tsunekawa <kenzo.lobos@tier4.jp> * chore: lint errors Signed-off-by: Kenzo Lobos-Tsunekawa <kenzo.lobos@tier4.jp> * feat: fixed errors during merge Signed-off-by: Kenzo Lobos-Tsunekawa <kenzo.lobos@tier4.jp> --------- Signed-off-by: Kenzo Lobos-Tsunekawa <kenzo.lobos@tier4.jp>
1 parent c08e6b3 commit 4823597

File tree

9 files changed

+164
-70
lines changed

9 files changed

+164
-70
lines changed

perception/autoware_probabilistic_occupancy_grid_map/CMakeLists.txt

+88-67
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,98 @@ find_package(Eigen3 REQUIRED)
1010
find_package(PCL REQUIRED)
1111

1212
if(NOT ${CUDA_FOUND})
13-
message(WARNING "cuda was not found, so the autoware_probabilistic_occupancy_grid_map package will not be built.")
14-
return()
13+
message(WARNING "cuda was not found, so only autoware::occupancy_grid_map::LaserscanBasedOccupancyGridMapNode will be built.")
14+
else()
15+
add_definitions(-DUSE_CUDA)
1516
endif()
1617

1718
include_directories(
1819
include
1920
SYSTEM
20-
${CUDA_INCLUDE_DIRS}
2121
${EIGEN3_INCLUDE_DIR}
2222
${grid_map_ros_INCLUDE_DIRS}
2323
)
2424

25-
# cSpell: ignore expt
26-
list(APPEND CUDA_NVCC_FLAGS --expt-relaxed-constexpr -diag-suppress 20012 --compiler-options -fPIC)
27-
set(CUDA_SEPARABLE_COMPILATION ON)
28-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
29-
30-
cuda_add_library(${PROJECT_NAME}_cuda SHARED
31-
lib/costmap_2d/occupancy_grid_map_fixed_kernel.cu
32-
lib/costmap_2d/occupancy_grid_map_projective_kernel.cu
33-
lib/updater/binary_bayes_filter_updater_kernel.cu
34-
lib/updater/log_odds_bayes_filter_updater_kernel.cu
35-
lib/utils/utils_kernel.cu
36-
)
37-
38-
target_link_libraries(${PROJECT_NAME}_cuda
39-
${CUDA_LIBRARIES}
40-
)
41-
42-
ament_auto_add_library(${PROJECT_NAME}_common SHARED
43-
lib/costmap_2d/occupancy_grid_map_base.cpp
44-
lib/updater/binary_bayes_filter_updater.cpp
45-
lib/utils/utils.cpp
46-
)
47-
target_link_libraries(${PROJECT_NAME}_common
48-
${PCL_LIBRARIES}
49-
${PROJECT_NAME}_cuda
50-
)
51-
52-
# PointcloudBasedOccupancyGridMap
53-
ament_auto_add_library(pointcloud_based_occupancy_grid_map SHARED
54-
src/pointcloud_based_occupancy_grid_map/pointcloud_based_occupancy_grid_map_node.cpp
55-
lib/costmap_2d/occupancy_grid_map_fixed.cpp
56-
lib/costmap_2d/occupancy_grid_map_projective.cpp
57-
)
58-
59-
target_link_libraries(pointcloud_based_occupancy_grid_map
60-
${PCL_LIBRARIES}
61-
${PROJECT_NAME}_common
62-
${PROJECT_NAME}_cuda
63-
)
25+
if(${CUDA_FOUND})
26+
include_directories(
27+
SYSTEM
28+
${CUDA_INCLUDE_DIRS}
29+
)
30+
31+
# cSpell: ignore expt
32+
list(APPEND CUDA_NVCC_FLAGS --expt-relaxed-constexpr -diag-suppress 20012 --compiler-options -fPIC)
33+
set(CUDA_SEPARABLE_COMPILATION ON)
34+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
35+
36+
cuda_add_library(${PROJECT_NAME}_cuda SHARED
37+
lib/costmap_2d/occupancy_grid_map_fixed_kernel.cu
38+
lib/costmap_2d/occupancy_grid_map_projective_kernel.cu
39+
lib/updater/binary_bayes_filter_updater_kernel.cu
40+
lib/updater/log_odds_bayes_filter_updater_kernel.cu
41+
lib/utils/utils_kernel.cu
42+
)
43+
44+
target_link_libraries(${PROJECT_NAME}_cuda
45+
${CUDA_LIBRARIES}
46+
)
47+
48+
ament_auto_add_library(${PROJECT_NAME}_common SHARED
49+
lib/costmap_2d/occupancy_grid_map_base.cpp
50+
lib/updater/binary_bayes_filter_updater.cpp
51+
lib/utils/utils.cpp
52+
)
53+
target_link_libraries(${PROJECT_NAME}_common
54+
${PCL_LIBRARIES}
55+
${PROJECT_NAME}_cuda
56+
)
57+
58+
# PointcloudBasedOccupancyGridMap
59+
ament_auto_add_library(pointcloud_based_occupancy_grid_map SHARED
60+
src/pointcloud_based_occupancy_grid_map/pointcloud_based_occupancy_grid_map_node.cpp
61+
lib/costmap_2d/occupancy_grid_map_fixed.cpp
62+
lib/costmap_2d/occupancy_grid_map_projective.cpp
63+
)
64+
65+
target_link_libraries(pointcloud_based_occupancy_grid_map
66+
${PCL_LIBRARIES}
67+
${PROJECT_NAME}_common
68+
${PROJECT_NAME}_cuda
69+
)
70+
71+
rclcpp_components_register_node(pointcloud_based_occupancy_grid_map
72+
PLUGIN "autoware::occupancy_grid_map::PointcloudBasedOccupancyGridMapNode"
73+
EXECUTABLE pointcloud_based_occupancy_grid_map_node
74+
)
75+
76+
# GridMapFusionNode
77+
ament_auto_add_library(synchronized_grid_map_fusion SHARED
78+
src/fusion/synchronized_grid_map_fusion_node.cpp
79+
lib/fusion_policy/fusion_policy.cpp
80+
lib/costmap_2d/occupancy_grid_map_fixed.cpp
81+
lib/updater/log_odds_bayes_filter_updater.cpp
82+
lib/utils/utils.cpp
83+
)
84+
85+
target_link_libraries(synchronized_grid_map_fusion
86+
${PCL_LIBRARIES}
87+
)
88+
89+
rclcpp_components_register_node(synchronized_grid_map_fusion
90+
PLUGIN "autoware::occupancy_grid_map::GridMapFusionNode"
91+
EXECUTABLE synchronized_grid_map_fusion_node
92+
)
93+
else()
94+
ament_auto_add_library(${PROJECT_NAME}_common SHARED
95+
lib/costmap_2d/occupancy_grid_map_base.cpp
96+
lib/updater/binary_bayes_filter_updater.cpp
97+
lib/utils/utils.cpp
98+
)
99+
100+
target_link_libraries(${PROJECT_NAME}_common
101+
${PCL_LIBRARIES}
102+
)
103+
endif()
64104

65-
rclcpp_components_register_node(pointcloud_based_occupancy_grid_map
66-
PLUGIN "autoware::occupancy_grid_map::PointcloudBasedOccupancyGridMapNode"
67-
EXECUTABLE pointcloud_based_occupancy_grid_map_node
68-
)
69105

70106
# LaserscanBasedOccupancyGridMap
71107
ament_auto_add_library(laserscan_based_occupancy_grid_map SHARED
@@ -83,28 +119,13 @@ rclcpp_components_register_node(laserscan_based_occupancy_grid_map
83119
EXECUTABLE laserscan_based_occupancy_grid_map_node
84120
)
85121

86-
# GridMapFusionNode
87-
ament_auto_add_library(synchronized_grid_map_fusion SHARED
88-
src/fusion/synchronized_grid_map_fusion_node.cpp
89-
lib/fusion_policy/fusion_policy.cpp
90-
lib/costmap_2d/occupancy_grid_map_fixed.cpp
91-
lib/updater/log_odds_bayes_filter_updater.cpp
92-
lib/utils/utils.cpp
93-
)
94122

95-
target_link_libraries(synchronized_grid_map_fusion
96-
${PCL_LIBRARIES}
97-
)
98-
99-
rclcpp_components_register_node(synchronized_grid_map_fusion
100-
PLUGIN "autoware::occupancy_grid_map::GridMapFusionNode"
101-
EXECUTABLE synchronized_grid_map_fusion_node
102-
)
103-
104-
install(
105-
TARGETS ${PROJECT_NAME}_cuda
106-
DESTINATION lib
107-
)
123+
if(${CUDA_FOUND})
124+
install(
125+
TARGETS ${PROJECT_NAME}_cuda
126+
DESTINATION lib
127+
)
128+
endif()
108129

109130
ament_auto_package(
110131
INSTALL_TO_SHARE

perception/autoware_probabilistic_occupancy_grid_map/include/autoware/probabilistic_occupancy_grid_map/costmap_2d/occupancy_grid_map_base.hpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@
5252
#ifndef AUTOWARE__PROBABILISTIC_OCCUPANCY_GRID_MAP__COSTMAP_2D__OCCUPANCY_GRID_MAP_BASE_HPP_
5353
#define AUTOWARE__PROBABILISTIC_OCCUPANCY_GRID_MAP__COSTMAP_2D__OCCUPANCY_GRID_MAP_BASE_HPP_
5454

55+
#ifdef USE_CUDA
5556
#include "autoware/probabilistic_occupancy_grid_map/utils/cuda_pointcloud.hpp"
5657

5758
#include <autoware/cuda_utils/cuda_unique_ptr.hpp>
59+
#endif
60+
5861
#include <autoware/universe_utils/math/unit_conversion.hpp>
5962
#include <nav2_costmap_2d/costmap_2d.hpp>
6063
#include <rclcpp/rclcpp.hpp>
@@ -78,10 +81,12 @@ class OccupancyGridMapInterface : public nav2_costmap_2d::Costmap2D
7881
const bool use_cuda, const unsigned int cells_size_x, const unsigned int cells_size_y,
7982
const float resolution);
8083

84+
#ifdef USE_CUDA
8185
virtual void updateWithPointCloud(
8286
[[maybe_unused]] const CudaPointCloud2 & raw_pointcloud,
8387
[[maybe_unused]] const CudaPointCloud2 & obstacle_pointcloud,
8488
[[maybe_unused]] const Pose & robot_pose, [[maybe_unused]] const Pose & scan_origin) {};
89+
#endif
8590

8691
void updateOrigin(double new_origin_x, double new_origin_y) override;
8792

@@ -102,28 +107,32 @@ class OccupancyGridMapInterface : public nav2_costmap_2d::Costmap2D
102107

103108
bool isCudaEnabled() const;
104109

110+
#ifdef USE_CUDA
105111
void setCudaStream(const cudaStream_t & stream);
106112

107113
const autoware::cuda_utils::CudaUniquePtr<std::uint8_t[]> & getDeviceCostmap() const;
108114

109115
void copyDeviceCostmapToHost() const;
116+
#endif
110117

111118
protected:
112119
rclcpp::Logger logger_{rclcpp::get_logger("pointcloud_based_occupancy_grid_map")};
113120
rclcpp::Clock clock_{RCL_ROS_TIME};
114121

115122
double resolution_inv_;
123+
bool use_cuda_;
116124

125+
#ifdef USE_CUDA
117126
cudaStream_t stream_;
118127

119-
bool use_cuda_;
120128
autoware::cuda_utils::CudaUniquePtr<std::uint8_t[]> device_costmap_;
121129
autoware::cuda_utils::CudaUniquePtr<std::uint8_t[]> device_costmap_aux_;
122130

123131
autoware::cuda_utils::CudaUniquePtr<Eigen::Matrix3f> device_rotation_map_;
124132
autoware::cuda_utils::CudaUniquePtr<Eigen::Vector3f> device_translation_map_;
125133
autoware::cuda_utils::CudaUniquePtr<Eigen::Matrix3f> device_rotation_scan_;
126134
autoware::cuda_utils::CudaUniquePtr<Eigen::Vector3f> device_translation_scan_;
135+
#endif
127136
};
128137

129138
} // namespace costmap_2d

perception/autoware_probabilistic_occupancy_grid_map/include/autoware/probabilistic_occupancy_grid_map/updater/binary_bayes_filter_updater.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
#ifndef AUTOWARE__PROBABILISTIC_OCCUPANCY_GRID_MAP__UPDATER__BINARY_BAYES_FILTER_UPDATER_HPP_
1616
#define AUTOWARE__PROBABILISTIC_OCCUPANCY_GRID_MAP__UPDATER__BINARY_BAYES_FILTER_UPDATER_HPP_
1717

18+
#ifdef USE_CUDA
1819
#include "autoware/cuda_utils/cuda_unique_ptr.hpp"
20+
#endif
21+
1922
#include "autoware/probabilistic_occupancy_grid_map/updater/ogm_updater_interface.hpp"
2023

2124
#include <Eigen/Core>
@@ -40,7 +43,9 @@ class OccupancyGridMapBBFUpdater : public OccupancyGridMapUpdaterInterface
4043
Eigen::Matrix2f probability_matrix_;
4144
double v_ratio_;
4245

46+
#ifdef USE_CUDA
4347
autoware::cuda_utils::CudaUniquePtr<float[]> device_probability_matrix_;
48+
#endif
4449
};
4550

4651
} // namespace costmap_2d

perception/autoware_probabilistic_occupancy_grid_map/include/autoware/probabilistic_occupancy_grid_map/updater/ogm_updater_interface.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
#include "autoware/probabilistic_occupancy_grid_map/cost_value/cost_value.hpp"
1919
#include "autoware/probabilistic_occupancy_grid_map/costmap_2d/occupancy_grid_map_base.hpp"
20+
21+
#ifdef USE_CUDA
2022
#include "autoware/probabilistic_occupancy_grid_map/utils/utils_kernel.hpp"
23+
#endif
2124

2225
#include <rclcpp/node.hpp>
2326

perception/autoware_probabilistic_occupancy_grid_map/include/autoware/probabilistic_occupancy_grid_map/utils/utils.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
#define AUTOWARE__PROBABILISTIC_OCCUPANCY_GRID_MAP__UTILS__UTILS_HPP_
1717

1818
#include "autoware/probabilistic_occupancy_grid_map/cost_value/cost_value.hpp"
19+
20+
#ifdef USE_CUDA
1921
#include "autoware/probabilistic_occupancy_grid_map/utils/cuda_pointcloud.hpp"
2022
#include "autoware/probabilistic_occupancy_grid_map/utils/utils_kernel.hpp"
23+
#endif
2124

2225
#include <builtin_interfaces/msg/time.hpp>
2326
#include <pcl_ros/transforms.hpp>
@@ -56,8 +59,10 @@ bool transformPointcloud(
5659
const sensor_msgs::msg::PointCloud2 & input, const tf2_ros::Buffer & tf2,
5760
const std::string & target_frame, sensor_msgs::msg::PointCloud2 & output);
5861

62+
#ifdef USE_CUDA
5963
bool transformPointcloudAsync(
6064
CudaPointCloud2 & input, const tf2_ros::Buffer & tf2, const std::string & target_frame);
65+
#endif
6166

6267
Eigen::Matrix4f getTransformMatrix(const geometry_msgs::msg::Pose & pose);
6368

perception/autoware_probabilistic_occupancy_grid_map/lib/costmap_2d/occupancy_grid_map_base.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ OccupancyGridMapInterface::OccupancyGridMapInterface(
8282
: Costmap2D(cells_size_x, cells_size_y, resolution, 0.f, 0.f, cost_value::NO_INFORMATION),
8383
use_cuda_(use_cuda)
8484
{
85+
#ifdef USE_CUDA
8586
if (use_cuda_) {
8687
min_height_ = -std::numeric_limits<double>::infinity();
8788
max_height_ = std::numeric_limits<double>::infinity();
@@ -99,12 +100,11 @@ OccupancyGridMapInterface::OccupancyGridMapInterface(
99100
device_rotation_scan_ = autoware::cuda_utils::make_unique<Eigen::Matrix3f>();
100101
device_translation_scan_ = autoware::cuda_utils::make_unique<Eigen::Vector3f>();
101102
}
103+
#endif
102104
}
103105

104106
void OccupancyGridMapInterface::updateOrigin(double new_origin_x, double new_origin_y)
105107
{
106-
using autoware::occupancy_grid_map::utils::copyMapRegionLaunch;
107-
108108
// project the new origin into the grid
109109
int cell_ox{static_cast<int>(std::floor((new_origin_x - origin_x_) / resolution_))};
110110
int cell_oy{static_cast<int>(std::floor((new_origin_y - origin_y_) / resolution_))};
@@ -130,6 +130,8 @@ void OccupancyGridMapInterface::updateOrigin(double new_origin_x, double new_ori
130130
// we need a map to store the obstacles in the window temporarily
131131
unsigned char * local_map{nullptr};
132132

133+
#ifdef USE_CUDA
134+
using autoware::occupancy_grid_map::utils::copyMapRegionLaunch;
133135
if (use_cuda_) {
134136
copyMapRegionLaunch(
135137
device_costmap_.get(), lower_left_x, lower_left_y, size_x_, size_y_,
@@ -138,6 +140,13 @@ void OccupancyGridMapInterface::updateOrigin(double new_origin_x, double new_ori
138140
cudaMemsetAsync(
139141
device_costmap_.get(), cost_value::NO_INFORMATION, size_x_ * size_y_ * sizeof(std::uint8_t),
140142
stream_);
143+
#else
144+
if (use_cuda_) {
145+
RCLCPP_ERROR(
146+
rclcpp::get_logger("pointcloud_based_occupancy_grid_map"),
147+
"The code was compiled without cuda.");
148+
return;
149+
#endif
141150
} else {
142151
local_map = new unsigned char[cell_size_x * cell_size_y];
143152

@@ -159,6 +168,7 @@ void OccupancyGridMapInterface::updateOrigin(double new_origin_x, double new_ori
159168
int start_y{lower_left_y - cell_oy};
160169

161170
// now we want to copy the overlapping information back into the map, but in its new location
171+
#ifdef USE_CUDA
162172
if (use_cuda_) {
163173
if (
164174
start_x < 0 || start_y < 0 || start_x + cell_size_x > size_x_ ||
@@ -174,6 +184,13 @@ void OccupancyGridMapInterface::updateOrigin(double new_origin_x, double new_ori
174184
copyMapRegionLaunch(
175185
device_costmap_aux_.get(), 0, 0, cell_size_x, cell_size_y, device_costmap_.get(), start_x,
176186
start_y, size_x_, size_y_, cell_size_x, cell_size_y, stream_);
187+
#else
188+
if (use_cuda_) {
189+
RCLCPP_ERROR(
190+
rclcpp::get_logger("pointcloud_based_occupancy_grid_map"),
191+
"The code was compiled without cuda.");
192+
return;
193+
#endif
177194
} else {
178195
copyMapRegion(
179196
local_map, 0, 0, cell_size_x, costmap_, start_x, start_y, size_x_, cell_size_x, cell_size_y);
@@ -187,10 +204,18 @@ void OccupancyGridMapInterface::updateOrigin(double new_origin_x, double new_ori
187204

188205
void OccupancyGridMapInterface::resetMaps()
189206
{
207+
#ifdef USE_CUDA
190208
if (use_cuda_) {
191209
cudaMemsetAsync(
192210
device_costmap_.get(), cost_value::NO_INFORMATION, getSizeInCellsX() * getSizeInCellsY(),
193211
stream_);
212+
#else
213+
if (use_cuda_) {
214+
RCLCPP_ERROR(
215+
rclcpp::get_logger("pointcloud_based_occupancy_grid_map"),
216+
"The code was compiled without cuda.");
217+
return;
218+
#endif
194219
} else {
195220
nav2_costmap_2d::Costmap2D::resetMaps();
196221
}
@@ -207,6 +232,7 @@ bool OccupancyGridMapInterface::isCudaEnabled() const
207232
return use_cuda_;
208233
}
209234

235+
#ifdef USE_CUDA
210236
void OccupancyGridMapInterface::setCudaStream(const cudaStream_t & stream)
211237
{
212238
if (isCudaEnabled()) {
@@ -227,6 +253,7 @@ void OccupancyGridMapInterface::copyDeviceCostmapToHost() const
227253
cudaMemcpyDeviceToHost, stream_);
228254
cudaStreamSynchronize(stream_);
229255
}
256+
#endif
230257

231258
} // namespace costmap_2d
232259
} // namespace autoware::occupancy_grid_map

0 commit comments

Comments
 (0)