37#include <rclcpp/version.h>
41#include <octomap_msgs/conversions.h>
43#if RCLCPP_VERSION_GTE(29, 6, 0)
44#include <tf2_ros/transform_listener.hpp>
47#include <tf2_ros/transform_listener.h>
49#include <rclcpp/clock.hpp>
50#include <rclcpp/executors.hpp>
51#include <rclcpp/experimental/buffers/intra_process_buffer.hpp>
52#include <rclcpp/logger.hpp>
53#include <rclcpp/logging.hpp>
54#include <rclcpp/node.hpp>
55#include <rclcpp/publisher.hpp>
56#include <rclcpp/version.h>
58#if RCLCPP_VERSION_GTE(20, 0, 0)
59#include <rclcpp/event_handler.hpp>
61#include <rclcpp/qos_event.hpp>
63#include <rclcpp/time.hpp>
64#include <rclcpp/utilities.hpp>
68static void publishOctomap(
const rclcpp::Publisher<octomap_msgs::msg::Octomap>::SharedPtr& octree_binary_pub,
71 octomap_msgs::msg::Octomap map;
75 map.header.stamp = rclcpp::Clock().now();
78 rclcpp::Clock steady_clock(RCL_STEADY_TIME);
81 if (!octomap_msgs::binaryMapToMsgData(*server.
getOcTreePtr(), map.data))
83#pragma GCC diagnostic push
84#pragma GCC diagnostic ignored "-Wold-style-cast"
85 RCLCPP_ERROR_THROTTLE(logger, steady_clock, 1000,
"Could not generate OctoMap message");
86#pragma GCC diagnostic pop
91#pragma GCC diagnostic push
92#pragma GCC diagnostic ignored "-Wold-style-cast"
93 RCLCPP_ERROR_THROTTLE(logger, steady_clock, 1000,
"Exception thrown while generating OctoMap message");
94#pragma GCC diagnostic pop
98 octree_binary_pub->publish(map);
103 rclcpp::init(argc, argv);
104 auto node = rclcpp::Node::make_shared(
"occupancy_map_server");
106 auto octree_binary_pub =
107 node->create_publisher<octomap_msgs::msg::Octomap>(
"octomap_binary", RMW_QOS_POLICY_HISTORY_KEEP_LAST);
108 auto clock_ptr = std::make_shared<rclcpp::Clock>(RCL_ROS_TIME);
109 std::shared_ptr<tf2_ros::Buffer> buffer = std::make_shared<tf2_ros::Buffer>(clock_ptr, tf2::durationFromSec(5.0));
111#if RCLCPP_VERSION_GTE(30, 0, 0)
112 tf2_ros::TransformListener listener(*buffer,
113 tf2_ros::TransformListener::RequiredInterfaces{
114 node->get_node_base_interface(), node->get_node_logging_interface(),
115 node->get_node_parameters_interface(), node->get_node_topics_interface() },
119 tf2_ros::TransformListener listener(*buffer, node,
false );
123 [&octree_binary_pub, &server, logger = node->get_logger()] { return publishOctomap(octree_binary_pub, server); });
const collision_detection::OccMapTreePtr & getOcTreePtr()
Get a pointer to the underlying octree for this monitor. Lock the tree before reading or writing usin...
void startMonitor()
start the monitor (will begin updating the octomap
const std::string & getMapFrame() const
Gets the map frame (this is set either by the constor or a parameter).
void setUpdateCallback(const std::function< void()> &update_callback)
Set the callback to trigger when updates to the maintained octomap are received.
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
void setNodeLoggerName(const std::string &name)
Call once after creating a node to initialize logging namespaces.
int main(int argc, char **argv)