-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolReal.h
More file actions
1610 lines (1477 loc) · 60.2 KB
/
controlReal.h
File metadata and controls
1610 lines (1477 loc) · 60.2 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
#pragma once
#include "URPose.h"
#include "ur_driver.h"
#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
#include "fileOpration.h"
#include "trans.h"
#include <math.h>
#include <functional>
#include "ur_kinetic.h"
#include "analyse_test1.h"
#include "getLineNumber.h"
#include"vectorOperation.h"
#include"automatic_calReachPercent.h"
#include"automatic_interpolate_p.h"
#include"automatic_getIKsolution.h"
#include"automatic_pieceResult_Struct.h"
#include"automatic_pieceWise.h"
#include"automatic_SHEET.h"
#include "automatic_TARGET_POINTS.h"
#include"linearWithParabola.h"
#include"LSPB.h"
#include"movel_Test.h"
using namespace std;
class controlReal {
private:
UrDriver urd; // start rt in urd's constructor
URPose urpose; // used for cin
vector<double> urpose_cur;
vector<double> urposeq; // used for cin and take getq in workWindows
vector<double> urposeq_cur; // used for record lateset urq
vector<double> urposepv_cur; //used for record latest end-tool's pose velocity
string cmd_str; // used for cin as well as send to ur3
URPose target; //target of IK:x y z rx ry rz
vector<vector<double>> sol; //solution of IK
bool executeTraj;
public:
controlReal() {
// init urpose's rx ry rz as work pose's rx ry rz
urpose.rx = 2.2214;
urpose.ry = -2.2214;
urpose.rz = 0;
// init urposeq
urposeq.resize(6);
urposeq[0] = 0;
urposeq[1] = 0;
urposeq[2] = 0;
urposeq[3] = 0;
urposeq[4] = 0;
urposeq[5] = 0;
executeTraj = 0;
//init sol
sol.resize(8);
for (int i = 0; i < 8; i++) {
sol[i].resize(6);
for (int j = 0; j < 6; j++) {
sol[i][j] = 0;
}
}
cout << "Welcome use controlReal" << endl;
}
~controlReal() { cout << "Thanks for using controlReal" << endl; };
void openworkWindows() {
thread* update_loop_thread = new thread(bind(&controlReal::update_loop, this));
thread* workWindows_thread = new thread(bind(&controlReal::workWindows, this));
workWindows_thread->join();
update_loop_thread->join();
}
void workWindows() {
char opt_ch;
int opt_int;
double opt_double;
while (1) {
cout << "Input cmd" << endl;
cin >> cmd_str;
if (cmd_str == "home") {
home();
}
else if (cmd_str == "up") {
up();
}
else if (cmd_str == "work") {
work();
}
else if (cmd_str == "mjq") {
cout << "Input q1,q2,q3,q4,q5,q6 [Degree]" << endl;
/*set arm_jointValueTarget*/
cin >> urposeq[0] >> urposeq[1] >> urposeq[2] >> urposeq[3] >>
urposeq[4] >> urposeq[5];
urposeq = { d2r(urposeq[0]), d2r(urposeq[1]), d2r(urposeq[2]),
d2r(urposeq[3]), d2r(urposeq[4]), d2r(urposeq[5]) };
mjq("empty", urposeq);
Sleep(2);
}
else if (cmd_str == "mjqr") {
cout << "Input q1,q2,q3,q4,q5,q6 [rad]" << endl;
cin >> urposeq[0] >> urposeq[1] >> urposeq[2] >> urposeq[3] >>
urposeq[4] >> urposeq[5];
urd.setMonitorFlag();
mjq("empty", urposeq);
Sleep(2);
}
else if (cmd_str == "mlp") {
cout << "Input x, y, z[mm] and Rx Ry Rz " << endl;
cin >> urposeq[0] >> urposeq[1] >> urposeq[2] >> urposeq[3] >>
urposeq[4] >> urposeq[5];
urposeq[0] *= mm2m;
urposeq[1] *= mm2m;
urposeq[2] *= mm2m;
urd.setMonitorFlag();
mlp(urposeq);
Sleep(2);
}
else if (cmd_str == "movep") {
cout << "Input x, y, z[mm] and Rx Ry Rz " << endl;
cin >> urposeq[0] >> urposeq[1] >> urposeq[2] >> urposeq[3] >>
urposeq[4] >> urposeq[5];
urposeq[0] *= mm2m;
urposeq[1] *= mm2m;
urposeq[2] *= mm2m;
urd.setMonitorFlag();
movep(urposeq);
Sleep(2);
}
else if (cmd_str == "uploadProg_movep" || cmd_str == "ug") {
uploadProg_movep();
}
else if (cmd_str == "mjp") {
cout << "Input x,y,z [mm] Default Rx Ry Rz=" << urpose.rx << " " << urpose.ry << " " << urpose.rz << endl;
cin >> urpose.x;
cin >> urpose.y;
cin >> urpose.z;
urpose.x = urpose.x * mm2m;
urpose.y = urpose.y * mm2m;
urpose.z = urpose.z * mm2m;
mjp(urpose);
Sleep(2);
}
else if (cmd_str == "mjpFull" || cmd_str == "mjpf") {
cout << "Input x,y,z [mm] and Rx Ry Rz" << endl;
cin >> urpose.x;
cin >> urpose.y;
cin >> urpose.z;
cin >> urpose.rx >> urpose.ry >> urpose.rz;
urpose.x = urpose.x * mm2m;
urpose.y = urpose.y * mm2m;
urpose.z = urpose.z * mm2m;
mjp(urpose);
Sleep(2);
}
else if (cmd_str == "moveRelative" || cmd_str == "mr") {
cout << "Input Relative Distance Delta_X,Delta_Y,Delta_Z [mm] "<< endl;
double Delta_X, Delta_Y, Delta_Z;
cin >> Delta_X >> Delta_Y >> Delta_Z;
urposeq = urd.getp();
urposeq[0] += Delta_X;
urposeq[1] += Delta_Y;
urposeq[2] += Delta_Z;
urposeq[0] *= mm2m;
urposeq[1] *= mm2m;
urposeq[2] *= mm2m;
mjp2(urposeq);
}
else if (cmd_str == "dotraj") {
dotraj(0);//use 0.008
cout << "Finish sendCmd" << endl;
}
else if (cmd_str == "dotrajmj") {
cout << "Start do traj" << endl;
dotraj(1);
cout << "Finish sendCmd" << endl;
}
else if (cmd_str == "uploadProg_servoj" || cmd_str == "uservoj") {
uploadProg_servoj2();
}
else if (cmd_str == "uploadProg_servoj3" || cmd_str == "uservoj3") {
uploadProg_servoj3();
}
else if (cmd_str == "uploadProg_servoj4" || cmd_str == "uservoj4") {
uploadPorg_servoj4();
}
else if (cmd_str == "automatic_adjust_parameter" || cmd_str == "autoap") {
//automatic_adjust_parameter();
automatic_adjust_parameter_OFpieceWise(0);
}
else if (cmd_str == "checkIK") {
checkIK();
}
else if (cmd_str == "exit") {
break;
}
else if (cmd_str == "getq") {
urposeq_cur = urd.getq();
cout << "[" << urposeq_cur[0] << " , " << urposeq_cur[1] << " , "
<< urposeq_cur[2] << " , " << urposeq_cur[3] << " , "
<< urposeq_cur[4] << " , " << urposeq_cur[5] << "]" << endl;
}
else if (cmd_str == "getp") {
urpose_cur = urd.getp();
cout << "[" << urpose_cur[0] << " , " << urpose_cur[1] << " , "
<< urpose_cur[2] << " , " << urpose_cur[3] << " , "
<< urpose_cur[4] << " , " << urpose_cur[5] << "]" << endl;
}
else if (cmd_str == "settcpProg") {
cout << "Input Rx Ry Rz" << endl;
cin >> urpose.rx;
cin >> urpose.ry;
cin >> urpose.rz;
}
else if (cmd_str == "settcp") {
cout << "Input x y z and Rx Ry Rz" << endl;
string settcp_x, settcp_y, settcp_z, settcp_Rx, settcp_Ry, settcp_Rz;
cin >> settcp_x >> settcp_y >> settcp_z >> settcp_Rx >> settcp_Ry >> settcp_Rz;
//set_tcp(p[0.,.2,.3,0.,3.14,0.])
cmd_str = "set_tcp(p[" + settcp_x + "," + settcp_y + "," + settcp_z + "," + settcp_Rx + "," + settcp_Ry + "," + settcp_Rz + "])\n";
urd.sendCmd(cmd_str);
}
else if (cmd_str == "sendcmd") {
cout << "Input cmd you want to send" << endl;
cin >> cmd_str;
cmd_str += "\n";
urd.sendCmd(cmd_str);
}
else if (cmd_str == "openMonitor" || cmd_str=="om") {
openMonitor();
}
else if (cmd_str == "interplote_p" || cmd_str == "ip") {
cout << "input lineNum of file being interploted, '0' as default:1000" << endl;
cin >> opt_int;
if (opt_int == 0) {
opt_int = 1000;
}
interplote_p(opt_int);
}
else if (cmd_str == "analyse_test1" || cmd_str == "atest1") {
//analyse_test1();
cmd_str=generateCodeForTest1();
urd.sendCmd(cmd_str);
urd.setMonitorFlag();
}
else if (cmd_str == "ls") {
cout << "home up work" << endl;
cout << "mjq mjqr mjp" << endl;
cout << "dotraj size dotrajmj size" << endl;
cout << "getq getp settcp" << endl;
cout << "exit" << endl;
}
else if (cmd_str == "testIK" ) {
testIK();
}
else if (cmd_str == "trajectory_planning" || cmd_str == "lwp") {
cout << "input a_normal" << endl;
double anormal_lwp = 0;
cin >> anormal_lwp;
cout << "input v in cartesian" << endl;
double v_lwp = 0;
cin >> v_lwp;
vector<double> td = get_timeSeqWith_constantVcartesian(v_lwp);
string cmd_str_lwp = linearWithParabola(td, anormal_lwp, 5);
cout << "move to satrt q" << endl;
ifstream qfile_lwp("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_targetPose_interpolated_afterIK.txt");
checkfp(qfile_lwp);
vector<double> startq_lwp(6);
qfile_lwp >> startq_lwp[0] >> startq_lwp[1] >> startq_lwp[2] >> startq_lwp[3] >> startq_lwp[4] >> startq_lwp[5];
qfile_lwp.close();
mjq("empty", startq_lwp);
Sleep(2000);
cout << "start move with speedj" << endl;
urd.setMonitorFlag();
urd.sendCmd(cmd_str_lwp);
while (urd.getMonitorFlag() == 1) {};//wait till ur3 stop
}
else if (cmd_str == "trajectory_planning_multi" || cmd_str == "lwp_multi") {
cout << "input max a" << endl;
double amax_lwp = 0;
cin >> amax_lwp;
cout << "input v in cartesian" << endl;
double v_lwp = 0;
cin >> v_lwp;
string cmd_str_lwp = linearWithParabola_multiAxis(v_lwp, amax_lwp);
cout << "move to satrt q" << endl;
ifstream qfile_lwp("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_targetPose_interpolated_afterIK.txt");
checkfp(qfile_lwp);
vector<double> startq_lwp(6);
qfile_lwp >> startq_lwp[0] >> startq_lwp[1] >> startq_lwp[2] >> startq_lwp[3] >> startq_lwp[4] >> startq_lwp[5];
qfile_lwp.close();
mjq("empty", startq_lwp);
Sleep(2000);
cout << "start move with speedj" << endl;
urd.setMonitorFlag();
urd.sendCmd(cmd_str_lwp);
while (urd.getMonitorFlag() == 1) {};//wait till ur3 stop
//calReachPercent
int lineNum;
Vector3d pc;
vector<double> ResultOF_calReachPercent(2);
ResultOF_calReachPercent = automatic_calReachPercent();
cout << "Reach Percent at v=" << v_lwp << ",amax=" << amax_lwp << " is:" << ResultOF_calReachPercent[0] << endl;
cout << "( ∑calDis(至点对应的实际点,至点对应的目标点) )/至点数=" << ResultOF_calReachPercent[1] << endl;
//显示Miss点
lineNum = getLineNumber("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_Missed.txt", "");
cout << "There are " << lineNum << " Miss Points" << endl;
if (lineNum > 0) {
ifstream ifp_missed("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_Missed.txt");
checkfp(ifp_missed);
for (int i = 0; i < lineNum; i++) {
ifp_missed >> pc[0] >> pc[1] >> pc[2];
cout << pc[0] << " " << pc[1] << " " << pc[2] << endl;
}
}
}
else if (cmd_str == "normal_lspb" || cmd_str == "nlspb") {
string cmd_str_lwp = Normal_LSPB_6D();
cout << "move to satrt q" << endl;
ifstream qfile_lwp("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_targetPose_interpolated_afterIK.txt");
checkfp(qfile_lwp);
vector<double> startq_lwp(6);
qfile_lwp >> startq_lwp[0] >> startq_lwp[1] >> startq_lwp[2] >> startq_lwp[3] >> startq_lwp[4] >> startq_lwp[5];
qfile_lwp.close();
mjq("empty", startq_lwp);
Sleep(2000);
cout << "start move with speedj" << endl;
urd.setMonitorFlag();
urd.sendCmd(cmd_str_lwp);
while (urd.getMonitorFlag() == 1) {};//wait till ur3 stop
}
else if (cmd_str == "normal_lspb_use_td" || cmd_str == "nlspb2") {
vector<double> normal_lspb_td_seq{0.032,0.064 };
vector<double> normal_lspb_td = Normal_LSPB_get_td(normal_lspb_td_seq, 5);
string cmd_str_lwp = Normal_LSPB_6D_use_td(normal_lspb_td);
cout << "move to satrt q" << endl;
ifstream qfile_lwp("C:/Users/XY/Desktop/path/automaticAdjustParameter/automatic_targetPose_interpolated_afterIK.txt");
checkfp(qfile_lwp);
vector<double> startq_lwp(6);
qfile_lwp >> startq_lwp[0] >> startq_lwp[1] >> startq_lwp[2] >> startq_lwp[3] >> startq_lwp[4] >> startq_lwp[5];
qfile_lwp.close();
mjq("empty", startq_lwp);
Sleep(2000);
cout << "start move with speedj" << endl;
urd.setMonitorFlag();
urd.sendCmd(cmd_str_lwp);
while (urd.getMonitorFlag() == 1) {};//wait till ur3 stop
}
else if (cmd_str == "getIKparam") {
getIKparam();
}
else if (cmd_str == "movel_Test" || cmd_str == "mltest") {
string cmd_str_mltest = generate_movel_code();
urd.setMonitorFlag();
urd.sendCmd(cmd_str_mltest);
while (urd.getMonitorFlag() == 1) {};//wait till ur3 stop
}
else {
cout << "No such cmd,input again plz" << endl;
}
}
}
void openMonitor() {
urd.setMonitorFlag2();
char ch = 'r';
while(1){
if (_kbhit()) {//如果有按键按下,则_kbhit()函数返回真
ch = _getch();//使用_getch()函数获取按下的键值
if (ch == 'c') { break; }//当按下ESC时循环,ESC键的键值时27.
}
}
urd.resetMonitorFlag2();
}
//static void* workWindowsHelper(void* contex) {
// return ((controlReal*)contex)->workWindows();
//}
void home() {
urd.sendCmd("movej([0, 0, 0, 0, 0, 0], a = 2, v = 2)\n");
Sleep(2);
}
void up() {
urd.sendCmd("movej([0, d2r(-90), 0, d2r(-90), 0, 0], a = 2, v = 2)\n");
}
void work() {
urd.sendCmd(
"movej([d2r(3.6), d2r(-70.74), d2r(-137.23), d2r(-62.03),d2r(90),d2r(3.6)], a = 2, v =2)\n");
Sleep(2);
}
// send servoj
void servoj(const string& q_str = "empty", const string& t = "0.008",
const vector<double>& q = { 0, 0, 0, 0, 0, 0 }) {
if (q_str == "empty") {
cout << "WARN at servoj:" << "servoj:q_str 为 empty,vector<double>类型的q的命令生成还没写" << endl;
return;
}
else {
cmd_str = "servoj(" + q_str + ",t=" + t + ",lookahead_time=0.1)\n";
urd.sendCmd(cmd_str);
}
}
// send movej
void mjq(const string& q_str = "empty",
const vector<double>& urposeq = { 0, 0, 0, 0, 0, 0 }) {
if (q_str == "empty") {
cmd_str = "movej([" + to_string(urposeq[0]) + "," + to_string(urposeq[1]) +
"," + to_string(urposeq[2]) + "," + to_string(urposeq[3]) + "," +
to_string(urposeq[4]) + "," + to_string(urposeq[5]) +
"],a=2,v=2)\n";
urd.sendCmd(cmd_str);
}
else {
cmd_str = "movej(" + q_str + ",a=2,v=2)\n";
urd.sendCmd(cmd_str);
}
}
void mlp(const vector<double>& urpose = { 0, 0, 0, 0, 0, 0 }) {
cmd_str = "movel(p[" + to_string(urpose[0]) + "," + to_string(urpose[1]) +
"," + to_string(urpose[2]) + "," + to_string(urpose[3]) + "," +
to_string(urpose[4]) + "," + to_string(urpose[5]) +
"],a=0.5,v=0.02)\n";
urd.sendCmd(cmd_str);
}
void movep(const vector<double>& urpose = { 0, 0, 0, 0, 0, 0 }) {
cmd_str = "movep(p[" + to_string(urpose[0]) + "," + to_string(urpose[1]) +
"," + to_string(urpose[2]) + "," + to_string(urpose[3]) + "," +
to_string(urpose[4]) + "," + to_string(urpose[5]) +
"],a=0.5,v=0.02)\n";
urd.sendCmd(cmd_str);
}
/*
Function: 发送关于movep的程序给UR3
i
o
m
*/
void uploadProg_movep() {
//URPose workPose(0.2, -0.1, 0.2, 2.225, -2.2194, 0);
URPose workPose(0.2, -0.1, 0.2, 2.2214, -2.2214, 0);
URPose urpose;
urpose = workPose;
urpose.x += 0.5 * mm2m;
urpose.y += 0.5 * mm2m;
urpose.z += 0.3 * mm2m;
double blendRadius=1*mm2m;//交融半径
std::string cmd_str;
char buf[128];
cmd_str = "def driverProg():\n";
cmd_str += "\tmovej(p[" + to_string(urpose.x) + "," + to_string(urpose.y) +
"," + to_string(urpose.z) + "," + to_string(urpose.rx) + "," +
to_string(urpose.ry) + "," + to_string(urpose.rz) +
"],a=2,v=2)\n";
//mp p1
urpose.x = 374.9*mm2m;
urpose.y = -99.5*mm2m;
urpose.z = 200.3*mm2m;
cmd_str += "\tmovep(p[" + to_string(urpose.x) + "," + to_string(urpose.y) +
"," + to_string(urpose.z) + "," + to_string(urpose.rx) + "," +
to_string(urpose.ry) + "," + to_string(urpose.rz) +
"],a=1.2,v=0.25,r=0.001)\n";
cmd_str += "end\n";
urd.setMonitorFlag();
urd.sendCmd(cmd_str);
}
void mjp(const URPose& urpose) {
cmd_str = "movej(p[" + to_string(urpose.x) + "," + to_string(urpose.y) +
"," + to_string(urpose.z) + "," + to_string(urpose.rx) + "," +
to_string(urpose.ry) + "," + to_string(urpose.rz) +
"],a=1.2,v=0.25)\n";
urd.sendCmd(cmd_str);
Sleep(2);
}
//将urposeq作为形参
void mjp2(const vector<double> & urposeq) {
cmd_str = "movej(p[" + to_string(urposeq[0]) + "," + to_string(urposeq[1]) +
"," + to_string(urposeq[2]) + "," + to_string(urposeq[3]) + "," +
to_string(urposeq[4]) + "," + to_string(urposeq[5]) +
"],a=2,v=2)\n";
urd.sendCmd(cmd_str);
Sleep(2);
}
/*
Function: used to trans "file after interploted" into "file composed by sol[q1-q6]"
i
o
m:
*/
void testIK() {
/*先赋值为work Pose*/
target.x = 200;
target.y = -100;
target.z = 200;
target.rx = 2.225;
target.ry = -2.2194;
target.rz = 0;
/*urpose_test为相对位置,work Pose + urpose_test即为最终目标位置*/
URPose urpose_test;
fileOpration fp_test("C:/Users/fx53v/Desktop/Do/test1.1000.txt");
urpose_test=fp_test.getNexp();
target.x += urpose_test.x;
target.y += urpose_test.y;
target.z += urpose_test.z;
target.x *= mm2m;
target.y *= mm2m;
target.z *= mm2m;
/*cal IK*/
sol = IK(target, urd.getq()[5]);
/*从sol中选择距离当前各轴位置最近的结果*/
double disMin_test = 2 * PI * 6;
double indexMin_test = 0;
double disTemp_test = 0;
vector<double> qcur_test(6);
qcur_test = urd.getq();
/*由于一个角在加或减360度后有一等效角,需要将等效角考虑进去*/
vector<double> qcur_test2(6);
for (int i = 0; i < 6; i++) {
if (qcur_test[i] > 0) {
qcur_test2[i] = qcur_test[i] - 360;
}
else if (qcur_test[i] < 0) {
qcur_test2[i] = qcur_test[i] + 360;
}
else//qcur_test[i]==0;
{
qcur_test2[i] = 360;
}
}
/*找到距离当前各轴位置最近的结果*/
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 6; j++) {
disTemp_test += min(absd(r2d(sol[i][j]) - qcur_test[j]), absd(r2d(sol[i][j]) - qcur_test2[j]));
}
if (disMin_test > disTemp_test) {
disMin_test = disTemp_test;
indexMin_test = i;
}
disTemp_test = 0;
}
/*将该结果的各值更新为和qcur较近的等效角*/
double equAngle;
for (int j = 0; j < 6; j++) {
if (sol[indexMin_test][j] > 0) {
equAngle = sol[indexMin_test][j] - 2 * PI;
if (absd(r2d(sol[indexMin_test][j]) - qcur_test[j]) > absd(r2d(equAngle) - qcur_test[j])) {
sol[indexMin_test][j] = equAngle;
}
}
else {//sol[indexMin_test][j] <= 0
equAngle = sol[indexMin_test][j] + 2 * PI;
if (absd(r2d(sol[indexMin_test][j]) - qcur_test[j]) > absd(r2d(equAngle) - qcur_test[j])) {
sol[indexMin_test][j] = equAngle;
}
}
}
/*Print Result*/
for (int j = 0; j < 6; j++) {
if (CheckZeroThresh(sol[indexMin_test][j])) {
sol[indexMin_test][j] = 0;
}
printf("%12.3f", r2d(sol[indexMin_test][j]));
}
printf("\n");
mjq("empty", sol[indexMin_test]);
return;
}
void dotrajNew(std::vector<double> inp_timestamps,
std::vector<std::vector<double> > inp_positions,
std::vector<std::vector<double> > inp_velocities) {
fileOpration fp("/home/linux80/path/IKresult10000.txt");
cout << "dotraj:move to start pos" << endl;
string q_str = fp.getNexq_str();
mjq(q_str);
Sleep(5);
urd.doTraj(inp_timestamps, inp_positions, inp_velocities);
}
//稀疏化(skip some q):once one joint reached deta 1 degree first,take
//密集化(interplote between q):the joint that has biggest deta degree as the rule to be cut
void interplote(int lineNum) {
//C:\Users\XY\Desktop\path\IKresult200000.txt
fileOpration fp("C:/Users/fx53v/Desktop/Do/IKresult200000.txt");
const double sgap = 0.0085;//standard gap = 1degree
/*put q into ram*/
vector<vector<double>> q(lineNum);
for (int i = 0; i < lineNum; i++) {
q[i].resize(6);
q[i] = fp.getNexq();
}
/*interplote and skip opration*/
vector<vector<double>> q_result;
vector<double> q_temp(6);//used in pushback
vector<double> q1_(6);
vector<double> q2_(6);
q1_ = q[0];
q_result.push_back(q1_);
double gapTemp;
for (int i = 1; i < lineNum; i++)
{
if ((gapTemp = gap2q(q1_, q[i])) > sgap) {//interplote
q2_ = q[i];
for (int j = 1; j < (((int)(gapTemp * m2um)) / ((int)(sgap * m2um))); j++) {
q_temp = { q1_[0] - (q1_[0] - q2_[0]) * absd(sgap / gapTemp) * j,
q1_[1] - (q1_[1] - q2_[1]) * absd(sgap / gapTemp) * j,
q1_[2] - (q1_[2] - q2_[2]) * absd(sgap / gapTemp) * j,
q1_[3] - (q1_[3] - q2_[3]) * absd(sgap / gapTemp) * j,
q1_[4] - (q1_[4] - q2_[4]) * absd(sgap / gapTemp) * j,
q1_[5] - (q1_[5] - q2_[5]) * absd(sgap / gapTemp) * j };
q_result.push_back(q_temp);
}
q_result.push_back(q2_);
if (i + 1 < lineNum)//update q1_
q1_ = q2_;
}
else
{
continue;
}
}
q_result.push_back(q[lineNum - 1]);//in case not push last q because not reach last gap
//put result into file
ofstream fpo("C:/Users/fx53v/Desktop/Do/IKresultSgap.txt");
if (!fpo) {
cout << "WARN at interplote:fail to open IKresultSgap" << endl;
}
for (int i = 0; i < q_result.size(); i++)
{
fpo << q_result[i][0] << " " << q_result[i][1] << " " << q_result[i][2] << " " <<
q_result[i][3] << " " << q_result[i][4] << " " << q_result[i][5] << endl;
}
fpo.close();
//check q_result
for (int i = 0; i < q_result.size() - 1; i++) {
cout << "i:" << i << " " << gap2q(q_result[i], q_result[i + 1]) << endl;;
}
}
double gap2q(vector<double> q1, vector<double> q2) {
double max = absd(q1[0] - q2[0]);
for (int i = 1; i < 6; i++) {
if (max < absd(q1[i] - q2[i])) {
max = absd(q1[i] - q2[i]);
}
}
return max;
}
double absd(const double& d1) {
if (d1 < 0) {
return -d1;
}
else
return d1;
}
/*
Function: use skip and interplote operation to make a pose set cut as standard distance
Input: lineNum - number of poses
Output: poses after handling
Main Idea: record start pose , start skip until two poses' dis >= sdis , then interplote between two pose
and push last pose . record last pose as start pose and start skip untill ......
*/
void interplote_p(int lineNum) {
fileOpration fp("C:/Users/fx53v/Desktop/Do/hand2.txt");
const double sdis = 0.1;//standard dis (mm)
/*put q into ram*/
vector<URPose> p(lineNum);
for (int i = 0; i < lineNum; i++) {
p[i] = fp.getNexp();
}
/*interplote and skip opration*/
vector<URPose> pResult;
URPose pTemp, p1, p2;
p1 = p[0];
pResult.push_back(p1);
double distemp;
for (int i = 1; i < lineNum; i++)
{
if ((distemp = dis2p(p1, p[i])) > sdis) {//interplote
p2 = p[i];
for (int j = 1; j < (((int)(distemp * m2um)) / ((int)(sdis * m2um))); j++) {
pTemp.x = p1.x - (p1.x - p2.x) * absd(sdis / distemp) * j;
pTemp.y = p1.y - (p1.y - p2.y) * absd(sdis / distemp) * j;
pTemp.z = p1.z - (p1.z - p2.z) * absd(sdis / distemp) * j;
//pTemp.rx = p1.rx - (p1.rx - p2.rx) * absd(sdis / distemp) * j;
//pTemp.ry = p1.ry - (p1.ry - p2.ry) * absd(sdis / distemp) * j;
//pTemp.rz = p1.rz - (p1.rz - p2.rz) * absd(sdis / distemp) * j;
pResult.push_back(pTemp);
}
pResult.push_back(p2);
p1 = p2;
}
else
{
continue;
}
}
pResult.push_back(p[lineNum - 1]);//in case not push last q because not reach last gap
//put result into file
ofstream fpo("C:/Users/fx53v/Desktop/Do/hand2_interploted.txt");
if (!fpo) {
cout << "WARN at interplote:fail to open IKresultSgap" << endl;
}
for (int i = 0; i < pResult.size(); i++)
{
fpo << pResult[i].x << " " << pResult[i].y << " " << pResult[i].z << endl;
//pResult[i].rx << " " << pResult[i].ry << " " << pResult[i].rz << endl;
}
fpo.close();
//check pResult -- [1,2 )
for (int i = 0; i < pResult.size() - 1; i++) {
cout << "i:" << i << " " << dis2p(pResult[i], pResult[i + 1]) << endl;;
}
}
double dis2p(const URPose& p1, const URPose& p2) {
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) + (p1.z - p2.z) * (p1.z - p2.z));
}
// read q pos file and send servoj at 125HZ
//falg_mj is used to check if our ik is right
void dotraj(bool flag_mj = 0, double t = 0.008) {//改t暂时要去ur_driver的upload_prog里改
fileOpration fp("C:/Users/fx53v/Desktop/Do/hand2_afterIK.txt");
/*INPUT 行数*/
int size_ = 17041;
//cin >> size_;
/*INPUT servoj time*/
cout << "INPUT servoj time" << endl;
cin >> t;
urd.setServojTime(t);
/*INPUT lookAheadTime*/
double lookAheadTime = 0.2;
//cout << "INPUT lookAheadTime" << endl;
//cin >> lookAheadTime;
urd.setLookaheadTime(lookAheadTime);
/*INPUT sleep time*/
cout << "INPUT sleepTime" << endl;
double sleepTime = 1;
cin >> sleepTime;
/*movej to start pose*/
cout << "dotraj:move to start pos" << endl;
string q_str = fp.getNexq_str();
mjq(q_str);
Sleep(2000);
/*upload prog*/
urd.uploadProg();
vector<double> q_d(6);
/*send servoj or movej*/
executeTraj = 1;
if (flag_mj == 0) {
// start send servoj
cout << "dotraj:start send servoj" << endl;
//while (fp.isEnd() != 1) {
for (int i = 0; i < size_ - 1; i++) {
q_d = fp.getNexq();
fp.qToDegree(q_d);//辅助打印函数
urd.servoj(q_d, 1);
//q_str = fp.getNexq_str();
//fp.qstrToDegree(q_str);
//servoj(q_str,t);
Sleep(sleepTime);
}
Sleep(10);
}
else//flag_mj==1
{
// start send mj
cout << "dotraj:start send mj" << endl;
//while (fp.isEnd() != 1) {
for (int i = 0; i < size_ - 1; i++) {
q_str = fp.getNexq_str();
mjq(q_str);
Sleep(2);
}
}
cout << "total ik pos :" << fp.getlineNum() << endl;
executeTraj = 0;
}
void uploadProg_servoj() {
int lineNum = 529;
ifstream ifp("C:/Users/fx53v/Desktop/Do/hand2_afterIK.txt");
ifstream ifp2("C:/Users/fx53v/Desktop/Do/hand2_interploted.txt");//hand2_interploted.txt:IK解对应的pose文档
double dis = 0.1;//dis of interploted
if (ifp.fail()) {
cout << "ERROR IN uploadProgram _" << __LINE__ << ": fail to open ifp" << endl;
system("pause");
exit(0);
}
if (ifp2.fail()) {
cout << "ERROR IN uploadProgram _" << __LINE__ << ": fail to open ifp2" << endl;
system("pause");
exit(0);
}
cout << "input v in tool space" << endl;
double v=0,t,a;//v=a/(theta+1)。a is the speed when theta=0
cin >> v;
a = v;
vector<double> q;
Vector3d pb,pc,pn;//pbefore pcurrent pnext
q.resize(6);
cout << "input look ahead time" << endl;
double lookahead_time;
cin >> lookahead_time;
cout << "input gain between 100 and 2000,input 0 to not use gain" << endl;
double gain;
cin >> gain;
cout << "move to satrt q" << endl;
ifp >> q[0] >> q[1] >> q[2] >> q[3] >> q[4] >> q[5];
ifp2 >> pb[0] >> pb[1] >> pb[2];
ifp2 >> pc[0] >> pc[1] >> pc[2];
mjq("empty", q);
Sleep(2000);
cmd_str = "def driverProg():\n";
for (int i = 1; i < lineNum-1; i++) {//最后一个点对应的q[lineNum-1]不读了,因为此时已经把hand2_interpolated.txt的最后一个点读进pn了。
ifp >> q[0] >> q[1] >> q[2] >> q[3] >> q[4] >> q[5];
ifp2 >> pn[0] >> pn[1] >> pn[2];
//v = a / (3*d2r(calAngleOFvector(pc - pb, pn - pc)) + 1);//根据夹角计算速度。
t = sqrt((pc[0] - pb[0]) * (pc[0] - pb[0]) + (pc[1] - pb[1]) * (pc[1] - pb[1]) + (pc[2] - pb[2]) * (pc[2] - pb[2])) / v;//t=d/v
//t = 0.008;//来自monitorLog的时间间隔
pb = pc;
pc = pn;
if (gain == 0) {
cmd_str += "\tservoj([" + to_string(q[0]) + "," + to_string(q[1]) +
"," + to_string(q[2]) + "," + to_string(q[3]) + "," +
to_string(q[4]) + "," + to_string(q[5]) +
"]," + "t=" + to_string(t) + "," + "lookahead_time=" + to_string(lookahead_time) + ")\n";
}
else {
cmd_str += "\tservoj([" + to_string(q[0]) + "," + to_string(q[1]) +
"," + to_string(q[2]) + "," + to_string(q[3]) + "," +
to_string(q[4]) + "," + to_string(q[5]) +
"]," + "t=" + to_string(t) + "," + "lookahead_time=" + to_string(lookahead_time) + "," + "gain=" + to_string(gain) + ")\n";
}
}
cmd_str += "end\n";
cout << "start move with servoj" << endl;
urd.sendCmd(cmd_str);
openMonitor();
}
//功能和uploadProg_servoj()一致,但是不需要手动调整行数了
//目标点集写入automatic_targetPose.txt
void uploadProg_servoj2() {
//pieceWise();
//double dis = 0.1;//dis of interploted
automatic_interplote_p(0);//插补
automatic_getIKsolution();//IK
//打开文件
ifstream ifp("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_targetPose_interpolated_afterIK.txt");
ifstream ifp2("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_targetPose_interpolated.txt");//hand2_interploted.txt:IK解对应的pose文档
if (ifp.fail()) {
cout << "ERROR IN uploadProgram _" << __LINE__ << ": fail to open ifp" << endl;
system("pause");
exit(0);
}
if (ifp2.fail()) {
cout << "ERROR IN uploadProgram _" << __LINE__ << ": fail to open ifp2" << endl;
system("pause");
exit(0);
}
int lineNum = getLineNumber("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_targetPose_interpolated_afterIK.txt", "");
cout << "input v in tool space" << endl;
double v = 0, t;
cin >> v;
vector<double> q;
Vector3d pb, pc;//pbefore pcurrent
q.resize(6);
cout << "input look ahead time" << endl;
double lookahead_time;
cin >> lookahead_time;
cout << "input gain between 100 and 2000,input 0 to not use gain" << endl;
double gain;
cin >> gain;
cout << "move to satrt q" << endl;
ifp >> q[0] >> q[1] >> q[2] >> q[3] >> q[4] >> q[5];
ifp2 >> pb[0] >> pb[1] >> pb[2];
mjq("empty", q);
Sleep(2000);
cmd_str = "def driverProg():\n";
for (int i = 1; i < lineNum; i++) {
ifp >> q[0] >> q[1] >> q[2] >> q[3] >> q[4] >> q[5];
ifp2 >> pc[0] >> pc[1] >> pc[2];
t = sqrt((pc[0] - pb[0]) * (pc[0] - pb[0]) + (pc[1] - pb[1]) * (pc[1] - pb[1]) + (pc[2] - pb[2]) * (pc[2] - pb[2])) / v;//t=d/v
//t = 0.008;//来自monitorLog的时间间隔
pb = pc;
if (gain == 0) {
cmd_str += "\tservoj([" + to_string(q[0]) + "," + to_string(q[1]) +
"," + to_string(q[2]) + "," + to_string(q[3]) + "," +
to_string(q[4]) + "," + to_string(q[5]) +
"]," + "t=" + to_string(t) + "," + "lookahead_time=" + to_string(lookahead_time) + ")\n";
}
else {
cmd_str += "\tservoj([" + to_string(q[0]) + "," + to_string(q[1]) +
"," + to_string(q[2]) + "," + to_string(q[3]) + "," +
to_string(q[4]) + "," + to_string(q[5]) +
"]," + "t=" + to_string(t) + "," + "lookahead_time=" + to_string(lookahead_time) + "," + "gain=" + to_string(gain) + ")\n";
}
}
cmd_str += "end\n";
cout << "start move with servoj" << endl;
urd.setMonitorFlag();
urd.sendCmd(cmd_str);
while (urd.getMonitorFlag() == 1) {};//wait till ur3 stop
//calReachPercent
vector<double> ResultOF_calReachPercent(2);
ResultOF_calReachPercent = automatic_calReachPercent();
cout << "Reach Percent at v=" << v << ",lookahead_time=" << lookahead_time << " is:" << ResultOF_calReachPercent[0] <<endl;
cout << "( ∑calDis(至点对应的实际点,至点对应的目标点) )/至点数=" << ResultOF_calReachPercent[1] << endl;
//显示Miss点
lineNum = getLineNumber("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_Missed.txt", "");
cout << "There are "<<lineNum<<" Miss Points" << endl;
if (lineNum > 0) {
ifstream ifp_missed("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_Missed.txt");
checkfp(ifp_missed);
for (int i = 0; i < lineNum; i++) {
ifp_missed >> pc[0] >> pc[1] >> pc[2];
cout << pc[0] << " " << pc[1] << " " << pc[2] << endl;
}
}
}
//增加了对pieceWise_control文件的读取
//目标点集写入automatic_targetPose.txt
void uploadProg_servoj3() {
//pieceWise();
//double dis = 0.1;//dis of interploted
automatic_interplote_p(0);//插补
automatic_getIKsolution();//IK
//打开文件
ifstream ifp("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_targetPose_interpolated_afterIK.txt");
ifstream ifp2("C:/Users/fx53v/Desktop/Do/automaticAdjustParameter/automatic_targetPose_interpolated.txt");//hand2_interploted.txt:IK解对应的pose文档
if (ifp.fail()) {
cout << "ERROR IN uploadProgram _" << __LINE__ << ": fail to open ifp" << endl;
system("pause");
exit(0);
}