版博士V2.0程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

591 rinda
22 KiB

  1. using HalconDotNet;
  2. using ImageToolKits;
  3. using MaiMuAOI.SysCtrl;
  4. using MaiMuControl.Device;
  5. using MaiMuControl.Device.AxisDev;
  6. using MaiMuControl.Device.CamDev;
  7. using MaiMuControl.Utils;
  8. using Newtonsoft.Json;
  9. using OpenCvSharp;
  10. using ProductionControl.UI;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Drawing;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Windows.Forms;
  21. using ToolKits.EnumTool;
  22. using static MaiMuAOI.ImageProcessing.DefectLib;
  23. using static MaiMuAOI.ImageProcessing.SizeLib;
  24. namespace MaiMuAOI.SysUI.DefectPicShow
  25. {
  26. public partial class DebugTestFrm : Form
  27. {
  28. private SizeLibProp Sizeprop = new SizeLibProp();
  29. private DefectLibProp Defectprop = new DefectLibProp(ConfMgr.Instance.SysConfigParams.Defect_CutSize,
  30. ConfMgr.Instance.SysConfigParams.Defect_ReSize, ConfMgr.Instance.SysConfigParams.Defect_Thresholds);
  31. SynchronizationContext SyncContext1 = null;
  32. SynchronizationContext SyncContext2 = null;
  33. public DebugTestFrm()
  34. {
  35. InitializeComponent();
  36. UIStyle.SetUIStyle(this);
  37. this.uiTitel1.FatherForm = this;
  38. }
  39. private void DebugTestFrm_Load(object sender, EventArgs e)
  40. {
  41. this.Left = 600;
  42. //显示tS曲线
  43. cbTS.Items.Clear();
  44. Type VelType = typeof(VelocityCurveType);
  45. foreach (object s in Enum.GetValues(VelType))
  46. {
  47. string st = EnumExtension.GetEnumDescription((VelocityCurveType)s);
  48. cbTS.Items.Add(st);
  49. }
  50. cbTS.Text = cbTS.Items[0].ToString();
  51. cbTS.SelectedIndex = 0;
  52. cbDir.Items.Clear();
  53. cbDir.Items.Add("相对运动");
  54. cbDir.Items.Add("绝对运动");
  55. cbDir.Text = cbDir.Items[0].ToString();
  56. cbDir.SelectedIndex = 0;
  57. cbAxisName.Items.Clear();
  58. cbAxisName.Items.Add("X1轴");
  59. cbAxisName.Items.Add("X2轴");
  60. cbAxisName.Items.Add("Y轴");
  61. cbAxisName.Items.Add("Z轴");
  62. cbAxisName.Text = cbAxisName.Items[0].ToString();
  63. cbAxisName.SelectedIndex = 0;
  64. cbCH.Items.Clear();
  65. Type lightenum = typeof(LightChannelEnum);
  66. foreach (object s in Enum.GetValues(lightenum))
  67. {
  68. string st = EnumExtension.GetEnumDescription((LightChannelEnum)s);
  69. cbCH.Items.Add(st);
  70. }
  71. cbCH.Text = cbCH.Items[0].ToString();
  72. cbCH.SelectedIndex = 0;
  73. cbLensMotor.SelectedIndex = 8;
  74. timer2.Enabled = true;
  75. if(SysMgr.Instance.QGSts())
  76. button5.Text = "气缸松开";
  77. else
  78. button5.Text = "气缸加紧";
  79. if (SysMgr.Instance.ThicknessSts())
  80. BtnHeightAck.Text = "高度传感器上升";
  81. else
  82. BtnHeightAck.Text = "高度传感器下降";
  83. this.propertyGrid1.SelectedObject = Sizeprop;
  84. this.propertyGrid2.SelectedObject = Defectprop;
  85. //Sizeprop.deserialize("");
  86. this.propertyGrid1.Refresh();
  87. //DATA
  88. SysMgr.Instance.SizeLib.finishEvent += new System.Action<SizeTask>((task) =>
  89. {
  90. Sizeprop.PT1 = task.PT1;
  91. Sizeprop.PT2 = task.PT2;
  92. Sizeprop.Shanxian = task.Shanxian;
  93. //prop.RowP= task.RowP;
  94. Sizeprop.Circle_Ymm = task.Circle_Ymm;
  95. Sizeprop.Circle_Xmm = task.Circle_Xmm;
  96. Sizeprop.offsetX = task.offsetX;
  97. Sizeprop.offsetY = task.offsetY;
  98. Sizeprop.MarkPointList = task.MarkPointList;
  99. Sizeprop.resultInfo = task.isSucceed ? "成功" : task.resultInfo;
  100. this.refreshUI1();
  101. });
  102. //Defectprop.deserialize("");
  103. this.propertyGrid2.Refresh();
  104. //DATA
  105. SysMgr.Instance.DefectLib.finishEvent += new System.Action<DefectTask>((task) =>
  106. {
  107. DateTime t1 = DateTime.Now;
  108. var t2 = t1 - task.nowTime;
  109. var t3 = t2.Milliseconds;
  110. Defectprop.InformationList = JsonConvert.SerializeObject(task.informationList);
  111. Defectprop.resultInfo = task.isSucceed ? "成功" : task.resultInfo;
  112. this.refreshUI2();
  113. });
  114. }
  115. private void refreshUI1()
  116. {
  117. SyncContext1.Post(m =>
  118. {
  119. var result = m as string;
  120. propertyGrid1.Refresh();
  121. //txtLog.Text = szLog;//不显示
  122. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  123. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  124. }, "异步操作完成结果");
  125. }
  126. private void refreshUI2()
  127. {
  128. SyncContext2.Post(m =>
  129. {
  130. var result = m as string;
  131. propertyGrid2.Refresh();
  132. //txtLog.Text = szLog;//不显示
  133. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  134. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  135. }, "异步操作完成结果");
  136. }
  137. #region 前部相机
  138. private void btnTrg_Click(object sender, EventArgs e)
  139. {
  140. SysMgr.Instance.CamDevFront.ClearImageQueue();
  141. Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 5000);
  142. if (acq.GrabStatus != "GrabPass")
  143. {
  144. ;
  145. }
  146. //ShowImage(acq.Image.CopyObj(1, -1));
  147. ShowImage1(acq.Image);
  148. }
  149. private void ShowImage1(HObject image)
  150. {
  151. lock (this)
  152. {
  153. Mat img = CamDev.HImageToMat(image);
  154. //imageBox1.ColorName = "red";
  155. imageBox1.SetInteractive(false);
  156. if (imageBox1.Image == null)
  157. imageBox1.RefreshWindow(img, ImageBox.ImageModeEnum.Zoom);//可以不显示区域
  158. else
  159. imageBox1.RefreshWindow(img, ImageBox.ImageModeEnum.Part);//可以不显示区域
  160. imageBox1.SetInteractive(true);
  161. //imageBox1.ColorName = "green";
  162. }
  163. }
  164. private void btnGrab_Click(object sender, EventArgs e)
  165. {
  166. if (this.btnGrab.Text == "连续拍照")
  167. {
  168. this.btnTrg.Enabled = false;
  169. this.btnGrab.Text = "停止拍照";
  170. timer1.Enabled = true;
  171. }
  172. else
  173. {
  174. this.btnTrg.Enabled = true;
  175. this.btnGrab.Text = "连续拍照";
  176. timer1.Enabled = false;
  177. }
  178. }
  179. private void timer1_Tick(object sender, EventArgs e)
  180. {
  181. SysMgr.Instance.CamDevFront.ClearImageQueue();
  182. Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 100);
  183. if (acq.GrabStatus != "GrabPass")
  184. {
  185. return;
  186. //MessageBox.Show( "获取图片失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  187. }
  188. //ShowImage(acq.Image.CopyObj(1, -1));
  189. ShowImage1(acq.Image);
  190. System.GC.Collect();
  191. }
  192. private void button2_Click(object sender, EventArgs e)
  193. {
  194. if (!SysMgr.Instance.CamDevFront.SetExposure((double)numExposure.Value))
  195. {
  196. stslog.Text = "曝光设置失败!";
  197. return;
  198. }
  199. if (!SysMgr.Instance.CamDevFront.SetGain((double)numGain.Value))
  200. {
  201. stslog.Text = "增益设置失败!";
  202. return;
  203. }
  204. stslog.Text = "曝光-增益-帧率设置成功";
  205. }
  206. private void button6_Click(object sender, EventArgs e)
  207. {
  208. SysMgr.Instance.CamDevFront.CloseCamera();
  209. button2.Enabled = false;
  210. btnTrg.Enabled = false;
  211. btnGrab.Enabled = false;
  212. }
  213. private void button7_Click(object sender, EventArgs e)
  214. {
  215. try
  216. {
  217. Sizeprop.resultInfo = "";
  218. SysMgr.Instance.SizeLib.add(new SizeTask()
  219. {
  220. engineName = Sizeprop.EngineName,
  221. file_path = Sizeprop.BmpPath,
  222. //bmp =read2Bmp(prop.BmpPath),
  223. index = Sizeprop.Index,
  224. //finishEvent = (res) =>
  225. //{
  226. //}
  227. });
  228. }
  229. catch (Exception ex)
  230. {
  231. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  232. }
  233. }
  234. #endregion
  235. #region 后部相机
  236. private void ShowImage2(HObject image)
  237. {
  238. lock (this)
  239. {
  240. Mat img = CamDev.HImageToMat(image);
  241. imageBox2.SetInteractive(false);
  242. if (imageBox2.Image == null)
  243. imageBox2.RefreshWindow(img, ImageBox.ImageModeEnum.Zoom);//可以不显示区域
  244. else
  245. imageBox2.RefreshWindow(img, ImageBox.ImageModeEnum.Part);//可以不显示区域
  246. imageBox2.SetInteractive(true);
  247. }
  248. }
  249. private void btnTrg2_Click(object sender, EventArgs e)
  250. {
  251. SysMgr.Instance.CamDevBack.ClearImageQueue();
  252. Acquisition acq = SysMgr.Instance.CamDevBack.Snap(1, 5000);
  253. if (acq.GrabStatus != "GrabPass")
  254. {
  255. ;
  256. }
  257. ShowImage2(acq.Image);
  258. }
  259. private void timer3_Tick(object sender, EventArgs e)
  260. {
  261. SysMgr.Instance.CamDevBack.ClearImageQueue();
  262. Acquisition acq = SysMgr.Instance.CamDevBack.Snap(1, 100);
  263. if (acq.GrabStatus != "GrabPass")
  264. {
  265. return;
  266. }
  267. ShowImage2(acq.Image);
  268. System.GC.Collect();
  269. }
  270. private void BtnGrab2_Click(object sender, EventArgs e)
  271. {
  272. if (this.BtnGrab2.Text == "连续拍照")
  273. {
  274. this.btnTrg2.Enabled = false;
  275. this.BtnGrab2.Text = "停止拍照";
  276. timer3.Enabled = true;
  277. }
  278. else
  279. {
  280. this.btnTrg2.Enabled = true;
  281. this.BtnGrab2.Text = "连续拍照";
  282. timer3.Enabled = false;
  283. }
  284. }
  285. private void BtnSet2_Click(object sender, EventArgs e)
  286. {
  287. if (!SysMgr.Instance.CamDevBack.SetExposure((double)numExp2.Value))
  288. {
  289. stslog.Text = "曝光设置失败!";
  290. return;
  291. }
  292. if (!SysMgr.Instance.CamDevBack.SetGain((double)numGain2.Value))
  293. {
  294. stslog.Text = "增益设置失败!";
  295. return;
  296. }
  297. stslog.Text = "曝光-增益-帧率设置成功";
  298. }
  299. private void BtnDisCam2_Click(object sender, EventArgs e)
  300. {
  301. SysMgr.Instance.CamDevBack.CloseCamera();
  302. BtnGrab2.Enabled = false;
  303. btnTrg2.Enabled = false;
  304. BtnSet2.Enabled = false;
  305. }
  306. private bool verifyThresholdsCount(string szThresholdsClass, int iThresholdsClassCount)
  307. {
  308. return szThresholdsClass.Trim().TrimEnd(new char[] { ',', ';' })
  309. .Split(new char[] { ',', ';' }).Count() == iThresholdsClassCount;
  310. }
  311. private void button8_Click(object sender, EventArgs e)
  312. {
  313. if (!Util.IsNumber(Defectprop.ThresholdsClass.Replace("-", "").Replace(",", "").Replace(";", "").Replace(".", "")))
  314. {
  315. MessageBox.Show("输入种类阀值与数量不一致!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  316. return;
  317. }
  318. if (!verifyThresholdsCount(Defectprop.ThresholdsClass, Defectprop.ThresholdsClassCount))
  319. {
  320. MessageBox.Show("输入种类阀值与数量不一致!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  321. return;
  322. }
  323. try
  324. {
  325. SysMgr.Instance.DefectLib.loadModelFile(Defectprop.ModelPath);
  326. Defectprop.resultInfo = "";
  327. SysMgr.Instance.DefectLib.add(new DefectTask()
  328. {
  329. nowTime = DateTime.Now,
  330. bmp = new Mat(Defectprop.BmpPath),
  331. cut_size = Defectprop.CutSize,
  332. resize = Defectprop.Resize,
  333. thresholds = Defectprop.Thresholds,
  334. thresholdsClass = Defectprop.ThresholdsClass,
  335. //finishEvent = (res) =>
  336. //{
  337. //}
  338. });
  339. }
  340. catch (Exception ex)
  341. {
  342. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  343. }
  344. }
  345. #endregion
  346. #region 轴移动
  347. private void btnMove_Click(object sender, EventArgs e)
  348. {
  349. if (SysMgr.Instance.AxisDev != null)
  350. {
  351. //测厚气缸缩回
  352. if(!SysMgr.Instance.ThicknessIsSafe())
  353. {
  354. stslog.Text = "高度传感器不在安全位置";
  355. return;
  356. }
  357. VelocityCurveParams _VelParams = new VelocityCurveParams();
  358. int axisNo = cbAxisName.SelectedIndex;
  359. _VelParams.Acc = (double)numAcc.Value;
  360. _VelParams.Dec = (double)numDec.Value;
  361. _VelParams.StartVel = (double)numST.Value;
  362. _VelParams.RunVel = (double)numET.Value;
  363. _VelParams.JerkT_S = cbTS.SelectedIndex;
  364. _VelParams.MM2PulseNum = SysMgr.Instance.GetMMtoPlus((AxisName)axisNo);
  365. if (cbDir.Text == "绝对运动")
  366. {
  367. stslog.Text = "绝对运动:" + numDist.Value.ToString();
  368. SysMgr.Instance.AxisDev.MoveAbsValue(axisNo, _VelParams, (double)numDist.Value);
  369. }
  370. else if (cbDir.Text == "相对运动")
  371. {
  372. stslog.Text = "相对运动:" + numDist.Value.ToString();
  373. SysMgr.Instance.AxisDev.MoveRelValue(axisNo, _VelParams, (double)numDist.Value);
  374. }
  375. }
  376. }
  377. private void btnJogP_MouseDown(object sender, MouseEventArgs e)
  378. {
  379. if (!SysMgr.Instance.ThicknessIsSafe())
  380. {
  381. stslog.Text = "高度传感器不在安全位置";
  382. return;
  383. }
  384. Button btn = sender as Button;
  385. int axisNo = cbAxisName.SelectedIndex;
  386. double val = 50;
  387. VelocityCurveParams velocityCurveParams = new VelocityCurveParams(100, 100, 0, 100, 1, SysMgr.Instance.GetMMtoPlus((AxisName)axisNo));
  388. if (btn.Name == "btnJogP")
  389. val = 50;
  390. else
  391. val = -50;
  392. SysMgr.Instance.AxisDev.MoveRelValue(axisNo, velocityCurveParams, val);
  393. }
  394. private void btnJogP_MouseUp(object sender, MouseEventArgs e)
  395. {
  396. int axisNo = cbAxisName.SelectedIndex;
  397. SysMgr.Instance.AxisDev.DecelStop(axisNo);
  398. }
  399. private void timer2_Tick(object sender, EventArgs e)
  400. {
  401. int axisNo = cbAxisName.SelectedIndex;
  402. try
  403. {
  404. numericUpDown1.Value = (decimal)SysMgr.Instance.AxisDev.GetFeedbackPos(axisNo) / SysMgr.Instance.GetMMtoPlus((AxisName)axisNo);
  405. }
  406. catch
  407. {
  408. numericUpDown1.Value = 0;
  409. }
  410. }
  411. #endregion
  412. #region 光源控制
  413. private void button4_Click(object sender, EventArgs e)
  414. {
  415. int ch = cbCH.SelectedIndex + 1;
  416. SysMgr.Instance.LightDev.SetLightDigitalValue(ch, (int)numericUpDown3.Value);
  417. }
  418. private void button1_Click(object sender, EventArgs e)
  419. {
  420. int ch = cbCH.SelectedIndex + 1;
  421. SysMgr.Instance.LightDev.OpenLight(ch);
  422. }
  423. private void button3_Click(object sender, EventArgs e)
  424. {
  425. int ch = cbCH.SelectedIndex + 1;
  426. SysMgr.Instance.LightDev.CloseLight(ch);
  427. }
  428. #endregion
  429. #region 产品加紧
  430. private void button5_Click(object sender, EventArgs e)
  431. {
  432. if (button5.Text == "气缸加紧")
  433. {
  434. button5.Text = "气缸松开";
  435. SysMgr.Instance.RunQG(true);
  436. }
  437. else
  438. {
  439. button5.Text = "气缸加紧";
  440. SysMgr.Instance.RunQG(false);
  441. }
  442. }
  443. #endregion
  444. #region 厚度
  445. private void BtnHeightAck_Click(object sender, EventArgs e)
  446. {
  447. if (BtnHeightAck.Text == "高度传感器下降")
  448. {
  449. BtnHeightAck.Text = "高度传感器上升";
  450. SysMgr.Instance.RunThickness(true);
  451. }
  452. else
  453. {
  454. BtnHeightAck.Text = "高度传感器下降";
  455. SysMgr.Instance.RunThickness(false);
  456. }
  457. }
  458. private void BtnReadHeight_Click(object sender, EventArgs e)
  459. {
  460. double val = SysMgr.Instance.ThicknessDev.GetValue();
  461. numHeightValue.Value = (decimal)val;
  462. }
  463. #endregion
  464. #region 张力
  465. private void btnReadTension_Click(object sender, EventArgs e)
  466. {
  467. double val = SysMgr.Instance.TensionDev.GetValue();
  468. numTensionValue.Value = (decimal)val;
  469. }
  470. #endregion
  471. #region 变焦
  472. private void btnLens_Click(object sender, EventArgs e)
  473. {
  474. int val = (int)SmallAxCmdPos.倍率0_58X;
  475. switch(cbLensMotor.SelectedIndex)
  476. {
  477. case 0:
  478. val = (int)SmallAxCmdPos.倍率0_58X;
  479. break;
  480. case 1:
  481. val = (int)SmallAxCmdPos.倍率1_0X;
  482. break;
  483. case 2:
  484. val = (int)SmallAxCmdPos.倍率1_5X;
  485. break;
  486. case 3:
  487. val = (int)SmallAxCmdPos.倍率2_0X;
  488. break;
  489. case 4:
  490. val = (int)SmallAxCmdPos.倍率2_5X;
  491. break;
  492. case 5:
  493. val = (int)SmallAxCmdPos.倍率3_0X;
  494. break;
  495. case 6:
  496. val = (int)SmallAxCmdPos.倍率3_5X;
  497. break;
  498. case 7:
  499. val = (int)SmallAxCmdPos.倍率4_0X;
  500. break;
  501. case 8:
  502. val = (int)SmallAxCmdPos.倍率4_5X;
  503. break;
  504. case 9:
  505. val = (int)SmallAxCmdPos.倍率5_0X;
  506. break;
  507. case 10:
  508. val = (int)SmallAxCmdPos.倍率5_5X;
  509. break;
  510. case 11:
  511. val = (int)SmallAxCmdPos.倍率6_0X;
  512. break;
  513. case 12:
  514. val = (int)SmallAxCmdPos.倍率6_5X;
  515. break;
  516. case 13:
  517. val = (int)SmallAxCmdPos.倍率7_0X;
  518. break;
  519. case 14:
  520. val = (int)SmallAxCmdPos.倍率7_5X;
  521. break;
  522. }
  523. int ret = SysMgr.Instance.LensMotorDev.MoveAbsPulse(0, new VelocityCurveParams(), val);
  524. if (ret == -1)
  525. MessageBox.Show("电机初始化出错", "镜头电机报警", MessageBoxButtons.OK, MessageBoxIcon.Error);
  526. if (ret == -2)
  527. MessageBox.Show("电机行程超限!", "镜头电机报警", MessageBoxButtons.OK, MessageBoxIcon.Error);
  528. //SysMgr.Instance.LensMotorDev.CheckDone(0, 10000);
  529. }
  530. private void btnLensM_Click(object sender, EventArgs e)
  531. {
  532. int val = (int)numLensPlus.Value;
  533. int ret = SysMgr.Instance.LensMotorDev.MoveAbsPulse(0, new VelocityCurveParams(), val);
  534. if (ret == -1)
  535. MessageBox.Show("电机初始化出错", "镜头电机报警", MessageBoxButtons.OK, MessageBoxIcon.Error);
  536. if (ret == -2)
  537. MessageBox.Show("电机行程超限!", "镜头电机报警", MessageBoxButtons.OK, MessageBoxIcon.Error);
  538. }
  539. #endregion
  540. private void DebugTestFrm_FormClosing(object sender, FormClosingEventArgs e)
  541. {
  542. timer1.Enabled = false;
  543. timer2.Enabled = false;
  544. timer3.Enabled = false;
  545. System.GC.Collect();
  546. }
  547. }
  548. }