moped_actionserver_alg_node.cpp
Go to the documentation of this file.
00001 #include "moped_actionserver_alg_node.h"
00002 
00003 MopedActionserverAlgNode::MopedActionserverAlgNode(void) :
00004         algorithm_base::IriBaseAlgorithm<MopedActionserverAlgorithm>(),
00005         moped_aserver_(public_node_handle_, "moped")
00006 {
00007         //init class attributes if necessary
00008         this->image_sent = false;
00009 
00010         // [init publishers]
00011   this->Image_publisher_ = this->public_node_handle_.advertise<sensor_msgs::Image>("/moped/image", 10);
00012         
00013         // [init subscribers]
00014   this->object_poses_subscriber_ = this->public_node_handle_.subscribe("object_poses", 10, &MopedActionserverAlgNode::object_poses_callback, this);
00015         
00016         // [init services]
00017         
00018         // [init clients]
00019         
00020         // [init action servers]
00021         moped_aserver_.registerStartCallback(boost::bind(&MopedActionserverAlgNode::mopedStartCallback, this, _1)); 
00022         moped_aserver_.registerStopCallback(boost::bind(&MopedActionserverAlgNode::mopedStopCallback, this)); 
00023         moped_aserver_.registerIsFinishedCallback(boost::bind(&MopedActionserverAlgNode::mopedIsFinishedCallback, this)); 
00024         moped_aserver_.registerHasSucceedCallback(boost::bind(&MopedActionserverAlgNode::mopedHasSucceedCallback, this)); 
00025         moped_aserver_.registerGetResultCallback(boost::bind(&MopedActionserverAlgNode::mopedGetResultCallback, this, _1)); 
00026         moped_aserver_.registerGetFeedbackCallback(boost::bind(&MopedActionserverAlgNode::mopedGetFeedbackCallback, this, _1)); 
00027         moped_aserver_.start();
00028         
00029         // [init action clients]
00030 }
00031 
00032 MopedActionserverAlgNode::~MopedActionserverAlgNode(void)
00033 {
00034         // [free dynamic memory]
00035 }
00036 
00037 void MopedActionserverAlgNode::mainNodeThread(void)
00038 {
00039         // [fill msg structures]
00040   //this->Image_msg_.data = my_var;
00041         
00042         // [fill srv structure and make request to the server]
00043         
00044         // [fill action structure and make request to the action server]
00045 
00046         // [publish messages]
00047 //  this->Image_publisher_.publish(this->Image_msg_);
00048 }
00049 
00050 /*      [subscriber callbacks] */
00051 void MopedActionserverAlgNode::object_poses_callback(const pr_msgs::ObjectPoseList::ConstPtr& msg) 
00052 { 
00053   ROS_INFO("MopedActionserverAlgNode::object_poses_callback: New Message Received"); 
00054 
00055   //use appropiate mutex to shared variables if necessary 
00056   //this->alg_.lock(); 
00057   //this->object_poses_mutex_.enter(); 
00058 
00059         // Check if we were waiting for a result.
00060         if( not this->image_sent )
00061         {
00062                 ROS_ERROR("New moped result received but wasn't requested.");
00063                 return;
00064         }
00065 
00066         this->image_sent = false;
00067         this->result = (*msg);
00068 
00069   //unlock previously blocked shared variables 
00070   //this->alg_.unlock(); 
00071   //this->object_poses_mutex_.exit(); 
00072 }
00073 
00074 /*      [service callbacks] */
00075 
00076 /*      [action callbacks] */
00077 void MopedActionserverAlgNode::mopedStartCallback(const iri_moped_actionserver::mopedGoalConstPtr& goal)
00078 { 
00079         alg_.lock();
00080 
00081         // Check if node is currently processing an image.
00082         if( this->image_sent )
00083         {
00084                 ROS_ERROR("Still processing last image.");
00085                 alg_.unlock();
00086                 return;
00087         }
00088  
00089         // Save received image to attribute.
00090         this->Image_msg_ = goal->image;
00091 
00092         // Publish image to /Image topic.
00093         this->Image_publisher_.publish(this->Image_msg_);
00094 
00095         // Set control variable to indicate an image has been sent.
00096         this->image_sent = true;
00097 
00098         //execute goal 
00099         alg_.unlock(); 
00100 } 
00101 
00102 void MopedActionserverAlgNode::mopedStopCallback(void) 
00103 { 
00104         alg_.lock(); 
00105 
00106         // Check if was running
00107         if( this->image_sent == false )
00108         {
00109                 ROS_ERROR("Received a stop request but Moped wasn't running.");
00110                 alg_.unlock(); 
00111                 return;         
00112         }
00113 
00114         //stop action
00115         this->image_sent = false;
00116 
00117         alg_.unlock(); 
00118 } 
00119 
00120 bool MopedActionserverAlgNode::mopedIsFinishedCallback(void) 
00121 { 
00122         bool ret = false; 
00123 
00124         alg_.lock();
00125 
00126         ret = not this->image_sent;
00127 
00128         alg_.unlock(); 
00129 
00130         return ret; 
00131 } 
00132 
00133 bool MopedActionserverAlgNode::mopedHasSucceedCallback(void) 
00134 { 
00135         bool ret = false; 
00136 
00137         alg_.lock();
00138 
00139         ret = not this->image_sent;
00140 
00141         alg_.unlock(); 
00142 
00143         return ret; 
00144 } 
00145 
00146 void MopedActionserverAlgNode::mopedGetResultCallback(iri_moped_actionserver::mopedResultPtr& result) 
00147 { 
00148         alg_.lock(); 
00149 
00150         //update result data to be sent to client 
00151         result->pose = this->result;
00152 
00153         alg_.unlock(); 
00154 } 
00155 
00156 void MopedActionserverAlgNode::mopedGetFeedbackCallback(iri_moped_actionserver::mopedFeedbackPtr& feedback) 
00157 { 
00158         alg_.lock(); 
00159         //keep track of feedback 
00160         //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00161         alg_.unlock(); 
00162 }
00163 
00164 /*      [action requests] */
00165 
00166 void MopedActionserverAlgNode::node_config_update(Config &config, uint32_t level)
00167 {
00168         this->alg_.lock();
00169 
00170         this->alg_.unlock();
00171 }
00172 
00173 void MopedActionserverAlgNode::addNodeDiagnostics(void)
00174 {
00175 }
00176 
00177 /* main function */
00178 int main(int argc,char *argv[])
00179 {
00180         return algorithm_base::main<MopedActionserverAlgNode>(argc, argv, "moped_actionserver_alg_node");
00181 }


iri_moped_actionserver
Author(s): frigual
autogenerated on Fri Dec 6 2013 20:49:30