EpsonRobotDevice.cs
11.3 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using log4net;
using URSoldering.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using URSoldering.LoadCSVLibrary;
namespace URSoldering.DeviceLibrary
{
public class EpsonDevice
{
private static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static TcpClient moveTcp = new TcpClient();
private static string EpsonIp = "";
private static int MovePointPort = 2000;
public static double Robot_LIM_Z = (double)ConfigAppSettings.GetNumValue(Setting_Init.Soldering_LIM_Z);
private static string MoveOK = "point ok";
private static string FreeOK = "free ok";
private static string LockOK = "lock ok";
internal static void Init(object epson_IP)
{
throw new NotImplementedException();
}
//最后一次从机械臂读取到的坐标位置
public static PointValue LastPoint = new PointValue(0, 0, 0, 0, 0);
//最后一次软件控制移动到的位置
public static PointValue LastSendPoint = new PointValue(0, 0, 0, 0, 0);
public delegate void OpEnd(string result);
private static event OpEnd OnMoveEnd;
public delegate void GetPoint(double x, double y, double z, double u, int hand);
private static event GetPoint WhenGetPoint;
private static object LockObj = new object();
/// <summary>
/// 是否锁住轴
/// </summary>
public static bool IsLock = true;
/// <summary>
/// 是否运行中
/// </summary>
public static bool IsRun = false;
public static void Init(string ip)
{
EpsonIp = ip;
}
/// <summary>
/// 是否有错误,(检查Ready和Alarm信号)
/// </summary>
/// <returns></returns>
public static bool HasError()
{
bool isReady = IO_VALUE.HIGH.Equals(RobotBean.KNDIOValue(IO_Type.EpsonRunning));
if (!isReady)
{
return true;
}
bool hasError = IO_VALUE.HIGH.Equals(RobotBean.KNDIOValue(IO_Type.EpsonAlarm));
return hasError;
}
public static string Start()
{
Stop();
LogUtil.info("开始连接Epson");
RobotBean.KNDIOMove(IO_Type.EpsonReset, IO_VALUE.HIGH);
Thread.Sleep(200);
RobotBean.KNDIOMove(IO_Type.EpsonReset, IO_VALUE.LOW);
Thread.Sleep(500);
//判断是否有报警
if (IO_VALUE.HIGH.Equals(RobotBean.KNDIOValue(IO_Type.EpsonAlarm)))
{
LogUtil.info("Epson有报警,进行复位");
//复位
RobotBean.KNDIOMove(IO_Type.EpsonReset, IO_VALUE.HIGH);
Thread.Sleep(200);
RobotBean.KNDIOMove(IO_Type.EpsonReset, IO_VALUE.HIGH);
Thread.Sleep(2000);
}
if (IO_VALUE.HIGH.Equals(RobotBean.KNDIOValue(IO_Type.EpsonAlarm)))
{
LogUtil.error("Epson报警无法复位");
return "Epson报警无法复位";
}
//启动程序
RobotBean.KNDIOMove(IO_Type.EpsonStart, IO_VALUE.HIGH);
Thread.Sleep(200);
RobotBean.KNDIOMove(IO_Type.EpsonStart, IO_VALUE.LOW);
try
{
//Running信号
RobotBean.WaitIo("Epson准备信号", IO_Type.EpsonRunning, IO_VALUE.HIGH, 3000);
}
catch (TimeoutException te)
{
LogUtil.error("Epson连接失败:" + te.Message);
return te.Message;
}
Thread.Sleep(200);
moveTcp = new TcpClient();
bool result = moveTcp.connect(EpsonIp, MovePointPort, new TcpClient.HandleMessage(OnMoveRevice));
if (!result)
{
LogUtil.error(LOGGER, "Epson 连接【" + EpsonIp + "】【" + MovePointPort + "】失败");
IsRun = false;
return "连接【" + EpsonIp + "】【" + MovePointPort + "】失败";
}
else
{
IsRun = true;
LogUtil.info(LOGGER, "连接【" + EpsonIp + "】【" + MovePointPort + "】成功");
return "";
}
}
public static bool LockAxis()
{
try
{
IsLock = true;
moveTcp.sendLine(GetSendStr("lock"));
bool freeResult = WaitUtil.Wait(2000, delegate ()
{
return IsLock.Equals(false);
});
IsLock = !freeResult;
return freeResult;
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦" + ex.ToString());
}
return true;
}
public static bool FreeAxis()
{
try
{
IsLock = false;
moveTcp.sendLine(GetSendStr("free"));
bool lockResult = WaitUtil.Wait(2000, delegate ()
{
return IsLock.Equals(true);
});
IsLock = lockResult;
return lockResult;
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦" + ex.ToString());
}
return false;
}
public static bool GetPosition(GetPoint AfterGet)
{
try
{
if (IsRun)
{
WhenGetPoint = AfterGet;
moveTcp.sendLine(GetSendStr("save"));
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦" + ex.ToString());
}
return true;
}
public static void MoveTo(double x, double y, double z, double u, bool IsHighSpeed, int hand,double limZ, OpEnd AfterMove)
{
EpsonDevice.OnMoveEnd = AfterMove;
SendMovePoint(x, y, z, u, IsHighSpeed, hand, limZ);
}
public static void MoveTo(double x, double y, double z, double u, bool IsHighSpeed, int hand, OpEnd AfterMove)
{
EpsonDevice.OnMoveEnd = AfterMove;
SendMovePoint(x, y, z, u, IsHighSpeed, hand, Robot_LIM_Z);
}
public static void Stop()
{
try
{
IsRun = false;
if (moveTcp.IsConnected())
{
moveTcp.close();
}
//关闭程序
RobotBean.KNDIOMove(IO_Type.EpsonStop, IO_VALUE.HIGH);
Thread.Sleep(500);
RobotBean.KNDIOMove(IO_Type.EpsonStop, IO_VALUE.LOW);
}
catch (Exception ex)
{
LogUtil.error("Stop Epson出错啦" + ex.ToString());
}
}
/// <summary>
/// 发送坐标移动
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z">上下</param>
/// <param name="u">旋转</param>
/// <param name="IsHighSpeed"></param>
/// <param name="hand">1=右手,2=左手</param>
private static void SendMovePoint(double x, double y, double z, double u, bool IsHighSpeed, int hand, double limZ)
{
lock (LockObj)
{
if (z > 0)
{
OnMoveEnd?.Invoke("坐标错误:Z坐标不能小于0,当前为:" + z);
}
if (limZ > EpsonDevice.Robot_LIM_Z)
{
limZ = EpsonDevice.Robot_LIM_Z;
}
string speed = "l";
if (IsHighSpeed)
{
speed = "h";
}
string handStr = "0";
if (hand == 2)
{
handStr = "l";
}
else if (hand == 1)
{
handStr = "r";
}
string str = "move" + "," + x + "," + y + "," + z + "," + u + "," + speed + "," + handStr + "," + limZ;
LastSendPoint = new PointValue(x, y, u, z, hand);
if (IsLock == false)
{
OnMoveEnd?.Invoke("轴没有锁定");
}
if (!IsRun)
{
OnMoveEnd?.Invoke("机器人未连接");
}
moveTcp.sendLine(str);
}
}
/// <summary>
/// 记录最后一次收到OK的时间
/// </summary>
private static Dictionary<string, DateTime> LastOkMap = new Dictionary<string, DateTime>();
public static DateTime MoveOKTime()
{
if (LastOkMap.ContainsKey(MoveOK))
{
return LastOkMap[MoveOK];
}
return new DateTime(0);
}
private static void AddOkTime(string type)
{
if (LastOkMap.ContainsKey(type))
{
LastOkMap.Remove(type);
}
LastOkMap.Add(type, DateTime.Now);
}
private static void OnMoveRevice(string message)
{
if (message == null || message.Equals(""))
{
return;
}
try
{
LogUtil.debug("【" + EpsonIp + "】【" + MovePointPort + "】收到数据:" + message.Replace("\r\n", ""));
if (message.Contains(MoveOK))
{
AddOkTime(MoveOK);
OnMoveEnd?.Invoke("");
}
else if (message.Contains(FreeOK))
{
AddOkTime(FreeOK);
IsLock = false;
}
else if (message.Contains(LockOK))
{
AddOkTime(LockOK);
IsLock = true;
}
else if (message.StartsWith("ERR"))
{
LogUtil.error("收到Epson机器人错误:" + message);
}
else
{
string[] posList = message.Split(',');
if (posList.Length == 5)
{
double x = Convert.ToDouble(posList[0]);
double y = Convert.ToDouble(posList[1]);
double z = Convert.ToDouble(posList[2]);
double u = Convert.ToDouble(posList[3]);
int hand = Convert.ToInt32(posList[4]);
WhenGetPoint?.Invoke(x, y, z, u, hand);
LastPoint = new PointValue(x, y, u, z, hand);
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "收到数据【" + message + "】出错啦" + ex.ToString());
}
}
private static string GetSendStr(string str)
{
return str + ",0,0,0,0,0,0,0";
}
}
}