ReferenceFrameTest.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 #include "UnitTestUtils.hpp"
3 
4 using namespace RobotDynamics;
5 
6 class ReferenceFrameTest : public ::testing::Test
7 {
8  protected:
9  virtual void SetUp()
10  {
11  std::srand(time(NULL));
12 
13  root1 = ReferenceFrame::createARootFrame("root1");
14  root2 = ReferenceFrame::createARootFrame("root2");
15 
16  frame1 = unit_test_utils::createRandomUnchangingFrame("frame1", root1, 1);
17  frame2 = unit_test_utils::createRandomUnchangingFrame("frame2", frame1, 2);
18  frame3 = unit_test_utils::createRandomUnchangingFrame("frame3", frame2, 3);
19 
20  frame4 = unit_test_utils::createRandomUnchangingFrame("frame4", root2, 1);
21  frame5 = unit_test_utils::createRandomUnchangingFrame("frame5", frame4, 2);
22  frame6 = unit_test_utils::createRandomUnchangingFrame("frame6", root2, 1);
23  frame7 = unit_test_utils::createRandomUnchangingFrame("frame7", frame6, 2);
24  frame8 = unit_test_utils::createRandomUnchangingFrame("frame8", frame7, 3);
25 
26  allFrames.push_back(root1);
27  allFrames.push_back(frame1);
28  allFrames.push_back(frame2);
29  allFrames.push_back(frame3);
30 
31  allFrames.push_back(root2);
32  allFrames.push_back(frame4);
33  allFrames.push_back(frame5);
34  allFrames.push_back(frame6);
35  allFrames.push_back(frame7);
36  allFrames.push_back(frame8);
37 
38  frames1.push_back(root1);
39  frames1.push_back(frame1);
40  frames1.push_back(frame2);
41  frames1.push_back(frame3);
42 
43  frames2.push_back(root2);
44  frames2.push_back(frame4);
45  frames2.push_back(frame5);
46  frames2.push_back(frame6);
47  frames2.push_back(frame7);
48  frames2.push_back(frame8);
49  }
50  virtual void TearDown()
51  {
52  allFrames.clear();
53  frames1.clear();
54  frames2.clear();
55  }
56 
59 
63 
69 
70  std::vector<ReferenceFramePtr> allFrames;
71  std::vector<ReferenceFramePtr> frames1;
72  std::vector<ReferenceFramePtr> frames2;
73 
74  int nTests = 1;
75 
76  private:
77 };
78 
79 TEST_F(ReferenceFrameTest, constructors)
80 {
81  ReferenceFrame testFrame(*frame1);
82 
83  EXPECT_TRUE(
84  unit_test_utils::checkSpatialMatrixEpsilonClose(testFrame.getTransformToRoot().toMatrix(), frame1->getTransformToRoot().toMatrix(), unit_test_utils::TEST_PREC));
85  EXPECT_TRUE(unit_test_utils::checkSpatialMatrixEpsilonClose(testFrame.getInverseTransformToRoot().toMatrix(), frame1->getInverseTransformToRoot().toMatrix(),
87  EXPECT_STREQ(testFrame.getName().c_str(), frame1->getName().c_str());
88  EXPECT_EQ(testFrame.getIsBodyFrame(), frame1->getIsBodyFrame());
89  EXPECT_EQ(testFrame.getIsWorldFrame(), frame1->getIsWorldFrame());
90 }
91 
92 TEST_F(ReferenceFrameTest, testRootsHaveNullParent)
93 {
94  EXPECT_TRUE(root1->getParentFrame() == nullptr);
95  EXPECT_TRUE(root2->getParentFrame() == nullptr);
96 
97  try
98  {
99  ReferenceFramePtr frame(new ReferenceFrame("blah", nullptr, SpatialTransform(), false, 1));
100  }
101  catch (ReferenceFrameException& e)
102  {
103  EXPECT_STREQ(e.what(), "You are not allowed to create a frame with parentFrame=nullptr. Only a root frame and the world frame may have parentFrame=nullptr");
104  }
105 }
106 
107 TEST_F(ReferenceFrameTest, testWorldFramePointerStuff)
108 {
109  const ReferenceFramePtr worldFrame1 = ReferenceFrame::getWorldFrame();
110  const ReferenceFramePtr worldFrame2 = ReferenceFrame::getWorldFrame();
111 
112  EXPECT_EQ(worldFrame1, worldFrame2);
113 }
114 
115 TEST_F(ReferenceFrameTest, testRootFramesArentTheSame)
116 {
117  EXPECT_FALSE(root1 == root2);
118 }
119 
120 TEST_F(ReferenceFrameTest, testGetRootFrame)
121 {
122  EXPECT_TRUE(frame2->getRootFrame() == root1.get());
123  EXPECT_TRUE(frame7->getRootFrame() == frame5->getRootFrame());
124 
125  frame7->verifyFramesHaveSameRoot(frame6);
126 
127  try
128  {
129  frame7->verifyFramesHaveSameRoot(frame1);
130  }
132  {
133  EXPECT_STREQ("Frames frame1 and frame7 have mismatched roots!", e.what());
134  }
135 }
136 
137 TEST_F(ReferenceFrameTest, testCheckReferenceFramesMatch)
138 {
139  try
140  {
141  frame2->checkReferenceFramesMatch(nullptr);
142  }
143  catch (ReferenceFrameException& e)
144  {
145  EXPECT_STREQ(e.what(), "Reference frame is nullptr!");
146  }
147 
148  try
149  {
150  frame2->checkReferenceFramesMatch(frame7);
151  }
152  catch (ReferenceFrameException& e)
153  {
154  EXPECT_STREQ(e.what(), "Reference frames do not match!");
155  }
156 }
157 
158 TEST_F(ReferenceFrameTest, testGetTransformBetweenFrames)
159 {
160  for (int i = 0; i < nTests; i++)
161  {
163 
166 
167  RobotDynamics::Math::SpatialTransform transform1 = tmpFrame1->getTransformToDesiredFrame(tmpFrame2);
168  RobotDynamics::Math::SpatialTransform transform2 = tmpFrame2->getTransformToDesiredFrame(tmpFrame1);
169 
170  RobotDynamics::Math::SpatialTransform shouldBeIdentity = transform1 * transform2;
171  RobotDynamics::Math::SpatialTransform identityTransform;
172 
173  EXPECT_TRUE(unit_test_utils::checkSpatialMatrixEpsilonClose(shouldBeIdentity.toMatrix(), identityTransform.toMatrix(), unit_test_utils::TEST_PREC));
174  }
175 
176  for (int i = 0; i < nTests; i++)
177  {
179 
182 
183  RobotDynamics::Math::SpatialTransform transform1 = tmpFrame1->getTransformToDesiredFrame(tmpFrame2);
184  RobotDynamics::Math::SpatialTransform transform2 = tmpFrame2->getTransformToDesiredFrame(tmpFrame1);
185 
186  RobotDynamics::Math::SpatialTransform shouldBeIdentity = transform1 * transform2;
187  RobotDynamics::Math::SpatialTransform identityTransform;
188 
189  EXPECT_TRUE(unit_test_utils::checkSpatialMatrixEpsilonClose(shouldBeIdentity.toMatrix(), identityTransform.toMatrix(), unit_test_utils::TEST_PREC));
190  }
191 }
192 
193 TEST_F(ReferenceFrameTest, testGetTransformToParent)
194 {
195  for (int i = 1; i < allFrames.size(); i++)
196  {
197  ReferenceFramePtr tmpFrame2 = allFrames[i];
198 
199  ReferenceFramePtr parentFrame = tmpFrame2->getParentFrame();
200 
201  if (parentFrame != nullptr)
202  {
203  Eigen::Matrix4d m1, m2;
204  RobotDynamics::Math::SpatialTransform t1 = tmpFrame2->getTransformToParent();
205  RobotDynamics::Math::SpatialTransform t2 = tmpFrame2->getTransformToDesiredFrame(parentFrame);
207  }
208  }
209 }
210 
211 TEST_F(ReferenceFrameTest, testGetTransformToRoot)
212 {
213  for (int j = 0; j < nTests; j++)
214  {
216 
217  for (int i = 0; i < allFrames.size(); i++)
218  {
219  ReferenceFramePtr frame = allFrames[i];
221 
222  EXPECT_TRUE(unit_test_utils::checkSpatialMatrixEpsilonClose(transformToRoot.toMatrix(), frame->getTransformToRoot().toMatrix(), unit_test_utils::TEST_PREC));
223  }
224  }
225 }
226 
227 TEST_F(ReferenceFrameTest, testGetTransformToSelf)
228 {
229  for (int i = 0; i < nTests; i++)
230  {
232 
233  for (int j = 0; j < allFrames.size(); j++)
234  {
235  ReferenceFramePtr tmpFrame = allFrames[j];
236  RobotDynamics::Math::SpatialTransform shouldBeIdentity = tmpFrame->getTransformToDesiredFrame(tmpFrame);
237 
239  EXPECT_TRUE(unit_test_utils::checkSpatialMatrixEpsilonClose(shouldBeIdentity.toMatrix(), identityTransform, unit_test_utils::TEST_PREC));
240  }
241  }
242 }
243 
244 int main(int argc, char** argv)
245 {
246  ::testing::InitGoogleTest(&argc, argv);
247  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
248  return RUN_ALL_TESTS();
249 }
std::string getName() const
Get the frame name.
static ReferenceFramePtr createRandomUnchangingFrame(const std::string &frameName, ReferenceFramePtr parentFrame, const unsigned int movableBodyId)
ReferenceFramePtr frame6
A custom exception for frame operations.
static ReferenceFramePtr createARootFrame(const std::string &frameName)
Creates a root frame with ReferenceFrame::parentFrame=nullptr.
ReferenceFramePtr root1
ReferenceFramePtr frame3
bool getIsBodyFrame() const
Get boolean telling if this frame is a body frame or not. If it is a body frame, A pointer to this fr...
const double TEST_PREC
SpatialMatrix toMatrix() const
Return transform as 6x6 spatial matrix.
ReferenceFramePtr frame8
static ReferenceFramePtr getARandomFrame(std::vector< ReferenceFramePtr > frames)
ReferenceFramePtr frame4
std::vector< ReferenceFramePtr > allFrames
ReferenceFramePtr frame5
static ReferenceFramePtr getWorldFrame()
Get a pointer to the world frame.
virtual const char * what() const
bool getIsWorldFrame() const
Get a boolean telling if this frame is the world frame.
ReferenceFramePtr frame7
ReferenceFramePtr frame2
std::vector< ReferenceFramePtr > frames1
TEST_F(ReferenceFrameTest, constructors)
std::shared_ptr< ReferenceFrame > ReferenceFramePtr
Compact representation of spatial transformations.
int main(int argc, char **argv)
ReferenceFramePtr frame1
virtual RobotDynamics::Math::SpatialTransform getTransformToRoot()
Get this frames ReferenceFrame::transformToRoot.
static RobotDynamics::Math::SpatialTransform getTransformToRootByClimbingTree(ReferenceFramePtr frame)
virtual RobotDynamics::Math::SpatialTransform getInverseTransformToRoot()
Get this frames ReferenceFrame::inverseTransformToRoot.
ReferenceFrame object used to tell what frame objects are expressed in. Every ReferenceFrame has a po...
ReferenceFramePtr root2
static bool checkSpatialMatrixEpsilonClose(const RobotDynamics::Math::SpatialMatrix &t1, const RobotDynamics::Math::SpatialMatrix &t2, const double epsilon)
SpatialMatrix SpatialMatrixIdentity
std::vector< ReferenceFramePtr > frames2
static void updateAllFrames(std::vector< ReferenceFramePtr > frames)
Namespace for all structures of the RobotDynamics library.
Definition: Body.h:21


rdl_dynamics
Author(s):
autogenerated on Tue Apr 20 2021 02:25:28