-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculate.cpp
More file actions
2014 lines (1612 loc) · 73.3 KB
/
Copy pathcalculate.cpp
File metadata and controls
2014 lines (1612 loc) · 73.3 KB
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
/*****************************************************************************************************************************
******************************************************************************************************************************
数値計算に必要な関数をまとめたソースファイル
******************************************************************************************************************************
*****************************************************************************************************************************/
#include "calculate.h"
//#include "myFilter.cpp"
/**************************** 速度・加速度,ZMP関係 *****************************************************************************************************/
float time_sum;
float sum_time;
/********************************************************
関数名:Calculate_Joint_VelAcc
説明 :位置データから速度,加速度の計算
引数 :const int count 時間
double pos[2] 位置データ
struct motion_data *motion 位置,速度,加速度データ
出力 :struct motion_data *motion
********************************************************/
void Calculate_Joint_VelAcc(const int count, double q[3][2][3]){//only for joints, not uppThigh,lowThigh and foot
static double pre[3][2][3];
static float buf_enc[3][2][3][2][FILTER_LEN];
time_sum += SAMPLING_TIME;
//現在(2016/10/24)データの平滑化を行っていないため必要に応じて実装してください!! Data smoothing not done
//simi added low pass filter
//if ((count % (SAMPLING_FREQ / 2000)) == 1) { //2000Hz
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
q[VEL][i][j] = (q[POS][i][j] - pre[POS][i][j]) / time_sum; //prev SAMPLING_TIME
q[VEL][i][j] = LowPassFilter(2000.0f, 2.0f, q[VEL][i][j], buf_enc[VEL][i][j]);
q[ACC][i][j] = (q[VEL][i][j] - pre[VEL][i][j]) / time_sum;
//q[ACC][i][j] = LowPassFilter(2000.0f, 2.0f, q[ACC][i][j], buf_enc[ACC][i][j]);
pre[POS][i][j] = q[POS][i][j];
pre[VEL][i][j] = q[VEL][i][j];
pre[ACC][i][j] = q[ACC][i][j];
}
//simi commented
/*q[VEL][i][H] = ( q[POS][i][H] - pre[POS][i][H] )/SAMPLING_TIME;
q[VEL][i][K] = ( q[POS][i][K] - pre[POS][i][K] )/SAMPLING_TIME;
q[VEL][i][A] = ( q[POS][i][A] - pre[POS][i][A] )/SAMPLING_TIME;
q[ACC][i][H] = ( q[VEL][i][H] - pre[VEL][i][H] )/SAMPLING_TIME;
q[ACC][i][K] = ( q[VEL][i][K] - pre[VEL][i][K] )/SAMPLING_TIME;
q[ACC][i][A] = ( q[VEL][i][A] - pre[VEL][i][A] )/SAMPLING_TIME;*/
}
sum_time = time_sum;
time_sum = 0.0f;
/*}
else {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
q[POS][i][j] = pre[POS][i][j];
q[VEL][i][j] = pre[VEL][i][j];
q[ACC][i][j] = pre[ACC][i][j];
}
}
}*/
//データの更新
/*for(int i=0;i<2;i++){
pre[POS][i][H] = q[POS][i][H];
pre[POS][i][K] = q[POS][i][K];
pre[POS][i][A] = q[POS][i][A];
pre[VEL][i][H] = q[VEL][i][H];
pre[VEL][i][K] = q[VEL][i][K];
pre[VEL][i][A] = q[VEL][i][A];
}*/
}
/********************************************************
関数名:Calculate_VelocityAcceleration
説明 :位置データから速度,加速度の計算
引数 :const int count 時間
double pos[2] 位置データ
struct motion_data *motion 位置,速度,加速度データ
出力 :struct motion_data *motion
********************************************************/
void Calculate_VelocityAcceleration(const int count, double pos[2], struct motion_data *motion, float buf[2][2][3][FILTER_LEN]){
//buf: Body, RUpp,LUpp,...: pos, vel, acc:X, Y
//現在(2016/10/24)データの平滑化を行っていないため必要に応じて実装してください!! As of (2016/10/24) data smoothing is not performed, please implement as needed !!
//データの初期化
if(count == 0){
for(int i=0;i<2;i++){
motion->prePos[i] = motion->pos[i];//simi to stop init vel, acc jump
motion->preVel[i] = motion->vel[i];
motion->preAcc[i] = motion->acc[i];
}
}
else {
//速度,加速度の計算
//if (count % (SAMPLING_FREQ / 2000) == 1) {
motion->pos[X] = pos[X];
motion->pos[Y] = pos[Y];
//motion->pos[X] = LowPassFilter(2000.0f, 10.0f, motion->pos[X], buf[POS][X]);
//motion->pos[Y] = LowPassFilter(2000.0f, 10.0f, motion->pos[Y], buf[POS][Y]);
motion->vel[X] = (motion->pos[X] - motion->prePos[X]) / sum_time;
motion->vel[X]= LowPassFilter(2000.0f, 2.0f, motion->vel[X], buf[VEL][X]);
motion->vel[Y] = (motion->pos[Y] - motion->prePos[Y]) / sum_time;
motion->vel[Y] = LowPassFilter(2000.0f, 2.0f, motion->vel[Y], buf[VEL][Y]);
motion->acc[X] = (motion->vel[X] - motion->preVel[X]) / sum_time;
motion->acc[X]= LowPassFilter(2000.0f, 2.0f, motion->acc[X], buf[ACC][X]);
motion->acc[Y] = (motion->vel[Y] - motion->preVel[Y]) / sum_time;
motion->acc[Y] = LowPassFilter(2000.0f, 2.0f, motion->acc[Y], buf[ACC][Y]);
//データの更新
for (int i = 0; i < 2; i++) {
motion->prePos[i] = motion->pos[i];
motion->preVel[i] = motion->vel[i];
motion->preAcc[i] = motion->acc[i];
}
}
/*else if(count!=0){
for (int i = 0; i < 2; i++) {
motion->pos[i] = motion->prePos[i];
motion->vel[i] = motion->preVel[i];
motion->acc[i] = motion->preAcc[i];
}
}*/
}
/********************************************************
関数名:Calculate_ZMP
説明 :位置データから速度,加速度の計算
引数 :const int count 時間
double pos[2] 位置データ
struct motion_data *motion 位置,速度,加速度データ
出力 :struct motion_data *motion
********************************************************/
double Calculation_ZMP( struct body_posi *body_posi){
double a=0;
double preZMP = (*body_posi).XZmp;
a= ( SUM_FOOT*( ((*body_posi).Foot[R].acc[Y]+GRAVITY)*(*body_posi).Foot[R].pos[X] - (*body_posi).Foot[R].acc[X]*(*body_posi).Foot[R].pos[Y]
+((*body_posi).Foot[L].acc[Y]+GRAVITY)*(*body_posi).Foot[L].pos[X] - (*body_posi).Foot[L].acc[X]*(*body_posi).Foot[L].pos[Y] )
+SUM_LOWER*( ((*body_posi).LowThigh[R].acc[Y]+GRAVITY)*(*body_posi).LowThigh[R].pos[X] - (*body_posi).LowThigh[R].acc[X]*(*body_posi).LowThigh[R].pos[Y]
+((*body_posi).LowThigh[L].acc[Y]+GRAVITY)*(*body_posi).LowThigh[L].pos[X] - (*body_posi).LowThigh[L].acc[X]*(*body_posi).LowThigh[L].pos[Y] )
+SUM_THIGH*( ((*body_posi).UppThigh[R].acc[Y]+GRAVITY)*(*body_posi).UppThigh[R].pos[X] - (*body_posi).UppThigh[R].acc[X]*(*body_posi).UppThigh[R].pos[Y]
+((*body_posi).UppThigh[L].acc[Y]+GRAVITY)*(*body_posi).UppThigh[L].pos[X] - (*body_posi).UppThigh[L].acc[X]*(*body_posi).UppThigh[L].pos[Y] )
+MASS_BODY*( ((*body_posi).Body.acc[Y]+GRAVITY)*(*body_posi).Body.pos[X] - (*body_posi).Body.acc[X]*(*body_posi).Body.pos[Y] )
)/(
+SUM_FOOT*( (*body_posi).Foot[R].acc[Y] +GRAVITY + (*body_posi).Foot[L].acc[Y] +GRAVITY )
+SUM_LOWER*( (*body_posi).LowThigh[R].acc[Y]+GRAVITY + (*body_posi).LowThigh[L].acc[Y]+GRAVITY )
+SUM_THIGH*( (*body_posi).UppThigh[R].acc[Y]+GRAVITY + (*body_posi).UppThigh[L].acc[Y]+GRAVITY )
+MASS_BODY *( (*body_posi).Body.acc[Y] +GRAVITY )
);
//simi to limit ZMP. determine actual limits
/*if (a > 300)
a = 299.0;
else if (a < -300)
a = -299.0;*/
if (abs(a) > 100)
a = preZMP;
return ( a );
}
// ************** CoGの計算 **************************//
//
// *****************************************************//
int Calculation_Center_of_Gravity( struct body_posi *body_posi ){
//上腿、下腿、足のそれぞれの重心の計算(R/L, X,Y)
(*body_posi).UppThigh[R].pos[X]=( (*body_posi).Hip[R][X]+(*body_posi).Knee[R][X] )/2; (*body_posi).UppThigh[R].pos[Y] = ( (*body_posi).Hip[R][Y]+(*body_posi).Knee[R][Y] ) /2;
(*body_posi).LowThigh[R].pos[X]=( (*body_posi).Knee[R][X]+(*body_posi).Ankle[R][X] )/2; (*body_posi).LowThigh[R].pos[Y] = ( (*body_posi).Knee[R][Y]+(*body_posi).Ankle[R][Y] ) /2;
(*body_posi).Foot[R].pos[X]=( (*body_posi).Ankle[R][X]+(*body_posi).Toe[R][X] )/2; (*body_posi).Foot[R].pos[Y] = ( (*body_posi).Ankle[R][Y]+(*body_posi).Toe[R][Y] ) /2;
(*body_posi).UppThigh[L].pos[X]=( (*body_posi).Hip[L][X]+(*body_posi).Knee[L][X] )/2; (*body_posi).UppThigh[L].pos[Y] = ( (*body_posi).Hip[L][Y]+(*body_posi).Knee[L][Y] ) /2;
(*body_posi).LowThigh[L].pos[X]=( (*body_posi).Knee[L][X]+(*body_posi).Ankle[L][X] )/2; (*body_posi).LowThigh[L].pos[Y] = ( (*body_posi).Knee[L][Y]+(*body_posi).Ankle[L][Y] ) /2;
(*body_posi).Foot[L].pos[X]=( (*body_posi).Ankle[L][X]+(*body_posi).Toe[L][X] )/2; (*body_posi).Foot[L].pos[Y] = ( (*body_posi).Ankle[L][Y]+(*body_posi).Toe[L][Y] ) /2;
(*body_posi).CoG[X]=0;
(*body_posi).CoG[Y]=0;
(*body_posi).CoG[X] = ( (*body_posi).Body.pos[X]*(SUM_BODY)
+(*body_posi).UppThigh[R].pos[X]*(SUM_THIGH) + (*body_posi).LowThigh[R].pos[X]*(SUM_LOWER) + (*body_posi).Foot[R].pos[X]*(SUM_FOOT)
+(*body_posi).UppThigh[L].pos[X]*(SUM_THIGH) + (*body_posi).LowThigh[L].pos[X]*(SUM_LOWER) + (*body_posi).Foot[L].pos[X]*(SUM_FOOT) )/ MASS_ALL;
(*body_posi).CoG[Y] = ( (*body_posi).Body.pos[Y]*(SUM_BODY)
+(*body_posi).UppThigh[R].pos[Y]*(SUM_THIGH) + (*body_posi).LowThigh[R].pos[Y]*(SUM_LOWER) + (*body_posi).Foot[R].pos[Y]*(SUM_FOOT)
+(*body_posi).UppThigh[L].pos[Y]*(SUM_THIGH) + (*body_posi).LowThigh[L].pos[Y]*(SUM_LOWER) + (*body_posi).Foot[L].pos[Y]*(SUM_FOOT) )/ MASS_ALL;
//デバッグ用
//printf("②CoG[X] =%lf\t, %lf\t\n ", (*body_posi).CoG[X],(*body_posi).CoG[Y] );
return 0;
}
// ************** ZMPとCoGの計算 **************************//
//
// *****************************************************//
void Calculate_ZMP_COG(int t,struct body_posi *body_posi){
(*body_posi).XZmp=0;
// calculate center of gravity of each body and whole body //
Calculation_Center_of_Gravity(body_posi);
// calculate velocity and acceleration //
//static double pre[3][7][2]; //pos, vel, acc: Body, RUpp,LUpp,...,X, Y
static float buf[7][2][2][3][FILTER_LEN]; //Body, RUpp,RLow, RFoot,LUpp,...: pos, vel, acc:X, Y.[2][3]
Calculate_VelocityAcceleration(t, body_posi->Body.pos, &body_posi->Body, buf[0]); //simi changed since buf is static
for (int i = 0; i < 2; i++) {
Calculate_VelocityAcceleration(t, body_posi->UppThigh[i].pos, &body_posi->UppThigh[i], buf[1 + i * 3]);
Calculate_VelocityAcceleration(t, body_posi->LowThigh[i].pos, &body_posi->LowThigh[i], buf[2 + i * 3]);
Calculate_VelocityAcceleration(t, body_posi->Foot[i].pos, &body_posi->Foot[i], buf[3 + i * 3]);
}
// calculate ZMP //
(*body_posi).XZmp = Calculation_ZMP(body_posi);
}
/********************************************************
関数名:Calculate_JacobMatrix
説明 :股関節基準のヤコビ行列の計算
メインループ内で使用
引数 :double enc_data[2][3] 3×3の単位行列
struct leg_state *Leg 遊脚,立脚の判定
struct jacob_data *jacobData ヤコビ行列関係の構造体
出力 :struct jacob_data *jacobData
********************************************************/
void Calculate_JacobMatrix(double angData[3][2][3], struct leg_state *Leg, struct jacob_data *jacobData){
//地面(x-y座標系)に対しての角度なので注意 Note that the angle is with respect to the ground (x-y coordinate system)
double q[2][3];
double dq[2][3];
double body_ang = 0;
/*[(HK * cos(t1 + t2)) / 2 - HK / 2 + KA * cos(t1 - t2 + t3 - t4 + t5) - KA * cos(t1) - (HK * cos(t1 - t2)) / 2 - (HK * cos(2 * t1)) / 2 + AT * sin(t1 - t2 + t3 - t4 + t5 + t6) + HK * cos(t1 - t2 + t3 - t4), (HK * cos(t1 + t2)) / 2 - HK / 2 + KA * cos(t1 - t2 + t3 - t4 + t5) - (HK * cos(t1 - t2)) / 2 - (HK * cos(2 * t1)) / 2 + AT * sin(t1 - t2 + t3 - t4 + t5 + t6) + HK * cos(t1 - t2 + t3 - t4), KA * cos(t1 - t2 + t3 - t4 + t5) + AT * sin(t1 - t2 + t3 - t4 + t5 + t6) + HK * cos(t1 - t2 + t3 - t4), KA * cos(t1 - t2 + t3 - t4 + t5) + AT * sin(t1 - t2 + t3 - t4 + t5 + t6) + HK * cos(t1 - t2 + t3 - t4), KA * cos(t1 - t2 + t3 - t4 + t5) + AT * sin(t1 - t2 + t3 - t4 + t5 + t6), AT * sin(t1 - t2 + t3 - t4 + t5 + t6)]
[KA * sin(t1) - (HK * sin(t1 + t2)) / 2 - KA * sin(t1 - t2 + t3 - t4 + t5) - HK * sin(t1 - t2 + t3 - t4) + (HK * sin(t1 - t2)) / 2 + AT * cos(t1 - t2 + t3 - t4 + t5 + t6) + (HK * sin(2 * t1)) / 2, (HK * sin(t1 - t2)) / 2 - (HK * sin(t1 + t2)) / 2 - KA * sin(t1 - t2 + t3 - t4 + t5) - HK * sin(t1 - t2 + t3 - t4) + AT * cos(t1 - t2 + t3 - t4 + t5 + t6) + (HK * sin(2 * t1)) / 2, AT * cos(t1 - t2 + t3 - t4 + t5 + t6) - KA * sin(t1 - t2 + t3 - t4 + t5) - HK * sin(t1 - t2 + t3 - t4), AT * cos(t1 - t2 + t3 - t4 + t5 + t6) - KA * sin(t1 - t2 + t3 - t4 + t5) - HK * sin(t1 - t2 + t3 - t4), AT * cos(t1 - t2 + t3 - t4 + t5 + t6) - KA * sin(t1 - t2 + t3 - t4 + t5), AT * cos(t1 - t2 + t3 - t4 + t5 + t6)]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[1, 1, 1, 1, 1, 1]*/
//遊脚の地面基準の関節角度 Ground angle of swing leg
//Calculation_Swing_Angle(angData[POS], Leg, q, body_ang); //simi commented
for (int j=0;j<3;j++){
q[R][j] = angData[POS][R][j];//simi added to make R H origin, indep of left leg
dq[R][j] = angData[VEL][R][j];
q[L][j] = angData[POS][L][j];
dq[L][j] = angData[VEL][L][j];
}
printf("H %lf, K %lf, A %lf\n", q[R][H], q[R][K], q[R][A]); //print cur
//ヤコビの計算(2×3) 地面(x-y平面)での腰基準ヤコビアン(行列計算の簡単化のため3×3)
//Jacobi calculation (2 × 5) 腰 ankle reference Jacobian on the ground (x-y plane) (3 × 5 to simplify matrix calculation)
jacobData->matrix[0][0] = (LEN_H_K * cos(q[L][A] + q[L][K])) / 2 - LEN_H_K / 2 + LEN_K_A * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) - LEN_K_A * cos(q[L][A]) - (LEN_H_K * cos(q[L][A] - q[L][K])) / 2 - (LEN_H_K * cos(2 * q[L][A])) / 2 + LEN_A_T * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]) + LEN_H_K * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H]);
jacobData->matrix[0][1] = (LEN_H_K * cos(q[L][A] + q[L][K])) / 2 - LEN_H_K / 2 + LEN_K_A * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) - (LEN_H_K * cos(q[L][A] - q[L][K])) / 2 - (LEN_H_K * cos(2 * q[L][A])) / 2 + LEN_A_T * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]) + LEN_H_K * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H]);
jacobData->matrix[0][2] = + LEN_K_A * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) + LEN_A_T * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]) + LEN_H_K * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H]);
jacobData->matrix[0][3] = + LEN_K_A * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) + LEN_A_T * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]) + LEN_H_K * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H]);
jacobData->matrix[0][4] = + LEN_K_A * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) + LEN_A_T * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]);
jacobData->matrix[0][5] = + LEN_A_T * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]);
jacobData->matrix[1][0] = LEN_K_A * sin(q[L][A]) - (LEN_H_K * sin(q[L][A] + q[L][K])) / 2 - LEN_K_A * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) - LEN_H_K * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H]) + (LEN_H_K * sin(q[L][A] - q[L][K])) / 2 + LEN_A_T * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]) + (LEN_H_K * sin(2 * q[L][A])) / 2;
jacobData->matrix[1][1] = - (LEN_H_K * sin(q[L][A] + q[L][K])) / 2 - LEN_K_A * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) - LEN_H_K * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H]) + (LEN_H_K * sin(q[L][A] - q[L][K])) / 2 + LEN_A_T * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]) + (LEN_H_K * sin(2 * q[L][A])) / 2;
jacobData->matrix[1][2] = - LEN_K_A * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) - LEN_H_K * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H]) + LEN_A_T * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]);
jacobData->matrix[1][3] = - LEN_K_A * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) - LEN_H_K * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H]) + LEN_A_T * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]);
jacobData->matrix[1][4] = - LEN_K_A * sin(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K]) + LEN_A_T * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]);
jacobData->matrix[1][5] = + LEN_A_T * cos(q[L][A] - q[L][K] + q[L][H] - q[R][H] + q[R][K] + q[R][A]);
jacobData->matrix[2][0] = 0.0;
jacobData->matrix[2][1] = 0.0;
jacobData->matrix[2][2] = 0.0;
jacobData->matrix[2][3] = 0.0;
jacobData->matrix[2][4] = 0.0;
jacobData->matrix[2][5] = 0.0;
jacobData->matrix[3][0] = 0.0;
jacobData->matrix[3][1] = 0.0;
jacobData->matrix[3][2] = 0.0;
jacobData->matrix[3][3] = 0.0;
jacobData->matrix[3][4] = 0.0;
jacobData->matrix[3][5] = 0.0;
jacobData->matrix[4][0] = 0.0;
jacobData->matrix[4][1] = 0.0;
jacobData->matrix[4][2] = 0.0;
jacobData->matrix[4][3] = 0.0;
jacobData->matrix[4][4] = 0.0;
jacobData->matrix[4][5] = 0.0;
jacobData->matrix[5][0] = 1.0;
jacobData->matrix[5][1] = 1.0;
jacobData->matrix[5][2] = 1.0;
jacobData->matrix[5][3] = 1.0;
jacobData->matrix[5][4] = 1.0;
jacobData->matrix[5][5] = 1.0;
////ヤコビの微分の計算(2×3) 地面(x-y平面)での腰基準ヤコビアン(行列計算の簡単化のため3×5)
//jacobData->diff[0][0] = -LEN_K_A*(dq[L][A])*sin( q[L][A]) - LEN_H_K*( dq[L][A] + dq[L][K])*sin( q[L][A] + q[L][K]) - LEN_H_K*( dq[L][A] + dq[L][K] + dq[R][H])*sin( q[L][A] + q[L][K] + q[R][H]) - LEN_K_A*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] )*sin( q[L][A] + q[L][K] + q[R][H] + q[R][K] ) - LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*sin( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[0][1] = - LEN_H_K*( dq[L][A] + dq[L][K])*sin( q[L][A] + q[L][K]) - LEN_H_K*( dq[L][A] + dq[L][K] + dq[R][H])*sin( q[L][A] + q[L][K] + q[R][H]) - LEN_K_A*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] )*sin( q[L][A] + q[L][K] + q[R][H] + q[R][K] ) - LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*sin( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[0][2] = - LEN_H_K*( dq[L][A] + dq[L][K] + dq[R][H])*sin( q[L][A] + q[L][K] + q[R][H]) - LEN_K_A*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] )*sin( q[L][A] + q[L][K] + q[R][H] + q[R][K] ) - LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*sin( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[0][3] = - LEN_K_A*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] )*sin( q[L][A] + q[L][K] + q[R][H] + q[R][K] ) - LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*sin( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[0][4] = - LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*sin( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[1][0] = LEN_K_A*(dq[L][A])*cos( q[L][A]) + LEN_H_K*( dq[L][A] + dq[L][K])*cos( q[L][A] + q[L][K]) + LEN_H_K*( dq[L][A] + dq[L][K] + dq[R][H])*cos( q[L][A] + q[L][K] + q[R][H]) + LEN_K_A*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] )*cos( q[L][A] + q[L][K] + q[R][H] + q[R][K] ) + LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*cos( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[1][1] = + LEN_H_K*( dq[L][A] + dq[L][K])*cos( q[L][A] + q[L][K]) + LEN_H_K*( dq[L][A] + dq[L][K] + dq[R][H])*cos( q[L][A] + q[L][K] + q[R][H]) + LEN_K_A*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] )*cos( q[L][A] + q[L][K] + q[R][H] + q[R][K] ) + LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*cos( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[1][2] = + LEN_H_K*( dq[L][A] + dq[L][K] + dq[R][H])*cos( q[L][A] + q[L][K] + q[R][H]) + LEN_K_A*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] )*cos( q[L][A] + q[L][K] + q[R][H] + q[R][K] ) + LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*cos( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[1][3] = + LEN_K_A*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] )*cos( q[L][A] + q[L][K] + q[R][H] + q[R][K] ) + LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*cos( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[1][4] = + LEN_A_T*( dq[L][A] + dq[L][K] + dq[R][H] + dq[R][K] + dq[R][A] )*cos( q[L][A] + q[L][K] + q[R][H] + q[R][K] + q[R][A] );
//jacobData->diff[2][0] = 0.0;
//jacobData->diff[2][1] = 0.0;
//jacobData->diff[2][2] = 0.0;
//jacobData->diff[2][3] = 0.0;
//jacobData->diff[2][4] = 0.0;
//jacobData->diff[3][0] = 0.0;
//jacobData->diff[3][1] = 0.0;
//jacobData->diff[3][2] = 0.0;
//jacobData->diff[3][3] = 0.0;
//jacobData->diff[3][4] = 0.0;
//jacobData->diff[4][0] = 0.0;
//jacobData->diff[4][1] = 0.0;
//jacobData->diff[4][2] = 0.0;
//jacobData->diff[4][3] = 0.0;
//jacobData->diff[4][4] = 0.0;
//
// //ヤコビの微分の計算(2×3) 地面(x-y平面)での腰基準ヤコビアン(行列計算の簡単化のため3×3)
// jacobData->diff[0][0] = -LEN_H_K*( dq[H] )*sin( q[H] ) - LEN_K_A*( dq[H] + dq[K] )*sin( q[H] + q[K] ) - LEN_A_T*( dq[H] + dq[K] + dq[A] )*sin( q[H] + q[K] + q[A] );
// jacobData->diff[0][1] = - LEN_K_A*( dq[H] + dq[K] )*sin( q[H] + q[K] ) - LEN_A_T*( dq[H] + dq[K] + dq[A] )*sin( q[H] + q[K] + q[A] );
// jacobData->diff[0][2] = - LEN_A_T*( dq[H] + dq[K] + dq[A] )*sin( q[H] + q[K] + q[A] );
// jacobData->diff[1][0] = LEN_H_K*( dq[H] )*cos( q[H] ) + LEN_K_A*( dq[H] + dq[K] )*cos( q[H] + q[K] ) + LEN_A_T*( dq[H] + dq[K] + dq[A] )*cos( q[H] + q[K] + q[A] );
// jacobData->diff[1][1] = LEN_K_A*( dq[H] + dq[K] )*cos( q[H] + q[K] ) + LEN_A_T*( dq[H] + dq[K] + dq[A] )*cos( q[H] + q[K] + q[A] );
// jacobData->diff[1][2] = LEN_A_T*( dq[H] + dq[K] + dq[A] )*cos( q[H] + q[K] + q[A] );
// jacobData->diff[2][0] = 0.0;
// jacobData->diff[2][1] = 0.0;
// jacobData->diff[2][2] = 0.0;
//ヤコビ行列の転置の計算
//Matrix33_Trans(jacobData->matrix, jacobData->trans);
Matrix66_Trans(jacobData->matrix, jacobData->trans);
//ヤコビ行列の擬似逆行列の計算
//Matrix33_PseudoInverse(jacobData->matrix, jacobData->pse);
//擬似逆行列の転置の計算
//Matrix33_Trans(jacobData->pse, jacobData->pse_trans);
/*for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("[%d][%d]%lf\t",i,j,jacobData->pse[i][j]);
}
printf("\n");
}
printf("\n");*/
}
/********************************************************
関数名:Calculate_JacobMatrix
説明 :股関節基準のヤコビ行列の計算
メインループ内で使用
引数 :double enc_data[2][3] 3×3の単位行列
struct leg_state *Leg 遊脚,立脚の判定
struct jacob_data *jacobData ヤコビ行列関係の構造体
struct jacob_data *jacobData ヤコビ行列関係の構造体
出力 :struct jacob_data *jacobData
********************************************************/
void Calculate_JacobMatrix(double angData[3][2][3], struct leg_state *Leg, struct jacob_hip *jh,struct jacob_body *jb){//sit ankle base- SUP leg
//地面(x-y座標系)に対しての角度なので注意 Note that the angle is with respect to the ground (x-y coordinate system)
double q[3];
double dq[3];
double body_ang = 0;
////for body only (sitting)
//Jb = np.array([[+LEN_BODY * math.cos(q[H] + q[A] + q[K]) + LEN_H_K * math.cos(-q[A] - q[K]) + LEN_K_A * math.cos(q[A])
// , +LEN_BODY * math.cos(q[H] + q[A] + q[K]) + LEN_H_K * math.cos(-q[A] - q[K])
// , +LEN_BODY * math.cos(q[A] + q[K] + q[H])],
// [+LEN_BODY * math.sin(q[A] + q[K] + q[H]) - LEN_H_K * math.sin(-q[A] - q[K]) + LEN_K_A * math.sin(q[A])
// , +LEN_BODY * math.sin(q[A] + q[K] + q[H]) - LEN_H_K * math.sin(-q[A] - q[K])
// , +LEN_BODY * math.sin(q[A] + q[K] + q[H])],
// [0., 0., 0.]] )
//Jh = np.array([[+LEN_H_K * math.cos(-q[A] - q[K]) + LEN_K_A * math.cos(q[A])
// , LEN_H_K * math.cos(-q[A] - q[K])],
// [-LEN_H_K * math.sin(-q[A] - q[K]) + LEN_K_A * math.sin(q[A])
// , -LEN_H_K * math.sin(-q[A] - q[K])],
// [0., 0.]] )
//遊脚の地面基準の関節角度 Ground angle of swing leg
//Calculation_Swing_Angle(angData[POS], Leg, q, body_ang); //simi commented
for (int j=0;j<3;j++)
q[j] = angData[POS][L][j];//simi added to make R H origin, indep of left leg. NOTE: SUP(L) angles may be opp of SW angles, in which case make these neg
for(int i=0;i<3;i++) dq[i] = angData[VEL][L][i];
printf("H %lf, K %lf, A %lf\n", q[H], q[K], q[A]); //print cur
//ヤコビの計算(2×3) 地面(x-y平面)での腰基準ヤコビアン(行列計算の簡単化のため3×3)
//Jacobi calculation (2 × 3) 腰 waist reference Jacobian on the ground (x-y plane) (3 × 3 to simplify matrix calculation)
jb->matrix[0][0] = LEN_BODY * cos(q[A] + q[K] + q[H]) + LEN_H_K * cos(-q[A] - q[K]) + LEN_K_A * cos(q[A]);
jb->matrix[0][1] = LEN_BODY * cos(q[A] + q[K] + q[H]) + LEN_H_K * cos(-q[A] - q[K]);
jb->matrix[0][2] = LEN_BODY * cos(q[A] + q[K] + q[H]);
jb->matrix[1][0] = LEN_BODY * sin(q[A] + q[K] + q[H]) - LEN_H_K * sin(-q[A] - q[K]) + LEN_K_A * sin(q[A]);
jb->matrix[1][1] = LEN_BODY * sin(q[A] + q[K] + q[H]) - LEN_H_K * sin(-q[A] - q[K]);
jb->matrix[1][2] = LEN_BODY * sin(q[A] + q[K] + q[H]);
jb->matrix[2][0] = 0;
jb->matrix[2][1] = 0;
jb->matrix[1][2] = 0;
jh->matrix[0][0] = + LEN_H_K * cos(-q[A] - q[K]) + LEN_K_A * cos(q[A]);
jh->matrix[0][1] = + LEN_H_K * cos(-q[A] - q[K]);
jh->matrix[1][0] = - LEN_H_K * sin(-q[A] - q[K]) + LEN_K_A * sin(q[A]);
jh->matrix[1][1] = - LEN_H_K * sin(-q[A] - q[K]);
jh->matrix[2][0] = 0;
jh->matrix[2][1] = 0;
////ヤコビの微分の計算(2×3) 地面(x-y平面)での腰基準ヤコビアン(行列計算の簡単化のため3×3)
//Jb->diff[0][0] = -LEN_H_K*( dq[H] )*sin( q[H] ) - LEN_K_A*( dq[H] + dq[K] )*sin( q[H] + q[K] ) - LEN_A_T*( dq[H] + dq[K] + dq[A] )*sin( q[H] + q[K] + q[A] );
//Jb->diff[0][1] = - LEN_K_A*( dq[H] + dq[K] )*sin( q[H] + q[K] ) - LEN_A_T*( dq[H] + dq[K] + dq[A] )*sin( q[H] + q[K] + q[A] );
//Jb->diff[0][2] = - LEN_A_T*( dq[H] + dq[K] + dq[A] )*sin( q[H] + q[K] + q[A] );
//Jb->diff[1][0] = LEN_H_K*( dq[H] )*cos( q[H] ) + LEN_K_A*( dq[H] + dq[K] )*cos( q[H] + q[K] ) + LEN_A_T*( dq[H] + dq[K] + dq[A] )*cos( q[H] + q[K] + q[A] );
//Jb->diff[1][1] = LEN_K_A*( dq[H] + dq[K] )*cos( q[H] + q[K] ) + LEN_A_T*( dq[H] + dq[K] + dq[A] )*cos( q[H] + q[K] + q[A] );
//Jb->diff[1][2] = LEN_A_T*( dq[H] + dq[K] + dq[A] )*cos( q[H] + q[K] + q[A] );
//Jb->diff[2][0] = 0.0;
//Jb->diff[2][1] = 0.0;
//Jb->diff[2][2] = 0.0;
//ヤコビ行列の転置の計算
Matrix33_Trans(jb->matrix, jb->trans);
Matrix22_Trans(jh->matrix, jh->trans);
////ヤコビ行列の擬似逆行列の計算
//Matrix33_PseudoInverse(jacobData->matrix, jacobData->pse);
////擬似逆行列の転置の計算
//Matrix33_Trans(jacobData->pse, jacobData->pse_trans);
/*for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("[%d][%d]%lf\t",i,j,jacobData->pse[i][j]);
}
printf("\n");
}
printf("\n");*/
}
void Calculate_JacobMatrix(double angData[3][2][3], struct leg_state* Leg, struct jacob_body* jacob_data) { //asc/desc hip base-SW leg
/*np.array([[+LEN_H_K * math.cos(q[H]) + LEN_K_A * math.cos(q[H] + q[K]) + LEN_A_T * math.sin(q[H] + q[K] + q[A]) #prev - LEN_H_K
, +LEN_K_A * math.cos(q[H] + q[K]) + LEN_A_T * math.sin(q[H] + q[K] + q[A])
, LEN_A_T * math.sin(q[H] + q[K] + q[A])],
[LEN_H_K * math.sin(q[H]) + LEN_K_A * math.sin(q[H] + q[K]) + LEN_A_T * math.cos(q[H] + q[K] + q[A])
, LEN_K_A * math.sin(q[H] + q[K]) + LEN_A_T * math.cos(q[H] + q[K] + q[A])
, LEN_A_T * math.cos(q[H] + q[K] + q[A])],
[0., 0., 0.]] )*/
double q[3];
double dq[3];
double body_ang = 0;
for (int j = 0;j < 3;j++)
q[j] = angData[POS][R][j];//simi added to make R H origin, indep of left leg
for (int i = 0;i < 3;i++) dq[i] = angData[VEL][R][i];
//printf("H %lf, K %lf, A %lf\n", q[H], q[K], q[A]); //print cur
//ヤコビの計算(2×3) 地面(x-y平面)での腰基準ヤコビアン(行列計算の簡単化のため3×3)
//Jacobi calculation (2 × 3) 腰 waist reference Jacobian on the ground (x-y plane) (3 × 3 to simplify matrix calculation)
jacob_data->matrix[0][0] = LEN_H_K * cos(q[H]) + LEN_K_A * cos(q[H] + q[K]) + LEN_A_T * sin(q[H] + q[K] + q[A]);
jacob_data->matrix[0][1] = LEN_K_A * cos(q[H] + q[K]) + LEN_A_T * sin(q[H] + q[K] + q[A]);
jacob_data->matrix[0][2] = LEN_A_T * sin(q[H] + q[K] + q[A]);
jacob_data->matrix[1][0] = LEN_H_K * sin(q[H]) + LEN_K_A * sin(q[H] + q[K]) + LEN_A_T * cos(q[H] + q[K] + q[A]);
jacob_data->matrix[1][1] = LEN_K_A * sin(q[H] + q[K]) + LEN_A_T * cos(q[H] + q[K] + q[A]);
jacob_data->matrix[1][2] = LEN_A_T * cos(q[H] + q[K] + q[A]);
jacob_data->matrix[2][0] = 0;
jacob_data->matrix[2][1] = 0;
jacob_data->matrix[1][2] = 0;
//ヤコビ行列の転置の計算
Matrix33_Trans(jacob_data->matrix, jacob_data->trans);
}
/**************************** インピーダンス制御関係 *****************************************************************************************************/
//int Impedance_Control_Acc(double foot[3][2][3], double target[3][2][3], double force[3]){
//
//
// //インピーダンス制御
// //足先加速度から
// force[X] = 0.0;
// force[Y] = ( MASS_ALL-MASS_d )*foot[ACC][R][Y]
// - VIS_Y*( foot[VEL][R][Y] - target[VEL][R][Y] )
// - SPR_Y*( foot[POS][R][Y] - target[POS][R][Y] );
// force[Z] = 0.0;
//
// return 0;
//}
//
//
//int Impedance_Control_Force(double foot[3][2][3], double target[3][2][3], double Force_d[3], double force[3]){
//
// double mass = MASS_ALL/MASS_d;
//
// //インピーダンス制御
// //足先力から
// force[X] = 0.0;
// force[Y] = ( mass - 1 )*Force_d[Y]
// - mass*VIS_Y*( foot[VEL][R][Y] - target[VEL][R][Y] )
// - mass*SPR_Y*( foot[POS][R][Y] - target[POS][R][Y] );
//
// force[Z] = 0.0;
//
// return 0;
//}
/********************************************************
関数名:ConvertForceToAcc
説明 :つま先の力からつま先の加速度に変換
引数 :double force[3] //つま先に加わる力
double acc[3] //つま先に生じる加速度
出力 :double acc[3]
********************************************************/
void ConvertForceToAcc(double force[3], double acc[3]){
//MASS_FOOTを質量行列に変更する必要あり?
acc[X] = force[X]/MASS_FOOT;
acc[Y] = force[Y]/MASS_FOOT;
acc[Z] = force[Z]/MASS_FOOT;
}
/**************************** 冗長性利用関係 *****************************************************************************************************/
/********************************************************
関数名:Calculate_UsingRedundancy2
説明 :トルクに関する冗長性利用を計算する関数
メインループ内で使用
引数 :double jointVal[2][3] エンコーダの値を元にした位置,速度
double targetAcc[3] 認知アシストにより与えられる加速度
struct jacob_data jacobData ヤコビ行列関係の構造体
double redundancyAcc[3] 冗長性利用プログラムにより得られた関節角加速度
出力 :double redundancyAcc[3]
********************************************************/
void Calculate_UsingRedundancy2(double jointVal[3][2][3], double force[3], double error[3][3], struct jacob_data *J, struct dynamic_data *D, double torq[3], double torq2[3]){
double I[3][3];
double nullSpaceVector[3];
double K_f[3][3] = {
{ 1.0, 0.0, 0.0 },
{ 0.0, 1.5, 0.0 },
{ 0.0, 0.0, 1.0 }
};
double D_d[3][3] = {
{ 1.0, 0.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 0.0 }
};
double K_d[3][3] = {
{ 1.0, 0.0, 0.0 },
{ 0.0, -290.0, 0.0 },
{ 0.0, 0.0, 0.0 }
};
//単位行列の計算
Matrix_Unit(I);
double F_tmp[3][3]={0.0};
double F[3]={0.0}, X_d[3]={0.0}, X[3]={0.0};
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
F_tmp[i][j] = K_f[i][j] - I[i][j];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
F[i] += F_tmp[i][j]*force[j];
X_d[i] += D_d[i][j] *error[VEL][j];
X[i] += K_d[i][j] *error[POS][j];
}
}
//ゼロ空間ベクトルの計算( (I-J#J)k )
Calculate_NullSpace(jointVal, J, nullSpaceVector);
double M[3][3]={0}, h[3]={0};
double M_tmp[3][3];
//Matrix33_Product(J->pse_trans, D->mass, M_tmp);
//Matrix33_Product(M_tmp, J->pse, M);
double V_tmp1[3]={0}, V_tmp2[3]={0}, V_tmp3[3]={0}; //一時保存のためのベクトル
//冗長性を利用した関節角加速度の計算
//( dJdq )
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
V_tmp1[i] += J->diff[i][j]*jointVal[VEL][R][j];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
V_tmp2[i] += M[i][j]*V_tmp1[j];
V_tmp3[i] += J->pse_trans[i][j]*D->h[j];
}
h[i] = V_tmp2[i] + V_tmp3[i];
}
double torq_tmp[3]={0.0};
for(int i=0;i<3;i++)
torq_tmp[i] = ( h[i] - X_d[i] - X[i]);// + F[i] ;//- nullSpaceVector[i];
for(int i=0;i<3;i++){
torq[i] = 0;
for(int j=0;j<3;j++){
torq[i] += J->trans[i][j]*torq_tmp[j];
}
}
for(int i=0;i<3;i++) torq2[i] = torq[i] + nullSpaceVector[i];
//デバック
//printf("%lf %lf %lf\n",nullSpaceVector[0], nullSpaceVector[1], nullSpaceVector[2]);
//printf("%lf %lf %lf\n",redundancyAcc[0], redundancyAcc[1], redundancyAcc[2]);
/*for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("%lf\t",jacobData->diff[i][j]);
}
printf("\n");
}
printf("\n");*/
}
/********************************************************
関数名:Calculate_NullSpace
説明 :冗長性利用におけるゼロ空間ベクトルの計算( (I-J#J)k )
引数 :double jointVal[3][2][3] 関節の角度,角速度,角加速度
double null_space[3][3] ヤコビ行列関係の構造体
double nullSpaceVector[3] ゼロ空間ベクトル
出力 :double nullSpaceVector[3]
********************************************************/
void Calculate_NullSpace(double jointVal[3][2][3], struct jacob_data *jacobData, double nullSpaceVector[3]){
double eta[3]; //ゼロ空間ベクトルの任意係数η
//ゼロ空間の計算( (I-J#J) )
NullSpace(jacobData);
//ゼロ空間ベクトルのための評価関数の計算( k )
EvaluationFunc_NullSpace(jointVal[POS][R], eta);
//ゼロ空間ベクトルの計算( (I-J#J)k )
//Calculate_NullSpaceVector(jacobData->null_space, eta, nullSpaceVector);
//デバック
//printf("%lf %lf %lf\n",eta[0], eta[1], eta[2]);
//printf("%lf %lf %lf\n",redundancyAcc[0], redundancyAcc[1], redundancyAcc[2]);
/*for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("%lf\t",jacobData->diff[i][j]);
}
printf("\n");
}
printf("\n");*/
}
/********************************************************
関数名:Calculate_UsingRedundancy
説明 :関節角加速度に関する冗長性利用を計算する関数
メインループ内で使用
引数 :double jointVal[2][3] エンコーダの値を元にした位置,速度
double targetAcc[3] 認知アシストにより与えられる加速度
struct jacob_data jacobData ヤコビ行列関係の構造体
double redundancyAcc[3] 冗長性利用プログラムにより得られた関節角加速度
出力 :double redundancyAcc[3]
********************************************************/
void Calculate_UsingRedundancy(double jointVal[3][2][3], double targetAcc[3], struct jacob_data *jacobData, double redundancyAcc[3]){
double eta[3];
double nullSpaceVector[3];
//ゼロ空間の計算( (I-J#J) )
NullSpace(jacobData);
//ゼロ空間ベクトルのための評価関数の計算( k )
EvaluationFunc_NullSpace(jointVal[POS][R], eta);
//ゼロ空間ベクトルの計算( (I-J#J)k )
// Calculate_NullSpaceVector(jacobData->null_space, eta, nullSpaceVector);
//冗長性利用後の各関節の角加速度の計算( d2q = J#(d2r-dJdq) + (I-J#J)k )
Calculate_JointAcc_UsingRedundancy(targetAcc, jointVal[VEL][R], jacobData, nullSpaceVector, redundancyAcc);
//デバック
//printf("%lf %lf %lf\n",eta[0], eta[1], eta[2]);
//printf("%lf %lf %lf\n",redundancyAcc[0], redundancyAcc[1], redundancyAcc[2]);
/*for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("%lf\t",jacobData->diff[i][j]);
}
printf("\n");
}
printf("\n");*/
}
/********************************************************
関数名:NullSpace
説明 :冗長性利用におけるゼロ空間の計算(この行列がゼロ行列でなければ冗長性が利用できる)
引数 :struct jacob_data jacobData ヤコビ行列関係の構造体
出力 :struct jacob_data jacobData
********************************************************/
void NullSpace(struct jacob_data *jacobData){
double I[3][3]; //単位行列
double M[3][3]; //一時保存のための行列
double N[3][3];
//単位行列の計算
Matrix_Unit(I);
//(J#)(J)の計算
//Matrix33_Product(jacobData->pse, jacobData->matrix, M);
//I-J#Jの計算
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
jacobData->null_space[i][j] = I[i][j]-M[i][j];
/*for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("[%d][%d]%lf\t",i,j,jacobData->null_space[i][j]);
}
printf("\n");
}
printf("\n");*/
}
///********************************************************
//関数名:NullSpace
//説明 :冗長性利用におけるゼロ空間のための評価関数の計算
//引数 :struct jacob_data jacobData ヤコビ行列関係の構造体
//
//出力 :struct jacob_data jacobData
//********************************************************/
//void EvaluationFunc_NullSpace(double enc_data[3], double eta[3]){
//
// double enc_min[3] = { 50, -60, -12.5 }; //各関節の関数の最小値
// for(int i=0;i<3;i++) enc_min[i] = enc_min[i]*PI/180; //deg→rad
//
// //重み ここを変更することで各関節トルクのウエイト値が変更されエネルギー効率を考慮することになる
// double Coeff[3][3] = {
// {100.0, 0.0, 0.0},
// {0.0, 0.0, 0.0},
// {0.0, 0.0, 100.0} };
//
// //各関節と評価関数の関係
// double C[3] = { (1.0/10)*pow( enc_data[H]-enc_min[H], 10.0),
// (1.0/25)*pow(-enc_data[K]+enc_min[K], 10.0), //膝だけエンコーダの向きが逆なので注意
// (1.0/0.01)*pow( enc_data[A]-enc_min[A], 10.0) };
// //Cを各関節角度で偏微分した値
// double C_diff[3] = { (10.0/10)*pow( enc_data[H]-enc_min[H], 9.0),
// -(10.0/25)*pow(-enc_data[K]+enc_min[K], 9.0), //膝だけエンコーダの向きが逆なので注意
// (10.0/0.01)*pow( enc_data[A]-enc_min[A], 9.0) };
//
// //評価関数
// double E = ( C[H] + C[K] + C[A] );
//
// //評価関数を各関節角度で偏微分した値
// double E_diff[3];
// for(int i=0;i<3;i++) E_diff[i] = C_diff[i];
//
// for(int i=0;i<3;i++){
// eta[i] = 0.0;
// for(int j=0;j<3;j++){
// eta[i] += -Coeff[i][j]*E_diff[j];
// }
// }
//
//
// //デバック用
// //printf("%lf %lf %lf\n",C_diff[0], C_diff[1], C_diff[2]);
// printf("%lf %lf %lf\n",eta[0], eta[1], eta[2]);
//}
/********************************************************
関数名:NullSpace
説明 :冗長性利用におけるゼロ空間のための評価関数の計算
引数 :struct jacob_data jacobData ヤコビ行列関係の構造体
出力 :struct jacob_data jacobData
********************************************************/
void EvaluationFunc_NullSpace(double enc_data[3], double eta[3]){
double enc_min[3] = { 20, -50, -12.5 }; //各関節の関数の最小値
for(int i=0;i<3;i++) enc_min[i] = enc_min[i]*PI/180; //deg→rad
//重み ここを変更することで各関節トルクのウエイト値が変更されエネルギー効率を考慮することになる
double Coeff[3][3] = {
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 10.0} };
//各関節と評価関数の関係
double C[3] = { (1.0/2)*pow( enc_data[H]-enc_min[H], 10.0),
(1.0/150)*pow(-enc_data[K]+enc_min[K], 10.0), //膝だけエンコーダの向きが逆なので注意
(1.0/0.1)*pow( enc_data[A]-enc_min[A], 10.0) + (1.0/2.0)*pow( enc_data[A]-15*PI/180, 2.0 ) };
//Cを各関節角度で偏微分した値
double C_diff[3] = { (10.0/2)*pow( enc_data[H]-enc_min[H], 9.0),
-(10.0/150)*pow(-enc_data[K]+enc_min[K], 9.0), //膝だけエンコーダの向きが逆なので注意
(10.0/0.1)*pow( enc_data[A]-enc_min[A], 9.0) + (2.0/2.0)*pow( enc_data[A]-15*PI/180, 1.0 ) };
//評価関数
double E = ( 1 / ( C[H] + C[K] + C[A] + 1 ) );
//評価関数を各関節角度で偏微分した値
double E_diff[3];
for(int i=0;i<3;i++) E_diff[i] = -C_diff[i]/(E*E);
for(int i=0;i<3;i++){
eta[i] = 0.0;
for(int j=0;j<3;j++){
eta[i] += Coeff[i][j]*E_diff[j];
}
}
//for(int i=0;i<3;i++) eta[i] = 0.0;
//デバック用
//printf("%lf %lf %lf\n",C_diff[H], C_diff[K], C_diff[A]);
//printf("%lf %lf %lf %lf\n",eta[0], eta[1], eta[2], E);
}
///********************************************************
//関数名:NullSpace
//説明 :冗長性利用におけるゼロ空間のための評価関数の計算
//引数 :struct jacob_data jacobData ヤコビ行列関係の構造体
//
//出力 :struct jacob_data jacobData
//********************************************************/
//void EvaluationFunc_NullSpace(double enc_data[3], double eta[3]){
//
// double enc[3];
// for(int i=0;i<3;i++) enc[i] = enc_data[i]*180/PI;
//
// //重み ここを変更することで各関節トルクのウエイト値が変更されエネルギー効率を考慮することになる
// double Coeff[3] = { (5.0*pow(10.0,4.0)), (1.0*pow(10.0,5.0)), (2.0*pow(10.0,6.0)) };
//
// //各関節と評価関数の関係
// double C[3] = { pow(10.0, -6.0)*pow( enc[H]-55.0, 4.0),
// pow(10.0, -6.0)*pow(-enc[K]+65.0, 4.0), //膝だけエンコーダの向きが逆なので注意
// pow(10.0, -5.0)*pow( enc[A]+15.0, 4.0) };
// //Cを各関節角度で偏微分した値
// double C_diff[3] = { -4.0*pow(10.0, -6.0)*pow( enc[H]-55.0, 3.0),
// 4.0*pow(10.0, -6.0)*pow(-enc[K]+65.0, 3.0), //膝だけエンコーダの向きが逆なので注意
// -4.0*pow(10.0, -5.0)*pow( enc[A]+15.0, 3.0) };
//
// //評価関数
// double E = 1/( C[H] + C[K] + C[A] + 1 );
//
// //評価関数を各関節角度で偏微分した値
// double E_diff[3];
// for(int i=0;i<3;i++) E_diff[i] = -1.0*C_diff[i]*pow(E, 2.0);
//
// for(int i=0;i<3;i++) eta[i] = Coeff[i]*E_diff[i];
//
//}
/********************************************************
関数名:NullSpace
説明 :冗長性利用におけるゼロ空間ベクトルの計算
引数 :double null_space[3][3] ヤコビ行列関係の構造体
double eta[3] 評価関数
double vector[3] ゼロ空間ベクトル
出力 :struct jacob_data jacobData
********************************************************/
void Calculate_NullSpaceVector(double null_space[3][3], double eta[3], double vector[3]){
for(int i=0;i<3;i++){
vector[i] = 0;
for(int j=0;j<3;j++){
vector[i] += null_space[i][j]*eta[j];
}
}
double vector2=0.0;
for(int i=0;i<3;i++)
vector2 += vector[i]*eta[i];
//printf("%lf\n",vector2);
//デバック用
//printf("%lf %lf %lf\n",eta[H], eta[K], eta[A]);
//printf("%lf %lf %lf\n",vector[H], vector[K], vector[A]);
/*for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("[%d][%d]%lf\t",i,j,null_space[i][j]);
}
printf("\n");
}
printf("\n");*/
}
/********************************************************
関数名:Calculate_JointAcc_UsingRedundancy
説明 :冗長性を利用してサブタスクを与えた各関節の角加速度を計算する関数
引数 :double targetAcc[3] 認知アシストにより与えられる加速度
double jointVel[3] 現在の関節速度
struct jacob_data jacobData ヤコビ行列関係の構造体
double nullSpaceVector[3] ゼロ空間ベクトル
double jointVal[3][3] 冗長性利用プログラムにより得られた関節角速度と角加速度
出力 :double jointVal[3][3]
********************************************************/
void Calculate_JointAcc_UsingRedundancy(double targetAcc[3], double jointVel[3], struct jacob_data *jacobData, double nullSpaceVector[3], double jointAcc[3]){
double V_tmp1[3]={0}, V_tmp2[3]={0}; //一時保存のためのベクトル
//冗長性を利用した関節角加速度の計算
//d2q = J#(d2r-dJdq) + (I-J#J)k
for(int i=0;i<3;i++){
jointAcc[i] = 0;
for(int j=0;j<3;j++){
//dJdq
for(int k=0;k<3;k++){
V_tmp1[j] += jacobData->diff[j][k]*jointVel[k];
}
//J#(d2r-dJdq)
V_tmp2[i] += jacobData->pse[i][j]*(targetAcc[j] - V_tmp1[j]);
}
//d2q = J#(d2r-dJdq) + (I-J#J)k
jointAcc[i] = V_tmp2[i] + nullSpaceVector[i];
}
//デバック用
//printf("%lf %lf %lf\n",jointAcc[H], jointAcc[K], jointAcc[A]);
/*for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("[%d][%d]%lf\t",i,j,jacobData->pse[i][j]);
}
printf("\n");
}
printf("\n");*/
}
/********************************************************
関数名:Matrix_Unit
説明 :3×3の単位行列の計算
引数 :double I[3][3] 3×3の単位行列
出力 :double I[3][3]
********************************************************/
void EnergyMinimization(double enc_data[2][3], struct leg_state *Leg, struct jacob_data *jacobData, double torq[3], double force[3]){
//地面(x-y座標系)に対しての角度なので注意
double theta[3];
double eta[3];
double force_tmp[3];
double force_null[3] = {0,0,0};
double force_image[3] = {0,0,0};