moped_handler_alg_node.cpp
Go to the documentation of this file.
00001 #include "moped_handler_alg_node.h"
00002 
00003 MopedHandlerAlgNode::MopedHandlerAlgNode(void) :
00004         algorithm_base::IriBaseAlgorithm<MopedHandlerAlgorithm>(),
00005         moped_client_("moped", true)
00006 {
00007         //init class attributes if necessary
00008         this->working = false;
00009 
00010         // [init publishers]
00011         this->outputOPL_publisher_ = this->public_node_handle_.advertise<pr_msgs::ObjectPoseList>("outputOPL", 10);
00012         
00013         // [init subscribers]
00014         this->subImage_.subscribe(this->public_node_handle_, "/camera/rgb/image_mono", 1);
00015         this->subPointCloud_.subscribe(this->public_node_handle_, "/camera/rgb/points", 1);
00016 
00017         typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::PointCloud2> MySyncPolicy;
00018         this->sync_ = new message_filters::Synchronizer<MySyncPolicy>(MySyncPolicy(10), subImage_, subPointCloud_);
00019         sync_->registerCallback(boost::bind(&MopedHandlerAlgNode::callback, this, _1, _2));
00020 
00021         // [init services]
00022   this->enable_server_ = this->public_node_handle_.advertiseService("enable", &MopedHandlerAlgNode::enableCallback, this);
00023         
00024         // [init clients]
00025         
00026         // [init action servers]
00027         
00028         // [init action clients]
00029 }
00030 
00031 MopedHandlerAlgNode::~MopedHandlerAlgNode(void)
00032 {
00033         // [free dynamic memory]
00034 }
00035 
00036 void MopedHandlerAlgNode::mainNodeThread(void)
00037 {
00038         // [fill msg structures]
00039   //this->ObjectPoseList_msg_.data = my_var;
00040         
00041         // [fill srv structure and make request to the server]
00042         
00043         // [fill action structure and make request to the action server]
00044 //      mopedMakeActionRequest(); // Not used here, first we have to receive image data from camera.
00045 
00046         // [publish messages]
00047 //  this->outputOPL_publisher_.publish(this->ObjectPoseList_msg_);
00048 }
00049 
00050 /*      [subscriber callbacks] */
00051 
00052 /*      [service callbacks] */
00053 bool MopedHandlerAlgNode::enableCallback(iri_moped_handler::enable::Request &req, iri_moped_handler::enable::Response &res) 
00054 { 
00055         ROS_INFO("MopedHandlerAlgNode::enableCallback: New Request Received!"); 
00056 
00057         if(req.enable)
00058         {
00059                 this->subImage_.subscribe(this->public_node_handle_, "/camera/rgb/image_mono", 1);
00060                 this->subPointCloud_.subscribe(this->public_node_handle_, "/camera/rgb/points", 1);
00061 
00062                 typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::PointCloud2> MySyncPolicy;
00063                 this->sync_ = new message_filters::Synchronizer<MySyncPolicy>(MySyncPolicy(10), subImage_, subPointCloud_);
00064                 sync_->registerCallback(boost::bind(&MopedHandlerAlgNode::callback, this, _1, _2));
00065         }
00066         else
00067         {
00068                 this->subImage_.subscribe(this->public_node_handle_, "falseTopic", 1);
00069                 this->subPointCloud_.subscribe(this->public_node_handle_, "falseTopic2", 1);
00070         }
00071 
00072         //use appropiate mutex to shared variables if necessary 
00073         //this->alg_.lock(); 
00074         //this->enable_mutex_.enter(); 
00075 
00076         //if(this->alg_.isRunning()) 
00077         //{ 
00078         //ROS_INFO("MopedHandlerAlgNode::enableCallback: Processin New Request!"); 
00079         //do operations with req and output on res 
00080         //res.data2 = req.data1 + my_var; 
00081         //} 
00082         //else 
00083         //{ 
00084         //ROS_INFO("MopedHandlerAlgNode::enableCallback: ERROR: alg is not on run mode yet."); 
00085         //} 
00086 
00087         //unlock previously blocked shared variables 
00088         //this->alg_.unlock(); 
00089         //this->enable_mutex_.exit(); 
00090 
00091         return true; 
00092 }
00093 
00094 /*      [action callbacks] */
00095 void MopedHandlerAlgNode::mopedDone(const actionlib::SimpleClientGoalState& state,      const iri_moped_actionserver::mopedResultConstPtr& result) 
00096 { 
00097         if( state.toString().compare("SUCCEEDED") == 0 ) 
00098                 ROS_INFO("MopedHandlerAlgNode::mopedDone: Goal Achieved!"); 
00099         else 
00100         {
00101                 ROS_INFO("MopedHandlerAlgNode::mopedDone: %s", state.toString().c_str()); 
00102                 return;
00103         }
00104 
00105         // Convert the sensor_msgs/PointCloud2 data to pcl/PointCloud
00106         pcl::fromROSMsg(this->inputPointCloud, this->pcl_);
00107 
00108         // Create result message.
00109         pr_msgs::ObjectPoseList outputMsg = result->pose;
00110 
00111         // Delete bad objects from result message
00112         vector<pr_msgs::ObjectPose> objects = outputMsg.object_list;
00113         vector<int> bad; // Indices containing bad objects.
00114         for(size_t i=0; i<objects.size(); ++i)
00115         {
00116                 float col = objects[i].pose2D.x;
00117                 float row = objects[i].pose2D.y;
00118 
00119                 // Check bounds
00120                 if( col < 0 or col >= 640 or row < 0 or row >= 480 )
00121                         bad.push_back(i);
00122         }
00123 
00124         // Delete objects in inverse order.
00125         for(int i=bad.size()-1; i>=0; --i)
00126                 outputMsg.object_list.erase( outputMsg.object_list.begin() + bad[i] );
00127 
00128         // For every good object detected...
00129         for(size_t i=0; i < outputMsg.object_list.size(); ++i)
00130         {
00131                 // Get 2D position
00132                 float col = outputMsg.object_list[i].pose2D.x;
00133                 float row = outputMsg.object_list[i].pose2D.y;
00134 
00135                 // Get 3D position according to pcl.
00136                 pcl::PointXYZ p = this->pcl_.at(col, row);
00137 
00138                 // Check p correctness.
00139                 // TODO: Complete.
00140 
00141                 // Set 3D position to output message.
00142                 outputMsg.object_list[i].pose.position.x = p.x;
00143                 outputMsg.object_list[i].pose.position.y = p.y;
00144                 outputMsg.object_list[i].pose.position.z = p.z;
00145 
00146                 // Feedback TODO: Comment.
00147                 ROS_INFO("Pixel (%f, %f) is at position (%f, %f, %f).", col, row, p.x, p.y, p.z);
00148         }
00149 
00150         // Publish data to output topic
00151         this->outputOPL_publisher_.publish(outputMsg);
00152 
00153         ROS_INFO("Sent final result to topic");
00154 
00155         this->working = false;
00156 } 
00157 
00158 void MopedHandlerAlgNode::mopedActive() 
00159 { 
00160         //ROS_INFO("MopedHandlerAlgNode::mopedActive: Goal just went active!"); 
00161 } 
00162 
00163 void MopedHandlerAlgNode::mopedFeedback(const iri_moped_actionserver::mopedFeedbackConstPtr& feedback) 
00164 { 
00165         //ROS_INFO("MopedHandlerAlgNode::mopedFeedback: Got Feedback!"); 
00166 
00167         bool feedback_is_ok = true; 
00168 
00169         //analyze feedback 
00170         //my_var = feedback->var; 
00171 
00172         //if feedback is not what expected, cancel requested goal 
00173         if( !feedback_is_ok ) 
00174         { 
00175                 moped_client_.cancelGoal(); 
00176                 //ROS_INFO("MopedHandlerAlgNode::mopedFeedback: Cancelling Action!"); 
00177         } 
00178 }
00179 
00180 /*      [action requests] */
00181 void MopedHandlerAlgNode::mopedMakeActionRequest() 
00182 { 
00183         ROS_INFO("MopedHandlerAlgNode::mopedMakeActionRequest: Starting New Request!"); 
00184 
00185         //wait for the action server to start 
00186         //will wait for infinite time 
00187         moped_client_.waitForServer();  
00188         ROS_INFO("MopedHandlerAlgNode::mopedMakeActionRequest: Server is Available!"); 
00189 
00190         //send a goal to the action 
00191         moped_goal_.image = this->inputImage; 
00192         moped_client_.sendGoal(moped_goal_, 
00193                                                         boost::bind(&MopedHandlerAlgNode::mopedDone,             this, _1, _2), 
00194                                                         boost::bind(&MopedHandlerAlgNode::mopedActive,   this), 
00195                                                         boost::bind(&MopedHandlerAlgNode::mopedFeedback, this, _1)); 
00196         ROS_INFO("MopedHandlerAlgNode::mopedMakeActionRequest: Goal Sent. Wait for Result!"); 
00197 }
00198 
00199 void MopedHandlerAlgNode::node_config_update(Config &config, uint32_t level)
00200 {
00201         this->alg_.lock();
00202 
00203         this->alg_.unlock();
00204 }
00205 
00206 void MopedHandlerAlgNode::callback(const ImageConstPtr& image, const PointCloud2ConstPtr& pcl)
00207 {
00208         if( this->working )
00209                 return;
00210         else
00211                 this->working = true;
00212 
00213         ROS_INFO("Received image and PCL with the same timestamp!");
00214 
00215         // Save image and PCL to attributes in order to work with them later.
00216         this->inputImage = *image;
00217         this->inputPointCloud = *pcl;
00218 
00219         // Send Image to Moped to process it and get the result.
00220         mopedMakeActionRequest();
00221 }
00222 
00223 void MopedHandlerAlgNode::addNodeDiagnostics(void)
00224 {
00225 }
00226 
00227 /* main function */
00228 int main(int argc,char *argv[])
00229 {
00230         return algorithm_base::main<MopedHandlerAlgNode>(argc, argv, "moped_handler_alg_node");
00231 }


iri_moped_handler
Author(s): frigual
autogenerated on Fri Dec 6 2013 20:55:45