handeye_log.cpp
Go to the documentation of this file.
00001 #include "handeye_log.h"
00002 
00003 // In the case that XYZRI
00004 void
00005 store_xyzri_img_depth( sensor_msgs::PointCloud2 &pcl_xyzri, std::ofstream &img_intensity){
00006     for (unsigned int rr = 0; rr < pcl_xyzri.height; rr++){
00007         for (unsigned int cc = 0; cc < pcl_xyzri.width; cc++){
00008             int index = rr * pcl_xyzri.width + cc;
00009             float *pstep = (float*)&pcl_xyzri.data[(index) * pcl_xyzri.point_step];
00010             img_intensity << rr << " " << cc << " " <<  pstep[4] << std::endl;
00011         }
00012     }
00013 }
00014 
00015 // In the case that XYZRGB Point cloud is received
00016 void 
00017 load_xyzrgb_matlab_file( pcl::PointCloud<pcl::PointXYZRGB> &pcl_xyzrgb, std::ofstream &matlab_file){
00018 
00019   pcl::PointCloud<pcl::PointXYZRGB>::iterator pt_iter = pcl_xyzrgb.begin();
00020   for (unsigned int rr = 0; rr < pcl_xyzrgb.height; ++rr) {
00021     for (unsigned int cc = 0; cc < pcl_xyzrgb.width; ++cc, ++pt_iter) {
00022       pcl::PointXYZRGB &pt = *pt_iter;
00023       RGBValue color;
00024       color.float_value= pt.rgb;
00025       //matlab_file << rr << " " << cc << " " << pt.x*1000 << " " << pt.y*1000 << " " << pt.z*1000 << " " << (int)color.Red << " " << (int)color.Green << " " << (int)color.Blue << std::endl;
00026       matlab_file << rr << " " << cc << " " << pt.x << " " << pt.y << " " << pt.z << " " << (int)color.Red << " " << (int)color.Green << " " << (int)color.Blue << std::endl;
00027     }
00028   }
00029 }
00030 
00031 // In the case that XYZRI Point cloud is received
00032 void
00033 load_xyzri_matlab_file( sensor_msgs::PointCloud2 &pcl_xyzri, std::ofstream &matlab_file){
00034     for (unsigned int rr = 0; rr < pcl_xyzri.height; rr++){
00035         for (unsigned int cc = 0; cc < pcl_xyzri.width; cc++){
00036             int index = rr * pcl_xyzri.width + cc;
00037             float *pstep = (float*)&pcl_xyzri.data[(index) * pcl_xyzri.point_step];
00038             // [ row column x y z range intensity]
00039             matlab_file << rr << " " << cc << " " <<  pstep[0] << " " << pstep[1] << " " << pstep[2] << " " <<  pstep[3] << " " << pstep[4] << std::endl;
00040         }
00041     }
00042 }
00043 
00044 // In the case that PointWithRange Point cloud is received
00045 void
00046 load_xyzr_matlab_file( pcl::PointCloud<pcl::PointWithRange> &pcl_xyzr, std::ofstream &matlab_file){
00047 
00048   pcl::PointCloud<pcl::PointWithRange>::iterator pt_iter = pcl_xyzr.begin();
00049   for (unsigned int rr = 0; rr < pcl_xyzr.height; ++rr) {
00050     for (unsigned int cc = 0; cc < pcl_xyzr.width; ++cc, ++pt_iter) {
00051       pcl::PointWithRange &pt = *pt_iter;
00052       //matlab_file << rr << " " << cc << " " << pt.x*1000 << " " << pt.y*1000 << " " << pt.z*1000 << " " << pt.intensity << std::endl;
00053       matlab_file << rr << " " << cc << " " << pt.x << " " << pt.y << " " << pt.z << " " << pt.range << std::endl;
00054     }
00055   }
00056 }
00057 
00058 // In the case that XYZ Point cloud is received
00059 void
00060 load_xyz_matlab_file( pcl::PointCloud<pcl::PointXYZ> &pcl_xyz, std::ofstream &matlab_file){
00061 
00062   pcl::PointCloud<pcl::PointXYZ>::iterator pt_iter = pcl_xyz.begin();
00063   for (unsigned int rr = 0; rr < pcl_xyz.height; ++rr) {
00064     for (unsigned int cc = 0; cc < pcl_xyz.width; ++cc, ++pt_iter) {
00065       pcl::PointXYZ &pt = *pt_iter;
00066       //matlab_file << rr << " " << cc << " " << pt.x*1000 << " " << pt.y*1000 << " " << pt.z*1000 << std::endl;
00067       matlab_file << rr << " " << cc << " " << pt.x << " " << pt.y << " " << pt.z << std::endl;
00068     }
00069   }
00070 }
00071 
00072 // In the case that XYZRGB Point cloud is received
00073 void
00074 load_xyzrgb_img_depth( pcl::PointCloud<pcl::PointXYZRGB> &pcl_xyzrgb, std::ofstream &img_depth){
00075   pcl::PointCloud<pcl::PointXYZRGB>::iterator pt_iter = pcl_xyzrgb.begin();
00076   for (unsigned int rr = 0; rr < pcl_xyzrgb.height; ++rr) {
00077     for (unsigned int cc = 0; cc < pcl_xyzrgb.width; ++cc, ++pt_iter) {
00078       pcl::PointXYZRGB &pt = *pt_iter;
00079       //img_depth << rr << " " << cc << " " <<  pt.z*1000 << std::endl;
00080       img_depth << rr << " " << cc << " " <<  pt.z << std::endl;
00081     }
00082   }
00083 }
00084 
00085 // In the case that XYZRI Point cloud is received
00086 void
00087 load_xyzri_img_depth( sensor_msgs::PointCloud2 &pcl_xyzri, std::ofstream &img_depth){
00088     for (unsigned int rr = 0; rr < pcl_xyzri.height; rr++){
00089         for (unsigned int cc = 0; cc < pcl_xyzri.width; cc++){
00090             int index = rr * pcl_xyzri.width + cc;
00091             float *pstep = (float*)&pcl_xyzri.data[(index) * pcl_xyzri.point_step];
00092             img_depth << rr << " " << cc << " " <<  pstep[3] << std::endl;
00093         }
00094     }
00095 }
00096 
00097 // In the case that PointWithRange Point cloud is received
00098 void
00099 load_xyzr_img_depth( pcl::PointCloud<pcl::PointWithRange> &pcl_xyzr, std::ofstream &img_depth){
00100   pcl::PointCloud<pcl::PointWithRange>::iterator pt_iter = pcl_xyzr.begin();
00101   for (unsigned int rr = 0; rr < pcl_xyzr.height; ++rr) {
00102     for (unsigned int cc = 0; cc < pcl_xyzr.width; ++cc, ++pt_iter) {
00103       pcl::PointWithRange &pt = *pt_iter;
00104       img_depth << rr << " " << cc << " " <<  pt.range*1000 << std::endl;
00105     }
00106   }
00107 }
00108 
00109 // In the case that XYZ Point cloud is received
00110 void
00111 load_xyz_img_depth( pcl::PointCloud<pcl::PointXYZ> &pcl_xyz, std::ofstream &img_depth){
00112   pcl::PointCloud<pcl::PointXYZ>::iterator pt_iter = pcl_xyz.begin();
00113   for (unsigned int rr = 0; rr < pcl_xyz.height; ++rr) {
00114     for (unsigned int cc = 0; cc < pcl_xyz.width; ++cc, ++pt_iter) {
00115       pcl::PointXYZ &pt = *pt_iter;
00116       img_depth << rr << " " << cc << " " <<  pt.z*1000 << std::endl;
00117     }
00118   }
00119 }
00120 
00121 HandeyeLog::HandeyeLog(): idx(0), nc(0), nr(0), pcl_type_(0), num_captures_(1) {
00122 
00123   this->event_server = CEventServer::instance();
00124   this->thread_server = CThreadServer::instance();
00125 
00126   this->mpcl2_thread_id_ = "store_matlab_pcl2_thread";
00127   this->iimg_thread_id_ = "store_intensity_image_thread";
00128   this->ipng_thread_id_ = "store_intensity_png_thread";
00129   this->dimg_thread_id_ = "store_depth_image_thread";
00130   this->tf_robot_thread_id_ = "store_tf_robot_thread";
00131   this->tf_pattern_thread_id_ = "store_tf_pattern_thread";
00132   this->tf_robot_inverse_thread_id_ = "store_tf_robot_inverse_thread";
00133 
00134   this->thread_server->create_thread(this->mpcl2_thread_id_);
00135   this->thread_server->create_thread(this->iimg_thread_id_);
00136   this->thread_server->create_thread(this->ipng_thread_id_);
00137   this->thread_server->create_thread(this->dimg_thread_id_);
00138   this->thread_server->create_thread(this->tf_robot_thread_id_);
00139   this->thread_server->create_thread(this->tf_pattern_thread_id_);
00140   this->thread_server->create_thread(this->tf_robot_inverse_thread_id_);
00141 
00142   this->thread_server->attach_thread(this->mpcl2_thread_id_, this->store_mpcl2_thread, this);
00143   this->thread_server->attach_thread(this->iimg_thread_id_, this->store_iimg_thread, this);
00144   this->thread_server->attach_thread(this->ipng_thread_id_, this->store_ipng_thread, this);
00145   this->thread_server->attach_thread(this->dimg_thread_id_, this->store_dimg_thread, this);
00146   this->thread_server->attach_thread(this->tf_robot_thread_id_, this->store_tf_robot_thread, this);
00147   this->thread_server->attach_thread(this->tf_pattern_thread_id_, this->store_tf_pattern_thread, this);
00148   this->thread_server->attach_thread(this->tf_robot_inverse_thread_id_, this->store_tf_robot_inverse_thread, this);
00149 
00150   this->mpcl2_event_on_id_ = "event_mpcl2_ON";
00151   this->mpcl2_event_off_id_ = "event_mpcl2_OFF";
00152   this->mpcl2_event_stop_id_ = "event_mpcl2_STOP";
00153   this->iimg_event_on_id_ = "event_iimg_ON";
00154   this->iimg_event_off_id_ = "event_iimg_OFF";
00155   this->iimg_event_stop_id_ = "event_iimg_STOP";
00156   this->ipng_event_on_id_ = "event_ipng_ON";
00157   this->ipng_event_off_id_ = "event_ipng_OFF";
00158   this->ipng_event_stop_id_ = "event_ipng_STOP";
00159   this->dimg_event_on_id_ = "event_dimg_ON";
00160   this->dimg_event_off_id_ = "event_dimg_OFF";
00161   this->dimg_event_stop_id_ = "event_dimg_STOP";
00162   this->tf_robot_event_on_id_ = "event_tf_robot_ON";
00163   this->tf_robot_event_off_id_ = "event_tf_robot_OFF";
00164   this->tf_robot_event_stop_id_ = "event_tf_robot_STOP";
00165   this->tf_pattern_event_on_id_ = "event_tf_pattern_ON";
00166   this->tf_pattern_event_off_id_ = "event_tf_pattern_OFF";
00167   this->tf_pattern_event_stop_id_ = "event_tf_pattern_STOP";
00168   this->tf_robot_inverse_event_on_id_ = "event_tf_robot_inverse_ON";
00169   this->tf_robot_inverse_event_off_id_ = "event_tf_robot_inverse_OFF";
00170   this->tf_robot_inverse_event_stop_id_ = "event_tf_robot_inverse_STOP";
00171 
00172   this->event_server->create_event(this->mpcl2_event_on_id_);
00173   this->event_server->create_event(this->mpcl2_event_off_id_);
00174   this->event_server->create_event(this->mpcl2_event_stop_id_);
00175   this->event_server->create_event(this->iimg_event_on_id_);
00176   this->event_server->create_event(this->iimg_event_off_id_);
00177   this->event_server->create_event(this->iimg_event_stop_id_);
00178   this->event_server->create_event(this->ipng_event_on_id_);
00179   this->event_server->create_event(this->ipng_event_off_id_);
00180   this->event_server->create_event(this->ipng_event_stop_id_);
00181   this->event_server->create_event(this->dimg_event_on_id_);
00182   this->event_server->create_event(this->dimg_event_off_id_);
00183   this->event_server->create_event(this->dimg_event_stop_id_);
00184   this->event_server->create_event(this->tf_robot_event_on_id_);
00185   this->event_server->create_event(this->tf_robot_event_off_id_);
00186   this->event_server->create_event(this->tf_robot_event_stop_id_);
00187   this->event_server->create_event(this->tf_pattern_event_on_id_);
00188   this->event_server->create_event(this->tf_pattern_event_off_id_);
00189   this->event_server->create_event(this->tf_pattern_event_stop_id_);
00190   this->event_server->create_event(this->tf_robot_inverse_event_on_id_);
00191   this->event_server->create_event(this->tf_robot_inverse_event_off_id_);
00192   this->event_server->create_event(this->tf_robot_inverse_event_stop_id_);
00193 
00194   this->thread_server->start_thread(this->mpcl2_thread_id_);
00195   this->thread_server->start_thread(this->iimg_thread_id_);
00196   this->thread_server->start_thread(this->ipng_thread_id_);
00197   this->thread_server->start_thread(this->dimg_thread_id_);
00198   this->thread_server->start_thread(this->tf_robot_thread_id_);
00199   this->thread_server->start_thread(this->tf_pattern_thread_id_);
00200   this->thread_server->start_thread(this->tf_robot_inverse_thread_id_);
00201 
00202   if (!this->nh.getParam("/handeye_log/flags/tf_robot", this->flag_tf_robot)){
00203       this->flag_tf_robot = false;
00204       this->nh.setParam("/handeye_log/flags/tf_robot", this->flag_tf_robot);
00205   }
00206 
00207   if (!this->nh.getParam("/handeye_log/flags/tf_pattern", this->flag_tf_pattern)){
00208       this->flag_tf_pattern = false;
00209       this->nh.setParam("/handeye_log/flags/tf_pattern", this->flag_tf_pattern);
00210   }
00211 
00212   if (!this->nh.getParam("/handeye_log/flags/matlab_pcl2", this->flag_mpcl2)){
00213       this->flag_mpcl2 = true;
00214       this->nh.setParam("/handeye_log/flags/matlab_pcl2", this->flag_mpcl2);
00215   }
00216 
00217   if (!this->nh.getParam("/handeye_log/flags/img_intensity", this->flag_iimg)){
00218       this->flag_iimg = true;
00219       this->nh.setParam("/handeye_log/flags/img_intensity", this->flag_iimg);
00220   }
00221 
00222   if (!this->nh.getParam("/handeye_log/flags/png_intensity", this->flag_ipng)){
00223       this->flag_ipng = true;
00224       this->nh.setParam("/handeye_log/flags/png_intensity", this->flag_ipng);
00225   }
00226 
00227   if (!this->nh.getParam("/handeye_log/flags/img_depth", this->flag_dimg)){
00228       this->flag_dimg = false;
00229       this->nh.setParam("/handeye_log/flags/img_depth", this->flag_dimg);
00230   }
00231 
00232   if (!this->nh.getParam("/handeye_log/flags/tf_robot_inverse", this->flag_tf_robot_inverse)){
00233       this->flag_tf_robot_inverse = false;
00234       this->nh.setParam("/handeye_log/flags/tf_robot_inverse", this->flag_tf_robot_inverse);
00235   }
00236 
00237   if (!this->nh.getParam("/handeye_log/num_captures", this->num_captures_)){
00238       this->num_captures_ = 1;
00239       this->nh.setParam("/handeye_log/num_captures", this->num_captures_);
00240   }
00241 
00242   this->cloudBuffer_.set_capacity(this->num_captures_);
00243   this->iimageBuffer_.set_capacity(this->num_captures_);
00244 
00245   //init class attributes if necessary
00246   this->tf_robot_ = this->tf_robot_.getIdentity();
00247   this->tf_pattern_ = this->tf_pattern_.getIdentity();
00248 
00249   //string for port names
00250   std::string pcl2_sub_name, iimg_sub_name, tf_robot_sub_name, tf_pattern_sub_name;
00251   pcl2_sub_name       = "input/pcl2/raw";
00252   iimg_sub_name       = "input/image/intensity";
00253   tf_robot_sub_name   = "input/pose/robot";
00254   tf_pattern_sub_name = "input/pose/pattern";
00255 
00256   // [init subscribers]
00257   this->pcl2_sub = this->nh.subscribe(pcl2_sub_name, 1, &HandeyeLog::pcl2_sub_callback, this);
00258   this->iimg_sub = this->nh.subscribe(iimg_sub_name, 1, &HandeyeLog::iimg_sub_callback, this);
00259   this->tf_robot_sub = this->nh.subscribe(tf_robot_sub_name, 1, &HandeyeLog::tf_robot_sub_callback, this);
00260   this->tf_pattern_sub = this->nh.subscribe(tf_pattern_sub_name, 1, &HandeyeLog::tf_pattern_sub_callback, this);
00261 
00262   // [init publishers]
00263 
00264   // [init services]
00265   this->srv = this->nh.advertiseService("store_image", &HandeyeLog::srv_callback, this);
00266 
00267   // [init clients]
00268 
00269   // [init action servers]
00270 
00271   // [init action clients]
00272   ROS_INFO("Starting storeRangeImage Node");
00273   ROS_INFO("Use [ rosservice call /store_image ] to store current range image");
00274 }
00275 
00276 HandeyeLog::~HandeyeLog()
00277 {
00278   // Assures to stop all threads correctly
00279   this->event_server->set_event(this->mpcl2_event_stop_id_);
00280   this->event_server->set_event(this->iimg_event_stop_id_);
00281   this->event_server->set_event(this->ipng_event_stop_id_);
00282   this->event_server->set_event(this->dimg_event_stop_id_);
00283   this->event_server->set_event(this->tf_robot_event_stop_id_);
00284   this->event_server->set_event(this->tf_pattern_event_stop_id_);
00285   this->event_server->set_event(this->tf_robot_inverse_event_stop_id_);
00286 
00287   this->thread_server->end_thread(this->mpcl2_thread_id_);
00288   this->thread_server->end_thread(this->iimg_thread_id_);
00289   this->thread_server->end_thread(this->ipng_thread_id_);
00290   this->thread_server->end_thread(this->dimg_thread_id_);
00291   this->thread_server->end_thread(this->tf_robot_thread_id_);
00292   this->thread_server->end_thread(this->tf_pattern_thread_id_);
00293   this->thread_server->end_thread(this->tf_robot_inverse_thread_id_);
00294 
00295   this->event_server->reset_event(this->mpcl2_event_on_id_);
00296   this->event_server->reset_event(this->iimg_event_on_id_);
00297   this->event_server->reset_event(this->ipng_event_on_id_);
00298   this->event_server->reset_event(this->dimg_event_on_id_);
00299   this->event_server->reset_event(this->tf_robot_event_on_id_);
00300   this->event_server->reset_event(this->tf_pattern_event_on_id_);
00301   this->event_server->reset_event(this->tf_robot_inverse_event_on_id_);
00302 
00303   this->thread_server->delete_thread(this->mpcl2_thread_id_);
00304   this->thread_server->delete_thread(this->iimg_thread_id_);
00305   this->thread_server->delete_thread(this->ipng_thread_id_);
00306   this->thread_server->delete_thread(this->dimg_thread_id_);
00307   this->thread_server->delete_thread(this->tf_robot_thread_id_);
00308   this->thread_server->delete_thread(this->tf_pattern_thread_id_);
00309   this->thread_server->delete_thread(this->tf_robot_inverse_thread_id_);
00310 
00311   this->event_server->delete_event(this->mpcl2_event_on_id_);
00312   this->event_server->delete_event(this->iimg_event_on_id_);
00313   this->event_server->delete_event(this->ipng_event_on_id_);
00314   this->event_server->delete_event(this->dimg_event_on_id_);
00315   this->event_server->delete_event(this->tf_robot_event_on_id_);
00316   this->event_server->delete_event(this->tf_pattern_event_on_id_);
00317   this->event_server->delete_event(this->tf_robot_inverse_event_on_id_);
00318 
00319   this->mpcl2_thread_id_ = "";
00320   this->iimg_thread_id_ = "";
00321   this->ipng_thread_id_ = "";
00322   this->dimg_thread_id_ = "";
00323   this->tf_robot_thread_id_ = "";
00324   this->tf_pattern_thread_id_ = "";
00325   this->tf_robot_inverse_thread_id_ = "";
00326 
00327   this->mpcl2_event_on_id_ = "";
00328   this->iimg_event_on_id_ = "";
00329   this->ipng_event_on_id_ = "";
00330   this->dimg_event_on_id_ = "";
00331   this->tf_robot_event_on_id_ = "";
00332   this->tf_pattern_event_on_id_ = "";
00333   this->tf_robot_inverse_event_on_id_ = "";
00334 }
00335 
00336 /*  [subscriber callbacks] */
00337 void 
00338 HandeyeLog::pcl2_sub_callback(const sensor_msgs::PointCloud2::ConstPtr& msg) 
00339 { 
00340   this->nh.getParam("/handeye_log/num_captures", this->num_captures_);
00341 
00342   // ROS_INFO("HandeyeLog::pcl2_sub_callback: New Message Received"); 
00343   if (pcl::getFieldIndex(*msg, "rgb")!=-1){
00344     pcl::fromROSMsg(*msg, this->pcl_xyzrgb_);
00345     this->pcl_type_ = 1;
00346   }else if ( (pcl::getFieldIndex(*msg, "range")!=-1) && (pcl::getFieldIndex(*msg, "intensity")!=-1)) {
00347     //pcl::fromROSMsg(*msg, this->pcl_xyzr_);
00348     this->pcl_xyzri_ = *msg;
00349     this->pcl_type_ = 2;
00350     //  this->xyzr_cb_.resize(this->num_captures_);
00351     // posar aquesta variable en Mutex ;)
00352     // this->xyzr_cb_.push_back(this->pcl_xyzr_);
00353     //this->cloudBuffer_.push_back(this->pcl_xyzri_);
00354   }else if (pcl::getFieldIndex(*msg, "range")!=-1){
00355     pcl::fromROSMsg(*msg, this->pcl_xyzr_);
00356     this->pcl_type_ = 3;
00357     //  this->xyzr_cb_.resize(this->num_captures_);
00358     // posar aquesta variable en Mutex ;)
00359     // this->xyzr_cb_.push_back(this->pcl_xyzr_);
00360     this->cloudBuffer_.push_back(this->pcl_xyzr_);
00361   }else{
00362     pcl::fromROSMsg(*msg, this->pcl_xyz_);
00363     this->pcl_type_ = 4;
00364   }
00365 }
00366 
00367 void 
00368 HandeyeLog::iimg_sub_callback(const sensor_msgs::Image::ConstPtr& msg)
00369 { 
00370   //TODO: It should work by just copying the ConstPtr to the original msg.
00371   // I am still wondering the reason why a copy has been coded :/
00372   this->intens_image_ = msg;
00373 
00374   //  ROS_INFO("HandeyeLog::iimg_sub_callback: New Message Received"); 
00375   try
00376     {
00377       if (sensor_msgs::image_encodings::isColor(msg->encoding))
00378             this->cv_ptr_ = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8);
00379       else {
00380 //          this->cv_ptr_ = cv_bridge::toCvCopy(msg, "8UC1");
00381         this->cv_ptr_ = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO8);
00382       }
00383     }
00384   catch (cv_bridge::Exception& e)
00385     {
00386       ROS_ERROR("cv_bridge exception: %s", e.what());
00387       return;
00388     }
00389   this->iimageBuffer_.push_back(this->cv_ptr_);
00390 }
00391 
00392 void 
00393 HandeyeLog::tf_robot_sub_callback(const geometry_msgs::PoseStamped::ConstPtr& msg){
00394   tf::Quaternion quat(msg->pose.orientation.x, msg->pose.orientation.y, msg->pose.orientation.z, msg->pose.orientation.w);
00395   tf::Vector3 pos(msg->pose.position.x, msg->pose.position.y, msg->pose.position.z);
00396   this->tf_robot_ = tf::Transform(quat, pos);
00397 }
00398 
00399 void
00400 HandeyeLog::tf_pattern_sub_callback(const geometry_msgs::PoseStamped::ConstPtr& msg){
00401   tf::Quaternion quat(msg->pose.orientation.x, msg->pose.orientation.y, msg->pose.orientation.z, msg->pose.orientation.w);
00402   tf::Vector3 pos(msg->pose.position.x, msg->pose.position.y, msg->pose.position.z);
00403   this->tf_pattern_ = tf::Transform(quat, pos);
00404 }
00405 
00406 bool
00407 HandeyeLog::srv_callback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
00408 {
00409   std::list<std::string> all_active_events;
00410   
00411   // ROS_INFO("HandeyeLog::srv_callback: New Service Request Received"); 
00412 
00413   this->nh.getParam("/handeye_log/flags/matlab_pcl2", this->flag_mpcl2);
00414   this->nh.getParam("/handeye_log/flags/img_depth", this->flag_dimg);
00415   this->nh.getParam("/handeye_log/flags/img_intensity", this->flag_iimg);
00416   this->nh.getParam("/handeye_log/flags/png_intensity", this->flag_ipng);
00417   this->nh.getParam("/handeye_log/flags/tf_robot", this->flag_tf_robot);
00418   this->nh.getParam("/handeye_log/flags/tf_pattern", this->flag_tf_pattern);
00419   this->nh.getParam("/handeye_log/flags/inverse", this->flag_tf_robot_inverse);
00420   
00421   if(this->flag_mpcl2){
00422     this->event_server->set_event(this->mpcl2_event_on_id_);
00423     all_active_events.push_back(this->mpcl2_event_off_id_);
00424   }
00425   
00426   if (this->flag_dimg){
00427     this->event_server->set_event(this->dimg_event_on_id_);
00428     all_active_events.push_back(this->dimg_event_off_id_);
00429   }
00430 
00431   if (this->flag_iimg){
00432     this->event_server->set_event(this->iimg_event_on_id_);
00433     all_active_events.push_back(this->iimg_event_off_id_);
00434   }
00435 
00436   if (this->flag_ipng){
00437     this->event_server->set_event(this->ipng_event_on_id_);
00438     all_active_events.push_back(this->ipng_event_off_id_);
00439   }
00440 
00441   if (this->flag_tf_robot){
00442     this->event_server->set_event(this->tf_robot_event_on_id_);
00443     all_active_events.push_back(this->tf_robot_event_off_id_);
00444   }
00445 
00446   if (this->flag_tf_pattern){
00447     this->event_server->set_event(this->tf_pattern_event_on_id_);
00448     all_active_events.push_back(this->tf_pattern_event_off_id_);
00449   }
00450 
00451   if(this->flag_tf_robot_inverse && !this->flag_tf_robot){
00452     this->event_server->set_event(this->tf_robot_inverse_event_on_id_);
00453     all_active_events.push_back(this->tf_robot_inverse_event_off_id_);
00454   }
00455 
00456   this->event_server->wait_all(all_active_events);
00457   this->thread_server->start_thread(this->mpcl2_thread_id_);
00458   this->thread_server->start_thread(this->iimg_thread_id_);
00459   this->thread_server->start_thread(this->ipng_thread_id_);
00460   this->thread_server->start_thread(this->dimg_thread_id_);
00461   this->thread_server->start_thread(this->tf_robot_thread_id_);
00462   this->thread_server->start_thread(this->tf_pattern_thread_id_);
00463   this->thread_server->start_thread(this->tf_robot_inverse_thread_id_);
00464         
00465   this->idx++;
00466   return true;
00467 }
00468 
00469 void *HandeyeLog::store_mpcl2_thread(void *param)
00470 {
00471   HandeyeLog *handeyeLog = (HandeyeLog *)param;
00472 
00473   // ROS_INFO("Thread Store MPCL2 started");
00474   // ROS_INFO("Waiting for Event Store MPCL2");
00475   std::list<std::string> event_list_store_mpcl2;
00476   event_list_store_mpcl2.push_back(handeyeLog->mpcl2_event_on_id_);
00477   event_list_store_mpcl2.push_back(handeyeLog->mpcl2_event_stop_id_);
00478   int eid = handeyeLog->event_server->wait_first(event_list_store_mpcl2);
00479   if (eid == 1)
00480     pthread_exit(NULL);
00481 
00482   // ROS_INFO("Event Store MPCL2 activated");
00483 
00484   // [xyzRGB matlab file name]
00485   std::stringstream matlab_file_fn;
00486   matlab_file_fn << "frameXYZRGB_"<< std::setw(2) << std::setfill('0') << handeyeLog->idx << ".dat";
00487 
00488   // [open xyzRGB matlab file]
00489   handeyeLog->matlab_file_.open(matlab_file_fn.str().c_str(), std::ios::out);
00490 
00491   // [store xyzRGB matlab file]
00492   if (handeyeLog->pcl_type_ == 1)
00493     load_xyzrgb_matlab_file(handeyeLog->pcl_xyzrgb_, handeyeLog->matlab_file_);
00494   else if (handeyeLog->pcl_type_ == 2)
00495     load_xyzri_matlab_file(handeyeLog->pcl_xyzri_, handeyeLog->matlab_file_);
00496   else if (handeyeLog->pcl_type_ == 3)
00497     load_xyzr_matlab_file(handeyeLog->pcl_xyzr_, handeyeLog->matlab_file_);
00498   else
00499     load_xyz_matlab_file(handeyeLog->pcl_xyz_, handeyeLog->matlab_file_);
00500 
00501   // [close range image file]
00502   handeyeLog->matlab_file_.close();
00503   ROS_INFO("Saved image %s", matlab_file_fn.str().c_str());
00504 
00505   handeyeLog->event_server->set_event(handeyeLog->mpcl2_event_off_id_);
00506 
00507   pthread_exit(NULL);
00508 }
00509 
00510 void *HandeyeLog::store_dimg_thread(void *param)
00511 {
00512   HandeyeLog *handeyeLog = (HandeyeLog *)param;
00513 
00514   // ROS_INFO("Thread Store DIMG started");
00515   // ROS_INFO("Waiting for Event Store DIMG");
00516   std::list<std::string> event_list_store_dimg;
00517   event_list_store_dimg.push_back(handeyeLog->dimg_event_on_id_);
00518   event_list_store_dimg.push_back(handeyeLog->dimg_event_stop_id_);
00519   int eid = handeyeLog->event_server->wait_first(event_list_store_dimg);
00520   if (eid == 1)
00521     pthread_exit(NULL);
00522 
00523   // ROS_INFO("Event Store DIMG activated");
00524 
00525   // [range image file name]
00526   for (int aa = 0; aa < handeyeLog->num_captures_; aa++){
00527     std::stringstream img_depth_fn;
00528     // img_depth_fn << "frameTOFDepth_" << std::setw(2) << std::setfill('0') << handeyeLog->idx << "_" << std::setw(2) << std::setfill('0') << aa << ".txt";
00529     img_depth_fn << "frameTOFDepth" << handeyeLog->idx << "_" << aa << ".txt";
00530 
00531     // [open range image file]
00532     handeyeLog->img_depth_.open(img_depth_fn.str().c_str(), std::ios::out);
00533 
00534     // [store range image file]
00535     
00536     if (handeyeLog->pcl_type_ == 1)
00537       load_xyzrgb_img_depth(handeyeLog->pcl_xyzrgb_, handeyeLog->img_depth_);
00538     else if (handeyeLog->pcl_type_ == 2)
00539       load_xyzri_img_depth(handeyeLog->pcl_xyzri_, handeyeLog->img_depth_);
00540     else if (handeyeLog->pcl_type_ == 3)
00541       load_xyzr_img_depth(handeyeLog->pcl_xyzr_, handeyeLog->img_depth_);
00542     else
00543       load_xyz_img_depth(handeyeLog->pcl_xyz_, handeyeLog->img_depth_);
00544 
00545     // [close range image file]
00546     handeyeLog->img_depth_.close();
00547     ROS_INFO("Saved image %s", img_depth_fn.str().c_str());
00548   }
00549 
00550   handeyeLog->event_server->set_event(handeyeLog->dimg_event_off_id_);
00551   
00552   pthread_exit(NULL);
00553 }
00554 
00555 void *HandeyeLog::store_iimg_thread(void *param)
00556 {
00557   HandeyeLog *handeyeLog = (HandeyeLog *)param;
00558 
00559   // ROS_INFO("Thread Store IIMG started");
00560   // ROS_INFO("Waiting for Event Store IIMG");
00561   std::list<std::string> event_list_store_iimg;
00562   event_list_store_iimg.push_back(handeyeLog->iimg_event_on_id_);
00563   event_list_store_iimg.push_back(handeyeLog->iimg_event_stop_id_);
00564   int eid =  handeyeLog->event_server->wait_first(event_list_store_iimg);
00565   if (eid == 1)
00566     pthread_exit(NULL);
00567 
00568   // ROS_INFO("Event Store IIMG activated");
00569   
00570   // [amplitude image file name]
00571   for (int aa = 0; aa < handeyeLog->num_captures_; aa++){
00572     std::stringstream img_intensity_fn;
00573     img_intensity_fn << "frameTOFAmplitude" << handeyeLog->idx << "_" << aa << ".txt";
00574     
00575     // [open amplitude image file]
00576     handeyeLog->img_intensity_.open(img_intensity_fn.str().c_str(), std::ios::out);
00577     
00578     // [store amplitude image file]
00579 
00580     if (handeyeLog->pcl_type_ == 2) {
00581         store_xyzri_img_depth(handeyeLog->pcl_xyzri_, handeyeLog->img_intensity_);
00582     } else {
00583         // cv::Mat_<uchar>::iterator pt_iter = handeyeLog->cv_ptr_->image.begin<uchar>();
00584         cv::Mat_<uchar>::iterator pt_iter = handeyeLog->iimageBuffer_[aa]->image.begin<uchar>();
00585         for (int rr = 0; rr < handeyeLog->cv_ptr_->image.rows; ++rr) {
00586             for (int cc = 0; cc < handeyeLog->cv_ptr_->image.cols; ++cc , ++pt_iter) {
00587                 handeyeLog->img_intensity_ << rr << " " << cc << " " <<  (float)*pt_iter << std::endl; // handeyeLog->cv_ptr_->image.at<float>(rr,cc) << std::endl;
00588             }
00589         }
00590     }
00591 
00592     // [close amplitude image file]
00593     handeyeLog->img_intensity_.close();
00594     ROS_INFO("Saved image %s", img_intensity_fn.str().c_str());
00595   }
00596   handeyeLog->event_server->set_event(handeyeLog->iimg_event_off_id_);
00597 
00598   pthread_exit(NULL);
00599 }
00600 
00601 void *HandeyeLog::store_ipng_thread(void *param)
00602 {
00603   HandeyeLog *handeyeLog = (HandeyeLog *)param;
00604 
00605   // ROS_INFO("Thread Store IPNG started");
00606   // ROS_INFO("Waiting for Event Store IPNG");
00607   std::list<std::string> event_list_store_ipng;
00608   event_list_store_ipng.push_back(handeyeLog->ipng_event_on_id_);
00609   event_list_store_ipng.push_back(handeyeLog->ipng_event_stop_id_);
00610   int eid = handeyeLog->event_server->wait_first(event_list_store_ipng);
00611   if (eid == 1)
00612     pthread_exit(NULL);
00613 
00614   // ROS_INFO("Event Store IPNG activated");
00615   
00616   // [stores PNG file]
00617   cv_bridge::CvImagePtr img_ptr = cv_bridge::toCvCopy(*handeyeLog->intens_image_, "bgr8");
00618   
00619   if (img_ptr) {
00620     std::stringstream filename;
00621     filename << "frame_" << handeyeLog->idx << ".png";
00622     imwrite(filename.str().c_str(), img_ptr->image);
00623     ROS_INFO("Saved image %s", filename.str().c_str());
00624   } else {
00625     ROS_WARN("Couldn't save image, no data!");
00626   }
00627 
00628 //  sensor_msgs::CvBridge g_bridge;
00629 //  if (g_bridge.fromImage(*handeyeLog->intens_image_, "bgr8")) {
00630 //    IplImage *image = g_bridge.toIpl();
00631 //    if (image) {
00632 //      std::stringstream filename;
00633 //      //filename << "frame_" << std::setw(2) << std::setfill('0') << handeyeLog->idx << ".png";
00634 //      filename << "frame_" << handeyeLog->idx << ".png";
00635 //      cvSaveImage(filename.str().c_str(), image);
00636 //      ROS_INFO("Saved image %s", filename.str().c_str());
00637 //    } else {
00638 //      ROS_WARN("Couldn't save image, no data!");
00639 //    }
00640 //  } else {
00641 //    ROS_ERROR("Unable to convert %s image to bgr8", handeyeLog->intens_image_->encoding.c_str());
00642 //  }
00643 
00644   handeyeLog->event_server->set_event(handeyeLog->ipng_event_off_id_);
00645 
00646   pthread_exit(NULL);
00647 }
00648 
00649 
00650 void *HandeyeLog::store_tf_robot_thread(void *param)
00651 {
00652   HandeyeLog *handeyeLog = (HandeyeLog *)param;
00653 
00654   // ROS_INFO("Thread Store TF_ROBOT started");
00655   // ROS_INFO("Waiting for Event Store TF_ROBOT");
00656   std::list<std::string> event_list_store_tf_robot;
00657   event_list_store_tf_robot.push_back(handeyeLog->tf_robot_event_on_id_);
00658   event_list_store_tf_robot.push_back(handeyeLog->tf_robot_event_stop_id_);
00659   int eid = handeyeLog->event_server->wait_first(event_list_store_tf_robot);
00660   if (eid == 1)
00661     pthread_exit(NULL);
00662 
00663   // ROS_INFO("Event Store TF_ROBOT activated");
00664   
00665     // [tf_robot file name]
00666     std::stringstream tf_robot_fn;
00667     //tf_robot_fn << "frame_" << std::setw(2) << std::setfill('0') << handeyeLog->idx << ".coords";
00668     tf_robot_fn << "frame_" << handeyeLog->idx << ".coords";
00669   
00670     // [open tf_robot file]
00671     handeyeLog->tf_robot_file_.open(tf_robot_fn.str().c_str(), std::ios::out);
00672 
00673     // [store tf_robot file]
00674     handeyeLog->tf_robot_file_ << handeyeLog->tf_robot_.getBasis()[0][0] << " " << handeyeLog->tf_robot_.getBasis()[0][1] << " " << handeyeLog->tf_robot_.getBasis()[0][2] << " " << handeyeLog->tf_robot_.getOrigin()[0]*1000 << " "
00675                << handeyeLog->tf_robot_.getBasis()[1][0] << " " << handeyeLog->tf_robot_.getBasis()[1][1] << " " << handeyeLog->tf_robot_.getBasis()[1][2] << " " << handeyeLog->tf_robot_.getOrigin()[1]*1000 << " "
00676                << handeyeLog->tf_robot_.getBasis()[2][0] << " " << handeyeLog->tf_robot_.getBasis()[2][1] << " " << handeyeLog->tf_robot_.getBasis()[2][2] << " " << handeyeLog->tf_robot_.getOrigin()[2]*1000 << std::endl;
00677 
00678     // [close range image file]
00679     handeyeLog->tf_robot_file_.close();
00680     ROS_INFO("Saved pose %s", tf_robot_fn.str().c_str());
00681 
00682   handeyeLog->event_server->set_event(handeyeLog->tf_robot_event_off_id_);
00683 
00684   pthread_exit(NULL);
00685 }
00686 
00687 void *HandeyeLog::store_tf_pattern_thread(void *param)
00688 {
00689   HandeyeLog *handeyeLog = (HandeyeLog *)param;
00690 
00691   // ROS_INFO("Thread Store TF_PATTERN started");
00692   // ROS_INFO("Waiting for Event Store TF_PATTERN");
00693   std::list<std::string> event_list_store_tf_pattern;
00694   event_list_store_tf_pattern.push_back(handeyeLog->tf_pattern_event_on_id_);
00695   event_list_store_tf_pattern.push_back(handeyeLog->tf_pattern_event_stop_id_);
00696   int eid = handeyeLog->event_server->wait_first(event_list_store_tf_pattern);
00697   if (eid == 1)
00698     pthread_exit(NULL);
00699 
00700   // ROS_INFO("Event Store TF_PATTERN activated");
00701 
00702     // [tf_pattern file name]
00703     std::stringstream tf_pattern_fn;
00704     tf_pattern_fn << "framePattern_" << std::setw(2) << std::setfill('0') << handeyeLog->idx << ".m";
00705   
00706     // [open tf_pattern file]
00707     handeyeLog->tf_pattern_file_.open(tf_pattern_fn.str().c_str(), std::ios::out);
00708 
00709     // [store tf_pattern file]
00710     handeyeLog->tf_pattern_file_ << handeyeLog->tf_pattern_.getBasis()[0][0] << " " << handeyeLog->tf_pattern_.getBasis()[0][1] << " " << handeyeLog->tf_pattern_.getBasis()[0][2] << " " << handeyeLog->tf_pattern_.getOrigin()[0]*1000 << " "
00711                  << handeyeLog->tf_pattern_.getBasis()[1][0] << " " << handeyeLog->tf_pattern_.getBasis()[1][1] << " " << handeyeLog->tf_pattern_.getBasis()[1][2] << " " << handeyeLog->tf_pattern_.getOrigin()[1]*1000 << " "
00712                  << handeyeLog->tf_pattern_.getBasis()[2][0] << " " << handeyeLog->tf_pattern_.getBasis()[2][1] << " " << handeyeLog->tf_pattern_.getBasis()[2][2] << " " << handeyeLog->tf_pattern_.getOrigin()[2]*1000 << std::endl;
00713 
00714     // [close range image file]
00715     handeyeLog->tf_pattern_file_.close();
00716 ROS_INFO("Saved pose %s", tf_pattern_fn.str().c_str());
00717 
00718   handeyeLog->event_server->set_event(handeyeLog->tf_pattern_event_off_id_);
00719 
00720   pthread_exit(NULL);
00721 }
00722 
00723 void *HandeyeLog::store_tf_robot_inverse_thread(void *param)
00724 {
00725   HandeyeLog *handeyeLog = (HandeyeLog *)param;
00726 
00727   // ROS_INFO("Thread Store TF_ROBOT_INVERSE started");
00728   // ROS_INFO("Waiting for Event Store TF_ROBOT_INVERSE");
00729   std::list<std::string> event_list_store_tf_robot_inverse;
00730   event_list_store_tf_robot_inverse.push_back(handeyeLog->tf_robot_inverse_event_on_id_);
00731   event_list_store_tf_robot_inverse.push_back(handeyeLog->tf_robot_inverse_event_stop_id_);
00732   int eid = handeyeLog->event_server->wait_first(event_list_store_tf_robot_inverse);
00733   if (eid == 1)
00734     pthread_exit(NULL);
00735 
00736   // ROS_INFO("Event Store TF_ROBOT_INVERSE activated");
00737 
00738   // [tf_robot file name]
00739   std::stringstream tf_robot_fn;
00740   //tf_robot_fn << "frame_" << std::setw(2) << std::setfill('0') << handeyeLog->idx << ".coords";
00741   tf_robot_fn << "frame_" << handeyeLog->idx << ".coords";
00742   
00743   // [open tf_robot file]
00744   handeyeLog->tf_robot_file_.open(tf_robot_fn.str().c_str(), std::ios::out);
00745 
00746   // [store tf_robot file]
00747   handeyeLog->tf_robot_file_ << handeyeLog->tf_robot_.inverse().getBasis()[0][0] << " " << handeyeLog->tf_robot_.inverse().getBasis()[0][1] << " " << handeyeLog->tf_robot_.inverse().getBasis()[0][2] << " " << handeyeLog->tf_robot_.inverse().getOrigin()[0]*1000 << " "
00748                        << handeyeLog->tf_robot_.inverse().getBasis()[1][0] << " " << handeyeLog->tf_robot_.inverse().getBasis()[1][1] << " " << handeyeLog->tf_robot_.inverse().getBasis()[1][2] << " " << handeyeLog->tf_robot_.inverse().getOrigin()[1]*1000 << " "
00749                        << handeyeLog->tf_robot_.inverse().getBasis()[2][0] << " " << handeyeLog->tf_robot_.inverse().getBasis()[2][1] << " " << handeyeLog->tf_robot_.inverse().getBasis()[2][2] << " " << handeyeLog->tf_robot_.inverse().getOrigin()[2]*1000 << std::endl;
00750 
00751   // [close range image file]
00752   handeyeLog->tf_robot_file_.close();
00753   ROS_INFO("Saved (inverse) pose %s", tf_robot_fn.str().c_str());
00754 
00755   handeyeLog->event_server->set_event(handeyeLog->tf_robot_inverse_event_off_id_);
00756 
00757   pthread_exit(NULL);
00758 }
00759 
00760 int
00761 main(int argc, char** argv)
00762 {
00763     ros::init(argc, argv, "store_rangeImage");
00764     HandeyeLog handeye_log;
00765     ros::spin();
00766     return 0;
00767 }


handeye_log
Author(s): Sergi Foix
autogenerated on Fri Dec 6 2013 21:18:26