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::MoveItVisualToolsPtr 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::MoveItVisualTools(grasp_data_.base_link_));
00098     visual_tools_->setLifetime(120.0);
00099     visual_tools_->loadMarkerPub();
00100 
00101     // ---------------------------------------------------------------------------------------------
00102     // Load grasp generator
00103     simple_grasps_.reset( new moveit_simple_grasps::SimpleGrasps(visual_tools_) );
00104 
00105     geometry_msgs::Pose pose;
00106     visual_tools_->generateEmptyPose(pose);
00107 
00108     // ---------------------------------------------------------------------------------------------
00109     // Animate open and closing end effector
00110 
00111     for (std::size_t i = 0; i < 4; ++i)
00112     {
00113       // Test visualization of end effector in OPEN position
00114       grasp_data_.setRobotStatePreGrasp( visual_tools_->getSharedRobotState() );
00115       visual_tools_->loadEEMarker(grasp_data_.ee_group_);
00116       const robot_model::JointModelGroup* ee_jmg = visual_tools_->getRobotModel()->getJointModelGroup(grasp_data_.ee_group_);
00117       visual_tools_->publishEEMarkers(pose, ee_jmg, rviz_visual_tools::ORANGE, "test_eef");
00118       ros::Duration(1.0).sleep();
00119 
00120       // Test visualization of end effector in CLOSED position
00121       grasp_data_.setRobotStateGrasp( visual_tools_->getSharedRobotState() );
00122       visual_tools_->loadEEMarker(grasp_data_.ee_group_);
00123       visual_tools_->publishEEMarkers(pose, ee_jmg, rviz_visual_tools::GREEN, "test_eef");
00124       ros::Duration(1.0).sleep();
00125     }
00126 
00127     // ---------------------------------------------------------------------------------------------
00128     // Generate grasps for a bunch of random objects
00129     geometry_msgs::Pose object_pose;
00130     std::vector<moveit_msgs::Grasp> possible_grasps;
00131 
00132     // Loop
00133     int i = 0;
00134     while(ros::ok())
00135     {
00136       ROS_INFO_STREAM_NAMED("test","Adding random object " << i+1 << " of " << num_tests);
00137 
00138       // Remove randomness when we are only running one test
00139       if (num_tests == 1)
00140         generateTestObject(object_pose);
00141       else
00142         generateRandomObject(object_pose);
00143 
00144       // Show the block
00145       visual_tools_->publishBlock(object_pose, rviz_visual_tools::BLUE, BLOCK_SIZE);
00146 
00147       possible_grasps.clear();
00148 
00149       // Generate set of grasps for one object
00150       simple_grasps_->generateBlockGrasps( object_pose, grasp_data_, possible_grasps);
00151 
00152       // Visualize them
00153       const robot_model::JointModelGroup* ee_jmg = visual_tools_->getRobotModel()->getJointModelGroup(grasp_data_.ee_group_);
00154       visual_tools_->publishAnimatedGrasps(possible_grasps, ee_jmg);
00155       //visual_tools_->publishGrasps(possible_grasps, grasp_data_.ee_parent_link_);
00156 
00157       // Test if done
00158       ++i;
00159       if( i >= num_tests )
00160         break;
00161     }
00162   }
00163 
00164   void generateTestObject(geometry_msgs::Pose& object_pose)
00165   {
00166     // Position
00167     geometry_msgs::Pose start_object_pose;
00168 
00169     start_object_pose.position.x = 0.4;
00170     start_object_pose.position.y = -0.2;
00171     start_object_pose.position.z = 0.0;
00172 
00173     // Orientation
00174     double angle = M_PI / 1.5;
00175     Eigen::Quaterniond quat(Eigen::AngleAxis<double>(double(angle), Eigen::Vector3d::UnitZ()));
00176     start_object_pose.orientation.x = quat.x();
00177     start_object_pose.orientation.y = quat.y();
00178     start_object_pose.orientation.z = quat.z();
00179     start_object_pose.orientation.w = quat.w();
00180 
00181     // Choose which object to test
00182     object_pose = start_object_pose;
00183 
00184     //visual_tools_->publishObject( object_pose, OBJECT_SIZE, true );
00185   }
00186 
00187   void generateRandomObject(geometry_msgs::Pose& object_pose)
00188   {
00189     // Position
00190     object_pose.position.x = fRand(0.1,0.9); //0.55);
00191     object_pose.position.y = fRand(-0.28,0.28);
00192     object_pose.position.z = 0.02;
00193 
00194     // Orientation
00195     double angle = M_PI * fRand(0.1,1);
00196     Eigen::Quaterniond quat(Eigen::AngleAxis<double>(double(angle), Eigen::Vector3d::UnitZ()));
00197     object_pose.orientation.x = quat.x();
00198     object_pose.orientation.y = quat.y();
00199     object_pose.orientation.z = quat.z();
00200     object_pose.orientation.w = quat.w();
00201   }
00202 
00203   double fRand(double fMin, double fMax)
00204   {
00205     double f = (double)rand() / RAND_MAX;
00206     return fMin + f * (fMax - fMin);
00207   }
00208 
00209 }; // end of class
00210 
00211 } // namespace
00212 
00213 
00214 int main(int argc, char *argv[])
00215 {
00216   int num_tests = 10;
00217   ros::init(argc, argv, "grasp_generator_test");
00218 
00219   ROS_INFO_STREAM_NAMED("main","Simple Grasps Test");
00220 
00221   ros::AsyncSpinner spinner(2);
00222   spinner.start();
00223 
00224   // Seed random
00225   srand(ros::Time::now().toSec());
00226 
00227   // Benchmark time
00228   ros::Time start_time;
00229   start_time = ros::Time::now();
00230 
00231   // Run Tests
00232   baxter_pick_place::GraspGeneratorTest tester(num_tests);
00233 
00234   // Benchmark time
00235   double duration = (ros::Time::now() - start_time).toNSec() * 1e-6;
00236   ROS_INFO_STREAM_NAMED("","Total time: " << duration);
00237   //std::cout << duration << "\t" << num_tests << std::endl;
00238 
00239   ros::Duration(1.0).sleep(); // let rviz markers finish publishing
00240 
00241   return 0;
00242 }


moveit_simple_grasps
Author(s): Dave Coleman
autogenerated on Thu Jun 6 2019 20:55:20