pr2_sbpl_ompl_planner_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 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 
00035 /* Author: E. Gil Jones */
00036 
00037 #include <gtest/gtest.h>
00038 #include <urdf_parser/urdf_parser.h>
00039 #include <sbpl_interface/sbpl_meta_interface.h>
00040 #include <ompl_interface_ros/ompl_interface_ros.h>
00041 #include <moveit_msgs/GetMotionPlan.h>
00042 #include <kinematic_constraints/utils.h>
00043 #include <planning_models/conversions.h>
00044 #include <collision_distance_field/collision_world_hybrid.h>
00045 #include <collision_distance_field_ros/collision_robot_hybrid_ros.h>
00046 #include <rdf_loader/rdf_loader.h>
00047 #include <ompl_interface_ros/ompl_interface_ros.h>
00048 
00049 class Pr2SBPLOMPLPlannerTester : public testing::Test{
00050 
00051 protected:
00052 
00053   virtual void SetUp()
00054   {
00055 
00056     rml_.reset(new rdf_loader::RDFLoader("robot_description"));
00057 
00058     acm_.reset(new collision_detection::AllowedCollisionMatrix());
00059 
00060     ros::NodeHandle nh;
00061 
00062     XmlRpc::XmlRpcValue coll_ops;
00063     nh.getParam("robot_description_planning/default_collision_operations", coll_ops);
00064 
00065     if (coll_ops.getType() != XmlRpc::XmlRpcValue::TypeArray)
00066     {
00067       ROS_WARN("default_collision_operations is not an array");
00068       return;
00069     }
00070 
00071     if (coll_ops.size() == 0)
00072     {
00073       ROS_WARN("No collision operations in default collision operations");
00074       return;
00075     }
00076 
00077     for (int i = 0 ; i < coll_ops.size() ; ++i)
00078     {
00079       if (!coll_ops[i].hasMember("object1") || !coll_ops[i].hasMember("object2") || !coll_ops[i].hasMember("operation"))
00080       {
00081         ROS_WARN("All collision operations must have two objects and an operation");
00082         continue;
00083       }
00084       acm_->setEntry(std::string(coll_ops[i]["object1"]), std::string(coll_ops[i]["object2"]), std::string(coll_ops[i]["operation"]) == "disable");
00085     }
00086   }
00087 
00088   virtual void TearDown()
00089   {
00090 
00091   }
00092 
00093 protected:
00094 
00095   boost::shared_ptr<rdf_loader::RDFLoader> rml_;
00096   collision_detection::AllowedCollisionMatrixPtr acm_;
00097 
00098 };
00099 
00100 TEST_F(Pr2SBPLOMPLPlannerTester, SimplePlan)
00101 {
00102   planning_scene::PlanningScenePtr ps(new planning_scene::PlanningScene());
00103   ps->setActiveCollisionDetector(collision_detection::CollisionDetectorAllocatorHybridROS::create());
00104   ps->configure(rml_->getURDF(), rml_->getSRDF());
00105   ASSERT_TRUE(ps->isConfigured());
00106   ps->getAllowedCollisionMatrixNonConst() = *acm_;
00107 
00108   sbpl_interface::SBPLMetaInterface sbpl_planner(ps->getRobotModel());
00109   ompl_interface_ros::OMPLInterfaceROS ompl_planner(ps->getRobotModel());
00110 
00111   moveit_msgs::GetMotionPlan::Request mplan_req;
00112   moveit_msgs::GetMotionPlan::Response mplan_res_sbpl;
00113   mplan_req.motion_plan_request.group_name = "right_arm";
00114   mplan_req.motion_plan_request.num_planning_attempts = 5;
00115   mplan_req.motion_plan_request.allowed_planning_time = ros::Duration(5.0);
00116   const std::vector<std::string>& joint_names = ps->getRobotModel()->getJointModelGroup("right_arm")->getJointModelNames();
00117   mplan_req.motion_plan_request.goal_constraints.resize(1);
00118   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints.resize(joint_names.size());
00119   for(unsigned int i = 0; i < joint_names.size(); i++)
00120   {
00121     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].joint_name = joint_names[i];
00122     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].position = 0.0;
00123     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_above = 0.001;
00124     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_below = 0.001;
00125     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].weight = 1.0;
00126   }
00127   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[0].position = -2.0;
00128   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[3].position = -.2;
00129   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[5].position = -.2;
00130 
00131   ros::WallTime before_sbpl = ros::WallTime::now();
00132   sbpl_planner.solve(ps,
00133                      mplan_req,
00134                      mplan_res_sbpl);
00135   std::cerr << "sbpl took " << (ros::WallTime::now()-before_sbpl) << std::endl;
00136 
00137   ASSERT_EQ(mplan_res_sbpl.error_code.val, mplan_res_sbpl.error_code.SUCCESS);
00138   EXPECT_GT(mplan_res_sbpl.trajectory.joint_trajectory.points.size(), 0);
00139 
00140   moveit_msgs::GetMotionPlan::Response mplan_res_ompl;
00141   ros::WallTime before_ompl = ros::WallTime::now();
00142   ompl_planner.solve(ps,
00143                      mplan_req,
00144                      mplan_res_ompl);
00145   std::cerr << "ompl took " << (ros::WallTime::now()-before_ompl) << std::endl;
00146 
00147   ASSERT_EQ(mplan_res_ompl.error_code.val, mplan_res_ompl.error_code.SUCCESS);
00148   EXPECT_GT(mplan_res_ompl.trajectory.joint_trajectory.points.size(), 0);
00149 }
00150 
00151 static const unsigned int NUM_TRIALS = 10;
00152 
00153 TEST_F(Pr2SBPLOMPLPlannerTester, ManyPlan)
00154 {
00155   planning_scene::PlanningScenePtr ps(new planning_scene::PlanningScene());
00156   ps->setActiveCollisionDetector(collision_detection::CollisionDetectorAllocatorHybridROS::create());
00157   ps->configure(rml_->getURDF(), rml_->getSRDF());
00158   ASSERT_TRUE(ps->isConfigured());
00159   ps->getAllowedCollisionMatrixNonConst() = *acm_;
00160 
00161   planning_models::RobotState *::JointStateGroup* start_jsg = ps->getCurrentState().getJointStateGroup("right_arm");
00162   planning_models::RobotState *goal_state(ps->getCurrentState());
00163   planning_models::RobotState *::JointStateGroup* goal_jsg = goal_state.getJointStateGroup("right_arm");
00164 
00165   sbpl_interface::SBPLMetaInterface sbpl_planner(ps->getRobotModel());
00166   ompl_interface_ros::OMPLInterfaceROS ompl_planner(ps->getRobotModel());
00167 
00168   moveit_msgs::GetMotionPlan::Request mplan_req;
00169   moveit_msgs::GetMotionPlan::Response mplan_res_sbpl;
00170   moveit_msgs::GetMotionPlan::Response mplan_res_ompl;
00171   mplan_req.motion_plan_request.group_name = "right_arm";
00172   mplan_req.motion_plan_request.num_planning_attempts = 5;
00173   mplan_req.motion_plan_request.allowed_planning_time = ros::Duration(5.0);
00174   const std::vector<std::string>& joint_names = ps->getRobotModel()->getJointModelGroup("right_arm")->getJointModelNames();
00175   mplan_req.motion_plan_request.goal_constraints.resize(1);
00176   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints.resize(joint_names.size());
00177   for(unsigned int i = 0; i < joint_names.size(); i++)
00178   {
00179     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].joint_name = joint_names[i];
00180     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].position = 0.0;
00181     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_above = 0.001;
00182     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_below = 0.001;
00183     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].weight = 1.0;
00184   }
00185 
00186   unsigned int comp_trials = 0;
00187   unsigned int succ_trials = 0;
00188   double max_planning_time = 0.0;
00189   double total_planning_time = 0.0;
00190   double max_sbpl_solve_time = 0.0;
00191   double total_sbpl_solve_time = 0.0;
00192   double max_ompl_solve_time = 0.0;
00193   double total_ompl_solve_time = 0.0;
00194   while(comp_trials < NUM_TRIALS) {
00195     while(1) {
00196       start_jsg->setToRandomValues();
00197       goal_jsg->setToRandomValues();
00198       std::vector<double> goal_vals;
00199       goal_jsg->getGroupStateValues(goal_vals);
00200       for(unsigned int i = 0; i < joint_names.size(); i++) {
00201         mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].position = goal_vals[i];
00202       }
00203       std::vector<double> start_vals;
00204       start_jsg->getGroupStateValues(start_vals);
00205       for(unsigned int i = 0; i < start_vals.size(); i++) {
00206         std::cerr << "Start joint " << i << " val " << start_vals[i] << std::endl;
00207       }
00208       for(unsigned int i = 0; i < goal_vals.size(); i++) {
00209         std::cerr << "Goal joint " << i << " val " << goal_vals[i] << std::endl;
00210       }
00211       moveit_msgs::GetMotionPlan::Response mplan_res;
00212       ros::WallTime before_sbpl = ros::WallTime::now();
00213       if(sbpl_planner.solve(ps,
00214                             mplan_req,
00215                             mplan_res)) {
00216         double st = (ros::WallTime::now()-before_sbpl).toSec();
00217         if(st > max_sbpl_solve_time) {
00218           max_sbpl_solve_time = st;
00219         }
00220         total_sbpl_solve_time += st;
00221         comp_trials++;
00222         succ_trials++;
00223         if(sbpl_planner.getLastPlanningStatistics().total_planning_time_.toSec() > max_planning_time) {
00224           max_planning_time = sbpl_planner.getLastPlanningStatistics().total_planning_time_.toSec();
00225         }
00226         ASSERT_LT(sbpl_planner.getLastPlanningStatistics().total_planning_time_.toSec(), 5.0);
00227         total_planning_time += sbpl_planner.getLastPlanningStatistics().total_planning_time_.toSec();
00228         ros::WallTime before_ompl = ros::WallTime::now();
00229         if(ompl_planner.solve(ps,
00230                               mplan_req,
00231                               mplan_res_ompl)) {
00232           double st = (ros::WallTime::now()-before_sbpl).toSec();
00233           if(st > max_sbpl_solve_time) {
00234             max_ompl_solve_time = st;
00235           }
00236           total_ompl_solve_time += st;
00237         } else {
00238           std::cerr << "ompl has a problem" << std::endl;
00239         }
00240         break;
00241       } else {
00242         if(mplan_res.error_code.val != moveit_msgs::MoveItErrorCodes::GOAL_IN_COLLISION &&
00243            mplan_res.error_code.val != moveit_msgs::MoveItErrorCodes::START_STATE_IN_COLLISION) {
00244           std::cerr << "Bad error code " << mplan_res.error_code.val << std::endl;
00245           comp_trials++;
00246           break;
00247         } else {
00248           //std::cerr << "Something in collision" << std::endl;
00249         }
00250       }
00251     }
00252   }
00253 
00254   std::cerr << "Average sbpl time " << total_sbpl_solve_time/(comp_trials*1.0) << " max " << max_sbpl_solve_time << std::endl;
00255   std::cerr << "Average ompl time " << total_ompl_solve_time/(comp_trials*1.0) << " max " << max_ompl_solve_time << std::endl;
00256 
00257   std::cerr << "Average planning time " << total_planning_time/(comp_trials*1.0) << " max " << max_planning_time << std::endl;
00258   EXPECT_EQ(succ_trials, comp_trials);
00259 
00260   //ASSERT_EQ(mplan_res.error_code.val, mplan_res.error_code.SUCCESS);
00261   //EXPECT_GT(mplan_res.trajectory.joint_trajectory.points.size(), 0);
00262 }
00263 
00264 int main(int argc, char **argv)
00265 {
00266   testing::InitGoogleTest(&argc, argv);
00267   ros::init(argc, argv, "test_sbpl_planning");
00268 
00269   return RUN_ALL_TESTS();
00270 }


pr2_test_sbpl_planner
Author(s): E. Gil Jones
autogenerated on Mon Sep 14 2015 14:17:56