-
Notifications
You must be signed in to change notification settings - Fork 41
/
LinkFeatures_TEST.cc
348 lines (287 loc) · 12.1 KB
/
LinkFeatures_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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 3.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 <iostream>
#include <ignition/physics/FindFeatures.hh>
#include <ignition/plugin/Loader.hh>
#include <ignition/physics/RequestEngine.hh>
#include <ignition/math/eigen3/Conversions.hh>
// Features
#include <ignition/physics/ForwardStep.hh>
#include <ignition/physics/FrameSemantics.hh>
#include <ignition/physics/GetBoundingBox.hh>
#include <ignition/physics/Link.hh>
#include <ignition/physics/sdf/ConstructWorld.hh>
#include <ignition/physics/sdf/ConstructModel.hh>
#include <ignition/physics/sdf/ConstructLink.hh>
#include <sdf/Root.hh>
#include <sdf/World.hh>
#include "test/Utils.hh"
struct TestFeatureList : ignition::physics::FeatureList<
ignition::physics::AddLinkExternalForceTorque,
ignition::physics::ForwardStep,
ignition::physics::sdf::ConstructSdfWorld,
ignition::physics::sdf::ConstructSdfModel,
ignition::physics::sdf::ConstructSdfLink,
ignition::physics::GetEntities,
ignition::physics::GetLinkBoundingBox,
ignition::physics::GetModelBoundingBox
> { };
using namespace ignition;
using TestEnginePtr = physics::Engine3dPtr<TestFeatureList>;
using TestWorldPtr = physics::World3dPtr<TestFeatureList>;
class LinkFeaturesFixture : public ::testing::Test
{
protected: void SetUp() override
{
ignition::plugin::Loader loader;
loader.LoadLib(dartsim_plugin_LIB);
ignition::plugin::PluginPtr dartsim =
loader.Instantiate("ignition::physics::dartsim::Plugin");
this->engine =
ignition::physics::RequestEngine3d<TestFeatureList>::From(dartsim);
ASSERT_NE(nullptr, this->engine);
}
protected: TestEnginePtr engine;
};
TestWorldPtr LoadWorld(
const TestEnginePtr &_engine,
const std::string &_sdfFile,
const Eigen::Vector3d &_gravity = Eigen::Vector3d{0, 0, -9.8})
{
sdf::Root root;
const sdf::Errors errors = root.Load(_sdfFile);
EXPECT_TRUE(errors.empty());
const sdf::World *sdfWorld = root.WorldByIndex(0);
// Make a copy of the world so we can set the gravity property
// TODO(addisu) Add a world property feature to set gravity instead of this
// hack
sdf::World worldCopy;
worldCopy.Load(sdfWorld->Element());
worldCopy.SetGravity(math::eigen3::convert(_gravity));
return _engine->ConstructWorld(worldCopy);
}
// A predicate-formatter for asserting that two vectors are approximately equal.
class AssertVectorApprox
{
public: explicit AssertVectorApprox(double _tol = 1e-6) : tol(_tol)
{
}
public: ::testing::AssertionResult operator()(
const char *_mExpr, const char *_nExpr, Eigen::Vector3d _m,
Eigen::Vector3d _n)
{
if (ignition::physics::test::Equal(_m, _n, this->tol))
return ::testing::AssertionSuccess();
return ::testing::AssertionFailure()
<< _mExpr << " and " << _nExpr << " ([" << _m.transpose()
<< "] and [" << _n.transpose() << "]"
<< ") are not equal";
}
private: double tol;
};
// Test setting force and torque.
TEST_F(LinkFeaturesFixture, LinkForceTorque)
{
auto world = LoadWorld(this->engine, TEST_WORLD_DIR "/empty.sdf",
Eigen::Vector3d::Zero());
// Add a sphere
sdf::Model modelSDF;
modelSDF.SetName("sphere");
modelSDF.SetRawPose(math::Pose3d(0, 0, 2, 0, 0, IGN_PI));
auto model = world->ConstructModel(modelSDF);
const double mass = 1.0;
math::MassMatrix3d massMatrix{mass, math::Vector3d{0.4, 0.4, 0.4},
math::Vector3d::Zero};
sdf::Link linkSDF;
linkSDF.SetName("sphere_link");
linkSDF.SetInertial({massMatrix, math::Pose3d::Zero});
auto link = model->ConstructLink(linkSDF);
ignition::physics::ForwardStep::Input input;
ignition::physics::ForwardStep::State state;
ignition::physics::ForwardStep::Output output;
AssertVectorApprox vectorPredicate(1e-4);
// Check that link is at rest
{
const auto frameData = link->FrameDataRelativeToWorld();
EXPECT_PRED_FORMAT2(vectorPredicate, Eigen::Vector3d::Zero(),
frameData.linearVelocity);
EXPECT_PRED_FORMAT2(vectorPredicate, Eigen::Vector3d::Zero(),
frameData.angularVelocity);
EXPECT_PRED_FORMAT2(vectorPredicate, Eigen::Vector3d::Zero(),
frameData.linearAcceleration);
EXPECT_PRED_FORMAT2(vectorPredicate, Eigen::Vector3d::Zero(),
frameData.angularAcceleration);
}
// The moment of inertia of the sphere is a multiple of the identity matrix.
// This means that the moi is invariant to rotation so we can use this matrix
// without converting it to the world frame.
Eigen::Matrix3d moi = math::eigen3::convert(massMatrix.Moi());
// Apply forces in the world frame at zero offset
// API: AddExternalForce(relForce, relPosition)
// API: AddExternalTorque(relTorque)
const Eigen::Vector3d cmdForce{1, -1, 0};
link->AddExternalForce(
physics::RelativeForce3d(physics::FrameID::World(), cmdForce),
physics::RelativePosition3d(*link, Eigen::Vector3d::Zero()));
const Eigen::Vector3d cmdTorque{0, 0, 0.1 * IGN_PI};
link->AddExternalTorque(
physics::RelativeTorque3d(physics::FrameID::World(), cmdTorque));
world->Step(output, state, input);
{
const auto frameData = link->FrameDataRelativeToWorld();
EXPECT_PRED_FORMAT2(vectorPredicate, cmdForce,
mass * (frameData.linearAcceleration));
// The moment of inertia of the sphere is a multiple of the identity matrix.
// Hence the gyroscopic coupling terms are zero
EXPECT_PRED_FORMAT2(vectorPredicate, cmdTorque,
moi * frameData.angularAcceleration);
}
world->Step(output, state, input);
// Check that the forces and torques are reset
{
const auto frameData = link->FrameDataRelativeToWorld();
EXPECT_PRED_FORMAT2(vectorPredicate, Eigen::Vector3d::Zero(),
frameData.linearAcceleration);
EXPECT_PRED_FORMAT2(vectorPredicate, Eigen::Vector3d::Zero(),
frameData.angularAcceleration);
}
// Apply forces in the local frame
// The sphere is rotated by pi in the +z so the local x and y axes are in
// the -x and -y of the world frame respectively
const Eigen::Vector3d cmdLocalForce{1, -1, 0};
link->AddExternalForce(
physics::RelativeForce3d(*link, cmdLocalForce),
physics::RelativePosition3d(*link, Eigen::Vector3d::Zero()));
const Eigen::Vector3d cmdLocalTorque{0.1 * IGN_PI, 0, 0};
link->AddExternalTorque(physics::RelativeTorque3d(*link, cmdLocalTorque));
world->Step(output, state, input);
{
const Eigen::Vector3d expectedForce =
Eigen::AngleAxisd(IGN_PI, Eigen::Vector3d::UnitZ()) * cmdLocalForce;
const Eigen::Vector3d expectedTorque =
Eigen::AngleAxisd(IGN_PI, Eigen::Vector3d::UnitZ()) * cmdLocalTorque;
const auto frameData = link->FrameDataRelativeToWorld();
EXPECT_PRED_FORMAT2(vectorPredicate, expectedForce,
mass * (frameData.linearAcceleration));
// The moment of inertia of the sphere is a multiple of the identity matrix.
// Hence the gyroscopic coupling terms are zero
EXPECT_PRED_FORMAT2(vectorPredicate, expectedTorque,
moi * frameData.angularAcceleration);
}
// Test the other AddExternalForce and AddExternalTorque APIs
// API: AddExternalForce(force)
// API: AddExternalTorque(torque)
link->AddExternalForce(cmdForce);
link->AddExternalTorque(cmdTorque);
world->Step(output, state, input);
{
const auto frameData = link->FrameDataRelativeToWorld();
EXPECT_PRED_FORMAT2(vectorPredicate, cmdForce,
mass * (frameData.linearAcceleration));
// The moment of inertia of the sphere is a multiple of the identity matrix.
// Hence the gyroscopic coupling terms are zero
EXPECT_PRED_FORMAT2(vectorPredicate, cmdTorque,
moi * frameData.angularAcceleration);
}
// Apply the force at an offset
// API: AddExternalForce(relForce, relPosition)
Eigen::Vector3d offset{0.1, 0.2, 0.3};
link->AddExternalForce(physics::RelativeForce3d(*link, cmdLocalForce),
physics::RelativePosition3d(*link, offset));
world->Step(output, state, input);
{
const auto frameData = link->FrameDataRelativeToWorld();
EXPECT_PRED_FORMAT2(vectorPredicate,
frameData.pose.linear() * cmdLocalForce,
mass * (frameData.linearAcceleration));
// The moment of inertia of the sphere is a multiple of the identity matrix.
// Hence the gyroscopic coupling terms are zero
EXPECT_PRED_FORMAT2(vectorPredicate,
frameData.pose.linear() * offset.cross(cmdLocalForce),
moi * frameData.angularAcceleration);
}
// Apply force at an offset using the more convenient API
// API: AddExternalForce(force, frame, position)
link->AddExternalForce(cmdLocalForce, *link, offset);
world->Step(output, state, input);
{
const auto frameData = link->FrameDataRelativeToWorld();
EXPECT_PRED_FORMAT2(vectorPredicate,
frameData.pose.linear() * cmdLocalForce,
mass * (frameData.linearAcceleration));
// The moment of inertia of the sphere is a multiple of the identity matrix.
// Hence the gyroscopic coupling terms are zero
EXPECT_PRED_FORMAT2(vectorPredicate,
frameData.pose.linear() * offset.cross(cmdLocalForce),
moi * frameData.angularAcceleration);
}
}
TEST_F(LinkFeaturesFixture, AxisAlignedBoundingBox)
{
auto world =
LoadWorld(this->engine, TEST_WORLD_DIR "test.world");
auto model = world->GetModel("double_pendulum_with_base");
auto baseLink = model->GetLink("base");
auto bbox = baseLink->GetAxisAlignedBoundingBox();
AssertVectorApprox vectorPredicate(1e-4);
EXPECT_PRED_FORMAT2(
vectorPredicate, physics::Vector3d(0.2, -0.8, 0), bbox.min());
EXPECT_PRED_FORMAT2(
vectorPredicate, physics::Vector3d(1.8, 0.8, 2.2), bbox.max());
// test with non-world frame
auto bboxModelFrame = baseLink->GetAxisAlignedBoundingBox(
model->GetFrameID());
EXPECT_PRED_FORMAT2(
vectorPredicate, physics::Vector3d(-0.8, -0.8, 0), bboxModelFrame.min());
EXPECT_PRED_FORMAT2(
vectorPredicate, physics::Vector3d(0.8, 0.8, 2.2), bboxModelFrame.max());
// test with non-world rotated frame
auto upperLink = model->GetLink("upper_link");
auto bboxUpperLinkFrame = baseLink->GetAxisAlignedBoundingBox(
upperLink->GetFrameID());
EXPECT_PRED_FORMAT2(vectorPredicate,
physics::Vector3d(-0.8, -0.1, -0.8), bboxUpperLinkFrame.min());
EXPECT_PRED_FORMAT2(vectorPredicate,
physics::Vector3d(0.8, 2.1, 0.8), bboxUpperLinkFrame.max());
}
TEST_F(LinkFeaturesFixture, ModelAxisAlignedBoundingBox)
{
auto world =
LoadWorld(this->engine, TEST_WORLD_DIR "contact.sdf");
auto model = world->GetModel("sphere");
auto bbox = model->GetAxisAlignedBoundingBox();
AssertVectorApprox vectorPredicate(1e-4);
EXPECT_PRED_FORMAT2(
vectorPredicate, physics::Vector3d(-1, -1, -0.5), bbox.min());
EXPECT_PRED_FORMAT2(
vectorPredicate, physics::Vector3d(2, 2, 1.5), bbox.max());
// test with non-world frame
auto link = model->GetLink("link0");
auto bboxLinkFrame = model->GetAxisAlignedBoundingBox(
link->GetFrameID());
EXPECT_PRED_FORMAT2(
vectorPredicate, physics::Vector3d(-1, -1, -1.0), bboxLinkFrame.min());
EXPECT_PRED_FORMAT2(
vectorPredicate, physics::Vector3d(2, 2, 1.0), bboxLinkFrame.max());
}
/////////////////////////////////////////////////
int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}