@@ -91,6 +91,9 @@ class DynamicsTest : public ::testing::Test
91
91
// transformations, spatial velocities, and spatial accelerations correctly.
92
92
void testForwardKinematics (const common::Uri& uri);
93
93
94
+ // Test inverse dynamics with various joint forces.
95
+ void testInverseDynamics (const common::Uri& uri);
96
+
94
97
// Compare dynamics terms in equations of motion such as mass matrix, mass
95
98
// inverse matrix, Coriolis force vector, gravity force vector, and external
96
99
// force vector.
@@ -1385,6 +1388,84 @@ void DynamicsTest::testForwardKinematics(const common::Uri& uri)
1385
1388
}
1386
1389
}
1387
1390
1391
+ // ==============================================================================
1392
+ void DynamicsTest::testInverseDynamics (const common::Uri& uri)
1393
+ {
1394
+ // ---------------------------- Settings --------------------------------------
1395
+ // Number of random state tests for each skeleton
1396
+ #ifndef NDEBUG // Debug mode
1397
+ const std::size_t nRandomItr = 2 ;
1398
+ #else
1399
+ const std::size_t nRandomItr = 100 ;
1400
+ #endif
1401
+
1402
+ // Lower and upper bound of configuration for system
1403
+ const double lb = -1.0 * math::constantsd::pi ();
1404
+ const double ub = 1.0 * math::constantsd::pi ();
1405
+
1406
+ // Lower and upper bound of joint damping and stiffness
1407
+ const double lbD = 0.0 ;
1408
+ const double ubD = 10.0 ;
1409
+ const double lbK = 0.0 ;
1410
+ const double ubK = 10.0 ;
1411
+
1412
+ // Lower and upper bound of joint forces
1413
+ const double lbF = -10.0 ;
1414
+ const double ubF = 10.0 ;
1415
+
1416
+ simulation::WorldPtr world = utils::SkelParser::readWorld (uri);
1417
+ ASSERT_TRUE (world != nullptr );
1418
+
1419
+ for (std::size_t i = 0 ; i < world->getNumSkeletons (); ++i)
1420
+ {
1421
+ dynamics::SkeletonPtr skel = world->getSkeleton (i);
1422
+ const std::size_t dofs = skel->getNumDofs ();
1423
+
1424
+ for (std::size_t j = 0 ; j < nRandomItr; ++j)
1425
+ {
1426
+ // Random joint stiffness and damping coefficients
1427
+ for (std::size_t l = 0 ; l < dofs; ++l)
1428
+ {
1429
+ dynamics::DegreeOfFreedom* dof = skel->getDof (l);
1430
+ dof->setDampingCoefficient (math::Random::uniform (lbD, ubD));
1431
+ dof->setSpringStiffness (math::Random::uniform (lbK, ubK));
1432
+
1433
+ double lbRP = dof->getPositionLowerLimit ();
1434
+ double ubRP = dof->getPositionUpperLimit ();
1435
+ lbRP = std::max (lbRP, -math::constantsd::pi ());
1436
+ ubRP = std::min (ubRP, +math::constantsd::pi ());
1437
+ dof->setRestPosition (math::Random::uniform (lbRP, ubRP));
1438
+ }
1439
+
1440
+ // Set random states and forces
1441
+ const VectorXd q0 = math::Random::uniform<VectorXd>(dofs, lb, ub);
1442
+ const VectorXd dq0 = math::Random::uniform<VectorXd>(dofs, lb, ub);
1443
+ const VectorXd f0 = math::Random::uniform<VectorXd>(dofs, lbF, ubF);
1444
+
1445
+ // Forward dynamics to compute accelerations
1446
+ skel->setPositions (q0);
1447
+ skel->setVelocities (dq0);
1448
+ skel->setForces (f0);
1449
+ skel->computeForwardDynamics ();
1450
+ const VectorXd ddq = skel->getAccelerations ();
1451
+
1452
+ // Test inverse dynamics to ensure the result joint forces are identical
1453
+ // to the forces used to compute the input accelerations
1454
+ skel->setAccelerations (ddq);
1455
+ skel->computeInverseDynamics (false , true , true );
1456
+ const VectorXd f = skel->getForces ();
1457
+ EXPECT_TRUE (equals (f, f0));
1458
+
1459
+ // Test forward dynamics to ensure the result joint accelerations are
1460
+ // identical to the accelerations used to compute the input forces
1461
+ skel->setForces (f);
1462
+ skel->computeForwardDynamics ();
1463
+ const VectorXd ddq2 = skel->getAccelerations ();
1464
+ EXPECT_TRUE (equals (ddq, ddq2));
1465
+ }
1466
+ }
1467
+ }
1468
+
1388
1469
// ==============================================================================
1389
1470
void DynamicsTest::compareEquationsOfMotion (const common::Uri& uri)
1390
1471
{
@@ -2163,6 +2244,18 @@ TEST_F(DynamicsTest, testForwardKinematics)
2163
2244
}
2164
2245
}
2165
2246
2247
+ // ==============================================================================
2248
+ TEST_F (DynamicsTest, testInverseDynamics)
2249
+ {
2250
+ for (std::size_t i = 0 ; i < getList ().size (); ++i)
2251
+ {
2252
+ #ifndef NDEBUG
2253
+ dtdbg << getList ()[i].toString () << std::endl;
2254
+ #endif
2255
+ testInverseDynamics (getList ()[i]);
2256
+ }
2257
+ }
2258
+
2166
2259
// ==============================================================================
2167
2260
TEST_F (DynamicsTest, compareEquationsOfMotion)
2168
2261
{
0 commit comments