Debian Codegraph @

unittests/integration/test_Joints.cpp

raw
1 /*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <array>
34 #include <iostream>
35
36 #include <gtest/gtest.h>
37
38 #include "dart/dynamics/BallJoint.hpp"
39 #include "dart/dynamics/BodyNode.hpp"
40 #include "dart/dynamics/EulerJoint.hpp"
41 #include "dart/dynamics/FreeJoint.hpp"
42 #include "dart/dynamics/PlanarJoint.hpp"
43 #include "dart/dynamics/PrismaticJoint.hpp"
44 #include "dart/dynamics/RevoluteJoint.hpp"
45 #include "dart/dynamics/ScrewJoint.hpp"
46 #include "dart/dynamics/Skeleton.hpp"
47 #include "dart/dynamics/TranslationalJoint.hpp"
48 #include "dart/dynamics/TranslationalJoint2D.hpp"
49 #include "dart/dynamics/UniversalJoint.hpp"
50 #include "dart/dynamics/WeldJoint.hpp"
51 #include "dart/math/Geometry.hpp"
52 #include "dart/math/Helpers.hpp"
53 #include "dart/simulation/World.hpp"
54 #include "dart/utils/SkelParser.hpp"
55
56 #include "TestHelpers.hpp"
57
58 using namespace dart;
59 using namespace dart::math;
60 using namespace dart::dynamics;
61 using namespace dart::simulation;
62
63 #define JOINT_TOL 0.01
64
65 //==============================================================================
66 class JOINTS : public testing::Test
67 {
68 public:
69 // Get reference frames
70 const std::vector<SimpleFrame*>& getFrames() const;
71
72 // Randomize the properties of all the reference frames
73 void randomizeRefFrames();
74
75 #ifdef _WIN32
76 template <typename JointType>
77 static typename JointType::Properties createJointProperties()
78 {
79 return typename JointType::Properties();
80 }
81 #endif
82
83 template <typename JointType>
84 void kinematicsTest(
85 #ifdef _WIN32
86 const typename JointType::Properties& _joint
87 = createJointProperties<JointType>());
88 #else
89 const typename JointType::Properties& _joint
90 = typename JointType::Properties());
91 #endif
92
93 protected:
94 // Sets up the test fixture.
95 void SetUp() override;
96
97 std::vector<SimpleFrame*> frames;
98 };
99
100 //==============================================================================
101 void JOINTS::SetUp()
102 {
103 // Create a list of reference frames to use during tests
104 frames.push_back(new SimpleFrame(Frame::World(), "refFrame1"));
105 frames.push_back(new SimpleFrame(frames.back(), "refFrame2"));
106 frames.push_back(new SimpleFrame(frames.back(), "refFrame3"));
107 frames.push_back(new SimpleFrame(frames.back(), "refFrame4"));
108 frames.push_back(new SimpleFrame(Frame::World(), "refFrame5"));
109 frames.push_back(new SimpleFrame(frames.back(), "refFrame6"));
110 }
111
112 //==============================================================================
113 const std::vector<SimpleFrame*>& JOINTS::getFrames() const
114 {
115 return frames;
116 }
117
118 //==============================================================================
119 void JOINTS::randomizeRefFrames()
120 {
121 for (std::size_t i = 0; i < frames.size(); ++i)
122 {
123 SimpleFrame* F = frames[i];
124
125 Eigen::Vector3d p = Random::uniform<Eigen::Vector3d>(-100, 100);
126 Eigen::Vector3d theta = Random::uniform<Eigen::Vector3d>(
127 -2 * dart::math::constantsd::pi(), 2 * dart::math::constantsd::pi());
128
129 Eigen::Isometry3d tf(Eigen::Isometry3d::Identity());
130 tf.translate(p);
131 tf.linear() = math::eulerXYZToMatrix(theta);
132
133 F->setRelativeTransform(tf);
134 F->setRelativeSpatialVelocity(Random::uniform<Eigen::Vector6d>(-100, 100));
135 F->setRelativeSpatialAcceleration(
136 Random::uniform<Eigen::Vector6d>(-100, 100));
137 }
138 }
139
140 //==============================================================================
141 template <typename JointType>
142 void JOINTS::kinematicsTest(const typename JointType::Properties& _properties)
143 {
144 int numTests = 1;
145
146 SkeletonPtr skeleton = Skeleton::create();
147 Joint* joint
148 = skeleton->createJointAndBodyNodePair<JointType>(nullptr, _properties)
149 .first;
150 joint->setTransformFromChildBodyNode(math::expMap(Eigen::Vector6d::Random()));
151 joint->setTransformFromParentBodyNode(
152 math::expMap(Eigen::Vector6d::Random()));
153
154 int dof = joint->getNumDofs();
155
156 //--------------------------------------------------------------------------
157 //
158 //--------------------------------------------------------------------------
159 VectorXd q = VectorXd::Zero(dof);
160 VectorXd dq = VectorXd::Zero(dof);
161
162 for (int idxTest = 0; idxTest < numTests; ++idxTest)
163 {
164 double q_delta = 0.000001;
165
166 for (int i = 0; i < dof; ++i)
167 {
168 q(i) = Random::uniform(-constantsd::pi() * 1.0, constantsd::pi() * 1.0);
169 dq(i) = Random::uniform(-constantsd::pi() * 1.0, constantsd::pi() * 1.0);
170 }
171
172 joint->setPositions(q);
173 joint->setVelocities(dq);
174
175 if (dof == 0)
176 return;
177
178 Eigen::Isometry3d T = joint->getRelativeTransform();
179 Jacobian J = joint->getRelativeJacobian();
180 Jacobian dJ = joint->getRelativeJacobianTimeDeriv();
181
182 //--------------------------------------------------------------------------
183 // Test T
184 //--------------------------------------------------------------------------
185 EXPECT_TRUE(math::verifyTransform(T));
186
187 //--------------------------------------------------------------------------
188 // Test analytic Jacobian and numerical Jacobian
189 // J == numericalJ
190 //--------------------------------------------------------------------------
191 Jacobian numericJ = Jacobian::Zero(6, dof);
192 for (int i = 0; i < dof; ++i)
193 {
194 // a
195 Eigen::VectorXd q_a = q;
196 joint->setPositions(q_a);
197 Eigen::Isometry3d T_a = joint->getRelativeTransform();
198
199 // b
200 Eigen::VectorXd q_b = q;
201 q_b(i) += q_delta;
202 joint->setPositions(q_b);
203 Eigen::Isometry3d T_b = joint->getRelativeTransform();
204
205 //
206 Eigen::Isometry3d Tinv_a = T_a.inverse();
207 Eigen::Matrix4d Tinv_a_eigen = Tinv_a.matrix();
208
209 // dTdq
210 Eigen::Matrix4d T_a_eigen = T_a.matrix();
211 Eigen::Matrix4d T_b_eigen = T_b.matrix();
212 Eigen::Matrix4d dTdq_eigen = (T_b_eigen - T_a_eigen) / q_delta;
213 // Matrix4d dTdq_eigen = (T_b_eigen * T_a_eigen.inverse()) / dt;
214
215 // J(i)
216 Eigen::Matrix4d Ji_4x4matrix_eigen = Tinv_a_eigen * dTdq_eigen;
217 Eigen::Vector6d Ji;
218 Ji[0] = Ji_4x4matrix_eigen(2, 1);
219 Ji[1] = Ji_4x4matrix_eigen(0, 2);
220 Ji[2] = Ji_4x4matrix_eigen(1, 0);
221 Ji[3] = Ji_4x4matrix_eigen(0, 3);
222 Ji[4] = Ji_4x4matrix_eigen(1, 3);
223 Ji[5] = Ji_4x4matrix_eigen(2, 3);
224 numericJ.col(i) = Ji;
225 }
226
227 for (int i = 0; i < dof; ++i)
228 {
229 for (int j = 0; j < 6; ++j)
230 EXPECT_NEAR(J.col(i)(j), numericJ.col(i)(j), JOINT_TOL);
231 }
232
233 //--------------------------------------------------------------------------
234 // Test first time derivative of analytic Jacobian and numerical Jacobian
235 // dJ == numerical_dJ
236 //--------------------------------------------------------------------------
237 Jacobian numeric_dJ = Jacobian::Zero(6, dof);
238 for (int i = 0; i < dof; ++i)
239 {
240 // a
241 Eigen::VectorXd q_a = q;
242 joint->setPositions(q_a);
243 Jacobian J_a = joint->getRelativeJacobian();
244
245 // b
246 Eigen::VectorXd q_b = q;
247 q_b(i) += q_delta;
248 joint->setPositions(q_b);
249 Jacobian J_b = joint->getRelativeJacobian();
250
251 //
252 Jacobian dJ_dq = (J_b - J_a) / q_delta;
253
254 // J(i)
255 numeric_dJ += dJ_dq * dq(i);
256 }
257
258 for (int i = 0; i < dof; ++i)
259 {
260 for (int j = 0; j < 6; ++j)
261 EXPECT_NEAR(dJ.col(i)(j), numeric_dJ.col(i)(j), JOINT_TOL);
262 }
263 }
264
265 // Forward kinematics test with high joint position
266 double posMin = -1e+64;
267 double posMax = +1e+64;
268
269 for (int idxTest = 0; idxTest < numTests; ++idxTest)
270 {
271 for (int i = 0; i < dof; ++i)
272 q(i) = Random::uniform(posMin, posMax);
273
274 skeleton->setPositions(q);
275
276 if (joint->getNumDofs() == 0)
277 return;
278
279 Eigen::Isometry3d T = joint->getRelativeTransform();
280 EXPECT_TRUE(math::verifyTransform(T));
281 }
282 }
283
284 // 0-dof joint
285 TEST_F(JOINTS, WELD_JOINT)
286 {
287 kinematicsTest<WeldJoint>();
288 }
289
290 // 1-dof joint
291 TEST_F(JOINTS, REVOLUTE_JOINT)
292 {
293 kinematicsTest<RevoluteJoint>();
294 }
295
296 // 1-dof joint
297 TEST_F(JOINTS, PRISMATIC_JOINT)
298 {
299 kinematicsTest<PrismaticJoint>();
300 }
301
302 // 1-dof joint
303 TEST_F(JOINTS, SCREW_JOINT)
304 {
305 kinematicsTest<ScrewJoint>();
306 }
307
308 // 2-dof joint
309 TEST_F(JOINTS, UNIVERSAL_JOINT)
310 {
311 kinematicsTest<UniversalJoint>();
312 }
313
314 // 2-dof joint
315 TEST_F(JOINTS, TRANSLATIONAL_JOINT_2D)
316 {
317 kinematicsTest<TranslationalJoint2D>();
318 }
319
320 // 3-dof joint
321 // TEST_F(JOINTS, BALL_JOINT)
322 //{
323 // kinematicsTest<BallJoint>();
324 //}
325 // TODO(JS): Disabled the test compares analytical Jacobian and numerical
326 // Jacobian since the meaning of BallJoint Jacobian is changed per
327 // we now use angular velocity and angular accertions as BallJoint's generalized
328 // velocities and accelerations, repectively.
329
330 // 3-dof joint
331 TEST_F(JOINTS, EULER_JOINT)
332 {
333 EulerJoint::Properties properties;
334
335 properties.mAxisOrder = EulerJoint::AxisOrder::XYZ;
336 kinematicsTest<EulerJoint>(properties);
337
338 properties.mAxisOrder = EulerJoint::AxisOrder::ZYX;
339 kinematicsTest<EulerJoint>(properties);
340 }
341
342 // 3-dof joint
343 TEST_F(JOINTS, TRANSLATIONAL_JOINT)
344 {
345 kinematicsTest<TranslationalJoint>();
346 }
347
348 // 3-dof joint
349 TEST_F(JOINTS, PLANAR_JOINT)
350 {
351 kinematicsTest<PlanarJoint>();
352 }
353
354 // 6-dof joint
355 // TEST_F(JOINTS, FREE_JOINT)
356 //{
357 // kinematicsTest<FreeJoint>();
358 //}
359 // TODO(JS): Disabled the test compares analytical Jacobian and numerical
360 // Jacobian since the meaning of FreeJoint Jacobian is changed per
361 // we now use spatial velocity and spatial accertions as FreeJoint's generalized
362 // velocities and accelerations, repectively.
363
364 //==============================================================================
365 template <
366 void (Joint::*setX)(std::size_t, double),
367 void (Joint::*setXLowerLimit)(std::size_t, double),
368 void (Joint::*setXUpperLimit)(std::size_t, double)>
369 void testCommandLimits(dynamics::Joint* joint)
370 {
371 const double lower = -5.0;
372 const double upper = +5.0;
373 const double mid = 0.5 * (lower + upper);
374 const double lessThanLower = -10.0;
375 const double greaterThanUpper = +10.0;
376
377 for (std::size_t i = 0; i < joint->getNumDofs(); ++i)
378 {
379 (joint->*setXLowerLimit)(i, lower);
380 (joint->*setXUpperLimit)(i, upper);
381
382 joint->setCommand(i, mid);
383 EXPECT_EQ(joint->getCommand(i), mid);
384 (joint->*setX)(i, mid);
385 EXPECT_EQ(joint->getCommand(i), mid);
386
387 joint->setCommand(i, lessThanLower);
388 EXPECT_EQ(joint->getCommand(i), lower);
389 (joint->*setX)(i, lessThanLower);
390 EXPECT_EQ(joint->getCommand(i), lessThanLower);
391
392 joint->setCommand(i, greaterThanUpper);
393 EXPECT_EQ(joint->getCommand(i), upper);
394 (joint->*setX)(i, greaterThanUpper);
395 EXPECT_EQ(joint->getCommand(i), greaterThanUpper);
396 }
397 }
398
399 //==============================================================================
400 TEST_F(JOINTS, COMMAND_LIMIT)
401 {
402 simulation::WorldPtr myWorld = utils::SkelParser::readWorld(
403 "dart://sample/skel/test/joint_limit_test.skel");
404 EXPECT_TRUE(myWorld != nullptr);
405
406 dynamics::SkeletonPtr pendulum = myWorld->getSkeleton("double_pendulum");
407 EXPECT_TRUE(pendulum != nullptr);
408
409 auto bodyNodes = pendulum->getBodyNodes();
410
411 for (auto bodyNode : bodyNodes)
412 {
413 Joint* joint = bodyNode->getParentJoint();
414
415 joint->setActuatorType(Joint::FORCE);
416 EXPECT_EQ(joint->getActuatorType(), Joint::FORCE);
417 testCommandLimits<
418 &Joint::setForce,
419 &Joint::setForceLowerLimit,
420 &Joint::setForceUpperLimit>(joint);
421
422 joint->setActuatorType(Joint::ACCELERATION);
423 EXPECT_EQ(joint->getActuatorType(), Joint::ACCELERATION);
424 testCommandLimits<
425 &Joint::setAcceleration,
426 &Joint::setAccelerationLowerLimit,
427 &Joint::setAccelerationUpperLimit>(joint);
428
429 joint->setActuatorType(Joint::VELOCITY);
430 EXPECT_EQ(joint->getActuatorType(), Joint::VELOCITY);
431 testCommandLimits<
432 &Joint::setVelocity,
433 &Joint::setVelocityLowerLimit,
434 &Joint::setVelocityUpperLimit>(joint);
435 }
436 }
437
438 //==============================================================================
439 TEST_F(JOINTS, POSITION_LIMIT)
440 {
441 double tol = 1e-3;
442
443 simulation::WorldPtr myWorld = utils::SkelParser::readWorld(
444 "dart://sample/skel/test/joint_limit_test.skel");
445 EXPECT_TRUE(myWorld != nullptr);
446
447 myWorld->setGravity(Eigen::Vector3d(0.0, 0.0, 0.0));
448
449 dynamics::SkeletonPtr pendulum = myWorld->getSkeleton("double_pendulum");
450 EXPECT_TRUE(pendulum != nullptr);
451
452 dynamics::Joint* joint0 = pendulum->getJoint("joint0");
453 dynamics::Joint* joint1 = pendulum->getJoint("joint1");
454
455 EXPECT_TRUE(joint0 != nullptr);
456 EXPECT_TRUE(joint1 != nullptr);
457
458 double limit0 = constantsd::pi() / 6.0;
459 double limit1 = constantsd::pi() / 6.0;
460
461 joint0->setLimitEnforcement(true);
462 joint0->setPositionLowerLimit(0, -limit0);
463 joint0->setPositionUpperLimit(0, limit0);
464
465 joint1->setLimitEnforcement(true);
466 joint1->setPositionLowerLimit(0, -limit1);
467 joint1->setPositionUpperLimit(0, limit1);
468
469 #ifndef NDEBUG // Debug mode
470 double simTime = 0.2;
471 #else
472 double simTime = 2.0;
473 #endif // ------- Debug mode
474 double timeStep = myWorld->getTimeStep();
475 int nSteps = simTime / timeStep;
476
477 // Two seconds with positive control forces
478 for (int i = 0; i < nSteps; i++)
479 {
480 joint0->setForce(0, 0.1);
481 joint1->setForce(0, 0.1);
482 myWorld->step();
483
484 double jointPos0 = joint0->getPosition(0);
485 double jointPos1 = joint1->getPosition(0);
486
487 EXPECT_GE(jointPos0, -limit0 - tol);
488 EXPECT_GE(jointPos1, -limit1 - tol);
489
490 EXPECT_LE(jointPos0, limit0 + tol);
491 EXPECT_LE(jointPos1, limit1 + tol);
492 }
493
494 // Two more seconds with negative control forces
495 for (int i = 0; i < nSteps; i++)
496 {
497 joint0->setForce(0, -0.1);
498 joint1->setForce(0, -0.1);
499 myWorld->step();
500
501 double jointPos0 = joint0->getPosition(0);
502 double jointPos1 = joint1->getPosition(0);
503
504 EXPECT_GE(jointPos0, -limit0 - tol);
505 EXPECT_GE(jointPos1, -limit1 - tol);
506
507 EXPECT_LE(jointPos0, limit0 + tol);
508 EXPECT_LE(jointPos1, limit1 + tol);
509 }
510 }
511
512 //==============================================================================
513 TEST_F(JOINTS, POSITION_AND_VELOCITY_LIMIT)
514 {
515 using namespace dart::math::suffixes;
516
517 const double tol = 1e-3;
518
519 simulation::WorldPtr myWorld = utils::SkelParser::readWorld(
520 "dart://sample/skel/test/joint_limit_test.skel");
521 EXPECT_TRUE(myWorld != nullptr);
522
523 myWorld->setGravity(Eigen::Vector3d(0.0, 0.0, 0.0));
524
525 dynamics::SkeletonPtr pendulum = myWorld->getSkeleton("double_pendulum");
526 EXPECT_TRUE(pendulum != nullptr);
527
528 dynamics::Joint* joint0 = pendulum->getJoint("joint0");
529 dynamics::Joint* joint1 = pendulum->getJoint("joint1");
530
531 EXPECT_TRUE(joint0 != nullptr);
532 EXPECT_TRUE(joint1 != nullptr);
533
534 const double posLimit0 = math::toRadian(360.0);
535 const double posLimit1 = math::toRadian(360.0);
536 const double velLimit0 = math::toRadian(5.0); // 5 degree per second
537 const double velLimit1 = math::toRadian(5.0); // 5 degree per second
538
539 joint0->setLimitEnforcement(true);
540 joint0->setPositionLowerLimit(0, -posLimit0);
541 joint0->setPositionUpperLimit(0, posLimit0);
542 joint0->setVelocityLowerLimit(0, -velLimit0);
543 joint0->setVelocityUpperLimit(0, velLimit0);
544
545 joint1->setLimitEnforcement(true);
546 joint1->setPositionLowerLimit(0, -posLimit1);
547 joint1->setPositionUpperLimit(0, posLimit1);
548 joint1->setVelocityLowerLimit(0, -velLimit1);
549 joint1->setVelocityUpperLimit(0, velLimit1);
550
551 #ifndef NDEBUG // Debug mode
552 double simTime = 0.2;
553 #else
554 double simTime = 2.0;
555 #endif // ------- Debug mode
556 double timeStep = myWorld->getTimeStep();
557 int nSteps = simTime / timeStep;
558
559 // Two seconds with positive control forces
560 for (int i = 0; i < nSteps; i++)
561 {
562 // Apply sufficient force to hit the limits
563 joint0->setForce(0, 1.0);
564 joint1->setForce(0, 1.0);
565 myWorld->step();
566
567 // Check position limits
568 const double jointPos0 = joint0->getPosition(0);
569 const double jointPos1 = joint1->getPosition(0);
570 EXPECT_GE(jointPos0, -posLimit0 - tol);
571 EXPECT_GE(jointPos1, -posLimit1 - tol);
572 EXPECT_LE(jointPos0, posLimit0 + tol);
573 EXPECT_LE(jointPos1, posLimit1 + tol);
574
575 // Check velocity limits
576 const double jointVel0 = joint0->getVelocity(0);
577 const double jointVel1 = joint1->getVelocity(0);
578 EXPECT_GE(jointVel0, -velLimit0 - tol);
579 EXPECT_GE(jointVel1, -velLimit1 - tol);
580 EXPECT_LE(jointVel0, velLimit0 + tol);
581 EXPECT_LE(jointVel1, velLimit1 + tol);
582 }
583
584 // Two more seconds with negative control forces
585 for (int i = 0; i < nSteps; i++)
586 {
587 // Apply sufficient force to hit the limits
588 joint0->setForce(0, -1.0);
589 joint1->setForce(0, -1.0);
590 myWorld->step();
591
592 // Check position limits
593 const double jointPos0 = joint0->getPosition(0);
594 const double jointPos1 = joint1->getPosition(0);
595 EXPECT_GE(jointPos0, -posLimit0 - tol);
596 EXPECT_GE(jointPos1, -posLimit1 - tol);
597 EXPECT_LE(jointPos0, posLimit0 + tol);
598 EXPECT_LE(jointPos1, posLimit1 + tol);
599
600 // Check velocity limits
601 const double jointVel0 = joint0->getVelocity(0);
602 const double jointVel1 = joint1->getVelocity(0);
603 EXPECT_GE(jointVel0, -velLimit0 - tol);
604 EXPECT_GE(jointVel1, -velLimit1 - tol);
605 EXPECT_LE(jointVel0, velLimit0 + tol);
606 EXPECT_LE(jointVel1, velLimit1 + tol);
607 }
608 }
609
610 //==============================================================================
611 TEST_F(JOINTS, JOINT_LIMITS)
612 {
613 simulation::WorldPtr myWorld = utils::SkelParser::readWorld(
614 "dart://sample/skel/test/joint_limit_test.skel");
615 EXPECT_TRUE(myWorld != nullptr);
616
617 myWorld->setGravity(Eigen::Vector3d(0.0, 0.0, 0.0));
618
619 dynamics::SkeletonPtr pendulum = myWorld->getSkeleton("double_pendulum");
620 EXPECT_TRUE(pendulum != nullptr);
621
622 dynamics::Joint* joint0 = pendulum->getJoint("joint0");
623
624 EXPECT_TRUE(joint0 != nullptr);
625
626 double limit = constantsd::pi() / 6.0;
627 Eigen::VectorXd limits = Eigen::VectorXd::Constant(1, constantsd::pi() / 2.0);
628
629 joint0->setPositionLowerLimit(0, -limit);
630 joint0->setPositionUpperLimit(0, limit);
631 EXPECT_EQ(
632 joint0->getPositionLowerLimits(), Eigen::VectorXd::Constant(1, -limit));
633 EXPECT_EQ(
634 joint0->getPositionUpperLimits(), Eigen::VectorXd::Constant(1, limit));
635
636 joint0->setPositionLowerLimits(-limits);
637 joint0->setPositionUpperLimits(limits);
638 EXPECT_EQ(joint0->getPositionLowerLimits(), -limits);
639 EXPECT_EQ(joint0->getPositionUpperLimits(), limits);
640
641 joint0->setVelocityLowerLimit(0, -limit);
642 joint0->setVelocityUpperLimit(0, limit);
643 EXPECT_EQ(
644 joint0->getVelocityLowerLimits(), Eigen::VectorXd::Constant(1, -limit));
645 EXPECT_EQ(
646 joint0->getVelocityUpperLimits(), Eigen::VectorXd::Constant(1, limit));
647
648 joint0->setVelocityLowerLimits(-limits);
649 joint0->setVelocityUpperLimits(limits);
650 EXPECT_EQ(joint0->getVelocityLowerLimits(), -limits);
651 EXPECT_EQ(joint0->getVelocityUpperLimits(), limits);
652
653 joint0->setAccelerationLowerLimit(0, -limit);
654 joint0->setAccelerationUpperLimit(0, limit);
655 EXPECT_EQ(
656 joint0->getAccelerationLowerLimits(),
657 Eigen::VectorXd::Constant(1, -limit));
658 EXPECT_EQ(
659 joint0->getAccelerationUpperLimits(),
660 Eigen::VectorXd::Constant(1, limit));
661
662 joint0->setAccelerationLowerLimits(-limits);
663 joint0->setAccelerationUpperLimits(limits);
664 EXPECT_EQ(joint0->getAccelerationLowerLimits(), -limits);
665 EXPECT_EQ(joint0->getAccelerationUpperLimits(), limits);
666
667 joint0->setForceLowerLimit(0, -limit);
668 joint0->setForceUpperLimit(0, limit);
669 EXPECT_EQ(
670 joint0->getForceLowerLimits(), Eigen::VectorXd::Constant(1, -limit));
671 EXPECT_EQ(joint0->getForceUpperLimits(), Eigen::VectorXd::Constant(1, limit));
672
673 joint0->setForceLowerLimits(-limits);
674 joint0->setForceUpperLimits(limits);
675 EXPECT_EQ(joint0->getForceLowerLimits(), -limits);
676 EXPECT_EQ(joint0->getForceUpperLimits(), limits);
677 }
678
679 //==============================================================================
680 void testJointCoulombFrictionForce(double _timeStep)
681 {
682 double tol = 1e-9;
683
684 simulation::WorldPtr myWorld = utils::SkelParser::readWorld(
685 "dart://sample/skel/test/joint_friction_test.skel");
686 EXPECT_TRUE(myWorld != nullptr);
687
688 myWorld->setGravity(Eigen::Vector3d(0.0, 0.0, 0.0));
689 myWorld->setTimeStep(_timeStep);
690
691 dynamics::SkeletonPtr pendulum = myWorld->getSkeleton("double_pendulum");
692 EXPECT_TRUE(pendulum != nullptr);
693 pendulum->disableSelfCollisionCheck();
694
695 dynamics::Joint* joint0 = pendulum->getJoint("joint0");
696 dynamics::Joint* joint1 = pendulum->getJoint("joint1");
697
698 EXPECT_TRUE(joint0 != nullptr);
699 EXPECT_TRUE(joint1 != nullptr);
700
701 double frictionForce = 5.0;
702
703 joint0->setLimitEnforcement(false);
704 joint1->setLimitEnforcement(false);
705
706 joint0->setCoulombFriction(0, frictionForce);
707 joint1->setCoulombFriction(0, frictionForce);
708
709 EXPECT_EQ(joint0->getCoulombFriction(0), frictionForce);
710 EXPECT_EQ(joint1->getCoulombFriction(0), frictionForce);
711
712 #ifndef NDEBUG // Debug mode
713 double simTime = 0.2;
714 #else
715 double simTime = 2.0;
716 #endif // ------- Debug mode
717 double timeStep = myWorld->getTimeStep();
718 int nSteps = simTime / timeStep;
719
720 // Two seconds with lower control forces than the friction
721 for (int i = 0; i < nSteps; i++)
722 {
723 joint0->setForce(0, +4.9);
724 joint1->setForce(0, +4.9);
725 myWorld->step();
726
727 double jointVel0 = joint0->getVelocity(0);
728 double jointVel1 = joint1->getVelocity(0);
729
730 EXPECT_NEAR(jointVel0, 0.0, tol);
731 EXPECT_NEAR(jointVel1, 0.0, tol);
732 }
733
734 // Another two seconds with lower control forces than the friction forces
735 for (int i = 0; i < nSteps; i++)
736 {
737 joint0->setForce(0, -4.9);
738 joint1->setForce(0, -4.9);
739 myWorld->step();
740
741 double jointVel0 = joint0->getVelocity(0);
742 double jointVel1 = joint1->getVelocity(0);
743
744 EXPECT_NEAR(jointVel0, 0.0, tol);
745 EXPECT_NEAR(jointVel1, 0.0, tol);
746 }
747
748 // Another two seconds with higher control forces than the friction forces
749 for (int i = 0; i < nSteps; i++)
750 {
751 joint0->setForce(0, 10.0);
752 joint1->setForce(0, 10.0);
753 myWorld->step();
754
755 double jointVel0 = joint0->getVelocity(0);
756 double jointVel1 = joint1->getVelocity(0);
757
758 EXPECT_GE(std::abs(jointVel0), 0.0);
759 EXPECT_GE(std::abs(jointVel1), 0.0);
760 }
761
762 // Spend 20 sec waiting the joints to stop
763 for (int i = 0; i < nSteps * 10; i++)
764 {
765 myWorld->step();
766 }
767 double jointVel0 = joint0->getVelocity(0);
768 double jointVel1 = joint1->getVelocity(0);
769
770 EXPECT_NEAR(jointVel0, 0.0, tol);
771 EXPECT_NEAR(jointVel1, 0.0, tol);
772
773 // Another two seconds with lower control forces than the friction forces
774 // and expect the joints to stop
775 for (int i = 0; i < nSteps; i++)
776 {
777 joint0->setForce(0, 4.9);
778 joint1->setForce(0, 4.9);
779 myWorld->step();
780
781 double jointVel0 = joint0->getVelocity(0);
782 double jointVel1 = joint1->getVelocity(0);
783
784 EXPECT_NEAR(jointVel0, 0.0, tol);
785 EXPECT_NEAR(jointVel1, 0.0, tol);
786 }
787 }
788
789 //==============================================================================
790 TEST_F(JOINTS, JOINT_COULOMB_FRICTION)
791 {
792 std::array<double, 3> timeSteps;
793 timeSteps[0] = 1e-2;
794 timeSteps[1] = 1e-3;
795 timeSteps[2] = 1e-4;
796
797 for (auto timeStep : timeSteps)
798 testJointCoulombFrictionForce(timeStep);
799 }
800
801 //==============================================================================
802 SkeletonPtr createPendulum(Joint::ActuatorType actType)
803 {
804 using namespace dart::math::suffixes;
805
806 Vector3d dim(1, 1, 1);
807 Vector3d offset(0, 0, 2);
808
809 SkeletonPtr pendulum = createNLinkPendulum(1, dim, DOF_ROLL, offset);
810 EXPECT_NE(pendulum, nullptr);
811
812 pendulum->disableSelfCollisionCheck();
813
814 for (std::size_t i = 0; i < pendulum->getNumBodyNodes(); ++i)
815 {
816 auto bodyNode = pendulum->getBodyNode(i);
817 bodyNode->removeAllShapeNodesWith<CollisionAspect>();
818 }
819
820 // Joint common setting
821 dynamics::Joint* joint = pendulum->getJoint(0);
822 EXPECT_NE(joint, nullptr);
823
824 joint->setActuatorType(actType);
825 joint->setPosition(0, 90.0_deg);
826 joint->setDampingCoefficient(0, 0.0);
827 joint->setSpringStiffness(0, 0.0);
828 joint->setLimitEnforcement(true);
829 joint->setCoulombFriction(0, 0.0);
830
831 return pendulum;
832 }
833
834 //==============================================================================
835 TEST_F(JOINTS, SpringRestPosition)
836 {
837 using namespace math::suffixes;
838
839 auto skel = createPendulum(Joint::ActuatorType::PASSIVE);
840 ASSERT_NE(skel, nullptr);
841
842 auto joint = skel->getRootJoint();
843 ASSERT_NE(joint, nullptr);
844
845 auto world = simulation::World::create();
846 ASSERT_NE(world, nullptr);
847
848 world->addSkeleton(skel);
849 world->setGravity(Eigen::Vector3d::Zero());
850
851 joint->setPosition(0, 0);
852 joint->setRestPosition(0, -1.0_pi);
853 joint->setPositionLowerLimit(0, -0.5_pi);
854 joint->setPositionUpperLimit(0, +0.5_pi);
855 joint->setSpringStiffness(0, 5);
856
857 EXPECT_DOUBLE_EQ(joint->getPosition(0), 0);
858
859 const auto tol = 1e-3;
860
861 // Joint starts from 0 and rotates towards its spring rest position (i.e.,
862 // -pi), but it also should stay within the joint limits
863 // (i.e., [-0.5pi, 0.5pi]).
864 for (auto i = 0u; i < 1000; ++i)
865 {
866 world->step();
867 const auto pos = joint->getPosition(0);
868 EXPECT_GE(pos, joint->getPositionLowerLimit(0) - tol);
869 EXPECT_LE(pos, joint->getPositionUpperLimit(0) + tol);
870 }
871
872 // After a while, the joint should come to rest at its lower position limit.
873 for (auto i = 0u; i < 500; ++i)
874 {
875 world->step();
876 const auto pos = joint->getPosition(0);
877 EXPECT_GE(pos, joint->getPositionLowerLimit(0) - tol);
878 EXPECT_LE(pos, joint->getPositionUpperLimit(0) + tol);
879 EXPECT_NEAR(pos, joint->getPositionLowerLimit(0), tol);
880 }
881 }
882
883 //==============================================================================
884 void testServoMotor()
885 {
886 using namespace dart::math::suffixes;
887
888 std::size_t numPendulums = 7;
889 double timestep = 1e-3;
890 double tol = 1e-9;
891 double posUpperLimit = 90.0_deg;
892 double posLowerLimit = 45.0_deg;
893 double sufficient_force = 1e+5;
894 double insufficient_force = 1e-1;
895
896 // World
897 simulation::WorldPtr world = simulation::World::create();
898 EXPECT_TRUE(world != nullptr);
899
900 world->setGravity(Eigen::Vector3d(0, 0, -9.81));
901 world->setTimeStep(timestep);
902
903 // Each pendulum has servo motor in the joint but also have different
904 // expectations depending on the property values.
905 //
906 // Pendulum0:
907 // - Condition: Zero desired velocity and sufficient servo motor force limits
908 // - Expectation: The desired velocity should be achieved.
909 // Pendulum 1:
910 // - Condition: Nonzero desired velocity and sufficient servo motor force
911 // limits
912 // - Expectation: The desired velocity should be achieved.
913 // Pendulum 2:
914 // - Condition: Nonzero desired velocity and insufficient servo motor force
915 // limits
916 // - Expectation: The desired velocity can be achieve or not. But when it's
917 // not achieved, the servo motor force is reached to the lower
918 // or upper limit.
919 // Pendulum 3:
920 // - Condition: Nonzero desired velocity, finite servo motor force limits,
921 // and position limits
922 // - Expectation: The desired velocity should be achieved unless joint
923 // position is reached to lower or upper position limit.
924 // Pendulum 4:
925 // - Condition: Nonzero desired velocity, infinite servo motor force limits,
926 // and position limits
927 // - Expectation: Same as the Pendulum 3's expectation.
928 // Pendulum 5:
929 // - Condition: Nonzero desired velocity, finite servo motor force limits,
930 // and infinite Coulomb friction
931 // - Expectation: The the pendulum shouldn't move at all due to the infinite
932 // friction.
933 // Pendulum 6:
934 // - Condition: Nonzero desired velocity, infinite servo motor force limits,
935 // and infinite Coulomb friction
936 // - Expectation: The the pendulum shouldn't move at all due to the friction.
937 // TODO(JS): Should a servo motor dominent Coulomb friction in this case?
938
939 std::vector<SkeletonPtr> pendulums(numPendulums);
940 std::vector<JointPtr> joints(numPendulums);
941 for (std::size_t i = 0; i < numPendulums; ++i)
942 {
943 pendulums[i] = createPendulum(Joint::SERVO);
944 joints[i] = pendulums[i]->getJoint(0);
945 }
946
947 joints[0]->setForceUpperLimit(0, sufficient_force);
948 joints[0]->setForceLowerLimit(0, -sufficient_force);
949
950 joints[1]->setForceUpperLimit(0, sufficient_force);
951 joints[1]->setForceLowerLimit(0, -sufficient_force);
952
953 joints[2]->setForceUpperLimit(0, insufficient_force);
954 joints[2]->setForceLowerLimit(0, -insufficient_force);
955
956 joints[3]->setForceUpperLimit(0, sufficient_force);
957 joints[3]->setForceLowerLimit(0, -sufficient_force);
958 joints[3]->setPositionUpperLimit(0, posUpperLimit);
959 joints[3]->setPositionLowerLimit(0, posLowerLimit);
960
961 joints[4]->setForceUpperLimit(0, constantsd::inf());
962 joints[4]->setForceLowerLimit(0, -constantsd::inf());
963 joints[4]->setPositionUpperLimit(0, posUpperLimit);
964 joints[4]->setPositionLowerLimit(0, posLowerLimit);
965
966 joints[5]->setForceUpperLimit(0, sufficient_force);
967 joints[5]->setForceLowerLimit(0, -sufficient_force);
968 joints[5]->setCoulombFriction(0, constantsd::inf());
969
970 joints[6]->setForceUpperLimit(0, constantsd::inf());
971 joints[6]->setForceLowerLimit(0, -constantsd::inf());
972 joints[6]->setCoulombFriction(0, constantsd::inf());
973
974 for (auto pendulum : pendulums)
975 world->addSkeleton(pendulum);
976
977 #ifndef NDEBUG // Debug mode
978 double simTime = 0.2;
979 #else
980 double simTime = 2.0;
981 #endif // ------- Debug mode
982 double timeStep = world->getTimeStep();
983 int nSteps = simTime / timeStep;
984
985 // Two seconds with lower control forces than the friction
986 for (int i = 0; i < nSteps; i++)
987 {
988 const double expected_vel = std::sin(world->getTime());
989
990 joints[0]->setCommand(0, 0.0);
991 joints[1]->setCommand(0, expected_vel);
992 joints[2]->setCommand(0, expected_vel);
993 joints[3]->setCommand(0, expected_vel);
994 joints[4]->setCommand(0, expected_vel);
995 joints[5]->setCommand(0, expected_vel);
996 joints[6]->setCommand(0, expected_vel);
997
998 world->step();
999
1000 std::vector<double> jointVels(numPendulums);
1001 for (std::size_t j = 0; j < numPendulums; ++j)
1002 jointVels[j] = joints[j]->getVelocity(0);
1003
1004 EXPECT_NEAR(jointVels[0], 0.0, tol);
1005 EXPECT_NEAR(jointVels[1], expected_vel, tol);
1006 bool result2 = std::abs(jointVels[2] - expected_vel) < tol
1007 || std::abs(
1008 joints[2]->getConstraintImpulse(0) / timeStep
1009 - insufficient_force)
1010 < tol
1011 || std::abs(
1012 joints[2]->getConstraintImpulse(0) / timeStep
1013 + insufficient_force)
1014 < tol;
1015 EXPECT_TRUE(result2);
1016 EXPECT_LE(
1017 joints[3]->getPosition(0), posUpperLimit + expected_vel * timeStep);
1018 EXPECT_GE(
1019 joints[3]->getPosition(0), posLowerLimit - expected_vel * timeStep);
1020 // EXPECT_LE(joints[4]->getPosition(0),
1021 // posUpperLimit + expected_vel * timeStep);
1022 // EXPECT_GE(joints[4]->getPosition(0),
1023 // posLowerLimit - expected_vel * timeStep);
1024 // TODO(JS): Position limits and servo motor with infinite force limits
1025 // doesn't work together because they compete against each other to achieve
1026 // different joint velocities with their infinit force limits. In this case,
1027 // the position limit constraint should dominent the servo motor constraint.
1028 EXPECT_NEAR(jointVels[5], 0.0, tol * 1e+2);
1029 // EXPECT_NEAR(jointVels[6], 0.0, tol * 1e+2);
1030 // TODO(JS): Servo motor with infinite force limits and infinite Coulomb
1031 // friction doesn't work because they compete against each other to achieve
1032 // different joint velocities with their infinit force limits. In this case,
1033 // the friction constraints should dominent the servo motor constraints.
1034 }
1035 }
1036
1037 //==============================================================================
1038 TEST_F(JOINTS, SERVO_MOTOR)
1039 {
1040 testServoMotor();
1041 }
1042
1043 //==============================================================================
1044 void testMimicJoint()
1045 {
1046 using namespace dart::math::suffixes;
1047
1048 double timestep = 1e-3;
1049 double tol = 1e-9;
1050 double tolPos = 1e-3;
1051 double sufficient_force = 1e+5;
1052
1053 // World
1054 simulation::WorldPtr world = simulation::World::create();
1055 EXPECT_TRUE(world != nullptr);
1056
1057 world->setGravity(Eigen::Vector3d(0, 0, -9.81));
1058 world->setTimeStep(timestep);
1059
1060 Vector3d dim(1, 1, 1);
1061 Vector3d offset(0, 0, 2);
1062
1063 SkeletonPtr pendulum = createNLinkPendulum(2, dim, DOF_ROLL, offset);
1064 EXPECT_NE(pendulum, nullptr);
1065
1066 pendulum->disableSelfCollisionCheck();
1067
1068 for (std::size_t i = 0; i < pendulum->getNumBodyNodes(); ++i)
1069 {
1070 auto bodyNode = pendulum->getBodyNode(i);
1071 bodyNode->removeAllShapeNodesWith<CollisionAspect>();
1072 }
1073
1074 std::vector<JointPtr> joints(2);
1075
1076 for (std::size_t i = 0; i < pendulum->getNumBodyNodes(); ++i)
1077 {
1078 dynamics::Joint* joint = pendulum->getJoint(i);
1079 EXPECT_NE(joint, nullptr);
1080
1081 joint->setActuatorType(Joint::SERVO);
1082 joint->setPosition(0, 90.0_deg);
1083 joint->setDampingCoefficient(0, 0.0);
1084 joint->setSpringStiffness(0, 0.0);
1085 joint->setLimitEnforcement(true);
1086 joint->setCoulombFriction(0, 0.0);
1087
1088 joints[i] = joint;
1089 }
1090
1091 joints[0]->setForceUpperLimit(0, sufficient_force);
1092 joints[0]->setForceLowerLimit(0, -sufficient_force);
1093
1094 joints[1]->setForceUpperLimit(0, sufficient_force);
1095 joints[1]->setForceLowerLimit(0, -sufficient_force);
1096
1097 // Second joint mimics the first one
1098 joints[1]->setActuatorType(Joint::MIMIC);
1099 joints[1]->setMimicJoint(joints[0], 1., 0.);
1100
1101 world->addSkeleton(pendulum);
1102
1103 #ifndef NDEBUG // Debug mode
1104 double simTime = 0.2;
1105 #else
1106 double simTime = 2.0;
1107 #endif // ------- Debug mode
1108 double timeStep = world->getTimeStep();
1109 int nSteps = simTime / timeStep;
1110
1111 // Two seconds with lower control forces than the friction
1112 for (int i = 0; i < nSteps; i++)
1113 {
1114 const double expected_vel = std::sin(world->getTime());
1115
1116 joints[0]->setCommand(0, expected_vel);
1117
1118 world->step();
1119
1120 // Check if the first joint achieved the velocity at each time-step
1121 EXPECT_NEAR(joints[0]->getVelocity(0), expected_vel, tol);
1122
1123 // Check if the mimic joint follows the reference joint
1124 EXPECT_NEAR(joints[0]->getPosition(0), joints[1]->getPosition(0), tolPos);
1125 }
1126
1127 // In the end, check once more if the mimic joint followed the reference joint
1128 EXPECT_NEAR(joints[0]->getPosition(0), joints[1]->getPosition(0), tolPos);
1129 }
1130
1131 //==============================================================================
1132 TEST_F(JOINTS, MIMIC_JOINT)
1133 {
1134 testMimicJoint();
1135 }
1136
1137 //==============================================================================
1138 TEST_F(JOINTS, JOINT_COULOMB_FRICTION_AND_POSITION_LIMIT)
1139 {
1140 const double timeStep = 1e-3;
1141 const double tol = 1e-2;
1142
1143 simulation::WorldPtr myWorld = utils::SkelParser::readWorld(
1144 "dart://sample/skel/test/joint_friction_test.skel");
1145 EXPECT_TRUE(myWorld != nullptr);
1146
1147 myWorld->setGravity(Eigen::Vector3d(0.0, 0.0, 0.0));
1148 myWorld->setTimeStep(timeStep);
1149
1150 dynamics::SkeletonPtr pendulum = myWorld->getSkeleton("double_pendulum");
1151 EXPECT_TRUE(pendulum != nullptr);
1152 pendulum->disableSelfCollisionCheck();
1153
1154 dynamics::Joint* joint0 = pendulum->getJoint("joint0");
1155 dynamics::Joint* joint1 = pendulum->getJoint("joint1");
1156
1157 EXPECT_TRUE(joint0 != nullptr);
1158 EXPECT_TRUE(joint1 != nullptr);
1159
1160 double frictionForce = 5.0;
1161
1162 joint0->setLimitEnforcement(true);
1163 joint1->setLimitEnforcement(true);
1164
1165 const double ll = -constantsd::pi() / 12.0; // -15 degree
1166 const double ul = +constantsd::pi() / 12.0; // +15 degree
1167
1168 std::size_t dof0 = joint0->getNumDofs();
1169 for (std::size_t i = 0; i < dof0; ++i)
1170 {
1171 joint0->setPosition(i, 0.0);
1172 joint0->setPosition(i, 0.0);
1173 joint0->setPositionLowerLimit(i, ll);
1174 joint0->setPositionUpperLimit(i, ul);
1175 }
1176
1177 std::size_t dof1 = joint1->getNumDofs();
1178 for (std::size_t i = 0; i < dof1; ++i)
1179 {
1180 joint1->setPosition(i, 0.0);
1181 joint1->setPosition(i, 0.0);
1182 joint1->setPositionLowerLimit(i, ll);
1183 joint1->setPositionUpperLimit(i, ul);
1184 }
1185
1186 joint0->setCoulombFriction(0, frictionForce);
1187 joint1->setCoulombFriction(0, frictionForce);
1188
1189 EXPECT_EQ(joint0->getCoulombFriction(0), frictionForce);
1190 EXPECT_EQ(joint1->getCoulombFriction(0), frictionForce);
1191
1192 #ifndef NDEBUG // Debug mode
1193 double simTime = 0.2;
1194 #else
1195 double simTime = 2.0;
1196 #endif // ------- Debug mode
1197 int nSteps = simTime / timeStep;
1198
1199 // First two seconds rotating in positive direction with higher control forces
1200 // than the friction forces
1201 for (int i = 0; i < nSteps; i++)
1202 {
1203 joint0->setForce(0, 100.0);
1204 joint1->setForce(0, 100.0);
1205 myWorld->step();
1206
1207 double jointPos0 = joint0->getPosition(0);
1208 double jointPos1 = joint1->getPosition(0);
1209
1210 double jointVel0 = joint0->getVelocity(0);
1211 double jointVel1 = joint1->getVelocity(0);
1212
1213 EXPECT_GE(std::abs(jointVel0), 0.0);
1214 EXPECT_GE(std::abs(jointVel1), 0.0);
1215
1216 EXPECT_GE(jointPos0, ll - tol);
1217 EXPECT_LE(jointPos0, ul + tol);
1218
1219 EXPECT_GE(jointPos1, ll - tol);
1220 EXPECT_LE(jointPos1, ul + tol);
1221 }
1222
1223 // Another two seconds rotating in negative direction with higher control
1224 // forces than the friction forces
1225 for (int i = 0; i < nSteps; i++)
1226 {
1227 joint0->setForce(0, -100.0);
1228 joint1->setForce(0, -100.0);
1229 myWorld->step();
1230
1231 double jointPos0 = joint0->getPosition(0);
1232 double jointPos1 = joint1->getPosition(0);
1233
1234 double jointVel0 = joint0->getVelocity(0);
1235 double jointVel1 = joint1->getVelocity(0);
1236
1237 EXPECT_GE(std::abs(jointVel0), 0.0);
1238 EXPECT_GE(std::abs(jointVel1), 0.0);
1239
1240 EXPECT_GE(jointPos0, ll - tol);
1241 EXPECT_LE(jointPos0, ul + tol);
1242
1243 EXPECT_GE(jointPos1, ll - tol);
1244 EXPECT_LE(jointPos1, ul + tol);
1245 }
1246 }
1247
1248 //==============================================================================
1249 template <int N>
1250 Eigen::Matrix<double, N, 1> random_vec(double limit = 100)
1251 {
1252 Eigen::Matrix<double, N, 1> v;
1253 for (std::size_t i = 0; i < N; ++i)
1254 v[i] = math::Random::uniform(-std::abs(limit), std::abs(limit));
1255 return v;
1256 }
1257
1258 //==============================================================================
1259 Eigen::Isometry3d random_transform(
1260 double translation_limit = 100,
1261 double rotation_limit = 2 * dart::math::constantsd::pi())
1262 {
1263 Eigen::Vector3d r = random_vec<3>(translation_limit);
1264 Eigen::Vector3d theta = random_vec<3>(rotation_limit);
1265
1266 Eigen::Isometry3d tf;
1267 tf.setIdentity();
1268 tf.translate(r);
1269
1270 if (theta.norm() > 0)
1271 tf.rotate(Eigen::AngleAxisd(theta.norm(), theta.normalized()));
1272
1273 return tf;
1274 }
1275
1276 //==============================================================================
1277 Eigen::Isometry3d predict_joint_transform(
1278 Joint* joint, const Eigen::Isometry3d& joint_tf)
1279 {
1280 return joint->getTransformFromParentBodyNode() * joint_tf
1281 * joint->getTransformFromChildBodyNode().inverse();
1282 }
1283
1284 //==============================================================================
1285 Eigen::Isometry3d get_relative_transform(BodyNode* bn, BodyNode* relativeTo)
1286 {
1287 return relativeTo->getTransform().inverse() * bn->getTransform();
1288 }
1289
1290 //==============================================================================
1291 TEST_F(JOINTS, CONVENIENCE_FUNCTIONS)
1292 {
1293 SkeletonPtr skel = Skeleton::create();
1294
1295 std::pair<Joint*, BodyNode*> pair;
1296
1297 pair = skel->createJointAndBodyNodePair<WeldJoint>();
1298 BodyNode* root = pair.second;
1299
1300 // -- set up the FreeJoint
1301 std::pair<FreeJoint*, BodyNode*> freepair
1302 = root->createChildJointAndBodyNodePair<FreeJoint>();
1303 FreeJoint* freejoint = freepair.first;
1304 BodyNode* freejoint_bn = freepair.second;
1305
1306 freejoint->setTransformFromParentBodyNode(random_transform());
1307 freejoint->setTransformFromChildBodyNode(random_transform());
1308
1309 // -- set up the EulerJoint
1310 std::pair<EulerJoint*, BodyNode*> eulerpair
1311 = root->createChildJointAndBodyNodePair<EulerJoint>();
1312 EulerJoint* eulerjoint = eulerpair.first;
1313 BodyNode* eulerjoint_bn = eulerpair.second;
1314
1315 eulerjoint->setTransformFromParentBodyNode(random_transform());
1316 eulerjoint->setTransformFromChildBodyNode(random_transform());
1317
1318 // -- set up the BallJoint
1319 std::pair<BallJoint*, BodyNode*> ballpair
1320 = root->createChildJointAndBodyNodePair<BallJoint>();
1321 BallJoint* balljoint = ballpair.first;
1322 BodyNode* balljoint_bn = ballpair.second;
1323
1324 balljoint->setTransformFromParentBodyNode(random_transform());
1325 balljoint->setTransformFromChildBodyNode(random_transform());
1326
1327 // Test a hundred times
1328 for (std::size_t n = 0; n < 100; ++n)
1329 {
1330 // -- convert transforms to positions and then positions back to transforms
1331 Eigen::Isometry3d desired_freejoint_tf = random_transform();
1332 freejoint->setPositions(
1333 FreeJoint::convertToPositions(desired_freejoint_tf));
1334 Eigen::Isometry3d actual_freejoint_tf
1335 = FreeJoint::convertToTransform(freejoint->getPositions());
1336
1337 Eigen::Isometry3d desired_eulerjoint_tf = random_transform();
1338 desired_eulerjoint_tf.translation() = Eigen::Vector3d::Zero();
1339 eulerjoint->setPositions(
1340 eulerjoint->convertToPositions(desired_eulerjoint_tf.linear()));
1341 Eigen::Isometry3d actual_eulerjoint_tf
1342 = eulerjoint->convertToTransform(eulerjoint->getPositions());
1343
1344 Eigen::Isometry3d desired_balljoint_tf = random_transform();
1345 desired_balljoint_tf.translation() = Eigen::Vector3d::Zero();
1346 balljoint->setPositions(
1347 BallJoint::convertToPositions(desired_balljoint_tf.linear()));
1348 Eigen::Isometry3d actual_balljoint_tf
1349 = BallJoint::convertToTransform(balljoint->getPositions());
1350
1351 // -- collect everything so we can cycle through the tests
1352 std::vector<Joint*> joints;
1353 std::vector<BodyNode*> bns;
1354 common::aligned_vector<Eigen::Isometry3d> desired_tfs;
1355 common::aligned_vector<Eigen::Isometry3d> actual_tfs;
1356
1357 joints.push_back(freejoint);
1358 bns.push_back(freejoint_bn);
1359 desired_tfs.push_back(desired_freejoint_tf);
1360 actual_tfs.push_back(actual_freejoint_tf);
1361
1362 joints.push_back(eulerjoint);
1363 bns.push_back(eulerjoint_bn);
1364 desired_tfs.push_back(desired_eulerjoint_tf);
1365 actual_tfs.push_back(actual_eulerjoint_tf);
1366
1367 joints.push_back(balljoint);
1368 bns.push_back(balljoint_bn);
1369 desired_tfs.push_back(desired_balljoint_tf);
1370 actual_tfs.push_back(actual_balljoint_tf);
1371
1372 for (std::size_t i = 0; i < joints.size(); ++i)
1373 {
1374 Joint* joint = joints[i];
1375 BodyNode* bn = bns[i];
1376 Eigen::Isometry3d tf = desired_tfs[i];
1377
1378 bool check_transform_conversion = equals(
1379 predict_joint_transform(joint, tf).matrix(),
1380 get_relative_transform(bn, bn->getParentBodyNode()).matrix());
1381 EXPECT_TRUE(check_transform_conversion);
1382
1383 if (!check_transform_conversion)
1384 {
1385 std::cout << "[" << joint->getName() << " Failed]\n";
1386 std::cout
1387 << "Predicted:\n"
1388 << predict_joint_transform(joint, tf).matrix() << "\n\nActual:\n"
1389 << get_relative_transform(bn, bn->getParentBodyNode()).matrix()
1390 << "\n\n";
1391 }
1392
1393 bool check_full_cycle
1394 = equals(desired_tfs[i].matrix(), actual_tfs[i].matrix());
1395 EXPECT_TRUE(check_full_cycle);
1396
1397 if (!check_full_cycle)
1398 {
1399 std::cout << "[" << joint->getName() << " Failed]\n";
1400 std::cout << "Desired:\n"
1401 << desired_tfs[i].matrix() << "\n\nActual:\n"
1402 << actual_tfs[i].matrix() << "\n\n";
1403 }
1404 }
1405 }
1406 }
1407
1408 //==============================================================================
1409 TEST_F(JOINTS, FREE_JOINT_RELATIVE_TRANSFORM_VELOCITY_ACCELERATION)
1410 {
1411 const std::size_t numTests = 50;
1412
1413 // Generate random reference frames
1414 randomizeRefFrames();
1415 auto refFrames = getFrames();
1416
1417 // Generate random relative frames
1418 randomizeRefFrames();
1419 auto relFrames = getFrames();
1420
1421 //-- Build a skeleton that contains two FreeJoints and two BodyNodes
1422 SkeletonPtr skel = Skeleton::create();
1423
1424 auto pair = skel->createJointAndBodyNodePair<FreeJoint>();
1425 FreeJoint* rootJoint = pair.first;
1426 BodyNode* rootBodyNode = pair.second;
1427 rootJoint->setRelativeTransform(random_transform());
1428 rootJoint->setRelativeSpatialVelocity(random_vec<6>());
1429 rootJoint->setRelativeSpatialAcceleration(random_vec<6>());
1430 rootJoint->setTransformFromParentBodyNode(random_transform());
1431 rootJoint->setTransformFromChildBodyNode(random_transform());
1432
1433 pair = rootBodyNode->createChildJointAndBodyNodePair<FreeJoint>();
1434 FreeJoint* joint1 = pair.first;
1435 BodyNode* bodyNode1 = pair.second;
1436 joint1->setTransformFromParentBodyNode(random_transform());
1437 joint1->setTransformFromChildBodyNode(random_transform());
1438
1439 //-- Actual terms
1440 Eigen::Isometry3d actualTf;
1441 Eigen::Vector6d actualVel;
1442 Eigen::Vector6d actualAcc;
1443
1444 Eigen::Vector3d actualLinVel;
1445 Eigen::Vector3d actualAngVel;
1446 Eigen::Vector3d actualLinAcc;
1447 Eigen::Vector3d actualAngAcc;
1448
1449 Eigen::Vector3d oldLinVel;
1450 Eigen::Vector3d oldAngVel;
1451 Eigen::Vector3d oldLinAcc;
1452 Eigen::Vector3d oldAngAcc;
1453
1454 //-- Test
1455 for (std::size_t i = 0; i < numTests; ++i)
1456 {
1457 const Eigen::Isometry3d desiredTf = random_transform();
1458 const Eigen::Vector6d desiredVel = random_vec<6>();
1459 const Eigen::Vector6d desiredAcc = random_vec<6>();
1460 const Eigen::Vector3d desiredLinVel = random_vec<3>();
1461 const Eigen::Vector3d desiredAngVel = random_vec<3>();
1462 const Eigen::Vector3d desiredLinAcc = random_vec<3>();
1463 const Eigen::Vector3d desiredAngAcc = random_vec<3>();
1464
1465 //-- Relative transformation
1466
1467 joint1->setRelativeTransform(desiredTf);
1468 actualTf = bodyNode1->getTransform(bodyNode1->getParentBodyNode());
1469 EXPECT_TRUE(equals(desiredTf.matrix(), actualTf.matrix()));
1470
1471 for (auto relativeTo : relFrames)
1472 {
1473 joint1->setTransform(desiredTf, relativeTo);
1474 actualTf = bodyNode1->getTransform(relativeTo);
1475 EXPECT_TRUE(equals(desiredTf.matrix(), actualTf.matrix()));
1476 }
1477
1478 //-- Relative spatial velocity
1479
1480 joint1->setRelativeSpatialVelocity(desiredVel);
1481 actualVel = bodyNode1->getSpatialVelocity(
1482 bodyNode1->getParentBodyNode(), bodyNode1);
1483
1484 EXPECT_TRUE(equals(desiredVel, actualVel));
1485
1486 for (auto relativeTo : relFrames)
1487 {
1488 for (auto inCoordinatesOf : refFrames)
1489 {
1490 joint1->setSpatialVelocity(desiredVel, relativeTo, inCoordinatesOf);
1491 actualVel = bodyNode1->getSpatialVelocity(relativeTo, inCoordinatesOf);
1492
1493 EXPECT_TRUE(equals(desiredVel, actualVel));
1494 }
1495 }
1496
1497 //-- Relative classic linear velocity
1498
1499 for (auto relativeTo : relFrames)
1500 {
1501 for (auto inCoordinatesOf : refFrames)
1502 {
1503 joint1->setSpatialVelocity(desiredVel, relativeTo, inCoordinatesOf);
1504 oldAngVel = bodyNode1->getAngularVelocity(relativeTo, inCoordinatesOf);
1505 joint1->setLinearVelocity(desiredLinVel, relativeTo, inCoordinatesOf);
1506
1507 actualLinVel
1508 = bodyNode1->getLinearVelocity(relativeTo, inCoordinatesOf);
1509 actualAngVel
1510 = bodyNode1->getAngularVelocity(relativeTo, inCoordinatesOf);
1511
1512 EXPECT_TRUE(equals(desiredLinVel, actualLinVel));
1513 EXPECT_TRUE(equals(oldAngVel, actualAngVel));
1514 }
1515 }
1516
1517 //-- Relative classic angular velocity
1518
1519 for (auto relativeTo : relFrames)
1520 {
1521 for (auto inCoordinatesOf : refFrames)
1522 {
1523 joint1->setSpatialVelocity(desiredVel, relativeTo, inCoordinatesOf);
1524 oldLinVel = bodyNode1->getLinearVelocity(relativeTo, inCoordinatesOf);
1525 joint1->setAngularVelocity(desiredAngVel, relativeTo, inCoordinatesOf);
1526
1527 actualLinVel
1528 = bodyNode1->getLinearVelocity(relativeTo, inCoordinatesOf);
1529 actualAngVel
1530 = bodyNode1->getAngularVelocity(relativeTo, inCoordinatesOf);
1531
1532 EXPECT_TRUE(equals(oldLinVel, actualLinVel));
1533 EXPECT_TRUE(equals(desiredAngVel, actualAngVel));
1534 }
1535 }
1536
1537 //-- Relative spatial acceleration
1538
1539 joint1->setRelativeSpatialAcceleration(desiredAcc);
1540 actualAcc = bodyNode1->getSpatialAcceleration(
1541 bodyNode1->getParentBodyNode(), bodyNode1);
1542
1543 EXPECT_TRUE(equals(desiredAcc, actualAcc));
1544
1545 for (auto relativeTo : relFrames)
1546 {
1547 for (auto inCoordinatesOf : refFrames)
1548 {
1549 joint1->setSpatialAcceleration(desiredAcc, relativeTo, inCoordinatesOf);
1550 actualAcc
1551 = bodyNode1->getSpatialAcceleration(relativeTo, inCoordinatesOf);
1552
1553 EXPECT_TRUE(equals(desiredAcc, actualAcc));
1554 }
1555 }
1556
1557 //-- Relative transform, spatial velocity, and spatial acceleration
1558 for (auto relativeTo : relFrames)
1559 {
1560 for (auto inCoordinatesOf : refFrames)
1561 {
1562 joint1->setSpatialMotion(
1563 &desiredTf,
1564 relativeTo,
1565 &desiredVel,
1566 relativeTo,
1567 inCoordinatesOf,
1568 &desiredAcc,
1569 relativeTo,
1570 inCoordinatesOf);
1571 actualTf = bodyNode1->getTransform(relativeTo);
1572 actualVel = bodyNode1->getSpatialVelocity(relativeTo, inCoordinatesOf);
1573 actualAcc
1574 = bodyNode1->getSpatialAcceleration(relativeTo, inCoordinatesOf);
1575
1576 EXPECT_TRUE(equals(desiredTf.matrix(), actualTf.matrix()));
1577 EXPECT_TRUE(equals(desiredVel, actualVel));
1578 EXPECT_TRUE(equals(desiredAcc, actualAcc));
1579 }
1580 }
1581
1582 //-- Relative classic linear acceleration
1583
1584 for (auto relativeTo : relFrames)
1585 {
1586 for (auto inCoordinatesOf : refFrames)
1587 {
1588 joint1->setSpatialAcceleration(desiredAcc, relativeTo, inCoordinatesOf);
1589 oldAngAcc
1590 = bodyNode1->getAngularAcceleration(relativeTo, inCoordinatesOf);
1591 joint1->setLinearAcceleration(
1592 desiredLinAcc, relativeTo, inCoordinatesOf);
1593
1594 actualLinAcc
1595 = bodyNode1->getLinearAcceleration(relativeTo, inCoordinatesOf);
1596 actualAngAcc
1597 = bodyNode1->getAngularAcceleration(relativeTo, inCoordinatesOf);
1598
1599 EXPECT_TRUE(equals(desiredLinAcc, actualLinAcc));
1600 EXPECT_TRUE(equals(oldAngAcc, actualAngAcc));
1601 }
1602 }
1603
1604 //-- Relative classic angular acceleration
1605
1606 for (auto relativeTo : relFrames)
1607 {
1608 for (auto inCoordinatesOf : refFrames)
1609 {
1610 joint1->setSpatialAcceleration(desiredAcc, relativeTo, inCoordinatesOf);
1611 oldLinAcc
1612 = bodyNode1->getLinearAcceleration(relativeTo, inCoordinatesOf);
1613 joint1->setAngularAcceleration(
1614 desiredAngAcc, relativeTo, inCoordinatesOf);
1615
1616 actualLinAcc
1617 = bodyNode1->getLinearAcceleration(relativeTo, inCoordinatesOf);
1618 actualAngAcc
1619 = bodyNode1->getAngularAcceleration(relativeTo, inCoordinatesOf);
1620
1621 EXPECT_TRUE(equals(oldLinAcc, actualLinAcc));
1622 EXPECT_TRUE(equals(desiredAngAcc, actualAngAcc));
1623 }
1624 }
1625 }
1626 }