forked from petercorke/robotics-toolbox-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicsIdentifier.m
1158 lines (926 loc) · 45.8 KB
/
DynamicsIdentifier.m
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
%DYNAMICSIDENTIFIER Class for robot dynamics identification
%
% Objects of the DynamicsIdentifier class produces the parameter
% linear robot dynamics equations in regression form.
%
% The various methods ...
%
% Example::
% mdl_twolink;
% did = DynamicsIdentifier(twolink)
% [YB, XB] = did.getbaseyx
%
% Methods::
%
%
% Properties (read/write)::
%
%
% Object properties (read only)::
%
% Notes::
% This module is new to RTB. Be aware of possible substantial
% changes in terms of function names and usage in the near future.
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also CodeGenerator, SerialLink.
% Copyright (C) 1993-2012, by Peter I. Corke
% Copyright (C) 2012-2015, by Joern Malzahn
%
% This file is part of The Robotics Toolbox for Matlab (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB. If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
%
% The code generation module originally emerged during the work on a project
% funded by the German Research Foundation (DFG, BE1569/7-1). The authors
% gratefully acknowledge the financial support.
classdef DynamicsIdentifier
properties (SetAccess = private)
rob
robn
end
properties
verbose
saveresult
end
methods
function DId = DynamicsIdentifier(rob,varargin)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
fprintf(2,'Note: this module is new to RTB. Be aware of possible substantial\n');
fprintf(2,'changes in terms of function names and usage in the near future.\n');
DId.rob = DynamicsIdentifier.preparerobot(rob.nofriction);
DId.robn = rob.nofriction;
end
function [Y, x, symExpr] = regressor_gravload(DId)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
q = DId.rob.gencoords;
symExpr = DId.rob.gravload(q).';
[Y, x] = DId.getminyx_gravload(symExpr);
end
function [Y, x, symExpr] = regressor_inertia(DId)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
[q] = DId.rob.gencoords;
symExpr = DId.rob.inertia(q);
[Y, x] = DId.getminyx_inertia(symExpr);
end
function [Y, x, symExpr] = regressor_coriolis(DId)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
[q, qd] = DId.rob.gencoords;
symExpr = DId.rob.coriolis(q,qd);
[Y, x] = DId.getminyx_coriolis(symExpr);
end
% ----
function [YB, XB] = getbaseyx(DId,varargin)
% DynamicsIdentifier.getbaseyx Derives the symbolic vector of base
% paramters along with the coresponding symbolic regressor matrix.
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
doOptimizeRegressor = 0;
if nargin > 1
doOptimizeRegressor = varargin{1};
end
[Yg,xg] = DId.regressor_gravload;
[Yi,xi] = DId.regressor_inertia;
[Yc,xc] = DId.regressor_coriolis;
fullY = [Yi,Yc,Yg];
fullX = [xi; xc; xg];
[stdY, stdX] = DynamicsIdentifier.minimalyx(fullY,fullX);
[YB,XB] = DynamicsIdentifier.std2baseyx(stdY, stdX, doOptimizeRegressor);
end
end
methods(Static)
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% higher level functions for regressor generation
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Y, x] = getminyx_ICG(iExpr, cExpr, gExpr)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
[Yi,xi] = DynamicsIdentifyer.getminyx_inertia(iExpr);
[Yc,xc] = DynamicsIdentifyer.getminyx_coriolis(cExpr);
[Yg,xg] = DynamicsIdentifyer.getminyx_gravload(gExpr);
fullY = [Yi,Yc,Yg];
fullX = [xi; xc; xg];
[Y, x] = minimalyx(fullY, fullX);
end
function [Y, x] = getminyx_inertia(inExpr)
[largeY,largeX] = DynamicsIdentifier.splityx_inertia(inExpr);
[Y,x] = DynamicsIdentifier.minimalyx(largeY,largeX);
end
function [Y, x] = getminyx_coriolis(inExpr)
[largeY,largeX] = DynamicsIdentifier.splityx_coriolis(inExpr);
[Y,x] = DynamicsIdentifier.minimalyx(largeY,largeX);
end
function [Y, x] = getminyx_gravload(inExpr)
[largeY,largeX] = DynamicsIdentifier.splityx_gravload(inExpr);
[Y,x] = DynamicsIdentifier.minimalyx(largeY,largeX);
end
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% lower level functions for regressor generation
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [largeY,largeX] = splityx_inertia(inExpr)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
nRows = size(inExpr,1);
nCols = size(inExpr,2);
qddString = sprintf('qdd%d ', 1:nRows);
eval(['syms ', qddString]);
eval(['qdd = [', qddString,'].'';']);
largeX = [];
largeY = [];
for iRow = 1:nRows
rowY = [];
rowX = [];
for iCol = 1:nCols
curEntry = (expand(inExpr(iRow,iCol)));
curString = char(curEntry);
[locSigns,allSummands] = regexp(curString,'-|+','match','split');
if numel(locSigns) < numel(allSummands)
locSigns = ['+',locSigns];
end
nSummands = numel(allSummands);
colR = cell(nSummands,1);
colX = cell(nSummands,1);
for iSummand = 1:nSummands
curSummand = allSummands(iSummand);
% Regular expression: match sine or cosine followed by any character
% [except a closing bracket] written in brackets followed by possibly
% one or none multiplication character \*. This pattern may repeat zero
% or more times. The exception for the bracket within the brackets is
% necessary to prevent double brackets to be matched.
regExp1 = '(sin|cos)'; % sine or cosine followed by
regExp2 = '\('; % a left round bracket followed by
regExp3 = '[^\)]*'; % any number of characters except a right round bracket (this is the argument to the sine/cosine), followed by
regExp4 = '(\)){1}'; % exactly one right bracket (closes the argument list), followed by
regExp5 = '(\^\d+){0,1}'; % possibly a power to the trigonometric function with multiple, followed by
regExp6 = '\*{0,1}'; % possibly one power multiplication character, followed by
regExpStr = ['(',regExp1,regExp2,regExp3,regExp4,regExp5,regExp6,')*'];
% extract regressor entry
[~, matchstring] = regexp(curSummand,regExpStr,'split','match');
if isempty(matchstring{1})
matchstring{1} = '1';
end
colR(iSummand) = strcat(locSigns(iSummand),'(', matchstring{1},')');
% extract parameter
param = regexprep(curSummand, regExpStr, '1');
colX{iSummand} = param{1};
end
% Insert found regressors and parameters in large parameter vector and
% large regressor matrix.
rowX = [rowX; colX(:)];
rowY = [rowY; colR(:)*qdd(iCol)];
end
if isempty(rowY)
rowY{1} = '0';
rowX{1} = '0';
end
% First extend the large regressor matrix
largeY = [largeY, sym(zeros(size(largeY,1),numel(rowY)));
sym(zeros(1,size(largeY,2))), sym(rowY(:).')];
largeX = [largeX; rowX(:)];
end
% remove zero parameters
zIdx = strcmp(largeX,'0');
zIdx = find(zIdx);
largeX(zIdx) = [];
largeY(:,zIdx) = [];
% Check
test = simplify(largeY*largeX-inExpr*qdd(:));
if any(test ~= zeros(size(largeY,1),1))
warning('Warning DynamicsIdentifier.splityx_inertia has made a mistake!')
end
end
function [largeY, largeX] = splityx_coriolis(inExpr)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
nRows = size(inExpr,1);
nCols = size(inExpr,2);
qdString = sprintf('qd%d ', 1:nRows);
eval(['syms ', qdString]);
eval(['qd = [', qdString,'].'';']);
largeX = [];
largeY = sym([]);
for iRow = 1:nRows
rowY = [];
rowX = [];
for iCol = 1:nCols
curEntry = (expand(inExpr(iRow,iCol)));
curString = char(curEntry);
[locSigns,allSummands] = regexp(curString,'-|+','match','split');
if isempty(allSummands{1})
allSummands = allSummands(2:end);
end
if (numel(locSigns) < numel(allSummands))
locSigns = ['+',locSigns];
end
nSummands = numel(allSummands);
if nSummands == 1
if strcmp(curString,'0')
continue;
end
end
colY = cell(nSummands,1);
colX = cell(nSummands,1);
for iSummand = 1:nSummands
curSummand = allSummands(iSummand);
% Regular expression: match sine or cosine followed by any character
% [except a closing bracket] written in brackets followed by possibly
% one or none multiplication character \*. This pattern may repeat zero
% or more times. The exception for the bracket within the brackets is
% necessary to prevent double brackets to be matched.
% regExpStr = '((sin|cos)\([^\)]*(\)){1}\*{0,1})*';
% Match and split string if you find an expression starting with
regExp0 = '(qd\d)?'; % eventually a joint value derivative
regExp1 = '(sin|cos)'; % sine or cosine followed by
regExp2 = '\('; % a left round bracket followed by
regExp3 = '[^\)]*'; % any number of characters except a right round bracket (this is the argument to the sine/cosine) followed by
regExp4 = '(\)){1}'; % exactly one right bracket (closes the argument list), followed by
regExp5 = '(\^\d+){0,1}'; % possibly a power to the trigonometric function with multiple, followed by
regExp6 = '\*?'; % possibly one power multiplication character, followed by
% regExpStr = '((sin|cos)\([^\)]*(\)){1}(\x5E\d)?\*?)*';
% regExpStr = ['(',regExp0,regExp1,regExp2,regExp3,regExp4,regExp5,regExp6,')*'];
regExpStr = ['(',regExp1,regExp2,regExp3,regExp4,regExp5,regExp6,')*'];
% extract regressor entry
[~, matchstring] = regexp(curSummand,regExpStr,'split','match'); % look for trigonometric functions and their powers
[matchstringQD] = regexp(curSummand,regExp0,'match'); % look for generalized velocity as a separate factor
if isempty(matchstring{1})
matchstring{1} = '1';
end
if isempty(matchstringQD{1})
matchstringQD{1} = '1';
end
colY(iSummand) = strcat(locSigns(iSummand),'(', matchstring{1},'*',matchstringQD{1},')');
% extract parameter
param = regexprep(curSummand, regExpStr, '1');
param = regexprep(param, regExp0, '1');
colX{iSummand} = param{1};
end
% Insert found regressors and parameters in large parameter vector and
% large regressor matrix.
rowX = [rowX; colX(:)];
rowY = [rowY; colY(:)*qd(iCol)];
end
if isempty(rowY)
rowY{1} = '0';
rowX{1} = '0';
end
% First extend the large regressor matrix
largeY = [largeY, sym(zeros(size(largeY,1),numel(rowY)));
sym(zeros(1,size(largeY,2))), sym(rowY(:).')];
largeX = [largeX; (rowX(:))];
end
% remove zero parameters
zIdx = strcmp(largeX,'0');
zIdx = find(zIdx);
largeX(zIdx) = [];
largeY(:,zIdx) = [];
% Check
test = simplify(largeY*largeX-inExpr*qd(:));
if any(test ~= zeros(size(largeY,1),1))
warning('Warning DynamicsIdentifier.splityx_coriolis has made a mistake!')
end
end
function [largeY,largeX] = splityx_gravload(inExpr)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
nRows = size(inExpr,1);
largeX = [];
largeY = [];
for iRow = 1:nRows
curEntry = (expand(inExpr(iRow)));
% pretty(curEntry);
curString = char(curEntry);
% Split expressions by signs
[locSigns,allSummands] = regexp(curString,'-|+','match','split');
if numel(locSigns) < numel(allSummands)
locSigns = ['+',locSigns];
end
nSummands = numel(allSummands);
if curEntry == sym(0) && nSummands == 1
rowY = cell(nSummands);
rowY(1) = [];
rowX = rowY;
else
rowY = cell(nSummands,1);
rowX = cell(nSummands,1);
for iSummand = 1:nSummands
curSummand = allSummands(iSummand);
% Regular expression: match sine or cosine followed by any character
% [except a closing bracket] written in brackets followed by possibly
% one or none multiplication character \*. This pattern may repeat zero
% or more times. The exception for the bracket within the brackets is
% necessary to prevent double brackets to be matched.
regExp1 = '(sin|cos)'; % sine or cosine followed by
regExp2 = '\('; % a left round bracket followed by
regExp3 = '[^\)]*'; % any number of characters except a right round bracket (this is the argument to the sine/cosine), followed by
regExp4 = '(\)){1}'; % exactly one right bracket (closes the argument list), followed by
regExp5 = '(\^\d+){0,1}'; % possibly a power to the trigonometric function with multiple, followed by
regExp6 = '\*{0,1}'; % possibly one power multiplication character, followed by
regExpStr = ['(',regExp1,regExp2,regExp3,regExp4,regExp5,regExp6,')*'];
% regExpStr =
% '((sin|cos)\([^\)]*(\)){1}(\^\d+){0,1}\*{0,1})*';
% % original from gravload iros code
% extract regressor entry
[~, matchstring] = regexp(curSummand,regExpStr,'split','match');
if isempty(matchstring{1})
matchstring{1} = '1';
end
rowY(iSummand) = strcat(locSigns(iSummand),'(', matchstring{1},')');
% extract parameter
param = regexprep(curSummand, regExpStr, '1');
rowX{iSummand} = param{1};
end
end
% Insert found regressors and parameters in large parameter vector and
% large regressor matrix.
if isempty(rowY)
rowY{1} = '0';
rowX{1} = '0';
end
largeY = [largeY, sym(zeros(size(largeY,1),nSummands));
sym(zeros(1,size(largeY,2))), sym(rowY(:).')];
largeX = [largeX; rowX(:)];
end
% remove zero parameters
zIdx = strcmp(largeX,'0');
zIdx = find(zIdx);
largeX(zIdx) = [];
largeY(:,zIdx) = [];
% Check
test = simplify(largeY*largeX-inExpr);
if any(test ~= zeros(size(largeY,1),1))
warning('Warning DynamicsIdentifier.splityx_gravload has made a mistake!')
end
end
function [Y, X] = minimalyx(largeY,largeX)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
[redY,redX] = DynamicsIdentifier.reduceyx(largeY,largeX);
[Y, X] = DynamicsIdentifier.uniqueyx(redY,redX);
end
function [redY,redX] = reduceyx(largeY,largeX)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
nRows = size(largeY,1);
% find unique parameters
allSymPar = sym(largeX);
if (size(largeY,2)~=numel(largeX))
allSymPar(allSymPar==0) = [];
end
[redX,~,idxToRedY] = unique(allSymPar);
nPar = numel(redX);
% The vector rowredSymPar only contains the unique parameters. The index vector idxTuUnR
% contains the index of the unique parameter vector uiqueSymPar to whithc
% the regressor component from rowR belongs. So iterate over all summands
% and add the summands to their corresponding locations in the unique
% regressor.
redY = sym(zeros(nRows,nPar));
for iRow = 1: nRows
for iX = 1:numel(allSymPar);
redY(iRow,idxToRedY(iX)) = redY(iRow,idxToRedY(iX)) + largeY(iRow,iX);
end
end
reduceTest = simplify(redY*redX-largeY*largeX);
if any(reduceTest ~= zeros(size(redY,1),1))
warning('Warning DynamicsIdentifier.reduceyx has made a mistake!')
end
end
function [uniqueY, uniqueX] = uniqueyx(redY,redX)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
uniqueY = sym([]);
uniqueX = sym([]);
nY = size(redY,2);
for iY = 1:nY
isThere = 0;
nUR = size(uniqueY,2);
for iUR = 1:nUR
tmpMat = [redY(:,iY), uniqueY(:,iUR)];
tmpRank = rank(tmpMat);
if tmpRank < 2 %nJoints
if isempty(findsym((redY(:,iY) ./ uniqueY(:,iUR))))
% column is already there: sum parameters
if (any((redY(:,iY) ./ uniqueY(:,iUR))<0)) % is positive or negative multiple?
uniqueX(iUR,1) = uniqueX(iUR,1) - redX(iY); % negative multiple
% disp('negative')
else
uniqueX(iUR,1) = uniqueX(iUR,1) + redX(iY); % positive multiple
% disp('positive')
end
isThere = 1;
end
else
end
end
if isThere == 0
% column is new: append
uniqueY(:,nUR+1) = redY(:,iY);
uniqueX(nUR+1,1) = redX(iY);
end
end
uniqueTest = simplify(redY*redX-uniqueY*uniqueX);
if any(uniqueTest ~= zeros(size(redY,1),1))
warning('Warning DynamicsIdentifier.uniqueyx has made a mistake!')
end
end
% extract inverse dynamics components
function [I] = extractinertia(inExpr)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
% inExpr must not contain gravity components!
nJoints = size(inExpr,1);
qddString = sprintf('qdd%d ', 1:nJoints);
eval(['syms ', qddString]);
eval(['symQDD = [', qddString,'].'';']);
qdString = sprintf('qd%d ', 1:nJoints);
eval(['syms ', qdString]);
eval(['symQD = [', qdString,'].'';']);
I = sym(zeros(nJoints));
QD = zeros(size(symQD));
inExpr = subs(inExpr,symQD,QD);
for iCol = 1:nJoints
QDD = sym(zeros(nJoints,1));
QDD(iCol) = sym(1);
for iRow = 1:nJoints
I(iRow,iCol) = subs(inExpr(iRow),symQDD,QDD);
end
end
% Check
inertiaTest = simplify(inExpr-I*symQDD(:));
if any(inertiaTest ~= zeros(size(nJoints)))
warning('Warning DynamicsIdentifier.extractinertia has made a mistake!')
end
end
function [ C ] = extractcoriolis( inExpr )
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
% inExpr must not contain gravity components!
nJoints = size(inExpr,1);
qdString = sprintf('qd%d ', 1:nJoints);
eval(['syms ', qdString]);
eval(['symQD = [', qdString,'].'';']);
qddString = sprintf('qdd%d ', 1:nJoints);
eval(['syms ', qddString]);
eval(['symQDD = [', qddString,'].'';']);
QDD = zeros(size(symQDD));
inExpr = subs(inExpr,symQDD,QDD);
C = sym(zeros(nJoints));
Csq = sym(zeros(nJoints));
% find the torques that depend on a single finite joint speed,
% these are due to the squared (centripetal) terms
%
% set QD = [1 0 0 ...] then resulting torque is due to qd_1^2
for j=1:nJoints
QD = sym(zeros(1,nJoints));
QD(j) = 1;
% tau = robot2.rne(q, QD, zeros(size(q)), [0 0 0]');
tauLocal = sym(zeros(nJoints,1));
for iRow = 1:nJoints
% tauLocal = subs(tau,[qd1,qd2,qd3],QD);
tauLocal(iRow) = subs(inExpr(iRow),symQD.',QD);
end
Csq(:,j) = Csq(:,j) + tauLocal;
end
% find the torques that depend on a pair of finite joint speeds,
% these are due to the product (Coridolis) terms
% set QD = [1 1 0 ...] then resulting torque is due to
% qd_1 qd_2 + qd_1^2 + qd_2^2
for j=1:nJoints
for k=j+1:nJoints
% find a product term qd_j * qd_k
QD = sym(zeros(1,nJoints));
QD(j) = 1;
QD(k) = 1;
for iRow = 1:nJoints
tauLocal(iRow) = subs(inExpr(iRow),symQD.',QD);
end
C(:,k) = C(:,k) + (tauLocal - Csq(:,k) - Csq(:,j)) * symQD(j);
end
end
C = C + Csq * diag(symQD);
end
% ----
function [y] = reshape_id_input(x)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
nRows = size(x,1);
nJoints = size(x,2);
y = zeros(nRows*nJoints,1);
for iJoint = 1:nJoints
y(iJoint:nJoints:end,1) = x(:,iJoint);
end
end
% ----
function [Ynum] = numericregressor(Y,varargin)
% DYNAMICSIDENTIFIER.NUMERICREGRESSOR Concatenates regressors for
% a given set of identification data samples.
%
% [Ynum, Tnum] = numericregressor(Y,T,Q,QD,QDD])
%
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
nJoints = size(Y,1);
nPar = size(Y,2);
Q = [];
Qd = [];
Qdd = [];
if nargin < 2
error('You should provide some data, if you want me to generate a numerical regressor...')
else
end
if nargin > 1
Q = varargin{1};
if size(Q,2) ~= nJoints
error('Input dimension mismatch: Q should be of size [nDataPoints x nJoints]');
end
for iJoint = 1:nJoints
eval(sprintf('q%d = Q(:,%d);',iJoint,iJoint));
end
end
if nargin > 2
Qd = varargin{2};
if size(Qd,2) ~= nJoints
error('Input dimension mismatch: Qd should be of size [nDataPoints x nJoints]');
end
for iJoint = 1:nJoints
eval(sprintf('qd%d = Qd(:,%d);',iJoint,iJoint));
end
end
if nargin > 3
Qdd = varargin{3};
if size(Qdd,2) ~= nJoints
error('Input dimension mismatch: Qdd should be of size [nDataPoints x nJoints]');
end
for iJoint = 1:nJoints
eval(sprintf('qdd%d = Qdd(:,%d);',iJoint,iJoint));
end
end
%initialize variables
nVals = size(Q,1);
Ynum = zeros(nVals*nJoints,nPar);
for iJoint = 1:nJoints
curStr = char((Y(iJoint,:)));
curStr = strrep(curStr,'*','.*');
curStr = strrep(curStr,'^','.^');
curStr = strrep(curStr,' 0,',' zeros(nVals,1),');
curStr = strrep(curStr,'[0,',' [zeros(nVals,1),');
curStr = strrep(curStr,' 0]',' zeros(nVals,1)]');
curStr = strrep(curStr,' 1]',' ones(nVals,1)]');
curStr = strrep(curStr,'matrix','');
Ynum(iJoint:nJoints:end,:) = eval(curStr);
end
end
% ----
function [Ynum, Tnum] = getregressionproblem(Y,T,varargin)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
nJoints = size(Y,1);
if nargin < 2
error('You should provide some data, if you want me to generate a numerical regressor...')
else
end
if nargin > 1
T = varargin{1};
if size(T,2) ~= nJoints
error('Input dimension mismatch: T should be of size [nDataPoints x nJoints]');
end
end
if nargin > 2
Q = varargin{1};
if size(Q,2) ~= nJoints
error('Input dimension mismatch: Q should be of size [nDataPoints x nJoints]');
end
for iJoint = 1:nJoints
eval(sprintf('q%d = Q(:,%d);',iJoint,iJoint));
end
end
if nargin > 3
Qd = varargin{2};
if size(Qd,2) ~= nJoints
error('Input dimension mismatch: Qd should be of size [nDataPoints x nJoints]');
end
for iJoint = 1:nJoints
eval(sprintf('qd%d = Qd(:,%d);',iJoint,iJoint));
end
end
if nargin > 4
Qdd = varargin{3};
if size(Qdd,2) ~= nJoints
error('Input dimension mismatch: Qdd should be of size [nDataPoints x nJoints]');
end
for iJoint = 1:nJoints
eval(sprintf('qdd%d = Qdd(:,%d);',iJoint,iJoint));
end
end
Tnum = DynamicsIdentifier.reshape_id_input(T);
Ynum = DynamicsIdentifier.numericregressor(Y,varargin{:});
end
% ----
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Regressor scaling and optimization (requires Optimization Toolbox)
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [YNum, fval]= scaledrandregressor(Y,nSamp,varargin)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
doOptimizeRegressor = 0;
if nargin > 2
doOptimizeRegressor = varargin{1};
end
nJoints = size(Y,1);
%% generate some random joint values (positions and derivatives)
% within these boundaries
blq = -1*pi; % lower position boundary
bhq = 1*pi; % upper position boundary
blqd = -2*pi; % lower velocity boundary
bhqd = 2*pi; % upper velocity boundary
blqdd = -4*pi; % lower accerleration boundary
bhqdd = 4*pi; % upper acceleration boundary
% position
rng('shuffle'); % create new seed for random number generator
q = blq + (bhq-blq).*rand(nSamp,nJoints);
% velocity
rng('shuffle'); % create new seed for random number generator
qd = blqd + (bhqd-blqd).*rand(nSamp,nJoints);
% acceleration
rng('shuffle'); % create new seed for random number generator
qdd = blqdd + (bhqdd-blqdd).*rand(nSamp,nJoints);
% collect all joint values in an optimization matrix
x = [q(:);qd(:);qdd(:)];
if exist('fminunc.m','file') && doOptimizeRegressor
x0 = x;
% create components of the constraint inequality A x < b
Ai = [eye(3*nJoints*nSamp); -eye(3*nJoints*nSamp)];
A = repmat(Ai,3,1);
bi2 = ones(2*3*nJoints*nSamp,1);
b = [bhq*bi2; bhqd*bi2; bhqdd*bi2];
% optimize the regressor under given constraints!
fun = @(x)DynamicsIdentifier.rand_regressor_cost_function(x,Y);
[x, fval] = fmincon(fun, x0, A, b);
else
fval = NaN;
% just use the unoptimized random numerical regressor
end
% assemble the best found regressor
YNum = DynamicsIdentifier.numericregressor(Y,... regressor
reshape(x(1:nJoints*nSamp),nSamp,nJoints),... % q
reshape(x((1:nJoints*nSamp)+nJoints*nSamp),nSamp,nJoints),... % qd
reshape(x((1:nJoints*nSamp)+2*nJoints*nSamp),nSamp,nJoints)); % qdd
end
% ----
function J = rand_regressor_cost_function(x,Y)
%
% Author::
% Joern Malzahn (joern.malzahn@tu-dortmund.de)
% 2015 IIT Istituto Italiano di Tecnologia, Genova, Italy.
% www.iit.it
%
% See also DynamicsIdentifier
nJoints = size(Y,1);
nSamp = numel(x)/nJoints/3;
% assemble regressor
YNum = DynamicsIdentifier.numericregressor(Y,... regressor
reshape(x(1:nJoints*nSamp),nSamp,nJoints),... % q
reshape(x((1:nJoints*nSamp)+nJoints*nSamp),nSamp,nJoints),... % qd
reshape(x((1:nJoints*nSamp)+2*nJoints*nSamp),nSamp,nJoints)); % qdd
% Criteria as used in
% M. Gautier: Numerical Calculation of the Base Inertial Parameters