@@ -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
462467void 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