AxisBean.cs
10.7 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
using DeviceLib;
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;
public static int TimeoutInterval = 500;
public int LastPosition = 0;
public string AxisName;
public AxisBean(ConfigMoveAxis axisConfig, string deviceName)
{
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();
//打开所有轴
int bot = ConfigAppSettings.GetIntValue(Setting_Init.ACBaudRate);
ACServerManager.OpenPort(Config.DeviceName, bot);
Thread.Sleep(50);
//初始化
if (!IsIntSlvBlock)
{
ACServerManager.InitSlvAddr(portName, slvAddr, Config.TargetSpeed, Config.AddSpeed, Config.DelSpeed);
Thread.Sleep(100);
}
ACServerManager.AlarmClear(portName, slvAddr);
Thread.Sleep(100);
ACServerManager.ServoOn(portName, slvAddr);
//打开所有轴
if (isCheck)
{
Thread.Sleep(1000);
if (!AxisIsOpen(out Msg))
{
return false;
}
}
IsIntSlvBlock = true;
return true;
}
public bool AxisIsOpen(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(300);
if (ACServerManager.ServerOnStatus(portName, slvAddr))
{
LogUtil.info(AxisName + "重新打开成功");
}
else
{
ACServerManager.ServoOff(portName, slvAddr);
msg = AxisName+ "打开失败 ";
LogUtil.info(AxisName + msg);
return false;
}
}
return true;
}
public void ServoOff()
{
LogUtil.info("ServoOff【" + AxisName + "】");
ACServerManager.ServoOff(Config.DeviceName, Config.GetAxisValue());
}
public void HomeMove(RobotMoveInfo MoveInfo)
{
Config.TargetPosition = 0;
LogUtil.info(AxisName + "speed[" + Config.TargetSpeed + "]开始原点返回");
MoveInfo.WaitList.Add(WaitResultInfo.WaitAxis(Config, true));
ACServerManager.HomeMove(Config.DeviceName, (short)Config.GetAxisValue(), Config.HomeHighSpeed);
}
public void AbsMove(RobotMoveInfo MoveInfo, int targetPosition, int targetSpeed)
{
if (MoveInfo == null)
{
AbsMove(targetPosition, targetSpeed);
}
else
{
MoveInfo.WaitList.Add(WaitResultInfo.WaitAxis(Config, targetPosition, targetSpeed));
Config.TargetPosition = targetPosition;
ACServerManager.AbsMove(Config.DeviceName, Config.GetAxisValue(), targetPosition, targetSpeed);
}
}
public static bool ACAxisMoveIsEnd(RobotMoveInfo 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(MoveInfo.Name + axis.DisplayStr + "目标位置[" + targetPosition + "]当前位置[" + outCount +
"],误差过大,重新开始运动,剩余[" + MoveInfo.CanWhileCount + "]次");
ACServerManager.SuddenStop(axis.DeviceName, axis.GetAxisValue());
ACServerManager.AbsMove(axis.DeviceName, axis.GetAxisValue(), targetPosition, targetSpeed);
MoveInfo.CanWhileCount--;
}
else
{
msg = " " + MoveInfo.MoveStep + MoveInfo.Name + axis.DisplayStr + ",目标位置[" + targetPosition + "]当前位置[" + outCount
+ "],误差过大,需要报警";
LogUtil.error(msg, MoveInfo.ErrorLogType);
}
}
return false;
}
public static bool HomeMoveIsEnd(RobotMoveInfo 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(MoveInfo.Name + axis.DisplayStr + "收到原点完成信号,当前位置[" + outCount + "],重新回原点,剩余[" + MoveInfo.CanWhileCount + "]次");
//LogUtil.error( StoreName + moveAxis.DisplayStr + "重新回原点");
ACServerManager.HomeMove(axis.DeviceName, axis.GetAxisValue(), axis.HomeHighSpeed, true);
MoveInfo.CanWhileCount--;
}
else
{
msg = MoveInfo.Name + " " + MoveInfo.MoveStep + axis.DisplayStr + ",收到原点完成信号,当前位置[" + outCount + "],误差过大,需要报警";
LogUtil.error(msg);
}
}
return false;
}
public int GetAclPosition()
{
int p = ACServerManager.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;
}
private 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(bool needCheck = false )
{
if (needCheck)
{
if(ACServerManager.GetBusyStatus(Config.DeviceName, Config.GetAxisValue()).Equals(0))
{
return;
}
}
ACServerManager.SuddenStop(Config.DeviceName, Config.GetAxisValue());
}
#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 AxisStopCheckMove()
{
if (!(axisCheckTimer == null))
{
axisCheckTimer.Stop();
}
return true;
}
private bool IsInProcess = false;
private DateTime lastOkTime = DateTime.Now;
private void CheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
TimeSpan pan = DateTime.Now - lastOkTime;
if (IsInProcess && pan.TotalMilliseconds < 100) { return; }
try
{
IsInProcess = true;
lastOkTime = DateTime.Now;
if (IOManager.IOValue(TargetIoType, 0).Equals(TargetIoValue))
{
AxisStopCheckMove();
LogUtil.info(AxisName + "上料轴,检测到 " + TargetIoType + "=" + TargetIoValue + ",停止运动,AclPosition=" + GetAclPosition().ToString());
SuddenStop();
}
}
catch (Exception ex)
{
LogUtil.error("CheckTimer_Elapsed出错:", ex);
}
finally
{
IsInProcess = false;
}
}
#endregion
}
}