-
Notifications
You must be signed in to change notification settings - Fork 5
/
tiny_multi_body.h
3048 lines (2643 loc) · 101 KB
/
tiny_multi_body.h
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
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TINY_MULTIBODY_H
#define TINY_MULTIBODY_H
/// TinyMultiBody follows the notation from the Featherstone Book,
/// Rigid Body Dynamics Algorithms, 2008. This Forward Dynamics implementation
/// of Articulated Body Algorithm is strongly influenced by the RBDL library.
/// By Martin L. Felis. https://rbdl.bitbucket.io
#include <string>
#include <vector>
#include "tiny_actuator.h"
#include "tiny_geometry.h"
#include "tiny_matrix_x.h"
#include "tiny_spatial_motion_vector.h"
#include "tiny_spatial_transform.h"
#include "tiny_symmetric_spatial_dyad.h"
#include "tiny_metrics.h"
enum TinyIntegrationType { INT_EULER, INT_EULER_SYMPLECTIC };
template <typename TinyScalar, typename TinyConstants>
class TinyLink {
typedef ::TinySpatialTransform<TinyScalar, TinyConstants>
TinySpatialTransform;
typedef ::TinyVector3<TinyScalar, TinyConstants> TinyVector3;
typedef ::TinyMatrix3x3<TinyScalar, TinyConstants> TinyMatrix3x3;
typedef ::TinySpatialMotionVector<TinyScalar, TinyConstants>
TinySpatialMotionVector;
typedef ::TinySymmetricSpatialDyad<TinyScalar, TinyConstants>
TinySymmetricSpatialDyad;
typedef ::TinyVectorX<TinyScalar, TinyConstants> TinyVectorX;
public:
TinyLink() = default;
TinyLink(TinyJointType joint_type, TinySpatialTransform& parent_link_to_joint,
const TinySymmetricSpatialDyad& inertia)
: m_joint_type(joint_type), m_X_T(parent_link_to_joint), m_I(inertia) {}
TinySpatialTransform m_X_T, adjm_RX_T; // parent_link_to_joint
TinySpatialTransform m_X_J, adjm_RX_J; // joint_to_child_link //depends on q
TinySpatialTransform m_X_parent2, adjm_RX_parent2; // parent_link_to_child_link
TinyScalar m_tau, adjm_Rtau;
TinyJointType m_joint_type{JOINT_REVOLUTE_Z};
TinySpatialTransform m_X_world, adjm_RX_world; // world_to_link
TinySpatialMotionVector
m_vJ, adjm_RvJ; // local joint velocity (relative to parent link)
TinySpatialMotionVector m_v, adjm_Rv; // global joint velocity (relative to world)
TinySpatialMotionVector m_a, adjm_Ra; // acceleration (relative to world)
TinySpatialMotionVector m_c, adjm_Rc; // velocity product acceleration
TinySymmetricSpatialDyad
m_I, adjm_RI; // local spatial inertia (constant) // TODO replace by its original
// terms (COM, gyration etc.)
TinySymmetricSpatialDyad m_IA, adjm_RIA; // spatial articulated inertia, IC in CRBA
TinySymmetricSpatialDyad m_Ia, adjm_RIa;
TinySpatialMotionVector m_pa, adjm_Rpa;
TinySpatialMotionVector m_pA, adjm_RpA; // bias forces or zero-acceleration forces
TinySpatialMotionVector m_S, adjm_RS; // motion subspace (spatial joint axis/matrix)
TinySpatialMotionVector m_ap, adjm_Rap;// a prime
TinySpatialMotionVector m_U, adjm_RU; // temp var in ABA, page 130
TinyScalar m_d; // temp var in ABA, page 130
TinyScalar m_u, adjm_Ru; // temp var in ABA, page 130
TinyScalar m_invd, adjm_RD;
TinySpatialMotionVector m_f; // temp var in RNEA, page 183
TinySpatialMotionVector
m_f_ext; // user-defined external force in world frame
// These two variables are managed by TinyMultiBody and should not be changed.
int m_parent_index{-1}; // index of parent link in TinyMultiBody
int m_index{-1}; // index of this link in TinyMultiBody
std::vector<const TinyGeometry<TinyScalar, TinyConstants>*>
m_collision_geometries;
std::vector<TinySpatialTransform>
m_X_collisions, adjm_RX_collisions; // offset of collision geometries (relative to this link
// frame)
std::vector<int> m_visual_uids1;
std::vector<int> m_visual_uids2;
std::vector<TinySpatialTransform>
m_X_visuals; // offset of geometry (relative to this link frame)
std::string m_link_name;
std::string m_joint_name;
// index in MultiBody q / qd arrays
int m_q_index{-2};
int m_qd_index{-2};
TinyScalar m_stiffness{TinyConstants::zero()};
TinyScalar m_damping{TinyConstants::zero()};
TinyScalar adjm_Rstiffness{TinyConstants::zero()};
TinyScalar adjm_Rdamping{TinyConstants::zero()};
void set_joint_type(TinyJointType type,
const TinyVector3& axis = TinyVector3::makeUnitX()) {
m_joint_type = type;
m_S.set_zero();
switch (m_joint_type) {
case JOINT_PRISMATIC_X:
m_S.m_bottomVec.setX(TinyConstants::one());
break;
case JOINT_PRISMATIC_Y:
m_S.m_bottomVec.setY(TinyConstants::one());
break;
case JOINT_PRISMATIC_Z:
m_S.m_bottomVec.setZ(TinyConstants::one());
break;
case JOINT_PRISMATIC_AXIS:
m_S.m_bottomVec = axis.normalized();
break;
case JOINT_REVOLUTE_X:
m_S.m_topVec.setX(TinyConstants::one());
break;
case JOINT_REVOLUTE_Y:
m_S.m_topVec.setY(TinyConstants::one());
break;
case JOINT_REVOLUTE_Z:
m_S.m_topVec.setZ(TinyConstants::one());
break;
case JOINT_REVOLUTE_AXIS:
m_S.m_topVec = axis.normalized();
break;
case JOINT_FIXED:
break;
default:
fprintf(stderr,
"Error: Unknown joint type encountered in " __FILE__ ":%i\n",
__LINE__);
}
}
void adj_jcalc_v(const TinySpatialMotionVector& RvJ,
const TinySpatialMotionVector& m_vJ,
const TinyScalar& qd,
TinyScalar& Rqd) {
// q and qd
if (m_joint_type == JOINT_FIXED)
return;
// jcalc(qd, &m_vJ);
switch (m_joint_type) {
case JOINT_PRISMATIC_X: {
Rqd += RvJ.m_bottomVec.getX();
break;
}
case JOINT_PRISMATIC_Y: {
Rqd += RvJ.m_bottomVec.getY();
break;
}
case JOINT_PRISMATIC_Z: {
Rqd += RvJ.m_bottomVec.getZ();
break;
}
case JOINT_PRISMATIC_AXIS: {
const TinyVector3& axis = m_S.m_bottomVec;
Rqd += RvJ.m_bottomVec.dot(axis);
break;
}
case JOINT_REVOLUTE_X: { // TODO set rotation
Rqd += RvJ.m_topVec.getX();
break;
}
case JOINT_REVOLUTE_Y: {
Rqd += RvJ.m_topVec.getY();
break;
}
case JOINT_REVOLUTE_Z: {
Rqd += RvJ.m_topVec.getZ();
break;
}
case JOINT_REVOLUTE_AXIS: {
const TinyVector3& axis = m_S.m_topVec;
Rqd += RvJ.m_topVec.dot(axis);
break;
}
case JOINT_FIXED:
break;
default:
fprintf(stderr,
"Error: Unknown joint type encountered in " __FILE__ ":%i\n",
__LINE__);
}
}
void adj_jcalc_x( TinySpatialTransform& RX_J,
TinySpatialTransform& RX_parent,
TinySpatialTransform& X_J,
TinySpatialTransform& X_parent,
const TinyScalar q,
TinyScalar& Rq ) {
m_X_T.adj_st_multiply(RX_parent, X_J, adjm_RX_T, RX_J);
RX_parent.set_zero();
if (m_joint_type == JOINT_FIXED)
return;
switch (m_joint_type) {
case JOINT_PRISMATIC_X:
Rq += RX_J.m_translation.getX();
break;
case JOINT_PRISMATIC_Y:
Rq += RX_J.m_translation.getY();
break;
case JOINT_PRISMATIC_Z:
Rq += RX_J.m_translation.getZ();
break;
case JOINT_PRISMATIC_AXIS: {
const TinyVector3& axis = m_S.m_bottomVec;
Rq += RX_J.m_translation.dot(axis);
break;
}
case JOINT_REVOLUTE_X: {
Rq += RX_J.m_rotation.adj_set_rotation_x(q);
break;
}
case JOINT_REVOLUTE_Y:
Rq += RX_J.m_rotation.adj_set_rotation_y(q);
break;
case JOINT_REVOLUTE_Z:
Rq += RX_J.m_rotation.adj_set_rotation_z(q);
break;
case JOINT_REVOLUTE_AXIS: {
const TinyVector3& axis = m_S.m_topVec;
TinyQuaternion<TinyScalar, TinyConstants> orn, Rorn;
orn.setRotation(axis, q);
RX_J.m_rotation.adj_setRotation(orn, Rorn);
Rorn.adj_setRotation(axis, q, Rq);
break;
}
case JOINT_FIXED:
break;
default:
fprintf(stderr,
"Error: Unknown joint type encountered in " __FILE__ ":%i\n",
__LINE__);
}
RX_J.set_zero();
}
void jcalc(TinyScalar q, TinySpatialTransform* X_J,
TinySpatialTransform* X_parent) const {
X_J->set_identity();
X_parent->set_identity();
switch (m_joint_type) {
case JOINT_PRISMATIC_X:
X_J->m_translation.setX(q);
break;
case JOINT_PRISMATIC_Y:
X_J->m_translation.setY(q);
break;
case JOINT_PRISMATIC_Z:
X_J->m_translation.setZ(q);
break;
case JOINT_PRISMATIC_AXIS: {
const TinyVector3& axis = m_S.m_bottomVec;
X_J->m_translation = axis * q;
break;
}
case JOINT_REVOLUTE_X:
X_J->m_rotation.set_rotation_x(q);
break;
case JOINT_REVOLUTE_Y:
X_J->m_rotation.set_rotation_y(q);
break;
case JOINT_REVOLUTE_Z:
X_J->m_rotation.set_rotation_z(q);
break;
case JOINT_REVOLUTE_AXIS: {
const TinyVector3& axis = m_S.m_topVec;
TinyQuaternion<TinyScalar, TinyConstants> orn;
orn.setRotation(axis, q);
X_J->m_rotation.setRotation(orn);
break;
}
case JOINT_FIXED:
// TinySpatialTransform is set to identity in its constructor already
// and never changes.
break;
default:
fprintf(stderr,
"Error: Unknown joint type encountered in " __FILE__ ":%i\n",
__LINE__);
}
*X_parent = m_X_T * (*X_J);
}
void adj_RX_J() {
m_X_T.adj_st_multiply(adjm_RX_parent2, m_X_J,
adjm_RX_T, adjm_RX_J);
}
inline void jcalc(TinyScalar qd, TinySpatialMotionVector* v_J) const {
switch (m_joint_type) {
case JOINT_PRISMATIC_X:
v_J->m_bottomVec.setX(qd);
break;
case JOINT_PRISMATIC_Y:
v_J->m_bottomVec.setY(qd);
break;
case JOINT_PRISMATIC_Z:
v_J->m_bottomVec.setZ(qd);
break;
case JOINT_PRISMATIC_AXIS: {
const TinyVector3& axis = m_S.m_bottomVec;
v_J->m_bottomVec = axis * qd;
break;
}
case JOINT_REVOLUTE_X:
v_J->m_topVec.setX(qd);
break;
case JOINT_REVOLUTE_Y:
v_J->m_topVec.setY(qd);
break;
case JOINT_REVOLUTE_Z:
v_J->m_topVec.setZ(qd);
break;
case JOINT_REVOLUTE_AXIS: {
const TinyVector3& axis = m_S.m_topVec;
v_J->m_topVec = axis * qd;
break;
}
case JOINT_FIXED:
break;
default:
fprintf(stderr,
"Error: Unknown joint type encountered in " __FILE__ ":%i\n",
__LINE__);
}
}
void adj_Rjcalc(const std::vector<TinyScalar>& _m_q,
const std::vector<TinyScalar>& _m_qd,
TinyVectorX& _adjm_Rq,
TinyVectorX& _adjm_Rqd) {
// q and qd
if (m_joint_type == JOINT_FIXED)
return;
// jcalc(qd, &m_vJ);
switch (m_joint_type) {
case JOINT_PRISMATIC_X: {
_adjm_Rqd[m_qd_index] += adjm_RvJ.m_bottomVec.getX();
_adjm_Rq[m_q_index] += adjm_RX_J.m_translation.getX();
break;
}
case JOINT_PRISMATIC_Y: {
_adjm_Rqd[m_qd_index] += adjm_RvJ.m_bottomVec.getY();
_adjm_Rq[m_q_index] += adjm_RX_J.m_translation.getY();
break;
}
case JOINT_PRISMATIC_Z: {
_adjm_Rqd[m_qd_index] += adjm_RvJ.m_bottomVec.getZ();
_adjm_Rq[m_q_index] += adjm_RX_J.m_translation.getZ();
break;
}
case JOINT_PRISMATIC_AXIS: {
const TinyVector3& axis = m_S.m_bottomVec;
_adjm_Rqd[m_qd_index] += adjm_RvJ.m_bottomVec.dot(axis);
_adjm_Rq[m_q_index] += adjm_RX_J.m_translation.dot(axis);
break;
}
case JOINT_REVOLUTE_X: { // TODO set rotation
_adjm_Rqd[m_qd_index] += adjm_RvJ.m_topVec.getX();
_adjm_Rq[m_q_index] +=
adjm_RX_J.m_rotation.adj_set_rotation_x(_m_q[m_q_index]);
break;
}
case JOINT_REVOLUTE_Y: {
_adjm_Rqd[m_qd_index] += adjm_RvJ.m_topVec.getY();
_adjm_Rq[m_q_index] +=
adjm_RX_J.m_rotation.adj_set_rotation_y(_m_q[m_q_index]);
break;
}
case JOINT_REVOLUTE_Z: {
_adjm_Rqd[m_qd_index] += adjm_RvJ.m_topVec.getZ();
_adjm_Rq[m_q_index] +=
adjm_RX_J.m_rotation.adj_set_rotation_z(_m_q[m_q_index]);
break;
}
case JOINT_REVOLUTE_AXIS: {
const TinyVector3& axis = m_S.m_topVec;
_adjm_Rqd[m_qd_index] += adjm_RvJ.m_topVec.dot(axis);
TinyQuaternion<TinyScalar, TinyConstants> orn, Rorn;
orn.setRotation(axis, _m_q[m_q_index]);
adjm_RX_J.m_rotation.adj_setRotation(orn, Rorn);
Rorn.adj_setRotation(axis, _m_q[m_q_index], _adjm_Rq[m_q_index]);
break;
}
case JOINT_FIXED:
break;
default:
fprintf(stderr,
"Error: Unknown joint type encountered in " __FILE__ ":%i\n",
__LINE__);
}
}
inline void jcalc(TinyScalar q) { jcalc(q, &m_X_J, &m_X_parent2); }
inline void jcalc1(TinyScalar q) { jcalc(q, &m_X_J, &m_X_parent2); }
inline void adj_jcalc(const TinyScalar& q,
TinyScalar& Rq,
const TinyScalar& qd,
TinyScalar& Rqd) {
adj_jcalc_v(adjm_RvJ, m_vJ, qd, Rqd);
adj_jcalc_x(adjm_RX_J, adjm_RX_parent2, m_X_J, m_X_parent2, q, Rq);
}
inline void jcalc(TinyScalar q, TinyScalar qd) {
jcalc(q);
jcalc(qd, &m_vJ);
}
};
template <typename TinyScalar, typename TinyConstants>
class TinyMultiBody {
typedef ::TinyLink<TinyScalar, TinyConstants> TinyLink;
typedef ::TinySpatialMotionVector<TinyScalar, TinyConstants>
TinySpatialMotionVector;
typedef ::TinyVector3<TinyScalar, TinyConstants> TinyVector3;
typedef ::TinyVectorX<TinyScalar, TinyConstants> TinyVectorX;
typedef ::TinyQuaternion<TinyScalar, TinyConstants> TinyQuaternion;
typedef ::TinySymmetricSpatialDyad<TinyScalar, TinyConstants>
TinySymmetricSpatialDyad;
typedef ::TinySpatialTransform<TinyScalar, TinyConstants>
TinySpatialTransform;
typedef ::TinyMatrix3x3<TinyScalar, TinyConstants> TinyMatrix3x3;
typedef ::TinyMatrix3xX<TinyScalar, TinyConstants> TinyMatrix3xX;
typedef ::TinyMatrix6xX<TinyScalar, TinyConstants> TinyMatrix6xX;
typedef ::TinyMatrixXxX<TinyScalar, TinyConstants> TinyMatrixXxX;
typedef ::TinyActuator<TinyScalar, TinyConstants> TinyActuator;
typedef ::TinyMetric<TinyScalar, TinyConstants> TinyMetric;
public:
std::vector<TinyLink> m_links;
TinyIntegrationType m_integration_type{INT_EULER_SYMPLECTIC};
/**
* Number of degrees of freedom, excluding floating-base coordinates.
*/
int m_dof{0};
/**
* Optionally defined actuator for this link that computes the dynamics of
* the control input and computes the joint torque.
* Memory is managed by TinyLink.
*/
TinyActuator* m_actuator{nullptr};
/**
* Dimensionality of joint positions q (including 7-DoF floating-base
* coordinates if this system is floating-base).
*/
int dof() const { return m_isFloating ? m_dof + 7 : m_dof; }
/**
* Dimensionality of joint velocities qd and accelerations qdd (including
* 6-DoF base velocity and acceleration, if this system is floating-base).
*/
int dof_qd() const { return m_isFloating ? m_dof + 6 : m_dof; }
int dof_state() const { return dof_qd() + dof(); }
int dof_u() const {return adjm_dof_u;}
/**
* Indices in `m_tau` that are controllable, i.e. actuated.
* For floating-base system, the index 0 corresponds to the first degree of
* freedom not part of the 6D floating-base coordinates.
*/
std::vector<int> m_control_indices;
/**
* Dimensionality of control input, i.e. number of actuated DOFs.
*/
int dof_actuated() const {
return static_cast<int>(m_control_indices.size());
}
/**
* Whether this system is floating or fixed to the world frame.
*/
bool m_isFloating{false};
// quantities related to adjoint methods
TinySpatialMotionVector adjm_spatial_gravity, adjm_Rspatial_gravity;
std::vector<TinyScalar> adjm_q, adjm_qd, adjm_qdd, adjm_tau;
TinyVectorX adjm_global_R;
int adjm_global_i;
std::vector<TinyScalar> adjm_global_ax, adjm_global_ay;
// ckpt
std::vector<std::vector<TinyScalar>> adjm_ckpt_state,
adjm_ckpt_state_cons;
std::vector<TinyVectorX> adjm_ckpt_u;
std::vector<TinyScalar> adjm_ckpt_dt;
std::vector<TinyScalar> adjm_this_state, adjm_next_state, adjm_u;
std::vector<TinyVectorX> adjm_Rx;
int adjm_dof_u = 0;
TinyVectorX adjm_Rq, adjm_Rqd, adjm_Rqdd, adjm_Rtau;
// quantities related to floating base
TinySpatialMotionVector m_baseVelocity, adjm_RbaseVelocity, adjm_baseVelocity_in_fk; // v_0
TinySpatialMotionVector m_baseAcceleration, adjm_RbaseAcceleration, adj_record_baseAcceleration; // a_0
TinySpatialMotionVector m_baseAppliedForce, adjm_RbaseAppliedForce; // f_ext_0 in world frame
TinySpatialMotionVector m_baseForce; // f_0 (used by RNEA)
TinySpatialMotionVector m_baseBiasForce, adjm_RbaseBiasForce; // pA_0
TinySymmetricSpatialDyad m_baseInertia, adjm_RbaseInertial; // I_0
TinySymmetricSpatialDyad m_baseArticulatedInertia, adjm_RbaseArticulatedInertia; // IA_0
TinySpatialTransform m_base_X_world, adjm_Rbase_X_world;
std::vector<int> m_visual_uids1;
std::vector<int> m_visual_uids2;
std::vector<TinySpatialTransform>
m_X_visuals; // offset of geometry (relative to the base frame)
std::vector<const TinyGeometry<TinyScalar, TinyConstants>*>
m_collision_geometries;
std::vector<TinySpatialTransform>
m_X_collisions, adjm_RX_collisions; // offset of collision geometries (relative to this link
// frame)
std::vector<TinyScalar> m_q, m_qd, m_qdd, m_tau;
TinySubmitProfileTiming m_profileTimingFunc;
explicit TinyMultiBody(bool isFloating = false)
: m_isFloating(isFloating), m_profileTimingFunc(nullptr) {}
inline void submitProfileTiming(const std::string& name) {
if (m_profileTimingFunc) {
m_profileTimingFunc(name);
}
}
/**
* Set 3D base position in world coordinates.
*/
void set_position(const TinyVector3& initial_position) {
m_base_X_world.m_translation.setValue(
initial_position[0], initial_position[1], initial_position[2]);
if (m_isFloating) {
m_q[4] = initial_position[0];
m_q[5] = initial_position[1];
m_q[6] = initial_position[2];
}
}
/**
* Ensures that the joint coordinates q, qd, qdd, tau are initialized
* properly in the MultiBody member variables.
*/
void initialize() {
// make sure m_dof and the q / qd indices in the links are accurate
int q_index = m_isFloating ? 7 : 0;
int qd_index = m_isFloating ? 6 : 0;
m_dof = 0; // excludes floating-base DOF
for (TinyLink& link : m_links) {
assert(link.m_index >= 0);
link.m_q_index = q_index;
link.m_qd_index = qd_index;
if (link.m_joint_type != JOINT_FIXED) {
++q_index;
++qd_index;
++m_dof;
} else {
link.m_q_index = -2;
link.m_qd_index = -2;
}
}
if (static_cast<int>(m_q.size()) != dof()) {
m_q.resize(dof(), TinyConstants::zero());
}
for (TinyScalar& v : m_q) {
v = TinyConstants::zero();
}
if (m_isFloating) {
m_q[3] = TinyConstants::one(); // make sure orientation is valid
}
if (static_cast<int>(m_qd.size()) != dof_qd()) {
m_qd.resize(dof_qd(), TinyConstants::zero());
}
for (TinyScalar& v : m_qd) {
v = TinyConstants::zero();
}
if (static_cast<int>(m_qdd.size()) != dof_qd()) {
m_qdd.resize(dof_qd(), TinyConstants::zero());
}
for (TinyScalar& v : m_qdd) {
v = TinyConstants::zero();
}
if (static_cast<int>(m_tau.size()) != m_dof) {
m_tau.resize(m_dof, TinyConstants::zero());
}
for (TinyScalar& v : m_tau) {
v = TinyConstants::zero();
}
// (Re-)create actuator to make sure it has the right degrees of freedom.
if (m_actuator) {
delete m_actuator;
m_actuator = new TinyActuator(dof_actuated());
}
}
/**
* Copy constructor. Skips visualization members, temporary variables.
* The actuator is not copied, but the original pointer `m_actuator` is
* carried over.
*/
template <typename Scalar, typename Utils>
TinyMultiBody(const TinyMultiBody<Scalar, Utils>& mb)
: m_links(mb.m_links),
m_integration_type(mb.m_integration_type),
m_dof(mb.m_dof),
m_actuator(mb.m_actuator),
m_control_indices(mb.m_control_indices),
m_isFloating(mb.m_isFloating),
m_baseVelocity(mb.m_baseVelocity),
m_baseAcceleration(mb.m_baseAcceleration),
m_baseAppliedForce(mb.m_baseAppliedForce),
m_baseForce(mb.m_baseForce),
m_baseBiasForce(mb.m_baseBiasForce),
m_baseInertia(mb.m_baseInertia),
m_base_X_world(mb.m_base_X_world),
m_collision_geometries(mb.m_collision_geometries),
m_X_collisions(mb.m_X_collisions),
m_q(mb.m_q),
m_qd(mb.m_qd),
m_qdd(mb.m_qdd),
m_tau(mb.m_tau) {
// TODO implement type conversion
static_assert(std::is_same<Scalar, TinyScalar>::value,
"Copy constructor for TinyMultiBody of different scalar type "
"not yet implemented.");
}
virtual ~TinyMultiBody() {
if (m_actuator) {
delete m_actuator;
}
}
void print_state() const {
printf("q: [");
for (int i = 0; i < dof(); ++i) {
if (i > 0) printf(" ");
printf("%.2f", TinyConstants::getDouble(m_q[i]));
}
printf("] \tqd: [");
for (int i = 0; i < dof_qd(); ++i) {
if (i > 0) printf(" ");
printf("%.2f", TinyConstants::getDouble(m_qd[i]));
}
printf("] \tqdd: [");
for (int i = 0; i < dof_qd(); ++i) {
if (i > 0) printf(" ");
printf("%.2f", TinyConstants::getDouble(m_qdd[i]));
}
printf("] \ttau: [");
for (int i = 0; i < m_dof; ++i) {
if (i > 0) printf(" ");
printf("%.2f", TinyConstants::getDouble(m_tau[i]));
}
printf("]\n");
}
const TinySpatialTransform& get_world_transform(int link) const {
if (link == -1) {
return m_base_X_world;
} else {
return m_links[link].m_X_world;
}
}
TinySpatialTransform& adj_get_world_transform(int link) {
if (link == -1) {
return adjm_Rbase_X_world;
} else {
return m_links[link].adjm_RX_world;
}
}
/**
* Compute center of mass of link in world coordinates.
* @param link Index of link in `m_links`.
* @return 3D coordinates of center of mass in world coordinates.
*/
const TinyVector3 get_world_com(int link) const {
const TinySpatialTransform& tf = get_world_transform(link);
if (link == -1) {
return tf.apply(m_baseInertia.m_center_of_mass);
} else {
return tf.apply(m_links[link].m_I.m_center_of_mass);
}
}
void adj_get_world_com(int link, TinyVector3& R) {
const TinySpatialTransform& tf = get_world_transform(link);
TinySpatialTransform& Rtf = adj_get_world_transform(link);
TinyVector3 Rpoint;
Rpoint.set_zero();
if (link == -1) {
tf.adj_apply(R, m_baseInertia.m_center_of_mass, Rtf, Rpoint);
} else {
tf.adj_apply(R, m_links[link].m_I.m_center_of_mass, Rtf, Rpoint);
}
}
std::vector<const TinyGeometry<TinyScalar, TinyConstants>*>&
get_collision_geometries(int i) {
if (i == -1) {
return m_collision_geometries;
} else {
return m_links[i].m_collision_geometries;
}
}
std::vector<TinySpatialTransform>& get_collision_transforms(int i) {
if (i == -1) {
return m_X_collisions;
} else {
return m_links[i].m_X_collisions;
}
}
std::vector<TinySpatialTransform>& adj_get_collision_transforms(int i) {
if (i == -1) {
return adjm_RX_collisions;
} else {
return m_links[i].adjm_RX_collisions;
}
}
static std::string joint_type_name(TinyJointType t) {
static std::string names[] = {
"JOINT_FIXED", "JOINT_PRISMATIC_X", "JOINT_PRISMATIC_Y",
"JOINT_PRISMATIC_Z", "JOINT_PRISMATIC_AXIS", "JOINT_REVOLUTE_X",
"JOINT_REVOLUTE_Y", "JOINT_REVOLUTE_Z", "JOINT_REVOLUTE_AXIS",
};
return names[int(t) + 1];
}
// attaches a new link, setting parent to the last link
void attach(TinyLink& link, bool is_controllable = true) {
if (m_links.empty())
link.m_parent_index = -1;
else
link.m_parent_index = m_links.size() - 1;
link.m_index = m_links.size();
if (link.m_joint_type != JOINT_FIXED) {
link.m_q_index = dof();
link.m_qd_index = dof_qd();
m_dof++;
if (is_controllable) {
if (m_control_indices.empty()) {
m_control_indices.push_back(0);
} else {
m_control_indices.push_back(m_control_indices.back() + 1);
}
}
} else {
link.m_q_index = -2;
link.m_qd_index = -2;
}
#ifdef DEBUG
printf(
"Attached link %i of type %s (parent: %i, index q: %i, index qd: "
"%i).\n",
link.m_index, joint_type_name(link.m_joint_type).c_str(),
link.m_parent_index, link.m_q_index, link.m_qd_index);
// link.m_S.print("joint.S");
#endif
m_links.push_back(link);
}
void attach_link(TinyLink& link, int parent_index,
bool is_controllable = true) {
attach(link, parent_index, is_controllable);
}
void attach(TinyLink& link, int parent_index, bool is_controllable = true) {
int sz = m_links.size();
assert(parent_index < sz);
link.m_index = sz;
link.m_parent_index = parent_index;
if (link.m_joint_type != JOINT_FIXED) {
link.m_q_index = dof();
link.m_qd_index = dof_qd();
m_dof++;
if (is_controllable) {
if (m_control_indices.empty()) {
m_control_indices.push_back(0);
} else {
m_control_indices.push_back(m_control_indices.back() + 1);
}
}
} else {
link.m_q_index = -2;
link.m_qd_index = -2;
}
#ifdef DEBUG
printf(
"Attached link %i of type %s (parent: %i, index q: %i, index qd: "
"%i).\n",
link.m_index, joint_type_name(link.m_joint_type).c_str(),
link.m_parent_index, link.m_q_index, link.m_qd_index);
// link.m_S.print("joint.S");
#endif
m_links.push_back(link);
}
inline TinyScalar get_q_for_link(const std::vector<TinyScalar>& q,
int link_index) const {
if (q.empty()) return TinyConstants::zero();
const TinyLink& link = m_links[link_index];
return link.m_joint_type == JOINT_FIXED ? TinyConstants::zero()
: q[link.m_q_index];
}
inline void adj_get_q_for_link(const TinyScalar& R,
TinyVectorX& Rq,
int link_index) const {
if (Rq.m_size == 0) return;
const TinyLink& link = m_links[link_index];
if (link.m_joint_type != JOINT_FIXED)
Rq[link.m_q_index] += R;
}
inline TinyScalar get_q_for_link(int link_index) const {
get_q_for_link(m_q, link_index);
}
inline void adj_get_qd_for_link(const TinyScalar& R,
TinyVectorX& Rqd,
int link_index) const {
if (Rqd.m_size == 0) return;
const TinyLink& link = m_links[link_index];
if (link.m_joint_type != JOINT_FIXED)
Rqd[link.m_qd_index] += R;
}
inline TinyScalar get_qd_for_link(const std::vector<TinyScalar>& qd,
int link_index) const {
if (qd.empty()) return TinyConstants::zero();
const TinyLink& link = m_links[link_index];
return link.m_joint_type == JOINT_FIXED ? TinyConstants::zero()
: qd[link.m_qd_index];
}
inline TinyScalar get_qd_for_link(int link_index) const {
return get_qd_for_link(m_qd, link_index);
}
inline TinyScalar get_qdd_for_link(const std::vector<TinyScalar>& qdd,
int link_index) const {
return get_qd_for_link(qdd, link_index);
}
inline TinyScalar get_qdd_for_link(int link_index) const {
return get_qdd_for_link(m_qdd, link_index);
}
inline TinyScalar get_tau_for_link(const std::vector<TinyScalar>& tau,
int link_index) const {
if (tau.empty()) return TinyConstants::zero();
const TinyLink& link = m_links[link_index];
int offset = m_isFloating ? -6 : 0;
// if (link.m_joint_type != JOINT_FIXED)
// printf("%.3lf %s\n",TinyConstants::getDouble(tau[link.m_qd_index + offset]), link.m_joint_name.c_str());
return link.m_joint_type == JOINT_FIXED ? TinyConstants::zero()
: tau[link.m_qd_index + offset];
}
inline void adj_get_tau_for_link(TinyLink& l) {
if (m_tau.empty()) return;
int offset = m_isFloating ? -6 : 0;
if (l.m_joint_type != JOINT_FIXED) {
adjm_Rtau[l.m_qd_index + offset] += l.adjm_Rtau;
}
}
inline TinyScalar get_tau_for_link(int link_index) const {
return get_tau_for_link(m_tau, link_index);
}
/**
* Set joint torques and external forces in all links and the base to zero.
*/
void clear_forces() {
m_baseAppliedForce.set_zero();
for (TinyLink& link : m_links) {
link.m_f_ext.set_zero();
}
for (int i = 0; i < m_dof; ++i) {
m_tau[i] = TinyConstants::zero();
}
}
void adj_fk(TinyVectorX& Rq,
TinyVectorX& Rqd,
TinyVectorX& Rqdd,
const std::vector<TinyScalar>& q,
const std::vector<TinyScalar>& qd = std::vector<TinyScalar>(),
const std::vector<TinyScalar>& qdd = std::vector<TinyScalar>()) {
assert(q.size() == dof());
assert(qd.empty() || qd.size() == dof_qd());
assert(qdd.empty() || qdd.size() == dof_qd());
// adjm_Rbase_X_world.print("adjm_Rbase_X_world 0");
// m_base_X_world.print("m_base_X_world ===");
if (m_isFloating) {
// update base-world transform from q, and update base velocity from qd
m_base_X_world.m_rotation.setRotation(
TinyQuaternion(q[0], q[1], q[2], q[3]));
m_base_X_world.m_translation.setValue(q[4], q[5], q[6]);
if (!qd.empty()) {
m_baseVelocity.m_topVec = TinyVector3(qd[0], qd[1], qd[2]);
m_baseVelocity.m_bottomVec = TinyVector3(qd[3], qd[4], qd[5]);
} else {
m_baseVelocity.set_zero();
}
TinySpatialMotionVector I0_mul_v0 = m_baseInertia.mul_org(m_baseVelocity);
m_baseBiasForce = m_baseVelocity.crossf(I0_mul_v0) - m_baseAppliedForce;
m_baseArticulatedInertia = m_baseInertia;
}
for (int i = m_links.size() - 1; i >= 0; i--) {
TinyLink& l = m_links[i];
int parent = l.m_parent_index;
// l.m_I.adj_sd_mul_inv(l.adjm_Rf, l.m_a, l.adjm_RI, l.adjm_Ra);
// l.adjm_RpA += l.adjmRf;
if (!qdd.empty())
; //TODO l.adjm_RS += l.adjm_Ra *
const TinySpatialMotionVector& parent_a =
parent >= 0 ? m_links[parent].m_a : m_baseAcceleration;
TinySpatialMotionVector Rparent_a;
Rparent_a.set_zero();
// if (i==1 || i ==10) {
// std::cout << i << " parent " << parent << "\n";
// l.adjm_RX_parent2.print("1 ---------- adjm_RX_parent1");
// l.adjm_Ra.print("l.adjm_Ra");
// }
l.m_X_parent2.adj_st_apply(l.adjm_Ra, parent_a, l.adjm_RX_parent2,
Rparent_a);
// if (i==1) {
// l.adjm_RX_parent2.print("1 ---------- adjm_RX_parent2");
// }
TinySpatialMotionVector Rv_x_vJ;
Rv_x_vJ.set_zero();
Rv_x_vJ += l.adjm_Ra;
if (parent > 0)
m_links[parent].adjm_Ra += Rparent_a;