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  return std::get<0>(tdp);
43 }
44 
45 inline std::vector<double> getJoints(TestDataPoint& tdp){
46  return std::get<1>(tdp);
47 }
48 
49 inline Eigen::Vector3d getPos(TestDataPoint& tdp){
50  return std::get<2>(tdp);
51 }
52 
53 std::string vecToString(std::vector<double>& vec) {
54  return std::string(vec.begin(), vec.end());
55 }
56 
60 class URDFKinematicsTest : public testing::TestWithParam<std::string>
61 {
62 protected:
63  virtual void SetUp();
64 
65 protected:
66  // ros stuff
68  robot_model::RobotModelConstPtr robot_model_ {
69  robot_model_loader::RobotModelLoader(GetParam()).getModel()};
70 
71  // parameters
73 
74  // test data
75  std::vector<TestDataPoint> testDataSet_;
76 };
77 
79 {
80  // get necessary parameters
81  ASSERT_TRUE(ph_.getParam(ARM_GROUP_NAME, group_name_));
83 
84  // Fill the testdata
85  testDataSet_.push_back(TestDataPoint("HomePos ", { 0, 0, 0, 0, 0, 0}, Eigen::Vector3d(0, 0, L0+L1+L2+L3)));
86  testDataSet_.push_back(TestDataPoint("TestPos1", { 0, M_PI_2, 0, 0, 0, 0}, Eigen::Vector3d(L1+L2+L3, 0, L0)));
87  testDataSet_.push_back(TestDataPoint("TestPos2", { 0, -M_PI_2, M_PI_2, 0, 0, 0}, Eigen::Vector3d(-L1, 0, L0-L2-L3)));
88  testDataSet_.push_back(TestDataPoint("TestPos3", { 0, -M_PI_2, -M_PI_2, 0, 0, 0}, Eigen::Vector3d(-L1, 0, L0+L2+L3)));
89  testDataSet_.push_back(TestDataPoint("TestPos4", { 0, 0, M_PI_2, 0, 0, 0}, Eigen::Vector3d(-L2-L3, 0, L0+L1)));
90  testDataSet_.push_back(TestDataPoint("TestPos5", {-M_PI_2, 0, -M_PI_2, 0, 0, 0}, Eigen::Vector3d(0, -L2-L3, L0+L1)));
91  testDataSet_.push_back(TestDataPoint("TestPos6", {-M_PI_2, -M_PI_2, -M_PI_2, 0, 0, 0}, Eigen::Vector3d(0, L1, L0+L2+L3)));
92  testDataSet_.push_back(TestDataPoint("TestPos7", { M_PI_2, -M_PI_2, 0, 0, 0, 0}, Eigen::Vector3d(0, -L1-L2-L3, L0)));
93  testDataSet_.push_back(TestDataPoint("TestPos8", { 0, 0, 0, 0, -M_PI_2, 0}, Eigen::Vector3d(L3, 0, L0+L1+L2)));
94  testDataSet_.push_back(TestDataPoint("TestPos9", { M_PI_2, 0, 0, 0, -M_PI_2, 0}, Eigen::Vector3d(0, L3, L0+L1+L2)));
95  testDataSet_.push_back(TestDataPoint("TestPos10", { 0, 0, 0, 0, 0, M_PI_2}, Eigen::Vector3d(0, 0, L0+L1+L2+L3)));
96 }
97 
98 INSTANTIATE_TEST_CASE_P(InstantiationName, URDFKinematicsTest, ::testing::Values(PARAM_MODEL) );
106 TEST_P(URDFKinematicsTest, forwardKinematics)
107 {
108  // check if the transformation is available
109  robot_state::RobotState rstate(robot_model_);
110  ASSERT_TRUE(rstate.knowsFrameTransform(tip_link_name_));
111 
112  // Loop over the testdata
113  for(TestDataPoint & testData : testDataSet_)
114  {
115  std::string name = getName(testData);
116  std::vector<double> joints = getJoints(testData);
117  Eigen::Vector3d posExp = getPos(testData);
118 
119  // get frame transform
120  rstate.setJointGroupPositions(group_name_, joints);
121  //rstate.setVariablePositions(joints);
122  rstate.update();
123  Eigen::Affine3d transform = rstate.getFrameTransform(tip_link_name_);
124 
125  // Test the position
126  double error_norm = (transform.translation() - posExp).norm();
127  EXPECT_NEAR(error_norm, 0, delta)
128  << "TestPosition \"" << name << "\" failed\n"
129  << "\t Joints: [" << Eigen::VectorXd::Map(joints.data(), joints.size()).transpose() << "]\n"
130  << "\t Expected position [" << posExp.transpose() << "]\n"
131  << "\t Real position [" << transform.translation().transpose() << "]";
132  }
133 }
134 
135 int main(int argc, char **argv)
136 {
137  ros::init(argc, argv, "urdf_tests");
138  testing::InitGoogleTest(&argc, argv);
139  return RUN_ALL_TESTS();
140 }
virtual void SetUp()
Definition: urdf_tests.cpp:78
std::vector< TestDataPoint > testDataSet_
Definition: urdf_tests.cpp:75
std::tuple< std::string, std::vector< double >, Eigen::Vector3d > TestDataPoint
Definition: urdf_tests.cpp:39
test fixture
Definition: urdf_tests.cpp:60
Eigen::Vector3d getPos(TestDataPoint &tdp)
Definition: urdf_tests.cpp:49
const std::string ARM_GROUP_NAME
Definition: urdf_tests.cpp:28
std::vector< double > getJoints(TestDataPoint &tdp)
Definition: urdf_tests.cpp:45
int main(int argc, char **argv)
Definition: urdf_tests.cpp:135
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:68
std::string group_name_
Definition: urdf_tests.cpp:72
TEST_P(URDFKinematicsTest, forwardKinematics)
test the kinematics of urdf model
Definition: urdf_tests.cpp:106
std::string getName(TestDataPoint &tdp)
Definition: urdf_tests.cpp:41
std::string vecToString(std::vector< double > &vec)
Definition: urdf_tests.cpp:53
std::string tip_link_name_
Definition: urdf_tests.cpp:72
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
INSTANTIATE_TEST_CASE_P(InstantiationName, URDFKinematicsTest,::testing::Values(PARAM_MODEL))
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:67
static constexpr double L0
Definition: urdf_tests.cpp:31
static constexpr double delta
Definition: urdf_tests.cpp:35


prbt_support
Author(s):
autogenerated on Tue Feb 2 2021 03:50:33