using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using ORiN2.interop.CAO; namespace Robot { public partial class frmMain : Form { private CaoEngine m_caoEng; private CaoWorkspaces m_caoWss; private CaoWorkspace m_caoWs; private CaoControllers m_caoCtrls; private CaoVariable m_caoRobBusy; private CaoVariable m_caoCurPos; // delegate function for thread which executes robot actions (move, approach, depart, etc.) private delegate void Call_RobMaster(frmMain frm); // delegate function for changing button enable from other thread private delegate void Call_ButtonEnable(bool bEnable); // delegate function for reading control parameter form other thread private delegate object Call_ReadControl(string strName, string strParam); private volatile bool m_bThreadFlag; private volatile int m_iThreadSwitch; private volatile string m_strParam; private Call_RobMaster m_thRobMaster; private IAsyncResult m_thAR; public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { try { // Create CaoEngine object m_caoEng = new CaoEngine(); // Get Workespaces object m_caoWss = m_caoEng.Workspaces; // Get Workspace(0) m_caoWs = m_caoWss.Item(0); // Get Controllers object m_caoCtrls = m_caoWs.Controllers; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { // <----- Initialize interfaces cboApproachComp.SelectedIndex = 0; cboApproachPath.SelectedIndex = 0; cboApproachType.SelectedIndex = 0; cboChange.SelectedIndex = 0; cboDepartComp.SelectedIndex = 0; cboDepartPath.SelectedIndex = 0; cboDriveIndex.SelectedIndex = 0; cboDrivePath.SelectedIndex = 0; cboMoveComp.SelectedIndex = 0; cboMovePath.SelectedIndex = 0; cboMoveType.SelectedIndex = 0; cboParam.SelectedIndex = 0; cboRotatePath.SelectedIndex = 0; cboRotateSuf.SelectedIndex = 0; // Initialize interfaces -----> } } private void btnConnect_Click(object sender, EventArgs e) { btnDisconnect_Click(sender, e); // Get connection parameter m_strParam = "Server=" + cboParam.Text; CaoController ctrl = null; CaoRobot rob = null; try { // Connect to controller ctrl = m_caoWs.AddController("Sample", "CaoProv.DENSO.RC8", "", m_strParam); // Get robot object rob = ctrl.AddRobot("RobWatch", ""); // Get variable object for watching robot status m_caoRobBusy = rob.AddVariable("@BUSY_STATUS", ""); // Get variable object for watching current position m_caoCurPos = rob.AddVariable("@CURRENT_POSITION", ""); // Start a thread which executes robot actions (move, approach, depart, etc.) m_bThreadFlag = true; m_iThreadSwitch = 0; m_thRobMaster = new Call_RobMaster(Thread_RobMaster); m_thAR = m_thRobMaster.BeginInvoke(this, null, null); ButtonEnable(true); btnCancel.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { // Release robot object if (rob != null) { Marshal.ReleaseComObject(rob); rob = null; } // Release controller object if (ctrl != null) { Marshal.ReleaseComObject(ctrl); ctrl = null; } } } private void btnDisconnect_Click(object sender, EventArgs e) { try { // Stop thread: m_thRobMaster m_bThreadFlag = false; if (m_thRobMaster != null) { // Wait until m_thRobMaster stops while (!m_thAR.IsCompleted) { System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(100); } m_thRobMaster.EndInvoke(m_thAR); m_thRobMaster = null; } // Get the number of connected controllers int CtrlCount = m_caoCtrls.Count; // If the connected controller exists, then disconnect from the controller if (CtrlCount > 0) { // Release variable object Marshal.ReleaseComObject(m_caoRobBusy); m_caoRobBusy = null; Marshal.ReleaseComObject(m_caoCurPos); m_caoCurPos = null; // Clear controller correction m_caoCtrls.Clear(); } ButtonEnable(false); btnCancel.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void frmMain_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) { // Disconnect from controller btnDisconnect_Click(sender, (EventArgs)e); // Release controllers object if (m_caoCtrls != null) { Marshal.ReleaseComObject(m_caoCtrls); m_caoCtrls = null; } // Release workspace object if (m_caoWs != null) { Marshal.ReleaseComObject(m_caoWs); m_caoWs = null; } // Release workspaces object if (m_caoWss != null) { Marshal.ReleaseComObject(m_caoWss); m_caoWss = null; } // Release engine object if (m_caoEng != null) { Marshal.ReleaseComObject(m_caoEng); m_caoEng = null; } } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } private void btnRobMaster_Click(object sender, EventArgs e) { if (sender.GetType() == typeof(Button)) { // Get tag of the pressed button int iTag = 0; if (int.TryParse((string)((Button)sender).Tag, out iTag)) { m_iThreadSwitch = iTag; } } } private void Thread_RobMaster(frmMain frm) { CaoController ctrl = null; CaoRobot rob = null; bool bTmp; int iTmp; float fltTmp; string strTmp, strTmp2, strOpt; try { // Connect to controller ctrl = frm.m_caoWs.AddController("Thread", "CaoProv.DENSO.RC8", "", frm.m_strParam); // Get robot object rob = ctrl.AddRobot("RobAction", ""); // Get arm control authority rob.Execute("Takearm", new object[] { 0, 1 }); while (frm.m_bThreadFlag) { if (frm.m_iThreadSwitch > 0) { Invoke(new Call_ButtonEnable(ButtonEnable), false); switch (frm.m_iThreadSwitch) { case 1: // Move iTmp = (int)Invoke(new Call_ReadControl(ReadControl), "cboMoveComp", "SelectedIndex"); strTmp = (string)Invoke(new Call_ReadControl(ReadControl), "cboMovePath", "Text"); strTmp = " "; strTmp += (string)Invoke(new Call_ReadControl(ReadControl), "cboMoveType", "Text"); strTmp += (string)Invoke(new Call_ReadControl(ReadControl), "txtMoveIndex", "Text"); bTmp = (bool)Invoke(new Call_ReadControl(ReadControl), "chkMoveNext", "Checked"); rob.Move(iTmp + 1, strTmp, bTmp ? "Next" : ""); break; case 2: // Change strTmp = (string)Invoke(new Call_ReadControl(ReadControl), "cboChange", "Text"); strTmp += (string)Invoke(new Call_ReadControl(ReadControl), "txtChange", "Text"); rob.Change(strTmp); break; case 3: // Speed strTmp = (string)Invoke(new Call_ReadControl(ReadControl), "txtSpeed", "Text"); if (float.TryParse(strTmp, out fltTmp)) { rob.Speed(-1, fltTmp); } break; case 4: // Drive strTmp = (string)Invoke(new Call_ReadControl(ReadControl), "cboDrivePath", "Text"); strTmp += "("; strTmp += (string)Invoke(new Call_ReadControl(ReadControl), "cboDriveIndex", "Text"); strTmp += ","; strTmp += (string)Invoke(new Call_ReadControl(ReadControl), "txtDriveValue", "Text"); strTmp += ")"; bTmp = (bool)Invoke(new Call_ReadControl(ReadControl), "chkDriveNext", "Checked"); rob.Execute("DriveEx", new object[] { strTmp, bTmp ? "Next" : "" }); break; case 5: // Rotate strOpt = (string)Invoke(new Call_ReadControl(ReadControl), "cboRotatePath", "Text"); bTmp = (bool)Invoke(new Call_ReadControl(ReadControl), "chkRotateNext", "Checked"); if (bTmp) strOpt += ", Next"; strTmp = (string)Invoke(new Call_ReadControl(ReadControl), "txtRotateValue", "Text"); if (float.TryParse(strTmp, out fltTmp)) { bTmp = (bool)Invoke(new Call_ReadControl(ReadControl), "rdoRotateSuf1", "Checked"); if (bTmp) { strTmp = "V" + (string)Invoke(new Call_ReadControl(ReadControl), "txtRotateSuf1", "Text"); strTmp += ", V" + (string)Invoke(new Call_ReadControl(ReadControl), "txtRotateSuf2", "Text"); strTmp += ", V" + (string)Invoke(new Call_ReadControl(ReadControl), "txtRotateSuf3", "Text"); strTmp2 = "V" + (string)Invoke(new Call_ReadControl(ReadControl), "txtRotateSuf4", "Text"); rob.Rotate(strTmp, fltTmp, strTmp2, strOpt); } else { strTmp = (string)Invoke(new Call_ReadControl(ReadControl), "cboRotateSuf", "Text"); strTmp2 = "V" + (string)Invoke(new Call_ReadControl(ReadControl), "txtRotateSuf4", "Text"); rob.Rotate(strTmp, fltTmp, strTmp2, strOpt); } } break; case 6: // Approach iTmp = (int)Invoke(new Call_ReadControl(ReadControl), "cboApproachComp", "SelectedIndex") + 1; strTmp = (string)Invoke(new Call_ReadControl(ReadControl), "cboApproachType", "Text"); strTmp += (string)Invoke(new Call_ReadControl(ReadControl), "txtApproachIndex", "Text"); strTmp2 = (string)Invoke(new Call_ReadControl(ReadControl), "cboApproachPath", "Text"); strTmp2 += (string)Invoke(new Call_ReadControl(ReadControl), "txtApproachValue", "Text"); bTmp = (bool)Invoke(new Call_ReadControl(ReadControl), "chkApproachNext", "Checked"); rob.Execute("Approach", new object[] { iTmp, strTmp, strTmp2, bTmp ? "Next" : "" }); break; case 7: // Depart iTmp = (int)Invoke(new Call_ReadControl(ReadControl), "cboDepartComp", "SelectedIndex") + 1; strTmp = (string)Invoke(new Call_ReadControl(ReadControl), "cboDepartPath", "Text"); strTmp += (string)Invoke(new Call_ReadControl(ReadControl), "txtDepartValue", "Text"); bTmp = (bool)Invoke(new Call_ReadControl(ReadControl), "chkDepartNext", "Checked"); rob.Execute("Depart", new object[] { iTmp, strTmp, bTmp ? "Next" : "" }); break; } Invoke(new Call_ButtonEnable(ButtonEnable), true); frm.m_iThreadSwitch = 0; } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (rob != null) { // Release control authority rob.Execute("Givearm", null); // Release robot object Marshal.ReleaseComObject(rob); rob = null; } if (ctrl != null) { // Remove from correction frm.m_caoCtrls.Remove(ctrl.Name); // Release controller object Marshal.ReleaseComObject(ctrl); ctrl = null; } } } private void btnCancel_Click(object sender, EventArgs e) { CaoController ctrl = null; CaoRobots robs = null; CaoRobot rob = null; try { // Get controller object and robots object if (m_caoCtrls.Count > 0) { ctrl = m_caoCtrls.Item(0); robs = ctrl.Robots; } // Get robot object and execute halt if (robs != null && robs.Count > 0) { rob = robs.Item(0); rob.Halt(""); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { // Release robot object if (rob != null) { Marshal.ReleaseComObject(rob); rob = null; } // Release robots object if (robs != null) { Marshal.ReleaseComObject(robs); robs = null; } // Release controller object if (ctrl != null) { Marshal.ReleaseComObject(ctrl); ctrl = null; } } } private void timWatch_Tick(object sender, EventArgs e) { if (m_caoRobBusy != null) { try { // Moving = True / False lblRobMovingValue.Text = m_caoRobBusy.Value.ToString(); } catch (Exception) { lblRobMovingValue.Text = "?"; } } if (m_caoCurPos != null) { try { Double[] dblPos = (Double[])m_caoCurPos.Value; // Current Position lblRobPositionValue.Text = "P(" + dblPos[0].ToString("0.00") + "," + dblPos[1].ToString("0.00") + "," + dblPos[2].ToString("0.00") + "," + dblPos[3].ToString("0.00") + "," + dblPos[4].ToString("0.00") + "," + dblPos[5].ToString("0.00") + "," + dblPos[6].ToString("0.00") + ")"; } catch (Exception) { lblRobPositionValue.Text = "P(?,?,?,?,?,?,?)"; } } } private object ReadControl(string strName, string strParam) { object objRet = null; Control[] c = this.Controls.Find(strName, true); if (c.Length > 0) { Type t = c[0].GetType(); System.Reflection.PropertyInfo pi = t.GetProperty(strParam); if (pi != null) { objRet = pi.GetValue(c[0], null); } } return objRet; } private void ButtonEnable(bool bEnable) { btnMove.Enabled = bEnable; btnChange.Enabled = bEnable; btnSpeed.Enabled = bEnable; btnDrive.Enabled = bEnable; btnRotate.Enabled = bEnable; btnApproach.Enabled = bEnable; btnDepart.Enabled = bEnable; } } }