FrmOrgConfig.cs 9.5 KB

using log4net; 
using URSoldering.Common;
using URSoldering.DeviceLibrary;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;

using System.Windows.Forms;
using URSoldering.Common.util;
using System.IO;
using System.Drawing.Drawing2D;
using URSoldering.LoadCSVLibrary;
using System.Threading.Tasks;
using System.Timers;
using HalconDotNet;

namespace URSoldering.Client
{
    public partial class FrmOrgConfig : FrmBase
    { 
        private bool isAuto = false;
        private bool isConnect = false;
       
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public FrmOrgConfig( )
        {
            InitializeComponent();
        }
        private System.Timers.Timer timer1 = new System.Timers.Timer();
        
        private void FrmBoardInfo_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            

            //此界面打开时,不能打开自动焊接
            
            if (WeldRobotBean.ISRun)
            {
                LogUtil.error("进入焊接界面,发现WeldRobotBean自动运行中,关闭自动运行");
                WeldRobotBean.StopRun();
            }
            if (EpsonDevice.IsRun)
            {
                EpsonDevice.FreeAxis();
              
                EpsonDevice.GetPosition(AfterGet);
            }
            else
            {
                //btnChange.Text = "机器人连接中。。。";
                RobotStatus(false);
            }
            isAuto = false;

            timer1.Interval = 1000;
            timer1.AutoReset = true;
            timer1.Elapsed += timer_Elapsed;
            timer1.Start();
            timer2.Start(); 
          
        }
       
        private void AfterMove(string result)
        {

        }
        private void AfterGet(double x, double y, double z, double u, int hand)
        {
            PointValue point = new PointValue(x, y, u, z, hand);
            //PointValue point = EpsonDevice.LastPoint;
            this.txtRobotX.Text = point.X.ToString();
            this.txtRobotY.Text = point.Y.ToString();
            this.txtRobotU.Text = point.U.ToString();
            this.txtRobotZ.Text = point.Z.ToString();

            string msg = WeldRobotBean.CheckEpsonPoint(point);
          
            if (point.HandDir.Equals(1))
            {
                cmbHand.SelectedIndex = 0;
            }
            else
            {
                cmbHand.SelectedIndex = 1;
            }
            if (point.X != 0 && isConnect == false)
            {
                EpsonDevice.FreeAxis();
                isConnect = true; 
                RobotStatus(true);
            }
        }
 
        private void RobotStatus(bool isConnect)
        { 
        }

        
        private bool isRun = false;
        private void timer_Elapsed(object sender, EventArgs e)
        {
            if (!EpsonDevice.IsRun && isRun.Equals(false))
            {
                Task.Factory.StartNew(delegate ()
                {
                    string result = EpsonDevice.Start();
                    if (result != "")
                    {
                        LogUtil.info("连接失败:" + result);
                    }
                    else
                    {
                        EpsonDevice.FreeAxis();
                        isAuto = false;
                    }
                });

                isRun = true;
            }
            else
            {
                //EpsonControl.FreeAxis();
                EpsonDevice.GetPosition(AfterGet);
            } 
        }

