moveit2
The MoveIt Motion Planning Framework for ROS 2.
test_collision_objects.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2021, Universitaet Hamburg
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: Michael 'v4hn' Goerner */
36 
37 #include <gtest/gtest.h>
41 #include <urdf_parser/urdf_parser.h>
42 #include <fstream>
43 #include <sstream>
44 #include <string>
45 
46 #include <tf2_eigen/tf2_eigen.hpp>
47 
48 namespace
49 {
50 void makeSphere(moveit_msgs::msg::CollisionObject& co)
51 {
52  // set valid primitive
53  shape_msgs::msg::SolidPrimitive primitive;
54  primitive.type = shape_msgs::msg::SolidPrimitive::SPHERE;
55  primitive.dimensions.push_back(/* SPHERE_RADIUS */ 1.0);
56  co.primitives.push_back(primitive);
57  geometry_msgs::msg::Pose pose;
58  pose.orientation.w = 1.0;
59  co.primitive_poses.push_back(pose);
60 }
61 } // namespace
62 
63 TEST(PlanningScene, fillInObjectPoseFromPrimitive)
64 {
65  moveit::core::RobotModelPtr robot_model(moveit::core::RobotModelBuilder("empty_robot", "base_link").build());
67 
68  moveit_msgs::msg::CollisionObject co;
69  co.header.frame_id = robot_model->getModelFrame();
70  co.id = "object_no_pose";
71  co.operation = moveit_msgs::msg::CollisionObject::ADD;
72  makeSphere(co);
73  scene.processCollisionObjectMsg(co);
74 
75  Eigen::Isometry3d primitive_pose;
76  tf2::fromMsg(co.primitive_poses.at(0), primitive_pose);
77  ASSERT_TRUE(scene.knowsFrameTransform(co.id)) << "failed to add object";
78  EXPECT_TRUE(scene.getFrameTransform(co.id).isApprox(primitive_pose))
79  << "scene did not use only primitive pose as object pose";
80 }
81 
82 TEST(PlanningScene, fillInPrimitivePose)
83 {
84  moveit::core::RobotModelPtr robot_model(moveit::core::RobotModelBuilder("empty_robot", "base_link").build());
86 
87  moveit_msgs::msg::CollisionObject co;
88  co.header.frame_id = robot_model->getModelFrame();
89  co.id = "object_no_primitive_pose";
90  co.operation = moveit_msgs::msg::CollisionObject::ADD;
91  makeSphere(co);
92  co.pose = co.primitive_poses.at(0);
93  co.primitive_poses.resize(0);
94  scene.processCollisionObjectMsg(co);
95 
96  Eigen::Isometry3d object_pose;
97  tf2::fromMsg(co.pose, object_pose);
98  ASSERT_TRUE(scene.knowsFrameTransform(co.id)) << "failed to add object";
99  EXPECT_TRUE(scene.getFrameTransform(co.id).isApprox(object_pose))
100  << "scene did not implicitly fill in identity pose for only primitive";
101 }
102 
103 TEST(PlanningScene, rememberMetadataWhenAttached)
104 {
105  moveit::core::RobotModelPtr robot_model(moveit::core::RobotModelBuilder("empty_robot", "base_link").build());
107 
108  // prepare planning scene message to add a colored object
109  moveit_msgs::msg::PlanningScene scene_msg;
110  scene_msg.robot_model_name = robot_model->getName();
111  scene_msg.is_diff = true;
112  scene_msg.robot_state.is_diff = true;
113 
114  moveit_msgs::msg::CollisionObject co;
115  co.header.frame_id = robot_model->getModelFrame();
116  co.id = "blue_sphere";
117  co.operation = moveit_msgs::msg::CollisionObject::ADD;
118  co.pose.orientation.w = 1.0;
119  makeSphere(co);
120 
121  // meta-data 1: object type
122  co.type.key = "blue_sphere_type";
123  co.type.db = "{'type':'CustomDB'}";
124  scene_msg.world.collision_objects.push_back(co);
125 
126  // meta-data 2: object color
127  moveit_msgs::msg::ObjectColor color;
128  color.id = co.id;
129  color.color.b = 1.0;
130  color.color.a = 1.0;
131  scene_msg.object_colors.push_back(color);
132 
133  EXPECT_FALSE(scene.hasObjectColor(co.id)) << "scene knows color before adding it(?)";
134  EXPECT_FALSE(scene.hasObjectType(co.id)) << "scene knows type before adding it(?)";
135 
136  // add object to scene
137  scene.usePlanningSceneMsg(scene_msg);
138 
139  EXPECT_TRUE(scene.hasObjectColor(co.id)) << "scene failed to add object color";
140  EXPECT_EQ(scene.getObjectColor(co.id), color.color) << "scene added wrong object color";
141  EXPECT_TRUE(scene.hasObjectType(co.id)) << "scene failed to add object type";
142  EXPECT_EQ(scene.getObjectType(co.id), co.type) << "scene added wrong object type";
143 
144  // attach object
145  moveit_msgs::msg::AttachedCollisionObject aco;
146  aco.object.operation = moveit_msgs::msg::CollisionObject::ADD;
147  aco.object.id = co.id;
148  aco.link_name = robot_model->getModelFrame();
149  scene.processAttachedCollisionObjectMsg(aco);
150 
151  EXPECT_EQ(scene.getObjectColor(co.id), color.color) << "scene forgot object color after it got attached";
152  EXPECT_EQ(scene.getObjectType(co.id), co.type) << "scene forgot object type after it got attached";
153 
154  // trying to remove object from the scene while it is attached is expected to fail
155  co.operation = moveit_msgs::msg::CollisionObject::REMOVE;
156  EXPECT_FALSE(scene.processCollisionObjectMsg(co))
157  << "scene removed attached object from collision world (although it's not there)";
158 
159  // detach again right away
160  aco.object.operation = moveit_msgs::msg::CollisionObject::REMOVE;
161  scene.processAttachedCollisionObjectMsg(aco);
162 
163  EXPECT_EQ(scene.getObjectColor(co.id), color.color) << "scene forgot specified color after attach/detach";
164  EXPECT_EQ(scene.getObjectType(co.id), co.type) << "scene forgot specified type after attach/detach";
165 }
166 
167 int main(int argc, char** argv)
168 {
169  testing::InitGoogleTest(&argc, argv);
170  return RUN_ALL_TESTS();
171 }
Easily build different robot models for testing. Essentially a programmer-friendly light wrapper arou...
This class maintains the representation of the environment as seen by a planning instance....
scene
Definition: pick.py:52
int main(int argc, char **argv)
TEST(PlanningScene, fillInObjectPoseFromPrimitive)