HCBoardManager_Axis.cs
22.6 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuichuanLibrary
{
partial class HCBoardManager
{
#region 单轴控制
public static bool ServoOn(short axisNo)
{
if (!CardInitOk())
{
return false;
}
//【2】上使能
UInt32 ret = ImcApi.IMC_AxServoOn(nCardHandle, axisNo, 1);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] ServoOn FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowLog(" Axis[" + axisNo + "] ServoOn OK");
return true;
}
}
public static bool ServoOff(short axisNo)
{
if (!CardInitOk())
{
return false;
}
//【2】下使能
UInt32 ret = ImcApi.IMC_AxServoOff(nCardHandle, axisNo, 1);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] ServoOff FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowLog(" Axis[" + axisNo + "] ServoOff OK");
return true;
}
}
#region 回零处理
/// <summary>
/// 开始回零
/// </summary>
/// <param name="axisNo">轴号</param>
/// <param name="highVel">回零高速</param>
/// <param name="lowVel">回零低速</param>
/// <param name="acc">回零加速度</param>
/// <param name="homeMethod">回零方法,默认28</param>
/// <param name="offset">回零偏移,默认0</param>
/// <param name="overtime">超时时间,默认120000</param>
/// <param name="posSrc">端子板信号源,默认0</param>
/// <returns></returns>
public static bool StartHomeMove(short axisNo,uint highVel, uint lowVel, uint acc,short homeMethod=28, int offset=0, uint overtime=120000, short posSrc=0)
{
if (!CardInitOk())
{
return false;
}
//【2】写入回原参数
ImcApi.THomingPara tHomingPara = new ImcApi.THomingPara();
tHomingPara.homeMethod = homeMethod;
tHomingPara.offset = offset;
tHomingPara.highVel = highVel;
tHomingPara.lowVel = lowVel;
tHomingPara.acc = acc;
tHomingPara.overtime = overtime;
tHomingPara.posSrc = posSrc;
HCLogUtil.info("Axis[" + axisNo + "] StartHoming highVel["+highVel+ "],lowVel[" + lowVel + "],acc[" + acc + "],homeMethod[" + homeMethod + "],offset[" + offset + "],overtime[" + overtime + "],posSrc["+ posSrc + "]");
//【3】开始回原
UInt32 ret = ImcApi.IMC_StartHoming(nCardHandle, axisNo, ref tHomingPara);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "]StartHoming FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowLog(" Axis[" + axisNo + "] StartHoming OK");
return true;
}
}
public static bool StopHomeMove(short axisNo)
{
if (!CardInitOk())
{
return false;
}
//【2】触发停止回原
UInt32 ret = ImcApi.IMC_StopHoming(nCardHandle, axisNo, 0); //0:表示平滑停止 1:表示急停
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] StopHoming FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false; ;
}
else
{
ShowLog(" Axis[" + axisNo + "] StopHoming OK");
return true;
}
}
public static short GetHomeStatus(short axisNo)
{
if (!CardInitOk())
{
return -1;
}
//【2】获取回原状态
short nHomingStatus = 0;
UInt32 ret = ImcApi.IMC_GetHomingStatus(nCardHandle, axisNo, ref nHomingStatus);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] GetHomingStatus FAIL, ErrorCode=0x" + ret.ToString("x8"));
return -1;
}
else
{
//ShowLog("轴回原状态为" + nHomingStatus.ToString());
return nHomingStatus;
}
}
public static bool FinishHome(short axisNo)
{
if (!CardInitOk())
{
return false;
}
//【2】退出回原模式
UInt32 ret = ImcApi.IMC_FinishHoming(nCardHandle, axisNo);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] FinishHoming FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowLog(" Axis[" + axisNo + "] FinishHoming OK!");
return true;
}
}
/// <summary>
/// 判断回零是否完成
/// </summary>
///<param name="axisNo">轴号</param>
/// <returns></returns>
public static bool HomeingIsEnd(short axisNo)
{
//回零状态结束,并且不再忙碌中
AxisSts sts = GetAxisSts(axisNo);
if (sts.BUSY.Equals(0) && sts.INP.Equals(1))
{
short homests = GetHomeStatus(axisNo);
if (homests.Equals(HomeSts_OK))
{
return true;
}
}
return false;
}
#endregion
/// <summary>
/// 判断轴是否在指定的位置
/// </summary>
/// <param name="axisNo">轴号</param>
/// <param name="targetPos">目标位置</param>
/// <param name="maxError">最大误差脉冲值</param>
/// <returns></returns>
public static bool IsInPosition(short axisNo, double targetPos, double maxError = 10)
{
double currP = GetAxisCurrPos(axisNo);
double chazhi = Math.Abs(currP - targetPos);
if (chazhi < maxError)
{
return true ;
}
return false;
}
public static bool MoveIsEnd(short axisNo )
{
AxisSts sts = GetAxisSts(axisNo);
if (sts.BUSY.Equals(0) && sts.INP.Equals(1))
{
return true;
}
return false;
}
public static bool AbsMove(short axisNo, double ptpPos, double ptpVel , double ptpAcc , double ptpDec )
{
if (!CardInitOk())
{
return false;
}
if (ptpVel > 0 && ptpAcc > 0 && ptpDec > 0)
{
if (!SetPTPSpeed(axisNo, ptpVel, ptpAcc, ptpDec))
{
return false;
}
}
short ptpType = 0;
return StartPtpMove(axisNo, ptpType, ptpPos);
}
public static bool RelMove(short axisNo, double ptpPos, double ptpVel , double ptpAcc , double ptpDec )
{
if (!CardInitOk())
{
return false;
}
if (ptpVel > 0 && ptpAcc > 0 && ptpDec > 0)
{
if (!SetPTPSpeed(axisNo, ptpVel, ptpAcc, ptpDec))
{
return false;
}
}
short ptpType = 1;
return StartPtpMove(axisNo, ptpType, ptpPos);
}
private static bool StartPtpMove(short axisNo, short ptpType, double ptpPos)
{
//【3】启动PTP
UInt32 ret = ImcApi.IMC_StartPtpMove(nCardHandle, axisNo, ptpPos, ptpType);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] StartPtpMove " + ptpType + " FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowLog(" Axis[" + axisNo + "] StartPtpMove " + ptpType + " OK");
return true;
}
}
public static bool SetPTPSpeed(short axisNo, double ptpVel, double ptpAcc, double ptpDec)
{
if (!CardInitOk())
{
return false;
}
UInt32 ret = ImcApi.IMC_SetSingleAxMvPara(nCardHandle, axisNo, ptpVel, ptpAcc, ptpDec);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] SetSingleAxMvPara FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowLog(" Axis[" + axisNo + "] SetSingleAxMvPara OK");
return true;
}
}
public static bool AxisStop(short axisNo)
{
if (!CardInitOk())
{
return false;
}
//【2】轴停止
UInt32 ret = ImcApi.IMC_AxMoveStop(nCardHandle, axisNo, 1); //StopType=1,立即停止
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] MoveStop FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowLog(" Axis[" + axisNo + "] MoveStop OK");
return true;
}
}
/// <summary>
/// 设置齿轮比
/// </summary>
/// <param name="axisNo">轴号</param>
/// <param name="Motor_Revolution">电子齿轮分子:电机分辨率</param>
/// <param name="Shaft_Revolution">电子齿轮分母:一圈脉冲数</param>
/// <returns></returns>
public static bool SetEcatSdo(ushort axisNo, int Motor_Revolution, int Shaft_Revolution)
{
if (!CardInitOk())
{
return false;
}
//【3】获取轴号对应的实际地址
short nphyStationId = 0, nphySlotId = 0;
uint abort_code = 0;
UInt32 ret = ImcApi.IMC_GetAxEcatStation(nCardHandle, axisNo, ref nphyStationId, ref nphySlotId);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] IMC_GetAxEcatStation FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
//【4】设置电子齿轮分子
ret = ImcApi.IMC_SetEcatSdo(nCardHandle, nphyStationId, 0x6091, 0x01, BitConverter.GetBytes(Motor_Revolution), 4, ref abort_code);
if (ret != 0 | abort_code != 0)
{
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] IMC_SetEcatSdo_1 FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowErrorLog(" Axis[" + axisNo + "] IMC_SetEcatSdo_1 FAIL, ErrorCode=0x" + abort_code.ToString("x8"));
return false;
}
}
//【4】设置电子齿轮分母
ret = ImcApi.IMC_SetEcatSdo(nCardHandle, nphyStationId, 0x6091, 0x02, BitConverter.GetBytes(Shaft_Revolution), 4, ref abort_code);
if (ret != 0 | abort_code != 0)
{
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] IMC_SetEcatSdo_2 FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowErrorLog(" Axis[" + axisNo + "] IMC_SetEcatSdo_2 FAIL, ErrorCode=0x" + abort_code.ToString("x8"));
return false;
}
}
return true;
}
public static bool SpeedMove(short axisNo, double targetVel)
{
if (!CardInitOk())
{
return false;
}
UInt32 ret = ImcApi.IMC_StartJogMove(nCardHandle, axisNo, targetVel);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] speed ["+targetVel+ "] StartJogMove FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
else
{
ShowLog(" Axis[" + axisNo + "] speed [" + targetVel + "] StartJogMove OK");
return true;
}
}
/// <summary>
/// 获取轴规划模式
/// </summary>
///<param name="axisNo">轴号</param>
/// <returns></returns>
public static short GetAxisPrfMode(short axisNo)
{
if (!CardInitOk())
{
return -1;
}
try
{
Int16[] nTimerAxPrfMode = { 0 };
UInt32 ret = ImcApi.IMC_GetAxPrfMode(nCardHandle, axisNo, nTimerAxPrfMode, 1); //变量需要定义为数组,默认读取1个轴
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] GetAxPrfMode FAIL, ErrorCode=0x" + ret.ToString("x8"));
return -1;
}
if (nTimerAxPrfMode.Length > 0)
{
return nTimerAxPrfMode[0];
}
}
catch (Exception ex)
{
ShowErrorLog("" + ex.ToString());
}
return -1;
}
/// <summary>
/// 获取轴状态
/// </summary>
///<param name="axisNo">轴号</param>
/// <returns></returns>
public static AxisSts GetAxisSts(short axisNo)
{
AxisSts sts = new AxisSts();
if (!CardInitOk())
{
return sts;
}
Int32[] nTimerAxSts = { 0 };
UInt32 ret = ImcApi.IMC_GetAxSts(nCardHandle, axisNo, nTimerAxSts, 1);
if (ret != 0 || nTimerAxSts.Length <= 0)
{
ShowErrorLog(" Axis[" + axisNo + "] GetAxSts FAIL, ErrorCode=0x" + ret.ToString("x8"));
return sts;
}
int axisState = nTimerAxSts[0];
//【3】更新界面显示
sts.ALM = ((axisState & 0x01) == 0x01) ? 1 : 0; //报警信号
sts.ServoOn = ((axisState & 0x02) == 0x02) ? 1 : 0; //使能信号
sts.BUSY = ((axisState & 0x04) == 0x04) ? 1 : 0; //轴忙信号
sts.INP = ((axisState & 0x08) == 0x08) ? 1 : 0; //轴到位信号
sts.WARN = ((axisState & 0x1000) == 0x1000) ? 1 : 0; //警告信号
sts.EMG = ((axisState & 0x200) == 0x200) ? 1 : 0; //急停信号
Int16 nTimerAxInput = 0;
ret = ImcApi.IMC_GetAxEcatDigitalInput(nCardHandle, axisNo, ref nTimerAxInput);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "]GetAxEcatDigitalInput ,ErrorCode=0x" + ret.ToString("x8"));
return sts;
}
sts.NEL = ((nTimerAxInput & 0x01) == 0x01) ? 1 : 0; //负限位信号
sts.PEL = ((nTimerAxInput & 0x02) == 0x02) ? 1 : 0; //正限位信号
sts.ORG = ((nTimerAxInput & 0x04) == 0x04) ? 1 : 0; //原点信号
return sts;
}
/// <summary>
/// 获取轴规划位置
/// </summary>
///<param name="axisNo">轴号</param>
/// <returns></returns>
public static double GetAxisPrfPos(short axisNo)
{
if (!CardInitOk())
{
return -1;
}
double[] nTimerAxPrfPos = { 0 };
UInt32 ret = ImcApi.IMC_GetAxPrfPos(nCardHandle, axisNo, nTimerAxPrfPos, 1);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] GetAxPrfPos FAIL, ErrorCode=0x" + ret.ToString("x8"));
return -1;
}
return nTimerAxPrfPos[0];
}
/// <summary>
/// 获取轴规划速度
/// </summary>
///<param name="axisNo">轴号</param>
/// <returns></returns>
public static double GetAxisPrfVel(short axisNo)
{
if (!CardInitOk())
{
return -1;
}
double[] nTimerAxPrfVel = { 0 };
UInt32 ret = ImcApi.IMC_GetAxPrfVel(nCardHandle, axisNo, nTimerAxPrfVel, 1);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] GetAxPrfVel FAIL, ErrorCode=0x" + ret.ToString("x8"));
return -1;
}
return nTimerAxPrfVel[0];
}
/// <summary>
/// 获取轴反馈位置
/// </summary>
///<param name="axisNo">轴号</param>
/// <returns></returns>
public static double GetAxisCurrPos(short axisNo)
{
if (!CardInitOk())
{
return -1;
}
double[] nTimerAxEncPos = { 0 };
UInt32 ret = ImcApi.IMC_GetAxEncPos(nCardHandle, axisNo, nTimerAxEncPos, 1);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] GetAxEncPos FAIL, ErrorCode=0x" + ret.ToString("x8"));
return -1;
}
return nTimerAxEncPos[0];
}
/// <summary>
///获取轴反馈速度
/// </summary>
///<param name="axisNo">轴号</param>
/// <returns></returns>
public static double GetAxisCurrVel(short axisNo)
{
if (!CardInitOk())
{
return -1;
}
double[] nTimerAxEncVel = { 0 };
UInt32 ret = ImcApi.IMC_GetAxEncVel(nCardHandle, axisNo, nTimerAxEncVel, 1);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] GetAxEncVel FAIL, ErrorCode=0x" + ret.ToString("x8"));
return -1;
}
return nTimerAxEncVel[0];
}
/// <summary>
/// 获取轴反馈加速度
/// </summary>
///<param name="axisNo">轴号</param>
/// <returns></returns>
public static double GetAxisCurrAcc(short axisNo)
{
if (!CardInitOk())
{
return -1;
}
double[] nTimerAxEncAcc = { 0 };
UInt32 ret = ImcApi.IMC_GetAxEncAcc(nCardHandle, axisNo, nTimerAxEncAcc, 1);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] GetAxEncAcc FAIL, ErrorCode=0x" + ret.ToString("x8"));
return -1;
}
return nTimerAxEncAcc[0];
}
/// <summary>
/// 清楚轴报警状态
/// </summary>
///<param name="axisNo">轴号</param>
/// <returns></returns>
public static bool ClearAxisSts(short axisNo)
{
if (!CardInitOk())
{
return false;
}
UInt32 ret = ImcApi.IMC_ClrAxSts(nCardHandle, axisNo);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] ClrAxSts FAIL, ErrorCode=0x" + ret.ToString("x8"));
return false;
}
return true;
}
public static short GetAxErrCode(short axisNo)
{
if (!CardInitOk())
{
return -1;
}
short outV = -1;
UInt32 ret = ImcApi.IMC_GetAxErrCode(nCardHandle, axisNo, ref outV);
if (ret != 0)
{
ShowErrorLog(" Axis[" + axisNo + "] GetAxEncAcc FAIL, ErrorCode=0x" + ret.ToString("x8"));
return -1;
}
return outV;
}
#endregion
#region 轴状态
public static string AxisPrfMode(short axCtrlMode)
{
axCtrlMode = (short)(axCtrlMode & 0xf);
string strCtrlMode = string.Empty;
switch (axCtrlMode)
{
case 0:
strCtrlMode = "0=无模式";
break;
case 1:
strCtrlMode = "1=点对点";
break;
case 2:
strCtrlMode = "2=JOG";
break;
case 3:
strCtrlMode = "3=电子齿轮";
break;
case 4:
strCtrlMode = "4=电子凸轮";
break;
case 5:
strCtrlMode = "5=PVT";
break;
case 6:
strCtrlMode = "6=龙门";
break;
case 7:
strCtrlMode = "7=手轮";
break;
case 9:
strCtrlMode = "8=点位连续";
break;
case 11:
strCtrlMode = "11=插补同步轴";
break;
case 15:
strCtrlMode = "15=回零";
break;
default:
break;
}
return strCtrlMode;
}
public static string AxisHomeSts(short homeStatus)
{
string sStr = "";
switch (homeStatus)
{
case 0:
sStr = "0=正在回零中";
break;
case 1:
sStr = "1=回零中断或者没有开始启动";
break;
case 2:
sStr = "2=回零结束,但没有到设定的目标位置";
break;
case 3:
sStr = "3=回零完成";
break;
case 4:
sStr = "4=回零中发生错误,同时速度不为0";
break;
case 5:
sStr = "5=回零中发生错误,同时速度为0";
break;
default:
break;
}
return sStr;
}
#endregion
/// <summary>
/// 回原点状态:0=正在回零中
/// </summary>
public static short HomeSts_Moveing = 0;
/// <summary>
/// 回原点状态:1=回零中断或者没有开始启动
/// </summary>
public static short HomeSts_NotStart = 1;
/// <summary>
/// 回原点状态:3=回零完成
/// </summary>
public static short HomeSts_OK = 3;
}
}