mofa_bridge.cpp
Go to the documentation of this file.
00001 #include "mofa_bridge.h"
00002 
00003 #ifdef PUBLISHIMAGE
00004 MofaBridge::MofaBridge() : imgtransport_(nh) {
00005 #else
00006 MofaBridge::MofaBridge() {
00007 #endif
00008 
00009   //init class attributes if necessary
00010   idx = 0;
00011   this->nc=0;
00012   this->nr=0;
00013 
00014   //init socket attributes
00015   this->serverip = "127.0.0.1";
00016   this->port = 4321;
00017   //create Server Socket
00018   this->csocket = new CSocketClient("Client socket");
00019   //create Event Socket
00020   this->eventserver = CEventServer::instance(); 
00021 
00022   //string for port names
00023   std::string pcl2_sub_name                    = "/mofa_bridge/input/pcl2/raw";
00024   std::string labeled_pcl2_sub_name            = "/mofa_bridge/output/pcl2/segmented";
00025   std::string request_observation_sub_name     = "/mofa_bridge/srv/request_observation";
00026 
00027   // [init subscribers]
00028   this->pcl2_sub = this->nh.subscribe(pcl2_sub_name, 1, &MofaBridge::pcl2_sub_callback, this);
00029   
00030   // [init publishers]
00031   this->labeled_pcl2_publisher = this->nh.advertise< pcl::PointCloud<pcl::PointXYZRGB> > (labeled_pcl2_sub_name, 5);
00032 
00033 #ifdef PUBLISHIMAGE
00034   this->image_pub_ = imgtransport_.advertise("image/out", 1);
00035 #endif
00036 
00037   // [init services]
00038   this->obs_request = this->nh.advertiseService(request_observation_sub_name, &MofaBridge::obs_request_callback, this);
00039 
00040   // [init clients]
00041   
00042   // [init action servers]
00043   
00044   // [init action clients]
00045   ROS_INFO("Starting mofaBridge Node");
00046   ROS_INFO("Use [ rosservice call /request_observation ] to store current range image and compute observation");
00047 }
00048 
00049 void MofaBridge::open(){
00050     TSocket_info config;
00051     config.address = this->serverip.c_str();
00052     config.port = this->port;
00053     ROS_INFO("[MoFA] Open\n");
00054 #ifdef DEBUGGING
00055                   reloadRGB();
00056 #endif
00057     
00058     //todo catch exception and reconnect after delay
00059     this->csocket->open(&config);
00060     this->csocket->config();
00061     this->connected = true;
00062     
00063     events.push_back(csocket->get_error_event_id());
00064     ROS_INFO("[MoFA] Error event id: %s\n", events.back().c_str());
00065                 
00066     events.push_back(csocket->get_rx_event_id());
00067     ROS_INFO("[MoFA] Rx event id: %s\n", events.back().c_str());
00068                 
00069 }
00070 
00071 /*  [subscriber callbacks] */
00072 void MofaBridge::pcl2_sub_callback(const sensor_msgs::PointCloud2ConstPtr& msg) 
00073 { 
00074   pcl::fromROSMsg(*msg, this->pcl_xyzrgb);
00075 
00076   this->nc=msg->width;
00077   this->nr=msg->height;
00078 
00079 }
00080 
00081 /*  [service callback] */
00082 bool MofaBridge::obs_request_callback(iri_wam_common_msgs::obs_request::Request &req, iri_wam_common_msgs::obs_request::Response &res){
00083 
00084   // [xyzRGB matlab file name]
00085   std::stringstream matlab_xyzrgb_fn;
00086   matlab_xyzrgb_fn << "last_image.dat";
00087 
00088   // [open xyzRGB matlab file]
00089   matlab_xyzrgb.open(matlab_xyzrgb_fn.str().c_str(), std::ios::out);
00090 
00091   // [store xyzRGB matlab file]
00092   pcl::PointCloud<pcl::PointXYZRGB>::iterator pt_iter = this->pcl_xyzrgb.begin();
00093   for(int rr = 0; rr < (int)pcl_xyzrgb.height; ++rr) {
00094     for(int cc = 0; cc < (int)pcl_xyzrgb.width; ++cc, ++pt_iter) {
00095         if(rr%2 == 0){ //burde downsampling
00096             if(cc%2 == 0){ //burde downsampling
00097                pcl::PointXYZRGB &pt = *pt_iter;
00098                RGBValue color;
00099                color.float_value = pt.rgb;
00100                matlab_xyzrgb << rr << " " << cc << " " << pt.x*1000 << " " << pt.y*1000 << " " << pt.z*1000 << " " << (int)color.Red << " " << (int)color.Green << " " << (int)color.Blue << std::endl;
00101             }
00102         }
00103     }
00104   }
00105 
00106   // [close range image file]
00107   matlab_xyzrgb.close();
00108   ROS_INFO("[MoFA] Saved XYZRGB image %s", matlab_xyzrgb_fn.str().c_str());
00109 
00110   int numbytes = 0;
00111   bool dataread = false;
00112   int numdata = 0;
00113   if(connected){
00114 
00115       int request = htonl(1);
00116       ROS_INFO("writing request");
00117       this->csocket->write((unsigned char*)&request,sizeof(int)); 
00118 
00119       ROS_INFO("[MoFA] Waiting event\n");
00120       try{
00121         //TODO this way of ensuring we're reading 4 bytes needs polishing
00122         while(!dataread && this->connected == true){
00123           int eventpos = this->eventserver->wait_first(events,4500000);
00124           switch(eventpos){
00125               case ERROR_POS: //disconnection
00126                   ROS_INFO("[MoFA] Socket error. Assuming disconnected. \n");
00127                   this->connected = false;
00128                   break;
00129               case RX_ID_POS: //rx_id
00130                   numdata = this->csocket->get_num_data();
00131                   ROS_INFO("[MoFA] New data on socket (%d Bytes)\n",numdata); 
00132                   if(numdata < sizeof(int)) //ensure all data is there
00133                     break;
00134                   numbytes = this->csocket->read((unsigned char *) &num_objects, sizeof(int)); 
00135                   dataread = true;
00136                   ROS_INFO("%d bytes read", numbytes);
00137 //                  if(numbytes < sizeof(int))
00138 //                    break;
00139                   num_objects = ntohl(num_objects);
00140                   if(this->num_objects < 0){
00141                       ROS_ERROR("MOFA reported an error %d",num_objects);
00142                   }else{
00143                       ROS_INFO("MOFA reported %d objects through the socket",num_objects);
00144                   }
00145 
00146                   sleep(1); //ensure the file is written?
00147                   reloadRGB();
00148                   res.num_objects = this->numobjset.size() - 1 ; 
00149                   res.num_objectsA = this->numobjAset.size() - 1 ; 
00150                   res.num_objectsB = this->numobjBset.size() - 1 ; 
00151                   ROS_INFO("Sending %d objects on A, %d on B of a total of %d",res.num_objectsA,res.num_objectsB,res.num_objects);
00152                   this->labeled_pcl2_publisher.publish(this->pcl_xyzrgb);
00153 
00154 #ifdef PUBLISHIMAGE
00155                   cv_image.header.stamp = ros::Time::now();
00156                   cv_image.header.frame_id = "camera";
00157                   cv_image.encoding = "bgr8"; 
00158                   cv_image.image = rgb_image;
00159                   image_pub_.publish(cv_image.toImageMsg());
00160 #endif
00161 
00162                   break;
00163               default:
00164                   ROS_ERROR("Unexpected event triggered by eventserver");
00165                   break;
00166           }
00167         }
00168       }catch(CEventTimeoutException &e){
00169           ROS_ERROR("Time out exception");
00170       }
00171   }else{
00172     ROS_ERROR("Mofa not connected"); 
00173   }
00174 
00175   return true;
00176 }
00177 
00178 void MofaBridge::reloadRGB(){
00179   //  int x, y, z, u, v;
00180     int red, green, blue;
00181     int label;
00182     RGBValue color;
00183     // [xyzRGB matlab file name]
00184     std::stringstream matlab_xyzrgb_fn;
00185     matlab_xyzrgb_fn << "last_image_labelled.dat";
00186 
00187   this->numobjset.clear();
00188   this->numobjAset.clear();
00189   this->numobjBset.clear();
00190 
00191   // [open xyzRGB matlab file]
00192   matlab_xyzrgb_in.open(matlab_xyzrgb_fn.str().c_str(), std::ios::in);
00193 
00194   // [store xyzRGB matlab file]
00195   pcl::PointCloud<pcl::PointXYZRGB>::iterator pt_iter = this->pcl_xyzrgb.begin();
00196   ROS_DEBUG("pcl height & width %d %d",(int)pcl_xyzrgb.height,(int)pcl_xyzrgb.width);
00197   for (int rr = 0; rr < (int)pcl_xyzrgb.height; ++rr) {
00198         for (int cc = 0; cc < (int)pcl_xyzrgb.width; ++cc, ++pt_iter) {
00199           pcl::PointXYZRGB &pt = *pt_iter;
00200             if(rr%2 == 0 && cc%2 == 0){
00201           
00202         //      matlab_xyzrgb_in >> u;
00203         //      matlab_xyzrgb_in >> v;
00204         //
00205         //      matlab_xyzrgb_in >> x;
00206         //      matlab_xyzrgb_in >> y;
00207         //      matlab_xyzrgb_in >> z;
00208         
00209               matlab_xyzrgb_in >> label;
00210         
00211               matlab_xyzrgb_in >> red;
00212               matlab_xyzrgb_in >> green;
00213               matlab_xyzrgb_in >> blue;
00214         
00215         //       if(label == 0){
00216         //         ROS_INFO("label %d RGB %d %d %d", label, red, green, blue);
00217         //       }
00218         
00219               //TODO more efficient ways of doing this:
00220               this->numobjset.insert(label); 
00221         
00222               if(cc < (int)(pcl_xyzrgb.width/2)){ 
00223                   this->numobjAset.insert(label); 
00224               }else{
00225                   this->numobjBset.insert(label); 
00226               }
00227         
00228               color.Red = red;
00229               color.Green = green;
00230               color.Blue = blue;
00231     
00232               pt.rgb = color.float_value;
00233 
00234               #ifdef PUBLISHIMAGE              
00235                     cv_image.at<Vec3b>(rr,cc)[0] = color.Blue;
00236                     cv_image.at<Vec3b>(rr,cc)[1] = color.Green;
00237                     cv_image.at<Vec3b>(rr,cc)[2] = color.Red;
00238               #endif
00239 
00240            }else{
00241               //burde upsampling
00242               color.Red = red;
00243               color.Green = green;
00244               color.Blue = blue;
00245               pt.rgb = color.float_value;
00246 
00247               #ifdef PUBLISHIMAGE              
00248                     cv_image.at<Vec3b>(rr,cc)[0] = color.Blue;
00249                     cv_image.at<Vec3b>(rr,cc)[1] = color.Green;
00250                     cv_image.at<Vec3b>(rr,cc)[2] = color.Red;
00251               #endif
00252            }
00253         }
00254   }
00255 
00256   ROS_INFO("Without the background, %d objects on A, %d on B of a total of %d",this->numobjAset.size()-1,this->numobjBset.size()-1,this->numobjset.size()-1);
00257   ROS_INFO("last label was %d",label);
00258 
00259   std::ostream_iterator< double > output( std::cout, " " ); 
00260   std::cout << "A contains: ";
00261   std::copy( this->numobjAset.begin(), this->numobjAset.end(), output );
00262   std::cout << std::endl << "B contains: ";
00263   std::copy( this->numobjBset.begin(), this->numobjBset.end(), output );
00264   std::cout << std::endl << "total contains: ";
00265   std::copy( this->numobjset.begin(), this->numobjset.end(), output );
00266 
00267   // [close range image file]
00268   matlab_xyzrgb_in.close();
00269   ROS_INFO("Loaded and processed segmented pcl image %s",matlab_xyzrgb_fn.str().c_str());
00270 
00271 }
00272 
00273 int main(int argc, char** argv)
00274 {
00275   ros::init(argc, argv, "mofa_bridge");
00276   MofaBridge mofa_bridge;
00277   mofa_bridge.open();
00278   ros::spin();
00279   return 0;
00280 }


iri_pcl_filters
Author(s): Sergi Foix
autogenerated on Fri Dec 6 2013 20:44:42