        private void FrmBoardInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            EpsonDevice.LockAxis();
            timer2.Stop();
            timer1.Stop();
            UsbCameraHDevelop.CloseAllCamera();
        }
         
        private void btnRead_Click(object sender, EventArgs e)
        {
            EpsonDevice.GetPosition(AfterGet);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (EpsonDevice.IsRun)
            {
               
            }
        }
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (this.IsDisposed)
            {
                this.Close();
            }
        }

        private void btnSetOrigin_Click(object sender, EventArgs e)
        {
            saveOriginInfo();
        }
        private void saveOriginInfo()
        { 
            ConfigAppSettings.SaveValue(Setting_Init.BOARD_ORIGIN_X, txtRobotX.Text);
            ConfigAppSettings.SaveValue(Setting_Init.BOARD_ORIGIN_Y, txtRobotY.Text);
        } 
 
        private void btnGoHome_Click(object sender, EventArgs e)
        {
            if (isAuto == false)
            {
                MessageBox.Show("请先切换到自动模式!");
                return;
            } 
            RobotBean.KNDIOMove(IO_Type.SendWire_Down, IO_VALUE.LOW); 
            RobotBean.KNDIOMove(IO_Type.SendWire_Up, IO_VALUE.HIGH); 

            double x = WeldRobotBean.EpsonOrgX;
            double y = WeldRobotBean.EpsonOrgY;
            double z = WeldRobotBean.EpsonOrgZ;
            double u = WeldRobotBean.EpsonOrgU;
            int hand = WeldRobotBean.EpsonOrgHandDir;
            if (x != 0 && y != 0 && z != 0 && u != 0)
            {
                EpsonDevice.MoveTo(x, y, z, u, false, hand,AfterMove);
            }
            else
            {
                MessageBox.Show("原点坐标无效!");
            }
        }

        private void btnSaveHome_Click(object sender, EventArgs e)
        {
            double orgx = FormUtil.getDoubleValue(txtRobotX);
            double orgy = FormUtil.getDoubleValue(txtRobotY);
            double orgu = FormUtil.getDoubleValue(txtRobotU);
            double orgz = FormUtil.getDoubleValue(txtRobotZ);
            //判断原点的Z轴
            if (orgz > EpsonDevice.Robot_LIM_Z)
            {
                MessageBox.Show("保存失败,原点Z坐标(" + orgz + ")不能高于最高Z点(" + EpsonDevice.Robot_LIM_Z + ")");
                return;
            } 

            int hand =GetHand();
            DialogResult result = MessageBox.Show("是否将当前位置X:" + orgx + ",Y:" + orgy + ",U:" + orgu + ",Z:" + orgz + "," + cmbHand.Text + "保存为原点?", "提示", MessageBoxButtons.YesNo);
            if (result.Equals(DialogResult.Yes))
            {
                WeldRobotBean.UpdateOrgPoint(orgx, orgy, orgu, orgz, hand); 
                MessageBox.Show("保存原点成功!");
            }
        } 

        private int GetHand()
        {
            if (cmbHand.SelectedIndex >= 0)
            {
                return cmbHand.SelectedIndex + 1;
            }
            else
            {
                return 0;
            }
        }
         

        
        private void btnSetClear1_Click(object sender, EventArgs e)
        {
            double clearx = FormUtil.getDoubleValue(txtRobotX);
            double cleary = FormUtil.getDoubleValue(txtRobotY);
            double clearu = FormUtil.getDoubleValue(txtRobotU);
            double clearz = FormUtil.getDoubleValue(txtRobotZ);
            //判断原点的Z轴
            if (clearz > EpsonDevice.Robot_LIM_Z)
            {
                MessageBox.Show("保存失败,清洗点1的Z坐标(" + clearz + ")不能高于最高Z点(" + EpsonDevice.Robot_LIM_Z + ")");
                return;
            }

            int hand = GetHand();
            DialogResult result = MessageBox.Show("是否将当前位置X:" + clearx + ",Y:" + cleary + ",U:" + clearu + ",Z:" + clearz + "," + cmbHand.Text + "保存为清洗点1?", "提示", MessageBoxButtons.YesNo);
            if (result.Equals(DialogResult.Yes))
            {
                WeldRobotBean.UpdateClear1Point(clearx, cleary, clearu, clearz, hand);
                MessageBox.Show("保存清洗点1成功!");
            }
        }

        private void btnSetClear2_Click(object sender, EventArgs e)
        {
            double clearx = FormUtil.getDoubleValue(txtRobotX);
            double cleary = FormUtil.getDoubleValue(txtRobotY);
            double clearu = FormUtil.getDoubleValue(txtRobotU);
            double clearz = FormUtil.getDoubleValue(txtRobotZ);
            //判断原点的Z轴
            if (clearz > EpsonDevice.Robot_LIM_Z)
            {
                MessageBox.Show("保存失败,清洗点2的Z坐标(" + clearz + ")不能高于最高Z点(" + EpsonDevice.Robot_LIM_Z + ")");
                return;
            }

            int hand = GetHand();
            DialogResult result = MessageBox.Show("是否将当前位置X:" + clearx + ",Y:" + cleary + ",U:" + clearu + ",Z:" + clearz + "," + cmbHand.Text + "保存为清洗点2?", "提示", MessageBoxButtons.YesNo);
            if (result.Equals(DialogResult.Yes))
            {
                WeldRobotBean.UpdateClear2Point(clearx, cleary, clearu, clearz, hand);
                MessageBox.Show("保存清洗点2成功!");
            }
        }  

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnMore_Click(object sender, EventArgs e)
        {  
            FrmPwd frmOrgConfig = new FrmPwd();
            this.Visible = false;
            frmOrgConfig.ShowDialog();
            this.Visible = true;
        }
    }
}