occupancy_map_server.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Jon Binney, Ioan Sucan */
36 
37 #include <memory>
38 #include <ros/ros.h>
42 
43 static const std::string LOGNAME = "occupancy_map_server";
44 
46 {
47  octomap_msgs::Octomap map;
48 
49  map.header.frame_id = server.getMapFrame();
50  map.header.stamp = ros::Time::now();
51 
52  server.getOcTreePtr()->lockRead();
53  try
54  {
55  if (!octomap_msgs::binaryMapToMsgData(*server.getOcTreePtr(), map.data))
56  ROS_ERROR_THROTTLE_NAMED(1, LOGNAME, "Could not generate OctoMap message");
57  }
58  catch (...)
59  {
60  ROS_ERROR_THROTTLE_NAMED(1, LOGNAME, "Exception thrown while generating OctoMap message");
61  }
62  server.getOcTreePtr()->unlockRead();
63 
64  octree_binary_pub.publish(map);
65 }
66 
67 int main(int argc, char** argv)
68 {
69  ros::init(argc, argv, "occupancy_map_server");
70  ros::NodeHandle nh;
71  ros::Publisher octree_binary_pub = nh.advertise<octomap_msgs::Octomap>("octomap_binary", 1);
72  std::shared_ptr<tf2_ros::Buffer> buffer = std::make_shared<tf2_ros::Buffer>(ros::Duration(5.0));
73  std::shared_ptr<tf2_ros::TransformListener> listener = std::make_shared<tf2_ros::TransformListener>(*buffer, nh);
75  server.setUpdateCallback([&octree_binary_pub, &server] { return publishOctomap(octree_binary_pub, server); });
76  server.startMonitor();
77 
78  ros::spin();
79  return 0;
80 }
ROS_ERROR_THROTTLE_NAMED
#define ROS_ERROR_THROTTLE_NAMED(period, name,...)
ros::Publisher
octomap_msgs::binaryMapToMsgData
static bool binaryMapToMsgData(const OctomapT &octomap, std::vector< int8_t > &mapData)
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
occupancy_map_monitor::OccupancyMapMonitor
Definition: occupancy_map_monitor.h:88
ros.h
occupancy_map_monitor.h
publishOctomap
static void publishOctomap(ros::Publisher &octree_binary_pub, occupancy_map_monitor::OccupancyMapMonitor &server)
Definition: occupancy_map_server.cpp:45
occupancy_map_monitor::OccupancyMapMonitor::getMapFrame
const std::string & getMapFrame() const
Definition: occupancy_map_monitor.h:150
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
occupancy_map_monitor::OccupancyMapMonitor::startMonitor
void startMonitor()
start the monitor (will begin updating the octomap
Definition: occupancy_map_monitor.cpp:387
main
int main(int argc, char **argv)
Definition: occupancy_map_server.cpp:67
occupancy_map_monitor::OccupancyMapMonitor::setUpdateCallback
void setUpdateCallback(const boost::function< void()> &update_callback)
Set the callback to trigger when updates to the maintained octomap are received.
Definition: occupancy_map_monitor.h:176
occupancy_map_monitor::OccupancyMapMonitor::getOcTreePtr
const collision_detection::OccMapTreePtr & getOcTreePtr()
Get a pointer to the underlying octree for this monitor. Lock the tree before reading or writing usin...
Definition: occupancy_map_monitor.h:138
transform_listener.h
LOGNAME
static const std::string LOGNAME
Definition: occupancy_map_server.cpp:43
conversions.h
ros::spin
ROSCPP_DECL void spin()
ros::Duration
ros::NodeHandle
ros::Time::now
static Time now()


occupancy_map_monitor
Author(s): Ioan Sucan , Jon Binney , Suat Gedikli
autogenerated on Sat Jul 6 2024 02:27:06