Debian Codegraph @

unittests/integration/test_ContactConstraint.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 <gtest/gtest.h>
34
35 #include "dart/common/common.hpp"
36 #include "dart/constraint/constraint.hpp"
37 #include "dart/dynamics/dynamics.hpp"
38 #include "dart/simulation/World.hpp"
39
40 #include "TestHelpers.hpp"
41
42 using namespace dart;
43
44 //==============================================================================
45 void testContactWithKinematicJoint(
46 const constraint::BoxedLcpSolverPtr& lcpSolver, double tol)
47 {
48 auto world = std::make_shared<simulation::World>();
49 world->setConstraintSolver(
50 std::make_unique<constraint::BoxedLcpConstraintSolver>(lcpSolver));
51
52 auto skeleton1 = dynamics::Skeleton::create("skeleton1");
53 auto pair1 = skeleton1->createJointAndBodyNodePair<dynamics::FreeJoint>();
54 auto bodyNode1 = pair1.second;
55 auto joint1 = pair1.first;
56 joint1->setActuatorType(dynamics::Joint::VELOCITY);
57 auto shape1
58 = std::make_shared<dynamics::BoxShape>(Eigen::Vector3d(1.0, 1.0, 1.0));
59 bodyNode1->createShapeNodeWith<VisualAspect, CollisionAspect, DynamicsAspect>(
60 shape1);
61 skeleton1->setPosition(5, 0.0);
62
63 auto skeleton2 = dynamics::Skeleton::create("skeleton2");
64 auto pair2 = skeleton2->createJointAndBodyNodePair<dynamics::FreeJoint>();
65 auto bodyNode2 = pair2.second;
66 auto joint2 = pair2.first;
67 joint2->setActuatorType(dynamics::Joint::FORCE);
68 auto shape2
69 = std::make_shared<dynamics::BoxShape>(Eigen::Vector3d(0.5, 0.5, 0.5));
70 bodyNode2->createShapeNodeWith<VisualAspect, CollisionAspect, DynamicsAspect>(
71 shape2);
72 skeleton2->setPosition(5, 0.75);
73
74 world->addSkeleton(skeleton1);
75 world->addSkeleton(skeleton2);
76
77 for (auto i = 0u; i < 100; ++i)
78 {
79 skeleton1->setCommand(3, 0.1);
80
81 world->step();
82
83 EXPECT_EQ(bodyNode1->getLinearVelocity()[0], 0.1);
84
85 // Need few steps to settle down
86 if (i > 15)
87 {
88 EXPECT_NEAR(bodyNode2->getLinearVelocity()[0], 0.1, tol);
89 }
90 }
91 }
92
93 //==============================================================================
94 TEST(ContactConstraint, ContactWithKinematicJoint)
95 {
96 testContactWithKinematicJoint(
97 std::make_shared<constraint::DantzigBoxedLcpSolver>(), 1e-6);
98
99 #ifdef DART_ARCH_32BITS
100 testContactWithKinematicJoint(
101 std::make_shared<constraint::PgsBoxedLcpSolver>(), 1e-3);
102 #else
103 testContactWithKinematicJoint(
104 std::make_shared<constraint::PgsBoxedLcpSolver>(), 1e-4);
105 #endif
106 }