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 Project { public partial class Form1 : Form { private CaoEngine caoEng; private CaoWorkspaces caoWss; private CaoWorkspace caoWs; // Robot objects private CaoController caoCtrl; // Robot Controller private CaoRobot caoRobot; // for Robot Motion private CaoVariable caoVal; // for MachineRock SW public Form1() { InitializeComponent(); } //-----------------------------Initialization---------------------------- private void cmdInit_Click(object sender, EventArgs e) { // Create Engine caoEng = new CaoEngine(); caoWss = caoEng.Workspaces; caoWs = caoWss.Item(0); cmdInit.Enabled = false; cmdConnectRobot.Enabled = true; } // --------------------------------------------------------------- // Connect with the Robot // --------------------------------------------------------------- private void cmdConnectRobot_Click(object sender, EventArgs e) { try { // Create object and connect with VRC caoCtrl = caoWs.AddController("Robot", "CaoProv.DENSO.RC8", "", "Server=192.168.0.1"); caoRobot = caoCtrl.AddRobot("VS", ""); // Valiable of status of Machine lock SW caoVal = caoCtrl.AddVariable("@LOCK",""); // Turned on the Machine lock SW caoVal.Value = true; // Get armControl authority caoRobot.Execute("TakeArm", new object[] { 0, 1 }); // Start motor(The motor cannot start when the machine lock is true) //caoRobot.Execute("Motor", new object[] { 1, 0 }); // Set the external movement speed of the robot caoRobot.Execute("ExtSpeed", new object[] { 50, 25, 25 }); // Enable command buttons cmdConnectRobot.Enabled = false; cmdDisconnectRobot.Enabled = true; cmdMove.Enabled = true; cmdApproach.Enabled = true; cmdDepart.Enabled = true; cmdDraw.Enabled = true; cmdDriveAEx.Enabled = true; cmdDriveEx.Enabled = true; cmdSpeed.Enabled = true; cmdChangeTool.Enabled = true; cmdChangeWork.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } // --------------------------------------------------------------- // Disconnect from the Robot // --------------------------------------------------------------- private void cmdDisconnectRobot_Click(object sender, EventArgs e) { try { // Disconnect from the caoVal if (caoVal != null) { Marshal.ReleaseComObject(caoVal); caoVal = null; } // Disconnect from the Robot if (caoRobot != null) { if (caoCtrl != null) { // Stop motor //caoRobot.Execute("Motor", new object[]{ 0, 0 }); caoRobot.Execute("GiveArm", ""); CaoRobots robots = caoCtrl.Robots; robots.Remove(caoRobot.Name); Marshal.ReleaseComObject(caoRobot); caoRobot = null; Marshal.ReleaseComObject(robots); robots = null; } } // Disconnect from the controller if (caoCtrl != null) { CaoControllers ctrls = caoWs.Controllers; ctrls.Remove(caoCtrl.Name); Marshal.ReleaseComObject(caoCtrl); caoCtrl = null; Marshal.ReleaseComObject(ctrls); ctrls = null; } cmdConnectRobot.Enabled = true; cmdDisconnectRobot.Enabled = false; cmdMove.Enabled = false; cmdApproach.Enabled = false; cmdDepart.Enabled = false; cmdDraw.Enabled = false; cmdDriveAEx.Enabled = false; cmdDriveEx.Enabled = false; cmdSpeed.Enabled = false; cmdChangeTool.Enabled = false; cmdChangeWork.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { cmdDisconnectRobot_Click(sender, e); // Delete CaoEngine if (caoWs != null) { Marshal.ReleaseComObject(caoWs); caoWs = null; } if (caoWss != null) { Marshal.ReleaseComObject(caoWss); caoWss = null; } if (caoEng != null) { Marshal.ReleaseComObject(caoEng); caoEng = null; } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdMove_Click(object sender, EventArgs e) { try { // Move command //caoRobot.Move(1, "P11", ""); //caoRobot.Move(1, "P12", ""); // Move command caoRobot.Move(1, new object[] { new object[] { 524, 0, 330, 180, 0, 180, 5 }, "P", "@0" }, ""); caoRobot.Move(1, "@P P( 535, -200, 250, 170, -5, 150, 5)", ""); // Move command //caoRobot.Move(1, new object[] { new object[] { 0, 45, 90, 0, 45, 0 }, "J", "@0" }, ""); //caoRobot.Move(1, "J(-22, 61, 76, 14, 48, -2)", ""); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdApproach_Click(object sender, EventArgs e) { try { // Approach command //caoRobot.Execute("Approach", new object[] { 2, "P11", "@P 100" }); // Approach command caoRobot.Execute("Approach", new object[] { 2, "P(524, 0, 330, 180, 0, 180, 5)", "@P 100" }); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdDepart_Click(object sender, EventArgs e) { try { // Depart command caoRobot.Execute("Depart", new object[] { 2, "@P 100" }); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdDraw_Click(object sender, EventArgs e) { try { // Draw command (X=50,Y=50,Z=50) caoRobot.Execute("Draw", new object[] { 2, "V(50,50,50)" }); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdDriveAEx_Click(object sender, EventArgs e) { try { // DriveAEx command caoRobot.Execute("DriveAEx", new object[] { "@P (1,10),(2,50),(3,75),(4,14),(5,55),(6,10)" }); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdDriveEx_Click(object sender, EventArgs e) { try { // DriveEx command caoRobot.Execute("DriveEx", new object[] { "@P (1,10),(2,50),(3,75),(4,14),(5,55),(6,10)" }); //caoRobot.Execute("DriveEx", new object[] { "@0 (1,-35)" }); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdSpeed_Click(object sender, EventArgs e) { try { // Speed command (Internal speed of 75%) caoRobot.Speed(-1, 75); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdChangeTool_Click(object sender, EventArgs e) { try { // Change Tool command (Tool1) caoRobot.Change("Tool1"); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void cmdChangeWork_Click(object sender, EventArgs e) { try { // Change Work command (Work1) caoRobot.Change("Work1"); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } } }