moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
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{
47protected:
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 {
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
78unsigned 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 ASSERT_NO_FATAL_FAILURE(generateXML());
96
97 auto root = srdf_xml_.FirstChildElement("robot");
98 ASSERT_NE(root, nullptr) << "No <robot> element in generated SRDF";
99 EXPECT_EQ(std::string(root->Attribute("name")), std::string("fanuc"));
100 EXPECT_TRUE(root->NoChildren());
101}
102
103TEST_F(SRDFTest, SetJoints)
104{
105 initializeWithFanuc();
106
108 initializeStep(pg);
109 std::string group_name = "manipulator";
110 pg.create(group_name);
111
112 std::vector<std::string> joints = { "joint_1", "joint_2" };
113 pg.setJoints(group_name, joints);
114
115 ASSERT_NO_FATAL_FAILURE(generateXML());
116
117 auto root = srdf_xml_.FirstChildElement("robot");
118 ASSERT_NE(root, nullptr) << "No <robot> element in generated SRDF";
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 ASSERT_NO_FATAL_FAILURE(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
136TEST_F(SRDFTest, SetLinks)
137{
138 initializeWithFanuc();
139
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 ASSERT_NO_FATAL_FAILURE(generateXML());
149
150 auto root = srdf_xml_.FirstChildElement("robot");
151 ASSERT_NE(root, nullptr) << "No <robot> element in generated SRDF";
152 auto group_el = root->FirstChildElement("group");
153 ASSERT_NE(group_el, nullptr);
154 EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
155 EXPECT_EQ(countElements(*group_el, "link"), 2u);
156
157 links.push_back("link_2");
158 links.push_back("link_3");
159 pg.setLinks(group_name, links);
160
161 ASSERT_NO_FATAL_FAILURE(generateXML());
162
163 root = srdf_xml_.FirstChildElement("robot");
164 group_el = root->FirstChildElement("group");
165 ASSERT_NE(group_el, nullptr);
166 EXPECT_EQ(countElements(*group_el, "link"), 4u);
167}
168
169TEST_F(SRDFTest, SetJointsThenLinks)
170{
171 initializeWithFanuc();
172
174 initializeStep(pg);
175 std::string group_name = "manipulator";
176 pg.create(group_name);
177
178 std::vector<std::string> joints = { "joint_1", "joint_2" };
179 pg.setJoints(group_name, joints);
180
181 ASSERT_NO_FATAL_FAILURE(generateXML());
182
183 auto root = srdf_xml_.FirstChildElement("robot");
184 ASSERT_NE(root, nullptr) << "No <robot> element in generated SRDF";
185 auto group_el = root->FirstChildElement("group");
186 ASSERT_NE(group_el, nullptr);
187 EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
188 EXPECT_EQ(countElements(*group_el, "joint"), 2u);
189 EXPECT_EQ(countElements(*group_el, "link"), 0u);
190
191 std::vector<std::string> links = { "base_link", "link_1" };
192 pg.setLinks(group_name, links);
193
194 ASSERT_NO_FATAL_FAILURE(generateXML());
195
196 root = srdf_xml_.FirstChildElement("robot");
197 group_el = root->FirstChildElement("group");
198 ASSERT_NE(group_el, nullptr);
199 EXPECT_EQ(std::string(group_el->Attribute("name")), group_name);
200 EXPECT_EQ(countElements(*group_el, "joint"), 2u);
201 EXPECT_EQ(countElements(*group_el, "link"), 2u);
202}
203
204int main(int argc, char** argv)
205{
206 testing::InitGoogleTest(&argc, argv);
207 rclcpp::init(argc, argv);
208 return RUN_ALL_TESTS();
209}
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_
void generateFiles(const std::string &config_name)
Helper function for generating all the files for a particular config to a temporary directory.
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.
std::filesystem::path getSharePath(const std::string &package_name)
Return a path for the given package's share folder.
Definition utilities.hpp:70
std::filesystem::path getSharePath(const std::string &package_name)
Return a path for the given package's share folder.
Definition utilities.hpp:70
int main(int argc, char **argv)
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