/
synthesis.c
2624 lines (2242 loc) · 72.9 KB
/
synthesis.c
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
/*
* ArtTreeKS: Finite dimensional kinematic synthesis for articulated trees
* Copyright(C) 2010-2012 Edgar Simo-Serra <esimo@iri.upc.edu>
* License: see synthesis.h
*/
#include "synthesis.h"
#include <math.h>
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
#include <dq/vec3.h>
#include "mem.h"
#include "kin_misc.h"
#define PF "%.18le"
/*
* Prototypes.
*/
/* Branches. */
static int syn_construct_branches_count( synthesis_t *syn, kin_branch_iter_t *branch, void *data );
static int syn_construct_branches_walk( synthesis_t *syn, kin_branch_iter_t *branch, void *data );
static int syn_branch_iter_walk( synthesis_t *syn,
int (*func)(synthesis_t*, kin_branch_iter_t*, void*),
void *data, kin_branch_iter_t *head, kin_branch_iter_t *last );
/* Joints. */
static void kin_joint_data_free( kin_joint_data_t *data );
static void kin_joint_data_dup( kin_joint_data_t *dest, const kin_joint_data_t *src );
static int kin_joint_data_claim( synthesis_t *syn, kin_joint_data_t *data, const char *name );
/* Saving. */
static int kin_obj_save( const kin_object_t *obj, FILE *stream, int depth, const char *sym );
/* Finalization. */
static int kin_obj_fin( synthesis_t *syn, kin_object_t *obj );
static int syn_joint_add( synthesis_t *parent, kin_joint_t *joint );
static int syn_tcp_add( synthesis_t *parent, kin_object_t *tcp );
/* Claims. */
static kin_claim_t* syn_claim_add( kin_claim_t *claim,
size_t len, size_t indep, double *v, double *lb, double *ub, const char *name );
static void syn_claim_destroy( kin_claim_t *claim );
static int syn_map_claim_to_vec( const kin_claim_t *par, int *n, int *ni,
double *v, double *lb, double *ub );
static kin_claim_t* syn_claim_x( synthesis_t *syn, size_t len, size_t indep,
double *v, double *lb, double *ub, const char *name );
static kin_claim_t* syn_claim_fvec( synthesis_t *parent, size_t len, size_t indep,
double *v, const char *name );
static int syn_set_bounds( synthesis_t *syn );
/* Function voodoo. */
static int kin_obj_chain_free( kin_object_t *obj );
static int kin_obj_chain_dup( const kin_object_t *obj, kin_object_t *newobj );
static int kin_obj_chain_fin( kin_object_t *obj, synthesis_t *syn );
static int kin_obj_chain_save( const kin_object_t *obj, FILE *stream, const char *self );
static int kin_obj_tcp_free( kin_object_t *obj );
static int kin_obj_tcp_dup( const kin_object_t *obj, kin_object_t *newobj );
static int kin_obj_tcp_fin( kin_object_t *obj, synthesis_t *syn );
static int kin_obj_tcp_save( const kin_object_t *obj, FILE *stream, const char *self );
static int kin_obj_split_free( kin_object_t *obj );
static int kin_obj_split_dup( const kin_object_t *obj, kin_object_t *newobj );
static int kin_obj_split_fin( kin_object_t *obj, synthesis_t *syn );
static int kin_obj_split_save( const kin_object_t *obj, FILE *stream, const char *self );
/**
* @brief Frees a branch.
* @param branch Branch to free.
*/
static void syn_branch_free( kin_branch_t *branch )
{
free( branch->joints );
}
/**
* @brief Small wrapper to count branches.
* @param syn Unused.
* @param branch Unused.
* @param data Unused.
*/
static int syn_construct_branches_count( synthesis_t *syn, kin_branch_iter_t *branch, void *data )
{
(void) syn;
(void) branch;
(void) data;
return 0;
}
/**
* @brief Walks through branches to construct the branch data.
* @param syn Synthesis object being walked through.
* @param branch Linked list of branch path.
* @param data Casted int* representing number of branch iterating through.
*/
static int syn_construct_branches_walk( synthesis_t *syn, kin_branch_iter_t *branch, void *data )
{
int *i, j, k, n;
kin_branch_iter_t *l;
kin_branch_t *b;
kin_object_t *tcp;
i = (int*)data;
/* Count joints. */
n = 0;
tcp = NULL;
for (l=branch; l!=NULL; l=l->next) {
if (l->obj->type == KIN_TYPE_CHAIN)
n += l->obj->d.chain.njoints;
else if (l->obj->type == KIN_TYPE_TCP)
tcp = l->obj;
}
assert( tcp != NULL );
/* Set pointers. */
b = &syn->branches[*i];
b->joints = memcalloc( n, sizeof(kin_joint_t*) );
b->njoints = n;
b->tcp = tcp;
j = 0;
for (l=branch; l!=NULL; l=l->next)
if (l->obj->type == KIN_TYPE_CHAIN)
for (k=0; k<l->obj->d.chain.njoints; k++)
b->joints[j++] = &l->obj->d.chain.joints[k];
/* Sanity checking. */
if ((b->tcp->d.tcp.claim_acc != NULL) &&
(b->tcp->d.tcp.claim_vel == NULL))
assert( "Velocity data can't be NULL if acceleration data is not NULL." );
/* Make sure accelerations have velocities. */
for (k=0; k < b->tcp->d.tcp.nP; k++) {
if (((b->tcp->d.tcp.A.mask_mask == NULL) ||
(b->tcp->d.tcp.A.mask_mask[k] != 0)) &&
((b->tcp->d.tcp.V.mask_mask != NULL) &&
(b->tcp->d.tcp.V.mask_mask[k] == 0))) {
assert( "Velocity mask must be a superset of acceleration mask." );
}
}
/* Increment branch. */
(*i)++;
return 0;
}
/**
* @brief Constructs the branches for a synthesis object.
* @param syn Synthesis object to create branches for.
*/
static int syn_construct_branches( synthesis_t *syn )
{
int i, n;
assert( syn->branches == NULL );
assert( !syn->finalized );
/* Count branches. */
n = syn_branch_iter( syn, syn_construct_branches_count, NULL );
assert( n > 0 );
/* Allocate branches. */
syn->nbranches = n;
syn->branches = memcalloc( syn->nbranches, sizeof(kin_branch_t) );
i = 0;
syn_branch_iter( syn, syn_construct_branches_walk, &i );
return 0;
}
/**
* @brief Walks over the syntehsis iteration branches.
* @param syn Synthesis object to walk over branches.
* @param func Function to execute at the end of each branch.
* @param head Head of tree-like topology.
* @param last Last element being processed.
* @return Number of branches found.
*/
static int syn_branch_iter_walk( synthesis_t *syn,
int (*func)(synthesis_t*, kin_branch_iter_t*, void*),
void *data, kin_branch_iter_t *head, kin_branch_iter_t *last )
{
int i, ret;
kin_branch_iter_t *tail;
kin_object_t *o;
/* Here we look at the last object and continue processing.
* The goal is to find all the TCP and run the function on them. */
ret = 0;
o = last->obj;
switch (o->type) {
case KIN_TYPE_CHAIN:
/* If the chain is not a dead end (something hanging on the end)
* we shal continue iterating past it. It's just a joining element. */
if (o->next != NULL) {
tail = memcalloc( 1, sizeof(kin_branch_iter_t) );
last->next = tail;
tail->obj = o->next;
ret = syn_branch_iter_walk( syn, func, data, head, tail );
last->next = NULL;
free( tail );
}
break;
case KIN_TYPE_SPLITTER:
/* Splitter has multiple branches leading off so we must iterate over
* each one exactly like the chain. */
tail = memcalloc( 1, sizeof(kin_branch_iter_t) );
last->next = tail;
for (i=0; i<o->d.split.nobjs; i++) {
tail->obj = o->d.split.objs[i];
ret += syn_branch_iter_walk( syn, func, data, head, tail );
}
last->next = NULL;
free( tail );
break;
case KIN_TYPE_TCP:
/* We found a head. Now all we have to do is process it. */
func( syn, head, data );
ret = 1; /* Mark that we found a head. */
/* Theoretically we can have a TCP in the middle so we must consider
* this possibility. */
if (o->next != NULL) {
tail = memcalloc( 1, sizeof(kin_branch_iter_t) );
last->next = tail;
tail->obj = o->next;
ret += syn_branch_iter_walk( syn, func, data, head, tail );
last->next = NULL;
free( tail );
}
break;
default:
/* This shouldn't happen. */
assert( "type mismatch" == NULL );
break;
}
return ret;
}
/**
* @brief Calculates a branch.
*
* The tree should be already updated from the possible data vectors if being used in the solver.
*
* What we have to do is:
*
* *0) Convert variable vector to function vector.
* 1) Set up mask data from mapped data (which should be read directly from claims).
* 2) Update position function vector and screw axis current location.
* 3) Update derivative function vector from new screw axis locations.
* 4) Convert masked function vector to mapped data.
* 5) Convert tree structure to function vector.
*
* * indicates this is not done in this function.
*/
int syn_calc_branch( synthesis_t *syn, kin_branch_t *branch )
{
int i, m;
dq_t T, R, S;
kin_joint_t *j;
plucker_t *pd, *pV;
plucker_t pa, *pA;
double *d;
kin_tcp_data_t *tcp;
const double p[3] = { 0., 0., 0. };
/* Branch must make sense. */
assert( syn->branches != NULL );
assert( branch->joints != NULL );
/* Shortcut. */
tcp = &branch->tcp->d.tcp;
/* Calculate plucker conditions for all the joints, we explicitly use them as equations. */
for (i=0; i<branch->njoints; i++) {
j = branch->joints[i];
j->cond[0] = (vec3_norm( j->S.s ) - 1.); /* ||s|| = 1 */
j->cond[1] = vec3_dot( j->S.s, j->S.s0 ); /* s.s0 = 0 */
/* Need to map data from memory to vector. */
mm_updateMask( &j->pos.values );
mm_updateMask( &j->vel.values );
mm_updateMask( &j->acc.values );
/* Initialize joint position to initial position. */
memcpy( &j->S_cur, &j->S, sizeof(plucker_t) );
}
/* Calculate all the frame data.
*
* Each frame is just a snapshot of the instantaneous kinematics.
*
* This is tricky because we have L-1 positions and up to L velocities/accelerations.
* So we handle them all at the same time, however we don't process the position
* 0 which corresponds to the reference system.
*/
for (m=0; m<syn->L; m++) {
//printf("START FRAME %d\n", m);
if (m != 0) {
dq_cr_point( T, p ); /* Create identity dual quaternion. */
/* Concatenate relative displacements around axes. */
for (i=0; i<branch->njoints; i++) {
j = branch->joints[i];
/* Calculate current joint position. */
dq_cr_line_plucker( S, j->S.s, j->S.s0 );
dq_op_f2g( S, T, S );
/* Calculate current joint position and calculate transformation
* based on the joint type. */
d = (double*) j->pos.values.mask_vec;
switch (j->type) {
case JOINT_TYPE_REVOLUTE:
plucker_from_dq( &j->S_cur, S ); /* Full dual quaternion. */
dq_cr_rotation_plucker( R, d[m-1], j->S.s, j->S.s0 );
break;
case JOINT_TYPE_PRISMATIC:
plucker_from_dqT( &j->S_cur, S ); /* Only translational part. */
dq_cr_translation( R, d[m-1], j->S.s );
break;
default:
assert( "unknown joint type" == NULL );
break;
}
dq_op_mul( T, T, R );
}
/* Since dual quaternions double map SE(3) we make it so all the
* dual quaternions are using the same sign convention. */
if (T[3] < 0.)
dq_op_sign( T, T );
/* Store in fvec: T-P. */
dq_op_sub( tcp->fvec_pos[m-1], T, tcp->P[m] );
}
/* Update velocities. */
if ((tcp->claim_vel != NULL) &&
(tcp->V.mask_len > m) &&
(tcp->V.mask_mask[m])) {
pd = &((plucker_t*) tcp->fvec_vel.mask_vec)[m]; /* Result. */
/* Initialize result. */
plucker_zero( pd );
/* Straight forward:
* v = \sum v_i S_i
*/
for (i=0; i<branch->njoints; i++) {
j = branch->joints[i];
d = (double*) j->vel.values.mask_vec;
lie_joint_mac( pd, d[m], &j->S_cur );
}
/* Copy result. */
pV = &((plucker_t*) tcp->V.mask_vec)[m]; /* Static data. */
plucker_sub( pd, pd, pV );
}
/* Update accelerations. */
if ((branch->tcp->d.tcp.claim_acc != NULL) &&
(branch->tcp->d.tcp.A.mask_len > m) &&
(branch->tcp->d.tcp.A.mask_mask[m] != 0)) {
int k;
kin_joint_t *jk;
/* Initialize result. */
plucker_zero( &pa );
/* Straight forward sum part:
* a_1 = \sum a_i S_i
*/
for (i=0; i<branch->njoints; i++) {
j = branch->joints[i];
d = (double*) j->acc.values.mask_vec;
lie_joint_mac( &pa, d[m], &j->S_cur );
}
/* More complex coriolis part.
* a_2 = \sum v_i \sum v_k [s_i, s_k]
*/
for (i=0; i<branch->njoints-1; i++) {
plucker_t acc;
j = branch->joints[i];
plucker_zero( &acc );
for (k=i+1; k<branch->njoints; k++) {
plucker_t br;
jk = branch->joints[k];
lie_joint_bracket( &br, &j->S_cur, &jk->S_cur );
d = (double*) jk->vel.values.mask_vec;
lie_joint_mac( &acc, d[m], &br );
}
d = (double*) j->vel.values.mask_vec;
lie_joint_mac( &pa, d[m], &acc );
}
/* Copy result. */
pd = (plucker_t*) branch->tcp->d.tcp.fvec_acc.mask_vec;
pA = (plucker_t*) branch->tcp->d.tcp.A.mask_vec;
plucker_sub( &pd[m], &pa, &pA[m] );
}
}
/* Convert back to map layout for the solver. */
mm_updateMap( &tcp->fvec_vel );
mm_updateMap( &tcp->fvec_acc );
return 0;
}
/**
* @brief Calculates all the fvec, conditions and the likes for the kinematic synthesis.
*
* This rewrites syn->fvec, but does not load from syn->x.
*
* @param syn Synthesis to calculate all the data from.
*/
int syn_calc( synthesis_t *syn )
{
int i;
/* Must be finalized. */
assert( syn->finalized );
/* Process all the branches. */
for (i=0; i<syn->nbranches; i++)
syn_calc_branch( syn, &syn->branches[i] );
/* We have to map back to the fvector. */
syn_map_to_fvec( syn, NULL, NULL, syn->fvec );
return 0;
}
/**
* @brief Walking over branches.
* @param syn Synthesis to iterate over branches.
* @param func Function to run on each branch.
* @param data User data to pass to function.
* @return Number of branches found.
*/
int syn_branch_iter( synthesis_t *syn, int (*func)(synthesis_t*, kin_branch_iter_t*, void*), void *data )
{
int ret;
kin_branch_iter_t *head;
assert( syn->obj != NULL );
head = memcalloc( 1, sizeof(kin_branch_iter_t) );
head->obj = syn->obj;
ret = syn_branch_iter_walk( syn, func, data, head, head );
free(head);
return ret;
}
/**
* @brief Initializes a kinematic joint.
*
* @param joint Joint to initialize.
* @param type Type of the joint.
* @sa kin_joint_init
*/
int kin_joint_init( kin_joint_t *joint, kin_joint_type_t type )
{
const double ub[6] = { 1., 1., 1., 100., 100., 100. };
const double lb[6] = { -1., -1., -1., -100., -100., -100. };
memset( joint, 0, sizeof(kin_joint_t) );
joint->type = type;
/* Default bounds. */
memcpy( &joint->S_lb, lb, 6*sizeof(double) );
memcpy( &joint->S_ub, ub, 6*sizeof(double) );
return 0;
}
/**
* @brief Frees the kinematic joint data.
*/
static void kin_joint_data_free( kin_joint_data_t *data )
{
mm_cleanup( &data->values );
mm_cleanup( &data->values_lb );
mm_cleanup( &data->values_ub );
#ifndef NDEBUG
memset( data, 0, sizeof(kin_joint_data_t) );
#endif /* NDEBUG */
}
/**
* @brief Frees the datastructure in joint.
*
* @param joint Joint to free.
* @sa kin_joint_init
*/
void kin_joint_free( kin_joint_t *joint )
{
kin_joint_data_free( &joint->pos );
kin_joint_data_free( &joint->vel );
kin_joint_data_free( &joint->acc );
}
/**
* @brief Duplicates data
*/
static void kin_joint_data_dup( kin_joint_data_t *dest, const kin_joint_data_t *src )
{
dest->nvalues = src->nvalues;
dest->constant = src->constant;
if (src->values.chunk != 0)
mm_initDup( &dest->values, &src->values );
if (src->values_lb.chunk != 0)
mm_initDup( &dest->values_lb, &src->values_lb );
if (src->values_ub.chunk != 0)
mm_initDup( &dest->values_ub, &src->values_ub );
}
/**
* @brief Duplicates a joint.
*/
void kin_joint_dupInit( kin_joint_t *nj, const kin_joint_t *oj )
{
memset( nj, 0, sizeof(kin_joint_t) );
/* Copy base stuff. */
nj->type = oj->type;
nj->const_S = oj->const_S;
nj->const_pos = oj->const_pos;
/* Copy axis info. */
memcpy( &nj->S, &oj->S, sizeof(plucker_t) );
memcpy( &nj->S_lb, &oj->S_lb, sizeof(plucker_t) );
memcpy( &nj->S_ub, &oj->S_ub, sizeof(plucker_t) );
/* Duplicate position and derivative memory. */
kin_joint_data_dup( &nj->pos, &oj->pos );
kin_joint_data_dup( &nj->vel, &oj->vel );
kin_joint_data_dup( &nj->acc, &oj->acc );
/* Copy conditional data. */
memcpy( nj->cond, oj->cond, 2*sizeof(double) );
}
/**
* @brief Claims joint data.
*/
static int kin_joint_data_claim( synthesis_t *syn, kin_joint_data_t *data, const char *name )
{
assert( data->claim == NULL );
if (data->values.chunk != 0) {
//assert( data->nvalues == syn->L-1 );
data->claim = syn_claim_x( syn, data->values.map_len, data->values.map_len,
(double*) data->values.map_vec,
(double*) data->values_lb.map_vec,
(double*) data->values_ub.map_vec, name );
}
return 0;
}
/**
* @brief has a joint create claims.
* @param syn Synthesis object to which the joint belongs.
* @param joint Joint to do the claiming.
*/
int kin_joint_claim( synthesis_t *syn, kin_joint_t *joint )
{
/* We must make sure it's not previously initialized and that data is valid. */
assert( joint->claim_S == NULL );
assert( joint->claim_cond == NULL );
assert( joint->claim_const == NULL );
assert( syn->L > 0 );
assert( joint->pos.values.map_len == syn->L-1 );
assert( joint->vel.values.map_len <= joint->pos.values.map_len+1 ); /* Have to account that pos contains relative transformations. */
assert( joint->acc.values.map_len <= joint->vel.values.map_len );
/* Claim the data. */
kin_joint_data_claim( syn, &joint->pos, "Joint Pos" );
kin_joint_data_claim( syn, &joint->vel, "Joint Vel" );
kin_joint_data_claim( syn, &joint->acc, "Joint Acc" );
/* Claim joint axis if necessary. */
if (!joint->const_S) {
switch (joint->type) {
case JOINT_TYPE_REVOLUTE:
joint->claim_S = syn_claim_x( syn, 6, 4,
(double*)&joint->S, (double*)&joint->S_lb, (double*)&joint->S_ub,
"Joint Plucker (R)" );
/* The conditions are not independent as they are what lowers the
* dimension from 6->4. */
joint->claim_cond = syn_claim_fvec( syn, 2, 0, joint->cond,
"Joint Plucker Cond (R)" );
break;
case JOINT_TYPE_PRISMATIC:
joint->claim_S = syn_claim_x( syn, 3, 2,
(double*)&joint->S.s, (double*)&joint->S_lb.s, (double*)&joint->S_ub.s,
"Joint Plucker (P)" );
/* The conditions are not independent as they are what lowers the
* dimension from 3->2. */
joint->claim_cond = syn_claim_fvec( syn, 1, 0, joint->cond,
"Joint Plucker Cond (P)" );
break;
default:
assert( "unknown joint type" == NULL );
break;
}
}
return 0;
}
/**
* @brief Sets the plucker coordinates for a joint.
*
* @param joint Joint to set plucker coordinates of.
* @param s Direction vector of the joint line.
* @param s0 Moment of the joint line.
*/
void kin_joint_setPlucker( kin_joint_t *joint, double s[3], double s0[3] )
{
assert( joint->type != JOINT_TYPE_NULL );
switch (joint->type) {
case JOINT_TYPE_REVOLUTE:
assert( fabs(vec3_dot( s, s0 )) <= DQ_PRECISION );
memcpy( joint->S.s, s, sizeof(double)*3 );
memcpy( joint->S.s0, s0, sizeof(double)*3 );
break;
case JOINT_TYPE_PRISMATIC:
memcpy( joint->S.s, s, sizeof(double)*3 );
memset( joint->S.s0, 0, sizeof(double)*3 );
break;
default:
assert( "unknown joint type" == NULL );
break;
}
}
/**
* @brief Sets the data for a joint.
* @param data Joint data to set.
* @param x Vector to set.
* @param len Length of vector.
* @param lb Default low bound value.
* @param ub Default upper bound value.
*/
static void kin_joint_data_set( kin_joint_data_t *data, double *x, int len, double lb, double ub, const int *mask )
{
int i;
double *pub, *plb;
assert( data->values.chunk == 0 );
/* The values are straight forward. */
data->nvalues = len;
mm_initMask( &data->values, sizeof(double), len, x, mask );
/* We'll create temporary vectors to create the bounds. */
plb = memmalloc( len * sizeof(double) );
pub = memmalloc( len * sizeof(double) );
for (i=0; i<len; i++) {
plb[i] = lb;
pub[i] = ub;
}
mm_initMask( &data->values_lb, sizeof(double), len, plb, mask );
mm_initMask( &data->values_ub, sizeof(double), len, pub, mask );
free( plb );
free( pub );
}
/**
* @brief Sets the positions.
* @param joint Joint to set positions of.
* @param x X positions to set.
* @param len Length of the positions (should be frames-1).
*/
void kin_joint_setPositions( kin_joint_t *joint, double *x, int len )
{
/* No mask, we always need position. */
switch (joint->type) {
case JOINT_TYPE_REVOLUTE:
kin_joint_data_set( &joint->pos, x, len, 0., 2.*M_PI, NULL );
break;
case JOINT_TYPE_PRISMATIC:
kin_joint_data_set( &joint->pos, x, len, -1000., 1000., NULL );
break;
default:
assert( "unknown joint type" == NULL );
break;
}
}
/**
* @brief Sets the velocities.
* @param joint Joint to set velocities of.
* @param v Velocities to set.
* @param len Length of velocity vector.
*/
void kin_joint_setVelocities( kin_joint_t *joint, double *v, int len, const int *mask )
{
switch (joint->type) {
case JOINT_TYPE_REVOLUTE:
kin_joint_data_set( &joint->vel, v, len, -10.*M_PI, 10.*M_PI, mask );
break;
case JOINT_TYPE_PRISMATIC:
kin_joint_data_set( &joint->vel, v, len, -10000., 10000., mask );
break;
default:
assert( "unknown joint type" == NULL );
break;
}
}
/**
* @brief Sets the accelerations.
* @param joint Joint to set accelerations of.
* @param a Accelerations to set.
* @param len Length of the accelerations vector.
*/
void kin_joint_setAccelerations( kin_joint_t *joint, double *a, int len, const int *mask )
{
switch (joint->type) {
case JOINT_TYPE_REVOLUTE:
kin_joint_data_set( &joint->acc, a, len, -10.*M_PI, 10.*M_PI, mask );
break;
case JOINT_TYPE_PRISMATIC:
kin_joint_data_set( &joint->acc, a, len, -10000., 10000., mask );
break;
default:
assert( "unknown joint type" == NULL );
break;
}
}
/**
* @brief Sets the axis of the joint as static (not updated in solver).
* @param joint Joint to set as static.
* @param constant Whether or not to set as static (constant).
*/
void kin_joint_setConstS( kin_joint_t *joint, int constant )
{
assert( joint->claim_S == NULL );
joint->const_S = constant;
}
/**
* @brief Sets the movements of the joint as static (not updated in solver).
* @param joint Joint to set as static.
* @param constant Whether or not to set as static (constant).
*/
void kin_joint_setConstPos( kin_joint_t *joint, int constant )
{
assert( joint->pos.claim == NULL );
joint->pos.constant = constant;
}
/**
* @brief Sets the bounds of the plucker position coordinate.
* @param joint Joint to set plucker bounds of.
* @param S_lb Lower bound to set the axis orientation.
* @param S_ub Upper bound to set the axis orientation.
* @param S0_lb Lower bound to set the moment of the axis.
* @param S0_lb Upper bound to set the moment of the axis.
*/
void kin_joint_setPluckerBounds( kin_joint_t *joint,
double *S_lb, double *S_ub, double *S0_lb, double *S0_ub )
{
#ifndef NDEBUG
int i;
for (i=0; i<3; i++) {
assert( S_lb[i] <= S_ub[i] );
assert( S0_lb[i] <= S0_ub[i] );
}
#endif /* NDEBUG */
if (S_lb != NULL)
memcpy( joint->S_lb.s, S_lb, sizeof(double)*3 );
if (S_ub != NULL)
memcpy( joint->S_ub.s, S_ub, sizeof(double)*3 );
if (S0_lb != NULL)
memcpy( joint->S_lb.s0, S0_lb, sizeof(double)*3 );
if (S0_ub != NULL)
memcpy( joint->S_ub.s0, S0_ub, sizeof(double)*3 );
}
/**
* @brief Sets the data bounds.
* @param data Data to set bounds of.
* @param lb Lower bounds.
* @param ub Upper bonds.
* @param len Length.
*/
static void kin_joint_data_setBounds( kin_joint_data_t *data,
double *lb, double *ub, int len )
{
#ifndef NDEBUG
int i;
for (i=0; i<len; i++)
assert( lb[i] <= ub[i] );
#endif /* NDEBUG */
assert( len == data->nvalues );
assert( data->values.chunk != 0 );
/* Copy values over. */
mm_initMask( &data->values_lb, sizeof(double), len, lb, data->values.mask_mask );
mm_initMask( &data->values_ub, sizeof(double), len, ub, data->values.mask_mask );
}
/**
* @brief Sets the position bounds for the joint parameters in a joint.
* @param joint Joint to set position bounds of.
* @param lb Lower bound vector.
* @param ub Upper bound vector.
* @param len Length of both lower and upper bounds which must match length of joint parameter vector.
*/
void kin_joint_setPositionBounds( kin_joint_t *joint,
double *lb, double *ub, int len )
{
kin_joint_data_setBounds( &joint->pos, lb, ub, len );
}
/**
* @brief Sets the velocity bounds for the joint parameters in a joint.
* @param joint Joint to set velocity bounds of.
* @param lb Lower bound vector.
* @param ub Upper bound vector.
* @param len Length of both lower and upper bounds which must match length of joint parameter vector.
*/
void kin_joint_setVelocityBounds( kin_joint_t *joint,
double *lb, double *ub, int len )
{
kin_joint_data_setBounds( &joint->vel, lb, ub, len );
}
/**
* @brief Sets the acceleration bounds for the joint parameters in a joint.
* @param joint Joint to set acceleration bounds of.
* @param lb Lower bound vector.
* @param ub Upper bound vector.
* @param len Length of both lower and upper bounds which must match length of joint parameter vector.
*/
void kin_joint_setAccelerationBounds( kin_joint_t *joint,
double *lb, double *ub, int len )
{
kin_joint_data_setBounds( &joint->acc, lb, ub, len );
}
/**
* @brief Frees a kinematic chain object data.
* @param obj Object to free data of.
* @return 0 on success.
*/
static int kin_obj_chain_free( kin_object_t *obj )
{
int i;
for (i=0; i<obj->d.chain.njoints; i++)
kin_joint_free( &obj->d.chain.joints[i] );
free( obj->d.chain.joints );
return 0;
}
/**
* @brief Duplicates the kinematic chain object data.
* @param obj Parent object to duplicate data of.
* @param newobj Child object to recieve a copy of the data.
* @return 0 on success.
*/
static int kin_obj_chain_dup( const kin_object_t *obj, kin_object_t *newobj )
{
int i;
/* Copy block over. */
memset( &newobj->d.chain, 0, sizeof(kin_chain_data_t) );
/* Copy joints over. */
newobj->d.chain.joints = memmalloc( obj->d.chain.njoints*sizeof(kin_joint_t) );
newobj->d.chain.njoints = obj->d.chain.njoints;
/* Duplicate joints. */
for (i=0; i<newobj->d.chain.njoints; i++)
kin_joint_dupInit( &newobj->d.chain.joints[i], &obj->d.chain.joints[i] );
return 0;
}
/**
* @brief Runs claim on a chain object.
* @param obj Kinematic object to claim data.
* @param syn Parent synthesis object.
*/
static int kin_obj_chain_fin( kin_object_t *obj, synthesis_t *syn )
{
int i;
/* Claim all joints. */
for (i=0; i<obj->d.chain.njoints; i++) {
kin_joint_claim( syn, &obj->d.chain.joints[i] );
/* Add joints to synthesis. */
syn_joint_add( syn, &obj->d.chain.joints[i] );
}
return 0;
}
/**
* @brief Generates strings to save data from joint information.
*/
static void kin_obj_chain_save_pos( char *str, char *str_lb, char *str_ub, int max,
const kin_joint_data_t *data )
{
int j, p, p_lb, p_ub;
const char *tail;
str[0] = '\0';
str_lb[0] = '\0';
str_ub[0] = '\0';
p = 0;
p_lb = 0;
p_ub = 0;
for (j=0; j<data->nvalues; j++) {
tail = (j != data->nvalues-1) ? "," : "";
if (data->values.mask_mask[j]) {
p += snprintf( &str[p], max - p,
PF"%s ", ((double*) data->values.mask_vec)[j], tail );
p_lb += snprintf( &str_lb[p_lb], max - p_lb,
PF"%s ", ((double*) data->values_lb.mask_vec)[j], tail );
p_ub += snprintf( &str_ub[p_ub], max - p_ub,
PF"%s ", ((double*) data->values_ub.mask_vec)[j], tail );
}
else {
p += snprintf( &str[p], max - p, "nil%s ", tail );
p_lb += snprintf( &str_lb[p_lb], max - p_lb, "nil%s ", tail );
p_ub += snprintf( &str_ub[p_ub], max - p_ub, "nil%s ", tail );
}
}
}
/**
* @brief Saves a kinematics object.
*/
static int kin_obj_chain_save( const kin_object_t *obj, FILE *stream, const char *self )
{
int i;
kin_joint_t *kj;
const char *str;
char pos_str[4096], pos_lb_str[4096], pos_ub_str[4096];
double *s, *s0, *lb, *lb0, *ub, *ub0;
for (i=0; i<obj->d.chain.njoints; i++) {
kj = &obj->d.chain.joints[i];
/* Joint information and creation. */
switch (kj->type) {
case JOINT_TYPE_REVOLUTE: str = "revolute"; break;
case JOINT_TYPE_PRISMATIC: str = "prismatic"; break;
default:
assert( "unknown joint type" == NULL );
break;
}
fprintf( stream,
" -- Joint %d\n"
" local j = kin_joint.new( \"%s\" )\n",
i, str );
/* Position information .*/
kin_obj_chain_save_pos( pos_str, pos_lb_str, pos_ub_str, 4096, &kj->pos );
fprintf( stream,
" j:setPositions( { %s } )\n"
" j:setPositionBounds( { %s },\n"
" { %s } )\n",