test.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <actionlib/client/simple_action_client.h>
00003 
00004 #include <move_arm_msgs/MoveArmAction.h>
00005 #include <move_arm_msgs/utils.h>
00006 
00007 #include <nav_msgs/Odometry.h>
00008 #include <boost/thread.hpp>
00009 
00010 #include "LinearMath/btTransform.h"
00011 #include "LinearMath/btVector3.h"
00012 #include <tf/transform_listener.h>
00013 
00014 
00015 // for opening gripper
00016 #include <pr2_controllers_msgs/Pr2GripperCommandAction.h>
00017 #include <actionlib/client/simple_action_client.h>
00018 
00019 // Our Action interface type, provided as a typedef for convenience
00020 typedef actionlib::SimpleActionClient<pr2_controllers_msgs::Pr2GripperCommandAction> GripperClient;
00021 
00022 class Gripper{
00023 private:
00024   GripperClient* gripper_client_;  
00025 
00026 public:
00027   //Action client initialization
00028   Gripper(){
00029 
00030     //Initialize the client for the Action interface to the gripper controller
00031     //and tell the action client that we want to spin a thread by default
00032     gripper_client_ = new GripperClient("r_gripper_controller/gripper_action", true);
00033     
00034     //wait for the gripper action server to come up 
00035     while(!gripper_client_->waitForServer(ros::Duration(5.0))){
00036       ROS_INFO("Waiting for the r_gripper_controller/gripper_action action server to come up");
00037     }
00038   }
00039 
00040   ~Gripper(){
00041     delete gripper_client_;
00042   }
00043 
00044   //Open the gripper
00045   void open(){
00046     pr2_controllers_msgs::Pr2GripperCommandGoal open;
00047     open.command.position = 0.08;
00048     open.command.max_effort = -1.0;  // Do not limit effort (negative)
00049     
00050     ROS_INFO("Sending open goal");
00051     gripper_client_->sendGoal(open);
00052     gripper_client_->waitForResult();
00053     if(gripper_client_->getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
00054       ROS_INFO("The gripper opened!");
00055     else
00056       ROS_INFO("The gripper failed to open.");
00057   }
00058 
00059   //Close the gripper
00060   void close(){
00061     pr2_controllers_msgs::Pr2GripperCommandGoal squeeze;
00062     squeeze.command.position = 0.0;
00063     squeeze.command.max_effort = 50.0;  // Close gently
00064     
00065     ROS_INFO("Sending squeeze goal, no block, just continue");
00066     gripper_client_->sendGoal(squeeze);
00067 /*
00068     ROS_INFO("Sending squeeze goal, wait for 10 seconds, check for stall, then continue");
00069     if(gripper_client_->sendGoalAndWait(squeeze,ros::Duration(10.0), ros::Duration(5.0)) == actionlib::SimpleClientGoalState::SUCCEEDED)
00070       ROS_INFO("The gripper closed!");
00071     else
00072     {
00073       if (gripper_client_->getResult()->stalled)
00074         ROS_INFO("The gripper failed to close and is stalled, possibly holding an object.");
00075       else
00076         ROS_INFO("The gripper failed to close and is not stalled, something is wrong.");
00077     }
00078 */
00079   }
00080 };
00081 
00082 class objectPose
00083 {
00084     public:
00085       bool got_pose_;
00086       nav_msgs::Odometry pose_;
00087       ros::Subscriber sub_;
00088 
00089       objectPose(ros::NodeHandle& nh,std::string topic)
00090       {
00091         this->got_pose_ = false;
00092         this->sub_ =  nh.subscribe(topic, 1, &objectPose::poseCallback, this);
00093       };
00094       void poseCallback(const nav_msgs::OdometryConstPtr& pose)
00095       {
00096          this->pose_ = *pose;
00097          this->got_pose_ = true;
00098       };
00099 };
00100 
00101 
00102 int main(int argc, char **argv){
00103   ros::init (argc, argv, "move_arm_pose_goal_test");
00104   ros::NodeHandle nh;
00105 
00106   // open gripper
00107   //Gripper gripper;
00108   //gripper.close();
00109 
00110   // spin some threaded spinners
00111   ros::MultiThreadedSpinner s(4);
00112   boost::thread* ros_spinner = new boost::thread(boost::bind(&ros::spin,s));
00113 
00114 
00115 
00116 
00117 
00118 
00119   // offset pose by transform from r_wrist_roll_link to r_gripper_tool_frame
00120   // get the fixed transform from r_wrist_roll_link to r_gripper_tool_frame
00121   std::string target_frame = "r_gripper_tool_frame";
00122   std::string command_link = "r_wrist_roll_link";
00123   ROS_INFO("waiting for transform from %s to %s",command_link.c_str(),target_frame.c_str());
00124   tf::TransformListener listener;
00125   tf::StampedTransform target_to_command_link; // tool frame in wrist_tool_link frame
00126   listener.waitForTransform(target_frame,command_link,ros::Time(),ros::Duration(100.0));
00127   listener.lookupTransform(target_frame,command_link,ros::Time(0),target_to_command_link);
00128 
00129 
00130 
00131   // getting outlet pose from sim
00132   objectPose ocb(nh,"/outlet/outlet_pose_ground_truth");
00133   ROS_INFO("waiting for /outlet/outlet_pose_ground_truth from sim");
00134   while (!ocb.got_pose_)
00135   {
00136     ROS_DEBUG("checking for outlet pose (%d)",  ocb.got_pose_ );
00137     ros::Duration(1.0).sleep();
00138   }
00139   // get reference frame of outlet_pose
00140   std::string outlet_reference_frame = ocb.pose_.header.frame_id;
00141 
00142   // set goal message
00143   btTransform outlet_transform(btQuaternion(ocb.pose_.pose.pose.orientation.x,
00144                                             ocb.pose_.pose.pose.orientation.y,
00145                                             ocb.pose_.pose.pose.orientation.z,
00146                                             ocb.pose_.pose.pose.orientation.w),
00147                                btVector3(ocb.pose_.pose.pose.position.x,
00148                                          ocb.pose_.pose.pose.position.y,
00149                                          ocb.pose_.pose.pose.position.z));
00150   //outlet_transform =  outlet_transform*(btTransform)target_to_command_link; 
00151 
00152 
00153   /*****************************************************/
00154   /*                                                   */
00155   /*   goalC: goal pose                                */
00156   /*                                                   */
00157   /*****************************************************/
00158   actionlib::SimpleActionClient<move_arm_msgs::MoveArmAction> move_arm("move_right_arm",true);
00159   ROS_INFO("Connecting to server");
00160   move_arm.waitForServer();
00161 
00162   move_arm_msgs::MoveArmGoal goalC;
00163 
00164   goalC.group_name = "right_arm";
00165   goalC.num_planning_attempts = 1;
00166   goalC.planner_id = std::string("");
00167   goalC.planner_service_name = std::string("ompl_planning/plan_kinematic_path");
00168   goalC.allowed_planning_time = 5.0;
00169   
00170   motion_planning_msgs::SimplePoseConstraint desired_poseC;
00171 
00172   desired_poseC.header = ocb.pose_.header;
00173   desired_poseC.link_name = command_link;
00174   desired_poseC.pose.position.x = outlet_transform.getOrigin().x()-0.30;
00175   desired_poseC.pose.position.y = outlet_transform.getOrigin().y();
00176   desired_poseC.pose.position.z = outlet_transform.getOrigin().z();
00177   desired_poseC.pose.orientation.x = outlet_transform.getRotation().x();
00178   desired_poseC.pose.orientation.y = outlet_transform.getRotation().y();
00179   desired_poseC.pose.orientation.z = outlet_transform.getRotation().z();
00180   desired_poseC.pose.orientation.w = outlet_transform.getRotation().w();
00181 
00182   std::cout << "pose " << std::endl
00183             << "frame " << desired_poseC.header.frame_id << std::endl
00184             << ", " << desired_poseC.pose.position.x
00185             << ", " << desired_poseC.pose.position.y
00186             << ", " << desired_poseC.pose.position.z
00187             << ", " << desired_poseC.pose.orientation.x
00188             << ", " << desired_poseC.pose.orientation.y
00189             << ", " << desired_poseC.pose.orientation.z
00190             << ", " << desired_poseC.pose.orientation.w
00191             << std::endl;
00192 
00193   // additional parameters
00194   desired_poseC.absolute_position_tolerance.x = 0.02;
00195   desired_poseC.absolute_position_tolerance.y = 0.02;
00196   desired_poseC.absolute_position_tolerance.z = 0.02;
00197 
00198   desired_poseC.absolute_roll_tolerance = 0.04;
00199   desired_poseC.absolute_pitch_tolerance = 0.04;
00200   desired_poseC.absolute_yaw_tolerance = 0.04;
00201   
00202   ROS_INFO("sending final pose goal to server");
00203   move_arm_msgs::addGoalConstraintToMoveArmGoal(desired_poseC,goalC);
00204 
00205   // add another goal
00206 
00207   if (nh.ok())
00208   {
00209     bool finished_within_time = false;
00210     move_arm.sendGoal(goalC);
00211     finished_within_time = move_arm.waitForResult(ros::Duration(200.0));
00212     if (!finished_within_time)
00213     {
00214       move_arm.cancelGoal();
00215       ROS_INFO("Timed out achieving goal C");
00216     }
00217     else
00218     {
00219       actionlib::SimpleClientGoalState state = move_arm.getState();
00220       bool success = (state == actionlib::SimpleClientGoalState::SUCCEEDED);
00221       if(success)
00222         ROS_INFO("Action finished: %s",state.toString().c_str());
00223       else
00224         ROS_INFO("Action failed: %s",state.toString().c_str());
00225     }
00226   }
00227 
00228   ros::shutdown();
00229   ros_spinner->join();
00230 }


pr2_plugs_gazebo_demo
Author(s): John Hsu
autogenerated on Mon Jan 6 2014 12:04:30