Utilities.cpp
Go to the documentation of this file.
00001 /*
00002  * Utilities.cpp
00003  *
00004  *  Created on: Dec 25, 2012
00005  *      Author: coky
00006  */
00007 #include <object_manipulation_tools/manipulation_utils/Utilities.h>
00008 #include <pcl/ros/conversions.h>
00009 #include <pcl/common/centroid.h>
00010 
00011 namespace manipulation_utils
00012 {
00013 /*
00014  * generateGraspPoses
00015  * Description:
00016  *              Generates additional poses by post multiplying a rotation matrix to the original pose.  The rotation matrix is
00017  *              produced by rotating about a axis by an angle increment.
00018  *
00019  * Input arguments:
00020  *              pose:                   Original pose.
00021  *              rotationAxix:   Axis of rotation.
00022  *              numCandidates:  Number of poses to generate.  The angle increment will be calculated  as 2*pi/numCandidates.
00023  *
00024  *      Output arguments:
00025  *              poses:                  Array of generated poses
00026  */
00027         void generateGraspPoses(const geometry_msgs::Pose &pose,tf::Vector3 rotationAxis,int numCandidates,
00028                         std::vector<geometry_msgs::Pose> &poses)
00029         {
00030                 tf::Transform graspTf = tf::Transform::getIdentity();
00031                 tf::Transform candidateTf;
00032                 tfScalar angle = tfScalar(2 * M_PI/(double(numCandidates)));
00033 
00034                 // converting initial pose to tf
00035                 tf::poseMsgToTF(pose,graspTf);
00036 
00037                 for(int i = 0; i < numCandidates; i++)
00038                 {
00039                         candidateTf = graspTf*tf::Transform(tf::Quaternion(rotationAxis,i*angle - M_PI),
00040                                         tf::Vector3(0.0f,0.0f,0.0f));
00041                         geometry_msgs::Pose candidatePose = geometry_msgs::Pose();
00042                         tf::poseTFToMsg(candidateTf,candidatePose);
00043                         poses.push_back(candidatePose);
00044                 }
00045         }
00046 
00047         /*
00048          * Generates additional grasps by rotating original grasp about the rotational axis 'numCandidate' times
00049          */
00050         void generateCandidateGrasps(const object_manipulation_msgs::Grasp firstGrasp,tf::Vector3 rotationAxis,int numCandidates,
00051                         std::vector<object_manipulation_msgs::Grasp> &graspCandidates)
00052         {
00053                 std::vector<geometry_msgs::Pose> graspPoses;
00054                 generateGraspPoses(firstGrasp.grasp_pose,rotationAxis,numCandidates,graspPoses);
00055 
00056                 // filling candidate grasp array
00057                 object_manipulation_msgs::Grasp grasp = firstGrasp;
00058                 for(std::size_t i = 0; i < graspPoses.size(); i++)
00059                 {
00060                         grasp.grasp_pose = graspPoses[i];
00061                         graspCandidates.push_back(grasp);
00062                 }
00063         }
00064 
00065         void createBoundingSphereCollisionModel(const sensor_msgs::PointCloud cluster,double radius,
00066                         arm_navigation_msgs::CollisionObject &obj)
00067         {
00068                 pcl::PointCloud<pcl::PointXYZ> cloud;
00069                 sensor_msgs::PointCloud2 clusterMsg;
00070                 sensor_msgs::convertPointCloudToPointCloud2(cluster,clusterMsg);
00071                 pcl::fromROSMsg(clusterMsg,cloud);
00072 
00073                 // computing centroid
00074                 Eigen::Vector4f centroid;
00075                 pcl::compute3DCentroid(cloud,centroid);
00076 
00077                 // saving centroid as in tf
00078                 tf::Transform t = tf::Transform::getIdentity();
00079                 t.setOrigin(tf::Vector3(centroid[0],centroid[1],centroid[2]));
00080 
00081 
00082                 // creating shape and pose
00083                 geometry_msgs::Pose pose;
00084                 arm_navigation_msgs::Shape shape;
00085                 shape.type = arm_navigation_msgs::Shape::SPHERE;
00086                 shape.dimensions.push_back(radius);
00087                 tf::poseTFToMsg(t,pose);
00088 
00089                 // storing results in object
00090                 obj.shapes.push_back(shape);
00091                 obj.poses.push_back(pose);
00092         }
00093 
00094         void rectifyPoseZDirection(const geometry_msgs::Pose &actual_pose,
00095                         const geometry_msgs::Pose &rectification_pose,geometry_msgs::Pose &rectified_pose)
00096         {
00097                 tf::Transform rectification_tf;
00098                 tf::poseMsgToTF(rectification_pose,rectification_tf);
00099                 rectifyPoseZDirection(actual_pose,rectification_tf,rectified_pose);
00100         }
00101 
00102         void rectifyPoseZDirection(const geometry_msgs::Pose &actual_pose,
00103                         const tf::Transform &rectification_tf,geometry_msgs::Pose &rectified_pose)
00104         {
00105                 tf::Transform actual_tf, rectified_tf;
00106                 tf::poseMsgToTF(actual_pose,actual_tf);
00107                 rectified_tf = actual_tf;
00108 
00109                 // finding angle and axis of rotation between vectors
00110                 tf::Vector3 z_desired, z_actual;
00111                 z_desired = rectification_tf.getBasis().getColumn(2);
00112                 z_actual = actual_tf.getBasis().getColumn(2);
00113 
00114                 double angle = std::abs(z_actual.angle(z_desired));
00115                 tf::Vector3 axis = z_desired.cross(z_actual);
00116 
00117                 ROS_WARN_STREAM("Utilities: z_des: ["<<z_desired.x()<<", "<<z_desired.y()<<", "<<z_desired.z()<<" ]");
00118                 ROS_WARN_STREAM("Utilities: z_act: ["<<z_actual.x()<<", "<<z_actual.y()<<", "<<z_actual.z()<<" ]");
00119 
00120                 ROS_WARN_STREAM("Utilities: rectifying pose has angle: "<<angle<<",and axis: "
00121                                 <<"[ "<<axis.x()<<", "<<axis.y()<<", "<<axis.z()<<" ]");
00122 
00123                 if(angle < 0.001f)
00124                 {
00125                         // not a significant orientation difference
00126                         rectified_pose = actual_pose;
00127                         return;
00128                 }
00129 
00130                 rectified_tf.setRotation(tf::Quaternion(axis,-angle)*actual_tf.getRotation());
00131 
00132                 tf::Vector3 z_rect = rectified_tf.getBasis().getColumn(2);
00133                 ROS_WARN_STREAM("Utilities: z_rec: ["<<z_rect.x()<<", "<<z_rect.y()<<", "<<z_rect.z()<<" ]");
00134 
00135                 //rectified_tf.setRotation(actual_tf.getRotation() * tf::Quaternion(axis,-angle));
00136                 tf::poseTFToMsg(rectified_tf,rectified_pose);
00137         }
00138 }


object_manipulation_tools
Author(s): Jnicho
autogenerated on Mon Oct 6 2014 00:56:17