508 lines
18 KiB
C#
508 lines
18 KiB
C#
using System;
|
||
using System.Drawing;
|
||
using System.Threading;
|
||
using System.Windows.Forms;
|
||
using ControlBeanExDll;
|
||
using TcpserverExDll;
|
||
|
||
namespace HitBotCSharpDemo
|
||
{
|
||
public partial class ShowForm : Form
|
||
{
|
||
Thread thread_Jog_Move;//创建按钮长按,连续移动模式的线程。 与点击事件冲突处理
|
||
bool moveFlag;//开始结束寸动标志
|
||
bool isInit = false;
|
||
Button btn;//声明按钮对象。
|
||
Label lbl;
|
||
int offset = 1;//寸动距离
|
||
int offset_jog = 2;//连续移动单次距离
|
||
static int io_Count = 6;//IO数量
|
||
int[] io_In = new int[io_Count];
|
||
int[] io_Out = new int[io_Count];
|
||
Label[] lbl_Current_Input;//IO显示控件数组
|
||
Label[] lbl_Current_Output;
|
||
bool output_State_Flag0 = true;
|
||
bool output_State_Flag1 = true;
|
||
bool output_State_Flag2 = true;
|
||
bool output_State_Flag3 = true;
|
||
bool output_State_Flag4 = true;
|
||
bool output_State_Flag5 = true;
|
||
float[] pos0;//点位1
|
||
int posHand0;
|
||
float[] pos1;//点位2
|
||
int posHand1;
|
||
float[] pos2;//点位3
|
||
int posHand2;
|
||
Thread thread_PosMove;
|
||
|
||
private ControlBeanEx robot;
|
||
public ShowForm()
|
||
{
|
||
InitializeComponent();
|
||
TcpserverEx.net_port_initial();
|
||
robot = TcpserverEx.get_robot(74);//替换为自己的机器的id号
|
||
}
|
||
|
||
private void ShowForm_Load(object sender, EventArgs e)
|
||
{
|
||
lbl_Current_Input = new Label[] { lbl_InputState_0, lbl_InputState_1, lbl_InputState_2, lbl_InputState_3, lbl_InputState_4, lbl_InputState_5 };//输入信号数组
|
||
lbl_Current_Output = new Label[] { lbl_OutputState_0, lbl_OutputState_1, lbl_OutputState_2, lbl_OutputState_3, lbl_OutputState_4, lbl_OutputState_5 };//输出信号数组
|
||
}
|
||
|
||
private void cob_Robot_ID_Click(object sender, EventArgs e)
|
||
{
|
||
cob_Robot_ID.Items.Clear();
|
||
for (int i = 0; i < 255; i++)
|
||
{
|
||
robot = TcpserverEx.get_robot(i);//替换为自己的机器的id号
|
||
if (robot.is_connected())
|
||
{
|
||
cob_Robot_ID.Items.Add(i.ToString());
|
||
}
|
||
}
|
||
}
|
||
|
||
private void btn_Init_Click(object sender, EventArgs e)
|
||
{
|
||
rit_Coord.Clear();
|
||
if (robot == null) return;
|
||
robot = TcpserverEx.get_robot(int.Parse(cob_Robot_ID.Text));
|
||
robot.check_joint(4, false);
|
||
if (robot.is_connected())
|
||
{
|
||
int ret = robot.initial(1, 240); //修改自己机器的型号,参数具体意义参考sdk说明文档
|
||
if (ret == 1)
|
||
{
|
||
robot.unlock_position();
|
||
isInit = true;
|
||
MessageBox.Show("robot" + "初始化完成");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("robot" + "初始化失败,返回值 = " + ret.ToString());
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("robot" + "未连接");
|
||
}
|
||
}
|
||
|
||
private void btn_Move_Click(object sender, EventArgs e) //点击按钮,寸动事件。
|
||
{
|
||
if (!isInit)
|
||
{
|
||
MessageBox.Show("机械臂未初始化");
|
||
return;
|
||
}
|
||
#region 按钮移动事件
|
||
if (chb_Inching.Checked == true)//寸动
|
||
{
|
||
|
||
robot.get_scara_param();
|
||
int hand = robot.get_lr();
|
||
float speed = 2;
|
||
robot.new_set_acc(30, 30, 30, 30);
|
||
#region 轴动
|
||
if (btn == btn_XP)//X正向移动
|
||
{
|
||
robot.new_movej_xyz_lr(robot.x + offset, robot.y, robot.z, robot.rotation, speed, 0, hand);//以 movej(直线模式) 的运动方式,并以指定手系达目标点
|
||
}
|
||
else if (btn == btn_XN)//X负向移动
|
||
{
|
||
robot.new_movej_xyz_lr(robot.x - offset, robot.y, robot.z, robot.rotation, speed, 0, hand);
|
||
}
|
||
if (btn == btn_YP)//Y正向移动
|
||
{
|
||
robot.new_movej_xyz_lr(robot.x, robot.y + offset, robot.z, robot.rotation, speed, 0, hand);//以 movej(直线模式) 的运动方式,并以指定手系达目标点
|
||
}
|
||
else if (btn == btn_YN)//Y负向移动
|
||
{
|
||
robot.new_movej_xyz_lr(robot.x, robot.y - offset, robot.z, robot.rotation, speed, 0, hand);
|
||
}
|
||
if (btn == btn_ZP)//Z正向移动
|
||
{
|
||
robot.new_movej_xyz_lr(robot.x, robot.y, robot.z + offset, robot.rotation, speed, 0, hand);//以 movej(直线模式) 的运动方式,并以指定手系达目标点
|
||
}
|
||
else if (btn == btn_ZN)//Z负向移动
|
||
{
|
||
robot.new_movej_xyz_lr(robot.x, robot.y, robot.z - offset, robot.rotation, speed, 0, hand);
|
||
}
|
||
if (btn == btn_RP)//R正向旋转
|
||
{
|
||
robot.new_movej_xyz_lr(robot.x, robot.y, robot.z, robot.rotation + offset, speed, 0, hand);//以 movej(直线模式) 的运动方式,并以指定手系达目标点
|
||
}
|
||
else if (btn == btn_RN)//R负向旋转
|
||
{
|
||
robot.new_movej_xyz_lr(robot.x, robot.y, robot.z, robot.rotation - offset, speed, 0, hand);
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
public void jog_Move()//连续移动方法
|
||
{
|
||
moveFlag = true;
|
||
while (moveFlag && chb_Inching.Checked == false && btn != null)
|
||
{
|
||
|
||
//robot.new_set_acc(100, 100, 100, 100);
|
||
if (btn == btn_XP)//X正向移动
|
||
{
|
||
robot.jog_move2(offset_jog, 0, 0, 0, 1);
|
||
//robot.jog_move2(robot.x - 1, robot.y, robot.z, robot.rotation,1);
|
||
//type: 1:是以xyz坐标系为准移动固定距离。 2是以关节轴为基准移动固定距离
|
||
}
|
||
else if (btn == btn_XN)//X负向移动
|
||
{
|
||
robot.jog_move2(-offset_jog, 0, 0, 0, 1);
|
||
}
|
||
if (btn == btn_YP)//Y正向移动
|
||
{
|
||
robot.jog_move2(0, offset_jog, 0, 0, 1);
|
||
}
|
||
else if (btn == btn_YN)//Y负向移动
|
||
{
|
||
robot.jog_move2(0, -offset_jog, 0, 0, 1);
|
||
}
|
||
if (btn == btn_ZP)//Z正向移动
|
||
{
|
||
robot.jog_move2(0, 0, offset_jog, 0, 1);
|
||
}
|
||
else if (btn == btn_ZN)//Z负向移动
|
||
{
|
||
robot.jog_move2(0, 0, -offset_jog, 0, 1);
|
||
}
|
||
if (btn == btn_RP)//R正向旋转
|
||
{
|
||
robot.jog_move2(0, 0, 0, offset_jog, 1);
|
||
}
|
||
else if (btn == btn_RN)//R负向旋转
|
||
{
|
||
robot.jog_move2(0, 0, 0, offset_jog, 1);
|
||
}
|
||
Thread.Sleep(200);
|
||
}
|
||
robot.wait_stop();
|
||
}
|
||
|
||
|
||
private void btn_gainJointState_Click(object sender, EventArgs e)//获取关节状态
|
||
{
|
||
if (!isInit)
|
||
{
|
||
MessageBox.Show("机械臂未初始化");
|
||
return;
|
||
}
|
||
int jointNumber = int.Parse(cob_Joint_Number.SelectedItem.ToString());
|
||
int ret = robot.get_joint_state(jointNumber);
|
||
switch (ret)
|
||
{
|
||
case 1:
|
||
rit_showJointState.Clear();
|
||
rit_showJointState.SelectedText = "关节" + jointNumber + "正常";
|
||
break;
|
||
default:
|
||
rit_showJointState.Clear();
|
||
rit_showJointState.SelectedText = "关节" + jointNumber + "异常!异常代码:" + ret + ".(详情请查看API文档)"; //可具体
|
||
break;
|
||
}
|
||
}
|
||
|
||
private void btn_Jog_MouseDown(object sender, MouseEventArgs e)
|
||
{
|
||
btn = (Button)sender;
|
||
if (btn == null)
|
||
{
|
||
return;
|
||
}
|
||
thread_Jog_Move = new Thread(jog_Move);
|
||
thread_Jog_Move.IsBackground = true;
|
||
thread_Jog_Move.Start();
|
||
|
||
}
|
||
|
||
private void tim_IO_Refresh_Tick(object sender, EventArgs e)//刷新IO和坐标定时器
|
||
{
|
||
robot.get_scara_param();
|
||
if (isInit)
|
||
{
|
||
rit_Coord.Text = "Coord: X:" + robot.x + "mm " + "Y:" + robot.y + "mm " + "Z:" + robot.z + "mm " + "R:" + robot.rotation + "°" + "Hand:" + (robot.get_lr() == 1 ? "R" : "L") + " ";
|
||
}
|
||
for (int i = 0; i < io_Count; i++)
|
||
{
|
||
io_In[i] = robot.get_digital_in(i);
|
||
io_Out[i] = robot.get_digital_out(i);
|
||
}
|
||
for (int i = 0; i < io_Count; i++)//输入
|
||
{
|
||
int ret_Input = io_In[i];
|
||
Label lbl_Current_IO = lbl_Current_Input[i];
|
||
switch (ret_Input)//• -1:io_number 参数错误 • 0:io 输入点没有被触发 • 1:io 输入点被触发 • 3:未初始化
|
||
{
|
||
case -1:
|
||
if (lbl_Current_Input[i].BackColor != Color.Gray)
|
||
lbl_Current_Input[i].BackColor = Color.Gray;
|
||
break;
|
||
case 0:
|
||
if (lbl_Current_Input[i].BackColor != Color.Green)
|
||
lbl_Current_Input[i].BackColor = Color.Green;
|
||
break;
|
||
case 1:
|
||
if (lbl_Current_Input[i].BackColor != Color.Red)
|
||
lbl_Current_Input[i].BackColor = Color.Red;
|
||
break;
|
||
case 3:
|
||
if (lbl_Current_Input[i].BackColor != Color.Gray)
|
||
lbl_Current_Input[i].BackColor = Color.Gray;
|
||
break;
|
||
}
|
||
}
|
||
for (int i = 0; i < io_Count; i++)//输出
|
||
{
|
||
int ret_Output = io_Out[i];
|
||
Label lbl_Current_IO = lbl_Current_Output[i];
|
||
switch (ret_Output)//• -1:io_number 参数错误 • 0:io 输入点没有被触发 • 1:io 输入点被触发 • 3:未初始化
|
||
{
|
||
case -1:
|
||
if (lbl_Current_Output[i].BackColor != Color.Gray)
|
||
lbl_Current_Output[i].BackColor = Color.Gray;
|
||
break;
|
||
case 0:
|
||
if (lbl_Current_Output[i].BackColor != Color.Green)
|
||
lbl_Current_Output[i].BackColor = Color.Green;
|
||
break;
|
||
case 1:
|
||
if (lbl_Current_Output[i].BackColor != Color.Red)
|
||
lbl_Current_Output[i].BackColor = Color.Red;
|
||
break;
|
||
case 3:
|
||
if (lbl_Current_Output[i].BackColor != Color.Gray)
|
||
lbl_Current_Output[i].BackColor = Color.Gray;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
private void lbl_Output_Click(object sender, EventArgs e)//设置输出事件
|
||
{
|
||
#region 判断输出编号
|
||
int output_Num = 0;
|
||
lbl = (Label)sender;
|
||
if (lbl == lbl_OutputState_0)
|
||
{
|
||
output_Num = 0;
|
||
robot.set_digital_out(output_Num, output_State_Flag0);
|
||
output_State_Flag0 = !output_State_Flag0;
|
||
}
|
||
else if (lbl == lbl_OutputState_1)
|
||
{
|
||
output_Num = 1;
|
||
robot.set_digital_out(output_Num, output_State_Flag1);
|
||
output_State_Flag1 = !output_State_Flag1;
|
||
}
|
||
else if (lbl == lbl_OutputState_2)
|
||
{
|
||
output_Num = 2;
|
||
robot.set_digital_out(output_Num, output_State_Flag2);
|
||
output_State_Flag2 = !output_State_Flag2;
|
||
}
|
||
else if (lbl == lbl_OutputState_3)
|
||
{
|
||
output_Num = 3;
|
||
robot.set_digital_out(output_Num, output_State_Flag3);
|
||
output_State_Flag3 = !output_State_Flag3;
|
||
}
|
||
else if (lbl == lbl_OutputState_4)
|
||
{
|
||
output_Num = 4;
|
||
robot.set_digital_out(output_Num, output_State_Flag4);
|
||
output_State_Flag4 = !output_State_Flag4;
|
||
}
|
||
else if (lbl == lbl_OutputState_5)
|
||
{
|
||
output_Num = 5;
|
||
robot.set_digital_out(output_Num, output_State_Flag5);
|
||
output_State_Flag5 = !output_State_Flag5;
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
private void btn_MouseUp(object sender, MouseEventArgs e)//停止连续移动
|
||
{
|
||
if (robot != null&&!chb_Inching.Checked)
|
||
{
|
||
robot.new_stop_move();
|
||
}
|
||
btn = null;
|
||
moveFlag = false;
|
||
}
|
||
|
||
private void btn_Position0_Click(object sender, EventArgs e)
|
||
{
|
||
if (!isInit)
|
||
{
|
||
MessageBox.Show("机械臂未初始化");
|
||
return;
|
||
}
|
||
rit_Position0.Clear();
|
||
rit_Position0.Text = "Coord: X:" + robot.x + "mm " + "Y:" + robot.y + "mm " + "Z:" + robot.z + "mm " + "R:" + robot.rotation + "°" + "Hand:" + (robot.get_lr() == 1 ? "R" : "L") + " ";
|
||
pos0 = new float[] { robot.x, robot.y, robot.z, robot.rotation };
|
||
posHand0 = robot.get_lr()/* == 1 ? "R" : "L"*/;
|
||
}
|
||
|
||
private void btn_Position1_Click(object sender, EventArgs e)
|
||
{
|
||
if (!isInit)
|
||
{
|
||
MessageBox.Show("机械臂未初始化");
|
||
return;
|
||
}
|
||
rit_Position1.Clear();
|
||
rit_Position1.Text = "Coord: X:" + robot.x + "mm " + "Y:" + robot.y + "mm " + "Z:" + robot.z + "mm " + "R:" + robot.rotation + "°" + "Hand:" + (robot.get_lr() == 1 ? "R" : "L") + " ";
|
||
pos1 = new float[] { robot.x, robot.y, robot.z, robot.rotation };
|
||
posHand1 = robot.get_lr()/* == 1 ? "R" : "L"*/;
|
||
}
|
||
|
||
private void btn_Position2_Click(object sender, EventArgs e)
|
||
{
|
||
if (!isInit)
|
||
{
|
||
MessageBox.Show("机械臂未初始化");
|
||
return;
|
||
}
|
||
rit_Position2.Clear();
|
||
rit_Position2.Text = "Coord: X:" + robot.x + "mm " + "Y:" + robot.y + "mm " + "Z:" + robot.z + "mm " + "R:" + robot.rotation + "°" + "Hand:" + (robot.get_lr() == 1 ? "R" : "L") + " ";
|
||
pos2 = new float[] { robot.x, robot.y, robot.z, robot.rotation };
|
||
posHand2 = robot.get_lr() /*== 1 ? "R" : "L"*/;
|
||
}
|
||
|
||
private void btn_Start_Click(object sender, EventArgs e)
|
||
{
|
||
btn_Pause.Enabled = true;
|
||
btn_Resume.Enabled = true;
|
||
if (pos0==null|| pos1 == null|| pos2 == null)
|
||
{
|
||
MessageBox.Show("请先设置3个点位!");
|
||
return;
|
||
}
|
||
thread_PosMove = new Thread(PosMove);
|
||
thread_PosMove.IsBackground = false;
|
||
thread_PosMove.Start();
|
||
}
|
||
|
||
private static object locker_PosMove = new object();
|
||
public void PosMove()
|
||
{
|
||
lock (locker_PosMove)
|
||
{
|
||
while (true)
|
||
{
|
||
robot.resume_move();
|
||
robot.get_scara_param();
|
||
robot.new_movej_xyz_lr(pos0[0], pos0[1], pos0[2], pos0[3], 50, 0, posHand0);
|
||
robot.wait_stop();
|
||
robot.new_movej_xyz_lr(pos1[0], pos1[1], pos1[2], pos1[3], 50, 0, posHand1);
|
||
robot.wait_stop();
|
||
robot.new_movej_xyz_lr(pos2[0], pos2[1], pos2[2], pos2[3], 50, 0, posHand2);
|
||
robot.wait_stop();
|
||
}
|
||
}
|
||
}
|
||
|
||
private void btn_Pause_Click(object sender, EventArgs e)
|
||
{
|
||
btn_Stop.Enabled = false;
|
||
robot.pause_move();
|
||
}
|
||
|
||
private void btn_Resume_Click(object sender, EventArgs e)
|
||
{
|
||
btn_Stop.Enabled = true;
|
||
robot.resume_move();
|
||
}
|
||
|
||
private void btn_Stop_Click(object sender, EventArgs e)
|
||
{
|
||
btn_Pause.Enabled = false;
|
||
btn_Resume.Enabled = false;
|
||
robot.new_stop_move();
|
||
thread_PosMove.Abort();
|
||
}
|
||
|
||
private void btn_Joint_Click(object sender, EventArgs e)
|
||
{
|
||
if (!isInit)
|
||
{
|
||
MessageBox.Show("机械臂未初始化");
|
||
return;
|
||
}
|
||
#region 关节回零
|
||
btn = (Button)sender;
|
||
if (btn == null)
|
||
{
|
||
return;
|
||
}
|
||
if (btn == btn_joint1)
|
||
{
|
||
robot.joint_home(1);//使轴回到零位
|
||
|
||
}
|
||
else if (btn == btn_joint2)
|
||
{
|
||
robot.joint_home(2);
|
||
}
|
||
else if (btn == btn_joint3)
|
||
{
|
||
robot.joint_home(3);
|
||
}
|
||
else if (btn == btn_joint4)
|
||
{
|
||
|
||
robot.joint_home(4);
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
private void rit_Coord_TextChanged(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void cob_Robot_ID_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void tap_Move_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void button1_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void button3_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void button2_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void button4_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|