00001 #include <ros/ros.h> 00002 #include <pr2_controllers_msgs/Pr2GripperCommandAction.h> 00003 #include <actionlib/client/simple_action_client.h> 00004 00005 // Our Action interface type, provided as a typedef for convenience 00006 typedef actionlib::SimpleActionClient<pr2_controllers_msgs::Pr2GripperCommandAction> GripperClient; 00007 00008 class Gripper{ 00009 private: 00010 GripperClient* gripper_client_; 00011 00012 public: 00013 //Action client initialization 00014 Gripper(){ 00015 00016 //Initialize the client for the Action interface to the gripper controller 00017 //and tell the action client that we want to spin a thread by default 00018 gripper_client_ = new GripperClient("r_gripper_controller/gripper_action", true); 00019 00020 //wait for the gripper action server to come up 00021 while(!gripper_client_->waitForServer(ros::Duration(30.0))){ 00022 ROS_INFO("Waiting for the r_gripper_controller/gripper_action action server to come up"); 00023 } 00024 } 00025 00026 ~Gripper(){ 00027 delete gripper_client_; 00028 } 00029 00030 //Open the gripper 00031 void open(){ 00032 pr2_controllers_msgs::Pr2GripperCommandGoal open; 00033 open.command.position = 0.07; 00034 open.command.max_effort = 100.0; // use -1 to not limit effort (negative) 00035 00036 ROS_INFO("Sending open goal"); 00037 /* 00038 gripper_client_->sendGoal(open); 00039 gripper_client_->waitForResult(); 00040 if(gripper_client_->getState() == actionlib::SimpleClientGoalState::SUCCEEDED) 00041 */ 00042 if (gripper_client_->sendGoalAndWait(open,ros::Duration(10.0),ros::Duration(5.0)) == actionlib::SimpleClientGoalState::SUCCEEDED) 00043 ROS_INFO("The gripper opened!"); 00044 else 00045 ROS_INFO("The gripper failed to open."); 00046 } 00047 00048 //Close the gripper 00049 void close(){ 00050 pr2_controllers_msgs::Pr2GripperCommandGoal squeeze; 00051 squeeze.command.position = 0.0; 00052 squeeze.command.max_effort = 50.0; // Close gently 00053 00054 ROS_INFO("Sending squeeze goal"); 00055 /* 00056 gripper_client_->sendGoal(squeeze); 00057 gripper_client_->waitForResult(); 00058 if(gripper_client_->getState() == actionlib::SimpleClientGoalState::SUCCEEDED) 00059 */ 00060 gripper_client_->sendGoal(squeeze); 00061 if (gripper_client_->waitForResult(ros::Duration(10.0))) 00062 ROS_INFO("The gripper closed!"); 00063 else 00064 ROS_INFO("The gripper failed to close."); 00065 } 00066 }; 00067 00068 int main(int argc, char** argv){ 00069 ros::init(argc, argv, "simple_gripper"); 00070 00071 int param = -1; 00072 if (argc > 1) 00073 param = atoi(argv[1]); 00074 00075 Gripper gripper; 00076 00077 switch (param) 00078 { 00079 case 0: 00080 for (int i=0; i < 100; i++) 00081 { 00082 gripper.open(); 00083 gripper.close(); 00084 } 00085 break; 00086 case 1: 00087 gripper.open(); 00088 break; 00089 case 2: 00090 gripper.close(); 00091 break; 00092 default: 00093 gripper.open(); 00094 break; 00095 } 00096 00097 return 0; 00098 } 00099