my_database_publ_alg_node.cpp
Go to the documentation of this file.
00001 #include "my_database_publ_alg_node.h"
00002 
00003 MyDatabasePublAlgNode::MyDatabasePublAlgNode(void) :
00004         algorithm_base::IriBaseAlgorithm<MyDatabasePublAlgorithm>() ,
00005         it_(this->public_node_handle_),
00006         iit_database(0),
00007     tf_listener_(ros::Duration(30.f))
00008 {
00009   //init class attributes if necessary
00010   this->loop_rate_ = 10;//in [Hz]
00011 
00012   // [init publishers]
00013   this->goal_publisher_ = this->public_node_handle_.advertise<geometry_msgs::PoseArray>("goal", 100);
00014         image_pub_ = it_.advertiseCamera("image_raw", 1);
00015   this->scan_publisher_ = this->public_node_handle_.advertise<sensor_msgs::LaserScan>("scan", 100);
00016   this->scan_proc_publisher_ = this->public_node_handle_.advertise<sensor_msgs::LaserScan>("scan_proc", 100);
00017   // [init subscribers]
00018   
00019   // [init services]
00020   
00021   // [init clients]
00022   
00023   // [init action servers]
00024   
00025   // [init action clients]
00026 
00027   init_sensor_info();
00028 }
00029 
00030 MyDatabasePublAlgNode::~MyDatabasePublAlgNode(void)
00031 {
00032   // [free dynamic memory]
00033 }
00034 
00035 void MyDatabasePublAlgNode::mainNodeThread(void)
00036 {
00037   // [fill msg structures]
00038   //this->PoseArray_msg_.data = my_var;
00039   //this->CameraInfo_msg.data = my_var;
00040         if (iit_database < 2157) //database length
00041                 update_msgs();  
00042   // [fill srv structure and make request to the server]
00043   
00044   // [fill action structure and make request to the action server]
00045 
00046   // [publish messages]
00047   this->goal_publisher_.publish(this->PoseArray_msg_);
00048         sensor_msgs::Image image_;
00049         this->img_msg_.toImageMsg( image_ );
00050         this->image_pub_.publish( image_,  this->CameraInfo_msg_ );
00051         this->scan_publisher_.publish(this->LaserScan_msg_);
00052         this->scan_proc_publisher_.publish(this->LaserScan_proc_msg_);
00053 }
00054 
00055 void MyDatabasePublAlgNode::init_sensor_info()
00056 {
00057         //Image
00058         img_msg_.header.frame_id = "/camera";
00059 
00060         //      Camera Info -----------------------------------------------------
00061         CameraInfo_msg_.height = 384;
00062         CameraInfo_msg_.width = 512;
00063         CameraInfo_msg_.distortion_model = "plumb_bob";
00064 //      kc << -0.202358, 0.053583, 0.000246051, -0.00136075, 0;
00065         CameraInfo_msg_.D.clear();
00066         CameraInfo_msg_.D.push_back(-0.202358);
00067         CameraInfo_msg_.D.push_back( 0.053583);
00068         CameraInfo_msg_.D.push_back( 0.000246051);
00069         CameraInfo_msg_.D.push_back( -0.00136075);
00070         CameraInfo_msg_.D.push_back( 0);
00071 
00072 //       K << 1324.17, 0, 518.221,
00073 //              0, 1323.21, 393.857,
00074 //              0, 0, 1;
00075         CameraInfo_msg_.K[0] =  1324.17;
00076         CameraInfo_msg_.K[2] = 518.221;
00077         CameraInfo_msg_.K[4] =  1323.21;
00078         CameraInfo_msg_.K[5] = 393.857;
00079         CameraInfo_msg_.K[8] = 1;
00080 // float64[9] CameraInfo_msg_.R
00081         CameraInfo_msg_.R[0] =  1.0;
00082         CameraInfo_msg_.R[4] =  1;
00083         CameraInfo_msg_.R[8] =  1.0;
00084 
00085 // float64[12] CameraInfo_msg_.P := K *[R | t]
00086         CameraInfo_msg_.P[0] =  1324.17*0.5;
00087         CameraInfo_msg_.P[2] = 518.221*0.5;
00088         CameraInfo_msg_.P[5] =  1323.21*0.5;
00089         CameraInfo_msg_.P[6] = 393.857*0.5;
00090         CameraInfo_msg_.P[10] = 1;
00091 
00092         CameraInfo_msg_.header.frame_id = "/camera";
00093     cam_model_.fromCameraInfo(CameraInfo_msg_);
00094 
00095 
00096     //Laser params
00097         LaserScan_msg_.header.frame_id = "/laser";
00098         LaserScan_msg_.angle_min = 0.69813; //40 deg
00099         LaserScan_msg_.angle_max = 2.4478; //100+40 deg
00100         LaserScan_msg_.angle_increment = 0.00436332309619; //0.25º
00101         LaserScan_msg_.time_increment = 0.0000173611115315; //?
00102         LaserScan_msg_.scan_time = 0.0250000003725; //?
00103         LaserScan_msg_.range_min = 0.0230000000447;
00104         LaserScan_msg_.range_max = 60.0;
00105 
00106     //Laser cleaned params
00107         LaserScan_proc_msg_.header.frame_id = "/laser";
00108         LaserScan_proc_msg_.angle_min = 1.2086; //69.25 deg
00109         LaserScan_proc_msg_.angle_max = 1.9635; //112.5
00110         LaserScan_proc_msg_.angle_increment = 0.00436332309619; //0.25º
00111         LaserScan_proc_msg_.time_increment = 0.0000173611115315; //?
00112         LaserScan_proc_msg_.scan_time = 0.0250000003725; //?
00113         LaserScan_proc_msg_.range_min = 0.0230000000447;
00114         LaserScan_proc_msg_.range_max = 60.0;
00115 
00116 
00117         //annotations .txt
00118         std::string homepath = getenv("HOME");
00119         std::string fileString = homepath + "/Desktop/laser_vision_pr/annotations/annotations_test.txt";
00120         //std::ifstream infile;
00121         //infile.open (fileString.c_str() , ifstream::in)
00122         int num_det, x, y, w, h, cont;
00123         char c[256];
00124         std::vector<cv::Rect> instant_detections;
00125         //infile.getline (c,256);
00126         //while (infile.good()){
00127                 //infile.getline (c,256);
00128         //}
00129         //infile.close();
00130         
00131         FILE * file_out;
00132         file_out = fopen (  fileString.c_str() , "r");
00133         fscanf(file_out, "%s " , c);
00134         //while(  !feof(file_out) ){
00135         for (unsigned int j = 0; j<2157; j++){
00136                 fscanf (file_out, "%d", &num_det);
00137                 instant_detections.clear();
00138                 //ROS_INFO("LASER SCAN: %d ", j);
00139                 for( int i =0 ; i < num_det; i++){
00140                         fscanf(file_out, " %d %d %d %d", &x , &y, &w, &h);
00141                         instant_detections.push_back( cv::Rect(x/2,y/2,w/2,h/2) );
00142                 }
00143                 box_detections.push_back( instant_detections );
00144                 
00145                 fscanf(file_out, "\n%s " , c);
00146                 cont++;
00147         }
00148         fclose(file_out);
00149 
00150 }
00151 
00152 void MyDatabasePublAlgNode::update_msgs()
00153 {
00154         cv::Mat outIm;
00155 
00156     ros::Time now = ros::Time::now();
00157         iit_database ++;
00158         //      Header
00159         CameraInfo_msg_.header.seq   = iit_database;
00160         CameraInfo_msg_.header.stamp = now;
00161         img_msg_.header.seq          = iit_database;
00162         img_msg_.header.stamp        = now;
00163         LaserScan_msg_.header.seq    = iit_database;
00164         LaserScan_msg_.header.stamp  = now;
00165         LaserScan_proc_msg_.header.seq    = iit_database;
00166         LaserScan_proc_msg_.header.stamp  = now;
00167 
00168         //Image --------------------------------------------------------
00169         std::string homepath = getenv("HOME");
00170         std::string fileString = homepath + "/Desktop/laser_vision_pr/test/camera/list.lst";
00171         FILE * file_out;
00172         file_out = fopen (  fileString.c_str() , "r");
00173         fseek(file_out , 14*(iit_database -1) , SEEK_SET );
00174         char file_name[20];
00175         fscanf(file_out, "%s\n" , file_name); 
00176         fclose(file_out);
00177         std::string string_file_name = file_name;
00178 
00179         fileString = homepath + "/Desktop/laser_vision_pr/test/camera/" + string_file_name; //list.lst -> listed all file names...
00180         outIm = cv::imread( fileString.c_str(), 1 );
00181     img_msg_.encoding = "bgr8";
00182     img_msg_.image    = outIm;
00183         
00184         //Laser Scan --------------------------------------------------
00185         fileString = homepath + "/Desktop/laser_vision_pr/test/laser/list.lst";
00186         file_out = fopen (  fileString.c_str() , "r");
00187         fseek(file_out , 13*(iit_database -1) , SEEK_SET );
00188         fscanf(file_out, "%s\n" , file_name); 
00189         fclose(file_out);
00190         string_file_name = file_name;
00191         fileString = homepath + "/Desktop/laser_vision_pr/test/laser/" + string_file_name;
00192         file_out = fopen (  fileString.c_str() , "r");  
00193         
00194 
00195         float f;
00196         LaserScan_msg_.ranges.clear();  
00197         while (  !feof(file_out) ) {
00198                 fscanf (file_out, "%f ", &f);
00199                 LaserScan_msg_.ranges.push_back( f/100 );
00200         }
00201         //for(unsigned int i = 2 ; i< 220 ; i++)
00202         //LaserScan_msg_.ranges[200]=5.0;
00203         fclose(file_out );
00204 
00205         //processed laser scans appearing in the image
00206         LaserScan_proc_msg_.ranges.clear();
00207         for(unsigned int i = 116; i<290 ;i++)
00208                 LaserScan_proc_msg_.ranges.push_back( LaserScan_msg_.ranges[i] );
00209 
00210 
00211         //cada iteracio
00212         img_dect det;
00213         std::vector<img_dect> img_detections;
00214         for ( unsigned int i = 0; i < box_detections[iit_database-1].size() ;  i++){
00215                 det.id = i;
00216                 det.Bbox = box_detections[iit_database-1][i];
00217                 det.score = 1.0f;
00218                 img_detections.push_back(det);
00219         }
00220 
00221         //ROS_INFO("box_detections: %d ",img_detections.size());
00222 
00223 
00224         std::vector<point2D3D> laserScans;
00225         tf::StampedTransform laser_to_camera_transf;
00226 
00227         try
00228         {
00229                 ros::Time acquisition_time = LaserScan_msg_.header.stamp;
00230                 ros::Duration timeout(1.0f / 30.f);
00231 
00232                 // wait for transform from laser to camera 
00233                 tf_listener_.waitForTransform(cam_model_.tfFrame(), LaserScan_msg_.header.frame_id,
00234                                                     acquisition_time, timeout);
00235 
00236                 tf_listener_.lookupTransform(cam_model_.tfFrame(), LaserScan_msg_.header.frame_id,
00237                                                    acquisition_time, laser_to_camera_transf);
00238 
00239 
00240                 //ROS_INFO("entering for : size = %d" , LaserScan_msg_.ranges.size());
00241                 for(unsigned int ii=0; ii < LaserScan_msg_.ranges.size(); ii++)
00242                 {
00243                         //read laser scan
00244                         tf::Point laser_point(LaserScan_msg_.ranges[ii]*cos(LaserScan_msg_.angle_min+LaserScan_msg_.angle_increment*ii),
00245                                                           LaserScan_msg_.ranges[ii]*sin(LaserScan_msg_.angle_min+LaserScan_msg_.angle_increment*ii),
00246                                                           0.f);
00247                         //ROS_INFO("point x = %f , y= %f, z= %f", laser_point.x(), laser_point.y(), laser_point.z() );
00248                         //transform laser 3d point to camera coordinates
00249                         tf::Point cam_point(laser_to_camera_transf * laser_point);
00250                         //ROS_INFO("camera point x = %f , y= %f, z= %f", cam_point.x(), cam_point.y(), cam_point.z() );
00251                         cv::Point3d cv_cam_point(cam_point.x(), cam_point.y(), cam_point.z());
00252                         //ROS_INFO("cv camera point x = %f , y= %f, z= %f", cv_cam_point.x, cv_cam_point.y, cv_cam_point.z );
00253                         // transform laser 3d camera point to image plane
00254                         cv::Point2d image_point = cam_model_.project3dToPixel(cv_cam_point);
00255                         //ROS_INFO("cv camera 2d point x = %f , y= %f", image_point.x, image_point.y );
00256                         //update vector
00257                         point2D3D p;
00258                         p.p3D.x = LaserScan_msg_.ranges[ii]*cos(LaserScan_msg_.angle_min+LaserScan_msg_.angle_increment*ii);
00259                         p.p3D.y = LaserScan_msg_.ranges[ii]*sin(LaserScan_msg_.angle_min+LaserScan_msg_.angle_increment*ii);
00260                         p.p3D.z = 0.0f;
00261                         p.p2D.x = image_point.x;
00262                         p.p2D.y = image_point.y;
00263                         laserScans.push_back( p );
00264                         //ROS_INFO("laser points x = %d , y= %d, z= %d", p.p3D.x, p.p3D.y, p.p3D.z );
00265                         //ROS_INFO("tf adq %d" , ii);
00266                 }
00267                 std::vector<las_dect> cam_laser_dets;
00268                 cam_detector_.camtoLaser(img_detections, laserScans, cam_laser_dets);
00269                 //ROS_INFO("camera laser dets size = %d",  cam_laser_dets.size() );
00270                 //pose array msg
00271                 PoseArray_msg_.header.seq   = iit_database;
00272                 PoseArray_msg_.header.stamp = now;
00273                 PoseArray_msg_.header.frame_id = "/laser";
00274                 geometry_msgs::Pose pp;
00275                 PoseArray_msg_.poses.clear();
00276                 for( unsigned int i = 0; i < cam_laser_dets.size() ; i++){
00277                         pp.position.x = cam_laser_dets[i].point.p3D.x;
00278                         pp.position.y = cam_laser_dets[i].point.p3D.y;
00279                         pp.position.z = cam_laser_dets[i].point.p3D.z;
00280                         pp.orientation.z = 1.0f;
00281                         PoseArray_msg_.poses.push_back(pp);
00282                 }
00283                 //void drawpeopledetection(const cv::Mat& img, cv::Mat & outIm, const std::vector<cv::Rect>& found_filtered);
00284                 //cam_detector_.drawpeopledetection(outIm, outIm, box_detections[iit_database-1] );
00285         //cam_detector_.drawalllaserdetections(outIm, outIm, laserScans);
00286                 //img_msg_.image    = outIm;
00287         }
00288         catch (tf::TransformException& ex)
00289         {
00290                 ROS_WARN("TF exception:\n%s", ex.what());
00291         }
00292 
00293 }
00294 
00295 /*  [subscriber callbacks] */
00296 
00297 /*  [service callbacks] */
00298 
00299 /*  [action callbacks] */
00300 
00301 /*  [action requests] */
00302 
00303 void MyDatabasePublAlgNode::node_config_update(Config &config, uint32_t level)
00304 {
00305   ROS_DEBUG("MyDatabasePublAlgNode::node_config_update");
00306   this->alg_.lock();
00307         iit_database = config.sequence_iterator;
00308         loop_rate_   = config.frame_rate;
00309   this->alg_.unlock();
00310 }
00311 
00312 void MyDatabasePublAlgNode::addNodeDiagnostics(void)
00313 {
00314 }
00315 
00316 /* main function */
00317 int main(int argc,char *argv[])
00318 {
00319   return algorithm_base::main<MyDatabasePublAlgNode>(argc, argv, "my_database_publ_alg_node");
00320 }


iri_my_database_publ
Author(s): Gonzalo Ferrer
autogenerated on Fri Dec 6 2013 23:31:04