grasp_server.cpp
Go to the documentation of this file.
00001 #include "../include/grasp_server.h"
00002 #include <tf/tf.h>
00003 #include <geometry_msgs/PointStamped.h>
00004 #include "icr_msgs/ContactPoint.h"
00005 #include "icr_msgs/Grasp.h"
00006 #include "icr_msgs/StampedContactPose.h" //REMOVE
00007 #include <geometry_msgs/PoseStamped.h>//REMOVE
00008 
00009 namespace ICR
00010 {
00011 
00012   GraspServer::GraspServer() : nh_private_("~"), ref_set_(false)
00013 {
00014   std::string searched_param;
00015   XmlRpc::XmlRpcValue phalange_config;
00016   XmlRpc::XmlRpcValue L_T_Cref;
00017 
00018   palm_pose_.frame_id_="/default";
00019   palm_pose_.child_frame_id_="/default";
00020 
00021   if(nh_private_.searchParam("palm_frame_id", searched_param))
00022     nh_private_.getParam(searched_param, palm_pose_.child_frame_id_);
00023   else
00024     ROS_WARN("No palm frame id found on the parameter server - using %s",palm_pose_.child_frame_id_.c_str()); 
00025 
00026   if (nh_private_.searchParam("phalanges", searched_param))
00027     {
00028       nh_.getParam(searched_param,phalange_config);
00029       ROS_ASSERT(phalange_config.getType() == XmlRpc::XmlRpcValue::TypeArray); 
00030     
00031       for (int32_t i = 0; i < phalange_config.size(); ++i) 
00032         {    
00033           L_T_Cref=phalange_config[i]["L_T_Cref"];
00034           ROS_ASSERT(phalange_config[i]["phl_frame_id"].getType() == XmlRpc::XmlRpcValue::TypeString);
00035           ROS_ASSERT(phalange_config[i]["phl_name"].getType() == XmlRpc::XmlRpcValue::TypeString);
00036           ROS_ASSERT(phalange_config[i]["sensor_topic"].getType() == XmlRpc::XmlRpcValue::TypeString);
00037           ROS_ASSERT(L_T_Cref.getType() == XmlRpc::XmlRpcValue::TypeArray);
00038           ROS_ASSERT(L_T_Cref.size()==6);//a transformation is specified by an offset vector + RPY values
00039           for (int32_t j = 0; j <L_T_Cref.size();j++) 
00040             ROS_ASSERT(L_T_Cref[j].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00041 
00042           tf::Transform tf;
00043           tf.setOrigin(tf::Vector3(L_T_Cref[0],L_T_Cref[1],L_T_Cref[2]));
00044           tf.setRotation(tf::createQuaternionFromRPY(L_T_Cref[3],L_T_Cref[4],L_T_Cref[5]));
00045 
00046           phalanges_.push_back(new Phalange(tf,(std::string)phalange_config[i]["phl_name"],(std::string)phalange_config[i]["phl_frame_id"] ,(std::string)phalange_config[i]["sensor_topic"]));
00047 
00048          }
00049     }
00050   else
00051     {
00052       ROS_ERROR("The hand phalange configurations are not specified - cannot start the Grasp Server");
00053       exit(0);
00054     }
00055 
00056   set_obj_srv_ = nh_.advertiseService("set_object",&GraspServer::setObject,this);
00057   grasp_pub_=nh_.advertise<icr_msgs::Grasp>("grasp",5); 
00058   //debug_pub_=nh_.advertise<geometry_msgs::PoseStamped>("debug_ff",5);  
00059 }
00060 //-----------------------------------------------------------------------------------------------
00061 GraspServer::~GraspServer()
00062 {
00063   for (unsigned int i=0; i<phalanges_.size();i++)
00064     delete phalanges_[i];
00065 
00066 }
00067 //-----------------------------------------------------------------------------------------------
00068 bool GraspServer::setObject(icr_msgs::SetObject::Request  &req, icr_msgs::SetObject::Response &res)
00069 {
00070   res.success=false;
00071   lock_.lock();
00072 
00073   //Transmit the new target object to the Phalanges
00074   for (unsigned int i=0;i<phalanges_.size();i++)
00075     phalanges_[i]->setTargetObjFrameId(req.object.points.header.frame_id);
00076    
00077  palm_pose_.frame_id_=req.object.points.header.frame_id;
00078  ref_set_=true;
00079 
00080   lock_.unlock();
00081   res.success=true;
00082   ROS_INFO("%s set as reference frame id in the grasp_server ", req.object.points.header.frame_id.c_str());
00083   return res.success;
00084 }
00085   //-----------------------------------------------------------------------------------------------
00086   void GraspServer::spin()
00087   {
00088     icr_msgs::Grasp g;
00089     icr_msgs::ContactPoint p;
00090 
00091     //Get the current contact points from the Phalanges and publish them
00092     for(unsigned int i = 0; i < phalanges_.size();i++)
00093       {
00094         p.position=phalanges_[i]->getStampedContactPose()->contact_pose.pose.position;
00095         p.touching=phalanges_[i]->getStampedContactPose()->contact_pose.touching;
00096         p.phalange=phalanges_[i]->getPhalangeName();
00097         g.points.push_back(p);
00098       }
00099 
00100     //Try to get the palm pose expressed in the reference frame id, if the reference is set
00101     if(ref_set_)
00102       try
00103         {
00104           tf_list_.waitForTransform(palm_pose_.frame_id_,palm_pose_.child_frame_id_,ros::Time(0),ros::Duration(0.5));
00105           tf_list_.lookupTransform(palm_pose_.frame_id_,palm_pose_.child_frame_id_,ros::Time(0), palm_pose_);
00106           tf::Vector3 pos=palm_pose_.getOrigin();
00107           tf::Quaternion ori=palm_pose_.getRotation().normalize();  
00108           g.palm_pose.position.x=pos.x();
00109           g.palm_pose.position.y=pos.y();
00110           g.palm_pose.position.z=pos.z();
00111           g.palm_pose.orientation.x=ori.x();
00112           g.palm_pose.orientation.y=ori.y();
00113           g.palm_pose.orientation.z=ori.z();
00114           g.palm_pose.orientation.w=ori.w();
00115         }
00116       catch(tf::TransformException ex)
00117         {
00118           ROS_ERROR("%s",ex.what());
00119         }
00120 
00121     g.header.frame_id=palm_pose_.frame_id_;
00122     g.header.stamp=palm_pose_.stamp_;
00123     grasp_pub_.publish(g);
00124 
00125     // geometry_msgs::PoseStamped dbg;
00126     // dbg.header=phalanges_[1]->getStampedContactPose()->header;
00127     // dbg.pose=phalanges_[1]->getStampedContactPose()->contact_pose.pose;
00128     // debug_pub_.publish(dbg);
00129     ros::spinOnce();
00130   }
00131 //-----------------------------------------------------------------------------------------------
00132 }//end namespace


icr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:36:10