-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSyncHomingCmd.cpp
More file actions
1997 lines (1813 loc) · 79.3 KB
/
Copy pathSyncHomingCmd.cpp
File metadata and controls
1997 lines (1813 loc) · 79.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
//
// Created by jeon on 18. 10. 21.
//
#include <iostream>
#include "Definitions.h"
#include <string.h>
#include <sstream>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <list>
#include <math.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/times.h>
#include <sys/time.h>
typedef void* HANDLE;
typedef int BOOL;
enum EAppMode
{
AM_UNKNOWN,
AM_DEMO,
AM_INTERFACE_LIST,
AM_PROTOCOL_LIST,
AM_VERSION_INFO,
AM_HOMING,
AM_POSITION_CON
};
using namespace std;
void* g_pKeyHandle = 0;
unsigned short g_usNodeId = 1;
string g_deviceName;
string g_protocolStackName;
string g_interfaceName;
string g_portName;
int g_baudrate = 0;
EAppMode g_eAppMode = AM_HOMING;
// Joint Parameters
unsigned short g_usNodeId_arr[] = {1, 2, 3, 4, 5, 6, 7};
int g_NbofJoint = sizeof(g_usNodeId_arr)/ sizeof(*g_usNodeId_arr);
int g_GearRatio[] = {160, 160, 160, 120, 100, 100, 100};
double g_AxisVelRatio[] = {1.6, 1.6, 1.6, 1.2, 1, 1, 1};
int g_PulseRev[] = {4096, 4096, 4096, 4096, 2048, 2048, 2048};
int g_HomeOffset_arr[] = {72818, 72818, 72818, 54613, 22378, 22378, 22378};
long g_SafeRange[] = {2148124, 1121393, 1856853, 1004885, 575715, 423253};
// Position Control Parameters
long g_PosDesired;
unsigned int g_ProfileVelocity = 1200;
long g_PosCenter[] = {0, 0, 0, 0, 0, 0, 0};
long g_PosPositiveLimit[] = {0, 0, 0, 0, 0, 0, 0};
int g_CapturedPosition;
// Homing Parameters
unsigned short g_HomingFirstId[] = {2, 4, 6};
unsigned short g_HomingSecondId[] = {1, 3, 5};
unsigned int g_HomingAcceleration;
unsigned int g_SpeedSwitch;
unsigned int g_SpeedIndex;
unsigned int l_SpeedSwitch;
unsigned int l_SpeedIndex;
int g_HomeOffset;
int l_HomeOffset;
unsigned short g_CurrentThreshold;
int g_HomePosition;
int g_IsQuickStopped;
// Digital Input/Output Parameters
unsigned short g_digitalIOIndex = 0x3142;
unsigned short g_digitalIPropIndex = 0x3141;
unsigned short g_SoftPosLimitIndex = 0x607D;
unsigned short g_TargetPosIndex = 0x607A;
unsigned char g_digiatlIPol = 0x02; // Digital Input Polarity
unsigned char g_digitalIO_1 = 0x01; // Digital Input Pin for Limit
unsigned char g_digitalIO_4 = 0x04; // Digital Input Pin for QuickStop
unsigned char g_SoftPosMin = 0x01; // Minimum Software Position Limit
unsigned char g_SoftPosMax = 0x02; // Maximum Software Position Limit
unsigned int g_NbofBytesToWrite_1 = 1;
unsigned int g_NbofBytesToWrite_2 = 2;
unsigned int g_NbofBytesToWrite_4 = 4;
// Digital Input/Output Pin Configuration Parameters
unsigned char g_NegativeLimit = 0;
unsigned char g_GeneralD = 19;
unsigned char g_QuickStop = 28;
unsigned char g_None = 255;
unsigned short g_LowActive = 0x0001;
unsigned short g_HighActive = 0x0000;
unsigned char data;
int g_PosLimitMin[] = {-5000000, -5000000, -5000000, -5000000, -5000000, -5000000, -5000000};
int g_PosLimitMax[] = {5000000, 5000000, 5000000, 5000000, 5000000, 5000000, 5000000};
int g_ZeroPos = 0;
const string g_programName = "HomingCmd";
#ifndef MMC_SUCCESS
#define MMC_SUCCESS 0
#endif
#ifndef MMC_FAILED
#define MMC_FAILED 1
#endif
#ifndef MMC_MAX_LOG_MSG_SIZE
#define MMC_MAX_LOG_MSG_SIZE 512
#endif
// Functions
void LogError(string functionName, int p_lResult, unsigned int p_ulErrorCode);
void LogInfo(string message);
void PrintUsage();
void PrintHeader();
void PrintSettings();
int OpenDevice(unsigned int* p_pErrorCode);
int CloseDevice(unsigned int* p_pErrorCode);
void SetDefaultParameters();
int ParseArguments(int argc, char** argv);
int HomingMode(HANDLE p_DeviceHandle, unsigned short p_usNodeId, unsigned int & p_rlErrorCode);
int BothHomingMode(HANDLE p_DeviceHandle, unsigned short p_usNodeId, unsigned int & p_rlErrorCode);
int SyncHomingMode(HANDLE p_DeviceHandle, unsigned short p_usNodeId, unsigned int & p_rlErrorCode);
int PositionControl(HANDLE p_DeviceHandle, unsigned short p_usNodeId, unsigned int & p_rlErrorCode, long PosDesired);
int DemoProfilePositionMode(HANDLE p_DeviceHandle, unsigned short p_usNodeId, unsigned int & p_rlErrorCode);
int Demo(unsigned int* p_pErrorCode);
int Home(unsigned int* p_pErrorCode);
int DemoSafetyPos(unsigned int* p_pErrorCode, long PosDesired);
int PrepareDemo(unsigned int* p_pErrorCode);
int PrintAvailableInterfaces();
int PrintAvailablePorts(char* p_pInterfaceNameSel);
int PrintAvailableProtocols();
int PrintDeviceVersion();
void PrintUsage()
{
cout << "Usage: HomingCmd" << endl;
cout << "\t-h : this help" << endl;
cout << "\t-n : node id (default 1)" << endl;
cout << "\t-d : device name (EPOS2, EPOS4, default - EPOS4)" << endl;
cout << "\t-s : protocol stack name (MAXON_RS232, CANopen, MAXON SERIAL V2, default - MAXON SERIAL V2)" << endl;
cout << "\t-i : interface name (RS232, USB, CAN_ixx_usb 0, CAN_kvaser_usb 0,... default - USB)" << endl;
cout << "\t-p : port name (COM1, USB0, CAN0,... default - USB0)" << endl;
cout << "\t-b : baudrate (115200, 1000000,... default - 1000000)" << endl;
cout << "\t-l : list available interfaces (valid device name and protocol stack required)" << endl;
cout << "\t-r : list supported protocols (valid device name required)" << endl;
cout << "\t-v : display device version" << endl;
}
void LogError(string functionName, int p_lResult, unsigned int p_ulErrorCode)
{
cerr << g_programName << ": " << functionName << " failed (result=" << p_lResult << ", errorCode=0x" << std::hex << p_ulErrorCode << ")"<< endl;
}
void LogInfo(string message)
{
cout << message << endl;
}
void SeparatorLine()
{
const int lineLength = 65;
for(int i=0; i<lineLength; i++)
{
cout << "-";
}
cout << endl;
}
void PrintSettings()
{
stringstream msg;
msg << "default settings:" << endl;
msg << "node id = " << g_usNodeId << endl;
msg << "device name = '" << g_deviceName << "'" << endl;
msg << "protocal stack name = '" << g_protocolStackName << "'" << endl;
msg << "interface name = '" << g_interfaceName << "'" << endl;
msg << "port name = '" << g_portName << "'"<< endl;
msg << "baudrate = " << g_baudrate;
LogInfo(msg.str());
SeparatorLine();
}
void SetDefaultParameters()
{
//USB
g_usNodeId = 1;
g_deviceName = "EPOS4";
g_protocolStackName = "MAXON SERIAL V2";
g_interfaceName = "USB";
g_portName = "USB0";
g_baudrate = 1000000;
}
int OpenDevice(unsigned int* p_pErrorCode)
{
int lResult = MMC_FAILED;
char* pDeviceName = new char[255];
char* pProtocolStackName = new char[255];
char* pInterfaceName = new char[255];
char* pPortName = new char[255];
strcpy(pDeviceName, g_deviceName.c_str());
strcpy(pProtocolStackName, g_protocolStackName.c_str());
strcpy(pInterfaceName, g_interfaceName.c_str());
strcpy(pPortName, g_portName.c_str());
LogInfo("Open device...");
g_pKeyHandle = VCS_OpenDevice(pDeviceName, pProtocolStackName, pInterfaceName, pPortName, p_pErrorCode);
if(g_pKeyHandle!=0 && *p_pErrorCode == 0)
{
unsigned int lBaudrate = 0;
unsigned int lTimeout = 0;
if(VCS_GetProtocolStackSettings(g_pKeyHandle, &lBaudrate, &lTimeout, p_pErrorCode)!=0)
{
if(VCS_SetProtocolStackSettings(g_pKeyHandle, g_baudrate, lTimeout, p_pErrorCode)!=0)
{
if(VCS_GetProtocolStackSettings(g_pKeyHandle, &lBaudrate, &lTimeout, p_pErrorCode)!=0)
{
if(g_baudrate==(int)lBaudrate)
{
lResult = MMC_SUCCESS;
}
}
}
}
}
else
{
g_pKeyHandle = 0;
}
delete []pDeviceName;
delete []pProtocolStackName;
delete []pInterfaceName;
delete []pPortName;
return lResult;
}
int CloseDevice(unsigned int* p_pErrorCode)
{
int lResult = MMC_FAILED;
*p_pErrorCode = 0;
LogInfo("Close device");
if(VCS_CloseDevice(g_pKeyHandle, p_pErrorCode)!=0 && *p_pErrorCode == 0)
{
lResult = MMC_SUCCESS;
}
return lResult;
}
int ParseArguments(int argc, char** argv)
{
int lOption;
int lResult = MMC_SUCCESS;
opterr = 0;
while((lOption = getopt(argc, argv, "hlrvHd:s:i:p:b:n:P:")) != -1)
{
switch (lOption)
{
case 'h':
PrintUsage();
lResult = 1;
break;
case 'd':
g_deviceName = optarg;
break;
case 's':
g_protocolStackName = optarg;
break;
case 'i':
g_interfaceName = optarg;
break;
case 'p':
g_portName = optarg;
break;
case 'b':
g_baudrate = atoi(optarg);
break;
case 'n':
g_usNodeId = (unsigned short)atoi(optarg);
break;
case 'l':
g_eAppMode = AM_INTERFACE_LIST;
break;
case 'r':
g_eAppMode = AM_PROTOCOL_LIST;
break;
case 'v':
g_eAppMode = AM_VERSION_INFO;
break;
case 'H':
g_eAppMode = AM_HOMING;
break;
case 'P':
g_PosDesired = (long)atoi(optarg);
g_eAppMode = AM_POSITION_CON;
break;
case '?': // unknown option...
stringstream msg;
msg << "Unknown option: '" << char(optopt) << "'!";
LogInfo(msg.str());
PrintUsage();
lResult = MMC_FAILED;
break;
}
}
return lResult;
}
//int HomingMode(HANDLE p_DeviceHandle, unsigned short p_usNodeId, unsigned int & p_rlErrorCode)
//{
// int lResult = MMC_SUCCESS;
// stringstream msg;
//
// g_HomingAcceleration = 1000;
// g_SpeedSwitch = 200;
// g_SpeedIndex = 10;
// g_HomeOffset = 24576;
//
// msg << "set Homing mode, node = " << p_usNodeId;
// LogInfo(msg.str());
//
// if(VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, g_digitalIO_4, &g_None, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) == 0)
// {
// LogError("VCS_SetObject", lResult, p_rlErrorCode);
// lResult = MMC_FAILED;
// }
// else
// {
// if(VCS_SetHomingParameter(p_DeviceHandle, p_usNodeId, g_HomingAcceleration, g_SpeedSwitch, g_SpeedIndex, g_HomeOffset, g_CurrentThreshold,
// g_HomePosition, &p_rlErrorCode) == 0)
// {
// LogError("VCS_SetHomingParameter", lResult, p_rlErrorCode);
// lResult = MMC_FAILED;
// }
// else
// {
// if(VCS_GetHomingParameter(p_DeviceHandle, p_usNodeId, &g_HomingAcceleration, &g_SpeedSwitch, &g_SpeedIndex, &g_HomeOffset, &g_CurrentThreshold,
// &g_HomePosition, &p_rlErrorCode) == 0)
// {
// LogError("VCS_GetHomingParameter", lResult, p_rlErrorCode);
// lResult = MMC_FAILED;
// }
// else
// {
// stringstream msg;
// msg << "Homing Parameters, node = " << p_usNodeId << endl;
// msg << "HomingAcceleration = " << g_HomingAcceleration << endl;
// msg << "SpeedSwitch = " << g_SpeedSwitch << endl;
// msg << "SpeedIndex = " << g_SpeedIndex << endl;
// msg << "HomeOffset = " << g_HomeOffset << endl;
// msg << "CurrentThreshold = " << g_interfaceName << endl;
// msg << "HomePosition = " << g_HomePosition;
// LogInfo(msg.str());
// SeparatorLine();
// if(VCS_ActivateHomingMode(p_DeviceHandle, p_usNodeId, &p_rlErrorCode) == 0)
// {
// LogError("VCS_ActivateHomingMode", lResult, p_rlErrorCode);
// lResult = MMC_FAILED;
// }
// else
// {
// if(VCS_FindHome(p_DeviceHandle, p_usNodeId, HM_NEGATIVE_LIMIT_SWITCH_AND_INDEX, &p_rlErrorCode) == 0)
// {
// LogError("VCS_FindHome", lResult, p_rlErrorCode);
// lResult = MMC_FAILED;
// }
// else
// {
// stringstream msg;
// LogInfo("Finding Home....");
// VCS_WaitForHomingAttained(p_DeviceHandle, p_usNodeId, 100000, &p_rlErrorCode);
// msg << "Home position is set, node = " << p_usNodeId;
// LogInfo(msg.str());
// if(VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, g_digitalIO_4, &g_QuickStop, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) == 0)
// {
// LogError("VCS_SetObject", lResult, p_rlErrorCode);
// lResult = MMC_FAILED;
// }
// else
// {
// if(VCS_GetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, g_digitalIO_4, &data, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) == 0)
// {
// LogError("VCS_GetObject", lResult, p_rlErrorCode);
// lResult = MMC_FAILED;
// }
// else
// {
// if(int(data) == 28)
// {
// LogInfo("Homing is complete, DigitalIO 4 is set as QuickStop");
// SeparatorLine();
// }
// else
// {
// LogInfo("Homing is not complete, DigitalIO 4 is NOT set as QuickStop");
// SeparatorLine();
// }
// }
// }
// }
//
// }
// }
// }
// }
//
// return lResult;
//}
//int BothHomingMode(HANDLE p_DeviceHandle, unsigned short p_usNodeId, unsigned int & p_rlErrorCode)
//{
// int lResult = MMC_SUCCESS;
// stringstream msg;
//
// g_HomingAcceleration = 1000;
// g_SpeedSwitch = 200;
// g_SpeedIndex = 10;
// g_HomeOffset = 24576;
//
// LogInfo("set Synchronized Homing mode");
//
// if(VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, 0x04, &g_None, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) &&
// VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, 0x01, &g_NegativeLimit, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) == 0)
// {
//
// lResult = MMC_FAILED;
// LogError("VCS_SetObject", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_SetHomingParameter(p_DeviceHandle, p_usNodeId, g_HomingAcceleration, g_SpeedSwitch, g_SpeedIndex, g_HomeOffset, g_CurrentThreshold,
// g_HomePosition, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_SetHomingParameter", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_GetHomingParameter(p_DeviceHandle, p_usNodeId, &g_HomingAcceleration, &g_SpeedSwitch, &g_SpeedIndex, &g_HomeOffset, &g_CurrentThreshold,
// &g_HomePosition, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_GetHomingParameter", lResult, p_rlErrorCode);
// }
// else
// {
// stringstream msg;
// msg << "Homing Parameters, node = " << p_usNodeId << endl;
// msg << "HomingAcceleration = " << g_HomingAcceleration << endl;
// msg << "SpeedSwitch = " << g_SpeedSwitch << endl;
// msg << "SpeedIndex = " << g_SpeedIndex << endl;
// msg << "HomeOffset = " << g_HomeOffset << endl;
// msg << "CurrentThreshold = " << g_interfaceName << endl;
// msg << "HomePosition = " << g_HomePosition;
// LogInfo(msg.str());
// SeparatorLine();
// if(VCS_ActivateHomingMode(p_DeviceHandle, p_usNodeId, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_ActivateHomingMode", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_FindHome(p_DeviceHandle, p_usNodeId, HM_NEGATIVE_LIMIT_SWITCH_AND_INDEX, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_FindHome", lResult, p_rlErrorCode);
// }
// else
// {
// stringstream msg;
// LogInfo("Finding Home....");
// VCS_WaitForHomingAttained(p_DeviceHandle, p_usNodeId, 100000, &p_rlErrorCode);
// msg << "Home position is set, node = " << p_usNodeId;
// LogInfo(msg.str());
//
// if(VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, 0x04, &g_None, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)
// && VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, 0x01, &g_QuickStop, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)== 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_SetObject", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_ActivateProfileVelocityMode(p_DeviceHandle, p_usNodeId, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_ActivateProfileVelocityMode", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_MoveWithVelocity(p_DeviceHandle, p_usNodeId, g_SpeedSwitch, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_MoveWithVelocity", lResult, p_rlErrorCode);
// }
// else
// {
// LogInfo("Finding Positive Limit Switch...");
// do
// {
// if(VCS_GetQuickStopState(p_DeviceHandle, p_usNodeId, &g_IsQuickStopped, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_GetQuickStopState", lResult, p_rlErrorCode);
// }
// }
// while (g_IsQuickStopped == 0);
// LogInfo("Found Positive Limit Switch!");
// if(VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, 0x01, &g_QuickStop, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)
// && VCS_SetObject(p_DeviceHandle, p_usNodeId, 0x3141, 0x02, &g_LowActive, g_NbofBytesToWrite_2, &g_NbofBytesToWrite_2, &p_rlErrorCode)== 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_SetObject", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_MoveWithVelocity(p_DeviceHandle, p_usNodeId, -g_SpeedIndex, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_MoveWithVelocity", lResult, p_rlErrorCode);
// }
// else
// {
// LogInfo("Finding Positive Home...");
// do
// {
// if(VCS_GetQuickStopState(p_DeviceHandle, p_usNodeId, &g_IsQuickStopped, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_GetQuickStopState", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_GetPositionIs(p_DeviceHandle, p_usNodeId, &g_CapturedPosition, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_GetPositionIs", lResult, p_rlErrorCode);
// }
// }
// }
// while (g_IsQuickStopped == 0);
// stringstream msg;
//
// g_PosPositiveLimit = (long)(g_CapturedPosition - g_HomeOffset);
// g_PosCenter = (long)(g_PosPositiveLimit/2);
//
// msg << "Found Positive Limit : " << g_PosPositiveLimit << endl;
// msg << "Found Center Position : " << g_PosCenter;
// LogInfo(msg.str());
//
// if(VCS_SetObject(p_DeviceHandle, p_usNodeId, 0x3141, 0x02, &g_HighActive, g_NbofBytesToWrite_2, &g_NbofBytesToWrite_2, &p_rlErrorCode)
// && VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, 0x01, &g_None, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)== 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_SetObject", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_ActivateProfilePositionMode(p_DeviceHandle, p_usNodeId, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_ActivateProfilePositionMode", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_MoveToPosition(p_DeviceHandle, p_usNodeId, g_PosCenter, 1, 1, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_MoveToPosition", lResult, p_rlErrorCode);
// }
// else
// {
// VCS_WaitForTargetReached(p_DeviceHandle, p_usNodeId, 100000, &p_rlErrorCode);
// if(VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, 0x01, &g_NegativeLimit, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)
// && VCS_SetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, 0x04, &g_QuickStop, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)== 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_SetObject", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_SetHomingParameter(p_DeviceHandle, p_usNodeId, g_HomingAcceleration, g_SpeedSwitch, g_SpeedIndex, 0, g_CurrentThreshold,
// g_HomePosition, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_SetHomingParameter", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_ActivateHomingMode(p_DeviceHandle, p_usNodeId, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_ActivateHomingMode", lResult, p_rlErrorCode);
// }
// else
// {
// if(VCS_FindHome(p_DeviceHandle, p_usNodeId, HM_ACTUAL_POSITION, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_FindHome", lResult, p_rlErrorCode);
// }
// else
// {
// VCS_WaitForHomingAttained(p_DeviceHandle, p_usNodeId, 15000, &p_rlErrorCode);
// if(VCS_GetHomingParameter(p_DeviceHandle, p_usNodeId, &g_HomingAcceleration, &g_SpeedSwitch, &g_SpeedIndex, &g_HomeOffset, &g_CurrentThreshold,
// &g_HomePosition, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_GetHomingParameter", lResult, p_rlErrorCode);
// }
// else
// {
// stringstream msg;
// msg << "Homing Parameters, node = " << p_usNodeId << endl;
// msg << "HomingAcceleration = " << g_HomingAcceleration << endl;
// msg << "SpeedSwitch = " << g_SpeedSwitch << endl;
// msg << "SpeedIndex = " << g_SpeedIndex << endl;
// msg << "HomeOffset = " << g_HomeOffset << endl;
// msg << "CurrentThreshold = " << g_interfaceName << endl;
// msg << "HomePosition = " << g_HomePosition;
// LogInfo(msg.str());
// SeparatorLine();
// }
// }
// }
// }
// }
// }
// }
// }
// }
// }
// }
// }
// }
// if(VCS_GetObject(p_DeviceHandle, p_usNodeId, g_digitalIOIndex, g_digitalIO_4, &data, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) == 0)
// {
// lResult = MMC_FAILED;
// LogError("VCS_GetObject", lResult, p_rlErrorCode);
// }
// else
// {
// if(int(data) == 28)
// {
// LogInfo("Homing is complete, DigitalIO 4 is set as QuickStop");
// SeparatorLine();
// }
// else
// {
// LogInfo("Homing is not complete, DigitalIO 4 is NOT set as QuickStop");
// SeparatorLine();
// }
// }
// }
// }
//
// }
// }
// }
//
// return lResult;
//}
int SyncHomingMode(HANDLE p_DeviceHandle, unsigned short p_usNodeId, unsigned int & p_rlErrorCode)
{
int lResult = MMC_SUCCESS;
stringstream msg;
g_HomingAcceleration = 1000;
g_SpeedSwitch = 200;
g_SpeedIndex = 10;
g_HomePosition = 0;
LogInfo("set Synchronized Homing mode");
// Parameter setting for Homing mode
for (int i=0; i<g_NbofJoint; i++)
{
if(lResult == MMC_SUCCESS)
{
if(VCS_SetObject(p_DeviceHandle, g_usNodeId_arr[i], g_SoftPosLimitIndex, g_SoftPosMin, &g_PosLimitMin[i], g_NbofBytesToWrite_4, &g_NbofBytesToWrite_4, &p_rlErrorCode) &&
VCS_SetObject(p_DeviceHandle, g_usNodeId_arr[i], g_SoftPosLimitIndex, g_SoftPosMax, &g_PosLimitMax[i], g_NbofBytesToWrite_4, &g_NbofBytesToWrite_4, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_SetObject", lResult, p_rlErrorCode);
}
}
}
for (int i=0; i<g_NbofJoint; i++)
{
if(lResult == MMC_SUCCESS)
{
if(VCS_SetObject(p_DeviceHandle, g_usNodeId_arr[i], g_digitalIOIndex, g_digitalIO_4, &g_None, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) &&
VCS_SetObject(p_DeviceHandle, g_usNodeId_arr[i], g_digitalIOIndex, g_digitalIO_1, &g_NegativeLimit, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_SetObject", lResult, p_rlErrorCode);
}
else
{
if(VCS_SetObject(p_DeviceHandle, g_usNodeId_arr[i], 0x6081, 0x00, &g_ProfileVelocity, g_NbofBytesToWrite_4, &g_NbofBytesToWrite_4, &p_rlErrorCode) == 0)
{
LogError("VCS_SetObject", lResult, p_rlErrorCode);
lResult = MMC_FAILED;
}
if(VCS_SetHomingParameter(p_DeviceHandle, g_usNodeId_arr[i], g_HomingAcceleration, (unsigned int)(g_SpeedSwitch*g_AxisVelRatio[i]),
(unsigned int)(g_SpeedIndex*g_AxisVelRatio[i]), g_HomeOffset_arr[i], g_CurrentThreshold, g_HomePosition, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_SetHomingParameter", lResult, p_rlErrorCode);
}
else
{
if(VCS_ActivateHomingMode(p_DeviceHandle, g_usNodeId_arr[i], &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_ActivateHomingMode", lResult, p_rlErrorCode);
}
}
}
}
}
// Find Negative limit of Joint 1, 3, 5, 7
for(int i=2; i>=0; i--)
{
if(lResult == MMC_SUCCESS)
{
if(VCS_FindHome(p_DeviceHandle, g_HomingSecondId[i], HM_NEGATIVE_LIMIT_SWITCH_AND_INDEX, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_FindHome", lResult, p_rlErrorCode);
}
}
}
// Waiting for Negative Limit
if(lResult == MMC_SUCCESS)
{
stringstream msg;
LogInfo("Finding Negative Limit....");
VCS_WaitForHomingAttained(p_DeviceHandle, g_HomingSecondId[0], 100000, &p_rlErrorCode);
VCS_WaitForHomingAttained(p_DeviceHandle, g_HomingSecondId[1], 100000, &p_rlErrorCode);
VCS_WaitForHomingAttained(p_DeviceHandle, g_HomingSecondId[2], 100000, &p_rlErrorCode);
msg << "Negative Limit is set, node = " << g_HomingSecondId[0] << ", " << g_HomingSecondId[1] << ", " << g_HomingSecondId[2] ;
LogInfo(msg.str());
}
// Ready for finding Positive Limit
if(lResult == MMC_SUCCESS)
{
for (int i=0; i<3; i++)
{
if(VCS_SetObject(p_DeviceHandle, g_HomingSecondId[i], g_digitalIOIndex, g_digitalIO_4, &g_None, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)
&& VCS_SetObject(p_DeviceHandle, g_HomingSecondId[i], g_digitalIOIndex, g_digitalIO_1, &g_QuickStop, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)== 0)
{
lResult = MMC_FAILED;
LogError("VCS_SetObject", lResult, p_rlErrorCode);
}
}
}
// Approach to Positive Limit Switch Aggressively
for (int i=2; i>=0; i--)
{
if(lResult == MMC_SUCCESS)
{
// g_ProfileVelocity = 800;
if(VCS_SetObject(p_DeviceHandle, g_HomingSecondId[i], 0x6081, 0x00, &g_ProfileVelocity, g_NbofBytesToWrite_4, &g_NbofBytesToWrite_4, &p_rlErrorCode) == 0)
{
LogError("VCS_SetObject", lResult, p_rlErrorCode);
lResult = MMC_FAILED;
}
else
{
if(VCS_ActivateProfilePositionMode(p_DeviceHandle, g_HomingSecondId[i], &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_ActivateProfilePositionMode", lResult, p_rlErrorCode);
}
}
}
}
for (int i=2; i>=0; i--)
{
if(lResult == MMC_SUCCESS)
{
if(VCS_MoveToPosition(p_DeviceHandle, g_HomingSecondId[i], g_SafeRange[g_HomingSecondId[i]-1], 1, 1, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_MoveToPosition", lResult, p_rlErrorCode);
}
}
}
if(lResult == MMC_SUCCESS)
{
VCS_WaitForTargetReached(p_DeviceHandle, g_HomingSecondId[2], 100000, &p_rlErrorCode);
VCS_WaitForTargetReached(p_DeviceHandle, g_HomingSecondId[1], 100000, &p_rlErrorCode);
VCS_WaitForTargetReached(p_DeviceHandle, g_HomingSecondId[0], 100000, &p_rlErrorCode);
}
// Find Positive limit Switch of Joint 1, 3, 5
if(lResult == MMC_SUCCESS)
{
for (int i=0; i<3; i++)
{
if(VCS_ActivateProfileVelocityMode(p_DeviceHandle, g_HomingSecondId[i], &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_ActivateProfileVelocityMode", lResult, p_rlErrorCode);
}
}
}
for (int i=2; i>=0; i--)
{
if(lResult == MMC_SUCCESS)
{
if(VCS_MoveWithVelocity(p_DeviceHandle, g_HomingSecondId[i], (long)(g_SpeedSwitch*g_AxisVelRatio[g_HomingSecondId[i]-1]), &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_MoveWithVelocity", lResult, p_rlErrorCode);
}
}
}
// Waiting for Positive Limit Switch
if(lResult == MMC_SUCCESS)
{
stringstream msg;
LogInfo("Finding Positive Limit Switch...");
for (int i=0; i<3; i++)
{
do
{
if(VCS_GetQuickStopState(p_DeviceHandle, g_HomingSecondId[i], &g_IsQuickStopped, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_GetQuickStopState", lResult, p_rlErrorCode);
}
}
while (g_IsQuickStopped == 0);
}
}
for (int i=0; i<3; i++)
{
if(lResult == MMC_SUCCESS)
{
if(VCS_SetObject(p_DeviceHandle, g_HomingSecondId[i], g_digitalIOIndex, g_digitalIO_1, &g_QuickStop, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)
&& VCS_SetObject(p_DeviceHandle, g_HomingSecondId[i], g_digitalIPropIndex, g_digiatlIPol, &g_LowActive, g_NbofBytesToWrite_2, &g_NbofBytesToWrite_2, &p_rlErrorCode)== 0)
{
lResult = MMC_FAILED;
LogError("VCS_SetObject", lResult, p_rlErrorCode);
}
}
}
// Find Positive Limit of Joint 1, 3, 5
for (int i=2; i>=0; i--)
{
if(lResult == MMC_SUCCESS)
{
if(VCS_MoveWithVelocity(p_DeviceHandle, g_HomingSecondId[i], -(long)(g_SpeedIndex*g_AxisVelRatio[g_HomingSecondId[i]-1]), &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_MoveWithVelocity", lResult, p_rlErrorCode);
}
}
}
// Waiting for Positive Limit
if(lResult == MMC_SUCCESS)
{
stringstream msg;
for (int i=0; i<3; i++)
{
do
{
if(VCS_GetQuickStopState(p_DeviceHandle, g_HomingSecondId[i], &g_IsQuickStopped, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_GetQuickStopState", lResult, p_rlErrorCode);
}
else
{
if(VCS_GetPositionIs(p_DeviceHandle, g_HomingSecondId[i], &g_CapturedPosition, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_GetPositionIs", lResult, p_rlErrorCode);
}
}
}
while (g_IsQuickStopped == 0);
stringstream msg;
g_PosPositiveLimit[g_HomingSecondId[i]-1] = (long)(g_CapturedPosition - g_HomeOffset_arr[g_HomingSecondId[i]-1]);
g_PosCenter[g_HomingSecondId[i]-1] = (long)(g_PosPositiveLimit[g_HomingSecondId[i]-1]/2);
g_PosLimitMin[g_HomingSecondId[i]-1] = -g_PosCenter[g_HomingSecondId[i]-1] - 5000;
g_PosLimitMax[g_HomingSecondId[i]-1] = g_PosCenter[g_HomingSecondId[i]-1] + 5000;
}
msg << "Positive Limit is set, node = " << g_HomingSecondId[0] << ", " << g_HomingSecondId[1] << ", " << g_HomingSecondId[2] ;
LogInfo(msg.str());
SeparatorLine();
}
// Set Limit Switch as None
if(lResult == MMC_SUCCESS)
{
for (int i=0; i<3; i++)
{
if(VCS_SetObject(p_DeviceHandle, g_HomingSecondId[i], g_digitalIPropIndex, g_digiatlIPol, &g_HighActive, g_NbofBytesToWrite_2, &g_NbofBytesToWrite_2, &p_rlErrorCode)
&& VCS_SetObject(p_DeviceHandle, g_HomingSecondId[i], g_digitalIOIndex, g_digitalIO_1, &g_None, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode)== 0)
{
lResult = MMC_FAILED;
LogError("VCS_SetObject", lResult, p_rlErrorCode);
}
else
{
if(VCS_ActivateProfilePositionMode(p_DeviceHandle, g_HomingSecondId[i], &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_ActivateProfilePositionMode", lResult, p_rlErrorCode);
}
}
}
}
// Move to Center Position
for (int i=2; i>=0; i--)
{
if(lResult == MMC_SUCCESS)
{
if(VCS_MoveToPosition(p_DeviceHandle, g_HomingSecondId[i], g_PosCenter[g_HomingSecondId[i]-1], 1, 1, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_MoveToPosition", lResult, p_rlErrorCode);
}
}
}
if(lResult == MMC_SUCCESS)
{
VCS_WaitForTargetReached(p_DeviceHandle, g_HomingSecondId[2], 100000, &p_rlErrorCode);
VCS_WaitForTargetReached(p_DeviceHandle, g_HomingSecondId[1], 100000, &p_rlErrorCode);
VCS_WaitForTargetReached(p_DeviceHandle, g_HomingSecondId[0], 100000, &p_rlErrorCode);
}
// Set Center position as Home
for (int i=0; i<3; i++)
{
if(lResult == MMC_SUCCESS)
{
if(VCS_SetObject(p_DeviceHandle, g_HomingSecondId[i], g_digitalIOIndex, g_digitalIO_1, &g_NegativeLimit, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) &&
VCS_SetObject(p_DeviceHandle, g_HomingSecondId[i], g_digitalIOIndex, g_digitalIO_4, &g_QuickStop, g_NbofBytesToWrite_1, &g_NbofBytesToWrite_1, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_SetObject", lResult, p_rlErrorCode);
}
else
{
if(VCS_SetHomingParameter(p_DeviceHandle, g_HomingSecondId[i], g_HomingAcceleration, (unsigned int)(g_SpeedSwitch*g_AxisVelRatio[g_HomingSecondId[i]-1]),
(unsigned int)(g_SpeedIndex*g_AxisVelRatio[g_HomingSecondId[i]-1]), 0, g_CurrentThreshold, g_HomePosition, &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_SetHomingParameter", lResult, p_rlErrorCode);
}
else
{
if(VCS_ActivateHomingMode(p_DeviceHandle, g_HomingSecondId[i], &p_rlErrorCode) == 0)
{
lResult = MMC_FAILED;
LogError("VCS_ActivateHomingMode", lResult, p_rlErrorCode);
}
else