Go to the documentation of this file.00001
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef OCTOMAP_SERVER_OCTOMAPSERVER_H
00039 #define OCTOMAP_SERVER_OCTOMAPSERVER_H
00040
00041 #include <ros/ros.h>
00042 #include <visualization_msgs/MarkerArray.h>
00043 #include <nav_msgs/OccupancyGrid.h>
00044 #include <std_msgs/ColorRGBA.h>
00045
00046 #include <arm_navigation_msgs/CollisionObject.h>
00047 #include <arm_navigation_msgs/CollisionMap.h>
00048
00049
00050 #include <sensor_msgs/PointCloud2.h>
00051 #include <std_srvs/Empty.h>
00052 #include <dynamic_reconfigure/server.h>
00053 #include <octomap_server/OctomapServerConfig.h>
00054
00055 #include <pcl/point_types.h>
00056 #include <pcl/ros/conversions.h>
00057 #include <pcl_ros/transforms.h>
00058 #include <pcl/sample_consensus/method_types.h>
00059 #include <pcl/sample_consensus/model_types.h>
00060 #include <pcl/segmentation/sac_segmentation.h>
00061 #include <pcl/io/pcd_io.h>
00062 #include <pcl/filters/extract_indices.h>
00063 #include <pcl/filters/passthrough.h>
00064
00065
00066 #include <tf/transform_listener.h>
00067 #include <tf/message_filter.h>
00068 #include <message_filters/subscriber.h>
00069 #include <octomap_msgs/Octomap.h>
00070 #include <octomap_msgs/GetOctomap.h>
00071 #include <octomap_msgs/BoundingBoxQuery.h>
00072 #include <octomap_msgs/conversions.h>
00073
00074 #include <octomap_ros/conversions.h>
00075 #include <octomap/octomap.h>
00076 #include <octomap/OcTreeKey.h>
00077
00078
00079 namespace octomap_server {
00080 class OctomapServer{
00081
00082 public:
00083 typedef pcl::PointCloud<pcl::PointXYZ> PCLPointCloud;
00084 typedef octomap_msgs::GetOctomap OctomapSrv;
00085 typedef octomap_msgs::BoundingBoxQuery BBXSrv;
00086
00087 typedef octomap::OcTree OcTreeT;
00088
00089 OctomapServer(ros::NodeHandle private_nh_ = ros::NodeHandle("~"));
00090 virtual ~OctomapServer();
00091 virtual bool octomapBinarySrv(OctomapSrv::Request &req, OctomapSrv::GetOctomap::Response &res);
00092 virtual bool octomapFullSrv(OctomapSrv::Request &req, OctomapSrv::GetOctomap::Response &res);
00093 bool clearBBXSrv(BBXSrv::Request& req, BBXSrv::Response& resp);
00094 bool resetSrv(std_srvs::Empty::Request& req, std_srvs::Empty::Response& resp);
00095
00096 virtual void insertCloudCallback(const sensor_msgs::PointCloud2::ConstPtr& cloud);
00097 virtual bool openFile(const std::string& filename);
00098
00099 protected:
00100 inline static void updateMinKey(const octomap::OcTreeKey& in, octomap::OcTreeKey& min){
00101 for (unsigned i=0; i<3; ++i)
00102 min[i] = std::min(in[i], min[i]);
00103 };
00104
00105 inline static void updateMaxKey(const octomap::OcTreeKey& in, octomap::OcTreeKey& max){
00106 for (unsigned i=0; i<3; ++i)
00107 max[i] = std::max(in[i], max[i]);
00108 };
00109
00111 inline bool isInUpdateBBX(const octomap::OcTree::iterator& it) const{
00112
00113 unsigned voxelWidth = (1 << (m_maxTreeDepth - it.getDepth()));
00114 octomap::OcTreeKey key = it.getIndexKey();
00115 return (key[0]+voxelWidth >= m_updateBBXMin[0]
00116 && key[1]+voxelWidth >= m_updateBBXMin[1]
00117 && key[0] <= m_updateBBXMax[0]
00118 && key[1] <= m_updateBBXMax[1]);
00119 }
00120
00121 void reconfigureCallback(octomap_server::OctomapServerConfig& config, uint32_t level);
00122 void publishBinaryOctoMap(const ros::Time& rostime = ros::Time::now()) const;
00123 void publishFullOctoMap(const ros::Time& rostime = ros::Time::now()) const;
00124 void publishAll(const ros::Time& rostime = ros::Time::now());
00125
00134 virtual void insertScan(const tf::Point& sensorOrigin, const PCLPointCloud& ground, const PCLPointCloud& nonground);
00135
00137 void filterGroundPlane(const PCLPointCloud& pc, PCLPointCloud& ground, PCLPointCloud& nonground) const;
00138
00144 bool isSpeckleNode(const octomap::OcTreeKey& key) const;
00145
00147 virtual void handlePreNodeTraversal(const ros::Time& rostime);
00148
00150 virtual void handleNode(const OcTreeT::iterator& it) {};
00151
00153 virtual void handleNodeInBBX(const OcTreeT::iterator& it) {};
00154
00156 virtual void handleOccupiedNode(const OcTreeT::iterator& it);
00157
00159 virtual void handleOccupiedNodeInBBX(const OcTreeT::iterator& it);
00160
00162 virtual void handleFreeNode(const OcTreeT::iterator& it);
00163
00165 virtual void handleFreeNodeInBBX(const OcTreeT::iterator& it);
00166
00168 virtual void handlePostNodeTraversal(const ros::Time& rostime);
00169
00171 virtual void update2DMap(const OcTreeT::iterator& it, bool occupied);
00172
00173 inline unsigned mapIdx(int i, int j) const{
00174 return m_gridmap.info.width*j + i;
00175 }
00176
00177 inline unsigned mapIdx(const octomap::OcTreeKey& key) const{
00178 return mapIdx((key[0] - m_paddedMinKey[0])/m_multires2DScale,
00179 (key[1] - m_paddedMinKey[1])/m_multires2DScale);
00180
00181 }
00182
00189 void adjustMapData(nav_msgs::OccupancyGrid& map, const nav_msgs::MapMetaData& oldMapInfo) const;
00190
00191 inline bool mapChanged(const nav_msgs::MapMetaData& oldMapInfo, const nav_msgs::MapMetaData& newMapInfo){
00192 return ( oldMapInfo.height != newMapInfo.height
00193 || oldMapInfo.width !=newMapInfo.width
00194 || oldMapInfo.origin.position.x != newMapInfo.origin.position.x
00195 || oldMapInfo.origin.position.y != newMapInfo.origin.position.y);
00196 }
00197
00198 static std_msgs::ColorRGBA heightMapColor(double h);
00199 ros::NodeHandle m_nh;
00200 ros::Publisher m_markerPub, m_binaryMapPub, m_fullMapPub, m_pointCloudPub, m_collisionObjectPub, m_mapPub, m_cmapPub, m_fmapPub, m_fmarkerPub;
00201 message_filters::Subscriber<sensor_msgs::PointCloud2>* m_pointCloudSub;
00202 tf::MessageFilter<sensor_msgs::PointCloud2>* m_tfPointCloudSub;
00203 ros::ServiceServer m_octomapBinaryService, m_octomapFullService, m_clearBBXService, m_resetService;
00204 tf::TransformListener m_tfListener;
00205 dynamic_reconfigure::Server<OctomapServerConfig> m_reconfigureServer;
00206
00207 octomap::OcTree* m_octree;
00208 octomap::KeyRay m_keyRay;
00209 octomap::OcTreeKey m_updateBBXMin;
00210 octomap::OcTreeKey m_updateBBXMax;
00211
00212 double m_maxRange;
00213 std::string m_worldFrameId;
00214 std::string m_baseFrameId;
00215 bool m_useHeightMap;
00216 std_msgs::ColorRGBA m_color;
00217 std_msgs::ColorRGBA m_colorFree;
00218 double m_colorFactor;
00219
00220 bool m_latchedTopics;
00221 bool m_publishFreeSpace;
00222
00223 double m_res;
00224 unsigned m_treeDepth;
00225 unsigned m_maxTreeDepth;
00226 double m_probHit;
00227 double m_probMiss;
00228 double m_thresMin;
00229 double m_thresMax;
00230
00231 double m_pointcloudMinZ;
00232 double m_pointcloudMaxZ;
00233 double m_occupancyMinZ;
00234 double m_occupancyMaxZ;
00235 double m_minSizeX;
00236 double m_minSizeY;
00237 bool m_filterSpeckles;
00238
00239 bool m_filterGroundPlane;
00240 double m_groundFilterDistance;
00241 double m_groundFilterAngle;
00242 double m_groundFilterPlaneDistance;
00243
00244 bool m_compressMap;
00245
00246
00247 bool m_incrementalUpdate;
00248 nav_msgs::OccupancyGrid m_gridmap;
00249 bool m_publish2DMap;
00250 bool m_mapOriginChanged;
00251 octomap::OcTreeKey m_paddedMinKey;
00252 unsigned m_multires2DScale;
00253 bool m_projectCompleteMap;
00254 };
00255 }
00256
00257 #endif