darwin_find_eggs_alg_node.cpp
Go to the documentation of this file.
00001 #include "darwin_find_eggs_alg_node.h"
00002 #include <pcl/filters/passthrough.h>
00003 #include <tf/transform_broadcaster.h>
00004 
00005 DarwinFindEggsAlgNode::DarwinFindEggsAlgNode(void) :
00006   algorithm_base::IriBaseAlgorithm<DarwinFindEggsAlgorithm>()
00007 {
00008   //init class attributes if necessary
00009   //this->loop_rate_ = 2;//in [Hz]
00010 
00011   // [init publishers]
00012   this->output_pcl2_publisher_ = this->public_node_handle_.advertise<sensor_msgs::PointCloud2>("output_pcl2", 1);
00013   
00014   // [init subscribers]
00015   this->input_pcl2_subscriber_ = this->public_node_handle_.subscribe("input_pcl2", 1, &DarwinFindEggsAlgNode::input_pcl2_callback, this);
00016   
00017   // [init services]
00018   
00019   // [init clients]
00020   
00021   // [init action servers]
00022   
00023   // [init action clients]
00024 }
00025 
00026 DarwinFindEggsAlgNode::~DarwinFindEggsAlgNode(void)
00027 {
00028   // [free dynamic memory]
00029 }
00030 
00031 void DarwinFindEggsAlgNode::mainNodeThread(void)
00032 {
00033   // [fill msg structures]
00034   //this->PointCloud2_msg_.data = my_var;
00035   
00036   // [fill srv structure and make request to the server]
00037   
00038   // [fill action structure and make request to the action server]
00039 
00040   // [publish messages]
00041 //  this->output_pcl2_publisher_.publish(this->PointCloud2_msg_);
00042 }
00043 
00044 /*  [subscriber callbacks] */
00045 void DarwinFindEggsAlgNode::input_pcl2_callback(const sensor_msgs::PointCloud2::ConstPtr& msg) 
00046 { 
00047   //ROS_INFO("DarwinFindEggsAlgNode::input_pcl2_callback: New Message Received"); 
00048 
00049         pcl::PointCloud<pcl::PointXYZRGB> cloud;
00050         pcl::fromROSMsg (*msg, cloud);
00051 
00052 
00053         pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_segmented (new pcl::PointCloud<pcl::PointXYZRGB>);
00054         pcl::PointXYZ *result=new pcl::PointXYZ[2];
00055   
00056   int pnumber = (int)cloud.size ();
00057 /*  
00058   // Create the filtering object
00059   pcl::PassThrough<pcl::PointXYZRGB> pass;
00060   pass.setInputCloud (cloud.makeShared());
00061   pass.setFilterFieldName ("rgb");
00062   pass.setFilterLimits (200.0, 260.0);
00063   //pass.setFilterLimitsNegative (true);
00064   pass.filter (*cloud_segmented); 
00065   */
00066 double mean_x=0.0, mean_y=0.0, mean_z=0.0;
00067 int acum=0;
00068   pcl::copyPointCloud<pcl::PointXYZRGB>(cloud, *cloud_segmented);
00069   for (int point_id = 0; point_id < pnumber; ++point_id)
00070   {
00071     //intensity comes codified in the rgb field
00072     uint32_t rgb = *reinterpret_cast<int*>(&cloud.points[point_id].rgb);
00073     uint8_t r = (rgb >> 16) & 0x0000ff;
00074     //uint8_t g = (rgb >> 8) & 0x0000ff;
00075     //uint8_t b = (rgb) & 0x0000ff;
00076     
00077   //  ROS_INFO("DarwinFindEggsAlgNode:: RGB %d R%d  G%d B%d",rgb,r,g,b); 
00078     if ((r >200 ) && (cloud.points[point_id].z <0.5)) {
00079       //get the point
00080       mean_x+=cloud.points[point_id].x;
00081       mean_y+=cloud.points[point_id].y;
00082       mean_z+=cloud.points[point_id].z;
00083       acum++;
00084       //mark in red the point in the pointcloud
00085       // pack r/g/b into rgb
00086     uint8_t r = 255, g = 0, b = 0; // Red color
00087     uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b);
00088     cloud_segmented->points[point_id].rgb = *reinterpret_cast<float*>(&rgb);
00089     }
00090   }    
00091   //publish the transformation in the TF
00092   static tf::TransformBroadcaster br;
00093   tf::Transform transform;
00094   transform.setOrigin (tf::Vector3 (mean_x/acum,mean_y/acum,mean_z/acum));
00095   transform.setRotation (tf::Quaternion (0,0,0,1));
00096   br.sendTransform (tf::StampedTransform (transform, ros::Time::now (), "/camboard_frame", "target_frame"));
00097   
00098   /*
00099   static tf::TransformBroadcaster br;
00100   tf::Transform transform;
00101   transform.setOrigin( tf::Vector3(centroid(0), centroid(1), centroid(2)) );
00102   transform.setRotation( tf::Quaternion(pose_c.x(), pose_c.y(), pose_c.z(), pose_c.w()) );
00103   br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "/camboard_frame", "centroid"));
00104   */
00105   
00106 /*
00107  *      // Segmentation
00108         pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
00109         pcl::PointIndices::Ptr inliers (new pcl::PointIndices ());
00110         // Create the segmentation object
00111         pcl::SACSegmentation<pcl::PointXYZ> seg;
00112         // Optional
00113         seg.setOptimizeCoefficients (true);
00114         // Mandatory
00115         seg.setModelType (pcl::SACMODEL_PLANE);
00116         seg.setMethodType (pcl::SAC_RANSAC);
00117         seg.setDistanceThreshold (0.02); // 1cm?
00118 
00119         // segment
00120         seg.setInputCloud (cloud.makeShared());
00121         seg.segment (*inliers, *coefficients);
00122         
00123 
00124     // Extract the planar inliers from the input cloud
00125     pcl::ExtractIndices<pcl::PointXYZ> extract;
00126     extract.setInputCloud (cloud.makeShared());
00127     extract.setIndices (inliers);
00128 
00129     // Remove the planar inliers, extract the rest
00130     extract.setNegative (true);
00131     extract.filter (*cloud_segmented); //*
00132 
00133         // copies all inliers of the model computed to another PointCloud
00134 //      pcl::copyPointCloud<pcl::PointXYZ>(cloud, *inliers, *cloud_segmented);
00135 */
00136 
00137 
00138   if (output_pcl2_publisher_.getNumSubscribers () > 0) {
00139         pcl::toROSMsg(*cloud_segmented, this->PointCloud2_msg_);
00140         this->output_pcl2_publisher_.publish(this->PointCloud2_msg_);
00141   }
00142 
00143 
00144 
00145 
00146 
00147   //use appropiate mutex to shared variables if necessary 
00148   //this->alg_.lock(); 
00149   //this->input_pcl2_mutex_.enter(); 
00150 
00151   //std::cout << msg->data << std::endl; 
00152 
00153   //unlock previously blocked shared variables 
00154   //this->alg_.unlock(); 
00155   //this->input_pcl2_mutex_.exit(); 
00156 }
00157 
00158 /*  [service callbacks] */
00159 
00160 /*  [action callbacks] */
00161 
00162 /*  [action requests] */
00163 
00164 cv::Mat DarwinFindEggsAlgNode::pointcloud_to_image(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud)
00165 {
00166         pcl::PointXYZRGB pixel;
00167         cv::Mat cvimage;
00168         uint32_t rgb;
00169         uint8_t r;
00170         uint8_t g;
00171         uint8_t b;
00172 
00173         
00174         cvimage = cv::Mat::zeros(cloud->height, cloud->width, CV_8UC3);//a black image 
00175         for (int i = 0; i < cvimage.rows; i++) {
00176                 //double* Mi = cvimage.ptr<double>(i);
00177                 
00178                 for (int j = 0; j< cvimage.cols; j++) {
00179                         pixel = cloud->at(j,i);
00180                         
00181                         rgb = *reinterpret_cast<int*>(&pixel.rgb);
00182                         r = (rgb >> 16) & 0x0000ff;
00183                         g = (rgb >> 8)  & 0x0000ff;
00184                         b = (rgb)       & 0x0000ff;
00185                         //ROS_INFO_STREAM("Color: " << rgb );
00186                         if (pixel.x == pixel.x) { // if x != NaN
00187                                 //Mi[j] = rgb;
00188                                 cvimage.at<cv::Vec3b>(i,j)[0] = b;
00189                                 cvimage.at<cv::Vec3b>(i,j)[1] = g;
00190                                 cvimage.at<cv::Vec3b>(i,j)[2] = r;
00191                         }
00192                 }
00193         }
00194         
00195         return cvimage;
00196 }
00197 
00198 void DarwinFindEggsAlgNode::node_config_update(Config &config, uint32_t level)
00199 {
00200   this->alg_.lock();
00201 
00202   this->alg_.unlock();
00203 }
00204 
00205 void DarwinFindEggsAlgNode::addNodeDiagnostics(void)
00206 {
00207 }
00208 
00209 /* main function */
00210 int main(int argc,char *argv[])
00211 {
00212   return algorithm_base::main<DarwinFindEggsAlgNode>(argc, argv, "darwin_find_eggs_alg_node");
00213 }


darwin_find_eggs
Author(s): galenya
autogenerated on Fri Dec 6 2013 20:36:09