urdf_tests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Pilz GmbH & Co. KG
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ros/ros.h"
18 #include <gtest/gtest.h>
19 #include <Eigen/Core>
20 #include <Eigen/Dense>
21 #include <moveit/robot_model_loader/robot_model_loader.h>
22 #include <moveit/robot_model/robot_model.h>
23 #include <moveit/robot_state/robot_state.h>
24 
25 #define _USE_MATH_DEFINES
26 #include <math.h>
27 
28 const std::string ARM_GROUP_NAME{ "arm_group_name" };
29 const std::string ARM_GROUP_TIP_LINK_NAME{ "arm_tip_link" };
30 
31 static constexpr double L0{ 0.2604 }; // Height of foot
32 static constexpr double L1{ 0.3500 }; // Height of first connector
33 static constexpr double L2{ 0.3070 }; // Height of second connector
34 static constexpr double L3{ 0.0840 }; // Distance last joint to flange
35 static constexpr double DELTA{ 1.0e-10 };
36 
37 const std::string PARAM_MODEL{ "robot_description" };
38 
39 typedef std::tuple<std::string, std::vector<double>, Eigen::Vector3d> TestDataPoint;
40 
41 inline std::string getName(TestDataPoint& tdp)
42 {
43  return std::get<0>(tdp);
44 }
45 
46 inline std::vector<double> getJoints(TestDataPoint& tdp)
47 {
48  return std::get<1>(tdp);
49 }
50 
51 inline Eigen::Vector3d getPos(TestDataPoint& tdp)
52 {
53  return std::get<2>(tdp);
54 }
55 
56 std::string vecToString(std::vector<double>& vec)
57 {
58  return std::string(vec.begin(), vec.end());
59 }
60 
64 class URDFKinematicsTest : public testing::TestWithParam<std::string>
65 {
66 protected:
67  void SetUp() override;
68 
69 protected:
70  // ros stuff
72  // The following call to RobotModelLoader will throw an error message about not not finding srdf,
73  // but it works just fine with just urdf.
74  robot_model::RobotModelConstPtr robot_model_{ robot_model_loader::RobotModelLoader(GetParam(), false).getModel() };
75 
76  // parameters
77  std::string tip_link_name_;
78 
79  // test data
80  std::vector<TestDataPoint> testDataSet_;
81 };
82 
84 {
85  // get necessary parameters
87 
88  // Fill the testdata
89  testDataSet_.push_back(TestDataPoint("HomePos ", { 0, 0, 0, 0, 0, 0 }, Eigen::Vector3d(0, 0, L0 + L1 + L2 + L3)));
90  testDataSet_.push_back(TestDataPoint("TestPos1", { 0, M_PI_2, 0, 0, 0, 0 }, Eigen::Vector3d(L1 + L2 + L3, 0, L0)));
91  testDataSet_.push_back(
92  TestDataPoint("TestPos2", { 0, -M_PI_2, M_PI_2, 0, 0, 0 }, Eigen::Vector3d(-L1, 0, L0 - L2 - L3)));
93  testDataSet_.push_back(
94  TestDataPoint("TestPos3", { 0, -M_PI_2, -M_PI_2, 0, 0, 0 }, Eigen::Vector3d(-L1, 0, L0 + L2 + L3)));
95  testDataSet_.push_back(TestDataPoint("TestPos4", { 0, 0, M_PI_2, 0, 0, 0 }, Eigen::Vector3d(-L2 - L3, 0, L0 + L1)));
96  testDataSet_.push_back(
97  TestDataPoint("TestPos5", { -M_PI_2, 0, -M_PI_2, 0, 0, 0 }, Eigen::Vector3d(0, -L2 - L3, L0 + L1)));
98  testDataSet_.push_back(
99  TestDataPoint("TestPos6", { -M_PI_2, -M_PI_2, -M_PI_2, 0, 0, 0 }, Eigen::Vector3d(0, L1, L0 + L2 + L3)));
100  testDataSet_.push_back(
101  TestDataPoint("TestPos7", { M_PI_2, -M_PI_2, 0, 0, 0, 0 }, Eigen::Vector3d(0, -L1 - L2 - L3, L0)));
102  testDataSet_.push_back(TestDataPoint("TestPos8", { 0, 0, 0, 0, -M_PI_2, 0 }, Eigen::Vector3d(L3, 0, L0 + L1 + L2)));
103  testDataSet_.push_back(
104  TestDataPoint("TestPos9", { M_PI_2, 0, 0, 0, -M_PI_2, 0 }, Eigen::Vector3d(0, L3, L0 + L1 + L2)));
105  testDataSet_.push_back(
106  TestDataPoint("TestPos10", { 0, 0, 0, 0, 0, M_PI_2 }, Eigen::Vector3d(0, 0, L0 + L1 + L2 + L3)));
107 }
108 
109 INSTANTIATE_TEST_CASE_P(InstantiationName, URDFKinematicsTest, ::testing::Values(PARAM_MODEL), );
117 TEST_P(URDFKinematicsTest, forwardKinematics)
118 {
119  // check if the transformation is available
120  robot_state::RobotState rstate(robot_model_);
121  ASSERT_TRUE(rstate.knowsFrameTransform(tip_link_name_));
122 
123  // Loop over the testdata
124  for (TestDataPoint& test_data : testDataSet_)
125  {
126  std::string name = getName(test_data);
127  std::vector<double> joints = getJoints(test_data);
128  Eigen::Vector3d pos_exp = getPos(test_data);
129 
130  // get frame transform
131  rstate.setVariablePositions(joints);
132  rstate.update();
133  Eigen::Affine3d transform = rstate.getFrameTransform(tip_link_name_);
134 
135  // Test the position
136  double error_norm = (transform.translation() - pos_exp).norm();
137  EXPECT_NEAR(error_norm, 0, DELTA) << "TestPosition \"" << name << "\" failed\n"
138  << "\t Joints: ["
139  << Eigen::VectorXd::Map(joints.data(), static_cast<long>(joints.size()))
140  .transpose()
141  << "]\n"
142  << "\t Expected position [" << pos_exp.transpose() << "]\n"
143  << "\t Real position [" << transform.translation().transpose() << "]";
144  }
145 }
146 
147 int main(int argc, char** argv)
148 {
149  ros::init(argc, argv, "urdf_tests");
150  testing::InitGoogleTest(&argc, argv);
151  return RUN_ALL_TESTS();
152 }
std::vector< TestDataPoint > testDataSet_
Definition: urdf_tests.cpp:80
std::tuple< std::string, std::vector< double >, Eigen::Vector3d > TestDataPoint
Definition: urdf_tests.cpp:39
test fixture
Definition: urdf_tests.cpp:64
Eigen::Vector3d getPos(TestDataPoint &tdp)
Definition: urdf_tests.cpp:51
const std::string ARM_GROUP_NAME
Definition: urdf_tests.cpp:28
std::vector< double > getJoints(TestDataPoint &tdp)
Definition: urdf_tests.cpp:46
int main(int argc, char **argv)
Definition: urdf_tests.cpp:147
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
robot_model::RobotModelConstPtr robot_model_
Definition: urdf_tests.cpp:74
static constexpr double DELTA
Definition: urdf_tests.cpp:35
TEST_P(URDFKinematicsTest, forwardKinematics)
test the kinematics of urdf model
Definition: urdf_tests.cpp:117
std::string getName(TestDataPoint &tdp)
Definition: urdf_tests.cpp:41
std::string vecToString(std::vector< double > &vec)
Definition: urdf_tests.cpp:56
std::string tip_link_name_
Definition: urdf_tests.cpp:77
static constexpr double L1
Definition: urdf_tests.cpp:32
const std::string ARM_GROUP_TIP_LINK_NAME
Definition: urdf_tests.cpp:29
static constexpr double L3
Definition: urdf_tests.cpp:34
const std::string PARAM_MODEL
Definition: urdf_tests.cpp:37
void SetUp() override
Definition: urdf_tests.cpp:83
bool getParam(const std::string &key, std::string &s) const
static constexpr double L2
Definition: urdf_tests.cpp:33
ros::NodeHandle ph_
Definition: urdf_tests.cpp:71
static constexpr double L0
Definition: urdf_tests.cpp:31
INSTANTIATE_TEST_CASE_P(InstantiationName, URDFKinematicsTest,::testing::Values(PARAM_MODEL),)


prbt_support
Author(s):
autogenerated on Mon May 3 2021 02:19:29