Skip to content

Commit 2af8e11

Browse files
committed
WIP support clear_map
1 parent 39f0a36 commit 2af8e11

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

nvblox_ros/include/nvblox_ros/nvblox_node.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <sensor_msgs/msg/image.hpp>
4747
#include <sensor_msgs/msg/point_cloud2.hpp>
4848
#include <std_msgs/msg/string.hpp>
49+
#include <std_srvs/srv/empty.hpp>
4950
#include <visualization_msgs/msg/marker.hpp>
5051

5152
#include <nvblox_msgs/srv/file_path.hpp>
@@ -173,6 +174,9 @@ class NvbloxNode : public rclcpp::Node
173174
void getEsdfAndGradientService(
174175
const std::shared_ptr<nvblox_msgs::srv::EsdfAndGradients::Request> request,
175176
std::shared_ptr<nvblox_msgs::srv::EsdfAndGradients::Response> response);
177+
void clearMapService(
178+
const std::shared_ptr<std_srvs::srv::Empty::Request> request,
179+
std::shared_ptr<std_srvs::srv::Empty::Response> response);
176180

177181
// Main tick function that process all input data queues in order
178182
virtual void tick();
@@ -449,6 +453,7 @@ class NvbloxNode : public rclcpp::Node
449453
rclcpp::Service<nvblox_msgs::srv::FilePath>::SharedPtr save_rates_service_;
450454
rclcpp::Service<nvblox_msgs::srv::FilePath>::SharedPtr save_timings_service_;
451455
rclcpp::Service<nvblox_msgs::srv::EsdfAndGradients>::SharedPtr send_esdf_and_gradient_service_;
456+
rclcpp::Service<std_srvs::srv::Empty>::SharedPtr clear_map_service_;
452457

453458
// Callback groups.
454459
rclcpp::CallbackGroup::SharedPtr group_processing_;
@@ -529,11 +534,14 @@ class NvbloxNode : public rclcpp::Node
529534
nvblox_msgs::srv::EsdfAndGradients>>;
530535
using FilePathServiceQueuedType = std::shared_ptr<ServiceRequestTask<NvbloxNode,
531536
nvblox_msgs::srv::FilePath>>;
537+
using ClearMapServiceQueuedType = std::shared_ptr<ServiceRequestTask<NvbloxNode,
538+
std_srvs::srv::Empty>>;
532539

533540
// Input queues. Unique pointers are used to enable more flexibility when deallocating.
534541
std::unique_ptr<std::list<sensor_msgs::msg::PointCloud2::ConstSharedPtr>> pointcloud_queue_;
535542
std::unique_ptr<std::list<EsdfServiceQueuedType>> esdf_service_queue_;
536543
std::unique_ptr<std::list<FilePathServiceQueuedType>> file_path_service_queue_;
544+
std::unique_ptr<std::list<ClearMapServiceQueuedType>> clear_map_service_queue_;
537545
std::unique_ptr<std::list<ImageTypeVariant>> depth_image_queue_;
538546
std::unique_ptr<std::list<ImageTypeVariant>>
539547
color_image_queue_;
@@ -544,6 +552,7 @@ class NvbloxNode : public rclcpp::Node
544552
static constexpr char kPointcloudQueueName[] = "pointcloud_queue";
545553
static constexpr char kFilePathServiceQueueName[] = "file_path_service_queue";
546554
static constexpr char kEsdfServiceQueueName[] = "esdf_service_queue";
555+
static constexpr char kClearMapServiceQueueName[] = "clear_map_service_queue";
547556

548557
// Input queue mutexes.
549558
std::mutex depth_queue_mutex_;
@@ -552,6 +561,7 @@ class NvbloxNode : public rclcpp::Node
552561
std::mutex color_mask_queue_mutex_;
553562
std::mutex pointcloud_queue_mutex_;
554563
std::mutex esdf_service_queue_mutex_;
564+
std::mutex clear_map_service_queue_mutex_;
555565
std::mutex file_path_service_queue_mutex_;
556566

557567
// Counts the number of messages dropped from the input queues.

