grasp_actions_alg_node_base.cpp
Go to the documentation of this file.
00001 
00008 #include "grasp_actions_alg_node.h"
00009 
00010 GraspActionsAlgNode::GraspActionsAlgNode(void) :
00011     algorithm_base::IriBaseAlgorithm<GraspActionsAlgorithm>(),
00012     pickup_aserver_(public_node_handle_, "pickup"),
00013     open_gripper_client_("open_gripper", true),
00014     close_gripper_client_("close_gripper", true)
00015 {
00016   //init class attributes if necessary
00017   //this->loop_rate_ = 2;//in [Hz]
00018 
00019   // [init publishers]
00020   // [init subscribers]
00021   // [init services]
00022   // [init clients]
00023   bhand_cmd_client_    = public_node_handle_.serviceClient<iri_wam_common_msgs::bhand_cmd>("bhand_cmd");
00024   joints_move_client_  = public_node_handle_.serviceClient<iri_wam_common_msgs::joints_move>("joints_move");
00025   wam_bhand_ik_client_ = public_node_handle_.serviceClient<iri_wam_common_msgs::wamInverseKinematics>("get_wam_ik");
00026 
00027   // [init action servers]
00028   pickup_aserver_.registerStartCallback(boost::bind(&GraspActionsAlgNode::pickupStartCallback, this, _1)); 
00029   pickup_aserver_.registerStopCallback(boost::bind(&GraspActionsAlgNode::pickupStopCallback, this)); 
00030   pickup_aserver_.registerIsFinishedCallback(boost::bind(&GraspActionsAlgNode::pickupIsFinishedCallback, this)); 
00031   pickup_aserver_.registerHasSucceedCallback(boost::bind(&GraspActionsAlgNode::pickupHasSucceedCallback, this)); 
00032   pickup_aserver_.registerGetResultCallback(boost::bind(&GraspActionsAlgNode::pickupGetResultCallback, this, _1)); 
00033   pickup_aserver_.registerGetFeedbackCallback(boost::bind(&GraspActionsAlgNode::pickupGetFeedbackCallback, this, _1)); 
00034   pickup_aserver_.start();
00035   // [init action clients]
00036   
00037 //   ros::NodeHandle private_node_handle("~");
00038   private_node_handle_.param<bool>("using_bhand", using_bhand_, true);
00039 }
00040 
00041 GraspActionsAlgNode::~GraspActionsAlgNode(void)
00042 {
00043   // [free dynamic memory]
00044 }
00045 
00046 void GraspActionsAlgNode::mainNodeThread(void)
00047 {
00048   // [fill msg structures]
00049   // [fill srv structure and make request to the server]
00050   // [fill action structure and make request to the action server]
00051   // [publish messages]
00052 }
00053 
00054 /*  [subscriber callbacks] */
00055 
00056 /*  [service callbacks] */
00057 
00058 /*  [action callbacks] */
00059 
00060 void
00061 GraspActionsAlgNode::moveArm(const geometry_msgs::PoseStamped pose)
00062 {
00063     iri_wam_common_msgs::wamInverseKinematics wam_bhand_ik_srv;
00064 
00065     // Need to transform from cartesian pose to joint state. Call to wam_ik
00066     wam_bhand_ik_srv.request.pose = pose;
00067 
00068     if (! wam_bhand_ik_client_.call(wam_bhand_ik_srv))
00069     {
00070         ROS_ERROR("Failed to call service %s", wam_bhand_ik_client_.getService().c_str());
00071         ROS_ERROR("No hand movement done");
00072         return;
00073     }
00074 
00075     iri_wam_common_msgs::joints_move joints_move_srv;
00076 
00077     // Convert JointState to a float64[] message
00078     for (std::vector<double>::iterator it = wam_bhand_ik_srv.response.joints.position.begin();
00079                                        it < wam_bhand_ik_srv.response.joints.position.end();
00080                                        it++)
00081         joints_move_srv.request.joints.push_back(*it);
00082 
00083     if (! joints_move_client_.call(joints_move_srv))
00084     {
00085       ROS_ERROR("Failed to move to grasp position: service call  %s",
00086                 joints_move_client_.getService().c_str());
00087 
00088       ROS_ERROR("No hand movement done");
00089       return;
00090     }
00091 
00092     ROS_DEBUG("Grasp position move success %d", joints_move_srv.response.success);
00093     return;
00094 }
00095 
00096 void
00097 GraspActionsAlgNode::moveFingers(const FingersConfiguration & fingers_config)
00098 {
00099     if (using_bhand_) // using Barret Hand
00100         moveFingersBHand(fingers_config);
00101     else // using Dynamixel Gripper
00102         moveFingersGripper(fingers_config);
00103     return;
00104 }
00105 
00106 void
00107 GraspActionsAlgNode::moveFingersGripper(const FingersConfiguration & fingers_config)
00108 {
00109     for (std::vector<std::string>::const_iterator it = fingers_config.begin();
00110                                                   it < fingers_config.end();
00111                                                   it++)
00112     {
00113         bool finished_within_time = false;
00114         if (it->compare("open") == 0) {
00115             open_gripperMakeActionRequest();
00116             finished_within_time = open_gripper_client_.waitForResult(ros::Duration(6.0));
00117         }
00118         else if (it->compare("close") == 0) {
00119             close_gripperMakeActionRequest();
00120             finished_within_time = close_gripper_client_.waitForResult(ros::Duration(6.0));
00121         }
00122         else{
00123             ROS_WARN_STREAM("Unknown gripper command " << *it << ". Only \"open\" and \"close\" commands are accepted.");
00124         }
00125         
00126         if (!finished_within_time)
00127             ROS_ERROR_STREAM("Failed call to gripper with action: " << * it);
00128     }
00129     return;
00130 }
00131 
00132 void
00133 GraspActionsAlgNode::moveFingersBHand(const FingersConfiguration & fingers_config)
00134 {
00135     iri_wam_common_msgs::bhand_cmd bhand_cmd_srv;
00136 
00137     for (std::vector<std::string>::const_iterator it = fingers_config.begin();
00138                                                   it < fingers_config.end();
00139                                                   it++)
00140     {
00141         bhand_cmd_srv.request.bhandcmd = * it;
00142 
00143         if (! bhand_cmd_client_.call(bhand_cmd_srv))
00144         {
00145           ROS_ERROR("Failed to call service %s", bhand_cmd_client_.getService().c_str());
00146           return;
00147         }
00148 
00149         ROS_DEBUG("bhand move success %d", bhand_cmd_srv.response.success);
00150     }
00151     return;
00152 }
00153 
00154 void
00155 GraspActionsAlgNode::run_grasp_actions(const GraspPhaseSequence & actions)
00156 {
00157     for (GraspPhaseSequence::const_iterator it = actions.begin(); it < actions.end(); it++)
00158     {
00159         // TODO: replace the swith by a nice std::map
00160         // TODO: not very happy with the cast system. Should think something
00161         // smarter
00162         switch((*it)->id_)
00163         {
00164             case SET_FINGER_POSITION:
00165                 moveFingers(static_cast<GraspPhaseSetFingers *>((* it).get())->finger_config_);
00166                 break;
00167             case MOVE_ARM:
00168                 moveArm(static_cast<GraspPhaseMoveArm *>((* it).get())->pose_);
00169                 break;
00170             default:
00171                 ROS_ERROR("Recieved an unknown GraspPhaseType. No action if performed");
00172         }
00173     }
00174 }
00175 
00176 void GraspActionsAlgNode::pickupStartCallback(const iri_wam_common_msgs::SimpleBhandPickUpGoalConstPtr& goal)
00177 {
00178     // Generate grasp actions from PickUpGoal and run them
00179     run_grasp_actions(grasp_generator_.generate(* goal));
00180 
00181     alg_.lock();
00182     status_ = 100;
00183     alg_.unlock();
00184 
00185     ROS_DEBUG("goal finished successfully, returning");
00186 }
00187 
00188 void GraspActionsAlgNode::pickupStopCallback(void) 
00189 { 
00190   alg_.lock(); 
00191     //stop action 
00192   alg_.unlock(); 
00193 } 
00194 
00195 bool GraspActionsAlgNode::pickupIsFinishedCallback(void) 
00196 { 
00197   bool ret = false; 
00198 
00199   alg_.lock(); 
00200     if(status_ == 100)
00201         ret = true; 
00202   alg_.unlock(); 
00203 
00204   return ret; 
00205 } 
00206 
00207 bool GraspActionsAlgNode::pickupHasSucceedCallback(void) 
00208 { 
00209   bool ret = false; 
00210 
00211   alg_.lock(); 
00212     if(status_ == 100)
00213         ret = true; 
00214   alg_.unlock(); 
00215 
00216   return ret; 
00217 } 
00218 
00219 void GraspActionsAlgNode::pickupGetResultCallback(iri_wam_common_msgs::SimpleBhandPickUpResultPtr& result) 
00220 { 
00221   alg_.lock(); 
00222     //update result data to be sent to client 
00223     //result->data = data; 
00224   alg_.unlock(); 
00225 } 
00226 
00227 void GraspActionsAlgNode::pickupGetFeedbackCallback(iri_wam_common_msgs::SimpleBhandPickUpFeedbackPtr& feedback) 
00228 { 
00229   alg_.lock(); 
00230     //keep track of feedback 
00231     //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00232   alg_.unlock(); 
00233 }
00234 
00235 /*  [action requests] */
00236 void GraspActionsAlgNode::node_config_update(Config &config, uint32_t level)
00237 {
00238     this->alg_.lock();
00239 
00240     ROS_INFO("Fingers configuration change. Pre-grasp: %s with name %s",
00241           config.pre_grasp_fingers_configuration.c_str(),
00242           config.grasp_fingers_configuration.c_str());
00243 
00244     pre_grasp_fingers_configuration_ = config.pre_grasp_fingers_configuration;
00245     grasp_fingers_configuration_     = config.grasp_fingers_configuration;
00246 
00247     this->alg_.unlock();
00248 }
00249 
00250 void GraspActionsAlgNode::addNodeDiagnostics(void)
00251 { }
00252 
00253 void GraspActionsAlgNode::open_gripperMakeActionRequest() 
00254 { 
00255     ROS_INFO("GraspActionsAlgNode::open_gripperMakeActionRequest: Starting New Request!"); 
00256 
00257     //wait for the action server to start 
00258     //will wait for infinite time 
00259     open_gripper_client_.waitForServer();  
00260     ROS_INFO("GraspActionsAlgNode::open_gripperMakeActionRequest: Server is Available!"); 
00261 
00262     //send a goal to the action 
00263     //open_gripper_goal_.data = my_desired_goal; 
00264     open_gripper_client_.sendGoal(open_gripper_goal_, 
00265                 boost::bind(&GraspActionsAlgNode::open_gripperDone,     this, _1, _2), 
00266                 boost::bind(&GraspActionsAlgNode::open_gripperActive,   this), 
00267                 boost::bind(&GraspActionsAlgNode::open_gripperFeedback, this, _1)); 
00268     ROS_INFO("GraspActionsAlgNode::open_gripperMakeActionRequest: Goal Sent. Wait for Result!"); 
00269 }
00270 
00271 void GraspActionsAlgNode::close_gripperMakeActionRequest() 
00272 { 
00273   ROS_DEBUG("GraspActionsAlgNode::close_gripperMakeActionRequest: Starting New Request!"); 
00274 
00275   //wait for the action server to start 
00276   //will wait for infinite time 
00277   close_gripper_client_.waitForServer();  
00278   ROS_DEBUG("GraspActionsAlgNode::close_gripperMakeActionRequest: Server is Available!"); 
00279 
00280   //send a goal to the action 
00281   //close_gripper_goal_.data = my_desired_goal; 
00282   close_gripper_client_.sendGoal(close_gripper_goal_, 
00283               boost::bind(&GraspActionsAlgNode::close_gripperDone,     this, _1, _2), 
00284               boost::bind(&GraspActionsAlgNode::close_gripperActive,   this), 
00285               boost::bind(&GraspActionsAlgNode::close_gripperFeedback, this, _1)); 
00286   ROS_DEBUG("GraspActionsAlgNode::close_gripperMakeActionRequest: Goal Sent. Wait for Result!"); 
00287 }
00288 
00289 /* [action callbacks] */
00290 void GraspActionsAlgNode::open_gripperDone(const actionlib::SimpleClientGoalState& state,  const iri_dynamixel_gripper::openResultConstPtr& result) 
00291 { 
00292     if( state.toString().compare("SUCCEEDED") == 0 ) 
00293         ROS_INFO("GraspActionsAlgNode::open_gripperDone: Goal Achieved!"); 
00294     else 
00295         ROS_WARN("GraspActionsAlgNode::open_gripperDone: %s", state.toString().c_str()); 
00296 } 
00297 void GraspActionsAlgNode::open_gripperActive() {}
00298 void GraspActionsAlgNode::open_gripperFeedback(const iri_dynamixel_gripper::openFeedbackConstPtr& feedback) {}
00299 
00300 void GraspActionsAlgNode::close_gripperDone(const actionlib::SimpleClientGoalState& state,  const iri_dynamixel_gripper::closeResultConstPtr& result) 
00301 { 
00302   if( state.toString().compare("SUCCEEDED") == 0 ) 
00303     ROS_DEBUG("GraspActionsAlgNode::close_gripperDone: Goal Achieved!"); 
00304   else 
00305     ROS_WARN("GraspActionsAlgNode::close_gripperDone: %s", state.toString().c_str()); 
00306 } 
00307 void GraspActionsAlgNode::close_gripperActive() {}
00308 void GraspActionsAlgNode::close_gripperFeedback(const iri_dynamixel_gripper::closeFeedbackConstPtr& feedback) {}


iri_grasp_actions
Author(s): pmonso
autogenerated on Fri Dec 6 2013 20:14:56