moveit2
The MoveIt Motion Planning Framework for ROS 2.
visualize_robot_collision_volume.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
38 #include <rclcpp/duration.hpp>
39 #include <rclcpp/executors/multi_threaded_executor.hpp>
40 #include <rclcpp/node.hpp>
41 #include <rclcpp/publisher.hpp>
42 #include <rclcpp/qos_event.hpp>
43 #include <rclcpp/time.hpp>
44 #include <rclcpp/utilities.hpp>
45 #include <memory>
46 
47 static const std::string ROBOT_DESCRIPTION = "robot_description";
48 
49 int main(int argc, char** argv)
50 {
51  rclcpp::init(argc, argv);
52  auto node = rclcpp::Node::make_shared("visualize_robot_collision_volume");
53 
54  rclcpp::executors::MultiThreadedExecutor executor;
55  executor.add_node(node);
56  double radius = 0.02;
57  int lifetime = 600;
58 
59  planning_scene_monitor::PlanningSceneMonitor psm(node, ROBOT_DESCRIPTION);
60  if (psm.getPlanningScene())
61  {
63  psm.startSceneMonitor();
64  psm.startStateMonitor();
65  auto pub_markers = node->create_publisher<visualization_msgs::msg::MarkerArray>("visualization_marker_array", 10);
66  std::cout << "\nListening for planning scene...\nType the number of spheres to generate and press Enter: " << '\n';
67  int num_spheres;
68  std::cin >> num_spheres;
69 
70  planning_scene::PlanningScenePtr scene = psm.getPlanningScene();
71  std::vector<double> aabb;
72  scene->getCurrentState().computeAABB(aabb);
73 
74  // publish the bounding box
75  visualization_msgs::msg::Marker mk;
76  mk.header.stamp = node->now();
77  mk.header.frame_id = scene->getPlanningFrame();
78  mk.ns = "bounding_box";
79  mk.id = 0;
80  mk.type = visualization_msgs::msg::Marker::CUBE;
81  mk.action = visualization_msgs::msg::Marker::ADD;
82  mk.pose.position.x = (aabb[0] + aabb[1]) / 2.0;
83  mk.pose.position.y = (aabb[2] + aabb[3]) / 2.0;
84  mk.pose.position.z = (aabb[4] + aabb[5]) / 2.0;
85  mk.pose.orientation.w = 1.0;
86  mk.scale.x = aabb[1] - aabb[0];
87  mk.scale.y = aabb[3] - aabb[2];
88  mk.scale.z = aabb[5] - aabb[4];
89  mk.color.r = 0.0f;
90  mk.color.g = 0.5f;
91  mk.color.b = 1.0f;
92  mk.color.a = 0.3f;
93  mk.lifetime = rclcpp::Duration::from_seconds(lifetime);
94  visualization_msgs::msg::MarkerArray arr;
95  arr.markers.push_back(mk);
96  pub_markers->publish(arr);
97 
98  Eigen::Isometry3d t;
99  t.setIdentity();
100  std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d>> points;
101  std::size_t published = 0;
102  random_numbers::RandomNumberGenerator rng;
104 
105  std_msgs::msg::ColorRGBA color;
106  color.r = 1.0f;
107  color.g = 0.0f;
108  color.b = 0.0f;
109  color.a = 1.0f;
110 
111  for (int i = 0; i < num_spheres; ++i)
112  {
113  t.translation() = Eigen::Vector3d(rng.uniformReal(aabb[0], aabb[1]), rng.uniformReal(aabb[2], aabb[3]),
114  rng.uniformReal(aabb[4], aabb[5]));
115  scene->getWorldNonConst()->clearObjects();
116  scene->getWorldNonConst()->addToObject("test", std::make_shared<shapes::Sphere>(radius), t);
118  scene->checkCollision(req, res);
119  if (res.collision)
120  {
121  points.push_back(t.translation());
122  if (points.size() - published >= 100 || (points.size() > published && i + 1 >= num_spheres))
123  {
124  arr.markers.clear();
125  for (std::size_t k = published; k < points.size(); ++k)
126  {
127  visualization_msgs::msg::Marker mk;
128  mk.header.stamp = node->now();
129  mk.header.frame_id = scene->getPlanningFrame();
130  mk.ns = "colliding";
131  mk.id = k;
132  mk.type = visualization_msgs::msg::Marker::SPHERE;
133  mk.action = visualization_msgs::msg::Marker::ADD;
134  mk.pose.position.x = points[k].x();
135  mk.pose.position.y = points[k].y();
136  mk.pose.position.z = points[k].z();
137  mk.pose.orientation.w = 1.0;
138  mk.scale.x = mk.scale.y = mk.scale.z = radius;
139  mk.color = color;
140  mk.lifetime = rclcpp::Duration::from_seconds(lifetime);
141  arr.markers.push_back(mk);
142  pub_markers->publish(arr);
143  }
144  pub_markers->publish(arr);
145  published = points.size();
146  }
147  }
148  }
149  }
150  executor.spin();
151  return 0;
152 }
PlanningSceneMonitor Subscribes to the topic planning_scene.
const planning_scene::PlanningScenePtr & getPlanningScene()
Avoid this function! Returns an unsafe pointer to the current planning scene.
void startStateMonitor(const std::string &joint_states_topic=DEFAULT_JOINT_STATES_TOPIC, const std::string &attached_objects_topic=DEFAULT_ATTACHED_COLLISION_OBJECT_TOPIC)
Start the current state monitor.
void startSceneMonitor(const std::string &scene_topic=DEFAULT_PLANNING_SCENE_TOPIC)
Start the scene monitor (ROS topic-based)
void startWorldGeometryMonitor(const std::string &collision_objects_topic=DEFAULT_COLLISION_OBJECT_TOPIC, const std::string &planning_scene_world_topic=DEFAULT_PLANNING_SCENE_WORLD_TOPIC, const bool load_octomap_monitor=true)
Start the OccupancyMapMonitor and listening for:
Vec3fX< details::Vec3Data< double > > Vector3d
Definition: fcl_compat.h:89
scene
Definition: pick.py:52
Representation of a collision checking request.
Representation of a collision checking result.
bool collision
True if collision was found, false otherwise.
int main(int argc, char **argv)