test/planning_scene_interface.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2023, Aaryan Murgunde
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 PickNik Robotics nor the
18  * names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 /* Author: Aaryan Murgunde */
37 
38 // STD
39 #include <vector>
40 #include <random>
41 
42 // ROS
43 #include <ros/ros.h>
44 
45 // Testing Framework
46 #include <gtest/gtest.h>
47 
48 // moveit PLanning Scene Interface
50 
51 class ClearSceneFixture : public ::testing::Test
52 {
53 protected:
55 
56  int object_counter = 0;
57 
58  moveit_msgs::CollisionObject randomCollisionObject()
59  {
60  std::random_device rd; // Seed the engine with a true random value if available
61  std::mt19937 gen(rd()); // Mersenne Twister 19937 generator
62  std::uniform_real_distribution<float> dist(-4.0f, 4.0f);
63 
64  moveit_msgs::CollisionObject collision_object;
65  collision_object.id = std::to_string(object_counter);
66  collision_object.header.frame_id = "panda_link0";
67  collision_object.primitives.resize(1);
68  collision_object.primitives[0].type = shape_msgs::SolidPrimitive::BOX;
69  collision_object.primitives[0].dimensions.resize(3);
70  collision_object.primitives[0].dimensions[0] = std::abs(dist(gen));
71  collision_object.primitives[0].dimensions[1] = std::abs(dist(gen));
72  collision_object.primitives[0].dimensions[2] = std::abs(dist(gen));
73  collision_object.primitive_poses.resize(1);
74  collision_object.primitive_poses[0].position.x = dist(gen);
75  collision_object.primitive_poses[0].position.y = dist(gen);
76  collision_object.primitive_poses[0].position.z = dist(gen);
77  collision_object.primitive_poses[0].orientation.w = 1.0;
78  collision_object.operation = collision_object.ADD;
79 
80  // Global random counter update
82 
83  return collision_object;
84  }
85  moveit_msgs::AttachedCollisionObject randomAttachedCollisionObject()
86  {
87  std::random_device rd; // Seed the engine with a true random value if available
88  std::mt19937 gen(rd()); // Mersenne Twister 19937 generator
89  std::uniform_real_distribution<float> dist(-4.0f, 4.0f);
90 
91  moveit_msgs::AttachedCollisionObject attached_collision_object;
92  attached_collision_object.link_name = "panda_link0";
93  attached_collision_object.object = randomCollisionObject();
94  // Global random counter update
96 
97  return attached_collision_object;
98  }
99 };
100 
101 // Test 1 : ENV with only collision objects
102 TEST_F(ClearSceneFixture, CollisionObjectClearTest)
103 {
104  // Verify the scene is clear
105  ASSERT_EQ(planning_scene_interface.getObjects().size(), 0ul);
106 
107  // Add and verify if the objects have been added
108  std::vector<moveit_msgs::CollisionObject> collision_objects;
109  collision_objects.reserve(4);
110 
111  for (int i = 0; i < 4; i++)
112  {
113  collision_objects.push_back(randomCollisionObject());
114  }
115 
116  planning_scene_interface.applyCollisionObjects(collision_objects);
117  ASSERT_EQ(planning_scene_interface.getObjects().size(), std::size_t(4));
118 
119  // Use the function call to clear the planning scene
120  planning_scene_interface.clear();
121 
122  // Verify the scene is cleared
123  ASSERT_EQ(planning_scene_interface.getObjects().size(), 0ul);
124 }
125 
126 // Test 2 : ENV with only Attached objects
127 TEST_F(ClearSceneFixture, AttachedObjectClearTest)
128 {
129  // Verify the scene is clear
130  ASSERT_EQ(planning_scene_interface.getAttachedObjects().size(), 0ul);
131 
132  // Add and verify if the objects have been added
133  std::vector<moveit_msgs::AttachedCollisionObject> attached_objects;
134  attached_objects.reserve(4);
135 
136  for (int i = 0; i < 4; i++)
137  {
138  attached_objects.push_back(randomAttachedCollisionObject());
139  }
140 
141  planning_scene_interface.applyAttachedCollisionObjects(attached_objects);
142  ASSERT_EQ(planning_scene_interface.getAttachedObjects().size(), std::size_t(4));
143 
144  // Use the function call to clear the planning scene
145  planning_scene_interface.clear();
146 
147  // Verify the scene is cleared
148  ASSERT_EQ(planning_scene_interface.getAttachedObjects().size(), 0ul);
149 }
150 
151 TEST_F(ClearSceneFixture, CollisionAndAttachedObjectClearTest)
152 {
153  // Verify the scene is clear
154  ASSERT_EQ((planning_scene_interface.getAttachedObjects().size() + planning_scene_interface.getObjects().size()), 0ul);
155 
156  // Add and verify if the objects have been added
157  std::vector<moveit_msgs::AttachedCollisionObject> attached_objects;
158  attached_objects.reserve(2);
159 
160  for (int i = 0; i < 2; i++)
161  {
162  attached_objects.push_back(randomAttachedCollisionObject());
163  }
164 
165  planning_scene_interface.applyAttachedCollisionObjects(attached_objects);
166 
167  std::vector<moveit_msgs::CollisionObject> collision_objects;
168  collision_objects.reserve(2);
169 
170  for (int i = 0; i < 2; i++)
171  {
172  collision_objects.push_back(randomCollisionObject());
173  }
174 
175  planning_scene_interface.applyCollisionObjects(collision_objects);
176 
177  ASSERT_EQ((planning_scene_interface.getAttachedObjects().size() + planning_scene_interface.getObjects().size()),
178  std::size_t(4));
179 
180  // Use the function call to clear the planning scene
181  planning_scene_interface.clear();
182 
183  // Verify the scene is cleared
184  ASSERT_EQ((planning_scene_interface.getAttachedObjects().size() + planning_scene_interface.getObjects().size()), 0ul);
185 }
186 
187 int main(int argc, char** argv)
188 {
189  ros::init(argc, argv, "planning_scene_interface_clearScene");
190  testing::InitGoogleTest(&argc, argv);
191 
193  spinner.start();
194 
195  int test_result = RUN_ALL_TESTS();
196 
197  return test_result;
198 }
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros.h
moveit::planning_interface::PlanningSceneInterface::clear
bool clear()
Remove all the collision and attached objects from the world via the planning scene of the move_group...
Definition: planning_scene_interface/src/planning_scene_interface.cpp:513
ros::AsyncSpinner
moveit::planning_interface::PlanningSceneInterface::applyAttachedCollisionObjects
bool applyAttachedCollisionObjects(const std::vector< moveit_msgs::AttachedCollisionObject > &attached_collision_objects)
Apply attached collision objects to the planning scene of the move_group node synchronously....
Definition: planning_scene_interface/src/planning_scene_interface.cpp:482
ClearSceneFixture::randomAttachedCollisionObject
moveit_msgs::AttachedCollisionObject randomAttachedCollisionObject()
Definition: test/planning_scene_interface.cpp:118
moveit::planning_interface::PlanningSceneInterface
Definition: planning_scene_interface.h:116
ClearSceneFixture
Definition: test/planning_scene_interface.cpp:51
spinner
void spinner()
ClearSceneFixture::planning_scene_interface
moveit::planning_interface::PlanningSceneInterface planning_scene_interface
Definition: test/planning_scene_interface.cpp:87
TEST_F
TEST_F(ClearSceneFixture, CollisionObjectClearTest)
Definition: test/planning_scene_interface.cpp:102
ClearSceneFixture::object_counter
int object_counter
Definition: test/planning_scene_interface.cpp:89
main
int main(int argc, char **argv)
Definition: test/planning_scene_interface.cpp:187
ClearSceneFixture::randomCollisionObject
moveit_msgs::CollisionObject randomCollisionObject()
Definition: test/planning_scene_interface.cpp:91
planning_scene_interface.h
f
f
moveit::planning_interface::PlanningSceneInterface::getObjects
std::map< std::string, moveit_msgs::CollisionObject > getObjects(const std::vector< std::string > &object_ids=std::vector< std::string >())
Get the objects identified by the given object ids list. If no ids are provided, return all the known...
Definition: planning_scene_interface/src/planning_scene_interface.cpp:416
moveit::planning_interface::PlanningSceneInterface::applyCollisionObjects
bool applyCollisionObjects(const std::vector< moveit_msgs::CollisionObject > &collision_objects, const std::vector< moveit_msgs::ObjectColor > &object_colors=std::vector< moveit_msgs::ObjectColor >())
Apply collision objects to the planning scene of the move_group node synchronously....
Definition: planning_scene_interface/src/planning_scene_interface.cpp:452
moveit::planning_interface::PlanningSceneInterface::getAttachedObjects
std::map< std::string, moveit_msgs::AttachedCollisionObject > getAttachedObjects(const std::vector< std::string > &object_ids=std::vector< std::string >())
Get the attached objects identified by the given object ids list. If no ids are provided,...
Definition: planning_scene_interface/src/planning_scene_interface.cpp:422


planning_interface
Author(s): Ioan Sucan
autogenerated on Sat Apr 27 2024 02:27:01