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 
44 
46 {
47 protected:
48  void SetUp() override
49  {
50  MoveItSetupTest::SetUp();
51  srdf_config_ = config_data_->get<SRDFConfig>("srdf");
52  }
53 
54  void initializeWithURDF(const std::filesystem::path& urdf)
55  {
56  config_data_->preloadWithURDFPath(urdf);
57  srdf_config_->updateRobotModel();
58  }
59 
61  {
62  initializeWithURDF(getSharePath("moveit_resources_fanuc_description") / "urdf" / "fanuc.urdf");
63  }
64 
65  void generateXML(const std::string& robot_name = "fanuc")
66  {
67  generateFiles<SRDFConfig>("srdf");
68  std::filesystem::path srdf_path = output_dir_ / "config" / (robot_name + ".srdf");
69  ASSERT_TRUE(std::filesystem::is_regular_file(srdf_path));
70 
71  ASSERT_EQ(srdf_xml_.LoadFile(srdf_path.c_str()), tinyxml2::XML_SUCCESS);
72  }
73 
74  std::shared_ptr<SRDFConfig> srdf_config_;
75  tinyxml2::XMLDocument srdf_xml_;
76 };
77 
78 unsigned int countElements(const tinyxml2::XMLNode& parent, const char* child_name)
79 {
80  unsigned int count = 0;
81  for (const tinyxml2::XMLElement* sub_el = parent.FirstChildElement(child_name); sub_el != nullptr;
82  sub_el = sub_el->NextSiblingElement(child_name))
83  {
84  count++;
85  }
86  return count;
87 }
88 
90 {
91  initializeWithFanuc();
92 
93  // do nothing
94 
95  generateXML();
96 
97  auto root = srdf_xml_.FirstChildElement("robot");
98  EXPECT_EQ(std::string(root->Attribute("name")), std::string("fanuc"));
99  EXPECT_TRUE(root->NoChildren());
100 }
101 
102 TEST_F(SRDFTest, SetJoints)
103 {
104  initializeWithFanuc();
105 
106  PlanningGroups pg;
107  initializeStep(pg);
108  std::string group_name = "manipulator";
109  pg.create(group_name);
110 
111  std::vector<std::string> joints = { "joint_1", "joint_2" };
112  pg.setJoints(group_name, joints);
113 
114  generateXML();
115 
116  auto root = srdf_xml_.FirstChildElement("robot");
117  auto group_el = root->FirstChildElement("group");
118  ASSERT_NE(group_el, nullptr);
119  EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
120  EXPECT_EQ(countElements(*group_el, "joint"), 2u);
121 
122  joints.push_back("joint_3");
123  joints.push_back("joint_4");
124  pg.setJoints(group_name, joints);
125 
126  generateXML();
127 
128  root = srdf_xml_.FirstChildElement("robot");
129  group_el = root->FirstChildElement("group");
130  ASSERT_NE(group_el, nullptr);
131  EXPECT_EQ(countElements(*group_el, "joint"), 4u);
132 }
133 
134 TEST_F(SRDFTest, SetLinks)
135 {
136  initializeWithFanuc();
137 
138  PlanningGroups pg;
139  initializeStep(pg);
140  std::string group_name = "manipulator";
141  pg.create(group_name);
142 
143  std::vector<std::string> links = { "base_link", "link_1" };
144  pg.setLinks(group_name, links);
145 
146  generateXML();
147 
148  auto root = srdf_xml_.FirstChildElement("robot");
149  auto group_el = root->FirstChildElement("group");
150  ASSERT_NE(group_el, nullptr);
151  EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
152  EXPECT_EQ(countElements(*group_el, "link"), 2u);
153 
154  links.push_back("link_2");
155  links.push_back("link_3");
156  pg.setLinks(group_name, links);
157 
158  generateXML();
159 
160  root = srdf_xml_.FirstChildElement("robot");
161  group_el = root->FirstChildElement("group");
162  ASSERT_NE(group_el, nullptr);
163  EXPECT_EQ(countElements(*group_el, "link"), 4u);
164 }
165 
166 TEST_F(SRDFTest, SetJointsThenLinks)
167 {
168  initializeWithFanuc();
169 
170  PlanningGroups pg;
171  initializeStep(pg);
172  std::string group_name = "manipulator";
173  pg.create(group_name);
174 
175  std::vector<std::string> joints = { "joint_1", "joint_2" };
176  pg.setJoints(group_name, joints);
177 
178  generateXML();
179 
180  auto root = srdf_xml_.FirstChildElement("robot");
181  auto group_el = root->FirstChildElement("group");
182  ASSERT_NE(group_el, nullptr);
183  EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
184  EXPECT_EQ(countElements(*group_el, "joint"), 2u);
185  EXPECT_EQ(countElements(*group_el, "link"), 0u);
186 
187  std::vector<std::string> links = { "base_link", "link_1" };
188  pg.setLinks(group_name, links);
189 
190  generateXML();
191 
192  root = srdf_xml_.FirstChildElement("robot");
193  group_el = root->FirstChildElement("group");
194  ASSERT_NE(group_el, nullptr);
195  EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
196  EXPECT_EQ(countElements(*group_el, "joint"), 2u);
197  EXPECT_EQ(countElements(*group_el, "link"), 2u);
198 }
199 
200 int main(int argc, char** argv)
201 {
202  testing::InitGoogleTest(&argc, argv);
203  rclcpp::init(argc, argv);
204  return RUN_ALL_TESTS();
205 }
std::shared_ptr< SRDFConfig > srdf_config_
Definition: test_srdf.cpp:74
tinyxml2::XMLDocument srdf_xml_
Definition: test_srdf.cpp:75
void initializeWithFanuc()
Definition: test_srdf.cpp:60
void generateXML(const std::string &robot_name="fanuc")
Definition: test_srdf.cpp:65
void initializeWithURDF(const std::filesystem::path &urdf)
Definition: test_srdf.cpp:54
void SetUp() override
Definition: test_srdf.cpp:48
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:51
int main(int argc, char **argv)
Definition: test_srdf.cpp:200
TEST_F(SRDFTest, Empty)
Definition: test_srdf.cpp:89
unsigned int countElements(const tinyxml2::XMLNode &parent, const char *child_name)
Definition: test_srdf.cpp:78