-
Notifications
You must be signed in to change notification settings - Fork 65
/
KinDynComputations.cpp
3085 lines (2533 loc) · 117 KB
/
KinDynComputations.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT)
// SPDX-License-Identifier: BSD-3-Clause
#include <iDynTree/KinDynComputations.h>
#include <iDynTree/ClassicalAcc.h>
#include <iDynTree/MatrixDynSize.h>
#include <iDynTree/VectorDynSize.h>
#include <iDynTree/Twist.h>
#include <iDynTree/Transform.h>
#include <iDynTree/Rotation.h>
#include <iDynTree/Utils.h>
#include <iDynTree/SpatialAcc.h>
#include <iDynTree/SpatialInertia.h>
#include <iDynTree/Wrench.h>
#include <iDynTree/EigenHelpers.h>
#include <iDynTree/Model.h>
#include <iDynTree/Traversal.h>
#include <iDynTree/FreeFloatingState.h>
#include <iDynTree/LinkState.h>
#include <iDynTree/LinkTraversalsCache.h>
#include <iDynTree/ForwardKinematics.h>
#include <iDynTree/Dynamics.h>
#include <iDynTree/Jacobians.h>
#include <iDynTree/ModelLoader.h>
#include <cassert>
#include <iostream>
#include <fstream>
namespace iDynTree
{
unsigned int DEFAULT_DYNAMICS_COMPUTATION_FRAME_INDEX=10000;
std::string DEFAULT_DYNAMICS_COMPUTATION_FRAME_NAME="iDynTreeDynCompDefaultFrame";
struct KinDynComputations::KinDynComputationsPrivateAttributes
{
private:
// Disable copy constructor and copy operator (move them to = delete when we support C++11)
KinDynComputationsPrivateAttributes(const KinDynComputationsPrivateAttributes&other)
{
assert(false);
}
KinDynComputationsPrivateAttributes& operator=(const Traversal& other)
{
assert(false);
return *this;
}
public:
// True if the the model is valid, false otherwise.
bool m_isModelValid;
// Frame velocity representaiton used by the class
FrameVelocityRepresentation m_frameVelRepr;
// Model used for dynamics computations
iDynTree::Model m_robot_model;
// Traversal (i.e. visit order of the links) used for dynamics computations
// this defines the link that is used as a floating base
iDynTree::Traversal m_traversal;
// Traversal cache
iDynTree::LinkTraversalsCache m_traversalCache;
// State of the model
// Frame where the reference frame is the world one
// and the frame is the base link one
iDynTree::FreeFloatingPos m_pos;
// Velocity of the floating system
// (Warning: this members is designed to work with the low-level
// dynamics algorithms of iDynTree , and so it always contain
// the base velocity expressed with the BODY_FIXED representation.
// If a different convention is used by the class, an approprate
// conversion is performed on set/get .
iDynTree::FreeFloatingVel m_vel;
// Base velocity expressed in the m_frameVelRepr representation
iDynTree::Twist m_baseVelSetViaRobotState;
// 3d gravity vector, expressed with the orientation of the inertial (world) frame
iDynTree::Vector3 m_gravityAcc;
// 3d gravity vector, expressed with the orientation of the base link frame
iDynTree::Vector3 m_gravityAccInBaseLinkFrame;
// Forward kinematics data structure
// true whenever computePosition has been called
// since the last call to setRobotState
bool m_isFwdKinematicsUpdated;
// storage of forward position kinematics results
iDynTree::LinkPositions m_linkPos;
// storage of forward velocity kinematics results
iDynTree::LinkVelArray m_linkVel;
bool m_isRawMassMatrixUpdated;
// storage of the CRBs, used to extract
LinkCompositeRigidBodyInertias m_linkCRBIs;
// container used to reduce the memory allocation when the span version of the
// generalizedBiasForces generalizedGravityForces and generalizedExternalForces is called
FreeFloatingGeneralizedTorques m_generalizedForcesContainer;
// Helper function to get the lockedInertia of the robot from the m_linkCRBIs
const SpatialInertia & getRobotLockedInertia();
// Process a jacobian that expects a body fixed base velocity depending on the selected FrameVelocityRepresentation
void processOnRightSideMatrixExpectingBodyFixedModelVelocity(MatrixView<double> mat);
void processOnLeftSideBodyFixedBaseMomentumJacobian(MatrixView<double> jac);
void processOnLeftSideBodyFixedAvgVelocityJacobian(MatrixView<double> jac);
void processOnLeftSideBodyFixedCentroidalAvgVelocityJacobian(MatrixView<double> jac, const FrameVelocityRepresentation & leftSideRepresentation);
// Transform a wrench from and to body fixed and the used representation
Wrench fromBodyFixedToUsedRepresentation(const Wrench & wrenchInBodyFixed, const Transform & inertial_X_link);
Wrench fromUsedRepresentationToBodyFixed(const Wrench & wrenchInUsedRepresentation, const Transform & inertial_X_link);
// storage of the raw output of the CRBA, used to extract
// the mass matrix and most the centroidal quantities
FreeFloatingMassMatrix m_rawMassMatrix;
// Total linear and angular momentum, expressed in the world frame
SpatialMomentum m_totalMomentum;
// Jacobian buffer, used for intermediate computation in COM jacobian
MatrixDynSize m_jacBuffer;
// Bias accelerations buffers
bool m_areBiasAccelerationsUpdated;
// Storate of base bias acceleration
SpatialAcc m_baseBiasAcc;
// storage of bias accelerations buffers (contains the bias acceleration for the given link in body-fixed)
LinkAccArray m_linkBiasAcc;
/** Base acceleration, in body-fixed representation */
SpatialAcc m_baseAcc;
/** Generalized acceleration, base part in body-fixed representation */
FreeFloatingAcc m_generalizedAccs;
/** Acceleration of each link, in body-fixed representation, i.e. \f$ {}^L \mathrm{v}_{A,L} \f$ */
LinkAccArray m_linkAccs;
// Inverse dynamics buffers
/** Base acceleration, in body-fixed representation */
SpatialAcc m_invDynBaseAcc;
/** Generalized **proper** (real-gravity) acceleration, base part in body-fixed representation */
FreeFloatingAcc m_invDynGeneralizedProperAccs;
/** **Proper** (real-gravity) acceleration of each link, in body-fixed representation */
LinkProperAccArray m_invDynLinkProperAccs;
/** External wrenches, in body-fixed representation */
LinkNetExternalWrenches m_invDynNetExtWrenches;
/** Internal wrenches, in body-fixed representation */
LinkInternalWrenches m_invDynInternalWrenches;
/** Buffer of robot velocity, always set to zero for gravity computations */
FreeFloatingVel m_invDynZeroVel;
/** Buffer of link velocities, always set to zero for gravity computations */
LinkVelArray m_invDynZeroLinkVel;
/** Buffer of link proper accelerations, always set to zero for external forces */
LinkAccArray m_invDynZeroLinkProperAcc;
KinDynComputationsPrivateAttributes()
{
m_isModelValid = false;
m_frameVelRepr = MIXED_REPRESENTATION;
m_isFwdKinematicsUpdated = false;
m_isRawMassMatrixUpdated = false;
m_areBiasAccelerationsUpdated = false;
m_baseVelSetViaRobotState = iDynTree::Twist::Zero();
}
};
typedef Eigen::Matrix<double,3,3,Eigen::RowMajor> Matrix3dRowMajor;
/**
* Function to convert a body fixed acceleration to a mixed acceleration.
*
* TODO refactor in a more general handling of conversion between the three
* derivative of frame velocities (inertial, body-fixed, mixed) and the sensors
* acceleration.
*
* @return The mixed acceleration
*/
Vector6 convertBodyFixedAccelerationToMixedAcceleration(const SpatialAcc & bodyFixedAcc,
const Twist & bodyFixedVel,
const Rotation & inertial_R_body)
{
Vector6 mixedAcceleration;
Eigen::Map<const Eigen::Vector3d> linBodyFixedAcc(bodyFixedAcc.getLinearVec3().data());
Eigen::Map<const Eigen::Vector3d> angBodyFixedAcc(bodyFixedAcc.getAngularVec3().data());
Eigen::Map<const Eigen::Vector3d> linBodyFixedTwist(bodyFixedVel.getLinearVec3().data());
Eigen::Map<const Eigen::Vector3d> angBodyFixedTwist(bodyFixedVel.getAngularVec3().data());
Eigen::Map<Eigen::Vector3d> linMixedAcc(mixedAcceleration.data());
Eigen::Map<Eigen::Vector3d> angMixedAcc(mixedAcceleration.data()+3);
Eigen::Map<const Matrix3dRowMajor> inertial_R_body_eig(inertial_R_body.data());
// First we account for the effect of linear/angular velocity
linMixedAcc = inertial_R_body_eig*(linBodyFixedAcc + angBodyFixedTwist.cross(linBodyFixedTwist));
// Angular acceleration can be copied
angMixedAcc = inertial_R_body_eig*angBodyFixedAcc;
return mixedAcceleration;
}
/**
* Function to convert mixed acceleration to body fixed acceleration
*
* TODO refactor in a more general handling of conversion between the three
* derivative of frame velocities (inertial, body-fixed, mixed) and the sensors
* acceleration.
*
* @return The body fixed acceleration
*/
SpatialAcc convertMixedAccelerationToBodyFixedAcceleration(const Vector6 & mixedAcc,
const Twist & bodyFixedVel,
const Rotation & inertial_R_body)
{
SpatialAcc bodyFixedAcc;
Eigen::Map<const Eigen::Vector3d> linMixedAcc(mixedAcc.data());
Eigen::Map<const Eigen::Vector3d> angMixedAcc(mixedAcc.data()+3);
Eigen::Map<const Eigen::Vector3d> linBodyFixedTwist(bodyFixedVel.getLinearVec3().data());
Eigen::Map<const Eigen::Vector3d> angBodyFixedTwist(bodyFixedVel.getAngularVec3().data());
Eigen::Map<const Matrix3dRowMajor> inertial_R_body_eig(inertial_R_body.data());
Eigen::Map<Eigen::Vector3d> linBodyFixedAcc(bodyFixedAcc.getLinearVec3().data());
Eigen::Map<Eigen::Vector3d> angBodyFixedAcc(bodyFixedAcc.getAngularVec3().data());
linBodyFixedAcc = inertial_R_body_eig.transpose()*linMixedAcc - angBodyFixedTwist.cross(linBodyFixedTwist);
angBodyFixedAcc = inertial_R_body_eig.transpose()*angMixedAcc;
return bodyFixedAcc;
}
/**
* Function to convert inertial acceleration to body acceleration.
*
* TODO refactor in a more general handling of conversion between the three
* derivative of frame velocities (inertial, body-fixed, mixed) and the sensors
* acceleration.
*
* @return The body fixed acceleration
*/
SpatialAcc convertInertialAccelerationToBodyFixedAcceleration(const Vector6 & inertialAcc,
const Transform & inertial_H_body)
{
SpatialAcc inertialAccProperForm;
fromEigen(inertialAccProperForm,toEigen(inertialAcc));
return inertial_H_body.inverse()*inertialAccProperForm;
}
KinDynComputations::KinDynComputations():
pimpl(new KinDynComputationsPrivateAttributes)
{
}
KinDynComputations::KinDynComputations(const KinDynComputations & other)
{
// copyng the class is disabled until we get rid of the legacy implementation
assert(false);
}
KinDynComputations& KinDynComputations::operator=(const KinDynComputations& other)
{
/*
if(this != &other)
{
*pimpl = *(other.pimpl);
}
return *this;
*/
// copyng the class is disable until we get rid of the legacy implementation
assert(false);
return *this;
}
KinDynComputations::~KinDynComputations()
{
delete this->pimpl;
}
//////////////////////////////////////////////////////////////////////////////
////// Private Methods
//////////////////////////////////////////////////////////////////////////////
void KinDynComputations::invalidateCache()
{
this->pimpl->m_isFwdKinematicsUpdated = false;
this->pimpl->m_isRawMassMatrixUpdated = false;
this->pimpl->m_areBiasAccelerationsUpdated = false;
}
void KinDynComputations::resizeInternalDataStructures()
{
assert(this->pimpl->m_isModelValid);
this->pimpl->m_pos.resize(this->pimpl->m_robot_model);
this->pimpl->m_vel.resize(this->pimpl->m_robot_model);
this->pimpl->m_linkPos.resize(this->pimpl->m_robot_model);
this->pimpl->m_linkVel.resize(this->pimpl->m_robot_model);
this->pimpl->m_linkCRBIs.resize(this->pimpl->m_robot_model);
this->pimpl->m_rawMassMatrix.resize(this->pimpl->m_robot_model);
this->pimpl->m_rawMassMatrix.zero();
this->pimpl->m_jacBuffer.resize(6,6+this->pimpl->m_robot_model.getNrOfDOFs());
this->pimpl->m_jacBuffer.zero();
this->pimpl->m_baseBiasAcc.zero();
this->pimpl->m_linkBiasAcc.resize(this->pimpl->m_robot_model);
this->pimpl->m_baseAcc.zero();
this->pimpl->m_generalizedAccs.resize(this->pimpl->m_robot_model);
this->pimpl->m_linkAccs.resize(this->pimpl->m_robot_model);
this->pimpl->m_invDynBaseAcc.zero();
this->pimpl->m_invDynGeneralizedProperAccs.resize(this->pimpl->m_robot_model);
this->pimpl->m_invDynNetExtWrenches.resize(this->pimpl->m_robot_model);
this->pimpl->m_invDynInternalWrenches.resize(this->pimpl->m_robot_model);
this->pimpl->m_invDynLinkProperAccs.resize(this->pimpl->m_robot_model);
this->pimpl->m_invDynZeroVel.resize(this->pimpl->m_robot_model);
this->pimpl->m_invDynZeroVel.baseVel().zero();
this->pimpl->m_invDynZeroVel.jointVel().zero();
this->pimpl->m_invDynZeroLinkVel.resize(this->pimpl->m_robot_model);
this->pimpl->m_invDynZeroLinkProperAcc.resize(this->pimpl->m_robot_model);
this->pimpl->m_traversalCache.resize(this->pimpl->m_robot_model);
this->pimpl->m_generalizedForcesContainer.resize(this->pimpl->m_robot_model);
for(LinkIndex lnkIdx = 0; lnkIdx < static_cast<LinkIndex>(pimpl->m_robot_model.getNrOfLinks()); lnkIdx++)
{
pimpl->m_invDynZeroLinkVel(lnkIdx).zero();
}
}
int KinDynComputations::getFrameIndex(const std::string& frameName) const
{
int index = this->pimpl->m_robot_model.getFrameIndex(frameName);
reportErrorIf(index < 0, "KinDynComputations::getFrameIndex", "requested frameName not found in model");
return index;
}
std::string KinDynComputations::getFrameName(FrameIndex frameIndex) const
{
return this->pimpl->m_robot_model.getFrameName(frameIndex);
}
void KinDynComputations::computeFwdKinematics()
{
if( this->pimpl->m_isFwdKinematicsUpdated )
{
return;
}
// Compute position and velocity kinematics
bool ok = ForwardPosVelKinematics(this->pimpl->m_robot_model,
this->pimpl->m_traversal,
this->pimpl->m_pos,
this->pimpl->m_vel,
this->pimpl->m_linkPos,
this->pimpl->m_linkVel);
this->pimpl->m_isFwdKinematicsUpdated = ok;
}
void KinDynComputations::computeRawMassMatrixAndTotalMomentum()
{
if( this->pimpl->m_isRawMassMatrixUpdated )
{
return;
}
// Compute raw mass matrix
bool ok = CompositeRigidBodyAlgorithm(pimpl->m_robot_model,
pimpl->m_traversal,
pimpl->m_pos.jointPos(),
pimpl->m_linkCRBIs,
pimpl->m_rawMassMatrix);
reportErrorIf(!ok,"KinDynComputations::computeRawMassMatrix","Error in computing mass matrix.");
// m_linkPos and m_linkVel are used in the computation of the total momentum
// so we need to make sure that they are updated
this->computeFwdKinematics();
// Compute total momentum
ComputeLinearAndAngularMomentum(pimpl->m_robot_model,
pimpl->m_linkPos,
pimpl->m_linkVel,
pimpl->m_totalMomentum);
this->pimpl->m_isRawMassMatrixUpdated = ok;
}
void KinDynComputations::computeBiasAccFwdKinematics()
{
if( this->pimpl->m_areBiasAccelerationsUpdated )
{
return;
}
// Convert input base acceleration, that in this case is zero
Vector6 zeroBaseAcc;
zeroBaseAcc.zero();
if( pimpl->m_frameVelRepr == BODY_FIXED_REPRESENTATION )
{
fromEigen(pimpl->m_baseBiasAcc,toEigen(zeroBaseAcc));
}
else if( pimpl->m_frameVelRepr == INERTIAL_FIXED_REPRESENTATION )
{
pimpl->m_baseBiasAcc = convertInertialAccelerationToBodyFixedAcceleration(zeroBaseAcc, pimpl->m_pos.worldBasePos());
}
else
{
assert(pimpl->m_frameVelRepr == MIXED_REPRESENTATION);
pimpl->m_baseBiasAcc = convertMixedAccelerationToBodyFixedAcceleration(zeroBaseAcc,
pimpl->m_vel.baseVel(),
pimpl->m_pos.worldBasePos().getRotation());
}
// Compute body-fixed bias accelerations
bool ok = ForwardBiasAccKinematics(pimpl->m_robot_model,
pimpl->m_traversal,
pimpl->m_pos,
pimpl->m_vel,
pimpl->m_baseBiasAcc,
pimpl->m_linkVel,
pimpl->m_linkBiasAcc);
reportErrorIf(!ok,"KinDynComputations::computeBiasAccFwdKinematics","Error in computing the bias accelerations.");
this->pimpl->m_areBiasAccelerationsUpdated = ok;
}
bool KinDynComputations::loadRobotModel(const Model& model)
{
this->pimpl->m_robot_model = model;
this->pimpl->m_isModelValid = true;
this->pimpl->m_robot_model.computeFullTreeTraversal(this->pimpl->m_traversal);
this->resizeInternalDataStructures();
this->invalidateCache();
return true;
}
bool KinDynComputations::isValid() const
{
return (this->pimpl->m_isModelValid);
}
FrameVelocityRepresentation KinDynComputations::getFrameVelocityRepresentation() const
{
return pimpl->m_frameVelRepr;
}
bool KinDynComputations::setFrameVelocityRepresentation(const FrameVelocityRepresentation frameVelRepr) const
{
if( frameVelRepr != INERTIAL_FIXED_REPRESENTATION &&
frameVelRepr != BODY_FIXED_REPRESENTATION &&
frameVelRepr != MIXED_REPRESENTATION )
{
reportError("KinDynComputations","setFrameVelocityRepresentation","unknown frame velocity representation");
return false;
}
// If there is a change in FrameVelocityRepresentation, we should also invalidate the bias acceleration cache, as
// the bias acceleration depends on the frameVelRepr even if it is always expressed in body fixed representation.
// All the other cache are fine because they are always stored in BODY_FIXED, and they do not depend on the frameVelRepr,
// as they are converted on the fly when the relative retrieval method is called.
if (frameVelRepr != pimpl->m_frameVelRepr)
{
this->pimpl->m_areBiasAccelerationsUpdated = false;
}
pimpl->m_frameVelRepr = frameVelRepr;
return true;
}
std::string KinDynComputations::getFloatingBase() const
{
LinkIndex base_link = this->pimpl->m_traversal.getBaseLink()->getIndex();
return this->pimpl->m_robot_model.getLinkName(base_link);
}
bool KinDynComputations::setFloatingBase(const std::string& floatingBaseName)
{
LinkIndex newFloatingBaseLinkIndex = this->pimpl->m_robot_model.getLinkIndex(floatingBaseName);
return this->pimpl->m_robot_model.computeFullTreeTraversal(this->pimpl->m_traversal,newFloatingBaseLinkIndex);
}
unsigned int KinDynComputations::getNrOfLinks() const
{
return this->pimpl->m_robot_model.getNrOfLinks();
}
const Model& KinDynComputations::getRobotModel() const
{
return this->pimpl->m_robot_model;
}
const Model& KinDynComputations::model() const
{
return pimpl->m_robot_model;
}
bool KinDynComputations::getRelativeJacobianSparsityPattern(const iDynTree::FrameIndex refFrameIndex,
const iDynTree::FrameIndex frameIndex,
iDynTree::MatrixDynSize & outJacobian) const
{
//I have the two links. Create the jacobian
outJacobian.resize(6, pimpl->m_robot_model.getNrOfDOFs());
return this->getRelativeJacobianSparsityPattern(refFrameIndex, frameIndex, MatrixView<double>(outJacobian));
}
bool KinDynComputations::getRelativeJacobianSparsityPattern(const iDynTree::FrameIndex refFrameIndex,
const iDynTree::FrameIndex frameIndex,
MatrixView<double> outJacobian) const
{
bool ok = (outJacobian.rows() == 6)
&& (outJacobian.cols() == pimpl->m_robot_model.getNrOfDOFs());
if( !ok )
{
reportError("KinDynComputations","getRelativeJacobianSparsityPattern","Wrong size in input outJacobian");
return false;
}
if (!pimpl->m_robot_model.isValidFrameIndex(frameIndex))
{
reportError("KinDynComputations","getRelativeJacobian","Frame index out of bounds");
return false;
}
if (!pimpl->m_robot_model.isValidFrameIndex(refFrameIndex))
{
reportError("KinDynComputations","getRelativeJacobian","Reference frame index out of bounds");
return false;
}
// clear the matrix
toEigen(outJacobian).setZero();
// This method computes the sparsity pattern of the relative Jacobian.
// For details on how to compute the relative Jacobian, see Traversaro's PhD thesis, 3.37
// or getRelativeJacobianExplicit method.
// Here we simply implement the same code, but trying to obtain only 1 and zeros.
// Get the links to which the frames are attached
LinkIndex jacobianLinkIndex = pimpl->m_robot_model.getFrameLink(frameIndex);
LinkIndex refJacobianLink = pimpl->m_robot_model.getFrameLink(refFrameIndex);
iDynTree::Traversal& relativeTraversal = pimpl->m_traversalCache.getTraversalWithLinkAsBase(pimpl->m_robot_model, refJacobianLink);
// Compute joint part
// We iterate from the link up in the traveral until we reach the base
LinkIndex visitedLinkIdx = jacobianLinkIndex;
// Generic adjoint transform matrix (6x6).
// Rotations are filled with 1.
Matrix6x6 genericAdjointTransform;
genericAdjointTransform.zero();
// Set 1 to rotation matrix (top left)
iDynTree::toEigen(genericAdjointTransform).topLeftCorner(3, 3).setOnes();
// Set 1 to p \times R (top right)
iDynTree::toEigen(genericAdjointTransform).topRightCorner(3, 3).setOnes();
// Set 1 to rotation matrix (top left)
iDynTree::toEigen(genericAdjointTransform).bottomRightCorner(3, 3).setOnes();
while (visitedLinkIdx != relativeTraversal.getBaseLink()->getIndex())
{
//get the pair of links in the traversal
LinkIndex parentLinkIdx = relativeTraversal.getParentLinkFromLinkIndex(visitedLinkIdx)->getIndex();
IJointConstPtr joint = relativeTraversal.getParentJointFromLinkIndex(visitedLinkIdx);
//Now for each Dof get the motion subspace
//{}^F s_{E,F}, i.e. the velocity of F wrt E written in F.
size_t dofOffset = joint->getDOFsOffset();
for (int i = 0; i < joint->getNrOfDOFs(); ++i)
{
// This is actually where we specify the pattern
SpatialMotionVector column = joint->getMotionSubspaceVector(i, visitedLinkIdx, parentLinkIdx);
for (size_t c = 0; c < column.size(); ++c) {
column(c) = std::abs(column(c)) < iDynTree::DEFAULT_TOL ? 0.0 : 1.0;
}
toEigen(outJacobian).col(dofOffset + i) = toEigen(genericAdjointTransform) * toEigen(column);
//have only 0 and 1 => divide component wise the column by itself
for (size_t r = 0; r < toEigen(outJacobian).col(dofOffset + i).size(); ++r) {
toEigen(outJacobian).col(dofOffset + i).coeffRef(r) = std::abs(toEigen(outJacobian).col(dofOffset + i).coeffRef(r)) < iDynTree::DEFAULT_TOL ? 0.0 : 1.0;
}
}
visitedLinkIdx = parentLinkIdx;
}
return true;
}
bool KinDynComputations::getFrameFreeFloatingJacobianSparsityPattern(const FrameIndex frameIndex,
iDynTree::MatrixDynSize & outJacobianPattern) const
{
outJacobianPattern.resize(6, 6 + getNrOfDegreesOfFreedom());
return this->getFrameFreeFloatingJacobianSparsityPattern(frameIndex,
MatrixView<double>(outJacobianPattern));
}
bool KinDynComputations::getFrameFreeFloatingJacobianSparsityPattern(const FrameIndex frameIndex,
MatrixView<double> outJacobianPattern) const
{
if (!pimpl->m_robot_model.isValidFrameIndex(frameIndex))
{
reportError("KinDynComputations","getFrameJacobian","Frame index out of bounds");
return false;
}
bool ok = (outJacobianPattern.rows() == 6)
&& (outJacobianPattern.cols() == pimpl->m_robot_model.getNrOfDOFs() + 6);
if( !ok )
{
reportError("KinDynComputations",
"getFrameFreeJacobianSparsityPattern",
"Wrong size in input outJacobianPattern");
return false;
}
// Get the link to which the frame is attached
LinkIndex jacobLink = pimpl->m_robot_model.getFrameLink(frameIndex);
Matrix6x6 genericAdjointTransform;
genericAdjointTransform.zero();
// Set 1 to rotation matrix (top left)
iDynTree::toEigen(genericAdjointTransform).topLeftCorner(3, 3).setOnes();
// Set 1 to p \times R (top right)
iDynTree::toEigen(genericAdjointTransform).topRightCorner(3, 3).setOnes();
// Set 1 to rotation matrix (top left)
iDynTree::toEigen(genericAdjointTransform).bottomRightCorner(3, 3).setOnes();
// We zero the jacobian
toEigen(outJacobianPattern).setZero();
// Compute base part
toEigen(outJacobianPattern).leftCols<6>() = toEigen(genericAdjointTransform);
// Compute joint part
// We iterate from the link up in the traveral until we reach the base
LinkIndex visitedLinkIdx = jacobLink;
while (visitedLinkIdx != pimpl->m_traversal.getBaseLink()->getIndex())
{
LinkIndex parentLinkIdx = pimpl->m_traversal.getParentLinkFromLinkIndex(visitedLinkIdx)->getIndex();
IJointConstPtr joint = pimpl->m_traversal.getParentJointFromLinkIndex(visitedLinkIdx);
size_t dofOffset = joint->getDOFsOffset();
for (unsigned i = 0; i < joint->getNrOfDOFs(); ++i)
{
SpatialMotionVector jointMotionSubspace = joint->getMotionSubspaceVector(i, visitedLinkIdx, parentLinkIdx);
// 1 or 0 in vector
for (size_t c = 0; c < jointMotionSubspace.size(); ++c) {
jointMotionSubspace(c) = std::abs(jointMotionSubspace(c)) < iDynTree::DEFAULT_TOL ? 0.0 : 1.0;
}
toEigen(outJacobianPattern).col(6 + dofOffset + i) = toEigen(genericAdjointTransform) * toEigen(jointMotionSubspace);
//have only 0 and 1 => divide component wise the column by itself
for (size_t r = 0; r < toEigen(outJacobianPattern).col(6 + dofOffset + i).size(); ++r) {
toEigen(outJacobianPattern).col(6 + dofOffset + i).coeffRef(r) = std::abs(toEigen(outJacobianPattern).col(6 + dofOffset + i).coeffRef(r)) < iDynTree::DEFAULT_TOL ? 0.0 : 1.0;
}
}
visitedLinkIdx = parentLinkIdx;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////
//// Degrees of freedom related methods
//////////////////////////////////////////////////////////////////////////////
unsigned int KinDynComputations::getNrOfDegreesOfFreedom() const
{
return (unsigned int)this->pimpl->m_robot_model.getNrOfDOFs();
}
std::string KinDynComputations::getDescriptionOfDegreeOfFreedom(int dof_index) const
{
return this->pimpl->m_robot_model.getJointName(dof_index);
}
std::string KinDynComputations::getDescriptionOfDegreesOfFreedom() const
{
std::stringstream ss;
for(unsigned int dof = 0; dof < this->getNrOfDegreesOfFreedom(); dof++ )
{
ss << "DOF Index: " << dof << " Name: " << this->getDescriptionOfDegreeOfFreedom(dof) << std::endl;
}
return ss.str();
}
bool KinDynComputations::setRobotState(const VectorDynSize& s,
const VectorDynSize& s_dot,
const Vector3& world_gravity)
{
Transform world_T_base = Transform::Identity();
Twist base_velocity = Twist::Zero();
return setRobotState(world_T_base,s,
base_velocity,s_dot,
world_gravity);
}
bool KinDynComputations::setRobotState(Span<const double> s,
Span<const double> s_dot,
Span<const double> world_gravity)
{
Transform world_T_base = Transform::Identity();
Twist base_velocity = Twist::Zero();
return setRobotState(world_T_base, s,
base_velocity, s_dot,
world_gravity);
}
bool KinDynComputations::setRobotState(iDynTree::MatrixView<const double> world_T_base,
Span<const double> s,
Span<const double> base_velocity,
Span<const double> s_dot,
Span<const double> world_gravity)
{
constexpr int expected_transform_cols = 4;
constexpr int expected_transform_rows = 4;
bool ok = (world_T_base.rows() == expected_transform_rows) && (world_T_base.cols() == expected_transform_cols);
if( !ok )
{
reportError("KinDynComputations","setRobotState","Wrong size in input world_T_base");
return false;
}
constexpr int expected_twist_size = 6;
ok = base_velocity.size() == expected_twist_size;
if( !ok )
{
reportError("KinDynComputations","setRobotState","Wrong size in input base_velocity");
return false;
}
return this->setRobotState(iDynTree::Transform(world_T_base),
s,
iDynTree::SpatialMotionVector(base_velocity),
s_dot,
world_gravity);
}
bool KinDynComputations::setRobotState(const Transform& world_T_base,
const VectorDynSize& s,
const Twist& base_velocity,
const VectorDynSize& s_dot,
const Vector3& world_gravity)
{
bool ok = s.size() == pimpl->m_robot_model.getNrOfPosCoords();
if( !ok )
{
reportError("KinDynComputations","setRobotState","Wrong size in input joint positions");
return false;
}
ok = s_dot.size() == pimpl->m_robot_model.getNrOfDOFs();
if( !ok )
{
reportError("KinDynComputations","setRobotState","Wrong size in input joint velocities");
return false;
}
this->invalidateCache();
// Save pos
this->pimpl->m_pos.worldBasePos() = world_T_base;
toEigen(this->pimpl->m_pos.jointPos()) = toEigen(s);
// Save gravity
this->pimpl->m_gravityAcc = world_gravity;
Rotation base_R_inertial = this->pimpl->m_pos.worldBasePos().getRotation().inverse();
toEigen(pimpl->m_gravityAccInBaseLinkFrame) = toEigen(base_R_inertial)*toEigen(this->pimpl->m_gravityAcc);
// Save vel
toEigen(pimpl->m_vel.jointVel()) = toEigen(s_dot);
this->pimpl->m_baseVelSetViaRobotState = base_velocity;
// Account for the different possible representations
if (pimpl->m_frameVelRepr == MIXED_REPRESENTATION)
{
pimpl->m_vel.baseVel() = pimpl->m_pos.worldBasePos().getRotation().inverse()*base_velocity;
}
else if (pimpl->m_frameVelRepr == BODY_FIXED_REPRESENTATION)
{
// Data is stored in body fixed
pimpl->m_vel.baseVel() = base_velocity;
}
else
{
assert(pimpl->m_frameVelRepr == INERTIAL_FIXED_REPRESENTATION);
// base_X_inertial \ls^inertial v_base
pimpl->m_vel.baseVel() = pimpl->m_pos.worldBasePos().inverse()*base_velocity;
}
return true;
}
void KinDynComputations::getRobotState(Transform& world_T_base,
VectorDynSize& s,
Twist& base_velocity,
VectorDynSize& s_dot,
Vector3& world_gravity)
{
getRobotState(s, s_dot, world_gravity);
world_T_base = this->pimpl->m_pos.worldBasePos();
// Account for the different possible representations
if (pimpl->m_frameVelRepr == MIXED_REPRESENTATION)
{
base_velocity = pimpl->m_pos.worldBasePos().getRotation() * pimpl->m_vel.baseVel();
}
else if (pimpl->m_frameVelRepr == BODY_FIXED_REPRESENTATION)
{
// Data is stored in body fixed
base_velocity = pimpl->m_vel.baseVel();
}
else
{
assert(pimpl->m_frameVelRepr == INERTIAL_FIXED_REPRESENTATION);
// base_X_inertial \ls^inertial v_base
base_velocity = pimpl->m_pos.worldBasePos() * pimpl->m_vel.baseVel();
}
}
bool KinDynComputations::getRobotState(iDynTree::MatrixView<double> world_T_base,
iDynTree::Span<double> s,
iDynTree::Span<double> base_velocity,
iDynTree::Span<double> s_dot,
iDynTree::Span<double> world_gravity)
{
bool ok = s.size() == pimpl->m_robot_model.getNrOfPosCoords();
if( !ok )
{
reportError("KinDynComputations","getRobotState","Wrong size in input joint positions");
return false;
}
ok = s_dot.size() == pimpl->m_robot_model.getNrOfDOFs();
if( !ok )
{
reportError("KinDynComputations","getRobotState","Wrong size in input joint velocities");
return false;
}
constexpr int expected_transform_cols = 4;
constexpr int expected_transform_rows = 4;
ok = (world_T_base.rows() == expected_transform_rows) && (world_T_base.cols() == expected_transform_cols);
if( !ok )
{
reportError("KinDynComputations","getRobotState","Wrong size in input world_T_base");
return false;
}
constexpr int expected_twist_size = 6;
ok = base_velocity.size() == expected_twist_size;
if( !ok )
{
reportError("KinDynComputations","getRobotState","Wrong size in input base_velocity");
return false;
}
getRobotState(s, s_dot, world_gravity);
toEigen(world_T_base) = toEigen(this->pimpl->m_pos.worldBasePos().asHomogeneousTransform());
// Account for the different possible representations
if (pimpl->m_frameVelRepr == MIXED_REPRESENTATION)
{
toEigen(base_velocity) = toEigen(pimpl->m_pos.worldBasePos().getRotation() * pimpl->m_vel.baseVel());
}
else if (pimpl->m_frameVelRepr == BODY_FIXED_REPRESENTATION)
{
// Data is stored in body fixed
toEigen(base_velocity) = toEigen(pimpl->m_vel.baseVel());
}
else
{
assert(pimpl->m_frameVelRepr == INERTIAL_FIXED_REPRESENTATION);
// base_X_inertial \ls^inertial v_base
toEigen(base_velocity) = toEigen(pimpl->m_pos.worldBasePos() * pimpl->m_vel.baseVel());
}
return true;
}
void KinDynComputations::getRobotState(iDynTree::VectorDynSize &s,
iDynTree::VectorDynSize &s_dot,
iDynTree::Vector3& world_gravity)
{
this->getRobotState(make_span(s), make_span(s_dot), make_span(world_gravity));
}
void KinDynComputations::getRobotState(iDynTree::Span<double> s,
iDynTree::Span<double> s_dot,
iDynTree::Span<double> world_gravity)
{
constexpr int expected_size_gravity = 3;
assert(s.size() == pimpl->m_robot_model.getNrOfDOFs());
assert(s_dot.size() == pimpl->m_robot_model.getNrOfDOFs());
assert(world_gravity.size() == expected_size_gravity);
toEigen(world_gravity) = toEigen(pimpl->m_gravityAcc);
toEigen(s) = toEigen(this->pimpl->m_pos.jointPos());
toEigen(s_dot) = toEigen(pimpl->m_vel.jointVel());
}
bool KinDynComputations::setJointPos(Span<const double> s)
{
bool ok = (s.size() == pimpl->m_robot_model.getNrOfPosCoords());
if( !ok )
{
reportError("KinDynComputations","setJointPos","Wrong size in input joint positions");
return false;
}
toEigen(this->pimpl->m_pos.jointPos()) = toEigen(s);
// Invalidate cache
this->invalidateCache();
return true;
}
bool KinDynComputations::setJointPos(const VectorDynSize& s)
{
return this->setJointPos(make_span(s));
}
bool KinDynComputations::setWorldBaseTransform(const iDynTree::Transform &world_T_base)
{
return setRobotState(world_T_base,
this->pimpl->m_pos.jointPos(),
this->pimpl->m_baseVelSetViaRobotState,
this->pimpl->m_vel.jointVel(),
this->pimpl->m_gravityAcc);
}
bool KinDynComputations::setWorldBaseTransform(iDynTree::MatrixView<const double> &world_T_base)
{
constexpr int expected_transform_cols = 4;
constexpr int expected_transform_rows = 4;
bool ok = (world_T_base.rows() == expected_transform_rows) && (world_T_base.cols() == expected_transform_cols);
if( !ok )
{
reportError("KinDynComputations","setWorldBaseTransform","Wrong size in input world_T_base");
return false;
}
return setWorldBaseTransform(iDynTree::Transform(world_T_base));
}
Transform KinDynComputations::getWorldBaseTransform() const
{
return this->pimpl->m_pos.worldBasePos();