Go to the documentation of this file.00001 #include <carl_demos/specific_actions.h>
00002
00003 using namespace std;
00004
00005 SpecificActions::SpecificActions() :
00006 armClient("carl_moveit_wrapper/common_actions/arm_action"),
00007 pickupClient("carl_moveit_wrapper/common_actions/pickup"),
00008 storeClient("carl_moveit_wrapper/common_actions/store"),
00009 obtainObjectServer(n, "carl_demos/obtain_object", boost::bind(&SpecificActions::executeObtainObject, this, _1), false)
00010 {
00011 recognizedObjectsCounter = 0;
00012
00013 recognizedObjectsSubscriber = n.subscribe("/object_recognition_listener/recognized_objects", 1, &SpecificActions::recognizedObjectsCallback, this);
00014
00015 segmentClient = n.serviceClient<std_srvs::Empty>("/rail_segmentation/segment");
00016
00017
00018 obtainObjectServer.start();
00019 }
00020
00021 void SpecificActions::executeObtainObject(const carl_demos::ObtainObjectGoalConstPtr &goal)
00022 {
00023 carl_demos::ObtainObjectFeedback feedback;
00024 carl_demos::ObtainObjectResult result;
00025 result.success = false;
00026
00027
00028 rail_manipulation_msgs::ArmGoal retractGoal;
00029 retractGoal.action = rail_manipulation_msgs::ArmGoal::RETRACT;
00030 armClient.sendGoal(retractGoal);
00031 bool completed = armClient.waitForResult(ros::Duration(20.0));
00032 bool succeeded = (armClient.getState() == actionlib::SimpleClientGoalState::SUCCEEDED);
00033 bool success = armClient.getResult()->success;
00034 if (!completed || !succeeded || !success)
00035 {
00036 ROS_INFO("Could not retract arm for segmentation.");
00037 obtainObjectServer.setSucceeded(result, "Could not retract arm during search.");
00038 return;
00039 }
00040
00041
00042 feedback.message = "Attempting to segment the surface.";
00043 obtainObjectServer.publishFeedback(feedback);
00044 std_srvs::Empty segment;
00045 recognizedObjectsCounter = 0;
00046 if (!segmentClient.call(segment))
00047 {
00048 ROS_INFO("Could not call segment service.");
00049 obtainObjectServer.setSucceeded(result, "Could not call segment service.");
00050 return;
00051 }
00052
00053
00054 feedback.message = "Waiting for recognition results.";
00055 ROS_INFO("Waiting on recognition...");
00056 obtainObjectServer.publishFeedback(feedback);
00057 bool finished = false;
00058 while (!finished)
00059 {
00060 {
00061 boost::mutex::scoped_lock lock(recognizedObjectsMutex);
00062 finished = recognizedObjectsCounter == 2;
00063 }
00064 }
00065
00066
00067 string objectName = boost::to_upper_copy(goal->object_name);
00068 bool pickupSucceeded = false;
00069 ROS_INFO("Looking for object %s...", objectName.c_str());
00070 for (unsigned int i = 0; i < recognizedObjects.objects.size(); i ++)
00071 {
00072 if (recognizedObjects.objects[i].name == objectName)
00073 {
00074 ROS_INFO("Found object! Attempting pickup...");
00075 rail_manipulation_msgs::PickupGoal pickupGoal;
00076 pickupGoal.lift = goal->lift;
00077 pickupGoal.verify = goal->verify;
00078
00079 for (unsigned int j = 0; j < recognizedObjects.objects[i].grasps.size(); j ++)
00080 {
00081 ROS_INFO("ATTEMPTING PICKUP WITH GRASP %d", j);
00082 pickupGoal.pose = recognizedObjects.objects[i].grasps[j].grasp_pose;
00083 pickupClient.sendGoal(pickupGoal);
00084 pickupClient.waitForResult(ros::Duration(30.0));
00085
00086 rail_manipulation_msgs::PickupResultConstPtr pickupResult = pickupClient.getResult();
00087 if (!pickupResult->success)
00088 {
00089 ROS_INFO("PICKUP FAILED, moving on to a new grasp...");
00090 }
00091 else
00092 {
00093 ROS_INFO("PICKUP SUCCEEDED");
00094 pickupSucceeded = true;
00095 break;
00096 }
00097 }
00098
00099 if (pickupSucceeded)
00100 break;
00101 }
00102 }
00103
00104 if (!pickupSucceeded)
00105 {
00106 ROS_INFO("Could not find or pickup the specified object.");
00107 obtainObjectServer.setSucceeded(result, "Could not find or pickup the specified object.");
00108 return;
00109 }
00110
00111
00112 rail_manipulation_msgs::StoreGoal storeGoal;
00113 storeClient.sendGoal(storeGoal);
00114 storeClient.waitForResult(ros::Duration(30.0));
00115 success = storeClient.getResult()->success;
00116 if (!success)
00117 {
00118 ROS_INFO("Could not store object.");
00119 obtainObjectServer.setSucceeded(result, "Could not store object.");
00120 return;
00121 }
00122
00123 ROS_INFO("Finished obtaining object successfully!");
00124 result.success = true;
00125 obtainObjectServer.setSucceeded(result);
00126 }
00127
00128 void SpecificActions::recognizedObjectsCallback(const rail_manipulation_msgs::SegmentedObjectList &objects)
00129 {
00130 boost::mutex::scoped_lock lock(recognizedObjectsMutex);
00131
00132 recognizedObjects = objects;
00133 recognizedObjectsCounter++;
00134 }
00135
00136 int main(int argc, char **argv)
00137 {
00138 ros::init(argc, argv, "specific_actions");
00139
00140 SpecificActions sa;
00141
00142 ros::spin();
00143 }