test_collision_detection.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: Ioan Sucan */
00036 
00037 #include <planning_scene_monitor/planning_scene_monitor.h>
00038 #include <collision_detection/collision_tools.h>
00039 #include <planning_models/conversions.h>
00040 #include <moveit_msgs/DisplayTrajectory.h>
00041 
00042 static const std::string ROBOT_DESCRIPTION="robot_description";
00043 
00044 void findSelfCollisionAndDisplayContacts()
00045 {
00046     ros::NodeHandle nh;
00047     planning_scene_monitor::PlanningSceneMonitor psm(ROBOT_DESCRIPTION);
00048     planning_scene::PlanningScenePtr scene = psm.getPlanningScene();
00049 
00050     ros::Publisher pub_scene = nh.advertise<moveit_msgs::PlanningScene>("planning_scene", 1);
00051     ros::Publisher pub_markers = nh.advertise<visualization_msgs::MarkerArray>("visualization_marker_array", 5);
00052 
00053     sleep(1);
00054 
00055     collision_detection::CollisionRequest req;
00056     do
00057     {
00058     collision_detection::CollisionResult res;
00059     scene->getCurrentState().setToRandomValues();
00060     scene->checkSelfCollision(req, res);
00061     if (res.collision)
00062         break;
00063     } while (true);
00064 
00065     moveit_msgs::PlanningScene psmsg;
00066     scene->getPlanningSceneMsg(psmsg);
00067     pub_scene.publish(psmsg);
00068 
00069     req.contacts = true;
00070     req.max_contacts = 1000;
00071     req.max_contacts_per_pair = 500;
00072     collision_detection::CollisionResult res;
00073     req.verbose = true;
00074     scene->checkSelfCollision(req, res);
00075     std_msgs::ColorRGBA color;
00076     color.r = 1.0f;
00077     color.g = 0.0f;
00078     color.b = 0.0f;
00079     color.a = 1.0f;
00080     visualization_msgs::MarkerArray arr;
00081     collision_detection::getCollisionMarkersFromContacts(arr, "/odom_combined", res.contacts, color, ros::Duration(30.0));
00082     pub_markers.publish(arr);
00083     ros::Duration(10).sleep();
00084 }
00085 
00086 void testSimple()
00087 {
00088     ros::NodeHandle nh;
00089     planning_scene_monitor::PlanningSceneMonitor psm(ROBOT_DESCRIPTION);
00090     planning_scene::PlanningScenePtr scene = psm.getPlanningScene();
00091 
00092     ros::Publisher pub_state = nh.advertise<moveit_msgs::DisplayTrajectory>("display_motion_plan", 20);
00093     ros::Publisher pub_scene = nh.advertise<moveit_msgs::PlanningScene>("planning_scene", 1);
00094     sleep(1);
00095 
00096     std::vector<shapes::ShapeConstPtr> attached_shapes(1, shapes::ShapeConstPtr(new shapes::Box(0.2, 0.1, 0.1)));
00097     Eigen::Affine3d t;
00098     t.setIdentity();
00099     std::vector<Eigen::Affine3d> attached_poses(1, t);
00100     std::vector<std::string> touch;
00101     touch.push_back("r_wrist_roll_link");
00102     touch.push_back("r_forearm_link");
00103     touch.push_back("r_gripper_palm_link");
00104     touch.push_back("r_gripper_l_finger_link");
00105     touch.push_back("r_gripper_r_finger_link");
00106 
00107     scene->getCurrentState().getLinkState("r_wrist_roll_link")->attachBody("attached", attached_shapes,
00108                                        attached_poses, touch);
00109 
00110     collision_detection::CollisionRequest req;
00111     req.verbose = true;
00112     unsigned int N = 2;
00113 
00114     for (unsigned int i = 0 ; i < N ; ++i)
00115     {
00116     collision_detection::CollisionResult res;
00117     do
00118     {
00119         ROS_INFO("Trying new state...");
00120         res.collision = false;
00121         if (i + 1 == N)
00122         scene->getCurrentState().setToDefaultValues();
00123         else
00124         scene->getCurrentState().setToRandomValues();
00125         scene->checkSelfCollision(req, res);
00126     }
00127     while (res.collision);
00128 
00129     ROS_INFO("Displaying valid state...");
00130     moveit_msgs::PlanningScene psmsg;
00131     scene->getPlanningSceneMsg(psmsg);
00132     pub_scene.publish(psmsg);
00133     ros::Duration(0.5).sleep();
00134 
00135     /*
00136     moveit_msgs::DisplayTrajectory d;
00137     d.model_id = scene->getRobotModel()->getName();
00138     planning_models::robotStateToRobotStateMsg(scene->getCurrentState(), d.robot_state);
00139     pub_state.publish(d);
00140     for (int j = 0 ; j < 10 ; ++j)
00141     {
00142         //        ros::spinOnce();
00143         ros::Duration(0.01).sleep();
00144     }
00145     */
00146     }
00147 
00148     sleep(1);
00149 
00150     planning_scene::PlanningScenePtr colliding = planning_scene::PlanningScene::clone(scene);
00151     // construct a planning scene with 100 objects and no collisions
00152     random_numbers::RandomNumberGenerator rng;
00153     req.verbose = false;
00154     for (int i = 0 ; i < 100000 ; ++i)
00155     {
00156     t.translation() = Eigen::Vector3d(rng.uniformReal(-1, 1), rng.uniformReal(-1, 1), rng.uniformReal(0, 2));
00157     scene->getWorldNonConst()->clearObjects();
00158     scene->getWorldNonConst()->addToObject("spere1", shapes::ShapeConstPtr(new shapes::Sphere(0.05)), t);
00159     collision_detection::CollisionResult res;
00160     scene->checkCollision(req, res);
00161     if (!res.collision)
00162     {
00163         int x = colliding->getWorld()->getObjectIds().size();
00164         colliding->getWorldNonConst()->addToObject("speres" + boost::lexical_cast<std::string>(x),
00165                                                         shapes::ShapeConstPtr(new shapes::Sphere(0.05)), t);
00166         std::cout << x << "\n";
00167         if (x == 100)
00168         break;
00169     }
00170     }
00171 
00172     moveit_msgs::PlanningScene psmsg;
00173     colliding->getPlanningSceneMsg(psmsg);
00174     pub_scene.publish(psmsg);
00175 
00176     ros::WallTime start = ros::WallTime::now();
00177     unsigned int M = 1000;
00178     for (unsigned int i = 0 ; i < M ; ++i)
00179     {
00180     collision_detection::CollisionResult res;
00181     colliding->checkCollision(req, res);
00182     if (res.collision)
00183         ROS_ERROR("PROBLEM");
00184     }
00185     ROS_INFO("%lf full-collision checks per second", (double)M / (ros::WallTime::now() - start).toSec());
00186 
00187 
00188     /*
00189     req.verbose = false;
00190     ros::WallTime start = ros::WallTime::now();
00191     //    unsigned int N = 50000;
00192 N = 50000;
00193     for (unsigned int i = 0 ; i < N ; ++i)
00194     {
00195     collision_detection::CollisionResult res;
00196     scene->getCurrentState().setToRandomValues();
00197     scene->checkSelfCollision(req, res);
00198     }
00199     ROS_INFO("%lf self-collision checks per second", (double)N / (ros::WallTime::now() - start).toSec());
00200     */
00201 }
00202 
00203 
00204 
00205 int main(int argc, char **argv)
00206 {
00207     ros::init(argc, argv, "test_collision_detection");
00208     ros::AsyncSpinner spinner(1);
00209     spinner.start();
00210 
00211     testSimple();
00212     findSelfCollisionAndDisplayContacts();
00213 
00214     ros::waitForShutdown();
00215 
00216     return 0;
00217 }


pr2_test_collision_detection
Author(s): Ioan Sucan
autogenerated on Mon Sep 14 2015 14:17:46