qnode_car_coop.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <ros/ros.h>
00014 #include <ros/network.h>
00015 #include <string>
00016 #include <std_msgs/String.h>
00017 #include <sstream>
00018 #include "../include/tibi_dabo_gui/qnode_car_coop.hpp"
00019 
00020 /*****************************************************************************
00021 ** Namespaces
00022 *****************************************************************************/
00023 
00024 namespace tibi_dabo_gui {
00025 
00026 /*****************************************************************************
00027 ** Implementation
00028 *****************************************************************************/
00029 
00030 QNode::QNode(int argc, char** argv ) :
00031   init_argc(argc),
00032   init_argv(argv),
00033   gui_action("gui",true),
00034   gui_action_aserver_(n, "gui_action_server")
00035   {}
00036 
00037 QNode::~QNode() {
00038   if(ros::isStarted()) {
00039     ros::shutdown(); // explicitly needed since we use ros::start();
00040     ros::waitForShutdown();
00041   }
00042   wait();
00043 }
00044 
00045 bool QNode::initialize()
00046 {
00047   if ( ! ros::master::check() ) {
00048       return false;
00049   }
00050   ros::start(); // explicitly needed since our nodehandle is going out of scope.
00051   //ros::NodeHandle n;
00052 
00053   // [service clients]
00054   get_locations_client = n.serviceClient<iri_goal_database::get_locations>("get_locations");
00055 
00056   //[action clients]
00057   this->gui_active=false;
00058 
00059   // [action servers]
00060   gui_action_aserver_.registerStartCallback(boost::bind(&QNode::gui_actionStartCallback, this, _1)); 
00061   gui_action_aserver_.registerStopCallback(boost::bind(&QNode::gui_actionStopCallback, this)); 
00062   gui_action_aserver_.registerIsFinishedCallback(boost::bind(&QNode::gui_actionIsFinishedCallback, this)); 
00063   gui_action_aserver_.registerHasSucceedCallback(boost::bind(&QNode::gui_actionHasSucceedCallback, this)); 
00064   gui_action_aserver_.registerGetResultCallback(boost::bind(&QNode::gui_actionGetResultCallback, this, _1)); 
00065   gui_action_aserver_.registerGetFeedbackCallback(boost::bind(&QNode::gui_actionGetFeedbackCallback, this, _1)); 
00066   gui_action_aserver_.start();
00067   this->new_gui_cmd=false;
00068   this->gui_cmd=gui_none;
00069 
00070   start();
00071   return true;
00072 }
00073 
00074 void QNode::run() {
00075   ros::Rate loop_rate(100);
00076   int count = 0;
00077   while ( ros::ok() )
00078   {
00079     ros::spinOnce();
00080     loop_rate.sleep();
00081     ++count;
00082   }
00083   std::cout << "Ros shutdown, proceeding to close the gui." << std::endl;
00084   Q_EMIT rosShutdown(); // used to signal the gui for a shutdown (useful to roslaunch)
00085 }
00086 
00087 void QNode::go()
00088 {
00089   mutex.lock();
00090   if(!this->gui_active)
00091   {
00092     this->gui_active=true;
00093     this->gui_goal.cmd_id=0;
00094     if(!guiMakeActionRequest())
00095       ROS_ERROR("QNode::go: guiMakeActionRequest failed!");
00096   }
00097   else
00098     ROS_INFO("QNode::go: guiAction active, goal not sent!");
00099   mutex.unlock();
00100 }
00101 
00102 bool QNode::getLocations(std::vector<std::string> & locations)
00103 {
00104   mutex.lock();
00105   //get locations from database service, fill locations vector
00106   bool ok=true;
00107   if(get_locations_client.call(this->get_locations_srv))
00108   {
00109     for(unsigned int i=0; i<this->get_locations_srv.response.locations.size(); i++)
00110     {
00111       locations.push_back(this->get_locations_srv.response.locations[i]);
00112     }
00113   }
00114   else
00115   {
00116     ok=false;
00117     ROS_ERROR("QNode::getLocations: unable to call get_locations service!");
00118   }
00119   mutex.unlock();
00120   return ok;
00121 }
00122 
00123 bool QNode::getGoal(std::string & goal)
00124 {
00125   mutex.lock();
00126   bool ok=true;
00127   if(this->new_gui_cmd)
00128   {
00129     this->new_gui_cmd=false;
00130     goal=this->gui_location_id;
00131   }
00132   else
00133     ok=false;
00134   mutex.unlock();
00135   return ok;
00136 }
00137 
00138 //gui action server
00139 void QNode::gui_actionStartCallback(const tibi_dabo_msgs::guiGoalConstPtr& goal)
00140 {
00141   ROS_INFO("QNode::gui_actionStartCallback"); 
00142   mutex.lock(); 
00143     //check goal 
00144   this->gui_cmd=(gui_cmds)goal->cmd_id;
00145   switch(this->gui_cmd)
00146   {
00147     case gui_set_goal: 
00148       if(goal->string_params.size()==1)
00149       {
00150         ROS_INFO("QNode::gui_actionStartCallback: goal_id=%s",goal->string_params[0].c_str());
00151         this->gui_location_id=goal->string_params[0];
00152         this->new_gui_cmd=true;
00153       }
00154       else
00155         this->new_gui_cmd=false;
00156       break;
00157     default: this->new_gui_cmd=false; 
00158     break;
00159   }
00160   mutex.unlock(); 
00161 } 
00162 
00163 void QNode::gui_actionStopCallback(void) 
00164 { 
00165   mutex.lock(); 
00166     //stop action 
00167   // cancel the goal
00168   mutex.unlock(); 
00169 } 
00170 
00171 bool QNode::gui_actionIsFinishedCallback(void) 
00172 { 
00173   bool ret = false; 
00174 
00175   mutex.lock(); 
00176     //if action has finish for any reason 
00177     ret = true; 
00178   mutex.unlock(); 
00179 
00180   return ret; 
00181 } 
00182 
00183 bool QNode::gui_actionHasSucceedCallback(void) 
00184 { 
00185   bool ret = false; 
00186 
00187   mutex.lock(); 
00188     //if goal was accomplished 
00189   ret=true;
00190     //ret = true; 
00191   mutex.unlock(); 
00192 
00193   return ret; 
00194 } 
00195 
00196 void QNode::gui_actionGetResultCallback(tibi_dabo_msgs::guiResultPtr& result) 
00197 { 
00198   mutex.lock(); 
00199     //update result data to be sent to client 
00200     //result->data = data; 
00201   mutex.unlock(); 
00202 } 
00203 
00204 void QNode::gui_actionGetFeedbackCallback(tibi_dabo_msgs::guiFeedbackPtr& feedback) 
00205 { 
00206   mutex.lock(); 
00207     //keep track of feedback
00208     //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00209   mutex.unlock(); 
00210 }
00211 
00212 //gui action client functions
00213 void QNode::guiDone(const actionlib::SimpleClientGoalState& state,  const tibi_dabo_msgs::guiResultConstPtr& result)
00214 {
00215   mutex.lock();
00216   if(state.state_==actionlib::SimpleClientGoalState::SUCCEEDED)
00217     ROS_INFO("QNode::guiDone: Goal Achieved!");
00218   else if(state.state_==actionlib::SimpleClientGoalState::ABORTED)
00219     ROS_INFO("QNode::guiDone: Goal aborted!");
00220   else if(state.state_==actionlib::SimpleClientGoalState::PREEMPTED)
00221     ROS_INFO("QNode::guiDone: Goal preempted!");
00222   else
00223     ROS_INFO("QNode::guiDone: Other termination state: PENDING, ACTIVE, RECALLED, REJECTED, LOST");
00224   this->gui_active=false;
00225   mutex.unlock();
00226 }
00227 
00228 void QNode::guiActive()
00229 {
00230   mutex.lock();
00231   ROS_INFO("QNode::guiActive: Goal just went active!"); 
00232   this->gui_active=true;
00233   mutex.unlock();
00234 }
00235 
00236 void QNode::guiFeedback(const tibi_dabo_msgs::guiFeedbackConstPtr& feedback)
00237 {
00238   mutex.lock();
00239   //ROS_INFO("QNode::guiFeedback: feedback received!");
00240   bool feedback_is_ok = true;
00241   if( !feedback_is_ok )
00242     gui_action.cancelGoal();
00243   mutex.unlock();
00244 }
00245 
00246 bool QNode::guiMakeActionRequest() 
00247 {
00248   ROS_INFO("QNode::guiMakeActionRequest: Starting New Request!"); 
00249   gui_action.waitForServer();
00250   ROS_INFO("QNode::guiMakeActionRequest: Server is available!"); 
00251   gui_action.sendGoal(this->gui_goal, 
00252               boost::bind(&QNode::guiDone,     this, _1, _2), 
00253               boost::bind(&QNode::guiActive,   this), 
00254               boost::bind(&QNode::guiFeedback, this, _1)); 
00255   ROS_INFO("QNode::guiMakeActionRequest: Goal Sent. Wait for Result!"); 
00256   return true;
00257 }
00258 
00259 }  // namespace tibi_dabo_gui


tibi_dabo_gui
Author(s): fherrero
autogenerated on Fri Dec 6 2013 22:01:34