1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/*
* Copyright (c) 2011-2021, The DART development contributors
* All rights reserved.
*
* The list of contributors can be found at:
* https://github.com/dartsim/dart/blob/master/LICENSE
*
* This file is provided under the following "BSD-style" License:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <gtest/gtest.h>
#include "dart/dart.hpp"
#include "dart/utils/sdf/SdfParser.hpp"
#include "TestHelpers.hpp"
using namespace dart::math;
using namespace dart::collision;
using namespace dart::dynamics;
using namespace dart::simulation;
using namespace dart::utils;
//==============================================================================
TEST(Issue1193, AngularVelAdd)
{
WorldPtr world = World::create();
EXPECT_TRUE(world != nullptr);
world->setGravity(Eigen::Vector3d(0.0, -10.0, 0.0));
const double dt = 0.001;
world->setTimeStep(dt);
SkeletonPtr sphereSkel = createSphere(0.05, Vector3d(0.0, 1.0, 0.0));
BodyNode* sphere = sphereSkel->getBodyNode(0);
Joint* sphereJoint = sphere->getParentJoint();
sphereJoint->setVelocity(0, 10.0); // ang_x -> Affect lz and ly
sphereJoint->setVelocity(1, 10.0); // ang_y -> No effect
sphereJoint->setVelocity(2, 10.0); // ang_z -> Affect lx and ly
world->addSkeleton(sphereSkel);
Eigen::Vector3d linearVelBefore = sphere->getLinearVelocity();
EXPECT_EQ(Vector3d::Zero(), linearVelBefore);
int maxSteps = 500;
for (int i = 0; i < maxSteps; i++)
{
world->step();
}
Vector3d linearVelAfter = sphere->getLinearVelocity();
double lx = linearVelAfter[0];
double ly = linearVelAfter[1];
double lz = linearVelAfter[2];
EXPECT_NEAR(0.0, lx, 1e-8);
EXPECT_NEAR(maxSteps * world->getGravity().y() * dt, ly, 1e-8);
EXPECT_NEAR(0.0, lz, 1e-8);
}
const double tol = 1e-5;
const int g_iters = 100000;
Eigen::Vector3d computeWorldAngularMomentum(const SkeletonPtr skel)
{
Eigen::Vector3d angMomentum = Eigen::Vector3d::Zero();
for (auto* bn : skel->getBodyNodes())
{
angMomentum += dart::math::dAdInvT(
bn->getWorldTransform(),
bn->getSpatialInertia() * bn->getSpatialVelocity())
.head<3>();
}
return angMomentum;
}
//==============================================================================
TEST(Issue1193, SingleBody)
{
WorldPtr world = World::create();
const double dt = 0.001;
world->setTimeStep(dt);
world->setGravity(Vector3d::Zero());
SkeletonPtr skel = createBox({1, 1, 1});
world->addSkeleton(skel);
auto rootBn = skel->getRootBodyNode();
Eigen::Vector3d initPosition = rootBn->getWorldTransform().translation();
Vector6d vels = compose({0, 25, 0}, {0, 0, -100});
rootBn->getParentJoint()->setVelocities(vels);
rootBn->addExtTorque({0, 50, 0});
for (int i = 0; i < g_iters; ++i)
{
world->step();
}
Eigen::Vector3d positionDiff
= rootBn->getWorldTransform().translation() - initPosition;
EXPECT_NEAR(0.0, positionDiff.x(), tol);
EXPECT_NEAR(0.0, positionDiff.y(), tol);
EXPECT_NEAR(g_iters * dt * vels[5], positionDiff.z(), tol);
}
//==============================================================================
TEST(Issue1193, SingleBodyWithOffDiagonalMoi)
{
WorldPtr world = World::create();
const double dt = 0.001;
world->setTimeStep(dt);
world->setGravity(Vector3d::Zero());
SkeletonPtr skel = createBox({1, 4, 9});
world->addSkeleton(skel);
auto rootBn = skel->getRootBodyNode();
rootBn->setMomentOfInertia(1.1, 1.1, 0.7, 0.1, 0, 0);
Eigen::Vector3d initPosition = rootBn->getWorldTransform().translation();
// Setting this to compose({0, 25, 0}, {0, 0, -100}) causes the test to fail.
Vector6d vels = compose({0, 2.5, 0}, {0, 0, -10.0});
rootBn->getParentJoint()->setVelocities(vels);
rootBn->addExtTorque({0, 50, 0});
//
for (int i = 0; i < g_iters; ++i)
{
world->step();
}
Eigen::Vector3d positionDiff
= rootBn->getWorldTransform().translation() - initPosition;
EXPECT_NEAR(0.0, positionDiff.x(), tol);
EXPECT_NEAR(0.0, positionDiff.y(), tol);
EXPECT_NEAR(g_iters * dt * vels[5], positionDiff.z(), tol);
}
//==============================================================================
TEST(Issue1193, SingleBodyWithJointOffset)
{
WorldPtr world = World::create();
const double dt = 0.001;
world->setTimeStep(dt);
world->setGravity(Vector3d::Zero());
SkeletonPtr skel = createBox({1, 1, 1});
world->addSkeleton(skel);
auto rootBn = skel->getRootBodyNode();
rootBn->setMomentOfInertia(1.1, 1.1, 0.7, 0.1, 0, 0);
Eigen::Vector3d initPosition = rootBn->getWorldTransform().translation();
Vector6d vels = compose({0, 2.5, 0}, {0, 0, -10});
auto* freeJoint = dynamic_cast<FreeJoint*>(rootBn->getParentJoint());
freeJoint->setVelocities(vels);
Eigen::Isometry3d jointPoseInParent = Eigen::Isometry3d::Identity();
jointPoseInParent.translate(Eigen::Vector3d(0.0, 4.0, 0));
freeJoint->setTransformFromParentBodyNode(jointPoseInParent);
rootBn->addExtTorque({0, 50, 0});
for (int i = 0; i < g_iters; ++i)
{
world->step();
}
Eigen::Vector3d positionDiff
= rootBn->getWorldTransform().translation() - initPosition;
EXPECT_NEAR(0.0, positionDiff.x(), tol);
EXPECT_NEAR(4.0, positionDiff.y(), tol);
EXPECT_NEAR(g_iters * dt * vels[5], positionDiff.z(), tol);
}
//==============================================================================
TEST(Issue1193, SingleBodyWithCOMOffset)
{
WorldPtr world = World::create();
const double dt = 0.001;
world->setTimeStep(dt);
world->setGravity(Vector3d::Zero());
SkeletonPtr skel = createBox({1, 1, 1});
world->addSkeleton(skel);
auto rootBn = skel->getRootBodyNode();
rootBn->setMomentOfInertia(1.0 / 6, 1.0 / 6.0, 1.0 / 6.0, 0, 0, 0);
rootBn->setLocalCOM({1, 5, 8});
const Eigen::Vector3d extForce{10, 0, 0};
rootBn->addExtForce(extForce);
world->step();
auto comLinearVel = rootBn->getCOMLinearVelocity();
// TODO (azeey) Improve FreeJoint integration so that a higher tolerance is
// not necessary here.
EXPECT_NEAR(extForce.x() * dt, comLinearVel.x(), tol * 1e2);
}
//==============================================================================
TEST(Issue1193, WithFixedJoint)
{
WorldPtr world = World::create();
const double dt = 0.001;
world->setTimeStep(dt);
world->setGravity(Vector3d::Zero());
SkeletonPtr skel = createBox({1, 1, 1}, {0, 0, 2});
world->addSkeleton(skel);
auto rootBn = skel->getRootBodyNode();
Eigen::Isometry3d comRelPose;
comRelPose = Eigen::Translation3d(0, 0, -2);
auto comFrame = SimpleFrame::createShared(rootBn, "CombinedCOM", comRelPose);
Eigen::Vector3d initComPosition = comFrame->getWorldTransform().translation();
GenericJoint<R1Space>::Properties joint2Prop(std::string("joint2"));
BodyNode::Properties link2Prop(
BodyNode::AspectProperties(std::string("link2")));
link2Prop.mInertia.setMass(1.0);
auto pair = rootBn->createChildJointAndBodyNodePair<WeldJoint>(
WeldJoint::Properties(joint2Prop), link2Prop);
auto* joint = pair.first;
Eigen::Isometry3d jointPoseInParent = Eigen::Isometry3d::Identity();
jointPoseInParent.translate(Eigen::Vector3d(0.0, 0.0, -4));
joint->setTransformFromParentBodyNode(jointPoseInParent);
// TODO (azeey) Improve FreeJoint integration so we can test with larger
// forces. Currently, increasing these forces much more causes the test to
// fail.
rootBn->setExtTorque({0, 2500, 0});
rootBn->setExtForce({0, 0, -1000});
for (int i = 0; i < g_iters; ++i)
{
world->step();
}
Eigen::Vector3d positionDiff
= comFrame->getWorldTransform().translation() - initComPosition;
EXPECT_NEAR(0.0, positionDiff.x(), tol);
EXPECT_NEAR(0.0, positionDiff.y(), tol);
}
//==============================================================================
TEST(Issue1193, WithRevoluteJoint)
{
auto world = SdfParser::readWorld(
"dart://sample/sdf/test/issue1193_revolute_test.sdf");
ASSERT_TRUE(world != nullptr);
const double dt = 0.001;
world->setTimeStep(dt);
SkeletonPtr skel = world->getSkeleton(0);
auto rootBn = skel->getRootBodyNode();
Eigen::Isometry3d comRelPose;
comRelPose = Eigen::Translation3d(0, 0, -2);
auto comFrame = SimpleFrame::createShared(rootBn, "CombinedCOM", comRelPose);
Eigen::Vector3d initComPosition = comFrame->getWorldTransform().translation();
auto* joint = skel->getJoint("revJoint");
ASSERT_NE(nullptr, joint);
for (int i = 0; i < g_iters; ++i)
{
joint->setCommand(0, 0.1);
world->step();
}
Eigen::Vector3d positionDiff
= comFrame->getWorldTransform().translation() - initComPosition;
EXPECT_NEAR(0.0, positionDiff.x(), tol * 5e2);
EXPECT_NEAR(0.0, positionDiff.y(), tol * 5e2);
}
//==============================================================================
TEST(Issue1193, ConservationOfMomentumWithRevoluteJointWithOffset)
{
auto world = SdfParser::readWorld(
"dart://sample/sdf/test/issue1193_revolute_with_offset_test.sdf");
ASSERT_TRUE(world != nullptr);
const double dt = 0.0001;
world->setTimeStep(dt);
world->setGravity(Vector3d::Zero());
SkeletonPtr skel = world->getSkeleton(0);
auto link1 = skel->getBodyNode("link1");
auto* joint = skel->getJoint("revJoint");
ASSERT_NE(nullptr, joint);
link1->getParentJoint()->setVelocities(compose({0, 0.25, 0}, {0, 0, -0.1}));
world->step();
Eigen::Vector3d maxAngMomentumChange = Eigen::Vector3d::Zero();
Eigen::Vector3d h0 = computeWorldAngularMomentum(skel);
for (int i = 1; i < g_iters; ++i)
{
joint->setCommand(0, 0.1);
world->step();
Eigen::Vector3d hNext = computeWorldAngularMomentum(skel);
maxAngMomentumChange
= (hNext - h0).cwiseAbs().cwiseMax(maxAngMomentumChange);
}
EXPECT_NEAR(0.0, maxAngMomentumChange.norm(), tol * 10);
}
|