pr2_sbpl_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_interface.h>
00040 #include <moveit_msgs/GetMotionPlan.h>
00041 #include <kinematic_constraints/utils.h>
00042 #include <planning_models/conversions.h>
00043 #include <collision_distance_field/collision_world_hybrid.h>
00044 #include <collision_distance_field_ros/collision_robot_hybrid_ros.h>
00045 #include <rdf_loader/rdf_loader.h>
00046 
00047 class Pr2SBPLPlannerTester : public testing::Test{
00048 
00049 protected:
00050 
00051   virtual void SetUp()
00052   {
00053 
00054     rml_.reset(new rdf_loader::RDFLoader("robot_description"));
00055 
00056     acm_.reset(new collision_detection::AllowedCollisionMatrix());
00057 
00058     ros::NodeHandle nh;
00059 
00060     XmlRpc::XmlRpcValue coll_ops;
00061     nh.getParam("robot_description_planning/default_collision_operations", coll_ops);
00062 
00063     if (coll_ops.getType() != XmlRpc::XmlRpcValue::TypeArray)
00064     {
00065       ROS_WARN("default_collision_operations is not an array");
00066       return;
00067     }
00068 
00069     if (coll_ops.size() == 0)
00070     {
00071       ROS_WARN("No collision operations in default collision operations");
00072       return;
00073     }
00074 
00075     for (int i = 0 ; i < coll_ops.size() ; ++i)
00076     {
00077       if (!coll_ops[i].hasMember("object1") || !coll_ops[i].hasMember("object2") || !coll_ops[i].hasMember("operation"))
00078       {
00079         ROS_WARN("All collision operations must have two objects and an operation");
00080         continue;
00081       }
00082       acm_->setEntry(std::string(coll_ops[i]["object1"]), std::string(coll_ops[i]["object2"]), std::string(coll_ops[i]["operation"]) == "disable");
00083     }
00084   }
00085 
00086   virtual void TearDown()
00087   {
00088 
00089   }
00090 
00091 protected:
00092 
00093   boost::shared_ptr<rdf_loader::RDFLoader> rml_;
00094   collision_detection::AllowedCollisionMatrixPtr acm_;
00095 
00096 };
00097 
00098 // TEST_F(Pr2SBPLPlannerTester, SimplePlan)
00099 // {
00100 //   planning_scene::PlanningScenePtr ps(new planning_scene::PlanningScene());
00101 //   ps->setActiveCollisionDetector(collision_detection::CollisionDetectorAllocatorHybridROS::create());
00102 //   ps->configure(rml_->getURDF(), rml_->getSRDF());
00103 //   ASSERT_TRUE(ps->isConfigured());
00104 //   ps->getAllowedCollisionMatrixNonConst() = *acm_;
00105 
00106 //   sbpl_interface::SBPLInterface sbpl_planner(ps->getRobotModel());
00107 
00108 //   moveit_msgs::GetMotionPlan::Request mplan_req;
00109 //   moveit_msgs::GetMotionPlan::Response mplan_res;
00110 //   mplan_req.motion_plan_request.group_name = "right_arm";
00111 //   mplan_req.motion_plan_request.num_planning_attempts = 5;
00112 //   mplan_req.motion_plan_request.allowed_planning_time = ros::Duration(5.0);
00113 //   const std::vector<std::string>& joint_names = ps->getRobotModel()->getJointModelGroup("right_arm")->getJointModelNames();
00114 //   mplan_req.motion_plan_request.goal_constraints.resize(1);
00115 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints.resize(joint_names.size());
00116 //   for(unsigned int i = 0; i < joint_names.size(); i++)
00117 //   {
00118 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].joint_name = joint_names[i];
00119 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].position = 0.0;
00120 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_above = 0.001;
00121 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_below = 0.001;
00122 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].weight = 1.0;
00123 //   }
00124 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[0].position = -2.0;
00125 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[3].position = -.2;
00126 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[5].position = -.2;
00127 
00128 //   sbpl_planner.solve(ps,
00129 //                      mplan_req,
00130 //                      mplan_res);
00131 
00132 //   ASSERT_EQ(mplan_res.error_code.val, mplan_res.error_code.SUCCESS);
00133 //   EXPECT_GT(mplan_res.trajectory.joint_trajectory.points.size(), 0);
00134 // }
00135 
00136 // TEST_F(Pr2SBPLPlannerTester, DiscretizePlan)
00137 // {
00138 //   planning_scene::PlanningScenePtr ps(new planning_scene::PlanningScene());
00139 //   ps->setActiveCollisionDetector(collision_detection::CollisionDetectorAllocatorHybridROS::create());
00140 //   ps->configure(rml_->getURDF(), rml_->getSRDF());
00141 //   ASSERT_TRUE(ps->isConfigured());
00142 //   ps->getAllowedCollisionMatrixNonConst() = *acm_;
00143 
00144 //   sbpl_interface::SBPLInterface sbpl_planner(ps->getRobotModel());
00145 
00146 //   moveit_msgs::GetMotionPlan::Request mplan_req;
00147 //   moveit_msgs::GetMotionPlan::Response mplan_res;
00148 //   mplan_req.motion_plan_request.group_name = "right_arm";
00149 //   mplan_req.motion_plan_request.num_planning_attempts = 5;
00150 //   mplan_req.motion_plan_request.allowed_planning_time = ros::Duration(5.0);
00151 //   const std::vector<std::string>& joint_names = ps->getRobotModel()->getJointModelGroup("right_arm")->getJointModelNames();
00152 //   mplan_req.motion_plan_request.goal_constraints.resize(1);
00153 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints.resize(joint_names.size());
00154 //   for(unsigned int i = 0; i < joint_names.size(); i++)
00155 //   {
00156 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].joint_name = joint_names[i];
00157 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].position = 0.0;
00158 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_above = 0.001;
00159 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_below = 0.001;
00160 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].weight = 1.0;
00161 //   }
00162 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[0].position = .466863;
00163 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[1].position = .492613;
00164 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[2].position = 0;
00165 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[3].position = -2.0439;
00166 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[4].position = -.454616;
00167 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[5].position = -.632607;
00168 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[6].position = -0.015;
00169 
00170 //   sbpl_planner.solve(ps,
00171 //                      mplan_req,
00172 //                      mplan_res);
00173 
00174 //   ASSERT_EQ(mplan_res.error_code.val, mplan_res.error_code.SUCCESS);
00175 //   EXPECT_GT(mplan_res.trajectory.joint_trajectory.points.size(), 0);
00176 // }
00177 
00178 // TEST_F(Pr2SBPLPlannerTester, HardPlan)
00179 // {
00180 //   planning_scene::PlanningScenePtr ps(new planning_scene::PlanningScene());
00181 //   ps->setActiveCollisionDetector(collision_detection::CollisionDetectorAllocatorHybridROS::create());
00182 //   ps->configure(rml_->getURDF(), rml_->getSRDF());
00183 //   ASSERT_TRUE(ps->isConfigured());
00184 //   ps->getAllowedCollisionMatrixNonConst() = *acm_;
00185 
00186 //   planning_models::RobotState *::JointStateGroup* start_jsg = ps->getCurrentState().getJointStateGroup("right_arm");
00187 //   std::vector<double> start_vals(7);
00188 //   start_vals[0] = -1.2851;
00189 //   start_vals[1] = 0.439296;
00190 //   start_vals[2] = -2.99119;
00191 //   start_vals[3] = -1.13797;
00192 //   start_vals[4] = 1.23849;
00193 //   start_vals[5] = -0.62;
00194 //   start_vals[6] = 1.71225;
00195 //   start_jsg->setStateValues(start_vals);
00196 
00197 //   sbpl_interface::SBPLInterface sbpl_planner(ps->getRobotModel());
00198 
00199 //   moveit_msgs::GetMotionPlan::Request mplan_req;
00200 //   moveit_msgs::GetMotionPlan::Response mplan_res;
00201 //   mplan_req.motion_plan_request.group_name = "right_arm";
00202 //   mplan_req.motion_plan_request.num_planning_attempts = 5;
00203 //   mplan_req.motion_plan_request.allowed_planning_time = ros::Duration(5.0);
00204 //   const std::vector<std::string>& joint_names = ps->getRobotModel()->getJointModelGroup("right_arm")->getJointModelNames();
00205 //   mplan_req.motion_plan_request.goal_constraints.resize(1);
00206 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints.resize(joint_names.size());
00207 //   for(unsigned int i = 0; i < joint_names.size(); i++)
00208 //   {
00209 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].joint_name = joint_names[i];
00210 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].position = 0.0;
00211 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_above = 0.001;
00212 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_below = 0.001;
00213 //     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].weight = 1.0;
00214 //   }
00215 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[0].position = .171837;
00216 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[1].position = .940657;
00217 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[2].position = -1.82044;
00218 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[3].position = -1.65047;
00219 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[4].position = 3.05812;
00220 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[5].position = -.787767;
00221 //   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[6].position = -.152367;
00222 
00223 //   sbpl_planner.solve(ps,
00224 //                      mplan_req,
00225 //                      mplan_res);
00226 
00227 //   ASSERT_EQ(mplan_res.error_code.val, mplan_res.error_code.SUCCESS);
00228 //   EXPECT_GT(mplan_res.trajectory.joint_trajectory.points.size(), 0);
00229 // }
00230 
00231 TEST_F(Pr2SBPLPlannerTester, HardPlan3)
00232 {
00233   planning_scene::PlanningScenePtr ps(new planning_scene::PlanningScene());
00234   ps->setActiveCollisionDetector(collision_detection::CollisionDetectorAllocatorHybridROS::create());
00235   ps->configure(rml_->getURDF(), rml_->getSRDF());
00236   ASSERT_TRUE(ps->isConfigured());
00237   ps->getAllowedCollisionMatrixNonConst() = *acm_;
00238 
00239   planning_models::RobotState *::JointStateGroup* start_jsg = ps->getCurrentState().getJointStateGroup("right_arm");
00240   std::vector<double> start_vals(7);
00241   start_vals[0] = -.785163;
00242   start_vals[1] = -.346628;
00243   start_vals[2] = -2.36346;
00244   start_vals[3] = -0.204096;
00245   start_vals[4] = -1.75754;
00246   start_vals[5] = -0.771607;
00247   start_vals[6] = 1.67731;
00248   start_jsg->setStateValues(start_vals);
00249 
00250   sbpl_interface::SBPLInterface sbpl_planner(ps->getRobotModel());
00251 
00252   moveit_msgs::GetMotionPlan::Request mplan_req;
00253   moveit_msgs::GetMotionPlan::Response mplan_res;
00254   mplan_req.motion_plan_request.group_name = "right_arm";
00255   mplan_req.motion_plan_request.num_planning_attempts = 5;
00256   mplan_req.motion_plan_request.allowed_planning_time = ros::Duration(5.0);
00257   const std::vector<std::string>& joint_names = ps->getRobotModel()->getJointModelGroup("right_arm")->getJointModelNames();
00258   mplan_req.motion_plan_request.goal_constraints.resize(1);
00259   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints.resize(joint_names.size());
00260   for(unsigned int i = 0; i < joint_names.size(); i++)
00261   {
00262     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].joint_name = joint_names[i];
00263     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].position = 0.0;
00264     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_above = 0.001;
00265     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_below = 0.001;
00266     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].weight = 1.0;
00267   }
00268 
00269   std::vector<double> goal_vals(7);
00270   goal_vals[0] = -1.78778;
00271   goal_vals[1] = 0.749619;
00272   goal_vals[2] = 0.594005;
00273   goal_vals[3] = -1.83274;
00274   goal_vals[4] = 1.5158;
00275   goal_vals[5] = -.500754;
00276   goal_vals[6] = -.108037;
00277 
00278   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[0].position = goal_vals[0];
00279   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[1].position = goal_vals[1];
00280   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[2].position = goal_vals[2];
00281   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[3].position = goal_vals[3];
00282   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[4].position = goal_vals[4];
00283   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[5].position = goal_vals[5];
00284   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[6].position = goal_vals[6];
00285 
00286   sbpl_planner.solve(ps,
00287                      mplan_req,
00288                      mplan_res);
00289 
00290   ASSERT_EQ(mplan_res.error_code.val, mplan_res.error_code.SUCCESS);
00291   EXPECT_GT(mplan_res.trajectory.joint_trajectory.points.size(), 0);
00292 }
00293 
00294 static const unsigned int NUM_TRIALS = 100;
00295 
00296 TEST_F(Pr2SBPLPlannerTester, ManyPlan)
00297 {
00298   planning_scene::PlanningScenePtr ps(new planning_scene::PlanningScene());
00299   ps->setActiveCollisionDetector(collision_detection::CollisionDetectorAllocatorHybridROS::create());
00300   ps->configure(rml_->getURDF(), rml_->getSRDF());
00301   ASSERT_TRUE(ps->isConfigured());
00302   ps->getAllowedCollisionMatrixNonConst() = *acm_;
00303 
00304   planning_models::RobotState *::JointStateGroup* start_jsg = ps->getCurrentState().getJointStateGroup("right_arm");
00305   planning_models::RobotState *goal_state(ps->getCurrentState());
00306   planning_models::RobotState *::JointStateGroup* goal_jsg = goal_state.getJointStateGroup("right_arm");
00307 
00308   sbpl_interface::SBPLInterface sbpl_planner(ps->getRobotModel());
00309 
00310   moveit_msgs::GetMotionPlan::Request mplan_req;
00311   mplan_req.motion_plan_request.group_name = "right_arm";
00312   mplan_req.motion_plan_request.num_planning_attempts = 5;
00313   mplan_req.motion_plan_request.allowed_planning_time = ros::Duration(5.0);
00314   const std::vector<std::string>& joint_names = ps->getRobotModel()->getJointModelGroup("right_arm")->getJointModelNames();
00315   mplan_req.motion_plan_request.goal_constraints.resize(1);
00316   mplan_req.motion_plan_request.goal_constraints[0].joint_constraints.resize(joint_names.size());
00317   for(unsigned int i = 0; i < joint_names.size(); i++)
00318   {
00319     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].joint_name = joint_names[i];
00320     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].position = 0.0;
00321     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_above = 0.001;
00322     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].tolerance_below = 0.001;
00323     mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].weight = 1.0;
00324   }
00325 
00326   unsigned int comp_trials = 0;
00327   unsigned int succ_trials = 0;
00328   double max_planning_time = 0.0;
00329   double total_planning_time = 0.0;
00330   while(comp_trials < NUM_TRIALS) {
00331     while(1) {
00332       start_jsg->setToRandomValues();
00333       goal_jsg->setToRandomValues();
00334       std::vector<double> goal_vals;
00335       goal_jsg->getGroupStateValues(goal_vals);
00336       for(unsigned int i = 0; i < joint_names.size(); i++) {
00337         mplan_req.motion_plan_request.goal_constraints[0].joint_constraints[i].position = goal_vals[i];
00338       }
00339       std::vector<double> start_vals;
00340       start_jsg->getGroupStateValues(start_vals);
00341       for(unsigned int i = 0; i < start_vals.size(); i++) {
00342         std::cerr << "Start joint " << i << " val " << start_vals[i] << std::endl;
00343       }
00344       for(unsigned int i = 0; i < goal_vals.size(); i++) {
00345         std::cerr << "Goal joint " << i << " val " << goal_vals[i] << std::endl;
00346       }
00347       moveit_msgs::GetMotionPlan::Response mplan_res;
00348       if(sbpl_planner.solve(ps,
00349                             mplan_req,
00350                             mplan_res)) {
00351         comp_trials++;
00352         succ_trials++;
00353         if(sbpl_planner.getLastPlanningStatistics().total_planning_time_.toSec() > max_planning_time) {
00354           max_planning_time = sbpl_planner.getLastPlanningStatistics().total_planning_time_.toSec();
00355         }
00356         ASSERT_LT(sbpl_planner.getLastPlanningStatistics().total_planning_time_.toSec(), 5.0);
00357         total_planning_time += sbpl_planner.getLastPlanningStatistics().total_planning_time_.toSec();
00358         break;
00359       } else {
00360         if(mplan_res.error_code.val != moveit_msgs::MoveItErrorCodes::GOAL_IN_COLLISION &&
00361            mplan_res.error_code.val != moveit_msgs::MoveItErrorCodes::START_STATE_IN_COLLISION) {
00362           std::cerr << "Bad error code " << mplan_res.error_code.val << std::endl;
00363           comp_trials++;
00364           break;
00365         } else {
00366           //std::cerr << "Something in collision" << std::endl;
00367         }
00368       }
00369     }
00370   }
00371 
00372   std::cerr << "Average planning time " << total_planning_time/(comp_trials*1.0) << " max " << max_planning_time << std::endl;
00373   EXPECT_EQ(succ_trials, comp_trials);
00374 
00375   //ASSERT_EQ(mplan_res.error_code.val, mplan_res.error_code.SUCCESS);
00376   //EXPECT_GT(mplan_res.trajectory.joint_trajectory.points.size(), 0);
00377 }
00378 
00379 int main(int argc, char **argv)
00380 {
00381   testing::InitGoogleTest(&argc, argv);
00382   ros::init(argc, argv, "test_sbpl_planning");
00383 
00384   return RUN_ALL_TESTS();
00385 }


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