Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 }
00147
00148 sleep(1);
00149
00150 planning_scene::PlanningScenePtr colliding = planning_scene::PlanningScene::clone(scene);
00151
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
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
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 }