average_point_cloud_alg_node.cpp
Go to the documentation of this file.
00001 #include "average_point_cloud_alg_node.h"
00002 #include <boost/foreach.hpp>
00003 
00004 
00005 AveragePointCloudAlgNode::AveragePointCloudAlgNode(void) :iBuffer_ (5), iCurrent_ (0), init_ (false)
00006 {
00007   //init class attributes if necessary
00008   //this->loop_rate_ = 2;//in [Hz]
00009   
00010   //TODO:read this value!!
00011   cloudBuffer_.resize(iBuffer_);
00012   // [init publishers]
00013   pcl2_pub_ = public_node_handle_.advertise<sensor_msgs::PointCloud2>("output", 1);
00014   
00015   // [init subscribers]
00016   pcl2_sub_ = public_node_handle_.subscribe<sensor_msgs::PointCloud2>("input", 10, &AveragePointCloudAlgNode::input_callback, this);
00017   
00018   // [init services]
00019   
00020   // [init clients]
00021   
00022   // [init action servers]
00023   
00024   // [init action clients]
00025 }
00026 
00027 AveragePointCloudAlgNode::~AveragePointCloudAlgNode(void)
00028 {
00029   // [free dynamic memory]
00030 }
00031 
00032 void AveragePointCloudAlgNode::mainNodeThread(void)
00033 {
00034   // [fill msg structures]
00035   //this->PointCloud2_msg.data = my_var;
00036   
00037   // [fill srv structure and make request to the server]
00038   
00039   // [fill action structure and make request to the action server]
00040 
00041   // [publish messages]
00042   //this->pcl2_pub_.publish(this->pcl2_final_msg_);
00043   //this->pcl2_pub_.publish(this->PointCloud_msg_);
00044 }
00045 
00046 /*  [subscriber callbacks] */
00047 void AveragePointCloudAlgNode::input_callback(const sensor_msgs::PointCloud2::ConstPtr& msg)
00048 { 
00049 
00050   if (pcl2_pub_.getNumSubscribers () > 0) 
00051   {
00052     //ROS_INFO("AveragePointCloudAlgNode::input_callback: New Message Received %d %d %d", iCurrent_, msg->width, msg->height);
00053 
00054     //pcl::fromROSMsg(*msg, pcl2_final_msg_);
00055     //pcl::fromROSMsg(*msg, pcl2_current_msg_);
00056     pcl2_final_msg_ = *msg;
00057     pcl2_current_msg_ = *msg;
00058 
00059     if (!init_) {
00060         // ROS_INFO("INIT"); 
00061         alg_.lock();
00062         input_mutex_.enter();
00063         
00064         cloudBuffer_.push_back(pcl2_current_msg_);
00065         if (++iCurrent_ == iBuffer_ - 1 ) {
00066             init_ = true;
00067             iCurrent_ = 0;
00068         }
00069         alg_.unlock();
00070         input_mutex_.exit();
00071     } else {
00072         // ROS_INFO("AVERAGING"); 
00073         alg_.lock();
00074         input_mutex_.enter();
00075 
00076         cloudBuffer_.push_back(pcl2_current_msg_);
00077 
00078         // clear final pcl2
00079         for (unsigned int rr=0; rr < msg->height; rr++){
00080             for (unsigned int cc=0; cc < msg->width; cc++){
00081                 int index = rr * pcl2_final_msg_.width + cc;
00082                 float *pstep = (float*)&pcl2_final_msg_.data[(index) * pcl2_final_msg_.point_step];
00083                 pstep[0] = 0;
00084                 pstep[1] = 0;
00085                 pstep[2] = 0;
00086                 pstep[3] = 0;
00087             }
00088         }
00089 
00090         // fill the final pcl2 with the averaged buffer pcls
00091         for (int k=0; k < iBuffer_; k++){
00092             for (unsigned int rr=0; rr < msg->height; rr++){
00093                 for (unsigned int cc=0; cc < msg->width; cc++){
00094                     int index = rr * pcl2_final_msg_.width + cc;
00095                     float *pstep_final = (float*)&pcl2_final_msg_.data[(index) * pcl2_final_msg_.point_step];
00096                     float *pstep_current =(float*)&cloudBuffer_[k].data[(index) * cloudBuffer_[k].point_step];
00097                     pstep_final[0] += pstep_current[0]/(iBuffer_);
00098                     pstep_final[1] += pstep_current[1]/(iBuffer_);
00099                     pstep_final[2] += pstep_current[2]/(iBuffer_);
00100                     pstep_final[3] += pstep_current[3]/(iBuffer_);
00101                 }
00102             }
00103             //PointCloud::iterator pcl2_current_it = cloudBuffer_[k].begin();
00104             //pcl2_final_it = pcl2_final_msg_.begin();
00105             //for (unsigned int rr=0; rr < msg->height; rr++){
00106             //    for (unsigned int cc=0; cc < msg->width; cc++, ++pcl2_current_it, ++pcl2_final_it){
00107             //        pcl2_final_it->x += pcl2_current_it->x/(iBuffer_);
00108             //        pcl2_final_it->y += pcl2_current_it->y/(iBuffer_);
00109             //        pcl2_final_it->z += pcl2_current_it->z/(iBuffer_);
00110                 //    }
00111             //}
00112         }
00113 
00114         alg_.unlock();
00115         input_mutex_.exit();
00116         pcl2_pub_.publish(pcl2_final_msg_);
00117     }
00118   }
00119  
00120 }
00121 
00122 /*  [service callbacks] */
00123 
00124 /*  [action callbacks] */
00125 
00126 /*  [action requests] */
00127 
00128 void AveragePointCloudAlgNode::node_config_update(Config &config, uint32_t level)
00129 {
00130   //update driver with new_cfg data
00131   alg_.lock();
00132   ROS_INFO ("Reconfigure request :%d, level %d", config.iBuffer, level);
00133   try
00134   {
00135     iBuffer_ = config.iBuffer;
00136     cloudBuffer_.resize(iBuffer_);
00137     init_ =false;
00138   }
00139   catch (CException & e)
00140   {
00141     std::cout << e.what () << std::endl;
00142   }
00143   alg_.unlock();
00144   // save the current configuration
00145   //this->config_ = config;
00146 }
00147 
00148 void AveragePointCloudAlgNode::addNodeDiagnostics(void)
00149 {
00150 }
00151 
00152 /* main function */
00153 int main(int argc,char *argv[])
00154 {
00155   return algorithm_base::main<AveragePointCloudAlgNode>(argc, argv, "average_point_cloud_alg_node");
00156 }


average_point_cloud_node
Author(s):
autogenerated on Fri Dec 6 2013 21:39:33