nvblox_ros/src/lib/nvblox_node.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ void NvbloxNode::subscribeToTopics()
223223
pointcloud_queue_ = std::make_unique<std::list<sensor_msgs::msg::PointCloud2::ConstSharedPtr>>();
224224
esdf_service_queue_ = std::make_unique<std::list<EsdfServiceQueuedType>>();
225225
file_path_service_queue_ = std::make_unique<std::list<FilePathServiceQueuedType>>();
226+
clear_map_service_queue_ = std::make_unique<std::list<ClearMapServiceQueuedType>>();
226227

227228
constexpr int kQueueSize = 10;
228229

@@ -457,6 +458,10 @@ void NvbloxNode::advertiseServices()
457458
&NvbloxNode::getEsdfAndGradientService, this, std::placeholders::_1,
458459
std::placeholders::_2),
459460
rmw_qos_profile_services_default);
461+
clear_map_service_ = create_service<std_srvs::srv::Empty>(
462+
"~/clear_map",
463+
std::bind(&NvbloxNode::clearMapService, this, std::placeholders::_1, std::placeholders::_2),
464+
rmw_qos_profile_services_default);
460465
}
461466

462467
void NvbloxNode::setupTimers()
@@ -791,6 +796,11 @@ void NvbloxNode::processServiceRequestTaskQueue()
791796
&file_path_service_queue_mutex_,
792797
service_ready,
793798
task);
799+
processQueue<ClearMapServiceQueuedType>(
800+
clear_map_service_queue_,
801+
&clear_map_service_queue_mutex_,
802+
service_ready,
803+
task);
794804
// If a service requested visualization, publish right now.
795805
if (publish_layers_requested_) {
796806
publishLayers();
@@ -1828,6 +1838,54 @@ void NvbloxNode::getEsdfAndGradientService(
18281838
pushOntoQueue(kEsdfServiceQueueName, task, esdf_service_queue_, &esdf_service_queue_mutex_);
18291839
task->waitForTaskCompletion();
18301840
}
1841+
void NvbloxNode::clearMapService(
1842+
const std::shared_ptr<std_srvs::srv::Empty::Request> request,
1843+
std::shared_ptr<std_srvs::srv::Empty::Response> response)
1844+
{
1845+
// Define the task function
1846+
TaskFunctionType<NvbloxNode, std_srvs::srv::Empty> request_task =
1847+
[](auto node,
1848+
auto service_request,
1849+
auto service_response) {
1850+
RCLCPP_INFO(node->get_logger(), "Clearing the entire map");
1851+
1852+
// Get the current position
1853+
Transform T_L_MC;
1854+
if (node->transformer_.lookupTransformToGlobalFrame(
1855+
node->params_.map_clearing_frame_id, rclcpp::Time(0),
1856+
&T_L_MC))
1857+
{
1858+
// Set radius to 0 to clear everything
1859+
float original_radius = node->params_.map_clearing_radius_m;
1860+
node->params_.map_clearing_radius_m = 0.0f;
1861+
1862+
// Call the existing function to clear everything
1863+
node->clearMapOutsideOfRadiusOfLastKnownPose();
1864+
1865+
// Restore the original radius
1866+
node->params_.map_clearing_radius_m = original_radius;
1867+
1868+
// Request visualization update
1869+
node->publish_layers_requested_ = true;
1870+
1871+
RCLCPP_INFO(node->get_logger(), "Map cleared successfully");
1872+
return true;
1873+
} else {
1874+
RCLCPP_ERROR(
1875+
node->get_logger(), "Failed to lookup transform for frame: %s",
1876+
node->params_.map_clearing_frame_id.get().c_str());
1877+
return false;
1878+
}
1879+
};
1880+
1881+
// Create the ServiceRequestTask
1882+
auto task =
1883+
std::make_shared<ServiceRequestTask<NvbloxNode, std_srvs::srv::Empty>>(
1884+
request_task, this, request, response);
1885+
// Push the task onto the queue and wait for completion.
1886+
pushOntoQueue(kClearMapServiceQueueName, task, clear_map_service_queue_, &clear_map_service_queue_mutex_);
1887+
task->waitForTaskCompletion();
1888+
}
18311889

18321890
} // namespace nvblox
18331891

0 commit comments

Comments
 (0)