graspObject_top_approach.cpp
Go to the documentation of this file.
00001 #include "rail_grasping/graspObject.h"
00002 
00003 using namespace std;
00004 
00005 graspObject::graspObject() :
00006     acPickup("jaco_arm/manipulation/pickup"),
00007     acGrasp("jaco_arm/manipulation/grasp"),
00008     acJointTrajectory("jaco_arm/joint_velocity_controller/trajectory"),
00009     acMoveArm("carl_moveit_wrapper/move_to_pose")
00010 {
00011   ROS_INFO("Starting rail grasping...");
00012   graspTransform.setOrigin(tf::Vector3(0.0, 0.0, 0.0));
00013   graspTransform.setRotation(tf::Quaternion(0, 0, 0, 1));
00014 
00015   cartesianCommandPub = n.advertise<wpi_jaco_msgs::CartesianCommand>("jaco_arm/cartesian_cmd", 1);
00016 
00017   armJointSubscriber = n.subscribe("jaco_arm/joint_states", 1, &graspObject::armJointStatesCallback, this);
00018 
00019   IKClient = n.serviceClient<carl_moveit::CallIK>("carl_moveit_wrapper/call_ik");
00020   cartesianPositionClient = n.serviceClient<wpi_jaco_msgs::GetCartesianPosition>("jaco_arm/get_cartesian_position");
00021   cartesianPathClient = n.serviceClient<carl_moveit::CartesianPath>("carl_moveit_wrapper/cartesian_path");
00022 
00023   jointNamesSet = false;
00024   armJointPos.resize(NUM_JACO_JOINTS);
00025   armJointNames.resize(NUM_JACO_JOINTS);
00026 
00027   while (!acPickup.waitForServer(ros::Duration(5.0)) &&
00028       !acGrasp.waitForServer(ros::Duration(5.0)) &&
00029       !acJointTrajectory.waitForServer(ros::Duration(5.0)))
00030   {
00031     ROS_INFO("Waiting for grasp, pickup, and arm trajectory action servers...");
00032   }
00033 
00034   requestGraspServer = n.advertiseService("rail_grasping/request_grasp", &graspObject::requestGrasp, this);
00035   requestReleaseServer = n.advertiseService("rail_grasping/request_release", &graspObject::requestRelease, this);
00036 
00037   ROS_INFO("Rail grasping started.");
00038 }
00039 
00040 void graspObject::armJointStatesCallback(const sensor_msgs::JointState &msg)
00041 {
00042   for (unsigned int i = 0; i < NUM_JACO_JOINTS; i++)
00043   {
00044     armJointPos[i] = msg.position[i];
00045   }
00046 
00047   if (!jointNamesSet)
00048   {
00049     for (unsigned int i = 0; i < NUM_JACO_JOINTS; i++)
00050     {
00051       armJointNames[i] = msg.name[i];
00052     }
00053     jointNamesSet = true;
00054   }
00055 }
00056 
00057 bool graspObject::requestRelease(rail_grasping::RequestGrasp::Request & req, rail_grasping::RequestGrasp::Response & res)
00058 {
00059   ROS_INFO("Received new release request...");
00060   //set release position
00061   /*
00062   graspTransform.setOrigin(tf::Vector3(req.graspPose.position.x, req.graspPose.position.y, req.graspPose.position.z));
00063   graspTransform.setRotation(tf::Quaternion(req.graspPose.orientation.x, req.graspPose.orientation.y, req.graspPose.orientation.z, req.graspPose.orientation.w));
00064   tfBroadcaster.sendTransform(tf::StampedTransform(graspTransform, ros::Time::now(), "base_footprint", "grasp_frame"));
00065   
00066   ros::Duration(1).sleep();
00067   
00068   //*********** Align gripper on approach angle ***********
00069   //Calculate pose on approach angle
00070   geometry_msgs::PoseStamped approachPose;
00071   geometry_msgs::PoseStamped poseOut;
00072   //approachPose.header.stamp = ros::Time::now();
00073   approachPose.header.frame_id = "grasp_frame";
00074   approachPose.pose.position.x = 0.0;
00075   approachPose.pose.position.y = 0.0;
00076   //approachPose.pose.position.z = 0.1; //note: disabled to see how close the arm will plan to
00077   approachPose.pose.position.z = 0.0;
00078   approachPose.pose.orientation.x = 0.0;
00079   approachPose.pose.orientation.y = 0.0;
00080   approachPose.pose.orientation.z = 0.0;
00081   approachPose.pose.orientation.w = 1.0;
00082   
00083   //adjustment to put approach pose from centered at fingers to jaco_link_hand
00084   approachPose.pose.position.z += .19;
00085   
00086   tfListener.transformPose("base_footprint", approachPose, poseOut);
00087   
00089   poseOut.pose.position.z += .05;
00091   */
00092 
00093   //Send goal to move arm service
00094 
00095   //try
00096   geometry_msgs::Pose releasePose = req.graspPose;
00097   releasePose.position.z += .05;
00098   //end try
00099 
00100   bool success = false;
00101   int counter = 0;
00102   while (!success)
00103   {
00104     ROS_INFO("Moving to approach angle...");
00105     carl_moveit::MoveToPoseGoal movePoseGoal;
00106     movePoseGoal.pose = releasePose;
00107     switch (counter)
00108     {
00109       case 0:
00110         movePoseGoal.pose.position.x += .005;
00111         break;
00112       case 1:
00113         movePoseGoal.pose.position.x -= .005;
00114         break;
00115       case 2:
00116         movePoseGoal.pose.position.y += .005;
00117         break;
00118       case 3:
00119         movePoseGoal.pose.position.y -= .005;
00120         break;
00121       case 4:
00122         movePoseGoal.pose.position.z += .005;
00123         break;
00124       case 5:
00125         movePoseGoal.pose.position.z -= .005;
00126         break;
00127       case 6:
00128         movePoseGoal.pose.position.x += .005;
00129         movePoseGoal.pose.position.y += .005;
00130         break;
00131       case 7:
00132         movePoseGoal.pose.position.x += .005;
00133         movePoseGoal.pose.position.y -= .005;
00134         break;
00135       case 8:
00136         movePoseGoal.pose.position.x -= .005;
00137         movePoseGoal.pose.position.y += .005;
00138         break;
00139       case 9:
00140         movePoseGoal.pose.position.x -= .005;
00141         movePoseGoal.pose.position.y -= .005;
00142         break;
00143     }
00144     acMoveArm.sendGoal(movePoseGoal);
00145     ROS_INFO("Approach angle arm move initiated.");
00146     acMoveArm.waitForResult(ros::Duration(20.0));
00147     carl_moveit::MoveToPoseResultConstPtr movePoseResult = acMoveArm.getResult();
00148 
00149     counter++;
00150 
00151     res.earlyFailureDetected = !movePoseResult->success;
00152     if (!res.earlyFailureDetected)
00153     {
00154       success = true;
00155       res.result = true;
00156     }
00157     else if (res.earlyFailureDetected && counter > 9)
00158     {
00159       ROS_INFO("Detected failure on moving to approach angle.");
00160       res.result = false;
00161       return false;
00162     }
00163   }
00164 
00166   wpi_jaco_msgs::GetCartesianPosition::Request ikReq;
00167   wpi_jaco_msgs::GetCartesianPosition::Response ikRes;
00168   cartesianPositionClient.call(ikReq, ikRes);
00169 
00170   wpi_jaco_msgs::CartesianCommand cartesianCmd;
00171   cartesianCmd.position = true;
00172   cartesianCmd.armCommand = true;
00173   cartesianCmd.fingerCommand = false;
00174   cartesianCmd.repeat = false;
00175   cartesianCmd.arm = ikRes.pos;
00176   cartesianCmd.arm.linear.z -= .05;
00177   cartesianCommandPub.publish(cartesianCmd);
00178 
00179   ros::Duration(5.0).sleep(); //wait for cartesian trajectory execution
00181 
00182   //Open gripper
00183   ROS_INFO("Fully opening gripper...");
00184   wpi_jaco_msgs::ExecuteGraspGoal openGripperGoal;
00185   openGripperGoal.closeGripper = false;
00186   openGripperGoal.limitFingerVelocity = false;
00187   acGrasp.sendGoal(openGripperGoal);
00188   acGrasp.waitForResult(ros::Duration(5.0));
00189   ROS_INFO("Gripper opened.");
00190 
00191   //*********** lift hand ***********
00192   ROS_INFO("Lifting hand...");
00193   wpi_jaco_msgs::ExecutePickupGoal pickupGoal;
00194   pickupGoal.limitFingerVelocity = false;
00195   pickupGoal.setLiftVelocity = false;
00196   acPickup.sendGoal(pickupGoal);
00197   acPickup.waitForResult(ros::Duration(10));
00198   ROS_INFO("hand lifting complete.");
00199 }
00200 
00201 bool graspObject::requestGrasp(rail_grasping::RequestGrasp::Request & req, rail_grasping::RequestGrasp::Response & res)
00202 {
00203   ROS_INFO("Received new grasp request...");
00204   //set new grasp position
00205   graspTransform.setOrigin(tf::Vector3(req.graspPose.position.x, req.graspPose.position.y, req.graspPose.position.z));
00206   graspTransform.setRotation(tf::Quaternion(req.graspPose.orientation.x, req.graspPose.orientation.y, req.graspPose.orientation.z, req.graspPose.orientation.w));
00207   tfBroadcaster.sendTransform(tf::StampedTransform(graspTransform, ros::Time::now(), "base_footprint", "grasp_frame"));
00208 
00209   ros::Duration(1).sleep();
00210 
00211   bool earlyFailureDetected;
00212   res.result = executeGrasp(&earlyFailureDetected);
00213   res.earlyFailureDetected = earlyFailureDetected;
00214 
00215   return true;
00216 }
00217 
00218 bool graspObject::executeGrasp(bool *earlyFailureFlag)
00219 {
00220   ROS_INFO("\nGrasp execution started");
00221 
00222   //Open gripper
00223   ROS_INFO("Fully opening gripper...");
00224   wpi_jaco_msgs::ExecuteGraspGoal openGripperGoal;
00225   openGripperGoal.closeGripper = false;
00226   openGripperGoal.limitFingerVelocity = false;
00227   acGrasp.sendGoal(openGripperGoal);
00228   acGrasp.waitForResult(ros::Duration(5.0));
00229   ROS_INFO("Gripper opened.");
00230 
00231   //*********** Align gripper on approach angle ***********
00232   //Calculate pose on approach angle
00233   geometry_msgs::PoseStamped approachPose;
00234   geometry_msgs::PoseStamped poseOut;
00235   //approachPose.header.stamp = ros::Time::now();
00236   approachPose.header.frame_id = "grasp_frame";
00237   approachPose.pose.position.x = 0.0;
00238   approachPose.pose.position.y = 0.0;
00239   //approachPose.pose.position.z = 0.1; //note: disabled to see how close the arm will plan to
00240   approachPose.pose.position.z = 0.0;
00241   approachPose.pose.orientation.x = 0.0;
00242   approachPose.pose.orientation.y = 0.0;
00243   approachPose.pose.orientation.z = 0.0;
00244   approachPose.pose.orientation.w = 1.0;
00245 
00246   //adjustment to put approach pose from centered at fingers to jaco_link_hand
00247   approachPose.pose.position.z += .19;
00248 
00249   tfListener.transformPose("base_footprint", approachPose, poseOut);
00250 
00252   poseOut.pose.position.z += .07;
00254 
00255   //Send goal to move arm service
00256   ROS_INFO("Moving to approach angle...");
00257   carl_moveit::MoveToPoseGoal movePoseGoal;
00258   movePoseGoal.pose = poseOut.pose;
00259   acMoveArm.sendGoal(movePoseGoal);
00260   ROS_INFO("Approach angle arm move initiated.");
00261   acMoveArm.waitForResult(ros::Duration(20.0));
00262   carl_moveit::MoveToPoseResultConstPtr movePoseResult = acMoveArm.getResult();
00263 
00264   *earlyFailureFlag = !movePoseResult->success;
00265   if (*earlyFailureFlag)
00266   {
00267     ROS_INFO("Detected failure on moving to approach angle.");
00268     return false;
00269   }
00270 
00272   /*
00273   //Previous method - using Kinova's cartesian controller
00274   wpi_jaco_msgs::GetCartesianPosition::Request ikReq;
00275   wpi_jaco_msgs::GetCartesianPosition::Response ikRes;
00276   cartesianPositionClient.call(ikReq, ikRes);
00277   
00278   wpi_jaco_msgs::CartesianCommand cartesianCmd;
00279   cartesianCmd.position = true;
00280   cartesianCmd.armCommand = true;
00281   cartesianCmd.fingerCommand = false;
00282   cartesianCmd.repeat = false;
00283   cartesianCmd.arm = ikRes.pos;
00284   cartesianCmd.arm.linear.z -= .085;
00285   cartesianCommandPub.publish(cartesianCmd);
00286 
00287   ros::Duration(5.0).sleep(); //wait for cartesian trajectory execution
00288   */
00289 
00290   carl_moveit::CartesianPathRequest pathReq;
00291   carl_moveit::CartesianPathResponse pathRes;
00292   tf::StampedTransform currentHandTransform;
00293   tfListener.waitForTransform("jaco_link_hand", "base_footprint", ros::Time::now(), ros::Duration(1.0));
00294   tfListener.lookupTransform("base_footprint", "jaco_link_hand", ros::Time(0), currentHandTransform);
00295   geometry_msgs::Pose graspPoseWaypoint;
00296   graspPoseWaypoint.position.x = currentHandTransform.getOrigin().x();
00297   graspPoseWaypoint.position.y = currentHandTransform.getOrigin().y();
00298   graspPoseWaypoint.position.z = currentHandTransform.getOrigin().z() - .085;
00299   graspPoseWaypoint.orientation.x = currentHandTransform.getRotation().x();
00300   graspPoseWaypoint.orientation.y = currentHandTransform.getRotation().y();
00301   graspPoseWaypoint.orientation.z = currentHandTransform.getRotation().z();
00302   graspPoseWaypoint.orientation.w = currentHandTransform.getRotation().w();
00303   pathReq.waypoints.push_back(graspPoseWaypoint);
00304   pathReq.avoidCollisions = false;
00305 
00306   if (!cartesianPathClient.call(pathReq, pathRes))
00307   {
00308     ROS_INFO("Failed to call cartesian path planner.");
00309     *earlyFailureFlag = true;
00310     return false;
00311   }
00313 
00314   //wait for asynchronous trajectory execution to complete
00315   //TODO: Update this to listen to the trajectory execution action server state for completion
00316   /*
00317   ROS_INFO("Waiting for trajectory execution...");
00318   ros::Duration(10.0).sleep();
00319   ROS_INFO("Done waiting for trajectory execution.");
00320   */
00321 
00322   //********** Move gripper along approach angle ***********
00323   /* TODO: Disabled for testing
00324   //Determine pickup pose
00325   geometry_msgs::PoseStamped pickupPose;
00326   geometry_msgs::PoseStamped pickupPoseOut;
00327   pickupPoseOut.header.frame_id = "base_footprint";
00328   //pickupPose.header.stamp = ros::Time::now();
00329   pickupPose.header.frame_id = "grasp_frame";
00330   pickupPose.pose.position.x = 0.0;
00331   pickupPose.pose.position.y = 0.0;
00332   pickupPose.pose.position.z = 0.0;
00333   pickupPose.pose.orientation.x = 0.0;
00334   pickupPose.pose.orientation.y = 0.0;
00335   pickupPose.pose.orientation.z = 0.0;
00336   pickupPose.pose.orientation.w = 1.0;
00337   
00338   //adjustment to put approach pose from centered at fingers to jaco_link_hand
00339   approachPose.pose.position.z += .19;
00340   
00341   tfListener.transformPose("base_footprint", pickupPose, pickupPoseOut);
00342   
00343   ROS_INFO("Moving arm to pickup pose...");
00344   */
00345   /************* Arm Movement With IK *****************/
00346   /* TODO: Disabled for testing
00347   if (!moveArmToPose(pickupPoseOut))
00348   {
00349     ROS_INFO("Failed to move arm to pickup pose");
00350     *earlyFailureFlag = true;
00351     return false;
00352   }
00353   ROS_INFO("Arm at pickup pose.");
00354   */
00355 
00356   //Close gripper
00357   ROS_INFO("Closing gripper...");
00358   wpi_jaco_msgs::ExecuteGraspGoal closeGripperGoal;
00359   closeGripperGoal.closeGripper = true;
00360   closeGripperGoal.limitFingerVelocity = false;
00361   acGrasp.sendGoal(closeGripperGoal);
00362   acGrasp.waitForResult(ros::Duration(5.0));
00363   ROS_INFO("Gripper closed.");
00364 
00365   //*********** lift object ***********
00366   /* Lift object using pickup action server instead, switch it out with this if the
00367    * Cartesian lift is not working consistantly
00368    */
00369   ROS_INFO("Lifting object...");
00370   /*
00371   //Previous method - using Kinova's Cartesian controller
00372   wpi_jaco_msgs::ExecutePickupGoal pickupGoal;
00373   pickupGoal.limitFingerVelocity = false;
00374   pickupGoal.setLiftVelocity = false;
00375   acPickup.sendGoal(pickupGoal);
00376   acPickup.waitForResult(ros::Duration(10));
00377   ROS_INFO("Object lifting complete.");
00378   */
00380   carl_moveit::CartesianPathRequest pickupPathReq;
00381   carl_moveit::CartesianPathResponse pickupPathRes;
00382   tfListener.waitForTransform("jaco_link_hand", "base_footprint", ros::Time::now(), ros::Duration(1.0));
00383   tfListener.lookupTransform("base_footprint", "jaco_link_hand", ros::Time(0), currentHandTransform);
00384   geometry_msgs::Pose pickupPoseWaypoint;
00385   pickupPoseWaypoint.position.x = currentHandTransform.getOrigin().x();
00386   pickupPoseWaypoint.position.y = currentHandTransform.getOrigin().y();
00387   pickupPoseWaypoint.position.z = currentHandTransform.getOrigin().z() + .085;
00388   pickupPoseWaypoint.orientation.x = currentHandTransform.getRotation().x();
00389   pickupPoseWaypoint.orientation.y = currentHandTransform.getRotation().y();
00390   pickupPoseWaypoint.orientation.z = currentHandTransform.getRotation().z();
00391   pickupPoseWaypoint.orientation.w = currentHandTransform.getRotation().w();
00392   pickupPathReq.waypoints.push_back(pickupPoseWaypoint);
00393   pickupPathReq.avoidCollisions = false;
00394 
00395   if (!cartesianPathClient.call(pickupPathReq, pickupPathRes))
00396   {
00397     ROS_INFO("Failed to call cartesian path planner.");
00398     return false;
00399   }
00401 
00402   /*
00403   //Calculate lift pose
00404   geometry_msgs::PoseStamped liftPose;
00405   geometry_msgs::PoseStamped liftPoseOut;
00406   liftPoseOut.header.frame_id = "base_footprint";
00407   //liftPose.header.stamp = ros::Time::now();
00408   if (side.compare("left") == 0)
00409     liftPose.header.frame_id = "l_wrist_roll_link";
00410   else
00411     liftPose.header.frame_id = "r_wrist_roll_link";
00412   liftPose.pose.position.x = 0.0;
00413   liftPose.pose.position.y = 0.0;
00414   liftPose.pose.position.z = 0.0;
00415   liftPose.pose.orientation.x = 0.0;
00416   liftPose.pose.orientation.y = 0.0;
00417   liftPose.pose.orientation.z = 0.0;
00418   liftPose.pose.orientation.w = 1.0;
00419   
00420   tfListener.transformPose("base_footprint", liftPose, liftPoseOut);
00421   
00422   liftPoseOut.pose.position.z += 0.1;
00423   
00424   if (!moveArmToPose(side, liftPoseOut))
00425   {
00426     ROS_INFO("Failed to move arm to lift pose");
00427     return false;
00428   }
00429   */
00430 
00431   return true;
00432 }
00433 
00434 bool graspObject::moveArmToPose(geometry_msgs::PoseStamped poseGoal)
00435 {
00436   //IK
00437   carl_moveit::CallIK::Request ikReq;
00438   carl_moveit::CallIK::Response ikRes;
00439   ikReq.pose = poseGoal.pose;
00440   IKClient.call(ikReq, ikRes);
00441 
00442   if (ikRes.success = false)
00443   {
00444     ROS_INFO("IK Server returned false");
00445     return false;
00446   }
00447 
00448   //Create and send trajectory to the arm
00449   //possibly send to arm directly as a joint goal with speed limitation?
00450   if (!jointNamesSet)
00451   {
00452     ROS_INFO("Joint data from the arm has not been received");
00453     return false;
00454   }
00455 
00456   trajectory_msgs::JointTrajectoryPoint startPoint;
00457   trajectory_msgs::JointTrajectoryPoint endPoint;
00458   startPoint.positions = armJointPos;
00459   endPoint.positions = ikRes.jointPositions;
00460   control_msgs::FollowJointTrajectoryGoal trajGoal;
00461   trajGoal.trajectory.joint_names = armJointNames;
00462   trajGoal.trajectory.points.clear();
00463   trajGoal.trajectory.points.push_back(startPoint);
00464   trajGoal.trajectory.points.push_back(endPoint);
00465   //trajGoal.goal_tolerance.clear();
00466   //TODO: determine if setting joint goal tolerances is required
00467   //trajGoal.goal_time_tolerance = ros::Duration(10.0);  //TODO: This is adjustable
00468 
00469   ROS_INFO("Sending trajectory goal directly to arm");
00470 
00471   acJointTrajectory.sendGoal(trajGoal);
00472   acJointTrajectory.waitForResult(ros::Duration(10.0));
00473 
00474   //TODO: check result of trajectory execution to determine return value
00475   return true;
00476 }
00477 
00478 void graspObject::publishGraspFrame()
00479 {
00480   tfBroadcaster.sendTransform(tf::StampedTransform(graspTransform, ros::Time::now(), "base_footprint", "grasp_frame"));
00481 }
00482 
00483 int main(int argc, char **argv)
00484 {
00485   ros::init(argc, argv, "rail_grasping");
00486 
00487   graspObject go;
00488 
00489   ros::Rate loop_rate(30);
00490   while (ros::ok())
00491   {
00492     ros::spinOnce();
00493     go.publishGraspFrame();
00494     loop_rate.sleep();
00495   }
00496 
00497   return 0;
00498 }


rail_grasping
Author(s): David Kent
autogenerated on Tue Mar 3 2015 19:18:31