BoxAutoPoint.cs
12.8 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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class BoxAutoPoint
{
private StoreMoveStep CurrStep;
private BoxBean box = null;
private System.Timers.Timer toolTimer = new System.Timers.Timer();
public PToolInfo paramInfo = new PToolInfo();
private string LogName = "";
public BoxAutoPoint(BoxBean box)
{
toolTimer.Enabled = false;
toolTimer.Interval = 500;
toolTimer.AutoReset = true;
toolTimer.Elapsed += ToolTimer_Elapsed;
this.box = box;
CurrStep = StoreMoveStep.Wait;
LogName = box.Name + "校准点位 ";
}
public bool IsStop()
{
return !toolTimer.Enabled;
}
public void Start()
{
CurrStep=(StoreMoveStep.AP_01_InoutHome);
LogUtil.info(LogName + CurrStep + "第" + paramInfo.CurrIndex + "列,旋转轴位置[" + paramInfo.GetCurrMiddleP() + "] :进出轴先原点返回");
ConfigMoveAxis axis = box.Config.InOut_Axis;
AxisManager.instance.HomeMove(axis.DeviceName, (short)axis.GetAxisValue(), axis.HomeHighSpeed, axis.HomeLowSpeed, axis.HomeAddSpeed);
toolTimer.Interval = 500;
toolTimer.Start();
}
private bool isInProcesss = false;
private bool HomeIsEnd(ConfigMoveAxis moveAxis)
{
if (AxisManager.instance.IsHomeMoveEnd(moveAxis.DeviceName, moveAxis.GetAxisValue()))
{
//原点完成并且位置=0
int outCount = AxisManager.instance.GetActualtPosition(moveAxis.DeviceName, moveAxis.GetAxisValue());
int errorCount = Math.Abs(outCount);
if (errorCount <= moveAxis.CanErrorCountMax)
{
return true;
}
}
return false;
}
private bool AbsMoveIsEnd(ConfigMoveAxis moveAxis, int targetP)
{
bool countError = false;
return AxisManager.instance.AbsMoveIsEnd(moveAxis.DeviceName, moveAxis.GetAxisValue(), targetP, moveAxis.CanErrorCountMax, out countError);
}
private void ToolTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
TimeSpan span = DateTime.Now - paramInfo.LastTime;
if (isInProcesss && span.TotalSeconds < 1)
{
return;
}
isInProcesss = true;
int currIndex = paramInfo.CurrIndex;
if (CurrStep.Equals(StoreMoveStep.AP_01_InoutHome))
{
if (HomeIsEnd(box.Config.InOut_Axis))
{
CurrStep=(StoreMoveStep.AP_02_UpdownHome);
LogUtil.info(LogName + CurrStep + " "+ currIndex + ":升降轴原点返回");
ConfigMoveAxis axis = box.Config.UpDown_Axis;
AxisManager.instance.HomeMove(axis.DeviceName, (short)axis.GetAxisValue(), axis.HomeHighSpeed, axis.HomeLowSpeed, axis.HomeAddSpeed);
}
}
else if (CurrStep.Equals(StoreMoveStep.AP_02_UpdownHome))
{
if (HomeIsEnd(box.Config.UpDown_Axis))
{
CurrStep=(StoreMoveStep.AP_03_MiddleMove);
ConfigMoveAxis axis = box.Config.Middle_Axis;
int p = paramInfo.GetCurrMiddleP();
LogUtil.info(LogName + CurrStep + " " + currIndex + ":旋转轴移动到目标位置:" + p);
AxisManager.instance.AbsMove(axis.DeviceName, (short)axis.GetAxisValue(), p, box.Config.MiddleAxis_P1_Speed, axis.AddSpeed, axis.DelSpeed);
}
}
else if (CurrStep.Equals(StoreMoveStep.AP_03_MiddleMove))
{
int p = paramInfo.GetCurrMiddleP();
if (AbsMoveIsEnd(box.Config.Middle_Axis, p))
{
CurrStep=(StoreMoveStep.AP_04_InoutToP);
ConfigMoveAxis iaxis = box.Config.InOut_Axis;
LogUtil.info(LogName + CurrStep + " " + currIndex + ":进出轴移动到目标位置:" + paramInfo.InoutTargetPosition);
AxisManager.instance.AbsMove(iaxis.DeviceName, (short)iaxis.GetAxisValue(), paramInfo.InoutTargetPosition, box.Config.InOutAxis_P1_Speed, iaxis.AddSpeed, iaxis.DelSpeed);
}
}
else if (CurrStep.Equals(StoreMoveStep.AP_04_InoutToP))
{
if (AbsMoveIsEnd(box.Config.InOut_Axis, paramInfo.InoutTargetPosition))
{
CurrStep=(StoreMoveStep.AP_05_UpdownMove);
ConfigMoveAxis axis = box.Config.UpDown_Axis;
LogUtil.info(LogName + CurrStep + " " + currIndex + ":升降轴移动到目标位置:" + paramInfo.UpdownTargetPosition);
AxisManager.instance.AbsMove(axis.DeviceName, (short)axis.GetAxisValue(), paramInfo.UpdownTargetPosition, paramInfo.UpdownSpeed, axis.AddSpeed, axis.DelSpeed);
toolTimer.Interval = 50;
}
}
else if (CurrStep.Equals(StoreMoveStep.AP_05_UpdownMove))
{
try
{
ConfigMoveAxis axis = box.Config.UpDown_Axis;
int moveS = AxisManager.instance.GetBusyStatus(axis.DeviceName, axis.GetAxisValue());
if (moveS.Equals(1))
{
IO_VALUE currValue = box.IOValue(IO_Type.CheckPos);
TimeSpan checkSpan = DateTime.Now - paramInfo.LastGetPTime;
//间隔两秒以上才有效
if (paramInfo.LastValue.Equals(IO_VALUE.LOW) && currValue.Equals(IO_VALUE.HIGH) && checkSpan.TotalSeconds > 2)
{
int currPos = AxisManager.instance.GetActualtPosition(axis.DeviceName, axis.GetAxisValue());
paramInfo.PositionList.Add(currPos);
int num = paramInfo.PositionList.Count;
int preValue = 0;
if (paramInfo.PositionList.Count > 1)
{
preValue = paramInfo.PositionList[num - 2];
}
LogUtil.info(LogName + CurrStep + " " + currIndex + "【" + num + "】【" + currPos + "】【" + Math.Abs(currPos - preValue) + "】");
paramInfo.LastValue = IO_VALUE.HIGH;
paramInfo.LastGetPTime = DateTime.Now;
}
paramInfo.LastValue = currValue;
}
else
{
//TODO 下一个
SaveAndNext(true);
}
}
catch (Exception ex)
{
LogUtil.error(LogName + "ToolTimer_Elapsed" + ex.ToString());
}
}
isInProcesss = false;
}
private void SaveAndNext(bool needNext = true)
{
toolTimer.Stop();
if (paramInfo.PositionList.Count > 0)
{
LogUtil.info(LogName + "当前第"+paramInfo.CurrIndex+"列运动结束,停止定时器,记录数据");
List<string> strList = new List<string>();
strList.Add("编号,旋转轴位置,标准位置,库位差值,升降轴库位出料前点P5,升降轴库位出料缓冲点P6,升降轴库位入料前点P3,升降轴库位入料缓冲点P4");
int index = 1;
foreach (int p in paramInfo.PositionList)
{
int P3 = p + paramInfo.P3Offset;
int P4 = p + paramInfo.P4Offset;
int P5 = p + paramInfo.P5Offset;
int P6 = p + paramInfo.P6Offset;
int cha = 0;
if (index > 1)
{
cha = p - paramInfo.PositionList[index - 2];
}
string spilt = ",";
string resultstr = index + spilt + paramInfo.GetCurrMiddleP()+ spilt + p + spilt + cha+ spilt + P5 + spilt + P6 + spilt + P3 + spilt + P4 + "";
strList.Add(resultstr);
index++;
}
string fileName = "position_" + paramInfo.GetCurrMiddleP()+ ".csv";
string filePath = paramInfo.FileTargetPath + fileName;
try
{
File.WriteAllLines(filePath, strList.ToArray(), Encoding.UTF8);
LogUtil.error(LogName + "保存位置到文件【" + filePath + "】成功");
}
catch (Exception ex)
{
LogUtil.error(LogName + "保存位置到文件【" + filePath + "】出错:" + ex.ToString());
}
}
else
{
LogUtil.info(LogName + "当前第" + paramInfo.CurrIndex + "列运动结束,停止定时器,暂无点位需要保存");
}
if (needNext)
{
int nextIndex = paramInfo.CurrIndex + 1;
if (nextIndex < paramInfo.MiddlePositionList.Count)
{
LogUtil.info(LogName + "开始下一列点位校准");
paramInfo.PositionList = new List<int>();
paramInfo.CurrIndex = nextIndex;
Start();
}
else
{
LogUtil.info(LogName + "已经是最后一列,位置校准结束");
}
}
//workMoveStatus(true);
}
public void StopMove()
{
SaveAndNext(false);
ACServerManager.instance.SuddenStop(box.Config.UpDown_Axis.DeviceName, box.Config.UpDown_Axis.GetAxisValue());
ACServerManager.instance.SuddenStop(box.Config.InOut_Axis.DeviceName, box.Config.InOut_Axis.GetAxisValue());
ACServerManager.instance.SuddenStop(box.Config.Middle_Axis.DeviceName, box.Config.Middle_Axis.GetAxisValue());
}
public string CurrStr()
{
string str= "位置校准中:"+CurrStep + "\r\n" +
"当前第" + (paramInfo.CurrIndex + 1) + "/" + paramInfo.MiddlePositionList.Count + "列,已校准库位" + paramInfo.PositionList.Count + "个";
return str;
}
}
public class PToolInfo
{
public int P3Offset = 0;
public int P4Offset = 0;
public int P5Offset = 0;
public int P6Offset = 0;
public int UpdownSpeed = 20;
/// <summary>
/// 升降轴开始位置
/// </summary>
public int UpdownStartPosition = 0;
/// <summary>
/// 升降轴目标位置
/// </summary>
public int UpdownTargetPosition = 0;
/// <summary>
/// 进出轴目标位置
/// </summary>
public int InoutTargetPosition = 0;
/// <summary>
/// 旋转轴位置
/// </summary>
public List<int> MiddlePositionList = new List<int>();
/// <summary>
/// 当前列
/// </summary>
public int CurrIndex = 0;
public int GetCurrMiddleP()
{
return MiddlePositionList[CurrIndex];
}
public string FileTargetPath = "";
public List<int> PositionList = new List<int>();
public IO_VALUE LastValue = IO_VALUE.LOW;
public DateTime LastGetPTime = DateTime.Now;
public DateTime LastTime = DateTime.Now;
public string ParamStr()
{
string msgStr = "请确认以下对点位参数,点击确定按钮开始自动校准点位:\r\n";
msgStr += "\t进出轴前进位置:" + InoutTargetPosition + "\r\n";
msgStr += "\t升降轴目标位置:" + UpdownTargetPosition + ",速度:"+UpdownSpeed+"\r\n";
msgStr += "\t旋转轴位置列表:";
foreach (int mP in MiddlePositionList)
{
msgStr += mP + ",";
}
if (msgStr.EndsWith(","))
{
msgStr = msgStr.Substring(0, msgStr.Length - 1);
}
msgStr += "\r\n";
msgStr += "\t点位偏移量:P3["+P3Offset+"],P4["+P4Offset+"],P5["+P5Offset+"],P6["+P6Offset+"]\r\n";
msgStr += "\t文件保存位置:" + FileTargetPath;
return msgStr;
}
}
}