AxisBean.cs
14.5 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
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 OnlineStore.DeviceLibrary
{
public class AxisBean
{
public ConfigMoveAxis Config =null;
////是否在原点返回
//private bool homeRunning = false;
////是否在移动
//private bool moveRunning = false;
////是否已连接
//public bool isConnect = false;
public static int TimeoutInterval = 500;
/// <summary>
/// 正常工作过程中判断位置是否到达时使用
/// </summary>
public int LastPosition = 0;
public string AxisName;
public AxisBean(ConfigMoveAxis axisConfig)
{
this.Config = axisConfig;
AxisName = Config.Explain + "[" + Config.DeviceName + "-" + Config.GetAxisValue() + "]";
}
private bool IsIntSlvBlock = false;
public bool Open(bool isCheck,out string Msg)
{
Msg = "";
string portName = Config.DeviceName;
int slvAddr = Config.GetAxisValue();
//打开所有轴
ACServerManager.OpenPort(Config.DeviceName);
Thread.Sleep(50);
//初始化
if (!IsIntSlvBlock)
{
ACServerManager.InitSlvAddr(portName, slvAddr, Config.TargetSpeed, Config.AddSpeed, Config.DelSpeed);
Thread.Sleep(100);
}
ACServerManager.AlarmClear(portName, slvAddr);
Thread.Sleep(50);
ACServerManager.ServoOn(portName, slvAddr);
Thread.Sleep(1000);
//打开所有轴
if (isCheck)
{
if (!OpenAxis(out Msg))
{
return false;
}
}
IsIntSlvBlock = true;
return true;
}
/// <summary>
/// 打开所有轴
/// </summary>
/// <returns></returns>
private bool OpenAxis( out string msg)
{
msg = "";
//判断轴是否正常
string portName = Config.DeviceName;
int slvAddr = Config.GetAxisValue();
if (ACServerManager.ServerOnStatus(portName, slvAddr))
{
LogUtil.info( AxisName+ "成功打开" );
}
else
{
//清理报警,再重新打开一次
LogUtil.info(AxisName+ "第一次打开失败,先清理一下报警,再重新打开一次");
ACServerManager.AlarmClear(portName, slvAddr);
System.Threading.Thread.Sleep(1200);
ACServerManager.ServoOn(portName, slvAddr);
System.Threading.Thread.Sleep(100);
if (ACServerManager.ServerOnStatus(portName, slvAddr))
{
LogUtil.info(AxisName + "清理报警后重新打卡轴成功:" + Config.Explain);
}
else
{
ACServerManager.ServoOff(portName, slvAddr);
msg= "打开轴" + Config.Explain + "失败 ";
LogUtil.info(AxisName + msg);
return false;
}
}
return true;
}
public void Close()
{
LogUtil.info("ServoOff【" + AxisName + "】");
ACServerManager.ServoOff(Config.DeviceName, Config.GetAxisValue());
}
public void HomeMove(LineMoveInfo MoveInfo)
{
Config.TargetPosition = 0;
LogUtil.info(Config.DisplayStr + "speed[" + Config.TargetSpeed + "]开始原点返回");
MoveInfo.WaitList.Add(WaitResultInfo.WaitAxis(Config, true));
ACServerManager.HomeMove(Config.DeviceName, (short)Config.GetAxisValue(), Config.HomeHighSpeed);
}
/// <summary>
/// 松下伺服电机运动
/// </summary>
public void AbsMove(LineMoveInfo MoveInfo, int targetPosition, int targetSpeed)
{
MoveInfo.WaitList.Add(WaitResultInfo.WaitAxis(Config, targetPosition, targetSpeed));
Config.TargetPosition = targetPosition;
ACServerManager.AbsMove(Config.DeviceName, Config.GetAxisValue(), targetPosition, targetSpeed);
}
/// <summary>
/// 判断AC伺服电机轴是否运动完成
/// </summary>
public static bool ACAxisMoveIsEnd(LineMoveInfo MoveInfo, ConfigMoveAxis axis,int targetPosition, int targetSpeed, out string msg)
{
msg = "";
string deviceName = axis.DeviceName;
short axisNo = axis.GetAxisValue();
bool isOk = ACServerManager.GetBusyStatus(deviceName, axisNo).Equals(0);
int outCount = ACServerManager.GetActualtPosition(deviceName, axisNo);
int errorCount = Math.Abs(outCount - targetPosition);
if (isOk)
{
if (errorCount <= axis.CanErrorCountMax)
{
return true;
}
//判断是否需要重新运动
if (MoveInfo.CanWhileCount > 0)
{
LogUtil.error(axis.DisplayStr + "目标位置[" + targetPosition + "]当前位置[" + outCount +
"],误差过大,重新开始运动,剩余[" + MoveInfo.CanWhileCount + "]次");
ACServerManager.AbsMove(axis.DeviceName, axis.GetAxisValue(), targetPosition, targetSpeed);
MoveInfo.CanWhileCount--;
}
else
{
msg = " storeMoveStep=" + MoveInfo.MoveStep + axis.DisplayStr + "目标位置[" + targetPosition + "]当前位置[" + outCount
+ "],误差过大,需要报警";
LogUtil.error(msg);
}
}
return false;
}
public static bool HomeMoveIsEnd(LineMoveInfo MoveInfo, ConfigMoveAxis axis, out string msg)
{
msg = "";
if (ACServerManager.IsHomeMoveEnd(axis.DeviceName, axis.GetAxisValue()))
{
//原点完成并且位置=0
int outCount = ACServerManager.GetActualtPosition(axis.DeviceName, axis.GetAxisValue());
int errorCount = Math.Abs(outCount);
if (errorCount <= axis.CanErrorCountMax)
{
return true;
}
//判断是否需要重新运动
if (MoveInfo.CanWhileCount > 0)
{
LogUtil.error(axis.DisplayStr + "收到原点完成信号,当前位置[" + outCount + "],重新回原点,剩余[" + MoveInfo.CanWhileCount + "]次");
//LogUtil.error( StoreName + moveAxis.DisplayStr + "重新回原点");
ACServerManager.HomeMove(axis.DeviceName, axis.GetAxisValue(), axis.HomeHighSpeed);
MoveInfo.CanWhileCount--;
}
else
{
msg = " storeMoveStep=" + MoveInfo.MoveStep + axis.DisplayStr + "收到原点完成信号,当前位置[" + outCount + "],误差过大,需要报警";
LogUtil.error(msg);
}
}
return false;
}
public int GetAclPosition()
{
int p = ACServerManager.GetActualtPosition(Config);
return p;
}
/// <summary>
/// 绝对运动至点,不等待结果
/// </summary>
public void AbsMove(int targetPos, double targetSpeed)
{
if (targetPos.Equals(-1))
{
return ;
}
LastPosition = -1;
if (targetSpeed > Config.TargetSpeed || targetSpeed <= 0)
{
targetSpeed = Config.TargetSpeed;
}
//小于1,表示是目标速度的百分比
else if (targetSpeed <= 1)
{
targetSpeed = Config.TargetSpeed * targetSpeed;
}
ACServerManager.AbsMove(Config.DeviceName, Config.GetAxisValue(), targetPos, (int)targetSpeed);
}
public void SuddenStop()
{
ACServerManager.SuddenStop(Config.DeviceName, Config.GetAxisValue());
}
///// <summary>
///// 移动到某点,会等到到达位置后结束
///// </summary>
///// <param name="pos"></param>
///// <returns></returns>
//public string AbsMoveAndWait(int pos, double targetSpeed = 0)
//{
// string result = AbsMove(pos, targetSpeed);
// if (!result.Equals(""))
// {
// return result;
// }
// moveRunning = true;
// while (true)
// {
// if (!moveRunning)
// {
// moveRunning = false;
// return AxisName + "用户中止";
// }
// Thread.Sleep(100);
// if (HasAlarm())
// {
// moveRunning = false;
// return AxisName + "伺服报警";
// }
// //判断Buzy及位置是否结束
// if (IsMoveEnd(pos))
// {
// //移动完成
// moveRunning = false;
// return "";
// }
// }
//}
//public bool HasAlarm()
//{
// return ACServerManager.GetAlarmStatus(Config.DeviceName, Config.GetAxisValue()).Equals(1);
//}
///// <summary>
///// 获取位置
///// </summary>
///// <param name="tryTimes">尝试次数</param>
///// <returns></returns>
//public int GetPos(int tryTimes)
//{
// for (int i = 0; i < tryTimes; i++)
// {
// int pos = ACServerManager.GetActualtPosition(Config.DeviceName, Config.GetAxisValue());
// if (pos != -1)
// {
// LastPosition = pos;
// return pos;
// }
// LogUtil.info("获取" + AxisName + "当前位置[" + pos + "]");
// Thread.Sleep(120);
// }
// return -1;
//}
//public bool IsMoveEnd(int targetPos)
//{
// bool isEnd = ACServerManager.GetBusyStatus(Config.DeviceName, Config.GetAxisValue()) == 0;
// return isEnd;
//}
//public void SuddenStop()
//{
// if (homeRunning)
// {
// homeRunning = false;
// }
// if (moveRunning)
// {
// moveRunning = false;
// }
// if (isConnect)
// {
// ACServerManager.SuddenStop(Config.DeviceName, Config.GetAxisValue());
// }
//}
//private void Stop()
//{
// if (homeRunning)
// {
// homeRunning = false;
// }
// if (moveRunning)
// {
// moveRunning = false;
// }
// if (isConnect)
// {
// //ACServerManager.Stop(Config.DeviceName, axisNo);
// }
//}
//public bool IsInPosition(int positionX, int chaValue)
//{
// if (moveRunning)
// {
// return false;
// }
// if (positionX.Equals(-1))
// {
// return true;
// }
// if (!LastPosition.Equals(-1))
// {
// int cha1 = positionX - LastPosition;
// if (Math.Abs(cha1) <= chaValue)
// {
// return true;
// }
// }
// LastPosition = GetPos(2);
// int XCha = positionX - LastPosition;
// if (Math.Abs(XCha) <= chaValue)
// {
// return true;
// }
// return false;
//}
///// <summary>
///// 异步原点返回
///// </summary>
///// <param name="timeout">超时时间</param>
///// <returns></returns>
//public string Home(TimeSpan timeout)
//{
// if (!isConnect)
// {
// return AxisName + "未连接";
// }
// if (moveRunning)
// {
// return AxisName + "正在运动";
// }
// homeRunning = true;
// string result = "";
// ACServerManager.HomeMove(Config.DeviceName, Config.GetAxisValue(), Config.HomeHighSpeed);
// double outTime = timeout.TotalMilliseconds;
// int time = 0;
// while (true)
// {
// if (!homeRunning)
// {
// result = AxisName + "用户中止";
// break;
// }
// else
// {
// Thread.Sleep(TimeoutInterval);
// if (isHomeEnd())
// {
// //LogUtil.info(Config.DeviceName + "原点返回停止,对比实际位置");
// int currentPos = GetPos(2);
// if (currentPos != -1 && Math.Abs(currentPos) <= Config.CanErrorCountMax)
// {
// LogUtil.info(AxisName + "原点返回完成,当前位置【" + currentPos + "】");
// //原点返回完成
// result = "";
// break;
// };
// }
// if (HasAlarm())
// {
// result = AxisName + "报警";
// break;
// }
// time = time + TimeoutInterval;
// if (time >= outTime)
// {
// result = AxisName + "超时";
// break;
// }
// }
// }
// homeRunning = false;
// return result;
//}
//public bool isHomeEnd()
//{
// bool isEnd = ACServerManager.IsHomeMoveEnd(Config.DeviceName, Config.GetAxisValue());
// return isEnd;
//}
}
}