moveit2
The MoveIt Motion Planning Framework for ROS 2.
test_srdf.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2022, Metro Robots
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 Metro Robots 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: David V. Lu!! */
39 #include <tinyxml2.h>
40 
41 static const rclcpp::Logger LOGGER = rclcpp::get_logger("test_srdf");
42 
46 
48 {
49 protected:
50  void SetUp() override
51  {
52  MoveItSetupTest::SetUp();
53  srdf_config_ = config_data_->get<SRDFConfig>("srdf");
54  }
55 
56  void initializeWithURDF(const std::filesystem::path& urdf)
57  {
58  config_data_->preloadWithURDFPath(urdf);
59  srdf_config_->updateRobotModel();
60  }
61 
63  {
64  initializeWithURDF(getSharePath("moveit_resources_fanuc_description") / "urdf" / "fanuc.urdf");
65  }
66 
67  void generateXML(const std::string& robot_name = "fanuc")
68  {
69  generateFiles<SRDFConfig>("srdf");
70  std::filesystem::path srdf_path = output_dir_ / "config" / (robot_name + ".srdf");
71  ASSERT_TRUE(std::filesystem::is_regular_file(srdf_path));
72 
73  ASSERT_EQ(srdf_xml_.LoadFile(srdf_path.c_str()), tinyxml2::XML_SUCCESS);
74  }
75 
76  std::shared_ptr<SRDFConfig> srdf_config_;
77  tinyxml2::XMLDocument srdf_xml_;
78 };
79 
80 unsigned int countElements(const tinyxml2::XMLNode& parent, const char* child_name)
81 {
82  unsigned int count = 0;
83  for (const tinyxml2::XMLElement* sub_el = parent.FirstChildElement(child_name); sub_el != nullptr;
84  sub_el = sub_el->NextSiblingElement(child_name))
85  {
86  count++;
87  }
88  return count;
89 }
90 
92 {
93  initializeWithFanuc();
94 
95  // do nothing
96 
97  generateXML();
98 
99  auto root = srdf_xml_.FirstChildElement("robot");
100  EXPECT_EQ(std::string(root->Attribute("name")), std::string("fanuc"));
101  EXPECT_TRUE(root->NoChildren());
102 }
103 
104 TEST_F(SRDFTest, SetJoints)
105 {
106  initializeWithFanuc();
107 
108  PlanningGroups pg;
109  initializeStep(pg);
110  std::string group_name = "manipulator";
111  pg.create(group_name);
112 
113  std::vector<std::string> joints = { "joint_1", "joint_2" };
114  pg.setJoints(group_name, joints);
115 
116  generateXML();
117 
118  auto root = srdf_xml_.FirstChildElement("robot");
119  auto group_el = root->FirstChildElement("group");
120  ASSERT_NE(group_el, nullptr);
121  EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
122  EXPECT_EQ(countElements(*group_el, "joint"), 2u);
123 
124  joints.push_back("joint_3");
125  joints.push_back("joint_4");
126  pg.setJoints(group_name, joints);
127 
128  generateXML();
129 
130  root = srdf_xml_.FirstChildElement("robot");
131  group_el = root->FirstChildElement("group");
132  ASSERT_NE(group_el, nullptr);
133  EXPECT_EQ(countElements(*group_el, "joint"), 4u);
134 }
135 
136 TEST_F(SRDFTest, SetLinks)
137 {
138  initializeWithFanuc();
139 
140  PlanningGroups pg;
141  initializeStep(pg);
142  std::string group_name = "manipulator";
143  pg.create(group_name);
144 
145  std::vector<std::string> links = { "base_link", "link_1" };
146  pg.setLinks(group_name, links);
147 
148  generateXML();
149 
150  auto root = srdf_xml_.FirstChildElement("robot");
151  auto group_el = root->FirstChildElement("group");
152  ASSERT_NE(group_el, nullptr);
153  EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
154  EXPECT_EQ(countElements(*group_el, "link"), 2u);
155 
156  links.push_back("link_2");
157  links.push_back("link_3");
158  pg.setLinks(group_name, links);
159 
160  generateXML();
161 
162  root = srdf_xml_.FirstChildElement("robot");
163  group_el = root->FirstChildElement("group");
164  ASSERT_NE(group_el, nullptr);
165  EXPECT_EQ(countElements(*group_el, "link"), 4u);
166 }
167 
168 TEST_F(SRDFTest, SetJointsThenLinks)
169 {
170  initializeWithFanuc();
171 
172  PlanningGroups pg;
173  initializeStep(pg);
174  std::string group_name = "manipulator";
175  pg.create(group_name);
176 
177  std::vector<std::string> joints = { "joint_1", "joint_2" };
178  pg.setJoints(group_name, joints);
179 
180  generateXML();
181 
182  auto root = srdf_xml_.FirstChildElement("robot");
183  auto group_el = root->FirstChildElement("group");
184  ASSERT_NE(group_el, nullptr);
185  EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
186  EXPECT_EQ(countElements(*group_el, "joint"), 2u);
187  EXPECT_EQ(countElements(*group_el, "link"), 0u);
188 
189  std::vector<std::string> links = { "base_link", "link_1" };
190  pg.setLinks(group_name, links);
191 
192  generateXML();
193 
194  root = srdf_xml_.FirstChildElement("robot");
195  group_el = root->FirstChildElement("group");
196  ASSERT_NE(group_el, nullptr);
197  EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
198  EXPECT_EQ(countElements(*group_el, "joint"), 2u);
199  EXPECT_EQ(countElements(*group_el, "link"), 2u);
200 }
201 
202 int main(int argc, char** argv)
203 {
204  testing::InitGoogleTest(&argc, argv);
205  rclcpp::init(argc, argv);
206  return RUN_ALL_TESTS();
207 }
std::shared_ptr< SRDFConfig > srdf_config_
Definition: test_srdf.cpp:76
tinyxml2::XMLDocument srdf_xml_
Definition: test_srdf.cpp:77
void initializeWithFanuc()
Definition: test_srdf.cpp:62
void generateXML(const std::string &robot_name="fanuc")
Definition: test_srdf.cpp:67
void initializeWithURDF(const std::filesystem::path &urdf)
Definition: test_srdf.cpp:56
void SetUp() override
Definition: test_srdf.cpp:50
Test environment with DataWarehouse setup and help for generating files in a temp dir.
moveit_setup::DataWarehousePtr config_data_
std::filesystem::path output_dir_
void setLinks(const std::string &group_name, const std::vector< std::string > &link_names)
Set the specified group's link names.
void setJoints(const std::string &group_name, const std::vector< std::string > &joint_names)
Set the specified group's joint names.
T * create(const std::string &name)
Create an item with the given name and return the pointer.
Definition: srdf_step.hpp:115
std::filesystem::path getSharePath(const std::string &package_name)
Return a path for the given package's share folder.
Definition: utilities.hpp:50
const rclcpp::Logger LOGGER
Definition: async_test.h:31
int main(int argc, char **argv)
Definition: test_srdf.cpp:202
TEST_F(SRDFTest, Empty)
Definition: test_srdf.cpp:91
unsigned int countElements(const tinyxml2::XMLNode &parent, const char *child_name)
Definition: test_srdf.cpp:80