Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #include <ros/ros.h>
00031 #include <octomap_msgs/conversions.h>
00032 #include <octomap/octomap.h>
00033 #include <fstream>
00034 
00035 #include <octomap_msgs/GetOctomap.h>
00036 using octomap_msgs::GetOctomap;
00037 
00038 #define USAGE "\nUSAGE: octomap_server_static <mapfile.[bt|ot]>\n" \
00039                 "  mapfile.bt: OctoMap filename to be loaded (.bt: binary tree, .ot: general octree)\n"
00040 
00041 using namespace std;
00042 using namespace octomap;
00043 
00044 class OctomapServerStatic{
00045 public:
00046   OctomapServerStatic(const std::string& filename)
00047     : m_octree(NULL), m_worldFrameId("/map")
00048   {
00049 
00050     ros::NodeHandle private_nh("~");
00051     private_nh.param("frame_id", m_worldFrameId, m_worldFrameId);
00052 
00053 
00054     
00055     if (filename.length() <= 3){
00056       ROS_ERROR("Octree file does not have .ot extension");
00057       exit(1);
00058     }
00059 
00060     std::string suffix = filename.substr(filename.length()-3, 3);
00061 
00062     
00063     if (suffix == ".bt"){
00064       OcTree* octree = new OcTree(filename);
00065 
00066       m_octree = octree;
00067     } else if (suffix == ".ot"){
00068       AbstractOcTree* tree = AbstractOcTree::read(filename);
00069       if (!tree){
00070         ROS_ERROR("Could not read octree from file");
00071         exit(1);
00072       }
00073 
00074       m_octree = dynamic_cast<AbstractOccupancyOcTree*>(tree);
00075 
00076     } else{
00077       ROS_ERROR("Octree file does not have .bt or .ot extension");
00078       exit(1);
00079     }
00080 
00081     if (!m_octree ){
00082       ROS_ERROR("Could not read right octree class in file");
00083       exit(1);
00084     }
00085 
00086     ROS_INFO("Read octree type \"%s\" from file %s", m_octree->getTreeType().c_str(), filename.c_str());
00087     ROS_INFO("Octree resultion: %f, size: %zu", m_octree->getResolution(), m_octree->size());
00088 
00089 
00090     m_octomapBinaryService = m_nh.advertiseService("octomap_binary", &OctomapServerStatic::octomapBinarySrv, this);
00091     m_octomapFullService = m_nh.advertiseService("octomap_full", &OctomapServerStatic::octomapFullSrv, this);
00092 
00093   }
00094 
00095   ~OctomapServerStatic(){
00096 
00097 
00098   }
00099 
00100   bool octomapBinarySrv(GetOctomap::Request  &req,
00101                         GetOctomap::Response &res)
00102   {
00103     ROS_INFO("Sending binary map data on service request");
00104     res.map.header.frame_id = m_worldFrameId;
00105     res.map.header.stamp = ros::Time::now();
00106     if (!octomap_msgs::binaryMapToMsg(*m_octree, res.map))
00107       return false;
00108 
00109     return true;
00110   }
00111 
00112   bool octomapFullSrv(GetOctomap::Request  &req,
00113                                      GetOctomap::Response &res)
00114   {
00115     ROS_INFO("Sending full map data on service request");
00116     res.map.header.frame_id = m_worldFrameId;
00117     res.map.header.stamp = ros::Time::now();
00118 
00119 
00120     if (!octomap_msgs::fullMapToMsg(*m_octree, res.map))
00121       return false;
00122 
00123     return true;
00124   }
00125 
00126 private:
00127   ros::ServiceServer m_octomapBinaryService, m_octomapFullService;
00128   ros::NodeHandle m_nh;
00129   std::string m_worldFrameId;
00130   AbstractOccupancyOcTree* m_octree;
00131 
00132 };
00133 
00134 int main(int argc, char** argv){
00135   ros::init(argc, argv, "octomap_server_static");
00136   std::string mapFilename("");
00137 
00138   if (argc == 2)
00139     mapFilename = std::string(argv[1]);
00140   else{
00141     ROS_ERROR("%s", USAGE);
00142     exit(1);
00143   }
00144 
00145   try{
00146     OctomapServerStatic ms(mapFilename);
00147     ros::spin();
00148   }catch(std::runtime_error& e){
00149     ROS_ERROR("octomap_server_static exception: %s", e.what());
00150     exit(2);
00151   }
00152 
00153   exit(0);
00154 }
00155 
00156