RdlInverseDynamicsTests.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include "UnitTestUtils.hpp"
4 #include "Human36Fixture.h"
5 
9 
10 using namespace std;
11 using namespace RobotDynamics;
12 using namespace RobotDynamics::Math;
13 
14 const double TEST_PREC = 1.0e-14;
15 
16 struct RbdlimInverseDynamicsFixture : public testing::Test
17 {
19  {
20  }
21 
22  void SetUp()
23  {
24  model = new Model;
25  model->gravity = SpatialVector(0., 0., 0., 0., -9.81, 0.);
26  }
27 
28  void TearDown()
29  {
31  delete model;
32  }
33 
35 };
36 
37 TEST_F(RbdlimInverseDynamicsFixture, TestInverseForwardDynamicsFloatingBase)
38 {
39  Body base_body(1., Vector3d(1., 0., 0.), Vector3d(1., 1., 1.));
40 
41  model->addBody(0, SpatialTransform(),
42  Joint(SpatialVector(0., 0., 0., 1., 0., 0.), SpatialVector(0., 0., 0., 0., 1., 0.), SpatialVector(0., 0., 0., 0., 0., 1.),
43  SpatialVector(0., 0., 1., 0., 0., 0.), SpatialVector(0., 1., 0., 0., 0., 0.), SpatialVector(1., 0., 0., 0., 0., 0.)),
44  base_body);
45 
46  // Initialization of the input vectors
47  VectorNd Q = VectorNd::Constant((size_t)model->dof_count, 0.);
48  VectorNd QDot = VectorNd::Constant((size_t)model->dof_count, 0.);
49  VectorNd QDDot = VectorNd::Constant((size_t)model->dof_count, 0.);
50  VectorNd Tau = VectorNd::Constant((size_t)model->dof_count, 0.);
51  VectorNd TauInv = VectorNd::Constant((size_t)model->dof_count, 0.);
52 
53  Q[0] = 1.1;
54  Q[1] = 1.2;
55  Q[2] = 1.3;
56  Q[3] = 0.1;
57  Q[4] = 0.2;
58  Q[5] = 0.3;
59 
60  QDot[0] = 1.1;
61  QDot[1] = -1.2;
62  QDot[2] = 1.3;
63  QDot[3] = -0.1;
64  QDot[4] = 0.2;
65  QDot[5] = -0.3;
66 
67  Tau[0] = 2.1;
68  Tau[1] = 2.2;
69  Tau[2] = 2.3;
70  Tau[3] = 1.1;
71  Tau[4] = 1.2;
72  Tau[5] = 1.3;
73 
74  forwardDynamics(*model, Q, QDot, Tau, QDDot);
75  inverseDynamics(*model, Q, QDot, QDDot, TauInv);
76 
78 }
79 
80 TEST_F(RbdlimInverseDynamicsFixture, TestInverseForwardDynamicsFloatingBaseNoKinematics)
81 {
82  Body base_body(1., Vector3d(1., 0., 0.), Vector3d(1., 1., 1.));
83 
84  model->addBody(0, SpatialTransform(),
85  Joint(SpatialVector(0., 0., 0., 1., 0., 0.), SpatialVector(0., 0., 0., 0., 1., 0.), SpatialVector(0., 0., 0., 0., 0., 1.),
86  SpatialVector(0., 0., 1., 0., 0., 0.), SpatialVector(0., 1., 0., 0., 0., 0.), SpatialVector(1., 0., 0., 0., 0., 0.)),
87  base_body);
88 
89  // Initialization of the input vectors
90  VectorNd Q = VectorNd::Constant((size_t)model->dof_count, 0.);
91  VectorNd QDot = VectorNd::Constant((size_t)model->dof_count, 0.);
92  VectorNd QDDot = VectorNd::Constant((size_t)model->dof_count, 0.);
93  VectorNd Tau = VectorNd::Constant((size_t)model->dof_count, 0.);
94  VectorNd TauInv = VectorNd::Constant((size_t)model->dof_count, 0.);
95 
96  Q[0] = 1.1;
97  Q[1] = 1.2;
98  Q[2] = 1.3;
99  Q[3] = 0.1;
100  Q[4] = 0.2;
101  Q[5] = 0.3;
102 
103  QDot[0] = 1.1;
104  QDot[1] = -1.2;
105  QDot[2] = 1.3;
106  QDot[3] = -0.1;
107  QDot[4] = 0.2;
108  QDot[5] = -0.3;
109 
110  Tau[0] = 2.1;
111  Tau[1] = 2.2;
112  Tau[2] = 2.3;
113  Tau[3] = 1.1;
114  Tau[4] = 1.2;
115  Tau[5] = 1.3;
116 
117  updateKinematicsCustom(*model, &Q, &QDot, &QDDot);
118  forwardDynamics(*model, Q, QDot, Tau, QDDot, nullptr, false);
119  inverseDynamics(*model, Q, QDot, QDDot, TauInv);
120 
122 }
123 
124 TEST_F(Human36, TestInverseForwardDynamicsFloatingBaseExternalForces)
125 {
126  randomizeStates();
127 
128  MatrixNd M(model_emulated->qdot_size, model_emulated->qdot_size);
129  VectorNd N(model_emulated->qdot_size);
130  M.setZero();
131  N.setZero();
132 
133  unsigned int foot_r_id = model_emulated->GetBodyId("foot_r");
134  SpatialForce F(model_emulated->bodyFrames[foot_r_id], -1., 2., 3.1, 82., -23., 500.1);
135 
136  MatrixNd J_r_foot(6, model_emulated->qdot_size);
137  J_r_foot.setZero();
138 
139  updateKinematics(*model_emulated, q, qdot, qddot);
140  calcBodySpatialJacobian(*model_emulated, q, foot_r_id, J_r_foot, false);
141  compositeRigidBodyAlgorithm(*model_emulated, q, M);
142  nonlinearEffects(*model_emulated, q, qdot, N);
143 
144  VectorNd tau_cf(model_emulated->qdot_size), tau_id(model_emulated->qdot_size);
145  tau_cf.setZero();
146 
147  tau_cf = M * qddot + N - J_r_foot.transpose() * F;
148 
149  std::shared_ptr<Math::SpatialForceV> f_ext(new Math::SpatialForceV(model_emulated->mBodies.size()));
150 
151  for (int i = 0; i < model_emulated->mBodies.size(); i++)
152  {
153  (*f_ext)[i].setIncludingFrame(model_emulated->bodyFrames[i], 0., 0., 0., 0., 0., 0.);
154  if (i == foot_r_id)
155  {
156  (*f_ext)[i].fx() = F.fx();
157  (*f_ext)[i].fy() = F.fy();
158  (*f_ext)[i].fz() = F.fz();
159  (*f_ext)[i].mx() = F.mx();
160  (*f_ext)[i].my() = F.my();
161  (*f_ext)[i].mz() = F.mz();
162  }
163  }
164 
165  inverseDynamics(*model_emulated, q, qdot, qddot, tau_id, f_ext.get());
166 
167  EXPECT_TRUE(unit_test_utils::checkVectorNdEpsilonClose(tau_id, tau_cf, 1e-11));
168 }
169 
170 TEST_F(Human36, TestInverseForwardDynamicsFloatingBaseExternalForcesNoKinematics)
171 {
172  randomizeStates();
173 
174  MatrixNd M(model_emulated->qdot_size, model_emulated->qdot_size);
175  VectorNd N(model_emulated->qdot_size);
176  M.setZero();
177  N.setZero();
178 
179  unsigned int foot_r_id = model_emulated->GetBodyId("foot_r");
180  SpatialForce F(model_emulated->bodyFrames[foot_r_id], -1., 2., 3.1, 82., -23., 500.1);
181 
182  MatrixNd J_r_foot(6, model_emulated->qdot_size);
183  J_r_foot.setZero();
184 
185  updateKinematics(*model_emulated, q, qdot, qddot);
186  calcBodySpatialJacobian(*model_emulated, q, foot_r_id, J_r_foot, false);
187  compositeRigidBodyAlgorithm(*model_emulated, q, M);
188  nonlinearEffects(*model_emulated, q, qdot, N);
189 
190  VectorNd tau_cf(model_emulated->qdot_size), tau_id(model_emulated->qdot_size);
191  tau_cf.setZero();
192 
193  tau_cf = M * qddot + N - J_r_foot.transpose() * F;
194 
195  std::shared_ptr<Math::SpatialForceV> f_ext(new Math::SpatialForceV(model_emulated->mBodies.size()));
196 
197  for (int i = 0; i < model_emulated->mBodies.size(); i++)
198  {
199  (*f_ext)[i].setIncludingFrame(model_emulated->bodyFrames[i], 0., 0., 0., 0., 0., 0.);
200  if (i == foot_r_id)
201  {
202  (*f_ext)[i].fx() = F.fx();
203  (*f_ext)[i].fy() = F.fy();
204  (*f_ext)[i].fz() = F.fz();
205  (*f_ext)[i].mx() = F.mx();
206  (*f_ext)[i].my() = F.my();
207  (*f_ext)[i].mz() = F.mz();
208  }
209  }
210 
211  inverseDynamics(*model_emulated, q, qdot, qddot, tau_id, f_ext.get(), false);
212 
213  EXPECT_TRUE(unit_test_utils::checkVectorNdEpsilonClose(tau_id, tau_cf, 1e-11));
214 }
215 
216 int main(int argc, char** argv)
217 {
218  ::testing::InitGoogleTest(&argc, argv);
219  return RUN_ALL_TESTS();
220 }
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
std::vector< SpatialForce, Eigen::aligned_allocator< SpatialForce > > SpatialForceV
Describes all properties of a single body.
Definition: Body.h:30
VectorNd Tau
VectorNd QDDot
VectorNd QDot
const double TEST_PREC
const double TEST_PREC
VectorNd Q
bool checkModelZeroVectorsAndMatrices(RobotDynamics::Model &model)
EIGEN_STRONG_INLINE double & fy()
Get reference to y-linear component.
Math::MotionVector gravity
the cartesian vector of the gravity
Definition: Model.h:196
int main(int argc, char **argv)
EIGEN_STRONG_INLINE double & fx()
Get reference to x-linear component.
EIGEN_STRONG_INLINE double & fz()
Get reference to z-linear component.
EIGEN_STRONG_INLINE double & my()
Get reference to y-angular component.
Compact representation of spatial transformations.
Describes a joint relative to the predecessor body.
Definition: Joint.h:228
Eigen::VectorXd VectorNd
Definition: rdl_eigenmath.h:25
EIGEN_STRONG_INLINE double & mz()
Get reference to z-angular component.
void calcBodySpatialJacobian(Model &model, const Math::VectorNd &Q, unsigned int body_id, Math::MatrixNd &G, bool update_kinematics=true)
Computes the spatial jacobian for a body. The result will be returned via the G argument and represen...
Definition: Kinematics.cc:1310
void updateKinematics(Model &model, const Math::VectorNd &Q, const Math::VectorNd &QDot, const Math::VectorNd &QDDot)
Updates and computes velocities and accelerations of the bodies.
Definition: Kinematics.cc:25
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
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
TEST_F(RbdlimInverseDynamicsFixture, TestInverseForwardDynamicsFloatingBase)
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
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
void nonlinearEffects(Model &model, const Math::VectorNd &Q, const Math::VectorNd &QDot, Math::VectorNd &Tau, bool update_kinematics=true)
Computes the coriolis forces.
Definition: Dynamics.cc:236
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