-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathAddedMassFeatures_TEST.cc
183 lines (150 loc) · 5.63 KB
/
AddedMassFeatures_TEST.cc
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
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#include <dart/dynamics/BodyNode.hpp>
#include <gz/common/Console.hh>
#include <gz/math/eigen3.hh>
#include <gz/plugin/Loader.hh>
#include <gz/physics/FrameSemantics.hh>
#include <gz/physics/GetEntities.hh>
#include <gz/physics/Joint.hh>
#include <gz/physics/RequestEngine.hh>
#include <gz/physics/RevoluteJoint.hh>
#include <gz/physics/AddedMass.hh>
#include <gz/physics/sdf/ConstructLink.hh>
#include <gz/physics/sdf/ConstructModel.hh>
#include <gz/physics/sdf/ConstructWorld.hh>
#include <gz/physics/dartsim/World.hh>
#include <sdf/Link.hh>
#include <sdf/Root.hh>
#include <sdf/World.hh>
#include <test/Utils.hh>
#include "Worlds.hh"
struct TestFeatureList : gz::physics::FeatureList<
gz::physics::GetEntities,
gz::physics::GetBasicJointState,
gz::physics::SetBasicJointState,
gz::physics::LinkFrameSemantics,
gz::physics::dartsim::RetrieveWorld,
gz::physics::sdf::ConstructSdfLink,
gz::physics::sdf::ConstructSdfModel,
gz::physics::sdf::ConstructSdfWorld,
gz::physics::AddedMass
> { };
using World = gz::physics::World3d<TestFeatureList>;
using WorldPtr = gz::physics::World3dPtr<TestFeatureList>;
using ModelPtr = gz::physics::Model3dPtr<TestFeatureList>;
using LinkPtr = gz::physics::Link3dPtr<TestFeatureList>;
/////////////////////////////////////////////////
auto LoadEngine()
{
gz::plugin::Loader loader;
loader.LoadLib(dartsim_plugin_LIB);
gz::plugin::PluginPtr dartsim =
loader.Instantiate("gz::physics::dartsim::Plugin");
auto engine =
gz::physics::RequestEngine3d<TestFeatureList>::From(dartsim);
return engine;
}
/////////////////////////////////////////////////
WorldPtr LoadWorld(const std::string &_world)
{
auto engine = LoadEngine();
EXPECT_NE(nullptr, engine);
sdf::Root root;
const sdf::Errors &errors = root.Load(_world);
EXPECT_EQ(0u, errors.size());
for (const auto & error : errors) {
std::cout << error << std::endl;
}
EXPECT_EQ(1u, root.WorldCount());
const sdf::World *sdfWorld = root.WorldByIndex(0);
EXPECT_NE(nullptr, sdfWorld);
auto world = engine->ConstructWorld(*sdfWorld);
EXPECT_NE(nullptr, world);
return world;
}
/////////////////////////////////////////////////
TEST(AddedMassFeatures, AddedMass)
{
// Expected spatial inertia matrix. This includes inertia due to the body's
// mass and added mass. Note that the ordering of the matrix is different
// than the one used in SDF.
Eigen::Matrix6d expectedSpatialInertia;
expectedSpatialInertia <<
17, 17, 18, 4, 9, 13,
17, 20, 20, 5, 10, 14,
18, 20, 22, 6, 11, 15,
4, 5, 6, 2, 2, 3,
9, 10, 11, 2, 8, 8,
13, 14, 15, 3, 8, 13;
// Expected spatial inertia matrix. This includes inertia due to the body's
// mass and added mass. Note that the ordering of the matrix is different
// than the one used in SDF.
Eigen::Matrix6d expectedZeroSpatialInertia;
expectedZeroSpatialInertia <<
1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 1;
const auto world = LoadWorld(dartsim::worlds::kAddedMassSdf);
ASSERT_NE(nullptr, world);
auto dartWorld = world->GetDartsimWorld();
ASSERT_NE(nullptr, dartWorld);
ASSERT_EQ(3u, dartWorld->getNumSkeletons());
{
const auto skeleton = dartWorld->getSkeleton("body_no_added_mass");
ASSERT_NE(skeleton, nullptr);
ASSERT_EQ(1u, skeleton->getNumBodyNodes());
const dart::dynamics::BodyNode *link = skeleton->getBodyNode("link");
ASSERT_NE(link, nullptr);
const Eigen::Matrix6d spatialInertia = link->getSpatialInertia();
ASSERT_TRUE(expectedZeroSpatialInertia.isApprox(spatialInertia));
const auto linkAddedMass =
world->GetModel("body_no_added_mass")->GetLink("link")->GetAddedMass();
ASSERT_TRUE(Eigen::Matrix6d::Zero().isApprox(
gz::math::eigen3::convert(linkAddedMass)));
}
{
const auto skeleton = dartWorld->getSkeleton("body_zero_added_mass");
ASSERT_NE(skeleton, nullptr);
ASSERT_EQ(1u, skeleton->getNumBodyNodes());
const dart::dynamics::BodyNode *link = skeleton->getBodyNode("link");
ASSERT_NE(link, nullptr);
const Eigen::Matrix6d spatialInertia = link->getSpatialInertia();
ASSERT_TRUE(expectedZeroSpatialInertia.isApprox(spatialInertia));
auto linkAddedMass =
world->GetModel("body_zero_added_mass")->GetLink("link")->GetAddedMass();
ASSERT_TRUE(Eigen::Matrix6d::Zero().isApprox(
gz::math::eigen3::convert(linkAddedMass)));
}
{
const auto skeleton = dartWorld->getSkeleton("body_added_mass");
ASSERT_NE(skeleton, nullptr);
ASSERT_EQ(1u, skeleton->getNumBodyNodes());
const dart::dynamics::BodyNode *link = skeleton->getBodyNode("link");
ASSERT_NE(link, nullptr);
const Eigen::Matrix6d spatialInertia = link->getSpatialInertia();
ASSERT_TRUE(expectedSpatialInertia.isApprox(spatialInertia));
auto linkAddedMass =
world->GetModel("body_added_mass")->GetLink("link")->GetAddedMass();
ASSERT_FALSE(Eigen::Matrix6d::Zero().isApprox(
gz::math::eigen3::convert(linkAddedMass)));
}
}