wifi_view_node.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Seigo ITO
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of Seigo ITO nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 // ROS
00031 #include "ros/ros.h"
00032 #include "ros/package.h"
00033 #include "sensor_msgs/PointCloud2.h"
00034 
00035 // PCL
00036 #include "pcl/io/pcd_io.h"
00037 #include "pcl/point_types.h"
00038 #include "pcl/filters/voxel_grid.h"
00039 
00040 // boost
00041 #include "boost/filesystem.hpp"
00042 #include "boost/filesystem/path.hpp"
00043 #include "boost/filesystem/operations.hpp"
00044 #include "boost/filesystem/fstream.hpp"
00045 #include "boost/tokenizer.hpp"
00046 #include "boost/algorithm/string.hpp"
00047 
00048 class WifiView
00049 {
00050 public:
00051   WifiView();
00052   ~WifiView();
00053   void load();
00054   void run();
00055 
00056 private:
00057   ros::NodeHandle nh_;
00058 
00059   // size of boxel grid filter
00060   float box_x_,box_y_,box_z_;
00061 
00062   // signal strength map
00063   std::map<std::string,sensor_msgs::PointCloud2> ap_map_;
00064 
00065   std::vector<ros::Publisher> wifi_map_pub_;
00066 
00067 };
00068 
00069 
00070 
00071 WifiView::WifiView()
00072 {
00073   // size of boxel grid filter
00074   box_x_ = 1.0;
00075   box_y_ = 1.0;
00076   box_z_ = 256.0;
00077 
00078   // load Wifi Map
00079   load();
00080 
00081   // Mac address map publisher
00082   std::map<std::string,sensor_msgs::PointCloud2>::iterator it;
00083 
00084   it = ap_map_.begin();
00085 
00086   while(it != ap_map_.end())
00087   {
00088     std::ostringstream topic;
00089     topic << "wifimap" << (*it).first;
00090     std::string t = boost::algorithm::replace_all_copy(topic.str(),":","");
00091     ros::Publisher each_map_pub = nh_.advertise<sensor_msgs::PointCloud2>(t.c_str(), 1);
00092     wifi_map_pub_.push_back(each_map_pub);
00093     ++it;
00094   }
00095 }
00096 
00097 
00098 
00099 WifiView::~WifiView()
00100 {
00101 
00102 }
00103 
00104 
00105 
00106 void WifiView::load()
00107 {
00108   int ap_count = 0;
00109   namespace f = boost::filesystem;
00110   f::path map_dir = ros::package::getPath("wifi_tools") + "/map";
00111   f::directory_iterator end;
00112   for( f::directory_iterator it(map_dir); it!=end; ++it )
00113   {
00114     if( !(f::is_directory(*it)) )
00115     {
00116       //mac address
00117       std::string leaf = (*it).path().leaf().c_str();
00118       boost::char_separator< char > sep(".");
00119       boost::tokenizer< boost::char_separator< char > > tokens(leaf, sep);
00120       boost::tokenizer< boost::char_separator< char > >::iterator file_it;
00121       file_it = tokens.begin();
00122       std::string mac_address = (std::string)(*file_it);
00123 
00124       // load WiFi data
00125       std::ifstream ap_ifs((*it).path().c_str());
00126       pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>),cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
00127       pcl::io::loadPCDFile<pcl::PointXYZ> ((*it).path().c_str(), *cloud);
00128 
00129       // boxel grid filter
00130       pcl::VoxelGrid<pcl::PointXYZ> box;
00131       box.setInputCloud (cloud);
00132       box.setLeafSize (box_x_,box_y_,box_z_);
00133       box.filter (*cloud_filtered);
00134 
00135       // cache data
00136       sensor_msgs::PointCloud2::Ptr cloud_msg(new sensor_msgs::PointCloud2());
00137       pcl::toROSMsg(*cloud_filtered, *cloud_msg);
00138       (*cloud_msg).header.frame_id = "/map";
00139       (*cloud_msg).header.stamp = ros::Time::now();
00140       ap_map_.insert(std::map<std::string,sensor_msgs::PointCloud2 >::value_type(mac_address,*cloud_msg));
00141       ap_count++;
00142     }
00143   }
00144   ROS_INFO("%d access points data have loaded.",ap_count);
00145 }
00146 
00147 
00148 
00149 void WifiView::run()
00150 {
00151   std::map<std::string,sensor_msgs::PointCloud2>::iterator it;
00152   std::vector<ros::Publisher>::iterator pub_it;
00153   it = ap_map_.begin();
00154   pub_it = wifi_map_pub_.begin();
00155   while(it != ap_map_.end())
00156   {
00157     (*pub_it).publish((*it).second);
00158     ++pub_it;
00159     ++it;
00160   }
00161 }
00162 
00163 
00164 
00165 int main(int argc, char** argv)
00166 {
00167   ros::init(argc, argv, "wifi_viewer");
00168   ros::NodeHandle nh;
00169   WifiView wifi_map;
00170 
00171   ros::Rate rate_Hz(1);
00172   while (ros::ok())
00173   {
00174     wifi_map.run();
00175     rate_Hz.sleep();
00176   }
00177   return (0);
00178 }


wifi_tools
Author(s): Seigo ITO
autogenerated on Mon Oct 6 2014 12:23:56