get_board_information_alg_node.cpp
Go to the documentation of this file.
00001 #include "get_board_information_alg_node.h"
00002 
00003 
00004 GetBoardInformationAlgNode::GetBoardInformationAlgNode(void) :
00005   algorithm_base::IriBaseAlgorithm<GetBoardInformationAlgorithm>(),
00006   EventInfoReady("Info ready"),
00007   EventServiceWaiting("Service Waiting")
00008 {
00009         //init class attributes if necessary
00010         //this->loop_rate_ = 2;//in [Hz]
00011         
00012         // [init publishers]
00013   this->believe_status_publisher_ = this->public_node_handle_.advertise<sensor_msgs::Image>("believe_status", 1);
00014         this->pointcloud_out_publisher_ = this->public_node_handle_.advertise<sensor_msgs::PointCloud2>("pointcloud_out", 1);
00015         
00016         // [init subscribers]
00017         this->unreachable_position_subscriber_ = this->public_node_handle_.subscribe("unreachable_position", 10, &GetBoardInformationAlgNode::unreachable_position_callback, this);
00018         this->plane_coefficients_subscriber_ = this->public_node_handle_.subscribe("plane_coefficients", 1, &GetBoardInformationAlgNode::plane_coefficients_callback, this);
00019         this->pointcloud_in_subscriber_ = this->public_node_handle_.subscribe("pointcloud_in", 0, &GetBoardInformationAlgNode::pointcloud_in_callback, this);
00020         this->image_in_subscriber_ = this->public_node_handle_.subscribe("image_in", 1, &GetBoardInformationAlgNode::image_in_callback, this);
00021         
00022         // [init services]
00023         this->get_board_info_server_ = this->public_node_handle_.advertiseService("get_board_info_srv", &GetBoardInformationAlgNode::get_board_infoCallback, this);
00024         
00025         // [init clients]
00026         
00027         // [init action servers]
00028         
00029         // [init action clients]
00030         
00031         // other
00032         is_service_waiting = false;
00033         waiting_for_pointcloud = false;
00034         waiting_for_image = false;
00035         waiting_for_coefficients = false;
00036         
00037         // initial non-existing image
00038         cv_bridge::CvImage cvbridge_aux;
00039         cvbridge_aux.encoding = sensor_msgs::image_encodings::BGR8;
00040         cvbridge_aux.image    = cv::Mat::zeros(cv::Size(640,480), CV_8UC3);
00041         cvbridge_aux.header.stamp = ros::Time::now();
00042         last_image = *cvbridge_aux.toImageMsg();
00043 }
00044 
00045 GetBoardInformationAlgNode::~GetBoardInformationAlgNode(void)
00046 {
00047         // [free dynamic memory]
00048 }
00049 
00050 void GetBoardInformationAlgNode::mainNodeThread(void)
00051 {
00052         // [fill msg structures]
00053   //this->Image_msg_.data = my_var;
00054         //this->PointCloud2_msg_.data = my_var;
00055         
00056         // [fill srv structure and make request to the server]
00057         
00058         // [fill action structure and make request to the action server]
00059 
00060         // [publish messages]
00061         //this->believe_status_publisher_.publish(this->Image_msg_);
00062         //this->pointcloud_out_publisher_.publish(this->PointCloud2_msg_);
00063         
00064         waiting_for_pointcloud = true;
00065         waiting_for_image = true;
00066         waiting_for_coefficients = true;
00067         
00068         // wait for the event to be signales. This function will get blocked until
00069         // the timeout expires.
00070         while (waiting_for_image || waiting_for_coefficients) {
00071                 try {
00072                         EventInfoReady.wait(5000000); // 5 secs
00073                 }catch(CEventTimeoutException& e){
00074                         //std::cout << e.what() << std::endl;
00075                         ROS_ERROR_STREAM("get_board_info -> main_thread: timed out!");
00076                         ROS_INFO_STREAM("Got pointcloud: " << not waiting_for_pointcloud);
00077                         ROS_INFO_STREAM("Got image:" << not waiting_for_image);
00078                         ROS_INFO_STREAM("Got coefficients: " << not waiting_for_coefficients);
00079                         
00080                         // retry -> reset booleans
00081                         waiting_for_pointcloud = true;
00082                         waiting_for_image = true;
00083                         waiting_for_coefficients = true;
00084                 }catch(CException& e){
00085                         //std::cout << e.what() << std::endl;
00086                         ROS_ERROR("get_board_info -> main_thread: Exception found waiting");
00087                         return;
00088                 }
00089         }
00090         
00091         this->alg_.lock();
00092         last_pointcloud = current_pointcloud; 
00093         //res.segmented_image = current_image; 
00094         last_image = current_image;
00095         last_plane_coeffs = current_plane_coeffs;
00096         this->alg_.unlock();
00097         
00098         // publish current believe
00099         this->Image_msg_ = this->alg_.getCurrentBelieveColors();
00100         
00101         if(this->believe_status_publisher_.getNumSubscribers())
00102                 this->believe_status_publisher_.publish(this->Image_msg_);
00103         
00104         if (is_service_waiting) {
00105                 is_service_waiting = false;
00106                 EventServiceWaiting.set();
00107         }
00108 }
00109 
00110 /*  [subscriber callbacks] */
00111 void GetBoardInformationAlgNode::unreachable_position_callback(const estirabot_msgs::ImagePoint::ConstPtr& msg) 
00112 { 
00113         ROS_DEBUG("GetBoardInformationAlgNode::unreachable_position_callback: New Message Received"); 
00114 
00115         //use appropiate mutex to shared variables if necessary 
00116         //this->alg_.lock(); 
00117         //this->unreachable_position_mutex_.enter(); 
00118         
00119         //std::cout << msg->data << std::endl; 
00120         
00121         alg_.updateUnreachableZones(msg);
00122         
00123         //unlock previously blocked shared variables 
00124         //this->alg_.unlock(); 
00125         //this->unreachable_position_mutex_.exit(); 
00126 }
00127 void GetBoardInformationAlgNode::plane_coefficients_callback(const pcl_msgs::ModelCoefficients::ConstPtr& msg) 
00128 { 
00129         ROS_DEBUG("GetBoardInformationAlgNode::plane_coefficients_callback: New Message Received"); 
00130 
00131         //use appropiate mutex to shared variables if necessary 
00132         //this->alg_.lock(); 
00133         //this->plane_coefficients_mutex_.enter(); 
00134 
00135         //std::cout << msg->data << std::endl; 
00136         current_plane_coeffs = *msg;
00137         waiting_for_coefficients = false;
00138         EventInfoReady.set();
00139 
00140         //unlock previously blocked shared variables 
00141         //this->alg_.unlock(); 
00142         //this->plane_coefficients_mutex_.exit(); 
00143 }
00144 void GetBoardInformationAlgNode::pointcloud_in_callback(const sensor_msgs::PointCloud2::ConstPtr& msg) 
00145 { 
00146         //ROS_INFO("GetBoardInformationAlgNode::pointcloud_in_callback: New Message Received"); 
00147 
00148         //use appropiate mutex to shared variables if necessary 
00149         //this->alg_.lock(); 
00150         //this->pointcloud_in_mutex_.enter(); 
00151 
00152         //std::cout << msg->data << std::endl; 
00153         //if (!has_pointcloud)
00154         //      ROS_INFO("GetBoardInformationAlgNode::pointcloud_in_callback: Pointcloud available"); 
00155         
00156         // if it was waiting for a pointcloud to be ready, publish it
00157         if (waiting_for_pointcloud) {
00158                 ROS_DEBUG("GetBoardInformationAlgNode::pointcloud_in_callback: Pointcloud available");
00159                 current_pointcloud = *msg;
00160                 //this->PointCloud2_msg_ = *msg;
00161                 this->pointcloud_out_publisher_.publish(*msg);
00162                 waiting_for_pointcloud = false;
00163         }
00164 
00165         //unlock previously blocked shared variables 
00166         //this->alg_.unlock(); 
00167         //this->pointcloud_in_mutex_.exit(); 
00168 }
00169 void GetBoardInformationAlgNode::image_in_callback(const sensor_msgs::Image::ConstPtr& msg) 
00170 { 
00171         ROS_DEBUG("GetBoardInformationAlgNode::image_in_callback: New Message Received"); 
00172 
00173         //use appropiate mutex to shared variables if necessary 
00174         //this->alg_.lock();
00175         //this->image_in_mutex_.enter(); 
00176 
00177         //std::cout << msg->data << std::endl; 
00178         current_image = *msg;
00179         this->alg_.updateCurrentBelieve(msg);
00180         waiting_for_image = false;
00181 
00182         //unlock previously blocked shared variables 
00183         //this->alg_.unlock(); 
00184         EventInfoReady.set();
00185         //this->image_in_mutex_.exit(); 
00186 }
00187 
00188 /*  [service callbacks] */
00189 bool GetBoardInformationAlgNode::get_board_infoCallback(iri_clean_board::BoardInfo::Request &req, iri_clean_board::BoardInfo::Response &res) 
00190 { 
00191         ROS_DEBUG("GetBoardInformationAlgNode::get_board_infoCallback: New Request Received!"); 
00192         
00193         this->get_board_info_mutex_.enter(); 
00194         
00195         // wait for new image, for having updated data
00196         
00197         /*is_service_waiting = true;
00198         while (is_service_waiting) {
00199                 try {
00200                         EventServiceWaiting.wait(1000000);
00201                 }catch(CEventTimeoutException& e){
00202                         std::cout << e.what() << std::endl;
00203                 }catch(CException& e){
00204                         std::cout << e.what() << std::endl;
00205                         return false;
00206                 }
00207         }*/
00208         // fill service with new data
00209         this->alg_.lock();
00210                 res.pointcloud = last_pointcloud; 
00211                 res.segmented_image = this->alg_.joinImageAndBelieve(last_image);
00212                 res.plane_coefficients = last_plane_coeffs.values;
00213         this->alg_.unlock();
00214         
00215         this->get_board_info_mutex_.exit();
00216         
00217         return true;
00218 }
00219 
00220 /*  [action callbacks] */
00221 
00222 /*  [action requests] */
00223 
00224 
00225 void GetBoardInformationAlgNode::node_config_update(Config &config, uint32_t level)
00226 {
00227         this->alg_.lock();
00228 
00229         this->alg_.unlock();
00230 }
00231 
00232 void GetBoardInformationAlgNode::addNodeDiagnostics(void)
00233 {
00234 }
00235 
00236 /* main function */
00237 int main(int argc,char *argv[])
00238 {
00239   //return algorithm_base::main<GetBoardInformationAlgNode>(argc, argv, "get_board_information_alg_node");
00240   return algorithm_base::main<GetBoardInformationAlgNode>(argc, argv, "get_board_information_alg_node");
00241 }


iri_clean_board
Author(s): David Martinez
autogenerated on Fri Dec 6 2013 23:52:37