grasp_filter_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 filter
00037 */
00038 
00039 // ROS
00040 #include <ros/ros.h>
00041 #include <geometry_msgs/PoseArray.h>
00042 #include <sensor_msgs/JointState.h>
00043 #include <Eigen/Core>
00044 #include <Eigen/Geometry>
00045 
00046 // MoveIt
00047 #include <moveit_msgs/MoveGroupAction.h>
00048 #include <moveit_msgs/DisplayTrajectory.h>
00049 #include <moveit_msgs/RobotState.h>
00050 #include <moveit/kinematic_constraints/utils.h>
00051 #include <moveit/robot_state/robot_state.h>
00052 #include <moveit/robot_state/conversions.h>
00053 #include <moveit/robot_model_loader/robot_model_loader.h>
00054 
00055 // Rviz
00056 #include <visualization_msgs/Marker.h>
00057 #include <visualization_msgs/MarkerArray.h>
00058 
00059 // Grasp
00060 #include <moveit_simple_grasps/simple_grasps.h>
00061 #include <moveit_simple_grasps/grasp_filter.h>
00062 #include <moveit_visual_tools/moveit_visual_tools.h>
00063 
00064 // Baxter specific properties
00065 #include <moveit_simple_grasps/grasp_data.h>
00066 #include <moveit_simple_grasps/custom_environment2.h>
00067 
00068 namespace moveit_simple_grasps
00069 {
00070 
00071 // Table dimensions
00072 static const double TABLE_HEIGHT = .92;
00073 static const double TABLE_WIDTH = .85;
00074 static const double TABLE_DEPTH = .47;
00075 static const double TABLE_X = 0.66;
00076 static const double TABLE_Y = 0;
00077 static const double TABLE_Z = -0.9/2+0.01;
00078 
00079 static const double BLOCK_SIZE = 0.04;
00080 
00081 class GraspGeneratorTest
00082 {
00083 private:
00084   // A shared node handle
00085   ros::NodeHandle nh_;
00086 
00087   // Grasp generator
00088   moveit_simple_grasps::SimpleGraspsPtr simple_grasps_;
00089 
00090   // Tool for visualizing things in Rviz
00091   moveit_visual_tools::MoveItVisualToolsPtr visual_tools_;
00092 
00093   // Grasp filter
00094   moveit_simple_grasps::GraspFilterPtr grasp_filter_;
00095 
00096   // data for generating grasps
00097   moveit_simple_grasps::GraspData grasp_data_;
00098 
00099   // Shared planning scene (load once for everything)
00100   planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor_;
00101 
00102   // which baxter arm are we using
00103   std::string arm_;
00104   std::string ee_group_name_;
00105   std::string planning_group_name_;
00106 
00107 public:
00108 
00109   // Constructor
00110   GraspGeneratorTest(int num_tests)
00111     : nh_("~")
00112   {
00113     // Get arm info from param server
00114     nh_.param("arm", arm_, std::string("left"));
00115     nh_.param("ee_group_name", ee_group_name_, std::string(arm_ + "_hand"));
00116     planning_group_name_ = arm_ + "_arm";
00117 
00118     ROS_INFO_STREAM_NAMED("test","Arm: " << arm_);
00119     ROS_INFO_STREAM_NAMED("test","End Effector: " << ee_group_name_);
00120     ROS_INFO_STREAM_NAMED("test","Planning Group: " << planning_group_name_);
00121 
00122     // ---------------------------------------------------------------------------------------------
00123     // Load grasp data
00124     if (!grasp_data_.loadRobotGraspData(nh_, ee_group_name_))
00125       ros::shutdown();
00126 
00127     // ---------------------------------------------------------------------------------------------
00128     // Load planning scene to share
00129     planning_scene_monitor_.reset(new planning_scene_monitor::PlanningSceneMonitor("robot_description"));
00130 
00131     // ---------------------------------------------------------------------------------------------
00132     // Load the Robot Viz Tools for publishing to Rviz
00133     visual_tools_.reset(new moveit_visual_tools::MoveItVisualTools(grasp_data_.base_link_, "/end_effector_marker", planning_scene_monitor_));
00134     visual_tools_->setLifetime(40.0);
00135     visual_tools_->loadEEMarker(grasp_data_.ee_group_);
00136     visual_tools_->setFloorToBaseHeight(-0.9);
00137 
00138     // Clear out old collision objects just because
00139     //visual_tools_->removeAllCollisionObjects();
00140 
00141     // Create a collision table for fun
00142     visual_tools_->publishCollisionTable(TABLE_X, TABLE_Y, 0, TABLE_WIDTH, TABLE_HEIGHT, TABLE_DEPTH, "table");
00143 
00144     // ---------------------------------------------------------------------------------------------
00145     // Load grasp generator
00146     simple_grasps_.reset( new moveit_simple_grasps::SimpleGrasps(visual_tools_) );
00147 
00148     // ---------------------------------------------------------------------------------------------
00149     // Load grasp filter
00150     robot_state::RobotState robot_state = planning_scene_monitor_->getPlanningScene()->getCurrentState();
00151     grasp_filter_.reset(new moveit_simple_grasps::GraspFilter(robot_state, visual_tools_) );
00152 
00153     // ---------------------------------------------------------------------------------------------
00154     // Generate grasps for a bunch of random objects
00155     geometry_msgs::Pose object_pose;
00156     std::vector<moveit_msgs::Grasp> possible_grasps;
00157     std::vector<trajectory_msgs::JointTrajectoryPoint> ik_solutions; // save each grasps ik solution for visualization
00158 
00159     // Loop
00160     for (int i = 0; i < num_tests; ++i)
00161     {
00162       ROS_INFO_STREAM_NAMED("test","Adding random object " << i+1 << " of " << num_tests);
00163 
00164       // Remove randomness when we are only running one test
00165       if (num_tests == 1)
00166         generateTestObject(object_pose);
00167       else
00168         generateRandomObject(object_pose);
00169 
00170       // Show the block
00171       visual_tools_->publishBlock(object_pose, rviz_visual_tools::BLUE, BLOCK_SIZE);
00172 
00173       possible_grasps.clear();
00174       ik_solutions.clear();
00175 
00176       // Generate set of grasps for one object
00177       simple_grasps_->generateBlockGrasps( object_pose, grasp_data_, possible_grasps);
00178 
00179       // Filter the grasp for only the ones that are reachable
00180       bool filter_pregrasps = true;
00181       grasp_filter_->filterGrasps(possible_grasps, ik_solutions, filter_pregrasps, grasp_data_.ee_parent_link_, planning_group_name_);
00182 
00183       // Visualize them
00184       const robot_model::JointModelGroup* ee_jmg = planning_scene_monitor_->getRobotModel()->getJointModelGroup(grasp_data_.ee_group_);
00185       visual_tools_->publishAnimatedGrasps(possible_grasps, ee_jmg);
00186       const robot_model::JointModelGroup* arm_jmg = planning_scene_monitor_->getRobotModel()->getJointModelGroup(planning_group_name_);
00187       visual_tools_->publishIKSolutions(ik_solutions, arm_jmg, 0.25);
00188 
00189       // Make sure ros is still going
00190       if(!ros::ok())
00191         break;
00192     }
00193 
00194 
00195   }
00196 
00197   void generateTestObject(geometry_msgs::Pose& object_pose)
00198   {
00199     // Position
00200     geometry_msgs::Pose start_object_pose;
00201     geometry_msgs::Pose end_object_pose;
00202 
00203     start_object_pose.position.x = 0.8;
00204     start_object_pose.position.y = -0.5;
00205     start_object_pose.position.z = 0.02;
00206 
00207     end_object_pose.position.x = 0.25;
00208     end_object_pose.position.y = 0.15;
00209     end_object_pose.position.z = 0.02;
00210 
00211     // Orientation
00212     double angle = M_PI / 1.5;
00213     Eigen::Quaterniond quat(Eigen::AngleAxis<double>(double(angle), Eigen::Vector3d::UnitZ()));
00214     start_object_pose.orientation.x = quat.x();
00215     start_object_pose.orientation.y = quat.y();
00216     start_object_pose.orientation.z = quat.z();
00217     start_object_pose.orientation.w = quat.w();
00218 
00219     angle = M_PI / 1.1;
00220     quat = Eigen::Quaterniond(Eigen::AngleAxis<double>(double(angle), Eigen::Vector3d::UnitZ()));
00221     end_object_pose.orientation.x = quat.x();
00222     end_object_pose.orientation.y = quat.y();
00223     end_object_pose.orientation.z = quat.z();
00224     end_object_pose.orientation.w = quat.w();
00225 
00226     // Choose which object to test
00227     object_pose = start_object_pose;
00228   }
00229 
00230   void generateRandomObject(geometry_msgs::Pose& object_pose)
00231   {
00232     // Position
00233     object_pose.position.x = visual_tools_->dRand(0.7,TABLE_DEPTH);
00234     object_pose.position.y = visual_tools_->dRand(-TABLE_WIDTH/2,-0.1);
00235     object_pose.position.z = TABLE_Z + TABLE_HEIGHT / 2.0 + BLOCK_SIZE / 2.0;
00236 
00237     // Orientation
00238     double angle = M_PI * visual_tools_->dRand(0.1,1);
00239     Eigen::Quaterniond quat(Eigen::AngleAxis<double>(double(angle), Eigen::Vector3d::UnitZ()));
00240     object_pose.orientation.x = quat.x();
00241     object_pose.orientation.y = quat.y();
00242     object_pose.orientation.z = quat.z();
00243     object_pose.orientation.w = quat.w();
00244   }
00245 
00246 }; // end of class
00247 
00248 } // namespace
00249 
00250 
00251 int main(int argc, char *argv[])
00252 {
00253   int num_tests = 5;
00254 
00255   ros::init(argc, argv, "grasp_generator_test");
00256 
00257   // Allow the action server to recieve and send ros messages
00258   ros::AsyncSpinner spinner(2);
00259   spinner.start();
00260 
00261   // Check for verbose flag
00262   bool verbose = false;
00263   if (argc > 1)
00264   {
00265     for (std::size_t i = 0; i < argc; ++i)
00266     {
00267       if (strcmp(argv[i], "--verbose") == 0)
00268       {
00269         ROS_INFO_STREAM_NAMED("main","Running in VERBOSE mode (much slower)");
00270         verbose = true;
00271       }
00272     }
00273   }
00274 
00275   // Seed random
00276   srand(ros::Time::now().toSec());
00277 
00278   // Benchmark time
00279   ros::Time start_time;
00280   start_time = ros::Time::now();
00281 
00282   // Run Tests
00283   moveit_simple_grasps::GraspGeneratorTest tester(num_tests);
00284 
00285   // Benchmark time
00286   double duration = (ros::Time::now() - start_time).toNSec() * 1e-6;
00287   ROS_INFO_STREAM_NAMED("","Total time: " << duration);
00288   std::cout << duration << "\t" << num_tests << std::endl;
00289 
00290   ros::Duration(1.0).sleep(); // let rviz markers finish publishing
00291 
00292   return 0;
00293 }


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