-
Notifications
You must be signed in to change notification settings - Fork 45
/
Joints.cpp
1773 lines (1541 loc) · 55 KB
/
Joints.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
#define BIORBD_API_EXPORTS
#include "RigidBody/Joints.h"
#include <rbdl/rbdl_utils.h>
#include <rbdl/Kinematics.h>
#include <rbdl/Dynamics.h>
#include "Utils/Scalar.h"
#include "Utils/String.h"
#include "Utils/Quaternion.h"
#include "Utils/Matrix.h"
#include "Utils/Matrix3d.h"
#include "Utils/Error.h"
#include "Utils/RotoTrans.h"
#include "Utils/Rotation.h"
#include "Utils/SpatialVector.h"
#include "RigidBody/GeneralizedCoordinates.h"
#include "RigidBody/GeneralizedVelocity.h"
#include "RigidBody/GeneralizedAcceleration.h"
#include "RigidBody/GeneralizedTorque.h"
#include "RigidBody/Segment.h"
#include "RigidBody/Markers.h"
#include "RigidBody/NodeSegment.h"
#include "RigidBody/MeshFace.h"
#include "RigidBody/Mesh.h"
#include "RigidBody/SegmentCharacteristics.h"
#include "RigidBody/Contacts.h"
#include "RigidBody/SoftContacts.h"
using namespace BIORBD_NAMESPACE;
rigidbody::Joints::Joints() :
RigidBodyDynamics::Model(),
m_segments(std::make_shared<std::vector<rigidbody::Segment>>()),
m_nbRoot(std::make_shared<unsigned int>(0)),
m_nbDof(std::make_shared<unsigned int>(0)),
m_nbQ(std::make_shared<unsigned int>(0)),
m_nbQdot(std::make_shared<unsigned int>(0)),
m_nbQddot(std::make_shared<unsigned int>(0)),
m_nRotAQuat(std::make_shared<unsigned int>(0)),
m_isKinematicsComputed(std::make_shared<bool>(false)),
m_totalMass(std::make_shared<utils::Scalar>(0))
{
// Redefining gravity so it is on z by default
this->gravity = utils::Vector3d (0, 0, -9.81);
}
rigidbody::Joints::Joints(const rigidbody::Joints &other) :
RigidBodyDynamics::Model(other),
m_segments(other.m_segments),
m_nbRoot(other.m_nbRoot),
m_nbDof(other.m_nbDof),
m_nbQ(other.m_nbQ),
m_nbQdot(other.m_nbQdot),
m_nbQddot(other.m_nbQddot),
m_nRotAQuat(other.m_nRotAQuat),
m_isKinematicsComputed(other.m_isKinematicsComputed),
m_totalMass(other.m_totalMass)
{
}
rigidbody::Joints::~Joints()
{
}
rigidbody::Joints rigidbody::Joints::DeepCopy() const
{
rigidbody::Joints copy;
copy.DeepCopy(*this);
return copy;
}
void rigidbody::Joints::DeepCopy(const rigidbody::Joints &other)
{
static_cast<RigidBodyDynamics::Model&>(*this) = other;
m_segments->resize(other.m_segments->size());
for (unsigned int i=0; i<other.m_segments->size(); ++i) {
(*m_segments)[i] = (*other.m_segments)[i].DeepCopy();
}
*m_nbRoot = *other.m_nbRoot;
*m_nbDof = *other.m_nbDof;
*m_nbQ = *other.m_nbQ;
*m_nbQdot = *other.m_nbQdot;
*m_nbQddot = *other.m_nbQddot;
*m_nRotAQuat = *other.m_nRotAQuat;
*m_isKinematicsComputed = *other.m_isKinematicsComputed;
*m_totalMass = *other.m_totalMass;
}
unsigned int rigidbody::Joints::nbGeneralizedTorque() const
{
return nbQddot();
}
unsigned int rigidbody::Joints::nbDof() const
{
return *m_nbDof;
}
std::vector<utils::String> rigidbody::Joints::nameDof() const
{
std::vector<utils::String> names;
for (unsigned int i = 0; i < nbSegment(); ++i) {
for (unsigned int j = 0; j < segment(i).nbDof(); ++j) {
names.push_back(segment(i).name() + "_" + segment(i).nameDof(j));
}
}
// Append Quaternion Q
for (unsigned int i = 0; i < nbSegment(); ++i) {
if (segment(i).isRotationAQuaternion()) {
names.push_back(segment(i).name() + "_" + segment(i).nameDof(3));
}
}
return names;
}
unsigned int rigidbody::Joints::nbQ() const
{
return *m_nbQ;
}
unsigned int rigidbody::Joints::nbQdot() const
{
return *m_nbQdot;
}
unsigned int rigidbody::Joints::nbQddot() const
{
return *m_nbQddot;
}
unsigned int rigidbody::Joints::nbRoot() const
{
return *m_nbRoot;
}
utils::Scalar rigidbody::Joints::mass() const
{
return *m_totalMass;
}
unsigned int rigidbody::Joints::AddSegment(
const utils::String &segmentName,
const utils::String &parentName,
const utils::String &translationSequence,
const utils::String &rotationSequence,
const std::vector<utils::Range>& QRanges,
const std::vector<utils::Range>& QDotRanges,
const std::vector<utils::Range>& QDDotRanges,
const rigidbody::SegmentCharacteristics& characteristics,
const utils::RotoTrans& referenceFrame,
int forcePlates)
{
rigidbody::Segment tp(
*this, segmentName, parentName, translationSequence,
rotationSequence, QRanges, QDotRanges, QDDotRanges, characteristics,
RigidBodyDynamics::Math::SpatialTransform(referenceFrame.rot().transpose(),
referenceFrame.trans()),
forcePlates);
if (this->GetBodyId(parentName.c_str()) ==
std::numeric_limits<unsigned int>::max()) {
*m_nbRoot +=
tp.nbDof(); // If the segment name is "Root", add the number of DoF of root
}
*m_nbDof += tp.nbDof();
*m_nbQ += tp.nbQ();
*m_nbQdot += tp.nbQdot();
*m_nbQddot += tp.nbQddot();
if (tp.isRotationAQuaternion()) {
++*m_nRotAQuat;
}
*m_totalMass +=
characteristics.mMass; // Add the segment mass to the total body mass
m_segments->push_back(tp);
return 0;
}
unsigned int rigidbody::Joints::AddSegment(
const utils::String &segmentName,
const utils::String &parentName,
const utils::String &seqR,
const std::vector<utils::Range>& QRanges,
const std::vector<utils::Range>& QDotRanges,
const std::vector<utils::Range>& QDDotRanges,
const rigidbody::SegmentCharacteristics& characteristics,
const utils::RotoTrans& referenceFrame,
int forcePlates)
{
rigidbody::Segment tp(
*this, segmentName, parentName, seqR, QRanges, QDotRanges, QDDotRanges,
characteristics, RigidBodyDynamics::Math::SpatialTransform(
referenceFrame.rot().transpose(), referenceFrame.trans()),
forcePlates);
if (this->GetBodyId(parentName.c_str()) ==
std::numeric_limits<unsigned int>::max()) {
*m_nbRoot +=
tp.nbDof(); // If the name of the segment is "Root", add the number of DoF of root
}
*m_nbDof += tp.nbDof();
*m_totalMass +=
characteristics.mMass; // Add the segment mass to the total body mass
m_segments->push_back(tp);
return 0;
}
utils::Vector3d rigidbody::Joints::getGravity() const
{
return gravity;
}
void rigidbody::Joints::setGravity(
const utils::Vector3d &newGravity)
{
gravity = newGravity;
}
void rigidbody::Joints::updateSegmentCharacteristics(
unsigned int idx,
const rigidbody::SegmentCharacteristics& characteristics)
{
utils::Error::check(idx < m_segments->size(),
"Asked for a wrong segment (out of range)");
(*m_segments)[idx].updateCharacteristics(*this, characteristics);
}
const rigidbody::Segment& rigidbody::Joints::segment(
unsigned int idx) const
{
utils::Error::check(idx < m_segments->size(),
"Asked for a wrong segment (out of range)");
return (*m_segments)[idx];
}
const rigidbody::Segment &rigidbody::Joints::segment(
const utils::String & name) const
{
return segment(static_cast<unsigned int>(getBodyBiorbdId(name.c_str())));
}
const std::vector<rigidbody::Segment>& rigidbody::Joints::segments() const
{
return *m_segments;
}
unsigned int rigidbody::Joints::nbSegment() const
{
return static_cast<unsigned int>(m_segments->size());
}
std::vector<RigidBodyDynamics::Math::SpatialVector>
rigidbody::Joints::dispatchedForce(
std::vector<std::vector<utils::SpatialVector>> &spatialVector,
unsigned int frame) const
{
// Iterator on the force table
std::vector<utils::SpatialVector>
sv2; // Gather in the same table the values at the same instant of different platforms
for (auto vec : spatialVector) {
sv2.push_back(vec[frame]);
}
// Call the equivalent function that only manages on instant
std::vector<RigidBodyDynamics::Math::SpatialVector> *sv_out = dispatchedForce(&sv2);
if (!sv_out){
utils::Error::raise("Dispatch forces failed");
}
return *sv_out;
}
std::vector<RigidBodyDynamics::Math::SpatialVector> * rigidbody::Joints::dispatchedForce(
std::vector<utils::SpatialVector> *sv)
const // a spatialVector per platform
{
if (!sv){
return nullptr;
}
// Output table
std::vector<RigidBodyDynamics::Math::SpatialVector> *sv_out = new std::vector<RigidBodyDynamics::Math::SpatialVector>();
// Null Spatial vector nul to fill the final table
utils::SpatialVector sv_zero(0.,0.,0.,0.,0.,0.);
sv_out->push_back(sv_zero); // The first one is associated with the universe
// Dispatch the forces
for (size_t i=0; i<nbSegment(); ++i) {
auto& segment ((*m_segments)[i]);
unsigned int nbDof = segment.nbDof();
if (nbDof == 0) { // Do not add anything if the nbDoF is zero
continue;
}
// For each segment
for (unsigned int i=0; i<nbDof-1; ++i) {
// Put a sv_zero on each DoF except the last one
sv_out->push_back(sv_zero);
}
if (segment.platformIdx() >= 0) {
// If the solid is in contact with the platform (!= -1)
sv_out->push_back((*sv)[static_cast<unsigned int>(segment.platformIdx())]);
} else {
// Otherwise, put zero
sv_out->push_back(sv_zero);
}
}
return sv_out;
}
std::vector<RigidBodyDynamics::Math::SpatialVector> * rigidbody::Joints::combineExtForceAndSoftContact(
std::vector<utils::SpatialVector> *f_ext,
std::vector<utils::Vector> *f_contacts,
const rigidbody::GeneralizedCoordinates& Q,
const rigidbody::GeneralizedVelocity& QDot,
bool updateKin
)
{
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#else
if (updateKin){
UpdateKinematicsCustom(&Q, &QDot);
}
updateKin = false;
#endif
std::vector<RigidBodyDynamics::Math::SpatialVector>* softContacts = dynamic_cast<rigidbody::SoftContacts*>(this)->softContactToSpatialVector(Q, QDot, updateKin);
std::vector<RigidBodyDynamics::Math::SpatialVector>* f_ext_rbdl = dispatchedForce(f_ext);
std::vector<RigidBodyDynamics::Math::SpatialVector>* f_contacts_rbdl = dynamic_cast<rigidbody::Contacts*>(this)->rigidContactToSpatialVector(Q, f_contacts, updateKin);
if (!f_ext_rbdl && !softContacts && !f_contacts_rbdl){
// Return a nullptr
return nullptr;
}
if (!f_ext_rbdl && !f_contacts_rbdl){
// Return the softContacts (and nullptr if softContacts is nullptr)
return softContacts;
}
if (!softContacts && !f_contacts_rbdl){
// Return the External forces
return f_ext_rbdl;
}
if (!f_ext_rbdl && !softContacts){
// Return the contact forces
return f_contacts_rbdl;
}
if (!f_contacts_rbdl)
{
for (size_t i=0; i<softContacts->size(); ++i){
// Combine the external forces with the soft contacts
(*f_ext_rbdl)[i] += (*softContacts)[i];
}
return f_ext_rbdl;
}
if (!softContacts)
{
for (size_t i=0; i<f_contacts_rbdl->size(); ++i){
// Combine the external forces with the soft contacts
(*f_ext_rbdl)[i] += (*f_contacts_rbdl)[i];
}
return f_ext_rbdl;
}
if (!f_ext_rbdl)
{
for (size_t i=0; i<f_contacts_rbdl->size(); ++i){
// Combine the external forces with the soft contacts
(*softContacts)[i] += (*f_contacts_rbdl)[i];
}
return softContacts;
}
for (size_t i=0; i<f_contacts_rbdl->size(); ++i){
// Combine the external forces with the soft contacts
(*f_ext_rbdl)[i] += (*softContacts)[i];
(*f_ext_rbdl)[i] += (*f_contacts_rbdl)[i];
}
delete softContacts;
delete f_contacts_rbdl;
return f_ext_rbdl;
}
int rigidbody::Joints::getBodyBiorbdId(
const utils::String &segmentName) const
{
for (int i=0; i<static_cast<int>(m_segments->size()); ++i)
if (!(*m_segments)[static_cast<unsigned int>(i)].name().compare(segmentName)) {
return i;
}
return -1;
}
int rigidbody::Joints::getBodyRbdlId(
const utils::String &segmentName) const
{
return GetBodyId(segmentName.c_str());
}
int rigidbody::Joints::getBodyRbdlIdToBiorbdId(
const int idx) const
{
// Assuming that this is also a joint type (via BiorbdModel)
const rigidbody::Joints &model = dynamic_cast<const rigidbody::Joints &>(*this);
std::string bodyName = model.GetBodyName(idx);
return model.getBodyBiorbdId(bodyName);
}
unsigned int rigidbody::Joints::getBodyBiorbdIdToRbdlId(
const int idx) const
{
return (*m_segments)[idx].id();
}
std::vector<std::vector<unsigned int> > rigidbody::Joints::getDofSubTrees()
{
// initialize subTrees
std::vector<std::vector<unsigned int> > subTrees;
std::vector<unsigned int> subTree_empty;
for (unsigned int j=0; j<this->mu.size(); ++j) {
subTrees.push_back(subTree_empty);
}
// Get all dof without parent
std::vector<unsigned int> dof_with_no_parent_id;
for (unsigned int i=1; i<this->mu.size(); ++i) { // begin at 1 because 0 is its own parent in rbdl.
if (this->lambda[i]==0) {
dof_with_no_parent_id.push_back(i);
}
}
// Get all subtrees of dofs without parents
for (unsigned int i=0; i<dof_with_no_parent_id.size(); ++i) {
unsigned int dof_id = dof_with_no_parent_id[i];
// initialize subTrees_temp
std::vector<std::vector<unsigned int> > subTrees_temp;
for (unsigned int j=0; j<this->mu.size(); ++j) {
subTrees_temp.push_back(subTree_empty);
}
std::vector<std::vector<unsigned int> > subTrees_temp_filled = recursiveDofSubTrees(subTrees_temp, dof_id);
for (unsigned int j=0; j<subTrees_temp.size(); ++j) {
if (subTrees_temp_filled[j].empty()) {
continue;
}
else
{
subTrees[j].insert(subTrees[j].end(),
subTrees_temp_filled[j].begin(),
subTrees_temp_filled[j].end());
}
}
}
subTrees.erase(subTrees.begin());
return subTrees;
}
std::vector<std::vector<unsigned int> > rigidbody::Joints::recursiveDofSubTrees(
std::vector<std::vector<unsigned int> >subTrees,
unsigned int idx)
{
unsigned int q_index_i = this->mJoints[idx].q_index;
subTrees[idx].push_back(q_index_i);
std::vector<std::vector<unsigned int> > subTrees_filled;
subTrees_filled = subTrees;
std::vector<unsigned int> child_idx = this->mu[idx];
if (child_idx.size() > 0){
for (unsigned int i=0; i<child_idx.size(); ++i) {
unsigned int cur_child_id = child_idx[i];
subTrees_filled = recursiveDofSubTrees(subTrees_filled, cur_child_id);
std::vector<unsigned int> subTree_child = subTrees_filled[cur_child_id];
subTrees_filled[idx].insert(subTrees_filled[idx].end(),
subTree_child.begin(),
subTree_child.end());
}
}
return subTrees_filled;
}
std::vector<utils::RotoTrans> rigidbody::Joints::allGlobalJCS(
const rigidbody::GeneralizedCoordinates &Q)
{
UpdateKinematicsCustom (&Q, nullptr, nullptr);
return allGlobalJCS();
}
std::vector<utils::RotoTrans> rigidbody::Joints::allGlobalJCS()
const
{
std::vector<utils::RotoTrans> out;
for (unsigned int i=0; i<m_segments->size(); ++i) {
out.push_back(globalJCS(i));
}
return out;
}
utils::RotoTrans rigidbody::Joints::globalJCS(
const rigidbody::GeneralizedCoordinates &Q,
const utils::String &name)
{
UpdateKinematicsCustom (&Q, nullptr, nullptr);
return globalJCS(name);
}
utils::RotoTrans rigidbody::Joints::globalJCS(
const rigidbody::GeneralizedCoordinates &Q,
unsigned int idx)
{
// update the Kinematics if necessary
UpdateKinematicsCustom (&Q, nullptr, nullptr);
return globalJCS(idx);
}
utils::RotoTrans rigidbody::Joints::globalJCS(
const utils::String &name) const
{
return globalJCS(static_cast<unsigned int>(getBodyBiorbdId(name)));
}
utils::RotoTrans rigidbody::Joints::globalJCS(
unsigned int idx) const
{
return CalcBodyWorldTransformation((*m_segments)[idx].id());
}
std::vector<utils::RotoTrans> rigidbody::Joints::localJCS()
const
{
std::vector<utils::RotoTrans> out;
for (unsigned int i=0; i<m_segments->size(); ++i) {
out.push_back(localJCS(i));
}
return out;
}
utils::RotoTrans rigidbody::Joints::localJCS(
const utils::String &name) const
{
return localJCS(static_cast<unsigned int>(getBodyBiorbdId(name.c_str())));
}
utils::RotoTrans rigidbody::Joints::localJCS(
const unsigned int idx) const
{
return (*m_segments)[idx].localJCS();
}
std::vector<rigidbody::NodeSegment>
rigidbody::Joints::projectPoint(
const rigidbody::GeneralizedCoordinates& Q,
const std::vector<rigidbody::NodeSegment>& v,
bool updateKin)
{
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
if (updateKin) {
UpdateKinematicsCustom(&Q, nullptr, nullptr);
}
updateKin = false;
// Assuming that this is also a marker type (via BiorbdModel)
const rigidbody::Markers& marks =
dynamic_cast<rigidbody::Markers&>(*this);
// Sécurité
utils::Error::check(marks.nbMarkers() == v.size(),
"Number of marker must be equal to number of Vector3d");
std::vector<rigidbody::NodeSegment> out;
for (unsigned int i = 0; i < marks.nbMarkers(); ++i) {
rigidbody::NodeSegment tp(marks.marker(i));
if (tp.nbAxesToRemove() != 0) {
tp = v[i].applyRT(globalJCS(tp.parent()).transpose());
// Prendre la position du nouveau marker avec les infos de celui du modèle
out.push_back(projectPoint(Q, tp, updateKin));
} else
// S'il ne faut rien retirer (renvoyer tout de suite la même position)
{
out.push_back(v[i]);
}
}
return out;
}
rigidbody::NodeSegment rigidbody::Joints::projectPoint(
const rigidbody::GeneralizedCoordinates &Q,
const utils::Vector3d &v,
int segmentIdx,
const utils::String& axesToRemove,
bool updateKin)
{
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
if (updateKin) {
UpdateKinematicsCustom (&Q);
}
// Create a marker
const utils::String& segmentName(segment(static_cast<unsigned int>
(segmentIdx)).name());
rigidbody::NodeSegment node( v.applyRT(globalJCS(
static_cast<unsigned int>(segmentIdx)).transpose()), "tp",
segmentName,
true, true, axesToRemove, static_cast<int>(GetBodyId(segmentName.c_str())));
// Project and then reset in global
return projectPoint(Q, node, false);
}
rigidbody::NodeSegment rigidbody::Joints::projectPoint(
const rigidbody::GeneralizedCoordinates &Q,
const rigidbody::NodeSegment &n,
bool updateKin)
{
// Assuming that this is also a Marker type (via BiorbdModel)
return dynamic_cast<rigidbody::Markers &>(*this).marker(Q, n, true,
updateKin);
}
utils::Matrix rigidbody::Joints::projectPointJacobian(
const rigidbody::GeneralizedCoordinates &Q,
rigidbody::NodeSegment node,
bool updateKin)
{
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
if (updateKin) {
UpdateKinematicsCustom (&Q);
}
updateKin = false;
// Assuming that this is also a Marker type (via BiorbdModel)
rigidbody::Markers &marks = dynamic_cast<rigidbody::Markers &>
(*this);
// If the point has not been projected, there is no effect
if (node.nbAxesToRemove() != 0) {
// Jacobian of the marker
node.applyRT(globalJCS(node.parent()).transpose());
utils::Matrix G_tp(marks.markersJacobian(Q, node.parent(),
utils::Vector3d(0,0,0), updateKin));
utils::Matrix JCor(utils::Matrix::Zero(9,nbQ()));
CalcMatRotJacobian(Q, GetBodyId(node.parent().c_str()),
utils::Matrix3d::Identity(), JCor, updateKin);
for (unsigned int n=0; n<3; ++n)
if (node.isAxisKept(n)) {
G_tp += JCor.block(n*3,0,3,nbQ()) * node(n);
}
return G_tp;
} else {
// Return the value
return utils::Matrix::Zero(3,nbQ());
}
}
utils::Matrix rigidbody::Joints::projectPointJacobian(
const rigidbody::GeneralizedCoordinates &Q,
const utils::Vector3d &v,
int segmentIdx,
const utils::String& axesToRemove,
bool updateKin)
{
// Find the point
const rigidbody::NodeSegment& p(projectPoint(Q, v, segmentIdx,
axesToRemove, updateKin));
// Return the value
return projectPointJacobian(Q, p, updateKin);
}
std::vector<utils::Matrix>
rigidbody::Joints::projectPointJacobian(
const rigidbody::GeneralizedCoordinates &Q,
const std::vector<rigidbody::NodeSegment> &v,
bool updateKin)
{
// Gather the points
const std::vector<rigidbody::NodeSegment>& tp(projectPoint(Q, v,
updateKin));
// Calculate the Jacobian if the point is not projected
std::vector<utils::Matrix> G;
for (unsigned int i=0; i<tp.size(); ++i) {
// Actual marker
G.push_back(projectPointJacobian(Q, rigidbody::NodeSegment(v[i]),
false));
}
return G;
}
RigidBodyDynamics::Math::SpatialTransform
rigidbody::Joints::CalcBodyWorldTransformation (
const rigidbody::GeneralizedCoordinates &Q,
const unsigned int segmentIdx,
bool updateKin)
{
// update the Kinematics if necessary
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
if (updateKin) {
UpdateKinematicsCustom (&Q);
}
return CalcBodyWorldTransformation(segmentIdx);
}
RigidBodyDynamics::Math::SpatialTransform
rigidbody::Joints::CalcBodyWorldTransformation(
const unsigned int segmentIdx) const
{
if (segmentIdx >= this->fixed_body_discriminator) {
unsigned int fbody_id = segmentIdx - this->fixed_body_discriminator;
unsigned int parent_id = this->mFixedBodies[fbody_id].mMovableParent;
utils::RotoTrans parentRT(
this->X_base[parent_id].E.transpose(),
this->X_base[parent_id].r);
utils::RotoTrans bodyRT(
this->mFixedBodies[fbody_id].mParentTransform.E.transpose(),
this->mFixedBodies[fbody_id].mParentTransform.r);
const utils::RotoTrans& transfo_tp = parentRT * bodyRT;
return RigidBodyDynamics::Math::SpatialTransform (transfo_tp.rot(),
transfo_tp.trans());
}
return RigidBodyDynamics::Math::SpatialTransform (
this->X_base[segmentIdx].E.transpose(), this->X_base[segmentIdx].r);
}
// Get a segment's angular velocity
utils::Vector3d rigidbody::Joints::segmentAngularVelocity(
const rigidbody::GeneralizedCoordinates &Q,
const rigidbody::GeneralizedVelocity &Qdot,
unsigned int idx,
bool updateKin)
{
// Assuming that this is also a joint type (via BiorbdModel)
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
const utils::String& segmentName(segment(idx).name());
unsigned int id(this->GetBodyId(segmentName.c_str()));
// Calculate the velocity of the point
return RigidBodyDynamics::CalcPointVelocity6D(
*this, Q, Qdot, id, utils::Vector3d(0, 0, 0), updateKin).block(0, 0, 3, 1);
}
utils::Vector3d rigidbody::Joints::CoM(
const rigidbody::GeneralizedCoordinates &Q,
bool updateKin)
{
// For each segment, find the CoM (CoM = sum(segment_mass * pos_com_seg) / total mass)
const std::vector<rigidbody::NodeSegment>& com_segment(CoMbySegment(Q,
updateKin));
utils::Vector3d com(0, 0, 0);
for (unsigned int i=0; i<com_segment.size(); ++i) {
com += (*m_segments)[i].characteristics().mMass * com_segment[i];
}
// Divide by total mass
com = com/this->mass();
// Return the CoM
return com;
}
utils::Vector3d rigidbody::Joints::angularMomentum(
const rigidbody::GeneralizedCoordinates &Q,
const rigidbody::GeneralizedVelocity &Qdot,
bool updateKin)
{
return CalcAngularMomentum(Q, Qdot, updateKin);
}
utils::Matrix rigidbody::Joints::massMatrix (
const rigidbody::GeneralizedCoordinates &Q,
bool updateKin)
{
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
RigidBodyDynamics::Math::MatrixNd massMatrix(nbQ(), nbQ());
massMatrix.setZero();
RigidBodyDynamics::CompositeRigidBodyAlgorithm(*this, Q, massMatrix, updateKin);
return massMatrix;
}
utils::Matrix rigidbody::Joints::massMatrixInverse (
const rigidbody::GeneralizedCoordinates &Q,
bool updateKin)
{
unsigned int i = 0; // for loop purpose
unsigned int j = 0; // for loop purpose
RigidBodyDynamics::Math::MatrixNd Minv(this->dof_count, this->dof_count);
Minv.setZero();
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
if (updateKin) {
UpdateKinematicsCustom(&Q, nullptr, nullptr);
}
// First Forward Pass
for (i = 1; i < this->mBodies.size(); i++) {
this->I[i].setSpatialMatrix(this->IA[i]);
}
// End First Forward Pass
// set F (n x 6 x n)
RigidBodyDynamics::Math::MatrixNd F_i(6, this->dof_count);
F_i.setZero();
// Fill a vector of matrix (6 x n)
std::vector<RigidBodyDynamics::Math::MatrixNd> F;
for (i = 1; i < this->mBodies.size(); i++)
{
F.push_back(F_i);
}
// Backward Pass
std::vector<std::vector<unsigned int>> subTrees = getDofSubTrees();
for (i = this->mBodies.size() - 1; i > 0; i--)
{
unsigned int q_index_i = this->mJoints[i].q_index;
const std::vector<unsigned int>& sub_tree = subTrees[q_index_i];
this->U[i] = this->IA[i] * this->S[i];
this->d[i] = this->S[i].dot(this->U[i]);
Minv(q_index_i, q_index_i) = 1.0 / (this->d[i]);
for (j = 0; j < sub_tree.size(); j++) {
const RigidBodyDynamics::Math::SpatialVector& Ftemp = F[q_index_i].block(0, sub_tree[j], 6, 1);
Minv(q_index_i,sub_tree[j]) -= (1.0/this->d[i]) * this->S[i].transpose() * Ftemp;
}
unsigned int lambda = this->lambda[i];
unsigned int lambda_q_i = this->mJoints[lambda].q_index;
if (lambda != 0) {
for (j = 0; j < sub_tree.size(); j++) {
F[q_index_i].block(0, sub_tree[j], 6, 1) += this->U[i] * Minv.block(q_index_i, sub_tree[j], 1, 1);
F[lambda_q_i].block(0, sub_tree[j], 6, 1) += this->X_lambda[i].toMatrixTranspose() * F[q_index_i].block(0, sub_tree[j], 6, 1);
}
RigidBodyDynamics::Math::SpatialMatrix Ia = this->IA[i]
- this->U[i]
* (this->U[i] / this->d[i]).transpose();
#ifdef BIORBD_USE_CASADI_MATH
this->IA[lambda]
+= this->X_lambda[i].toMatrixTranspose()
* Ia * this->X_lambda[i].toMatrix();
#else
this->IA[lambda].noalias()
+= this->X_lambda[i].toMatrixTranspose()
* Ia * this->X_lambda[i].toMatrix();
#endif
}
}
// End Backward Pass
// Second Forward Pass
for (i = 1; i < this->mBodies.size(); i++) {
unsigned int q_index_i = this->mJoints[i].q_index;
unsigned int lambda = this->lambda[i];
unsigned int lambda_q_i = this->mJoints[lambda].q_index;
RigidBodyDynamics::Math::SpatialTransform X_lambda = this->X_lambda[i];
if (lambda != 0){
// Minv[i,i:] = Dinv[i]* (U[i,:].transpose() * Xmat) * F[lambda,:,i:])
for (j = q_index_i; j < this->dof_count; j++) {
// RigidBodyDynamics::Math::SpatialVector Ftemp = F[lambda_q_i].block(0, q_index_i, 6, 1);
RigidBodyDynamics::Math::SpatialVector Ftemp = F[lambda_q_i].block(0, j, 6, 1);
Minv(q_index_i, j) -=
(1.0/this->d[i]) * (this->U[i].transpose() * X_lambda.toMatrix()) * Ftemp;
}
}
// F[i,:,i:] = np.outer(S,Minv[i,i:]) // could be simplified (S * M[q_index_i,q_index_i:]^T)
for (j = q_index_i; j < this->dof_count; j++) {
F[q_index_i].block(0, j, 6, 1) = this->S[i] * Minv.block(q_index_i, j, 1, 1); // outer product
}
if (lambda != 0){
// F[i,:,i:] += Xmat.transpose() * F[lambda,:,i:]
for (j = q_index_i; j < this->dof_count; j++) {
F[q_index_i].block(0, j, 6, 1) +=
X_lambda.toMatrix() * F[lambda_q_i].block(0, j, 6, 1);
}
}
}
// End of Second Forward Pass
// Fill in full matrix (currently only upper triangular)
for (j = 0; j < this->dof_count; j++)
{
for (i = 0; i < this->dof_count; i++)
{
if (j < i) {
Minv(i, j) = Minv(j, i);
}
}
}
return Minv;
}
utils::Vector3d rigidbody::Joints::CoMdot(
const rigidbody::GeneralizedCoordinates &Q,
const rigidbody::GeneralizedVelocity &Qdot,
bool updateKin)
{
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
// For each segment, find the CoM
utils::Vector3d com_dot(0,0,0);
// CoMdot = sum(mass_seg * Jacobian * qdot)/mass totale
utils::Matrix Jac(utils::Matrix(3,this->dof_count));
for (auto segment : *m_segments) {
Jac.setZero();
RigidBodyDynamics::CalcPointJacobian(
*this, Q, GetBodyId(segment.name().c_str()),
segment.characteristics().mCenterOfMass, Jac, updateKin);
com_dot += ((Jac*Qdot) * segment.characteristics().mMass);
updateKin = false;
}
// Divide by total mass
com_dot = com_dot/mass();
// Return the velocity of CoM
return com_dot;
}
utils::Vector3d rigidbody::Joints::CoMddot(
const rigidbody::GeneralizedCoordinates &Q,
const rigidbody::GeneralizedVelocity &Qdot,
const rigidbody::GeneralizedAcceleration &Qddot,
bool updateKin)
{
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
utils::Scalar mass;
RigidBodyDynamics::Math::Vector3d com, com_ddot;
RigidBodyDynamics::Utils::CalcCenterOfMass(
*this, Q, Qdot, &Qddot, mass, com, nullptr, &com_ddot,
nullptr, nullptr, updateKin);
// Return the acceleration of CoM
return com_ddot;
}
utils::Matrix rigidbody::Joints::CoMJacobian(
const rigidbody::GeneralizedCoordinates &Q,
bool updateKin)
{
#ifdef BIORBD_USE_CASADI_MATH
updateKin = true;
#endif
// Total jacobian
utils::Matrix JacTotal(utils::Matrix::Zero(3,this->dof_count));
// CoMdot = sum(mass_seg * Jacobian * qdot)/mass total
utils::Matrix Jac(utils::Matrix::Zero(3,this->dof_count));
for (auto segment : *m_segments) {
Jac.setZero();
RigidBodyDynamics::CalcPointJacobian(
*this, Q, GetBodyId(segment.name().c_str()),
segment.characteristics().mCenterOfMass, Jac, updateKin);
JacTotal += segment.characteristics().mMass*Jac;
updateKin = false;
}