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   
00009   
00010 
00011   
00012   this->output_pcl2_publisher_ = this->public_node_handle_.advertise<sensor_msgs::PointCloud2>("output_pcl2", 1);
00013   
00014   
00015   this->input_pcl2_subscriber_ = this->public_node_handle_.subscribe("input_pcl2", 1, &DarwinFindEggsAlgNode::input_pcl2_callback, this);
00016   
00017   
00018   
00019   
00020   
00021   
00022   
00023   
00024 }
00025 
00026 DarwinFindEggsAlgNode::~DarwinFindEggsAlgNode(void)
00027 {
00028   
00029 }
00030 
00031 void DarwinFindEggsAlgNode::mainNodeThread(void)
00032 {
00033   
00034   
00035   
00036   
00037   
00038   
00039 
00040   
00041 
00042 }
00043 
00044 
00045 void DarwinFindEggsAlgNode::input_pcl2_callback(const sensor_msgs::PointCloud2::ConstPtr& msg) 
00046 { 
00047   
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 
00059 
00060 
00061 
00062 
00063 
00064 
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     
00072     uint32_t rgb = *reinterpret_cast<int*>(&cloud.points[point_id].rgb);
00073     uint8_t r = (rgb >> 16) & 0x0000ff;
00074     
00075     
00076     
00077   
00078     if ((r >200 ) && (cloud.points[point_id].z <0.5)) {
00079       
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       
00085       
00086     uint8_t r = 255, g = 0, b = 0; 
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   
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 
00100 
00101 
00102 
00103 
00104 
00105   
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
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   
00148   
00149   
00150 
00151   
00152 
00153   
00154   
00155   
00156 }
00157 
00158 
00159 
00160 
00161 
00162 
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);
00175         for (int i = 0; i < cvimage.rows; i++) {
00176                 
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                         
00186                         if (pixel.x == pixel.x) { 
00187                                 
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 
00210 int main(int argc,char *argv[])
00211 {
00212   return algorithm_base::main<DarwinFindEggsAlgNode>(argc, argv, "darwin_find_eggs_alg_node");
00213 }