pr2_moveit_planning_scene_speed_test.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2012, Willow Garage, Inc.
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 Willow Garage 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 
00037 #include <gtest/gtest.h>
00038 #include <sstream>
00039 #include <algorithm>
00040 #include <ctype.h>
00041 #include <planning_models/robot_model.h>
00042 #include <planning_models/kinematic_state.h>
00043 #include <planning_models/transforms.h>
00044 #include <geometric_shapes/shape_operations.h>
00045 #include <planning_scene_monitor/planning_scene_monitor.h>
00046 #include <collision_distance_field_ros/collision_robot_hybrid_ros.h>
00047 #include <collision_distance_field/collision_world_hybrid.h>
00048 
00049 #include <boost/filesystem.hpp>
00050 #include <ros/console.h>
00051 
00052 static const unsigned int TRIALS = 10000;
00053 
00054 class Pr2DistanceFieldPlanningSceneTester : public testing::Test{
00055 
00056 protected:
00057 
00058   virtual void SetUp()
00059   {
00060 
00061     robot_model_loader_.reset(new robot_model_loader::RDFLoader("robot_description"));
00062 
00063     planning_scene::PlanningScenePtr ps(new planning_scene::PlanningScene());
00064     ps->setActiveCollisionDetector(collision_detection::CollisionDetectorAllocatorHybridROS::create());
00065     planning_scene_monitor_.reset(new planning_scene_monitor::PlanningSceneMonitor(ps, robot_model_loader_));
00066   }
00067 
00068   virtual void TearDown()
00069   {
00070 
00071   }
00072 
00073 protected:
00074 
00075   boost::shared_ptr<robot_model_loader::RDFLoader> robot_model_loader_;
00076   boost::shared_ptr<planning_scene_monitor::PlanningSceneMonitor> planning_scene_monitor_;
00077 };
00078 
00079 TEST_F(Pr2DistanceFieldPlanningSceneTester, SpeedTestSlow)
00080 {
00081   planning_models::RobotState *kstate(planning_scene_monitor_->getPlanningScene()->getCurrentState());
00082   planning_models::RobotState *::JointStateGroup* jsg = kstate.getJointStateGroup("right_arm");
00083 
00084   Eigen::Affine3d id = Eigen::Affine3d::Identity();
00085   id.translation().x() = .5;
00086   id.translation().z() = .7;
00087   planning_scene_monitor_->getPlanningScene()->getWorldNonConst()->addToObject("sphere", shapes::ShapeConstPtr(new shapes::Sphere(0.4)), id);
00088 
00089   const collision_detection::CollisionWorldHybrid* hy_world
00090     = dynamic_cast<const collision_detection::CollisionWorldHybrid*>(planning_scene_monitor_->getPlanningScene()->getCollisionWorld().get());
00091   ASSERT_TRUE(hy_world);
00092 
00093   const collision_detection::CollisionRobotHybrid* hy_robot
00094     = dynamic_cast<const collision_detection::CollisionRobotHybrid*>(planning_scene_monitor_->getPlanningScene()->getCollisionRobot().get());
00095   ASSERT_TRUE(hy_robot);
00096 
00097   ros::WallDuration total_speed_df;
00098   ros::WallDuration total_speed_fcl;
00099 
00100   unsigned int in_collision_df = 0;
00101   unsigned int in_collision_fcl = 0;
00102   unsigned int fcl_in_coll_df_not = 0;
00103 
00104   collision_detection::CollisionRequest req;
00105   req.group_name = "right_arm";
00106   req.contacts = true;
00107 
00108   collision_detection::CollisionResult res1;
00109   //first check with this group doesn't count
00110   hy_world->checkCollisionDistanceField(req,
00111                                         res1,
00112                                         *hy_robot->getCollisionRobotDistanceField().get(),
00113                                         kstate,
00114                                         planning_scene_monitor_->getPlanningScene()->getAllowedCollisionMatrix());
00115   planning_scene_monitor_->getPlanningScene()->checkCollision(req,
00116                                                               res1,
00117                                                               kstate,
00118                                                               planning_scene_monitor_->getPlanningScene()->getAllowedCollisionMatrix());
00119 
00120   for(unsigned int i = 0; i < TRIALS; i++) {
00121     jsg->setToRandomValues();
00122     collision_detection::CollisionResult res;
00123     ros::WallTime before = ros::WallTime::now();
00124     hy_world->checkCollisionDistanceField(req,
00125                                           res,
00126                                           *hy_robot->getCollisionRobotDistanceField().get(),
00127                                           kstate,
00128                                           planning_scene_monitor_->getPlanningScene()->getAllowedCollisionMatrix());
00129     total_speed_df += (ros::WallTime::now()-before);
00130     bool df_in_collision = false;
00131     if(res.collision) {
00132       in_collision_df++;
00133       df_in_collision = true;
00134     }
00135     res = collision_detection::CollisionResult();
00136     before = ros::WallTime::now();
00137     planning_scene_monitor_->getPlanningScene()->checkCollision(req,
00138                                                                 res,
00139                                                                 kstate,
00140                                                                 planning_scene_monitor_->getPlanningScene()->getAllowedCollisionMatrix());
00141 
00142     total_speed_fcl += (ros::WallTime::now()-before);
00143     if(res.collision) {
00144       in_collision_fcl++;
00145       if(!df_in_collision) {
00146         fcl_in_coll_df_not++;
00147       }
00148     }
00149   }
00150   std::cerr << "Average time df " << total_speed_df.toSec()/(1.0*TRIALS) << " hz " << (1.0/(total_speed_df.toSec()/(1.0*TRIALS))) << std::endl;
00151   std::cerr << "In collision df " << in_collision_df << std::endl;
00152   std::cerr << "Average time fcl " << total_speed_fcl.toSec()/(1.0*TRIALS) << " hz " << (1.0/(total_speed_fcl.toSec()/(1.0*TRIALS))) << std::endl;
00153   std::cerr << "In collision fcl " << in_collision_fcl << std::endl;
00154   std::cerr << "Fcl in collision df not " << fcl_in_coll_df_not << std::endl;
00155 }
00156 
00157 TEST_F(Pr2DistanceFieldPlanningSceneTester, SpeedTestFast)
00158 {
00159   planning_models::RobotState *kstate(planning_scene_monitor_->getPlanningScene()->getCurrentState());
00160   planning_models::RobotState *::JointStateGroup* jsg = kstate.getJointStateGroup("right_arm");
00161 
00162   Eigen::Affine3d id = Eigen::Affine3d::Identity();
00163   id.translation().x() = .5;
00164   id.translation().z() = .7;
00165   planning_scene_monitor_->getPlanningScene()->getWorldNonConst()->addToObject("sphere", shapes::ShapeConstPtr(new shapes::Sphere(0.4)), id);
00166 
00167   const collision_detection::CollisionWorldHybrid* hy_world
00168     = dynamic_cast<const collision_detection::CollisionWorldHybrid*>(planning_scene_monitor_->getPlanningScene()->getCollisionWorld().get());
00169   ASSERT_TRUE(hy_world);
00170 
00171   const collision_detection::CollisionRobotHybrid* hy_robot
00172     = dynamic_cast<const collision_detection::CollisionRobotHybrid*>(planning_scene_monitor_->getPlanningScene()->getCollisionRobot().get());
00173   ASSERT_TRUE(hy_robot);
00174 
00175   ros::WallDuration total_speed_df;
00176   ros::WallDuration total_speed_fcl;
00177 
00178   unsigned int in_collision_df = 0;
00179   unsigned int in_collision_fcl = 0;
00180   unsigned int fcl_in_coll_df_not = 0;
00181 
00182   collision_detection::CollisionRequest req;
00183   req.group_name = "right_arm";
00184   req.contacts = true;
00185 
00186   collision_detection::CollisionResult res1;
00187   //first check with this group doesn't count
00188   boost::shared_ptr<collision_detection::GroupStateRepresentation> gsr;
00189   hy_world->checkCollisionDistanceField(req,
00190                                         res1,
00191                                         *hy_robot->getCollisionRobotDistanceField().get(),
00192                                         kstate,
00193                                         planning_scene_monitor_->getPlanningScene()->getAllowedCollisionMatrix(),
00194                                         gsr);
00195   planning_scene_monitor_->getPlanningScene()->checkCollision(req,
00196                                                               res1,
00197                                                               kstate,
00198                                                               planning_scene_monitor_->getPlanningScene()->getAllowedCollisionMatrix());
00199 
00200   for(unsigned int i = 0; i < TRIALS; i++) {
00201     jsg->setToRandomValues();
00202     collision_detection::CollisionResult res;
00203     ros::WallTime before = ros::WallTime::now();
00204     hy_world->checkCollisionDistanceField(req,
00205                                           res,
00206                                           *hy_robot->getCollisionRobotDistanceField().get(),
00207                                           kstate,
00208                                           gsr);
00209     ros::WallDuration dur(ros::WallTime::now()-before);
00210     total_speed_df += dur;
00211     //std::cerr << "Took " << dur << std::endl;
00212     bool df_in_collision = false;
00213     if(res.collision) {
00214       in_collision_df++;
00215       df_in_collision = true;
00216     }
00217     res = collision_detection::CollisionResult();
00218     before = ros::WallTime::now();
00219     planning_scene_monitor_->getPlanningScene()->checkCollision(req,
00220                                                                 res,
00221                                                                 kstate,
00222                                                                 planning_scene_monitor_->getPlanningScene()->getAllowedCollisionMatrix());
00223 
00224     total_speed_fcl += (ros::WallTime::now()-before);
00225     if(res.collision) {
00226       in_collision_fcl++;
00227       if(!df_in_collision) {
00228         fcl_in_coll_df_not++;
00229       }
00230     }
00231   }
00232   std::cerr << "Average time df " << total_speed_df.toSec()/(1.0*TRIALS) << " hz " << (1.0/(total_speed_df.toSec()/(1.0*TRIALS))) << std::endl;
00233   std::cerr << "In collision df " << in_collision_df << std::endl;
00234   std::cerr << "Average time fcl " << total_speed_fcl.toSec()/(1.0*TRIALS) << " hz " << (1.0/(total_speed_fcl.toSec()/(1.0*TRIALS))) << std::endl;
00235   std::cerr << "In collision fcl " << in_collision_fcl << std::endl;
00236   std::cerr << "Fcl in collision df not " << fcl_in_coll_df_not << std::endl;
00237 }
00238 
00239 
00240 int main(int argc, char **argv)
00241 {
00242   ros::init(argc, argv, "pr2_moveit_planning_scene_speed_test");
00243   testing::InitGoogleTest(&argc, argv);
00244   return RUN_ALL_TESTS();
00245 }


pr2_test_collision_distance_field
Author(s): E. Gil Jones
autogenerated on Mon Oct 6 2014 11:17:14