RdlCompositeRigidBodyTests.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "rdl_dynamics/Model.h"
6 
7 #include <gtest/gtest.h>
8 
9 #include "UnitTestUtils.hpp"
10 
11 #include "Fixtures.h"
12 
13 using namespace std;
14 using namespace RobotDynamics;
15 using namespace RobotDynamics::Math;
16 
17 const double TEST_PREC = 1.0e-12;
18 
19 class CompositeRigidBodyTestFixture : public testing::Test
20 {
21  public:
23  {
24  }
25 
26  void SetUp()
27  {
28  model = new Model;
29  model->gravity = SpatialVector(0., 0., 0., 0., -9.81, 0.);
30  }
31 
32  void TearDown()
33  {
34  delete model;
35  }
36 
38 };
39 
40 TEST_F(CompositeRigidBodyTestFixture, TestCompositeRigidBodyForwardDynamicsFloatingBase)
41 {
42  Body base_body(1., Vector3d(1., 0., 0.), Vector3d(1., 1., 1.));
43 
44  model->addBody(0, SpatialTransform(),
45  Joint(SpatialVector(0., 0., 0., 1., 0., 0.), SpatialVector(0., 0., 0., 0., 1., 0.), SpatialVector(0., 0., 0., 0., 0., 1.),
46  SpatialVector(0., 0., 1., 0., 0., 0.), SpatialVector(0., 1., 0., 0., 0., 0.), SpatialVector(1., 0., 0., 0., 0., 0.)),
47  base_body);
48 
49  // Initialization of the input vectors
50  VectorNd Q = VectorNd::Constant((size_t)model->dof_count, 0.);
51  VectorNd QDot = VectorNd::Constant((size_t)model->dof_count, 0.);
52  VectorNd QDDot = VectorNd::Constant((size_t)model->dof_count, 0.);
53  VectorNd Tau = VectorNd::Constant((size_t)model->dof_count, 0.);
54  VectorNd TauInv = VectorNd::Constant((size_t)model->dof_count, 0.);
55 
56  MatrixNd H = MatrixNd::Constant((size_t)model->dof_count, (size_t)model->dof_count, 0.);
57  VectorNd C = VectorNd::Constant((size_t)model->dof_count, 0.);
58  VectorNd QDDot_zero = VectorNd::Constant((size_t)model->dof_count, 0.);
59  VectorNd QDDot_crba = VectorNd::Constant((size_t)model->dof_count, 0.);
60 
61  Q[0] = 1.1;
62  Q[1] = 1.2;
63  Q[2] = 1.3;
64  Q[3] = 0.1;
65  Q[4] = 0.2;
66  Q[5] = 0.3;
67 
68  QDot[0] = 1.1;
69  QDot[1] = -1.2;
70  QDot[2] = 1.3;
71  QDot[3] = -0.1;
72  QDot[4] = 0.2;
73  QDot[5] = -0.3;
74 
75  Tau[0] = 2.1;
76  Tau[1] = 2.2;
77  Tau[2] = 2.3;
78  Tau[3] = 1.1;
79  Tau[4] = 1.2;
80  Tau[5] = 1.3;
81 
82  forwardDynamics(*model, Q, QDot, Tau, QDDot);
83 
84  compositeRigidBodyAlgorithm(*model, Q, H);
85 
86  inverseDynamics(*model, Q, QDot, QDDot_zero, C);
87 
88  EXPECT_TRUE(linSolveGaussElimPivot(H, C * -1. + Tau, QDDot_crba));
89 
91 }
92 
93 TEST_F(FloatingBase12DoF, TestCRBAFloatingBase12DoF)
94 {
95  MatrixNd H = MatrixNd::Zero((size_t)model->dof_count, (size_t)model->dof_count);
96 
97  VectorNd C = VectorNd::Constant((size_t)model->dof_count, 0.);
98  VectorNd QDDot_zero = VectorNd::Constant((size_t)model->dof_count, 0.);
99  VectorNd QDDot_crba = VectorNd::Constant((size_t)model->dof_count, 0.);
100 
101  Q[0] = 1.1;
102  Q[1] = 1.2;
103  Q[2] = 1.3;
104  Q[3] = 0.1;
105  Q[4] = 0.2;
106  Q[5] = 0.3;
107  Q[6] = -1.3;
108  Q[7] = -1.4;
109  Q[8] = -1.5;
110  Q[9] = -0.3;
111  Q[10] = -0.4;
112  Q[11] = -0.5;
113 
114  QDot[0] = 1.1;
115  QDot[1] = -1.2;
116  QDot[2] = 1.3;
117  QDot[3] = -0.1;
118  QDot[4] = 0.2;
119  QDot[5] = -0.3;
120  QDot[6] = -1.1;
121  QDot[7] = 1.2;
122  QDot[8] = -1.3;
123  QDot[9] = 0.1;
124  QDot[10] = -0.2;
125  QDot[11] = 0.3;
126 
127  Tau[0] = -1.1;
128  Tau[1] = 1.2;
129  Tau[2] = -1.3;
130  Tau[3] = 1.1;
131  Tau[4] = -1.2;
132  Tau[5] = 1.3;
133  Tau[6] = 0.1;
134  Tau[7] = -0.2;
135  Tau[8] = 0.3;
136  Tau[9] = -0.1;
137  Tau[10] = 0.2;
138  Tau[11] = -0.3;
139 
140  forwardDynamics(*model, Q, QDot, Tau, QDDot);
141  compositeRigidBodyAlgorithm(*model, Q, H);
142  inverseDynamics(*model, Q, QDot, QDDot_zero, C);
143 
144  EXPECT_TRUE((linSolveGaussElimPivot(H, C * -1. + Tau, QDDot_crba)));
145 
147 }
148 
149 TEST_F(FloatingBase12DoF, TestCRBAFloatingBase12DoFInverseDynamics)
150 {
151  MatrixNd H_crba = MatrixNd::Zero((size_t)model->dof_count, (size_t)model->dof_count);
152  MatrixNd H_id = MatrixNd::Zero((size_t)model->dof_count, (size_t)model->dof_count);
153 
154  Q[0] = 1.1;
155  Q[1] = 1.2;
156  Q[2] = 1.3;
157  Q[3] = 0.1;
158  Q[4] = 0.2;
159  Q[5] = 0.3;
160  Q[6] = -1.3;
161  Q[7] = -1.4;
162  Q[8] = -1.5;
163  Q[9] = -0.3;
164  Q[10] = -0.4;
165  Q[11] = -0.5;
166 
167  QDot.setZero();
168 
169  assert(model->dof_count == 12);
170 
171  updateKinematicsCustom(*model, &Q, NULL, NULL);
172  compositeRigidBodyAlgorithm(*model, Q, H_crba, false);
173 
174  VectorNd H_col = VectorNd::Zero(model->dof_count);
175  VectorNd QDDot_zero = VectorNd::Zero(model->dof_count);
176 
177  unsigned int i;
178  for (i = 0; i < model->dof_count; i++)
179  {
180  // compute each column
181  VectorNd delta_a = VectorNd::Zero(model->dof_count);
182  delta_a[i] = 1.;
183 
184  // compute ID (model, q, qdot, delta_a)
185  VectorNd id_delta = VectorNd::Zero(model->dof_count);
186  inverseDynamics(*model, Q, QDot, delta_a, id_delta);
187 
188  // compute ID (model, q, qdot, zero)
189  VectorNd id_zero = VectorNd::Zero(model->dof_count);
190  inverseDynamics(*model, Q, QDot, QDDot_zero, id_zero);
191 
192  H_col = id_delta - id_zero;
193  H_id.block<12, 1>(0, i) = H_col;
194  }
195 
196  EXPECT_TRUE(unit_test_utils::checkMatrixNdEpsilonClose(H_crba, H_id, TEST_PREC));
197 }
198 
199 TEST_F(FixedBase6DoF, TestCRBAFloatingBase12DoFInverseDynamics)
200 {
201  MatrixNd H_crba = MatrixNd::Zero((size_t)model->dof_count, (size_t)model->dof_count);
202  MatrixNd H_id = MatrixNd::Zero((size_t)model->dof_count, (size_t)model->dof_count);
203 
204  Q[0] = 1.1;
205  Q[1] = 1.2;
206  Q[2] = 1.3;
207  Q[3] = 0.1;
208  Q[4] = 0.2;
209  Q[5] = 0.3;
210 
211  QDot.setZero();
212 
213  assert(model->dof_count == 6);
214 
215  updateKinematicsCustom(*model, &Q, NULL, NULL);
216  compositeRigidBodyAlgorithm(*model, Q, H_crba, false);
217 
218  VectorNd H_col = VectorNd::Zero(model->dof_count);
219  VectorNd QDDot_zero = VectorNd::Zero(model->dof_count);
220 
221  unsigned int i;
222  for (i = 0; i < 6; i++)
223  {
224  // compute each column
225  VectorNd delta_a = VectorNd::Zero(model->dof_count);
226  delta_a[i] = 1.;
227 
228  // compute ID (model, q, qdot, delta_a)
229  VectorNd id_delta = VectorNd::Zero(model->dof_count);
230  inverseDynamics(*model, Q, QDot, delta_a, id_delta);
231 
232  // compute ID (model, q, qdot, zero)
233  VectorNd id_zero = VectorNd::Zero(model->dof_count);
234  inverseDynamics(*model, Q, QDot, QDDot_zero, id_zero);
235 
236  H_col.setZero();
237  H_col = id_delta - id_zero;
238 
239  H_id.block<6, 1>(0, i) = H_col;
240  }
241 
243 }
244 
245 TEST_F(CompositeRigidBodyTestFixture, TestCompositeRigidBodyForwardDynamicsSpherical)
246 {
247  Body base_body(1., Vector3d(0., 0., 0.), Vector3d(2., 2., 3.));
248 
249  model->addBody(0, SpatialTransform(), Joint(JointTypeSpherical), base_body);
250  VectorNd Q = VectorNd::Constant((size_t)model->q_size, 0.);
251  model->SetQuaternion(1, Quaternion(), Q);
252  MatrixNd H = MatrixNd::Constant((size_t)model->qdot_size, (size_t)model->qdot_size, 0.);
253  compositeRigidBodyAlgorithm(*model, Q, H, true);
254 
255  Matrix3d H_ref(2., 0., 0., 0., 2., 0., 0., 0., 3.);
256 
258 }
259 
260 int main(int argc, char** argv)
261 {
262  ::testing::InitGoogleTest(&argc, argv);
263  return RUN_ALL_TESTS();
264 }
static bool checkVectorNdEpsilonClose(const RobotDynamics::Math::VectorNd &v1, const RobotDynamics::Math::VectorNd &v2, const double epsilon)
void compositeRigidBodyAlgorithm(Model &model, const Math::VectorNd &Q, Math::MatrixNd &H, bool update_kinematics=true)
Computes the joint space inertia matrix by using the Composite Rigid Body Algorithm.
Definition: Dynamics.cc:302
Describes all properties of a single body.
Definition: Body.h:30
VectorNd Tau
VectorNd QDDot
bool linSolveGaussElimPivot(MatrixNd A, VectorNd b, VectorNd &x)
Solves a linear system using gaussian elimination with pivoting.
VectorNd QDot
const double TEST_PREC
VectorNd Q
Math::MotionVector gravity
the cartesian vector of the gravity
Definition: Model.h:196
TEST_F(CompositeRigidBodyTestFixture, TestCompositeRigidBodyForwardDynamicsFloatingBase)
Compact representation of spatial transformations.
Describes a joint relative to the predecessor body.
Definition: Joint.h:228
Quaternion that are used for singularity free joints.
Definition: Quaternion.h:25
Eigen::VectorXd VectorNd
Definition: rdl_eigenmath.h:25
static bool checkMatrixNdEpsilonClose(const RobotDynamics::Math::MatrixNd &t1, const RobotDynamics::Math::MatrixNd &t2, const double epsilon)
Contains all information about the rigid body model.
Definition: Model.h:121
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
Eigen::MatrixXd MatrixNd
Definition: rdl_eigenmath.h:26
int main(int argc, char **argv)
const double TEST_PREC
void inverseDynamics(Model &model, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::VectorNd &QDDot, Math::VectorNd &Tau, Math::SpatialForceV *f_ext=nullptr, bool update_kinematics=true)
Definition: Dynamics.cc:23
3 DoF joint using Quaternions for joint positional variables and
Definition: Joint.h:195
void forwardDynamics(Model &model, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::VectorNd &Tau, Math::VectorNd &QDDot, Math::SpatialForceV *f_ext=nullptr, bool update_kinematics=true)
Computes forward dynamics with the Articulated Body Algorithm.
Definition: Dynamics.cc:455
Namespace for all structures of the RobotDynamics library.
Definition: Body.h:21
static bool checkMatrix3dEpsilonClose(const RobotDynamics::Math::Matrix3d &t1, const RobotDynamics::Math::Matrix3d &t2, const double epsilon)


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