graspObject_back.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.7; //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 
00251   //Send goal to move arm service
00252   ROS_INFO("Moving to approach angle...");
00253   carl_moveit::MoveToPoseGoal movePoseGoal;
00254   movePoseGoal.pose = poseOut.pose;
00255   acMoveArm.sendGoal(movePoseGoal);
00256   ROS_INFO("Approach angle arm move initiated.");
00257   acMoveArm.waitForResult(ros::Duration(20.0));
00258   carl_moveit::MoveToPoseResultConstPtr movePoseResult = acMoveArm.getResult();
00259 
00260   *earlyFailureFlag = !movePoseResult->success;
00261   if (*earlyFailureFlag)
00262   {
00263     ROS_INFO("Detected failure on moving to approach angle.");
00264     return false;
00265   }
00266 
00267   //Determine pickup pose
00268   carl_moveit::CartesianPath pathSrvObj;
00269   geometry_msgs::PoseStamped pickupPose;
00270   geometry_msgs::PoseStamped pickupPoseOut;
00271   pickupPoseOut.header.frame_id = "base_footprint";
00272   //pickupPose.header.stamp = ros::Time::now();
00273   pickupPose.header.frame_id = "grasp_frame";
00274   pickupPose.pose.position.x = 0.0;
00275   pickupPose.pose.position.y = 0.0;
00276   pickupPose.pose.position.z = 0.0;
00277   pickupPose.pose.orientation.x = 0.0;
00278   pickupPose.pose.orientation.y = 0.0;
00279   pickupPose.pose.orientation.z = 0.0;
00280   pickupPose.pose.orientation.w = 1.0;
00281 
00282   //adjustment to put approach pose from centered at fingers to jaco_link_hand
00283   pickupPose.pose.position.z += .19;
00284 
00285   tfListener.transformPose("base_footprint", pickupPose, pickupPoseOut);
00286 
00287   tf::StampedTransform currentHandTransform;
00288   /*
00289   tfListener.waitForTransform("jaco_link_hand", "base_footprint", ros::Time::now(), ros::Duration(1.0));
00290   tfListener.lookupTransform("base_footprint", "jaco_link_hand", ros::Time(0), currentHandTransform);
00291   geometry_msgs::Pose graspPoseWaypoint;
00292   graspPoseWaypoint.position.x = currentHandTransform.getOrigin().x();
00293   graspPoseWaypoint.position.y = currentHandTransform.getOrigin().y();
00294   graspPoseWaypoint.position.z = currentHandTransform.getOrigin().z() - .085;
00295   graspPoseWaypoint.orientation.x = currentHandTransform.getRotation().x();
00296   graspPoseWaypoint.orientation.y = currentHandTransform.getRotation().y();
00297   graspPoseWaypoint.orientation.z = currentHandTransform.getRotation().z();
00298   graspPoseWaypoint.orientation.w = currentHandTransform.getRotation().w();
00299   pathSrvObj.request.waypoints.push_back(graspPoseWaypoint);
00300   */
00301   pathSrvObj.request.waypoints.push_back(pickupPoseOut.pose);
00302   pathSrvObj.request.avoidCollisions = false;
00303 
00304   if (!cartesianPathClient.call(pathSrvObj))
00305   {
00306     ROS_INFO("Failed to call cartesian path planner.");
00307     *earlyFailureFlag = true;
00308     return false;
00309   }
00310 
00311   //wait for asynchronous trajectory execution to complete
00312   //TODO: Update this to listen to the trajectory execution action server state for completion
00313   /*
00314   ROS_INFO("Waiting for trajectory execution...");
00315   ros::Duration(10.0).sleep();
00316   ROS_INFO("Done waiting for trajectory execution.");
00317   */
00318 
00319   //********** Move gripper along approach angle ***********
00320   /* TODO: Disabled for testing
00321   //Determine pickup pose
00322   geometry_msgs::PoseStamped pickupPose;
00323   geometry_msgs::PoseStamped pickupPoseOut;
00324   pickupPoseOut.header.frame_id = "base_footprint";
00325   //pickupPose.header.stamp = ros::Time::now();
00326   pickupPose.header.frame_id = "grasp_frame";
00327   pickupPose.pose.position.x = 0.0;
00328   pickupPose.pose.position.y = 0.0;
00329   pickupPose.pose.position.z = 0.0;
00330   pickupPose.pose.orientation.x = 0.0;
00331   pickupPose.pose.orientation.y = 0.0;
00332   pickupPose.pose.orientation.z = 0.0;
00333   pickupPose.pose.orientation.w = 1.0;
00334   
00335   //adjustment to put approach pose from centered at fingers to jaco_link_hand
00336   approachPose.pose.position.z += .19;
00337   
00338   tfListener.transformPose("base_footprint", pickupPose, pickupPoseOut);
00339   
00340   ROS_INFO("Moving arm to pickup pose...");
00341   */
00342   /************* Arm Movement With IK *****************/
00343   /* TODO: Disabled for testing
00344   if (!moveArmToPose(pickupPoseOut))
00345   {
00346     ROS_INFO("Failed to move arm to pickup pose");
00347     *earlyFailureFlag = true;
00348     return false;
00349   }
00350   ROS_INFO("Arm at pickup pose.");
00351   */
00352 
00353   //Close gripper
00354   ROS_INFO("Closing gripper...");
00355   wpi_jaco_msgs::ExecuteGraspGoal closeGripperGoal;
00356   closeGripperGoal.closeGripper = true;
00357   closeGripperGoal.limitFingerVelocity = false;
00358   acGrasp.sendGoal(closeGripperGoal);
00359   acGrasp.waitForResult(ros::Duration(5.0));
00360   ROS_INFO("Gripper closed.");
00361 
00362   //*********** lift object ***********
00363   /* Lift object using pickup action server instead, switch it out with this if the
00364    * Cartesian lift is not working consistantly
00365    */
00366   ROS_INFO("Lifting object...");
00367   /*
00368   //Previous method - using Kinova's Cartesian controller
00369   wpi_jaco_msgs::ExecutePickupGoal pickupGoal;
00370   pickupGoal.limitFingerVelocity = false;
00371   pickupGoal.setLiftVelocity = false;
00372   acPickup.sendGoal(pickupGoal);
00373   acPickup.waitForResult(ros::Duration(10));
00374   ROS_INFO("Object lifting complete.");
00375   */
00377   carl_moveit::CartesianPathRequest pickupPathReq;
00378   carl_moveit::CartesianPathResponse pickupPathRes;
00379   tfListener.waitForTransform("jaco_link_hand", "base_footprint", ros::Time::now(), ros::Duration(1.0));
00380   tfListener.lookupTransform("base_footprint", "jaco_link_hand", ros::Time(0), currentHandTransform);
00381   geometry_msgs::Pose pickupPoseWaypoint;
00382   pickupPoseWaypoint.position.x = currentHandTransform.getOrigin().x();
00383   pickupPoseWaypoint.position.y = currentHandTransform.getOrigin().y();
00384   pickupPoseWaypoint.position.z = currentHandTransform.getOrigin().z() + .085;
00385   pickupPoseWaypoint.orientation.x = currentHandTransform.getRotation().x();
00386   pickupPoseWaypoint.orientation.y = currentHandTransform.getRotation().y();
00387   pickupPoseWaypoint.orientation.z = currentHandTransform.getRotation().z();
00388   pickupPoseWaypoint.orientation.w = currentHandTransform.getRotation().w();
00389   pickupPathReq.waypoints.push_back(pickupPoseWaypoint);
00390   pickupPathReq.avoidCollisions = false;
00391 
00392   if (!cartesianPathClient.call(pickupPathReq, pickupPathRes))
00393   {
00394     ROS_INFO("Failed to call cartesian path planner.");
00395     return false;
00396   }
00398 
00399   /*
00400   //Calculate lift pose
00401   geometry_msgs::PoseStamped liftPose;
00402   geometry_msgs::PoseStamped liftPoseOut;
00403   liftPoseOut.header.frame_id = "base_footprint";
00404   //liftPose.header.stamp = ros::Time::now();
00405   if (side.compare("left") == 0)
00406     liftPose.header.frame_id = "l_wrist_roll_link";
00407   else
00408     liftPose.header.frame_id = "r_wrist_roll_link";
00409   liftPose.pose.position.x = 0.0;
00410   liftPose.pose.position.y = 0.0;
00411   liftPose.pose.position.z = 0.0;
00412   liftPose.pose.orientation.x = 0.0;
00413   liftPose.pose.orientation.y = 0.0;
00414   liftPose.pose.orientation.z = 0.0;
00415   liftPose.pose.orientation.w = 1.0;
00416   
00417   tfListener.transformPose("base_footprint", liftPose, liftPoseOut);
00418   
00419   liftPoseOut.pose.position.z += 0.1;
00420   
00421   if (!moveArmToPose(side, liftPoseOut))
00422   {
00423     ROS_INFO("Failed to move arm to lift pose");
00424     return false;
00425   }
00426   */
00427 
00428   return true;
00429 }
00430 
00431 bool graspObject::moveArmToPose(geometry_msgs::PoseStamped poseGoal)
00432 {
00433   //IK
00434   carl_moveit::CallIK::Request ikReq;
00435   carl_moveit::CallIK::Response ikRes;
00436   ikReq.pose = poseGoal.pose;
00437   IKClient.call(ikReq, ikRes);
00438 
00439   if (ikRes.success = false)
00440   {
00441     ROS_INFO("IK Server returned false");
00442     return false;
00443   }
00444 
00445   //Create and send trajectory to the arm
00446   //possibly send to arm directly as a joint goal with speed limitation?
00447   if (!jointNamesSet)
00448   {
00449     ROS_INFO("Joint data from the arm has not been received");
00450     return false;
00451   }
00452 
00453   trajectory_msgs::JointTrajectoryPoint startPoint;
00454   trajectory_msgs::JointTrajectoryPoint endPoint;
00455   startPoint.positions = armJointPos;
00456   endPoint.positions = ikRes.jointPositions;
00457   control_msgs::FollowJointTrajectoryGoal trajGoal;
00458   trajGoal.trajectory.joint_names = armJointNames;
00459   trajGoal.trajectory.points.clear();
00460   trajGoal.trajectory.points.push_back(startPoint);
00461   trajGoal.trajectory.points.push_back(endPoint);
00462   //trajGoal.goal_tolerance.clear();
00463   //TODO: determine if setting joint goal tolerances is required
00464   //trajGoal.goal_time_tolerance = ros::Duration(10.0);  //TODO: This is adjustable
00465 
00466   ROS_INFO("Sending trajectory goal directly to arm");
00467 
00468   acJointTrajectory.sendGoal(trajGoal);
00469   acJointTrajectory.waitForResult(ros::Duration(10.0));
00470 
00471   //TODO: check result of trajectory execution to determine return value
00472   return true;
00473 }
00474 
00475 void graspObject::publishGraspFrame()
00476 {
00477   tfBroadcaster.sendTransform(tf::StampedTransform(graspTransform, ros::Time::now(), "base_footprint", "grasp_frame"));
00478 }
00479 
00480 int main(int argc, char **argv)
00481 {
00482   ros::init(argc, argv, "rail_grasping");
00483 
00484   graspObject go;
00485 
00486   ros::Rate loop_rate(30);
00487   while (ros::ok())
00488   {
00489     ros::spinOnce();
00490     go.publishGraspFrame();
00491     loop_rate.sleep();
00492   }
00493 
00494   return 0;
00495 }


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