FrmAutoFindPos.cs
22.4 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
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using eyemLib_Sharp;
using static CodeLibrary.EyemDecode;
namespace OnlineStore.XLRStore
{
internal partial class FrmAutoFindPos : FrmBase
{
internal FrmAutoFindPos()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
boxEquip = StoreManager.XLRStore.boxEquip;
chkBoxCloseCam.Checked = bool.Parse(Common.ConfigAppSettings.GetValue(Setting_Init.CloseCamDetect));
SetState(false);
cmbColor.SelectedIndex = 0;
}
BoxEquip boxEquip;
string posSide = "AA";
private void SetState(bool state)
{
tabControl1.Enabled = state;
chkBoxCloseCam.Enabled = state;
}
/// <summary>
/// 保存参数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
boxEquip.Config.Drawer_Rows = int.Parse(txtDrawerRows.Text);
boxEquip.Config.Drawer_Columns = int.Parse(txtDrawerColumns.Text);
boxEquip.Config.Rows_In_Drawer = int.Parse(txtRowsInDrawer.Text);
boxEquip.Config.Cols_In_Drawer = int.Parse(txtColsInDrawer.Text);
boxEquip.Config.Column_Spacing = int.Parse(txtColumnSpacing.Text);
boxEquip.Config.Row_Spacing = int.Parse(txtRowSpacing.Text);
boxEquip.Config.FindPos_Drawer_X_Error = float.Parse(txtMark_X_err.Text);
boxEquip.Config.FindPos_Drawer_Y_Error = float.Parse(txtMark_Y_err.Text);
boxEquip.Config.Validate_Drawer_X_Error = float.Parse(txtMarkX_err_Validate.Text);
boxEquip.Config.Validate_Drawer_Y_Error = float.Parse(txtMarkY_err_Validate.Text);
boxEquip.autoFindPos.XCoeffOfCoorToPulse = float.Parse(txtCoffX.Text);
boxEquip.autoFindPos.YCoeffOfCoorToPulse = float.Parse(txtCoofY.Text);
if (posSide.Equals("AA"))
{
boxEquip.Config.PixelToPulse_A_X = float.Parse(txtCoffX.Text);
boxEquip.Config.PixelToPulse_A_Y = float.Parse(txtCoofY.Text);
boxEquip.Config.FindPos_Drawer_A_X = float.Parse(txtMark_X.Text);
boxEquip.Config.FindPos_Drawer_A_Y = float.Parse(txtMark_Y.Text);
boxEquip.Config.Validate_Drawer_A_X = float.Parse(txtMarkX_Validate.Text);
boxEquip.Config.Validate_Drawer_A_Y = float.Parse(txtMarkY_Validate.Text);
}
else
{
boxEquip.Config.PixelToPulse_B_X = float.Parse(txtCoffX.Text);
boxEquip.Config.PixelToPulse_B_Y = float.Parse(txtCoofY.Text);
boxEquip.Config.FindPos_Drawer_B_X = float.Parse(txtMark_X.Text);
boxEquip.Config.FindPos_Drawer_B_Y = float.Parse(txtMark_Y.Text);
boxEquip.Config.Validate_Drawer_B_X = float.Parse(txtMarkX_Validate.Text);
boxEquip.Config.Validate_Drawer_B_Y = float.Parse(txtMarkY_Validate.Text);
}
StoreManager.SaveBoxConfig(boxEquip.Config);
}
private void FrmAutoFindPos_Load(object sender, EventArgs e)
{
txtDrawerRows.Text = boxEquip.Config.Drawer_Rows.ToString();
txtDrawerColumns.Text = boxEquip.Config.Drawer_Columns.ToString();
txtRowsInDrawer.Text = boxEquip.Config.Rows_In_Drawer.ToString();
txtColsInDrawer.Text = boxEquip.Config.Cols_In_Drawer.ToString();
txtColumnSpacing.Text = boxEquip.Config.Column_Spacing.ToString();
txtRowSpacing.Text = boxEquip.Config.Row_Spacing.ToString();
txtMark_X_err.Text = boxEquip.Config.FindPos_Drawer_X_Error.ToString();
txtMark_Y_err.Text = boxEquip.Config.FindPos_Drawer_Y_Error.ToString();
txtMarkX_err_Validate.Text = boxEquip.Config.Validate_Drawer_X_Error.ToString();
txtMarkY_err_Validate.Text = boxEquip.Config.Validate_Drawer_Y_Error.ToString();
UpdateData();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
posSide = "AA";
UpdateData();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
posSide = "BB";
UpdateData();
}
private void UpdateData()
{
if (posSide.Equals("AA"))
{
txtMark_X.Text = boxEquip.Config.FindPos_Drawer_A_X.ToString();
txtMark_Y.Text = boxEquip.Config.FindPos_Drawer_A_Y.ToString();
txtMarkX_Validate.Text = boxEquip.Config.Validate_Drawer_A_X.ToString();
txtMarkY_Validate.Text = boxEquip.Config.Validate_Drawer_A_Y.ToString();
txtCoffX.Text = boxEquip.Config.PixelToPulse_A_X.ToString();
txtCoofY.Text = boxEquip.Config.PixelToPulse_A_Y.ToString();
}
else
{
txtMark_X.Text = boxEquip.Config.FindPos_Drawer_B_X.ToString();
txtMark_Y.Text = boxEquip.Config.FindPos_Drawer_B_Y.ToString();
txtMarkX_Validate.Text = boxEquip.Config.Validate_Drawer_B_X.ToString();
txtMarkY_Validate.Text = boxEquip.Config.Validate_Drawer_B_Y.ToString();
txtCoffX.Text = boxEquip.Config.PixelToPulse_B_X.ToString();
txtCoofY.Text = boxEquip.Config.PixelToPulse_B_Y.ToString();
}
groupBox5.Text = string.Format("更新其他抽屉内的库位,数据源为:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
groupBox6.Text = string.Format("当前抽屉:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
groupBox7.Text = string.Format("当前抽屉:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
}
int rows_in_drawer = 0;
int colmns_in_drawer = 0;
bool isBreak = false;
private void button9_Click(object sender, EventArgs e)
{
btnStartVision.Enabled = false;
btnPauseVision.Enabled = true;
timer1.Enabled = true;
timer1.Start();
StringBuilder head2 = new StringBuilder(boxEquip.CID + posSide + txtStartRow.Text.PadLeft(2, '0') + txtStartCol.Text.PadLeft(2, '0') + "0101");
boxEquip.autoFindPosMode = true;
boxEquip.StartFindPos(new InOutParam(new InOutPosInfo("AutoFindPos", head2.ToString())));
}
private void button10_Click(object sender, EventArgs e)
{
boxEquip.autoFindPos.SetParam(int.Parse(txtCurRow.Text), int.Parse(txtCurCol.Text), int.Parse(txtStartRow.Text), int.Parse(txtStartCol.Text), float.Parse(txtCoffX.Text), float.Parse(txtCoofY.Text));
StringBuilder head2 = new StringBuilder(boxEquip.CID + posSide + txtStartRow.Text.PadLeft(2, '0') + txtStartCol.Text.PadLeft(2, '0'));
head2.Append("0101");
LineMoveP lineMoveP = new LineMoveP();
lineMoveP.UpdatePosById(head2.ToString());
boxEquip.autoFindPos.PullUpdown_Diff_mid = lineMoveP.PullAxis_Updown_P2 - lineMoveP.PullAxis_Updown_P4;
boxEquip.autoFindPos.PullUpdown_Diff_high = lineMoveP.PullAxis_Updown_P3 - lineMoveP.PullAxis_Updown_P4;
boxEquip.autoFindPos.UpdownAxis_Diff_Upper_Low = lineMoveP.Updown_P7_P13 - lineMoveP.Updown_P6_P12;
boxEquip.autoFindPos.Startposition_MoveAxis = lineMoveP.MoveAxis_P3;
boxEquip.autoFindPos.Startposition_PullUpdpwnAxis = lineMoveP.PullAxis_Updown_P4;
boxEquip.autoFindPos.Startposition_UpDownAxis = lineMoveP.Updown_P6_P12;
int.TryParse(txtRowsInDrawer.Text, out rows_in_drawer);
int.TryParse(txtColsInDrawer.Text, out colmns_in_drawer);
boxEquip.autoFindPos.RowsInDraw = rows_in_drawer;
boxEquip.autoFindPos.ColumnsInDraw = colmns_in_drawer;
btnStartVision.Enabled = true;
boxEquip.autoFindPosMode = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
label18.Text = $"当前抽屉的行:{boxEquip.autoFindPos.CurRow},列:{boxEquip.autoFindPos.CurColumn}";
}
private void button11_Click(object sender, EventArgs e)
{
if (btnPauseVision.Text.Equals("暂停"))
{
boxEquip.autoFindPosMode = false;
btnPauseVision.Text = "继续";
btnStopVision.Enabled = true;
}
else
{
boxEquip.autoFindPosMode = true;
btnPauseVision.Text = "暂停";
btnStopVision.Enabled = false;
}
}
private void button12_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
listBox1.Items.Clear();
foreach (string item in boxEquip.autoFindPos.FailedDrawers)
{
listBox1.Items.Add(item);
}
boxEquip.FindPosEnd();
btnPauseVision.Enabled = false;
btnStopVision.Enabled = false;
}
/// <summary>
/// 取像并获取mark点坐标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAcquireImage1_Click(object sender, EventArgs e)
{
Bitmap bitmap = null;
bool rtn = false;
eyemLib_Sharp.EyemLibDemo.EyemOcsFXYR eyemOcsFXYR;
if (posSide.Equals("AA"))
{
bitmap = boxEquip.AcqImage("box_A");
}
else
{
bitmap = boxEquip.AcqImage("box_B");
}
pictureBox1.Image = null;
if (bitmap != null)
{
int res = AutoFindPos.GetMarkInfo(bitmap, boxEquip.CID + posSide + "_Manual", string.Format("{0}", DateTime.Now.ToString("yyyyMMddhhmmss")), out eyemOcsFXYR, out Bitmap bitmap1, (SelectColor)cmbColor.SelectedIndex);
if (bitmap1 != null)
pictureBox1.Image = bitmap1;
else
pictureBox1.Image = bitmap;
if (res == 0)
{
txtMark_X.Text = eyemOcsFXYR.fX.ToString("f2");
txtMark_Y.Text = eyemOcsFXYR.fY.ToString("f2");
lblResult.Text = "获取mark点信息成功";
lblResult.BackColor = Color.Green;
}
else
{
lblResult.Text = "获取mark点信息失败,异常码:" + res;
lblResult.BackColor = Color.Red;
}
}
else
{
lblResult.Text = "取像失败,请检查相机参数";
lblResult.BackColor = Color.Red;
}
}
private void checkBoxOpenAuto_CheckedChanged(object sender, EventArgs e)
{
SetState(checkBoxOpenAuto.Checked);
}
private void chkBoxCloseCam_CheckedChanged(object sender, EventArgs e)
{
boxEquip.CloseCamDetect = chkBoxCloseCam.Checked;
Common.ConfigAppSettings.SaveValue(Setting_Init.CloseCamDetect, boxEquip.CloseCamDetect.ToString());
}
/// <summary>
/// 选择抽屉并更新到其他抽屉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpdate_Click(object sender, EventArgs e)
{
int.TryParse(txtRowsInDrawer.Text, out rows_in_drawer);
int.TryParse(txtColsInDrawer.Text, out colmns_in_drawer);
progressBar1.Maximum = boxEquip.Config.Drawer_Rows * boxEquip.Config.Drawer_Columns * rows_in_drawer * colmns_in_drawer;
progressBar1.Minimum = 0;
int count = 0;
//位置配置
string appPath = Application.StartupPath;
//如果总配置文件存在,保存到总的配置文件
string positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_BoxPosition);
if (!File.Exists(positionConfigFile))
{
string nameStr = boxEquip.DeviceID.ToString();
positionConfigFile = positionConfigFile.Replace(".csv", "_" + nameStr + ".csv");
}
string info = "";
bool ok = false;
isBreak = false;
Task.Factory.StartNew(new Action(() =>
{
for (int i = 1; i <= boxEquip.Config.Drawer_Rows; i++)
{
for (int j = 1; j <= boxEquip.Config.Drawer_Columns; j++)
{
for (int d = 1; d <= rows_in_drawer; d++)
{
for (int k = 1; k <= colmns_in_drawer; k++)
{
StringBuilder head1 = new StringBuilder(boxEquip.CID + posSide);
head1.Append(i.ToString().PadLeft(2, '0'));
head1.Append(j.ToString().PadLeft(2, '0'));
head1.Append(d.ToString().PadLeft(2, '0'));
head1.Append(k.ToString().PadLeft(2, '0'));
BoxPosition ktkPosition = CSVPositionReader<BoxPosition>.GetPositon(head1.ToString());
StringBuilder head2 = new StringBuilder(boxEquip.CID + posSide + txtCurRow.Text.PadLeft(2, '0') + txtCurCol.Text.PadLeft(2, '0'));
head2.Append(d.ToString().PadLeft(2, '0'));
head2.Append(k.ToString().PadLeft(2, '0'));
BoxPosition srcPosition = CSVPositionReader<BoxPosition>.GetPositon(head2.ToString());
if (ktkPosition != null && srcPosition != null)
{
ktkPosition.ComAxis_AB_P2 = srcPosition.ComAxis_AB_P2;
ktkPosition.ComAxis_AB_P3 = srcPosition.ComAxis_AB_P3;
ktkPosition.PullAxis_Inout_P2_P4 = srcPosition.PullAxis_Inout_P2_P4;
ktkPosition.PullAxis_Inout_P3_P5 = srcPosition.PullAxis_Inout_P3_P5;
ktkPosition.XAxis_AB_P3 = srcPosition.XAxis_AB_P3;
bool result = CSVPositionReader<BoxPosition>.SavePostion(positionConfigFile, ktkPosition);
if (result)
info = $"{head2.ToString()}->{head1.ToString()} 成功...{(count * 100 / progressBar1.Maximum).ToString("d")}%";
else
info = $"{head2.ToString()}->{head1.ToString()} 保存失败...{(count * 100 / progressBar1.Maximum).ToString("d")}%";
ok = result;
}
else if (ktkPosition == null)
{
info = $"{head2.ToString()}->{head1.ToString()} 失败,目标仓位不存在...{(count * 100 / progressBar1.Maximum).ToString("d")}%";
ok = false;
}
else if (srcPosition == null)
{
info = $"{head2.ToString()}->{head1.ToString()} 失败,源仓位不存在...{(count * 100 / progressBar1.Maximum).ToString("d")}%";
ok = false;
}
label8.Text = info;
label8.BackColor = ok ? Color.Green : Color.Red;
count++;
progressBar1.Value = count; ;
System.Threading.Thread.Sleep(50);
if (isBreak)
return;
}
}
}
}
info = $"更新完成!";
label8.Text = info;
label8.BackColor = Color.Green;
}));
}
/// <summary>
/// 文本框内容变化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtChanged(object sender, EventArgs e)
{
groupBox5.Text = string.Format("更新其他抽屉内的库位,数据源为:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
groupBox6.Text = string.Format("当前抽屉:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
groupBox7.Text = string.Format("当前抽屉:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
}
private void btnStopUpdate_Click(object sender, EventArgs e)
{
isBreak = true;
}
private void btnAcquireImage2_Click(object sender, EventArgs e)
{
Bitmap bitmap = null;
eyemLib_Sharp.EyemLibDemo.EyemOcsFXYR eyemOcsFXYR;
if (posSide.Equals("AA"))
{
bitmap = boxEquip.AcqImage("box_A");
}
else
{
bitmap = boxEquip.AcqImage("box_B");
}
pictureBox1.Image = null;
if (bitmap != null)
{
int res = AutoFindPos.GetMarkInfo(bitmap, boxEquip.CID + posSide + "_Manual", string.Format("{0}", DateTime.Now.ToString("yyyyMMddhhmmss")), out eyemOcsFXYR, out Bitmap bitmap1);
if (bitmap1 != null)
pictureBox2.Image = bitmap1;
else
pictureBox2.Image = bitmap;
if (res == 0)
{
txtMarkX_Validate.Text = eyemOcsFXYR.fX.ToString("f2");
txtMarkY_Validate.Text = eyemOcsFXYR.fY.ToString("f2");
lblResult.Text = "获取mark点信息成功";
lblResult.BackColor = Color.Green;
}
else
{
lblResult.Text = "获取mark点信息失败,异常码:" + res;
lblResult.BackColor = Color.Red;
}
}
else
{
lblResult.Text = "取像失败,请检查相机参数";
lblResult.BackColor = Color.Red;
}
}
private void btnInoutToPosition1_Click(object sender, EventArgs e)
{
boxEquip.PullAxis_Inout.AbsMove(null, boxEquip.Config.PullAxis_Inout_P1, boxEquip.Config.PullAxis_Inout_P1_Speed);
}
private void button4_Click(object sender, EventArgs e)
{
if (posSide.Equals("AA"))
{
boxEquip.PullAxis_Inout.AbsMove(null, boxEquip.Config.PullAxis_Inout_CamA, boxEquip.Config.PullAxis_Inout_P1_Speed);
}
else
{
boxEquip.PullAxis_Inout.AbsMove(null, boxEquip.Config.PullAxis_Inout_CamB, boxEquip.Config.PullAxis_Inout_P1_Speed);
}
}
private void btnMoveAxisToPos1_Click(object sender, EventArgs e)
{
StringBuilder head2 = new StringBuilder(boxEquip.CID + posSide + txtCurRow.Text.PadLeft(2, '0') + txtCurCol.Text.PadLeft(2, '0'));
head2.Append("0101");
LineMoveP lineMoveP = new LineMoveP();
lineMoveP.UpdatePosById(head2.ToString());
boxEquip.MoveAxis.AbsMove(null, lineMoveP.MoveAxis_P3, boxEquip.Config.MoveAxis_FindPosSpeed);
}
private void btnUndwnToPos1_Click(object sender, EventArgs e)
{
StringBuilder head2 = new StringBuilder(boxEquip.CID + posSide + txtCurRow.Text.PadLeft(2, '0') + txtCurCol.Text.PadLeft(2, '0'));
head2.Append("0101");
LineMoveP lineMoveP = new LineMoveP();
lineMoveP.UpdatePosById(head2.ToString());
boxEquip.UpdownAxis.AbsMove(null, lineMoveP.Updown_P6_P12, boxEquip.Config.UpdownAxis_FindPosSpeed);
}
private void btnPullUpdwnToPos1_Click(object sender, EventArgs e)
{
StringBuilder head2 = new StringBuilder(boxEquip.CID + posSide + txtCurRow.Text.PadLeft(2, '0') + txtCurCol.Text.PadLeft(2, '0'));
head2.Append("0101");
LineMoveP lineMoveP = new LineMoveP();
lineMoveP.UpdatePosById(head2.ToString());
boxEquip.PullAxis_Updown.AbsMove(null, lineMoveP.PullAxis_Updown_P4, boxEquip.Config.PullAxis_Updown_FindPosSpeed);
}
private void chkCloseOutStoreCheck_CheckedChanged(object sender, EventArgs e)
{
boxEquip.CloseOutStoreCheck = chkCloseOutStoreCheck.Checked;
}
private void btnOpenImg_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.Filter = "Image|*.bmp";
if (op.ShowDialog() == DialogResult.OK)
{
EyemLibDemo.eyemImageRead(op.FileName, -1,out EyemLibDemo.EyemImage img);
eyemLib_Sharp.EyemLibDemo.EyemOcsFXYR eyemOcsFXYR;
int res = AutoFindPos.GetMarkInfo(img, boxEquip.CID + posSide + "_Manual", string.Format("{0}", DateTime.Now.ToString("yyyyMMddhhmmss")), out eyemOcsFXYR, out Bitmap bitmap1, (SelectColor)cmbColor.SelectedIndex);
if (bitmap1 != null)
pictureBox1.Image = bitmap1;
else
pictureBox1.Image = Image.FromFile(op.FileName);
if (res == 0)
{
txtMark_X.Text = eyemOcsFXYR.fX.ToString("f2");
txtMark_Y.Text = eyemOcsFXYR.fY.ToString("f2");
lblResult.Text = "获取mark点信息成功";
lblResult.BackColor = Color.Green;
}
else
{
lblResult.Text = "获取mark点信息失败,异常码:" + res;
lblResult.BackColor = Color.Red;
}
}
}
}
}