-
Notifications
You must be signed in to change notification settings - Fork 68
/
ModelUnitTest.cpp
466 lines (372 loc) · 16 KB
/
ModelUnitTest.cpp
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT)
// SPDX-License-Identifier: BSD-3-Clause
#include <iDynTree/FixedJoint.h>
#include <iDynTree/RevoluteJoint.h>
#include <iDynTree/PrismaticJoint.h>
#include <iDynTree/Model.h>
#include <iDynTree/ModelTestUtils.h>
#include <iDynTree/Traversal.h>
#include <iDynTree/TestUtils.h>
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
// For modelTransformsers testing
#include <iDynTree/ModelTransformers.h>
#include <iDynTree/ForwardKinematics.h>
#include <iDynTree/Dynamics.h>
#include <iDynTree/FreeFloatingState.h>
#include <iDynTree/SubModel.h>
using namespace iDynTree;
void createCopyAndDestroy(const Model & model)
{
Model * p_model = new Model(model);
ASSERT_EQUAL_DOUBLE(p_model->getNrOfLinks(),model.getNrOfLinks());
ASSERT_EQUAL_DOUBLE(p_model->getNrOfJoints(),model.getNrOfJoints());
ASSERT_EQUAL_DOUBLE(p_model->getNrOfFrames(),model.getNrOfFrames());
*p_model = model;
ASSERT_EQUAL_DOUBLE(p_model->getNrOfLinks(),model.getNrOfLinks());
ASSERT_EQUAL_DOUBLE(p_model->getNrOfJoints(),model.getNrOfJoints());
ASSERT_EQUAL_DOUBLE(p_model->getNrOfFrames(),model.getNrOfFrames());
delete p_model;
}
void checkComputeTraversal(const Model & model)
{
Traversal traversal;
bool ok = model.computeFullTreeTraversal(traversal);
ASSERT_EQUAL_DOUBLE(ok,true);
ASSERT_EQUAL_DOUBLE(traversal.getNrOfVisitedLinks(),model.getNrOfLinks());
}
void checkNeighborSanity(const Model & model, bool verbose)
{
for(size_t link =0; link < model.getNrOfLinks(); link++ )
{
if( verbose )
{
std::cout << "Link " << model.getLinkName(link) << " has "
<< model.getNrOfNeighbors(link) << " neeighbors" << std::endl;
}
for(unsigned int neigh_i = 0; neigh_i < model.getNrOfNeighbors(link); neigh_i++ )
{
LinkIndex neighIndex = model.getNeighbor(link,neigh_i).neighborLink;
std::string neighName = model.getLinkName(neighIndex);
if( verbose )
{
std::cout << "neighbor " << neigh_i << " is " << neighName << std::endl;
}
}
}
}
bool isStringInVector(const std::string & str,
const std::vector<std::string> & vec)
{
return std::find(vec.begin(), vec.end(), str) != vec.end();
}
void getRandomSubsetOfJoints(const Model & model,
size_t nrOfJointsInSubset,
std::vector<std::string>& subsetOfJoints)
{
while( subsetOfJoints.size() < nrOfJointsInSubset )
{
JointIndex randomJoint = (JointIndex) getRandomInteger(0,model.getNrOfJoints()-1);
std::string randomJointName = model.getJointName(randomJoint);
// If the random added joint is not in the vector, add it
if( !isStringInVector(randomJointName,subsetOfJoints) )
{
subsetOfJoints.push_back(randomJointName);
}
}
}
void getRandomJointPositonsForJointsNotInReducedModels(const Model & fullModel,
const std::vector<std::string>& subsetOfJointsInReducedModel,
std::unordered_map<std::string, double>& removedJointPositions,
FreeFloatingPos& fullModelPos)
{
for(JointIndex jntIndex = 0; jntIndex < fullModel.getNrOfJoints(); jntIndex++)
{
// Check if joint is in reduced model
std::string jointName = fullModel.getJointName(jntIndex);
// Only set non-zero position if the DOF size is exactly 1
if (!isStringInVector(jointName, subsetOfJointsInReducedModel))
{
if (fullModel.getJoint(jntIndex)->getNrOfDOFs() == 1)
{
double jointConf = iDynTree::getRandomDouble();
removedJointPositions[jointName] = jointConf;
fullModelPos.jointPos()(fullModel.getJoint(jntIndex)->getPosCoordsOffset()) = jointConf;
}
}
}
}
class RNEAHelperClass
{
private:
Model model;
LinkVelArray linkVels;
LinkAccArray linkAccs;
LinkNetExternalWrenches linkExtF;
LinkInternalWrenches linkIntF;
Traversal traversal;
public:
RNEAHelperClass(const Model& _model): model(_model),
linkVels(_model),
linkAccs(_model),
linkExtF(_model),
linkIntF(_model)
{
model.computeFullTreeTraversal(traversal);
}
bool runRNEA(const FreeFloatingPos & pos, const FreeFloatingVel & vel,
const FreeFloatingAcc & acc, FreeFloatingGeneralizedTorques & trqs)
{
bool ok = true;
ok = ok && ForwardVelAccKinematics(model,traversal,
pos,vel,acc,
linkVels,linkAccs);
ok = ok && RNEADynamicPhase(model,traversal,
pos.jointPos(),linkVels,linkAccs,
linkExtF,linkIntF,trqs);
return ok;
}
};
/**
* Copy a vector of the dofs from the reduced model to a full model.
*
*/
template<typename vectorType>
void copyFromReducedToFull(const vectorType & reducedVector,
vectorType & fullVector,
const Model & reducedModel,
const Model & fullModel)
{
for(JointIndex jntReduced = 0; jntReduced < reducedModel.getNrOfJoints(); jntReduced++ )
{
IJointConstPtr jnt = reducedModel.getJoint(jntReduced);
std::string jointName = reducedModel.getJointName(jntReduced);
if( jnt->getNrOfDOFs() > 0 )
{
IJointConstPtr jntInFullModel = fullModel.getJoint(fullModel.getJointIndex(jointName));
assert(jntInFullModel->getNrOfDOFs() > 0);
assert(fullVector.size() == fullModel.getNrOfPosCoords());
assert(reducedVector.size() == reducedModel.getNrOfPosCoords());
assert(jntInFullModel->getPosCoordsOffset() < fullVector.size());
assert(jnt->getPosCoordsOffset() < reducedVector.size());
fullVector(jntInFullModel->getPosCoordsOffset()) = reducedVector(jnt->getPosCoordsOffset());
}
}
}
/**
* Copy a vector of the dofs from the reduced model to a full model.
*
*/
template<typename vectorType>
void copyFromFullToReduced( vectorType & reducedVector,
const vectorType & fullVector,
const Model & reducedModel,
const Model & fullModel)
{
for(JointIndex jntReduced = 0; jntReduced < reducedModel.getNrOfJoints(); jntReduced++ )
{
IJointConstPtr jnt = reducedModel.getJoint(jntReduced);
std::string jointName = reducedModel.getJointName(jntReduced);
if( jnt->getNrOfDOFs() > 0 )
{
IJointConstPtr jntInFullModel = fullModel.getJoint(fullModel.getJointIndex(jointName));
reducedVector(jnt->getPosCoordsOffset()) = fullVector(jntInFullModel->getPosCoordsOffset());
}
}
}
/**
* \todo Move outsite in proper ModelTransformersUnitTest file.
*/
void checkReducedModel(const Model & model)
{
// Create a random reduced version of the model
// and check that the RNEA for the full model
// and for the reduced model (with the removed
// joint positions, velocity and accelerations set to 0)
// is giving the same results
for(size_t jnts=0; jnts < model.getNrOfJoints(); jnts += 5)
{
FreeFloatingPos fullPos(model);
std::vector<std::string> jointInReducedModel;
getRandomSubsetOfJoints(model,jnts,jointInReducedModel);
// Get random positions for reduced models
std::unordered_map<std::string, double> removedJointPositions;
getRandomJointPositonsForJointsNotInReducedModels(model, jointInReducedModel, removedJointPositions, fullPos);
Model reducedModel;
bool ok = createReducedModel(model, jointInReducedModel, reducedModel, removedJointPositions);
ASSERT_EQUAL_DOUBLE(ok,1.0);
// Check that the two models have the same number of frames
ASSERT_EQUAL_DOUBLE(reducedModel.getNrOfJoints(),jnts);
ASSERT_EQUAL_DOUBLE(reducedModel.getNrOfJoints(),jointInReducedModel.size());
ASSERT_EQUAL_DOUBLE(model.getNrOfFrames(),reducedModel.getNrOfFrames());
// Run RNEA
RNEAHelperClass fullRNEA(model);
RNEAHelperClass reducedRNEA(reducedModel);
FreeFloatingPos reducedPos(reducedModel);
FreeFloatingVel reducedVel(reducedModel);
FreeFloatingAcc reducedAcc(reducedModel);
FreeFloatingGeneralizedTorques reducedTrqs(reducedModel);
FreeFloatingGeneralizedTorques reducedTrqsCheck(reducedModel);
reducedPos.worldBasePos() = getRandomTransform();
getRandomVector(reducedPos.jointPos());
reducedVel.baseVel() = getRandomTwist();
getRandomVector(reducedVel.jointVel());
reducedAcc.baseAcc() = getRandomTwist();
getRandomVector(reducedAcc.jointAcc());
FreeFloatingVel fullVel(model);
FreeFloatingAcc fullAcc(model);
FreeFloatingGeneralizedTorques fullTrqs(model);
fullPos.worldBasePos() = reducedPos.worldBasePos();
fullVel.baseVel() = reducedVel.baseVel();
fullAcc.baseAcc() = reducedAcc.baseAcc();
copyFromReducedToFull(reducedPos.jointPos(),fullPos.jointPos(),reducedModel,model);
copyFromReducedToFull(reducedVel.jointVel(),fullVel.jointVel(),reducedModel,model);
copyFromReducedToFull(reducedAcc.jointAcc(),fullAcc.jointAcc(),reducedModel,model);
fullRNEA.runRNEA(fullPos,fullVel,fullAcc,fullTrqs);
reducedRNEA.runRNEA(reducedPos,reducedVel,reducedAcc,reducedTrqs);
reducedTrqsCheck.baseWrench() = fullTrqs.baseWrench();
copyFromFullToReduced(reducedTrqsCheck.jointTorques(),fullTrqs.jointTorques(),reducedModel,model);
ASSERT_EQUAL_VECTOR_TOL(reducedTrqs.baseWrench().asVector(),reducedTrqsCheck.baseWrench().asVector(),1e-8);
ASSERT_EQUAL_VECTOR_TOL(reducedTrqs.jointTorques(),reducedTrqsCheck.jointTorques(),1e-8);
}
}
void checkExtractSubModel(const Model & model)
{
for(size_t jnts=1; jnts < model.getNrOfJoints(); jnts += 5)
{
Traversal traversal;
bool ok = model.computeFullTreeTraversal(traversal);
ASSERT_EQUAL_DOUBLE(ok,true);
std::vector<std::string> jointInReducedModel;
getRandomSubsetOfJoints(model,jnts,jointInReducedModel);
iDynTree::SubModelDecomposition subModels;
ok = subModels.splitModelAlongJoints(model,traversal,jointInReducedModel);
ASSERT_EQUAL_DOUBLE(ok,true);
for(int subModelIdx = 0; subModelIdx < subModels.getNrOfSubModels(); subModelIdx++)
{
const Traversal & subModelTraversal = subModels.getTraversal(subModelIdx);
Model reducedModel;
ok = extractSubModel(model, subModelTraversal, reducedModel);
ASSERT_EQUAL_DOUBLE(ok,true);
for (int linkIdx = 0; linkIdx < subModelTraversal.getNrOfVisitedLinks(); linkIdx++)
{
auto linkSubModel = subModelTraversal.getLink(linkIdx);
auto linkModel = model.getLink(model.getLinkIndex(reducedModel.getLinkName(linkIdx)));
ASSERT_EQUAL_MATRIX(linkSubModel->getInertia().asMatrix(), linkModel->getInertia().asMatrix());
}
}
}
}
void checkAll(const Model & model)
{
createCopyAndDestroy(model);
checkNeighborSanity(model,false);
checkComputeTraversal(model);
checkReducedModel(model);
checkExtractSubModel(model);
}
void checkSimpleModel()
{
std::cout << "Checking simple model... " << std::endl;
double rotInertiaData[3*3] = {14.0,0.0,0.0,
0.0,12.0,0.0,
0.0,0.0,10.0};
SpatialInertia inertiaLink0(1.0,Position(100,0,0),RotationalInertia(rotInertiaData,3,3));
Link link0;
link0.setInertia(inertiaLink0);
Link link1(link0);
FixedJoint fixJoint(0,1,Transform(Rotation::Identity(),Position(1,3,4)));
{
Model model;
model.addLink("link0",link0);
model.addLink("link1",link1);
model.addJoint("fixedJoint",&fixJoint);
model.addAdditionalFrameToLink("link0", "frame0_0", iDynTree::Transform::Identity());
model.addAdditionalFrameToLink("link1", "frame1_0", iDynTree::Transform::Identity());
model.addAdditionalFrameToLink("link1", "frame1_1", iDynTree::Transform::Identity());
ASSERT_EQUAL_DOUBLE(model.getNrOfLinks(),2);
ASSERT_EQUAL_DOUBLE(model.getNrOfJoints(),1);
ASSERT_EQUAL_DOUBLE(model.getNrOfNeighbors(0),1);
ASSERT_EQUAL_DOUBLE(model.getNrOfNeighbors(1),1);
ASSERT_EQUAL_DOUBLE(model.getNeighbor(0,0).neighborLink,1);
ASSERT_EQUAL_DOUBLE(model.getNeighbor(1,0).neighborLink,0);
std::vector<FrameIndex> link0_frames;
model.getLinkAdditionalFrames(0, link0_frames);
ASSERT_EQUAL_DOUBLE(link0_frames.size(), 1);
std::vector<FrameIndex> link1_frames;
model.getLinkAdditionalFrames(1, link1_frames);
ASSERT_EQUAL_DOUBLE(link1_frames.size(), 2);
ASSERT_IS_TRUE(link1_frames[0] < link1_frames[1]);
createCopyAndDestroy(model);
checkComputeTraversal(model);
}
}
void checkInsertJointAndLink()
{
std::cout << "Checking InsertJointAndLink... " << std::endl;
double rotInertiaData[3*3] = {14.0,0.0,0.0,
0.0,12.0,0.0,
0.0,0.0,10.0};
SpatialInertia inertiaLink0(1.0,Position(100,0,0),RotationalInertia(rotInertiaData,3,3));
Link link0;
link0.setInertia(inertiaLink0);
Link link1(link0);
FixedJoint fixJoint(0,1,Transform(Rotation::Identity(),Position(1,3,4)));
Link linkToInsert(link0);
FixedJoint newJoint(2,1,Transform(Rotation::Identity(),Position(1,3,4)));
Transform newTransform(Rotation::Identity(),Position(1,3,4));
{
Model model;
model.addLink("link0",link0);
model.addLink("link1",link1);
model.addJoint("fixedJoint",&fixJoint);
std::cerr << "Inserting link named insertedLink and joint named newJoint "<< std::endl;
JointIndex ok= model.insertLinkToExistingJointAndAddJointForDisplacedLink("fixedJoint","link0",newTransform,"newJoint",&newJoint,"insertedLink",linkToInsert);
//std::cerr << "Return value of insert JointAndLink "<<ok << std::endl;
std::cerr <<model.toString()<< std::endl;
if (ok==-1)
{
ASSERT_EQUAL_DOUBLE(ok,1);
}
//std::cout << "Inserted link and joint "<< std::endl;
ASSERT_EQUAL_DOUBLE(model.getNrOfLinks(),3);
ASSERT_EQUAL_DOUBLE(model.getNrOfJoints(),2);
ASSERT_EQUAL_DOUBLE(model.getNrOfNeighbors(0),1);
ASSERT_EQUAL_DOUBLE(model.getNrOfNeighbors(1),1);
ASSERT_EQUAL_DOUBLE(model.getNrOfNeighbors(2),2);
ASSERT_EQUAL_DOUBLE(model.getNeighbor(0,0).neighborLink,2);
ASSERT_EQUAL_DOUBLE(model.getNeighbor(1,0).neighborLink,2);
ASSERT_EQUAL_DOUBLE(model.getNeighbor(2,0).neighborLink,0);
ASSERT_EQUAL_DOUBLE(model.getNeighbor(2,1).neighborLink,1);
}
}
void checkRandomChains()
{
std::cout << "Checking random chains..." << std::endl;
for(int i=2; i <= 50; i += 15 )
{
Model randomModel = getRandomChain(i);
std::cout << "Checking reduced model for random chain of size: " << i << std::endl;
checkAll(randomModel);
}
}
void checkRandomModels()
{
std::cout << "Checking random models..." << std::endl;
for(int i=2; i <= 100; i += 30 )
{
Model randomModel = getRandomModel(i);
std::cout << "Checking reduced model for random model of size: " << i << std::endl;
checkAll(randomModel);
}
}
int main()
{
checkSimpleModel();
checkRandomChains();
checkRandomModels();
checkInsertJointAndLink();
return EXIT_SUCCESS;
}