PointAndClick.cpp
Go to the documentation of this file.
00001 #include <remote_manipulation_markers/PointAndClick.h>
00002 
00003 using namespace std;
00004 
00005 PointAndClick::PointAndClick() :
00006     pnh("~"), specifiedGraspServer(pnh, "execute_grasp", boost::bind(&PointAndClick::executeGraspCallback, this, _1), false)
00007 {
00008   //read in parameters
00009   string graspTopic;
00010   string calculatedPosesTopic;
00011   pnh.param<string>("grasp_topic", graspTopic, "grasp");
00012   pnh.param<string>("calculated_poses_topic", calculatedPosesTopic, "grasp_sampler/sampled_grasps");
00013 
00014   //messages
00015   graspsSubscriber = n.subscribe(calculatedPosesTopic, 1, &PointAndClick::graspsCallback, this);
00016 
00017   //services
00018   cycleGraspsServer = pnh.advertiseService("cycle_grasps", &PointAndClick::cycleGraspsCallback, this);
00019 
00020   //actionlib
00021   graspClient = new actionlib::SimpleActionClient<rail_manipulation_msgs::PickupAction>(graspTopic);
00022 
00023   imServer.reset( new interactive_markers::InteractiveMarkerServer("grasp_selector", "grasp_selector_server", false));
00024   ros::Duration(0.1).sleep();
00025   imServer->applyChanges();
00026 
00027   graspsReceived = false;
00028   graspIndex = 0;
00029 
00030   specifiedGraspServer.start();
00031 }
00032 
00033 PointAndClick::~PointAndClick()
00034 {
00035   delete graspClient;
00036 }
00037 
00038 void PointAndClick::graspsCallback(const geometry_msgs::PoseArray &grasps)
00039 {
00040   graspList = grasps;
00041   graspIndex = 0;
00042 
00043   updateMarker();
00044 
00045   graspsReceived = true;
00046 }
00047 
00048 void PointAndClick::updateMarker()
00049 {
00050   geometry_msgs::PoseStamped pose;
00051   pose.header.frame_id = graspList.header.frame_id;
00052   pose.pose = graspList.poses[graspIndex];
00053 
00054   visualization_msgs::InteractiveMarker gripperMarker;
00055   if (imServer->get("gripper", gripperMarker))
00056   {
00057     //update gripper marker pose
00058     imServer->setPose("gripper", pose.pose, pose.header);
00059   }
00060   else
00061   {
00062     //create new gripper marker
00063     gripperMarker = Common::makeGripperMarker(pose);
00064     imServer->insert(gripperMarker);
00065   }
00066   imServer->applyChanges();
00067 }
00068 
00069 void PointAndClick::executeGraspCallback(const remote_manipulation_markers::SpecifiedGraspGoalConstPtr &goal)
00070 {
00071   remote_manipulation_markers::SpecifiedGraspResult result;
00072   remote_manipulation_markers::SpecifiedGraspFeedback feedback;
00073 
00074   feedback.message = "The robot is attempting to move to your selected grasp.";
00075   specifiedGraspServer.publishFeedback(feedback);
00076 
00077   tf::Transform graspTransform;
00078   graspTransform.setOrigin(tf::Vector3(graspList.poses[graspIndex].position.x, graspList.poses[graspIndex].position.y, graspList.poses[graspIndex].position.z));
00079   graspTransform.setRotation(tf::Quaternion(graspList.poses[graspIndex].orientation.x, graspList.poses[graspIndex].orientation.y, graspList.poses[graspIndex].orientation.z, graspList.poses[graspIndex].orientation.w));
00080   ros::Time now = ros::Time::now();
00081   tfBroadcaster.sendTransform(tf::StampedTransform(graspTransform, now, graspList.header.frame_id, "selected_grasp_frame"));
00082   tfListener.waitForTransform("selected_grasp_frame", graspList.header.frame_id, now, ros::Duration(5.0));
00083 
00084   rail_manipulation_msgs::PickupGoal graspGoal;
00085   graspGoal.pose.header.frame_id = "selected_grasp_frame";
00086   graspGoal.pose.pose.position.x = goal->depthOffset;
00087   graspGoal.pose.pose.orientation.w = 1.0;
00088   graspGoal.lift = false;
00089   graspGoal.verify = false;
00090   graspClient->sendGoal(graspGoal);
00091   graspClient->waitForResult(ros::Duration(30.0));
00092   rail_manipulation_msgs::PickupResultConstPtr graspResult = graspClient->getResult();
00093   result.success = graspResult->success;
00094   result.executionSuccess = graspResult->executionSuccess;
00095   if (!graspResult->executionSuccess)
00096   {
00097     ROS_INFO("Grasp failed!");
00098     feedback.message = "The robot failed to plan to your selected grasp.";
00099   }
00100   else
00101   {
00102     ROS_INFO("Grasp succeeded.");
00103     feedback.message = "Grasp successful!";
00104   }
00105   specifiedGraspServer.publishFeedback(feedback);
00106   specifiedGraspServer.setSucceeded(result);
00107 }
00108 
00109 bool PointAndClick::cycleGraspsCallback(remote_manipulation_markers::CycleGrasps::Request &req, remote_manipulation_markers::CycleGrasps::Response &res)
00110 {
00111   if (req.forward)
00112   {
00113     if (graspIndex < graspList.poses.size() - 1)
00114       cycleGraspsForward();
00115   }
00116   else
00117   {
00118     if (graspIndex > 0)
00119     cycleGraspsBackward();
00120   }
00121 
00122   res.grasp.header.frame_id = graspList.header.frame_id;
00123   res.grasp.pose = graspList.poses[graspIndex];
00124   res.index = graspIndex;
00125 }
00126 
00127 void PointAndClick::cycleGraspsForward()
00128 {
00129   graspIndex ++;
00130   updateMarker();
00131 }
00132 
00133 void PointAndClick::cycleGraspsBackward()
00134 {
00135   graspIndex --;
00136   updateMarker();
00137 }
00138 
00139 int main(int argc, char **argv)
00140 {
00141   ros::init(argc, argv, "point_and_click");
00142 
00143   PointAndClick pac;
00144 
00145   ros::spin();
00146 }


remote_manipulation_markers
Author(s): David Kent
autogenerated on Thu Jun 6 2019 22:05:39