GraspPlannerServer.cpp
Go to the documentation of this file.
00001 /*
00002  * GraspPlannerServer.cpp
00003  *
00004  *  Created on: Aug 9, 2012
00005  *      Author: jnicho
00006  */
00007 
00008 #include "object_manipulation_tools/services/GraspPlannerServer.h"
00009 #include <geometry_msgs/PolygonStamped.h>
00010 #include <geometry_msgs/PoseArray.h>
00011 #include <object_manipulation_msgs/GraspPlanning.h>
00012 #include <boost/foreach.hpp>
00013 
00014 GraspPlannerServer::GraspPlannerServer(boost::shared_ptr<GraspPlannerInterface> &plannerPtr):
00015 _GraspPlanner(plannerPtr),
00016 _ServiceName(SERVICE_NAME),
00017 _PublishResults(false),
00018 _PosePubTopic(POSE_PUBLISH_TOPIC_NAME),
00019 _PlanePubTopic(PLANE_PUBLISH_TOPIC_NAME),
00020 _PublishInterval(0.4f),
00021 _ResultsSet(false)
00022 {
00023         // TODO Auto-generated constructor stub
00024 }
00025 
00026 GraspPlannerServer::~GraspPlannerServer() {
00027         // TODO Auto-generated destructor stub
00028         finish();
00029 }
00030 
00031 void GraspPlannerServer::init()
00032 {
00033         ros::NodeHandle nh;
00034 
00035         //checking pointer to grasp planner
00036         if(_GraspPlanner == NULL)
00037         {
00038                 ROS_ERROR_STREAM("Grasp Planner Server: Grasp planner pointer is null, exiting");
00039                 ros::shutdown();
00040                 return;
00041         }
00042 
00043         // fetching parameters from ros
00044         std::string nameSpace = "/" + ros::this_node::getName();
00045         ros::param::param(nameSpace + "/" + PARAM_NAME_PUBLISH_RESULTS,_PublishResults,false);
00046 
00047         // setting up ros server
00048         _ServiceServer = nh.advertiseService(_ServiceName,&GraspPlannerServer::serviceCallback,this);
00049         ROS_INFO("%s",std::string(_GraspPlanner->getPlannerName() + " advertising service: " + _ServiceName).c_str());
00050 
00051         // setting up ros timer
00052         _PublishTimer = nh.createTimer(ros::Duration(_PublishInterval),&GraspPlannerServer::timerCallback,this);
00053 
00054         // setting up ros publishers
00055         _PlanePublisher = nh.advertise<geometry_msgs::PolygonStamped>(nameSpace + "/" + _PlanePubTopic,1);
00056         _PosePublisher = nh.advertise<geometry_msgs::PoseStamped>(nameSpace + "/" + _PosePubTopic,1);
00057         ros::spin();
00058 }
00059 
00060 void GraspPlannerServer::finish()
00061 {
00062 //      if(_GraspPlanner)
00063 //      {
00064 //              delete _GraspPlanner;
00065 //              _GraspPlanner = NULL;
00066 //      }
00067 }
00068 
00069 bool GraspPlannerServer::serviceCallback(object_manipulation_msgs::GraspPlanning::Request &req,
00070                 object_manipulation_msgs::GraspPlanning::Response &res)
00071 {
00072         if(_GraspPlanner)
00073         {
00074                 bool success = _GraspPlanner->planGrasp(req,res);
00075 
00076                 if(success)
00077                 {
00078                         std::string worldFrameId = req.target.reference_frame_id;
00079 
00080                         // storing last valid grasp pose
00081                         _LastValidGraspPoseMsg.header.frame_id = worldFrameId;
00082                         _LastValidGraspPoseMsg.pose = geometry_msgs::Pose();
00083 
00084                         if(res.grasps.size() > 0)
00085                         {
00086                                 _LastValidGraspPoseMsg.pose = res.grasps[0].grasp_pose;
00087                         }
00088 
00089                         // storing last valid contact plane grasp contact plane
00090                         tf::Transform transform = tf::Transform::getIdentity();
00091                         tf::poseMsgToTF(res.grasps[0].grasp_pose,transform);
00092                         _LastValidTablePolygonMsg.header.frame_id = worldFrameId;
00093                         _LastValidTablePolygonMsg.polygon = GraspPlannerServer::createPlanePolygon(transform,0.2f,0.2f);
00094 
00095                         _ResultsSet = true;
00096                 }
00097 
00098                 return success;
00099         }
00100         else
00101         {
00102                 return false;
00103         }
00104 }
00105 
00106 void GraspPlannerServer::timerCallback(const ros::TimerEvent &evnt)
00107 {
00108         // retrieving parameter from ros
00109         std::string nameSpace = "/" + ros::this_node::getName();
00110         ros::param::param(nameSpace + "/" + PARAM_NAME_PUBLISH_RESULTS,_PublishResults,_PublishResults);
00111 
00112         if(_PublishResults && _ResultsSet)
00113         {
00114                 ros::Time pubTime = ros::Time::now();
00115                 _LastValidGraspPoseMsg.header.stamp = pubTime;
00116                 _LastValidTablePolygonMsg.header.stamp = pubTime;
00117 
00118                 _PosePublisher.publish(_LastValidGraspPoseMsg);
00119                 _PlanePublisher.publish(_LastValidTablePolygonMsg);
00120         }
00121 }
00122 
00123 geometry_msgs::Polygon GraspPlannerServer::createPlanePolygon(const tf::Transform &transform,tfScalar width, tfScalar length)
00124 {
00125         geometry_msgs::Polygon polygon = geometry_msgs::Polygon();
00126         tf::Vector3 point;
00127         std::vector<tf::Vector3> planePoints = std::vector<tf::Vector3>();
00128         planePoints.push_back(transform(tf::Vector3(width/2,length/2,0)));
00129         planePoints.push_back(transform(tf::Vector3(width/2,-length/2,0)));
00130         planePoints.push_back(transform(tf::Vector3(-width/2,-length/2,0)));
00131         planePoints.push_back(transform(tf::Vector3(-width/2,length/2,0)));
00132 
00133         polygon.points = std::vector<geometry_msgs::Point32>();
00134         BOOST_FOREACH(tf::Vector3 v,planePoints)
00135         {
00136                 geometry_msgs::Point32 p;
00137                 p.x = v.x();
00138                 p.y = v.y();
00139                 p.z = v.z();
00140                 polygon.points.push_back(p);
00141         }
00142         return polygon;
00143 }
00144 
00145 //int main(int argc, char** argv)
00146 //{
00147 //      ros::init(argc,argv,"grasp_planner_server");
00148 //      ros::NodeHandle nh;
00149 //
00150 //      GraspPlannerServer graspPlannerServer = GraspPlannerServer();
00151 //      graspPlannerServer.init();
00152 //
00153 //      return 0;
00154 //}


object_manipulation_tools
Author(s): Jnicho
autogenerated on Mon Oct 6 2014 00:56:17