private void InitUV2XYParam() { try { _station.List_UV2XYResult = new List <HTuple>(); tool_vision = new ToolKits.FunctionModule.Vision(); for (int i = 0; i < _station.Num_Camera; i++) { HTuple Item_UV2XYResult = null; //if (!obj_camera[i].isEnable) //{ // Item_UV2XYResult = new HTuple(-1); // _station.List_UV2XYResult.Add(Item_UV2XYResult); // continue; //} if (File.Exists(_station.SystemUV2XYDir + "\\Camera_" + i + "\\" + "UV2XY" + ".dat")) { tool_vision.read_hom2d(_station.SystemUV2XYDir + "\\Camera_" + i + "\\" + "UV2XY" + ".dat", out Item_UV2XYResult); _station.List_UV2XYResult.Add(Item_UV2XYResult); } else { Item_UV2XYResult = new HTuple(-1); _station.List_UV2XYResult.Add(Item_UV2XYResult); } } } catch (Exception ex) { HTUi.PopError("无法读取UV-XY结果文件:" + ex.ToString()); } }
/// <summary> /// 到指定位置拍照 /// </summary> /// <param name="x">相机X坐标</param> /// <param name="y">相机Y坐标</param> /// <param name="z">相机Z坐标</param> /// <param name="htWindow">图像视窗</param> /// <param name="img">拍到的图像</param> public void SnapPos(double x, double y, double z, HTHalControl.HTWindowControl htWindow, out HObject img) { string errMsg = ""; img = null; if (MultiAxisMove(new string[] { "X", "Y", "Z" }, new double[] { x, y, z }, true, out errMsg) != 0) { HTUi.PopError(errMsg); return; } if (_station._RunMode == 1) { return; } //3. 触发 //if(SWPosTrig(new string[] { "X"},out errMsg)!=0) //{ // HTUi.PopError(errMsg); // return; //} //4. 取图 if (_station.operation.CaputreOneImage(_station.CamereDev[_station.SelectedIndex], "Halcon", out img, out errMsg) != 0) { HTUi.PopError("采集图像失败:" + errMsg); return; } ShowImage(htWindow, img, null); }
private void btnTool_Click(object sender, EventArgs e) { if (HTM.LoadUI() < 0) { HTUi.PopError("打开轴调试助手界面失败"); } }
private void btnSaveUVXY2D_Click(object sender, EventArgs e) { this.btnSaveUVXY2D.Focus(); HTuple uvHxy = null; try { GetDGVValue(dgvUVXY2D, new int[] { 0, 1 }, new int[] { 0, 2 }, ref uvHxy); } catch (Exception) { MessageBox.Show("获取2D相机UV-XY矩阵数据失败!"); return; } //保存 string filePath = _station.SystemUV2XYDir + "\\Camera_" + _station.SelectedIndex + "\\" + "UV2XY" + ".dat"; try { string dir = Directory.GetParent(filePath).FullName; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } tool_vision.save_hom2d(filePath, uvHxy); _station.List_UV2XYResult[_station.SelectedIndex] = uvHxy; } catch (Exception ex) { HTUi.PopError("2D相机UV-XY矩阵保存失败!\n" + ex.ToString()); return; } HTUi.TipHint("2D相机UV-XY矩阵保存成功!"); HTLog.Info("2D相机UV-XY矩阵保存成功!"); }
private async void btnUVXYCalib2D_Click(object sender, EventArgs e) { try { if (this.aoiModels == null) { this.aoiModels = new Model(); string folderPath = _station.SystemUV2XYDir + "\\Camera_" + _station.SelectedIndex; if (!this.aoiModels.ReadModel(folderPath)) { HTUi.PopError("未创建2D标定模板!"); return; } HOperatorSet.ReadTuple(folderPath + "\\Pos_Uv2Xy.tup", out Pos_Uv2Xy); } //设定走位范围 CalibSearchRange csRangeFrm = new CalibSearchRange(); if (csRangeFrm.ShowDialog() != DialogResult.OK) { return; } double xyRange = csRangeFrm.Range; //条件:标定板中心在2D相机视野中心位置时的chuck x y的粗略位置已知,即 //料片视野为左下芯片为中心作为初始点位 _station.operation.XUv2xy = Pos_Uv2Xy[0]; //App.obj_Chuck.ref_x + ((App.obj_Pdt.BlockNumber - 1) * App.obj_Pdt.BlockSpace + (App.obj_Pdt.ColumnNumber - 1) * App.obj_Pdt.ColomnSpace)/2 + App.obj_Pdt.RightEdge; _station.operation.YUv2xy = Pos_Uv2Xy[1]; //App.obj_Chuck.ref_y + App.obj_Pdt.TopEdge + (App.obj_Pdt.RowNumber - 1) * App.obj_Pdt.RowSpace/2; _station.operation.ZUv2xy = Pos_Uv2Xy[2]; //App.obj_Pdt.ZFocus; HTuple uvHxy = new HTuple(); bool status = await Task.Run(() => { return(_station.operation.calibUV2XY ( _station.SelectedIndex, ref uvHxy, this.aoiModels, xyRange, 20 )); }); if (!status) { HTUi.PopError("2D相机uvHxy标定失败!"); return; } for (int i = 0; i < dgvUVXY2D.RowCount; i++) { dgvUVXY2D.Rows[i].Cells[0].Value = uvHxy[i * 3].D; dgvUVXY2D.Rows[i].Cells[1].Value = uvHxy[i * 3 + 1].D; dgvUVXY2D.Rows[i].Cells[2].Value = uvHxy[i * 3 + 2].D; } HTUi.TipHint("2D相机uvHxy标定成功!"); HTLog.Info("2D相机uvHxy标定成功!"); } catch (Exception ex) { HTUi.PopError("2D相机uvHxy标定失败:" + ex.Message); btnSnap.Enabled = true; } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSavePara_Click(object sender, EventArgs e) { try { _station.SaveStationParams(); _station.SaveCfg(); } catch (Exception ex) { HTUi.PopError("保存数据失败!\n" + ex.ToString()); } }
/// <summary> /// UV2XY标定时加载光源、点位和视觉参数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLoadPara_Click(object sender, EventArgs e) { try { //App.obj_light.FlashMultiLight(LightUseFor.ScanPoint1st); HTUi.TipHint("参数加载成功"); } catch (Exception exp) { HTUi.PopError(exp.ToString()); return; } }
private void btnCreateCalibModel2D_Click(object sender, EventArgs e) { try { if (modelForm == null || modelForm.IsDisposed) { modelForm = new Form(); if (this.mTmpFrm != null && !this.mTmpFrm.IsDisposed) { this.mTmpFrm.Dispose(); } this.mTmpFrm = new MainTemplateForm(ToolKits.TemplateEdit.MainTemplateForm.TemplateScence.Match, this.htWindowCalibration, new MainTemplateForm.TemplateParam()); modelForm.Controls.Clear(); this.modelForm.Controls.Add(this.mTmpFrm); this.mTmpFrm.Dock = DockStyle.Fill; modelForm.Size = new Size(300, 450); modelForm.TopMost = true; modelForm.Show(); int SH = Screen.PrimaryScreen.Bounds.Height; int SW = Screen.PrimaryScreen.Bounds.Width; modelForm.Location = new Point(SW - modelForm.Size.Width, SH / 8); Task.Run(() => { while (!mTmpFrm.WorkOver) { Thread.Sleep(200); } HTUi.TipHint("创建标定模板完成!"); HTLog.Info("创建标定模板完成!"); modelForm.Close(); }); } else { modelForm.Activate(); } } catch (Exception ex) { HTUi.PopError("创建标定模板失败:" + ex.Message); btnSnap.Enabled = true; } }
/// <summary> /// 配置轨道宽度 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnFixTrack_Click(object sender, EventArgs e) { switch (HTUi.PopSelect("请在以下选项中选择配置导轨方式。", "直接配置", "轴调试助手", "取消")) { case 0: //保存为选中产品的轨道宽度 break; case 1: if (HTM.LoadUI() < 0) { HTUi.PopError("打开轴调试助手界面失败"); } break; default: break; } }
private void btnSnap_Click(object sender, EventArgs e) { try { if (_station._RunMode == 1) { return; } string errStr = ""; string[] TrigChnName = new string[1]; TrigChnName[0] = "TrigX"; //if(_station.operation.SWPosTrig(TrigChnName,out errStr) !=0) //{ // HTUi.PopError(errStr); // btnSnap.Enabled = true; // return; //} //4. 取图 HOperatorSet.GenEmptyObj(out Image); if (_station.operation.CaputreOneImage(_station.CamereDev[_station.SelectedIndex], "Halcon", out Image, out errStr) != 0) { HTUi.PopError(errStr); btnSnap.Enabled = true; return; } ShowImage(htWindowCalibration, Image, null); if (errStr != "") { HTUi.PopError(errStr); btnSnap.Enabled = true; } } catch (Exception ex) { HTUi.PopError("单张采集失败:" + ex.Message); btnSnap.Enabled = true; } }
public void SetupUI() { try { if (Created) { _station.InitStationParams(); dgvUVXY2D.Rows.Clear(); InitUV2XYParam(); dgvUVXY2D.Rows.Add(); if (_station.Num_Camera > _station.SelectedIndex) { if (_station.List_UV2XYResult[_station.SelectedIndex].Type != HTupleType.INTEGER) { SetDGVValue(ref dgvUVXY2D, _station.List_UV2XYResult[_station.SelectedIndex]); } } } } catch (Exception ex) { HTUi.PopError("" + ex.ToString()); } }
/// <summary> /// 已测试 /// 软触发拍照下,计算相机的uv->xy坐标系变换的操作,如large相机 /// 要求之前设置好相机/光源/触发至“合理”状态 /// [x] = [ H1 H2 H3 ] [u] /// [y] = [ H4 H5 H6 ] * [v] /// [1] /// </summary> /// <param name="matUV2XY">uv->xy变换矩阵,将2*3的矩阵按照行堆叠的方式存储为1*6的数组, ref传递方式,需要数组有一个初始值</param> /// <param name="cam">相机控制类,注意曝光时间需要设置合适。软触发拍照建议曝光时间在100ms左右</param> /// <param name="modelID">匹配的ncc模板</param> /// <param name="score_thresh">匹配分数,需要大于0.5</param> /// <param name="axisPara">运动轴信息(xy插补),具体包括轴速,加速度</param> /// <param name="initPoint">初始点,中间仅适用了xy信息,需要保证走到初始点时室内内包含完整的,可匹配的model</param> /// <param name="lightInd">软触发拍照对应的光源id,需要事先设置好对应光源合适的时间</param> /// <param name="axisInd">x,y轴号</param> /// <param name="xyRange">计算变换时,以初始点为中心,随机走位的范围,单位mm</param> /// <param name="nPoints">随机走位多少个点</param> /// <param name="lightDelay">软触发拍照光源的延迟,单位us,典型值10us</param> /// <param name="timeOut">单次运动的延迟,单位ms</param> /// <param name="winID">(调试用)显示窗口ID</param> /// <returns>操作是否成功</returns> public bool calibUV2XY ( int camInd, ref HTuple matUV2XY, Model model, double xyRange, int nPoints = 20 ) { //if (model.ReadModel(CalibrUV2XYModelPath) == false) //{ // HTUi.PopError("加载标定模版失败!"); // return false; //} string errMsg = ""; if (_station.operation.MultiAxisMove(_station.AxisXYZ, new double[] { _xUv2xy, _yUv2xy, _zUv2xy }, true, out errMsg) != 0) { HTUi.PopError("无法移动至标定点位置!"); return(false); } Thread.Sleep(200); // App.obj_light.FlashMultiLight(LightUseFor.ScanPoint1st); //if(_station.operation.SWPosTrig(_station.AxisX,out errMsg)!=0) //{ // HTUi.PopError(errMsg); // return false; //} Thread.Sleep(10); if (image != null) { image.Dispose(); } HOperatorSet.GenEmptyObj(out image); if (_station.operation.CaputreOneImage(_station.CamereDev[camInd], "Halcon", out image, out errMsg) != 0) { HTUi.PopError("采集图像失败:" + errMsg); return(false); } //3. match HTuple u, v, angle; bool status = matchModel(camInd, ref image, model, out u, out v, out angle); HObject showRegion = new HObject(); showRegion.Dispose(); HOperatorSet.GenCrossContourXld(out showRegion, u, v, 512, 0); _station._rtUi._fm.ShowImage(_station._rtUi._fm.htWindowCalibration, image, showRegion); //image.Dispose(); if (!status) { HTUi.PopError("获取匹配初始位置图像失败"); return(false); } HTuple xArr = new HTuple(), yArr = new HTuple(), uArr = new HTuple(), vArr = new HTuple(); Random rand = new Random(); //4. for ... snap , match, add <u,v,x,y> for (int i = 0; i < nPoints; i++) { DateTime t1 = DateTime.Now; //大于或等于 0.0 且小于 1.0 的双精度浮点数 double x = (rand.NextDouble() - 0.5) * xyRange + _xUv2xy; double y = (rand.NextDouble() - 0.5) * xyRange + _yUv2xy; if (_station.operation.MultiAxisMove(new string[] { _station.AxisXYZ[0], _station.AxisXYZ[1], }, new double[] { x, y }, true, out errMsg) != 0) { HTUi.PopError("无法移动至标定点位置!"); return(false); } Thread.Sleep(200);; //if (_station.operation.SWPosTrig(_station.AxisX, out errMsg) != 0) //{ // HTUi.PopError(errMsg); // return false; //} Thread.Sleep(10); if (image != null) { image.Dispose(); } HOperatorSet.GenEmptyObj(out image); if (_station.operation.CaputreOneImage(_station.CamereDev[camInd], "Halcon", out image, out errMsg) != 0) { HTUi.PopError("采集图像失败:" + errMsg); return(false); } if (matchModel(camInd, ref image, model, out u, out v, out angle)) //found something { xArr.Append(x); yArr.Append(y); uArr.Append(u); vArr.Append(v); } showRegion.Dispose(); HOperatorSet.GenCrossContourXld(out showRegion, u, v, 512, 0); _station._rtUi._fm.ShowImage(_station._rtUi._fm.htWindowCalibration, image, showRegion); //image.Dispose(); } if (xArr.Length < 10) { HTUi.PopError("有效点数不够"); return(false); } //5. least square estimation Matrix <double> In = Matrix <double> .Build.Dense(3, xArr.Length, 1.0); //by default In[2,:] = 1.0 Matrix <double> Out = Matrix <double> .Build.Dense(2, xArr.Length); Out.SetRow(0, xArr.ToDArr()); Out.SetRow(1, yArr.ToDArr()); In.SetRow(0, uArr.ToDArr()); In.SetRow(1, vArr.ToDArr()); Matrix <double> A = vec2Mat(In, Out); //6. move to center of uv space double[] aArr = A.ToRowWiseArray(); //need to be tested _station._calibrationUV2XYParameter = string.Join(",", aArr.ToArray()); //parse if (matUV2XY == null) { matUV2XY = new HTuple(); } for (int i = 0; i < 6; i++) { matUV2XY.Append(aArr[i]); } return(true); }
/// <summary> /// 重命名产品 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRename_Click(object sender, EventArgs e) { string souce = (string)lbxProductCategory.SelectedValue; if (souce == (string)JFHubCenter.Instance.SystemCfg.GetItemValue("CurrentID")) { HTUi.PopError("当前产品名称不可修改"); return; } String newname = ""; if (!MSG.Inputbox("new", "rename", out newname)) { return; } if (newname == string.Empty) { HTUi.PopWarn("命名错误,名字不能为空"); return; } var pdtlist = GetProductList(); foreach (var pdt in pdtlist) { if (pdt == newname) { HTUi.PopWarn("命名冲突,当前产品已存在"); return; } } //重新命名Recipe manager中recipe ID JFDLAFProductRecipe jFDLAFProductRecipe = JFHubCenter.Instance.RecipeManager.GetRecipe("Product", (string)souce) as JFDLAFProductRecipe; if (jFDLAFProductRecipe == null) { HTUi.PopWarn("Recipe Manager中不存在recipe:" + souce); return; } jFDLAFProductRecipe.Categoty = "Product"; jFDLAFProductRecipe.ID = newname; JFDLAFBoxRecipe jFDLAFBoxRecipe = JFHubCenter.Instance.RecipeManager.GetRecipe("Box", (string)souce) as JFDLAFBoxRecipe; if (jFDLAFBoxRecipe == null) { HTUi.PopWarn("Recipe Manager中不存在recipe:" + souce); return; } jFDLAFBoxRecipe.Categoty = "Box"; jFDLAFBoxRecipe.ID = newname; JFHubCenter.Instance.RecipeManager.AddRecipe(jFDLAFProductRecipe.Categoty, jFDLAFProductRecipe.ID, jFDLAFProductRecipe as IJFRecipe); JFHubCenter.Instance.RecipeManager.AddRecipe(jFDLAFBoxRecipe.Categoty, jFDLAFBoxRecipe.ID, jFDLAFBoxRecipe as IJFRecipe); JFHubCenter.Instance.RecipeManager.RemoveRecipe("Product", souce); JFHubCenter.Instance.RecipeManager.RemoveRecipe("Box", souce); JFHubCenter.Instance.RecipeManager.Save(); ChangeProdcutName(newname, souce); HTUi.TipHint("重命名成功!"); lbxProductCategory.DataSource = GetProductList(); return; }
private async void btnSaveCalibModel2D_Click(object sender, EventArgs e) { if (!this.mTmpFrm.tmpResult.createTmpOK) { HTUi.PopError("创建标定模板失败!"); return; } Form_Wait.ShowForm(); bool result = await Task.Run(() => { try { this.aoiModels = new Model(); this.aoiModels.Dispose(); this.aoiModels.showContour = this.mTmpFrm.tmpResult.showContour.CopyObj(1, -1); this.aoiModels.defRows = this.mTmpFrm.tmpResult.defRows; this.aoiModels.defCols = this.mTmpFrm.tmpResult.defCols; this.aoiModels.modelType = this.mTmpFrm.tmpResult.modelType; this.aoiModels.modelID = Vision.CopyModel(this.mTmpFrm.tmpResult.modelID, this.mTmpFrm.tmpResult.modelType); this.aoiModels.scoreThresh = this.mTmpFrm.mte_TmpPrmValues.Score; this.aoiModels.angleStart = this.mTmpFrm.mte_TmpPrmValues.AngleStart; this.aoiModels.angleExtent = this.mTmpFrm.mte_TmpPrmValues.AngleExtent; //保存至硬盘 string folderPath = _station.SystemUV2XYDir + "\\Camera_" + _station.SelectedIndex; _station.CalibrUV2XYModelPath = folderPath; if (!this.aoiModels.WriteModel(folderPath)) { HTLog.Error("保存标定模板失败!"); HTUi.PopError("保存标定模板失败!"); return(false); } string errMsg = ""; Pos_Uv2Xy = new HTuple(); double xUV2xy = 0, yUV2xy = 0, zUV2xy = 0; if (!_station.GetAxisPosition(_station.AxisXYZ[0], out xUV2xy, out errMsg)) { HTLog.Error(errMsg + ":保存获取X轴反馈坐标失败!"); HTUi.PopError(errMsg + ":保存获取X轴反馈坐标失败!"); return(false); } if (!_station.GetAxisPosition(_station.AxisXYZ[1], out yUV2xy, out errMsg)) { HTLog.Error(errMsg + ":保存获取Y轴反馈坐标失败!"); HTUi.PopError(errMsg + ":保存获取Y轴反馈坐标失败!"); return(false); } if (!_station.GetAxisPosition(_station.AxisXYZ[2], out zUV2xy, out errMsg)) { HTLog.Error(errMsg + ":保存获取Z轴反馈坐标失败!"); HTUi.PopError(errMsg + ":保存获取Z轴反馈坐标失败!"); return(false); } _station.operation.YUv2xy = yUV2xy; _station.operation.XUv2xy = xUV2xy; _station.operation.ZUv2xy = zUV2xy; Pos_Uv2Xy.Append(_station.operation.XUv2xy); Pos_Uv2Xy.Append(_station.operation.YUv2xy); Pos_Uv2Xy.Append(_station.operation.ZUv2xy); HOperatorSet.WriteTuple(Pos_Uv2Xy, folderPath + "\\Pos_Uv2Xy.tup"); HTUi.TipHint("保存标定模板成功!"); HTLog.Info("保存标定模板成功!"); return(true); } catch (Exception ex) { HTLog.Error(ex.ToString()); HTUi.PopError(ex.ToString()); return(false); } }); Form_Wait.CloseForm(); if (!result) { HTLog.Error("保存标定模板失败!"); HTUi.PopError("保存标定模板失败!"); return; } HTUi.TipHint("保存标定模板成功!"); HTLog.Info("保存标定模板成功!"); }