You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
18 KiB
C#

using HalconDotNet;
using Newtonsoft.Json.Linq;
using Rs.Controls;
using Rs.DataAccess;
using Rs.Framework;
using Rs.Motion;
using Rs.MotionPlat.Commom;
using Rs.MotionPlat.Flow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Rs.MotionPlat.Recipe
{
public partial class TurnoverLocationRecipe : BaseForm
{
SqliteHelper db = new SqliteHelper();
DataTable dt = new DataTable();
public TurnoverLocationRecipe()
{
InitializeComponent();
cameraTemplate1.ItemName = "TurnoverTray";
cameraTemplate1.SetFindShapeMode( EFindShapeMode.SelfDefine );
cameraTemplate1.FindShapeEvent += CameraTemplate1_FindShapeEvent;
}
private void CameraTemplate1_FindShapeEvent(ChoiceTech.Halcon.Control.HWindow_Final arg1, HalconDotNet.HObject arg2)
{
/*
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
reduce_domain (Image21212121, Rectangle, ImageReduced)
threshold (ImageReduced, Region, 0, 100)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 3000, 4000)
select_shape (SelectedRegions, SelectedRegions1, 'circularity', 'and', 0.8, 1)
smallest_circle (SelectedRegions1, Row, Column, Radius)
gen_circle (Circle, Row, Column, Radius)
*/
//HOperatorSet.GenRectangle1(out HObject rectSearch, 427, 555, 1097, 799);
//HOperatorSet.ReduceDomain(arg2, rectSearch, out HObject imageReduced);
HOperatorSet.Threshold(arg2, out HObject region, 0, 100);
HOperatorSet.Connection(region, out HObject connectRegion);
if (connectRegion.IsInitialized() && connectRegion.CountObj() > 0)
{
HOperatorSet.SelectShape(connectRegion, out HObject select1Regions, "area", "and", 400, 600);
HOperatorSet.SelectShape(select1Regions, out HObject select2Regions, "circularity", "and", 0.8, 1);
HOperatorSet.SmallestCircle(select2Regions, out HTuple circleRow, out HTuple circleColumn, out HTuple circleRadius);
if (circleRadius.Type.ToString() !="EMPTY" && circleRadius.DArr.Length >= 1)
{
HOperatorSet.GenCrossContourXld(out HObject cross, circleRow, circleColumn, 20, 0);
arg1.DispObj(cross, "green");
}
}
}
private void BindPoints()
{
string querySql = $"select * from TrayBasePoints where trayname='Turnover'";
DataTable dtBasePoints = db.GetDataTable(querySql);
if(ObjectHelper.IsNotNullorEmpty(dtBasePoints))
{
dgvBasePoints.DataSource = dtBasePoints;
}
querySql = $"select * from TrayRunPoints where trayname='Turnover'";
DataTable dtRunPoints = db.GetDataTable(querySql);
if (ObjectHelper.IsNotNullorEmpty(dtRunPoints))
{
dgvRunPoints.DataSource = dtRunPoints;
}
}
private void btnSavePoint_Click(object sender, EventArgs e)
{
Button btnTeach = (Button)sender;
string name = btnTeach.Name.Replace("btnSaveTurnoverTray", "");
Control txt = ControlManager.FindControl(groupBox1, "txtTurnoverTray" + name);
if (txt != null)
{
string updateSql = $"update NormalTray set {name}={txt.Text} where trayname='{dt.Rows[0]["trayname"]}'";
if (db.ExecuteNonQuery(updateSql) > 0)
{
Msg.ShowInfo("save point ok!");
}
}
}
private void TurnoverLocationRecipe_Load(object sender, EventArgs e)
{
dgvSlotOffset.AutoGenerateColumns = false;
string querySql = "select * from NormalTray where trayname='Turnover'";
dt=db.GetDataTable(querySql);
if(ObjectHelper.IsNotNullorEmpty(dt))
{
DataRow dr = dt.Rows[0];
foreach(DataColumn dc in dt.Columns)
{
string columname=dc.ColumnName;
Control txt = ControlManager.FindControl(groupBox1, "txtTurnoverTray" + columname);
if(txt != null)
{
txt.Text = dr[columname].ToString();
}
}
}
dgvBasePoints.AutoGenerateColumns = false;
dgvRunPoints.AutoGenerateColumns = false;
BindPoints();
querySql = $"select * from SlotOffset where RecipeName='{GlobalVar.CurRecipe}' and TrayName='Turnover'";
DataTable dtSlotOffsets = db.GetDataTable(querySql);
dgvSlotOffset.DataSource = dtSlotOffsets;
}
private void btnCreateMatrix_Click(object sender, EventArgs e)
{
if (ObjectHelper.IsNotNullorEmpty(dt))
{
string deleteSql = $"delete from TrayBasePoints where trayname='{dt.Rows[0]["TrayName"]}'";
int rowEffect = db.ExecuteNonQuery(deleteSql);
deleteSql = $"delete from TrayRunPoints where trayname='{dt.Rows[0]["TrayName"]}'";
rowEffect = db.ExecuteNonQuery(deleteSql);
if (rowEffect>0)
{
int rowNum = int.Parse(dt.Rows[0]["RowNum"].ToString());
int colNum = int.Parse(dt.Rows[0]["ColumnNum"].ToString());
int index = 1;
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < colNum; j++)
{
double xBase = double.Parse(dt.Rows[0]["MarkX1"].ToString()) + (j * double.Parse(dt.Rows[0]["SlotOffsetDistX"].ToString()));
double yBase = double.Parse(dt.Rows[0]["MarkY1"].ToString()) + (i * double.Parse(dt.Rows[0]["SlotOffsetDistY"].ToString()));
double xRun = double.Parse(dt.Rows[0]["MarkX1"].ToString()) + double.Parse(dt.Rows[0]["Mark2CenterDistX"].ToString()) + (j * double.Parse(dt.Rows[0]["SlotOffsetDistX"].ToString()));
double yRun = double.Parse(dt.Rows[0]["MarkY1"].ToString()) + double.Parse(dt.Rows[0]["Mark2CenterDistY"].ToString()) + (i * double.Parse(dt.Rows[0]["SlotOffsetDistY"].ToString()));
string insertBaseSql = $"insert into TrayBasePoints(trayid,recipename,trayname,slotindex,x,y) values({dt.Rows[0]["id"]},'{GlobalVar.CurRecipe}', '{dt.Rows[0]["TrayName"]}',{index},{xBase},{yBase})";
string insertRunSql = $"insert into TrayRunPoints(trayid,recipename,trayname,slotindex,x,y) values({dt.Rows[0]["id"]},'{GlobalVar.CurRecipe}', '{dt.Rows[0]["TrayName"]}',{index},{xRun},{yRun})";
db.ExecuteNonQuery(insertBaseSql);
db.ExecuteNonQuery(insertRunSql);
index++;
}
}
}
}
BindPoints();
}
private void btnTeach_Click(object sender, EventArgs e)
{
Button btnTeach = (Button)sender;
string name = btnTeach.Name.Replace("btnTeach","");
Control txt = ControlManager.FindControl(groupBox1, "txt" + name);
if(txt != null)
{
double pos = Ops.GetCurPosition(txt.Tag.ToString());
txt.Text = pos.ToString("0.000");
if(ObjectHelper.IsNotNullorEmpty(dt))
{
string updateSql = $"update NormalTray set {txt.Name.Replace("txtTurnoverTray","")}={pos} where trayname='{dt.Rows[0]["trayname"]}'";
if(db.ExecuteNonQuery(updateSql)>0)
{
string querySql = "select * from NormalTray where trayname='Turnover'";
dt = db.GetDataTable(querySql);
Msg.ShowInfo("teach point ok!");
}
}
}
}
private void btnMove_Click(object sender, EventArgs e)
{
Button btnTeach = (Button)sender;
string name = btnTeach.Name.Replace("btnMove", "");
Control txt = ControlManager.FindControl(groupBox1, "txt" + name);
if (txt != null)
{
double targetPos = double.Parse(txt.Text);
DialogResult dr = Msg.ShowQuestion($"are you sure to move axis {txt.Tag} to postion at:{targetPos}?");
if(dr == DialogResult.OK)
{
Motion.ErrorCode errCode = AxisControl.GetAxis(txt.Tag.ToString()).MovePos(targetPos, 4);
if(errCode != Motion.ErrorCode.Ok)
{
Msg.ShowError($"axis {txt.Tag.ToString()} move fail {errCode}");
}
}
}
}
private void slotOffsetCtl1_SlotValueChanged(int slotIndex, string xORy, double value)
{
//string deleteSql = $"delete from SlotOffset where recipename='{GlobalVar.CurRecipe}' and trayname='{slotOffsetCtl1.TrayName}' and slotindex={slotIndex}";
//if(db.ExecuteNonQuery(deleteSql)>=0)
//{
// string insertSql = $"insert into slotoffset(recipename,trayname,slotindex,offsetx,offsety) values('{GlobalVar.CurRecipe}','{slotOffsetCtl1.TrayName}',{slotIndex},,)";
//}
//string querySql = $"select * from slotoffset where recipename='{GlobalVar.CurRecipe}' and trayname='{slotOffsetCtl1.TrayName}'";
//DataTable dt = db.GetDataTable(querySql);
//if(ObjectHelper.IsNotNullorEmpty(dt))
//{
// string updateSql = "";
// if (xORy.ToLower() == "x")
// {
// updateSql = $"update slotoffset set offsetx={value} where recipename='{GlobalVar.CurRecipe}' and trayname='{slotOffsetCtl1.TrayName}'";
// }
// else
// {
// updateSql = $"update slotoffset set offsety={value} where recipename='{GlobalVar.CurRecipe}' and trayname='{slotOffsetCtl1.TrayName}'";
// }
// db.ExecuteNonQuery(updateSql);
//}
//else
//{
// string insertSql = "";
// if (xORy.ToLower() == "x")
// {
// insertSql = $"insert into slotoffset(recipename,trayname,slotindex,offsetx) values('{GlobalVar.CurRecipe}','{slotOffsetCtl1.TrayName}',{slotIndex},{value})";
// }
// else
// {
// insertSql = $"insert into slotoffset(recipename,trayname,slotindex,offsety) values('{GlobalVar.CurRecipe}','{slotOffsetCtl1.TrayName}',{slotIndex},{value})";
// }
// db.ExecuteNonQuery(insertSql);
//}
}
private TraySlot selectedSlot;
private void rsTray1_SlotClickEvent(TraySlot arg1, MouseEventArgs arg2)
{
selectedSlot = arg1;
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
private void goToolStripMenuItem_Click(object sender, EventArgs e)
{
if (selectedSlot != null)
{
int nozzleIndex = 0;
string querySql = $"select * from TrayRunPoints where trayid={dt.Rows[0]["id"]} and slotindex={selectedSlot.Index}";
DataTable dtGoPosition = db.GetDataTable(querySql);
if (ObjectHelper.IsNotNullorEmpty(dtGoPosition))
{
double targetX = double.Parse(dtGoPosition.Rows[0]["X"].ToString());
double targetY = double.Parse(dtGoPosition.Rows[0]["Y"].ToString());
ToolStripMenuItem subMenu = sender as ToolStripMenuItem;
ToolStripMenuItem parentMenu =(ToolStripMenuItem) subMenu.OwnerItem;
if (parentMenu.Text.IndexOf("Nozzle") >= 0)
{
nozzleIndex = int.Parse(parentMenu.Text.Replace("Nozzle", "").Replace("GO", ""));
SlotPoint dist = TrayPointManager.GetDistToNozzle1(nozzleIndex);
targetX += dist.X;
targetY += dist.Y;
}
DialogResult dr = Msg.ShowQuestion($"Are you sure to move loadx to {targetX},loady to {targetY}?");
if (dr == DialogResult.OK)
{
Motion.ErrorCode errCode = AxisControl.GetAxis("LoadX").MovePos(targetX, 4);
if (errCode > Motion.ErrorCode.Ok)
{
Msg.ShowError($"axis loadx move fail,ret={errCode}");
return;
}
errCode = AxisControl.GetAxis("LoadY").MovePos(targetY, 4);
if (errCode > Motion.ErrorCode.Ok)
{
Msg.ShowError($"axis loady move fail,ret={errCode}");
return;
}
if(subMenu.Text.IndexOf("TakeProduct")>=0)
{
TakeProduct(selectedSlot.Index, nozzleIndex);
}
else if(subMenu.Text.IndexOf("DumpProduct")>=0)
{
DumpProduct(selectedSlot.Index, nozzleIndex);
}
}
}
}
}
/// <summary>
/// 取料
/// </summary>
private void TakeProduct(int slotIndex,int nozzleIndex)
{
ErrorCode errCode = ErrorCode.Ok;
Task.Run(() => {
//开吸嘴真空
//关闭周转盘真空吸
//打开周转盘真空破
//关闭真空破
while(true)
{
if (Ops.IsStop("LoadX") && Ops.IsStop("LoadY"))
break;
Thread.Sleep(10);
}
DialogResult dr = Msg.ShowQuestion("Are you sure to take product?", MessageBoxButtons.OKCancel);
if (dr == DialogResult.OK)
{
errCode = AxisControl.GetAxis($"NozzleZ{nozzleIndex}").MovePos(SysConfigParam.GetValue<double>($"TurnoverNozzle{nozzleIndex}TakeHeight"), GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"NozzleZ{nozzleIndex}"))
{
Thread.Sleep(10);
}
Ops.On($"{nozzleIndex}号吸嘴真空吸电磁阀");
Thread.Sleep(200);
Ops.Off($"周转盘{slotIndex}号穴位真空吸");
Thread.Sleep(100);
Ops.On($"周转盘{slotIndex}号穴位真空破");
Thread.Sleep(100);
Ops.Off($"周转盘{slotIndex}号穴位真空破");
errCode = AxisControl.GetAxis($"NozzleZ{nozzleIndex}").MovePos(0, GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"NozzleZ{nozzleIndex}"))
{
Thread.Sleep(10);
}
}
});
}
/// <summary>
/// 放料
/// </summary>
private void DumpProduct(int slotIndex, int nozzleIndex)
{
ErrorCode errCode = ErrorCode.Ok;
Task.Run(() => {
//开吸嘴真空
//关闭周转盘真空吸
//打开周转盘真空破
//关闭真空破
while (true)
{
if (Ops.IsStop("LoadX") && Ops.IsStop("LoadY"))
break;
Thread.Sleep(10);
}
DialogResult dr = Msg.ShowQuestion("Are you sure to dump product?", MessageBoxButtons.OKCancel);
if (dr== DialogResult.OK)
{
errCode = AxisControl.GetAxis($"NozzleZ{nozzleIndex}").MovePos(SysConfigParam.GetValue<double>($"TurnoverNozzle{nozzleIndex}TakeHeight"), GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"NozzleZ{nozzleIndex}"))
{
Thread.Sleep(10);
}
Ops.On($"周转盘{slotIndex}号穴位真空吸");
Thread.Sleep(100);
Ops.Off($"{nozzleIndex}号吸嘴真空吸电磁阀");
Thread.Sleep(50);
Ops.On($"{nozzleIndex}号吸嘴真空破电磁阀");
Thread.Sleep(100);
Ops.Off($"{nozzleIndex}号吸嘴真空破电磁阀");
errCode = AxisControl.GetAxis($"NozzleZ{nozzleIndex}").MovePos(0, GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"NozzleZ{nozzleIndex}"))
{
Thread.Sleep(10);
}
}
});
}
private void dgvSlotOffset_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
string slotindex = dgvSlotOffset.Rows[e.RowIndex].Cells[0].Value.ToString();
string offsetx = dgvSlotOffset.Rows[e.RowIndex].Cells[1].Value.ToString();
string offsety = dgvSlotOffset.Rows[e.RowIndex].Cells[2].Value.ToString();
string offseta = dgvSlotOffset.Rows[e.RowIndex].Cells[3].Value.ToString();
string updateSql = $"update slotoffset set offsetx={offsetx}, offsety={offsety},offseta={offseta} where recipename='{GlobalVar.CurRecipe}' and trayname='Turnover' and slotindex={slotindex}";
db.ExecuteNonQuery(updateSql);
TurnoverSlotOffset.Reload();
}
}
}
}