Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

381 linhas
14 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("X轴");
  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. timer2.Enabled = true;
  74. if(SysMgr.Instance.QGSts())
  75. button5.Text = "气缸松开";
  76. else
  77. button5.Text = "气缸加紧";
  78. this.propertyGrid2.SelectedObject = Defectprop;
  79. //Sizeprop.deserialize("");
  80. //DATA
  81. SysMgr.Instance.SizeLib.finishEvent += new System.Action<SizeTask>((task) =>
  82. {
  83. Sizeprop.PT1 = task.PT1;
  84. Sizeprop.PT2 = task.PT2;
  85. Sizeprop.Shanxian = task.Shanxian;
  86. //prop.RowP= task.RowP;
  87. Sizeprop.Circle_Ymm = task.Circle_Ymm;
  88. Sizeprop.Circle_Xmm = task.Circle_Xmm;
  89. Sizeprop.offsetX = task.offsetX;
  90. Sizeprop.offsetY = task.offsetY;
  91. Sizeprop.MarkPointList = task.MarkPointList;
  92. Sizeprop.resultInfo = task.isSucceed ? "成功" : task.resultInfo;
  93. this.refreshUI1();
  94. });
  95. //Defectprop.deserialize("");
  96. this.propertyGrid2.Refresh();
  97. //DATA
  98. SysMgr.Instance.DefectLib.finishEvent += new System.Action<DefectTask>((task) =>
  99. {
  100. DateTime t1 = DateTime.Now;
  101. var t2 = t1 - task.nowTime;
  102. var t3 = t2.Milliseconds;
  103. Defectprop.InformationList = JsonConvert.SerializeObject(task.informationList);
  104. Defectprop.resultInfo = task.isSucceed ? "成功" : task.resultInfo;
  105. this.refreshUI2();
  106. });
  107. }
  108. private void refreshUI1()
  109. {
  110. SyncContext1.Post(m =>
  111. {
  112. var result = m as string;
  113. //txtLog.Text = szLog;//不显示
  114. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  115. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  116. }, "异步操作完成结果");
  117. }
  118. private void refreshUI2()
  119. {
  120. SyncContext2.Post(m =>
  121. {
  122. var result = m as string;
  123. propertyGrid2.Refresh();
  124. //txtLog.Text = szLog;//不显示
  125. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  126. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  127. }, "异步操作完成结果");
  128. }
  129. #region 前部相机
  130. #endregion
  131. #region 后部相机
  132. private void ShowImage2(HObject image)
  133. {
  134. lock (this)
  135. {
  136. Mat img = CamDev.HImageToMat(image);
  137. imageBox2.SetInteractive(false);
  138. if (imageBox2.Image == null)
  139. imageBox2.RefreshWindow(img, ImageBox.ImageModeEnum.Zoom);//可以不显示区域
  140. else
  141. imageBox2.RefreshWindow(img, ImageBox.ImageModeEnum.Part);//可以不显示区域
  142. imageBox2.SetInteractive(true);
  143. }
  144. }
  145. private void btnTrg2_Click(object sender, EventArgs e)
  146. {
  147. SysMgr.Instance.CamDevBack.ClearImageQueue();
  148. Acquisition acq = SysMgr.Instance.CamDevBack.Snap(1, 5000);
  149. if (acq.GrabStatus != "GrabPass")
  150. {
  151. ;
  152. }
  153. ShowImage2(acq.Image);
  154. }
  155. private void timer3_Tick(object sender, EventArgs e)
  156. {
  157. SysMgr.Instance.CamDevBack.ClearImageQueue();
  158. Acquisition acq = SysMgr.Instance.CamDevBack.Snap(1, 100);
  159. if (acq.GrabStatus != "GrabPass")
  160. {
  161. return;
  162. }
  163. ShowImage2(acq.Image);
  164. System.GC.Collect();
  165. }
  166. private void BtnGrab2_Click(object sender, EventArgs e)
  167. {
  168. if (this.BtnGrab2.Text == "连续拍照")
  169. {
  170. this.btnTrg2.Enabled = false;
  171. this.BtnGrab2.Text = "停止拍照";
  172. timer3.Enabled = true;
  173. }
  174. else
  175. {
  176. this.btnTrg2.Enabled = true;
  177. this.BtnGrab2.Text = "连续拍照";
  178. timer3.Enabled = false;
  179. }
  180. }
  181. private void BtnSet2_Click(object sender, EventArgs e)
  182. {
  183. if (!SysMgr.Instance.CamDevBack.SetExposure((double)numExp2.Value))
  184. {
  185. stslog.Text = "曝光设置失败!";
  186. return;
  187. }
  188. if (!SysMgr.Instance.CamDevBack.SetGain((double)numGain2.Value))
  189. {
  190. stslog.Text = "增益设置失败!";
  191. return;
  192. }
  193. stslog.Text = "曝光-增益-帧率设置成功";
  194. }
  195. private void BtnDisCam2_Click(object sender, EventArgs e)
  196. {
  197. SysMgr.Instance.CamDevBack.CloseCamera();
  198. BtnGrab2.Enabled = false;
  199. btnTrg2.Enabled = false;
  200. BtnSet2.Enabled = false;
  201. }
  202. private bool verifyThresholdsCount(string szThresholdsClass, int iThresholdsClassCount)
  203. {
  204. return szThresholdsClass.Trim().TrimEnd(new char[] { ',', ';' })
  205. .Split(new char[] { ',', ';' }).Count() == iThresholdsClassCount;
  206. }
  207. private void button8_Click(object sender, EventArgs e)
  208. {
  209. if (!Util.IsNumber(Defectprop.ThresholdsClass.Replace("-", "").Replace(",", "").Replace(";", "").Replace(".", "")))
  210. {
  211. MessageBox.Show("输入种类阀值与数量不一致!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  212. return;
  213. }
  214. if (!verifyThresholdsCount(Defectprop.ThresholdsClass, Defectprop.ThresholdsClassCount))
  215. {
  216. MessageBox.Show("输入种类阀值与数量不一致!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  217. return;
  218. }
  219. try
  220. {
  221. SysMgr.Instance.DefectLib.loadModelFile(Defectprop.ModelPath);
  222. Defectprop.resultInfo = "";
  223. SysMgr.Instance.DefectLib.add(new DefectTask()
  224. {
  225. nowTime = DateTime.Now,
  226. bmp = new Mat(Defectprop.BmpPath),
  227. cut_size = Defectprop.CutSize,
  228. resize = Defectprop.Resize,
  229. thresholds = Defectprop.Thresholds,
  230. thresholdsClass = Defectprop.ThresholdsClass,
  231. //finishEvent = (res) =>
  232. //{
  233. //}
  234. });
  235. }
  236. catch (Exception ex)
  237. {
  238. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  239. }
  240. }
  241. #endregion
  242. #region 轴移动
  243. private void btnMove_Click(object sender, EventArgs e)
  244. {
  245. if (SysMgr.Instance.AxisDev != null)
  246. {
  247. VelocityCurveParams _VelParams = new VelocityCurveParams();
  248. int axisNo = cbAxisName.SelectedIndex + 1;
  249. _VelParams.Acc = (double)numAcc.Value;
  250. _VelParams.Dec = (double)numDec.Value;
  251. _VelParams.StartVel = (double)numST.Value;
  252. _VelParams.RunVel = (double)numET.Value;
  253. _VelParams.JerkT_S = cbTS.SelectedIndex;
  254. _VelParams.MM2PulseNum = SysMgr.Instance.GetMMtoPlus((AxisName)axisNo);
  255. if (cbDir.Text == "绝对运动")
  256. {
  257. stslog.Text = "绝对运动:" + numDist.Value.ToString();
  258. SysMgr.Instance.AxisDev.MoveAbsValue(axisNo, _VelParams, (double)numDist.Value);
  259. }
  260. else if (cbDir.Text == "相对运动")
  261. {
  262. stslog.Text = "相对运动:" + numDist.Value.ToString();
  263. SysMgr.Instance.AxisDev.MoveRelValue(axisNo, _VelParams, (double)numDist.Value);
  264. }
  265. }
  266. }
  267. private void btnJogP_MouseDown(object sender, MouseEventArgs e)
  268. {
  269. Button btn = sender as Button;
  270. int axisNo = cbAxisName.SelectedIndex + 1;
  271. double val = 50;
  272. VelocityCurveParams velocityCurveParams = new VelocityCurveParams(100, 100, 0, 100, 1, SysMgr.Instance.GetMMtoPlus((AxisName)axisNo));
  273. if (btn.Name == "btnJogP")
  274. val = 50;
  275. else
  276. val = -50;
  277. SysMgr.Instance.AxisDev.MoveRelValue(axisNo, velocityCurveParams, val);
  278. }
  279. private void btnJogP_MouseUp(object sender, MouseEventArgs e)
  280. {
  281. int axisNo = cbAxisName.SelectedIndex + 1;
  282. SysMgr.Instance.AxisDev.DecelStop(axisNo);
  283. }
  284. private void timer2_Tick(object sender, EventArgs e)
  285. {
  286. int axisNo = cbAxisName.SelectedIndex + 1;
  287. try
  288. {
  289. numericUpDown1.Value = (decimal)SysMgr.Instance.AxisDev.GetFeedbackPos(axisNo) / SysMgr.Instance.GetMMtoPlus((AxisName)axisNo);
  290. }
  291. catch
  292. {
  293. numericUpDown1.Value = 0;
  294. }
  295. }
  296. #endregion
  297. #region 光源控制
  298. private void button4_Click(object sender, EventArgs e)
  299. {
  300. int ch = cbCH.SelectedIndex + 1;
  301. SysMgr.Instance.LightDev.SetLightDigitalValue(ch, (int)numericUpDown3.Value);
  302. }
  303. private void button1_Click(object sender, EventArgs e)
  304. {
  305. int ch = cbCH.SelectedIndex + 1;
  306. SysMgr.Instance.LightDev.OpenLight(ch);
  307. }
  308. private void button3_Click(object sender, EventArgs e)
  309. {
  310. int ch = cbCH.SelectedIndex + 1;
  311. SysMgr.Instance.LightDev.CloseLight(ch);
  312. }
  313. #endregion
  314. #region 产品加紧
  315. private void button5_Click(object sender, EventArgs e)
  316. {
  317. if (button5.Text == "气缸加紧")
  318. {
  319. button5.Text = "气缸松开";
  320. SysMgr.Instance.RunQG(true);
  321. }
  322. else
  323. {
  324. button5.Text = "气缸加紧";
  325. SysMgr.Instance.RunQG(false);
  326. }
  327. }
  328. #endregion
  329. #region 厚度
  330. #endregion
  331. #region 张力
  332. #endregion
  333. #region 变焦
  334. #endregion
  335. private void DebugTestFrm_FormClosing(object sender, FormClosingEventArgs e)
  336. {
  337. timer2.Enabled = false;
  338. timer3.Enabled = false;
  339. System.GC.Collect();
  340. }
  341. }
  342. }