URRobotClient.cs
7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using URSoldering.Common;
namespace URSoldering.DeviceLibrary
{
public class URRobotClient
{
public delegate void SafetModePro(string mode);
public static event SafetModePro SafetModeFun;
public delegate void RobotModePro(string mode);
public static event RobotModePro RobotModeFun;
private static URTcpClient listenClient = null;
private static int Port = 30003;
public static string LastMoveCMD = "";
public static void StartListen(string ip)
{
listenClient = new URTcpClient();
listenClient.DefaultDataLength = 1078;
listenClient.ReviceSleepMS = 5;
listenClient.connect(ip, Port, HandleMessage);
}
public static void StopListen()
{
if (listenClient != null)
{
listenClient.close();
}
}
public static bool IsConnected()
{
if (listenClient != null)
{
return listenClient.IsConnected();
}return false;
}
public static string LogName
{
get { return "【UR端口:" + Port + "】"; }
}
private static double BitToDouble(byte[] data)
{
double f = BitConverter.ToDouble(data, 0);
return f;
}
private static int BitToInt(byte[] data)
{
int f = BitConverter.ToInt32(data, 0);
return f;
}
private static double BitToFloat(string hexString)
{
uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] floatVals = BitConverter.GetBytes(num);
double f = BitConverter.ToDouble(floatVals, 0);
return f;
}
private static string ByteToStr(byte[] reviceData)
{
string strFromat = "{0:X2}";
string reviceMsg = "";
foreach (byte data in reviceData)
{
reviceMsg = reviceMsg + " " + String.Format(strFromat, data);
}return reviceMsg;
}
private static void HandleMessage(byte[] reviceData)
{
if (LastMoveCMD.Equals("").Equals(false))
{
listenClient.sendLine(LastMoveCMD);
LastMoveCMD = "";
}
StopListen();
try
{
byte[] msgSizeArray = reviceData.Skip(0).Take(4).ToArray();
//byte[] timeArray = reviceData.Skip(4).Take(8).ToArray();
//byte[] qTargetArray = reviceData.Skip(12).Take(48).ToArray();
//byte[] qdTargetArray = reviceData.Skip(60).Take(48).ToArray();
//byte[] qddArray = reviceData.Skip(108).Take(48).ToArray();
//byte[] iTargetArray = reviceData.Skip(156).Take(48).ToArray();
int messageSize = BitToInt(reviceData.Skip(0).Take(4).ToArray().Reverse<byte>().ToArray());
int maxdouble = (messageSize - 4) / 8;
List<double> doubleList = new List<double>();
doubleList.Add(messageSize);
if (maxdouble > 61)
{
int[] array = new int[] { 55,56,57,58,59,60,94,101,103,132};
for (int i = 0; i < maxdouble; i++)
{
if (array.Contains(i))
{
if (reviceData.Length >= (12 + i * 8))
{
byte[] data = reviceData.Skip(4 + i * 8).Take(8).ToArray().Reverse<byte>().ToArray();
double value = BitToDouble(data);
//LogUtil.info("第【"+(i+1)+"】个double["+ByteToStr(data) +"]值:"+value);
doubleList.Add(value);
}
else
{
break;
}
}
else
{
doubleList.Add(0);
}
}
if (doubleList.Count > 61)
{
double x = doubleList[56] * 1000;
double y = doubleList[57] * 1000;
double z = doubleList[58] * 1000;
double rx = doubleList[59] * 1;
double ry = doubleList[60] * 1;
double rz = doubleList[61] * 1;
URPointValue newp = new URPointValue(x, y, z, rx, ry, rz);
int robotMode =(int) doubleList[95];
int safetyMode =(int) doubleList[102];
string robotModeStr = GetRobotMode(robotMode);
string safetyModeStr = GetSafetyMode(safetyMode);
int programState = (int)doubleList[132];
SafetModeFun?.Invoke(safetyModeStr);
RobotModeFun?.Invoke(robotModeStr);
if (!URRobotControl.IsSamePoint(newp, URRobotControl.LastPoint))
{
LogUtil.URLInfo(LogName + "length[" + messageSize + "],data长[" + reviceData.Length + "],【" + programState + "】【" + robotModeStr + "】【" + safetyModeStr + "】坐标" + newp.ToShowStr()+"");
}
URRobotControl.LastPoint = newp;
}
}
else
{
string reviceMsg = ByteToStr(reviceData);
LogUtil.URLError(LogName + "length[" + messageSize + "],data长[" + reviceData.Length + "]无法获取坐标,数据长度不正确");
}
}
catch(Exception ex)
{
LogUtil.URLError(LogName+ "HandleMessage出错:" + ex.ToString());
StopListen();
}
}
public static string GetRobotMode(int type)
{
switch (type)
{
case 0:
return "ROBOT_MODE_DISCONNECTED";
case 1:
return "ROBOT_MODE_CONFIRM_SAFETY";
case 2:
return "ROBOT_MODE_BOOTING";
case 3:
return "ROBOT_MODE_POWER_OFF";
case 4:
return "ROBOT_MODE_POWER_ON";
case 5:
return "ROBOT_MODE_IDLE";
case 6:
return "ROBOT_MODE_BACKDRIVE";
case 7:
return "ROBOT_MODE_RUNNING";
case 8:
return "ROBOT_MODE_UPDATING_FIRMWARE";
default:
return "";
}
}
public static string GetSafetyMode(int type)
{
switch (type)
{
case 1:
return "SAFETY_MODE_NORMAL";
case 2:
return "SAFETY_MODE_REDUCED";
case 3:
return "SAFETY_MODE_PROTECTIVE_STOP";
case 4:
return "SAFETY_MODE_RECOVERY";
case 5:
return "SAFETY_MODE_SAFEGUARD_STOP";
case 6:
return "SAFETY_MODE_SYSTEM_EMERGENCY_STOP";
case 7:
return "SAFETY_MODE_ROBOT_EMERGENCY_STOP";
case 8:
return "SAFETY_MODE_VIOLATION";
case 9:
return "SAFETY_MODE_FAULT";
default:
return "";
}
}
}
}