SpatialForceTests.cpp
Go to the documentation of this file.
1 //
2 // Created by jordan on 9/27/16.
3 //
4 #include <gtest/gtest.h>
5 
6 #include "Fixtures.h"
9 #include "UnitTestUtils.hpp"
10 
11 using namespace RobotDynamics::Math;
12 
13 class SpatialForceTests : public ::testing::Test
14 {
15  public:
17 
18  void SetUp()
19  {
20  }
21 
22  void TearDown()
23  {
24  }
25 };
26 
27 TEST_F(FixedBase3DoFPlanar, changeFrameAndCopy)
28 {
29  SpatialForce v1(model->getReferenceFrame("body_b"), 1., 2., 3., 4., 5., 6.);
30  SpatialForce v2;
31 
32  randomizeStates();
33  updateKinematicsCustom(*model, &Q, nullptr, nullptr);
34 
36 
37  EXPECT_FALSE(v2.mx() == v1.mx());
38  EXPECT_FALSE(v2.my() == v1.my());
39  EXPECT_FALSE(v2.mz() == v1.mz());
40  EXPECT_FALSE(v2.fx() == v1.fx());
41  // EXPECT_FALSE(v2.fy()==v1.fy()); // its a rot about y axis, so this one isn't supposed to change
42  EXPECT_FALSE(v2.fz() == v1.fz());
43 
45 
46  EXPECT_TRUE(v2.mx() == v1.mx());
47  EXPECT_TRUE(v2.my() == v1.my());
48  EXPECT_TRUE(v2.mz() == v1.mz());
49  EXPECT_TRUE(v2.fx() == v1.fx());
50  EXPECT_TRUE(v2.fy() == v1.fy());
51  EXPECT_TRUE(v2.fz() == v1.fz());
52 
53  SpatialForce v3(model->getReferenceFrame("body_b"), 1., 2., 3., 4., 5., 6.);
54  SpatialForce v4;
55 
57 
58  EXPECT_FALSE(v3.mx() == v4.mx());
59  EXPECT_FALSE(v3.my() == v4.my());
60  EXPECT_FALSE(v3.mz() == v4.mz());
61  EXPECT_FALSE(v3.fx() == v4.fx());
62  // EXPECT_FALSE(v3.fy()==v4.fy()); // its a rot about y axis, so this one isn't supposed to change
63  EXPECT_FALSE(v3.fz() == v4.fz());
64 
66 
67  EXPECT_TRUE(v3.mx() == v4.mx());
68  EXPECT_TRUE(v3.my() == v4.my());
69  EXPECT_TRUE(v3.mz() == v4.mz());
70  EXPECT_TRUE(v3.fx() == v4.fx());
71  EXPECT_TRUE(v3.fy() == v4.fy());
72  EXPECT_TRUE(v3.fz() == v4.fz());
73 }
74 
75 TEST_F(FixedBase3DoFPlanar, test_framed_accessors)
76 {
77  SpatialForce v1(model->getReferenceFrame("body_a"), 1., 2., 3., 4., 5., 6.);
78 
81 
82  EXPECT_STREQ(fvl.getReferenceFrame()->getName().c_str(), v1.getReferenceFrame()->getName().c_str());
83 
84  EXPECT_EQ(fva.x(), v1.mx());
85  EXPECT_EQ(fva.y(), v1.my());
86  EXPECT_EQ(fva.z(), v1.mz());
87 
88  EXPECT_EQ(fvl.x(), v1.fx());
89  EXPECT_EQ(fvl.y(), v1.fy());
90  EXPECT_EQ(fvl.z(), v1.fz());
91 
92  v1.setIncludingFrame(model->getReferenceFrame("body_b"), ForceVector(0.1, -0.1, 0.2, -0.2, 0.3, -0.3));
93 
94  EXPECT_STREQ(v1.getReferenceFrame()->getName().c_str(), "body_b");
95  EXPECT_EQ(v1.mx(), 0.1);
96  EXPECT_EQ(v1.my(), -0.1);
97  EXPECT_EQ(v1.mz(), 0.2);
98 
99  EXPECT_EQ(v1.fx(), -0.2);
100  EXPECT_EQ(v1.fy(), 0.3);
101  EXPECT_EQ(v1.fz(), -0.3);
102 
103  v1.setIncludingFrame(model->getReferenceFrame("body_c"), 3., 4., 5., 6., 7., 8.);
104 
105  EXPECT_STREQ(v1.getReferenceFrame()->getName().c_str(), "body_c");
106  EXPECT_EQ(v1.mx(), 3.);
107  EXPECT_EQ(v1.my(), 4.);
108  EXPECT_EQ(v1.mz(), 5.);
109 
110  EXPECT_EQ(v1.fx(), 6.);
111  EXPECT_EQ(v1.fy(), 7.);
112  EXPECT_EQ(v1.fz(), 8.);
113 }
114 
116 {
117  SpatialForce v1(model->getReferenceFrame("body_a"), 0., 1., 0., 0., 0., 0.);
118  SpatialForce v2(model->getReferenceFrame("body_b"), 0., 1., 0., 0., 0., 0.);
119 
120  try
121  {
122  // Frame mismatches, should throw
123  SpatialForce v3 = v1 + v2;
124  }
125  catch (ReferenceFrameException& e)
126  {
127  EXPECT_STREQ("Reference frames do not match!", e.what());
128  }
129 
130  SpatialForce v4(model->getReferenceFrame("body_b"), 0., 1., 0., 0., 0., 0.);
131 
132  SpatialForce v5 = v2 + v4;
133 
135 
136  SpatialForce v6 = v5;
137 
139 
140  SpatialForce v7(model->getReferenceFrame("body_b"), -0.2, 0.3, -0.4, 0.5, -0.6, 0.7);
141 
142  EXPECT_EQ(v7.mx(), -0.2);
143  EXPECT_EQ(v7.my(), 0.3);
144  EXPECT_EQ(v7.mz(), -0.4);
145  EXPECT_EQ(v7.fx(), 0.5);
146  EXPECT_EQ(v7.fy(), -0.6);
147  EXPECT_EQ(v7.fz(), 0.7);
148 
149  SpatialForce v8 = v6 - v7;
150 
151  EXPECT_TRUE(unit_test_utils::checkSpatialVectorsEpsilonClose(v8, SpatialVector(0.2, 1.7, 0.4, -0.5, 0.6, -0.7), unit_test_utils::TEST_PREC));
152 }
153 
154 TEST_F(SpatialForceTests, testChangeFrame)
155 {
156  Vector3d rot(1.1, 1.2, 1.3);
157  Vector3d trans(1.1, 1.2, 1.3);
158 
159  SpatialTransform X_st;
160  X_st.r = trans;
161 
162  SpatialMatrix X_66_matrix(SpatialMatrix::Zero(6, 6));
163  X_66_matrix = Xrotz_mat(rot[2]) * Xroty_mat(rot[1]) * Xrotx_mat(rot[0]) * Xtrans_mat(trans);
164  X_st.E = X_66_matrix.block<3, 3>(0, 0);
165 
166  std::shared_ptr<ReferenceFrame> frameA(new ReferenceFrame("frameA", ReferenceFrame::getWorldFrame(), X_st, 0, 0));
167 
168  ForceVector v(1.1, 2.1, 3.1, 4.1, 5.1, 6.1);
169 
170  SpatialForce m(frameA, v);
171 
172  v.transform(X_st.inverse());
173 
175 
177 
178  m.setIncludingFrame(frameA, v);
179 
180  EXPECT_TRUE(unit_test_utils::checkSpatialVectorsEpsilonClose(frameA->getTransformToParent().applyAdjoint(m), m.changeFrameAndCopy(ReferenceFrame::getWorldFrame()),
182  EXPECT_TRUE(unit_test_utils::checkSpatialVectorsEpsilonClose(frameA->getTransformFromParent().applyTranspose(m),
184 }
185 
186 TEST_F(SpatialForceTests, testFrameCorrectness)
187 {
188  SpatialForce F(ReferenceFrame::getWorldFrame(), 0., 0., 0., 1., 2., 3.);
189  SpatialTransform X_z = Xrotz(M_PI_2);
190 
191  std::shared_ptr<ReferenceFrame> R_2(new ReferenceFrame("Frame2", ReferenceFrame::getWorldFrame(), X_z, true, 1));
192 
193  F.changeFrame(R_2);
194 
196 
197  EXPECT_TRUE(
198  unit_test_utils::checkSpatialVectorsEpsilonClose(F.transform_copy(R_2->getTransformToRoot()), SpatialVector(0., 0., 0., 1., 2., 3.), unit_test_utils::TEST_PREC));
199 }
200 
201 int main(int argc, char** argv)
202 {
203  ::testing::InitGoogleTest(&argc, argv);
204  return RUN_ALL_TESTS();
205 }
ReferenceFramePtr getReferenceFrame() const
Get a pointer to the reference frame this FrameObject is expressed in.
Definition: FrameObject.hpp:52
void setIncludingFrame(ReferenceFramePtr referenceFrame, const SpatialVector &v)
SpatialMatrix Xrotx_mat(const double &xrot)
Creates a rotational transformation around the X-axis.
A custom exception for frame operations.
FrameVector getFramedLinearPart() const
Get linear part of spatial force as a frame vector.
const double TEST_PREC
VectorNd Q
SpatialMatrix Xtrans_mat(const Vector3d &displacement)
Creates a transformation of a linear displacement.
A ForceVector is a SpatialVector containing 3 moments and 3 linear forces.
Definition: ForceVector.hpp:27
EIGEN_STRONG_INLINE double & fy()
Get reference to y-linear component.
static ReferenceFramePtr getWorldFrame()
Get a pointer to the world frame.
virtual const char * what() const
void transform(const SpatialTransform &X)
Performs the following in place transform .
EIGEN_STRONG_INLINE double & fx()
Get reference to x-linear component.
EIGEN_STRONG_INLINE double & fz()
Get reference to z-linear component.
SpatialTransform Xrotz(const double &zrot)
Get transform with zero translation and pure rotation about z axis.
FrameVector getFramedAngularPart() const
Get angular part of spatial force as a frame vector.
EIGEN_STRONG_INLINE double & my()
Get reference to y-angular component.
Compact representation of spatial transformations.
virtual void changeFrame(ReferenceFramePtr desiredFrame)
Change the ReferenceFrame this FrameObject is expressed in.
Definition: FrameObject.cpp:12
SpatialTransform inverse() const
Returns inverse of transform.
A FrameVector is a 3D vector with a ReferenceFrame, and all operations between FrameVectors and other...
Definition: FrameVector.hpp:33
ReferenceFrame object used to tell what frame objects are expressed in. Every ReferenceFrame has a po...
EIGEN_STRONG_INLINE double & mz()
Get reference to z-angular component.
SpatialMatrix Xroty_mat(const double &yrot)
Creates a rotational transformation around the Y-axis.
TEST_F(FixedBase3DoFPlanar, changeFrameAndCopy)
void updateKinematicsCustom(Model &model, const Math::VectorNd *Q, const Math::VectorNd *QDot=nullptr, const Math::VectorNd *QDDot=nullptr)
Selectively updates model internal states of body positions, velocities and/or accelerations.
Definition: Kinematics.cc:72
Math types such as vectors and matrices and utility functions.
Definition: ForceVector.hpp:21
static bool checkSpatialVectorsEpsilonClose(const RobotDynamics::Math::SpatialVector &v1, const RobotDynamics::Math::SpatialVector &v2, const double epsilon)
A SpatialForce is a spatial vector with the angular part being three moments and the linear part bein...
EIGEN_STRONG_INLINE double & mx()
Get reference to x-angular component.
Definition: ForceVector.hpp:96
int main(int argc, char **argv)
SpatialMatrix Xrotz_mat(const double &zrot)
Creates a rotational transformation around the Z-axis.
SpatialForce changeFrameAndCopy(ReferenceFramePtr referenceFrame) const
Copy and change frame.
ForceVector transform_copy(const SpatialTransform &X) const
Copy then transform a ForceVector by .


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