simple_grasps_test.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2014, University of Colorado, Boulder
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Univ of CO, Boulder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Dave Coleman
00036    Desc:   Tests the grasp generator
00037 */
00038 
00039 // ROS
00040 #include <ros/ros.h>
00041 #include <geometry_msgs/PoseArray.h>
00042 #include <Eigen/Core>
00043 #include <Eigen/Geometry>
00044 
00045 // Grasp generation
00046 #include <moveit_simple_grasps/simple_grasps.h>
00047 
00048 // Baxter specific properties
00049 #include <moveit_simple_grasps/custom_environment2.h>
00050 
00051 namespace baxter_pick_place
00052 {
00053 
00054 static const double BLOCK_SIZE = 0.04;
00055 
00056 class GraspGeneratorTest
00057 {
00058 private:
00059   // A shared node handle
00060   ros::NodeHandle nh_;
00061 
00062   // Grasp generator
00063   moveit_simple_grasps::SimpleGraspsPtr simple_grasps_;
00064 
00065   // class for publishing stuff to rviz
00066   moveit_visual_tools::VisualToolsPtr visual_tools_;
00067 
00068   // robot-specific data for generating grasps
00069   moveit_simple_grasps::GraspData grasp_data_;
00070 
00071   // which baxter arm are we using
00072   std::string arm_;
00073   std::string ee_group_name_;
00074   std::string planning_group_name_;
00075 
00076 public:
00077 
00078   // Constructor
00079   GraspGeneratorTest(int num_tests)
00080     : nh_("~")
00081   {
00082     nh_.param("arm", arm_, std::string("left"));
00083     nh_.param("ee_group_name", ee_group_name_, std::string(arm_ + "_hand"));
00084     planning_group_name_ = arm_ + "_arm";
00085 
00086     ROS_INFO_STREAM_NAMED("test","Arm: " << arm_);
00087     ROS_INFO_STREAM_NAMED("test","End Effector: " << ee_group_name_);
00088     ROS_INFO_STREAM_NAMED("test","Planning Group: " << planning_group_name_);
00089     
00090     // ---------------------------------------------------------------------------------------------
00091     // Load grasp data specific to our robot
00092     if (!grasp_data_.loadRobotGraspData(nh_, ee_group_name_))
00093       ros::shutdown();
00094 
00095     // ---------------------------------------------------------------------------------------------
00096     // Load the Robot Viz Tools for publishing to Rviz
00097     visual_tools_.reset(new moveit_visual_tools::VisualTools(grasp_data_.base_link_));
00098     visual_tools_->setLifetime(120.0);
00099     visual_tools_->setMuted(false);
00100     visual_tools_->loadEEMarker(grasp_data_.ee_group_, planning_group_name_);
00101 
00102     // ---------------------------------------------------------------------------------------------
00103     // Load grasp generator
00104     simple_grasps_.reset( new moveit_simple_grasps::SimpleGrasps(visual_tools_) );
00105 
00106     // ---------------------------------------------------------------------------------------------
00107     // Generate grasps for a bunch of random objects
00108     geometry_msgs::Pose object_pose;
00109     std::vector<moveit_msgs::Grasp> possible_grasps;
00110 
00111     // Allow ROS to catchup
00112     ros::Duration(2.0).sleep();
00113 
00114     // Loop
00115     int i = 0;
00116     while(ros::ok())
00117     {
00118       ROS_INFO_STREAM_NAMED("test","Adding random object " << i+1 << " of " << num_tests);
00119 
00120       // Remove randomness when we are only running one test
00121       if (num_tests == 1)
00122         generateTestObject(object_pose);
00123       else
00124         generateRandomObject(object_pose);
00125 
00126       // Show the block
00127       visual_tools_->publishBlock(object_pose, moveit_visual_tools::BLUE, BLOCK_SIZE);
00128 
00129       possible_grasps.clear();
00130 
00131       // Generate set of grasps for one object
00132       simple_grasps_->generateBlockGrasps( object_pose, grasp_data_, possible_grasps);
00133 
00134       // Visualize them
00135       visual_tools_->publishAnimatedGrasps(possible_grasps, grasp_data_.ee_parent_link_);
00136       //visual_tools_->publishGrasps(possible_grasps, grasp_data_.ee_parent_link_);
00137 
00138       // Test if done
00139       ++i;
00140       if( i >= num_tests )
00141         break;
00142     }
00143   }
00144 
00145   void generateTestObject(geometry_msgs::Pose& object_pose)
00146   {
00147     // Position
00148     geometry_msgs::Pose start_object_pose;
00149 
00150     start_object_pose.position.x = 0.4;
00151     start_object_pose.position.y = -0.2;
00152     start_object_pose.position.z = 0.0;
00153 
00154     // Orientation
00155     double angle = M_PI / 1.5;
00156     Eigen::Quaterniond quat(Eigen::AngleAxis<double>(double(angle), Eigen::Vector3d::UnitZ()));
00157     start_object_pose.orientation.x = quat.x();
00158     start_object_pose.orientation.y = quat.y();
00159     start_object_pose.orientation.z = quat.z();
00160     start_object_pose.orientation.w = quat.w();
00161 
00162     // Choose which object to test
00163     object_pose = start_object_pose;
00164 
00165     //visual_tools_->publishObject( object_pose, OBJECT_SIZE, true );
00166   }
00167 
00168   void generateRandomObject(geometry_msgs::Pose& object_pose)
00169   {
00170     // Position
00171     object_pose.position.x = fRand(0.1,0.9); //0.55);
00172     object_pose.position.y = fRand(-0.28,0.28);
00173     object_pose.position.z = 0.02;
00174 
00175     // Orientation
00176     double angle = M_PI * fRand(0.1,1);
00177     Eigen::Quaterniond quat(Eigen::AngleAxis<double>(double(angle), Eigen::Vector3d::UnitZ()));
00178     object_pose.orientation.x = quat.x();
00179     object_pose.orientation.y = quat.y();
00180     object_pose.orientation.z = quat.z();
00181     object_pose.orientation.w = quat.w();
00182   }
00183 
00184   double fRand(double fMin, double fMax)
00185   {
00186     double f = (double)rand() / RAND_MAX;
00187     return fMin + f * (fMax - fMin);
00188   }
00189 
00190 }; // end of class
00191 
00192 } // namespace
00193 
00194 
00195 int main(int argc, char *argv[])
00196 {
00197   int num_tests = 10;
00198   ros::init(argc, argv, "grasp_generator_test");
00199 
00200   ROS_INFO_STREAM_NAMED("main","Simple Grasps Test");
00201 
00202   ros::AsyncSpinner spinner(2);
00203   spinner.start();
00204 
00205   // Seed random
00206   srand(ros::Time::now().toSec());
00207 
00208   // Benchmark time
00209   ros::Time start_time;
00210   start_time = ros::Time::now();
00211 
00212   // Run Tests
00213   baxter_pick_place::GraspGeneratorTest tester(num_tests);
00214 
00215   // Benchmark time
00216   double duration = (ros::Time::now() - start_time).toNSec() * 1e-6;
00217   ROS_INFO_STREAM_NAMED("","Total time: " << duration);
00218   //std::cout << duration << "\t" << num_tests << std::endl;
00219 
00220   ros::Duration(1.0).sleep(); // let rviz markers finish publishing
00221 
00222   return 0;
00223 }


moveit_simple_grasps
Author(s): Dave Coleman
autogenerated on Fri Aug 28 2015 11:36:01