AxisBean.cs
18.0 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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceLibrary
{
public class AxisBean
{
public static Dictionary<string, List<AxisBean>> List = new Dictionary<string, List<AxisBean>>();
public ConfigMoveAxis Config = null;
public static int TimeoutInterval = 500;
/// <summary>
/// 正常工作过程中判断位置是否到达时使用
/// </summary>
public int LastPosition = 0;
/// <summary>
/// 干涉检测回调 from, to
/// </summary>
public event Func<int,int,(bool,string)> interference;
public string AxisName;
public bool ForceSafeCheck = false;
public string DeviceGroupName;
public AxisBean(ConfigMoveAxis axisConfig, string deviceGroupName)
{
DeviceGroupName = deviceGroupName;
this.Config = axisConfig;
AxisName = deviceGroupName + " " + Config.Explain + "[" + Config.DeviceName + "-" + Config.GetAxisValue() + "]";
if (!List.ContainsKey(deviceGroupName))
List.Add(deviceGroupName, new List<AxisBean>());
List[deviceGroupName].Add(this);
}
int openretry=0;
public bool Open(bool isCheck, out string Msg)
{
Msg = "";
string portName = Config.DeviceName;
short slvAddr = Config.GetAxisValue();
if (AxisManager.IsServeoOn(portName, slvAddr))
{ openretry = 0; return true; }
openretry++;
{
Msg += string.Format("第{0}次尝试打开轴:{1}", openretry, Config.Explain);
if (AxisManager.GetBusyStatus(portName, slvAddr) == 1)
{
AxisManager.SuddenStop(portName, slvAddr);
Msg += string.Format("...伺服忙碌,急停{0}次", openretry);
Thread.Sleep(100);
}
//if (AxisManager.GetAlarmStatus(portName, slvAddr) != 0)
//{
// AxisManager.AlarmClear(portName, slvAddr);
// Msg += string.Format("...尝试清除报警第(0)次", openretry);
// Thread.Sleep(100);
//}
AxisManager.ServoOn(portName, slvAddr);
Thread.Sleep(200);
if (AxisManager.IsServeoOn(portName, slvAddr))
{
Msg += "...使能成功";
openretry = 0;
return true;
}
else
Msg += "...尝试使能失败";
Thread.Sleep(1000);
if (openretry < 5)
Msg = "伺服连接中...";
}
return false;
}
/// <summary>
/// 打开所有轴
/// </summary>
/// <returns></returns>
private bool OpenAxis(out string msg)
{
msg = "";
//判断轴是否正常
string portName = Config.DeviceName;
short slvAddr = Config.GetAxisValue();
if (AxisManager.IsServeoOn(portName, slvAddr))
{
LogUtil.info(AxisName + "成功打开");
}
else
{
//清理报警,再重新打开一次
LogUtil.info(AxisName + "第一次打开失败,先清理一下报警,再重新打开一次");
AxisManager.AlarmClear(portName, slvAddr);
System.Threading.Thread.Sleep(1000);
AxisManager.ServoOn(portName, slvAddr);
System.Threading.Thread.Sleep(100);
if (AxisManager.IsServeoOn(portName, slvAddr))
{
LogUtil.info(AxisName + "清理报警后重新打卡轴成功:" + Config.Explain);
}
else
{
AxisManager.ServoOff(portName, slvAddr);
msg = "打开轴" + Config.Explain + "失败 ";
LogUtil.info(AxisName + msg);
return false;
}
}
return true;
}
public void ServoOff()
{
LogUtil.info("ServoOff【" + AxisName + "】");
AxisManager.ServoOff(Config.DeviceName, Config.GetAxisValue());
}
public bool HasHome() {
return AxisManager.GetHomeEndStatus(Config.DeviceName, (short)Config.GetAxisValue()) == 3;
}
public void HomeMove(MoveInfo MoveInfo,bool force=true)
{
if (!force && AxisManager.GetHomeEndStatus(Config.DeviceName, (short)Config.GetAxisValue()) == 3)
{
LogUtil.info(AxisName + " 有原点信号,无需回原");
return;
}
Config.TargetPosition = 0;
MoveInfo.log(AxisName + "speed[" + Config.HomeHighSpeed + "]开始原点返回");
MoveInfo.WaitList.Add(WaitResultInfo.WaitAxis(Config, true));
var HomeLowSpeed = Config.HomeLowSpeed > 0 ? Config.HomeLowSpeed : Config.HomeHighSpeed / 10;
var HomeAddSpeed = Config.HomeAddSpeed > 0 ? Config.HomeAddSpeed : Config.HomeHighSpeed * 5;
AxisManager.HomeMove(Config.DeviceName, (short)Config.GetAxisValue(), Config.HomeHighSpeed, HomeLowSpeed, HomeAddSpeed);
}
/// <summary>
/// 松下伺服电机运动
/// </summary>
public void AbsMove(MoveInfo MoveInfo, int targetPosition, int targetSpeed)
{
if (IsInPosition(targetPosition))
{
LogUtil.info(AxisName + $" 已在目标点:{targetPosition}");
return;
}
if (ForceSafeCheck && !IsSafe(targetPosition,out string msg))
{
//Msg.add(msg, MsgLevel.alarm);
RobotManage.UserPause(msg);
return;
}
if (MoveInfo == null)
{
AbsMove(targetPosition, targetSpeed);
}
else
{
MoveInfo.WaitList.Add(WaitResultInfo.WaitAxis(Config, targetPosition, targetSpeed));
Config.TargetPosition = targetPosition;
var AddSpeed = Config.AddSpeed > 0 ? Config.AddSpeed : targetSpeed * 4;
var DelSpeed = Config.DelSpeed > 0 ? Config.DelSpeed : targetSpeed * 4;
AxisManager.AbsMove(Config.DeviceName, Config.GetAxisValue(), targetPosition, targetSpeed, AddSpeed, DelSpeed);// Config.AddSpeed, Config.DelSpeed);
}
}
public void SpeedMove(int targetSpeed)
{
AxisManager.SpeedMove(Config.DeviceName, Config.GetAxisValue(), targetSpeed, targetSpeed * 4, targetSpeed * 4);
}
/// <summary>
/// 判断AC伺服电机轴是否运动完成
/// </summary>
public static bool ACAxisMoveIsEnd(MoveInfo MoveInfo, ConfigMoveAxis axis, int targetPosition, int targetSpeed, out string msg)
{
msg = "";
string deviceName = axis.DeviceName;
short axisNo = axis.GetAxisValue();
bool isOk = AxisManager.GetBusyStatus(deviceName, axisNo).Equals(0);
int outCount = AxisManager.GetActualtPosition(deviceName, axisNo);
int errorCount = Math.Abs(outCount - targetPosition);
if (isOk)
{
if (errorCount <= axis.CanErrorCountMax)
{
return true;
}
//判断是否需要重新运动
if (MoveInfo.CanWhileCount > 0)
{
string clearMsg = "";
//判断轴是否报警
if (MoveInfo.CanWhileCount <= 3)
{
int isAlarm = AxisManager.GetAlarmStatus(deviceName, axisNo);
if (isAlarm.Equals(1))
{
clearMsg = "清理报警,";
AxisManager.AlarmClear(deviceName, axisNo);
Thread.Sleep(200);
AxisManager.ServoOn(deviceName, axisNo);
Thread.Sleep(200);
}
}
if (string.IsNullOrEmpty(clearMsg))
{
AxisManager.SuddenStop(axis.DeviceName, axis.GetAxisValue());
Thread.Sleep(100);
}
MoveInfo.error($"{ MoveInfo.Name} {MoveInfo.MoveStep} {axis.DisplayStr}目标位置{targetPosition}当前位置{outCount},误差过大,{clearMsg}重新开始运动,剩余{MoveInfo.CanWhileCount}次");
var AddSpeed = axis.AddSpeed > 0 ? axis.AddSpeed : targetSpeed * 4;
var DelSpeed = axis.DelSpeed > 0 ? axis.DelSpeed : targetSpeed * 4;
AxisManager.AbsMove(axis.DeviceName, axis.GetAxisValue(), targetPosition, targetSpeed, AddSpeed, DelSpeed);// , axis.AddSpeed, axis.DelSpeed);
MoveInfo.CanWhileCount--;
Thread.Sleep(200);
}
else
{
msg = $"{MoveInfo.Name} {MoveInfo.MoveStep} {axis.DisplayStr},目标位置{targetPosition},当前位置{outCount},误差过大,需要报警";
MoveInfo.error(msg);
}
}
return false;
}
public static bool HomeMoveIsEnd(MoveInfo MoveInfo, ConfigMoveAxis axis, out string msg)
{
msg = "";
if (AxisManager.IsHomeMoveEnd(axis.DeviceName, axis.GetAxisValue()))// || AxisManager.AxisStsINP(axis.DeviceName, axis.GetAxisValue()).Equals(1))
{
//原点完成并且位置=0
int outCount = AxisManager.GetActualtPosition(axis.DeviceName, axis.GetAxisValue());
int errorCount = Math.Abs(outCount);
if (errorCount <= axis.CanErrorCountMax)
{
return true;
}
//判断是否需要重新运动
if (MoveInfo.CanWhileCount > 0)
{
MoveInfo.error(MoveInfo.Name + axis.DisplayStr + "收到原点完成信号,当前位置[" + outCount + "],重新回原点,剩余[" + MoveInfo.CanWhileCount + "]次");
//LogUtil.error( StoreName + moveAxis.DisplayStr + "重新回原点");
AxisManager.HomeMove(axis.DeviceName, axis.GetAxisValue(), axis.HomeHighSpeed, axis.HomeLowSpeed, axis.HomeAddSpeed);
MoveInfo.CanWhileCount--;
}
else
{
msg = MoveInfo.Name + " " + MoveInfo.MoveStep + axis.DisplayStr + ",收到原点完成信号,当前位置[" + outCount + "],误差过大,需要报警";
MoveInfo.error(msg);
}
}
return false;
}
public int GetAclPosition()
{
int p = AxisManager.GetActualtPosition(Config.DeviceName, Config.GetAxisValue());
return p;
}
public bool IsInPosition(int targetP)
{
int currp = GetAclPosition();
int chaz = targetP - currp;
if (Math.Abs(chaz) < Config.CanErrorCountMax)
{
return true;
}
return false;
}
/// <summary>
/// 绝对运动至点,不等待结果
/// </summary>
private void AbsMove(int targetPos, int targetSpeed)
{
if (targetPos.Equals(-1))
{
return;
}
LastPosition = -1;
//if (targetSpeed <= 0)//targetSpeed > Config.TargetSpeed ||
//{
// targetSpeed = Config.TargetSpeed;
//}
//小于1,表示是目标速度的百分比
if (targetSpeed <= 1)
{
targetSpeed = Config.TargetSpeed * targetSpeed;
}
var AddSpeed = Config.AddSpeed > 0 ? Config.AddSpeed : targetSpeed * 4;
var DelSpeed = Config.DelSpeed > 0 ? Config.DelSpeed : targetSpeed * 4;
AxisManager.AbsMove(Config.DeviceName, Config.GetAxisValue(), targetPos, targetSpeed, AddSpeed, DelSpeed);// , Config.AddSpeed, Config.DelSpeed);
}
/// <summary>
/// 绝对运动至点,不等待结果
/// </summary>
public void RelMove(int targetPos, double targetSpeed)
{
if (targetPos.Equals(-1))
{
return;
}
LastPosition = -1;
//if (targetSpeed <= 0)//targetSpeed > Config.TargetSpeed ||
//{
// targetSpeed = Config.TargetSpeed;
//}
//小于1,表示是目标速度的百分比
if (targetSpeed <= 1)
{
targetSpeed = Config.TargetSpeed * targetSpeed;
}
AxisManager.RelMove(Config.DeviceName, Config.GetAxisValue(), targetPos, (int)targetSpeed, (int)targetSpeed * 4, (int)targetSpeed * 4);// , Config.AddSpeed, Config.DelSpeed);
}
public bool IsBusy {
get => AxisManager.GetBusyStatus(Config.DeviceName, Config.GetAxisValue()) == 1;
}
public bool IsServeoOn
{
get => AxisManager.IsServeoOn(Config.DeviceName, Config.GetAxisValue());
}
public void SuddenStop()
{
AxisManager.SuddenStop(Config.DeviceName, Config.GetAxisValue());
}
/// <summary>
/// 判断轴运动是否安全
/// </summary>
/// <returns></returns>
public bool IsSafe(int Targetpos,out string msg) {
msg = "";
if (interference == null)
return true;
bool result;
var from = GetAclPosition();
(result,msg) = interference.Invoke(from, Targetpos);
if (result) {
LogUtil.info(AxisName + $" 干涉:{msg},From:{from},To:{Targetpos}");
}
return !result;
}
#region 匀速上升处理
private System.Timers.Timer axisCheckTimer = null;
internal string TargetIoType = "";
internal IO_VALUE TargetIoValue = IO_VALUE.HIGH;
public bool BatchAxisStartCheck(string targetIo, IO_VALUE value = IO_VALUE.HIGH)
{
if (String.IsNullOrEmpty(targetIo))
{
targetIo = "";
}
if (axisCheckTimer == null)
{
axisCheckTimer = new System.Timers.Timer();
axisCheckTimer.AutoReset = true;
axisCheckTimer.Interval += 30;
axisCheckTimer.Elapsed += CheckTimer_Elapsed;
axisCheckTimer.Enabled = false;
}
TargetIoValue = value;
TargetIoType = targetIo;
IsInProcess = false;
axisCheckTimer.Start();
return true;
}
public bool StopAxisCheckMove()
{
if (!(axisCheckTimer == null))
{
axisCheckTimer.Stop();
}
return true;
}
private bool IsInProcess = false;
private void CheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (IsInProcess ) { return; }
try
{
IsInProcess = true;
if (IOManager.IOValue(TargetIoType, DeviceGroupName).Equals(TargetIoValue))
{
SuddenStop();
StopAxisCheckMove();
LogUtil.info(AxisName + "上料轴,检测到 " + TargetIoType + "=" + TargetIoValue + ",停止运动");
}
}
catch (Exception ex)
{
LogUtil.error("CheckTimer_Elapsed出错:", ex);
}
finally
{
IsInProcess = false;
}
}
#endregion
public static bool RunMultiAxis(bool isCheck, out string msg, List<AxisBean> axisArray)
{
msg = "";
foreach (AxisBean axis in axisArray)
{
bool result = axis.Open(false, out msg);
if (!result)
{
LogUtil.error(msg);
return false;
}
}
Thread.Sleep(50);
return true;
}
public static void StopMultiAxis(List<AxisBean> axisArray)
{
foreach (AxisBean axis in axisArray)
{
axis.SuddenStop();
Thread.Sleep(10);
}
}
public static void CloseMultiAxis(List<AxisBean> axisArray)
{
foreach (AxisBean axis in axisArray)
{
axis.ServoOff();
Thread.Sleep(10);
}
}
public static bool ClearMultiAlarm(out string msg, List<AxisBean> axisArray)
{
msg = "";
bool isok = true;
foreach (AxisBean axis in axisArray)
{
string portName = axis.Config.DeviceName;
short slvAddr = axis.Config.GetAxisValue();
if (AxisManager.GetAlarmStatus(portName, slvAddr) != 0)
{
AxisManager.AlarmClear(portName, slvAddr);
Thread.Sleep(200);
if (AxisManager.GetAlarmStatus(portName, slvAddr) != 0)
{
msg += string.Format("轴(0)存在报警,清除失败", axis.Config.Explain);
isok = false;
}
else
msg += string.Format("轴(0)存在报警,清除成功", axis.Config.Explain);
}
}
Thread.Sleep(50);
return isok;
}
}
}