URRobotControl.cs
21.5 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
using log4net;
using URSoldering.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using URToolKit;
namespace URSoldering.DeviceLibrary
{
public class URRobotControl
{
private static string spiltStr = ",";
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static TcpClient controlTcp = new TcpClient();
public static URPointValue LastPoint = new URPointValue();
public static URPointValue LastSendPoint = new URPointValue();
public static string RobotIp = "";
public static int ControlPort = 29999;
public static double Robot_LIM_Z = (double)ConfigAppSettings.GetNumValue(Setting_Init.Soldering_LIM_Z);
private static string CMD_powerOn = "power on";
private static string CMD_powerOff = "power off";
private static string CMD_robotMode = "robotmode";
private static string CMD_brakeRelease = "brake release";
private static string CMD_Safetymode = "Safetymode";
private static string REV_PowerOn = "powering on";
private static string REV_BrakeRelease = "brake releasing";
private static string REV_RobotMode = "robotmode";
private static string REV_SafetyMode = "safetymode";
/// <summary>
/// 记录最后一次收到OK的时间
/// </summary>
private static Dictionary<string, DateTime> LastOkMap = new Dictionary<string, DateTime>();
/// <summary>
/// 是否锁住轴
/// </summary>
public static bool IsLock = true;
/// <summary>
/// 是否运行中
/// </summary>
public static bool IsRun = false;
public static bool IsStartConnect = false;
private static int startCount = 0;
/// <summary>
/// 上一次启动的时间,UR机械臂启动需要时间,默认需要3分钟,若三分钟后还没有链接,需要重新启动
/// </summary>
private static DateTime PreStartTime = DateTime.Now;
private static int StartTimeOutSeconds = 20000;
public static string WarnMsg = "";
private static System.Timers.Timer reconnectTimer = null;
public static void InitConfig(string ip)
{
RobotIp = ip;
URRobotClient.SafetModeFun += SafetyEProcess;
URRobotClient.RobotModeFun += ModeEProcess;
}
public static string LogName
{
get { return "【" + RobotIp + " ," + ControlPort + "】"; }
}
public static string GetStatus()
{
if (WarnMsg.Equals(""))
{
if (IsRun)
{
return "机器人正常连接中";
}
else
{
if (IsStartConnect)
{
TimeSpan span = DateTime.Now - PreStartTime;
if (span.TotalMilliseconds > StartTimeOutSeconds)
{
return "机器人启动超时";
}
else
{
return "机器人启动中";
}
}
else
{
return "机器人未连接";
}
}
}
else
{
return WarnMsg;
}
}
public static void Reset()
{
URWithOutTP a = new URWithOutTP(); //创建对象
a.restartURControl(RobotIp); //重置UR控制器,然后可以利用dashboard,poweron,brake release
LogUtil.info(LogName + "重置完成");
Thread.Sleep(100);
}
/// <summary>
/// 开始启动连接机器人
/// </summary>
/// <returns></returns>
public static bool StartRobot()
{
if (IsRun)
{
return true;
}
if (reconnectTimer == null)
{
reconnectTimer = new System.Timers.Timer();
reconnectTimer.AutoReset = true;
reconnectTimer.Interval = 400;
reconnectTimer.Elapsed += reconnectTimer_Elapsed;
reconnectTimer.Enabled = false;
}
IsStartConnect = true;
return StartConnect();
}
private static bool StartConnect()
{
try
{
if (RobotIp.Equals(""))
{
LogUtil.info("未初始化UR机械臂的IP地址");
return false;
}
if (startCount > 0)
{
TimeSpan span = DateTime.Now - PreStartTime;
if (span.TotalMilliseconds < StartTimeOutSeconds)
{
LogUtil.info("" + LogName + "正在连接中,不需要重连");
return true;
}
else
{
LogUtil.info("" + LogName + "距离上次启动已经超过10秒,重新开始连接");
StopRobot();
}
}
WarnMsg = "";
IsRun = false;
ClearPreCMD();
LogUtil.info("" + LogName + "开始连接");
PreStartTime = DateTime.Now;
startCount++;
controlTcp = new TcpClient();
controlTcp.TimeOutTime = 2000;
bool result = controlTcp.connect(RobotIp, ControlPort, new TcpClient.HandleMessage(OnControlRevice));
if (!result)
{
LogUtil.error(LOGGER, "连接" + LogName + "失败");
return false;
}
else
{
LogUtil.info(LOGGER, "连接" + LogName + "成功");
}
//发送
controlTcp.sendLine(CMD_Safetymode);
//controlTcp.sendLine(CMD_robotMode);
preCheckTime = DateTime.Now;
return true;
}
catch (Exception ex)
{
LogUtil.error("" + LogName + "starttcp error:" + ex.ToString());
return false;
}
}
private static DateTime preCheckTime = DateTime.Now;
private static bool IsInPorcess = false;
private static void reconnectTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (IsInPorcess)
{
return;
}
IsInPorcess = true;
try
{
if (IsRun && IsStartConnect)
{
TimeSpan span = DateTime.Now - preCheckTime;
if (span.TotalSeconds > 2)
{
preCheckTime = DateTime.Now;
//判断500在连接上,则获取急停状态
if (controlTcp.IsConnected())
{
//controlTcp.sendLine(CMD_Safetymode);
}
else
{
LogUtil.error("" + LogName + "reconnectTimer_Elapsed检测到 机械臂已经断开,需要重连!");
stopTcp();
StartConnect();
}
}
}
if (IsRun)
{
//如果监听的断开,直接重新连接
if (!URRobotClient.IsConnected())
{
URRobotClient.StartListen(RobotIp);
}else if (URRobotClient.IsTimeOut())
{
LogUtil.URSError( URRobotClient.LogName+ "接收数据超时,断开后重连" );
URRobotClient.StopListen();
URRobotClient.StartListen(RobotIp);
}
}
}
catch (Exception ex)
{
LOGGER.Error("" + LogName + " 定时器出错:" + ex.ToString());
}
IsInPorcess = false;
}
public static bool SendCMD(string cmd, int msendons)
{
try
{
LogUtil.URSInfo(LogName + "将于【" + msendons + "】后发送数据:" + cmd);
if (msendons > 0)
{
Task.Factory.StartNew(delegate ()
{
Thread.Sleep(msendons);
controlTcp.sendLine(cmd);
});
}
else
{
controlTcp.sendLine(cmd);
}
}
catch (Exception ex)
{
LogUtil.URSError("" + LogName + "SendCMD出错啦cmd[" + cmd + "]msendons[" + msendons + "]" + ex.ToString());
}
return true;
}
private static void OnControlRevice(string message)
{
if (message == null || message.Equals(""))
{
return;
}
try
{
//Connected: Universal Robots Dashboard Server
message = message.Replace("\n", "");
if (IsRun.Equals(false))
{
LogUtil.URSInfo(LogName + "收到数据:" + message);
}
if (message.ToLower().IndexOf(REV_PowerOn) >= 0)
{
SendCMD(CMD_brakeRelease, 1000);
}
else if (message.ToLower().IndexOf(REV_BrakeRelease) >= 0)
{
SendCMD(CMD_robotMode, 2000);
}
else if (message.ToLower().IndexOf(REV_RobotMode) >= 0)
{
ModeProcess(message);
}
else if (message.ToLower().IndexOf(REV_SafetyMode) >= 0)
{
SafetyProcess(message);
}
else
{
//急停模式
}
}
catch (Exception ex)
{
LogUtil.URSError(LogName + " OnControlRevice出错啦" + ex.ToString());
}
}
private static void ModeProcess(string message)
{
if (IsRun)
{
return;
}
string msg = message.ToLower().Replace(REV_RobotMode + ": ", "").ToUpper().Trim();
if (msg.Equals(URStatus.POWER_ON))
{
SendCMD(CMD_brakeRelease, 1000);
}
else if (msg.Equals(URStatus.RUNNING) || msg.Equals(URStatus.IDLE))
{
WarnMsg = "";
LogUtil.URSInfo(LogName + " 当前状态:" + msg + ",机器人连接成功!");
LogUtil.info(LogName + " 当前状态:" + msg + ",机器人连接成功!");
IsRun = true;
reconnectTimer.Enabled = true;
}
else
{
if (!IsTimeOut(message))
{
LogUtil.URSInfo(LogName + "当前状态:" + msg + ",发送打开命令" + CMD_powerOn);
SendCMD(CMD_powerOn, 1000);
}
else
{
WarnMsg = "机器人状态[" + msg + "],启动超时";
StopRobot();
}
}
}
private static void SafetyProcess(string message)
{
if (IsRun)
{
return;
}
string msg = message.ToLower().Replace(REV_SafetyMode + ": ", "").ToUpper().Trim();
if (msg.Equals(URStatus.SFETY_POWER_OFF) || msg.Equals(URStatus.ROBOT_EMERGENCY_STOP))
{
if (!IsTimeOut(message))
{
WarnMsg = "机器人急停中[" + msg + "],请先打开急停";
SendCMD(CMD_Safetymode, 1000);
}
else
{
WarnMsg = "机器人急停中[" + msg + "],启动超时";
StopRobot();
}
}
else
{
SendCMD(CMD_robotMode, 500);
}
}
private static void ModeEProcess(string mode)
{
if (!IsRun)
{
return;
}
mode = mode.Replace("ROBOT_MODE_", "").ToUpper().Trim();
if (mode.Equals(URStatus.POWER_ON))
{
SendCMD(CMD_brakeRelease, 1000);
}
else if (mode.Equals(URStatus.RUNNING) || mode.Equals(URStatus.IDLE))
{
}
else
{
WarnMsg = "机器人状态[" + mode + "],尝试自动复位";
LogUtil.info(LogName + WarnMsg);
IsRun = false;
IsStartConnect = true;
PreStartTime = DateTime.Now;
SendCMD(CMD_powerOn, 500);
}
}
private static void SafetyEProcess(string safetyStatus)
{
if (!IsRun)
{
return;
}
safetyStatus = safetyStatus.Replace("SAFETY_MODE_", "").ToUpper().Trim();
if (safetyStatus.Equals(URStatus.SFETY_POWER_OFF))
{
WarnMsg = "机器人急停中[" + safetyStatus + "],请复位";
LogUtil.info(LogName + WarnMsg);
StopRobot();
}
else if (safetyStatus.Equals(URStatus.ROBOT_EMERGENCY_STOP))
{
WarnMsg = "机器人急停中[" + safetyStatus + "],请复位";
LogUtil.info(LogName + WarnMsg);
StopRobot();
}
else
{
// SendCMD(CMD_robotMode, 500);
}
}
private static bool IsTimeOut(string status)
{
TimeSpan span = DateTime.Now - PreStartTime;
if (span.TotalMilliseconds > StartTimeOutSeconds)
{
string logMsg = LogName + "【" + status + "】 已持续" + Math.Round(span.TotalMilliseconds) + ",启动超时!";
LogUtil.info(logMsg);
LogUtil.URSInfo(logMsg);
return true;
}
return false;
}
private static void stopTcp()
{
try
{
IsRun = false;
startCount--;
//controlTcp.sendLine(CMD_powerOff);
controlTcp.close();
}
catch (Exception ex)
{
LogUtil.error(LogName + "stopTcp出错啦" + ex.ToString());
}
}
/// <summary>
/// 停止机器人,断开连接
/// </summary>
public static void StopRobot()
{
try
{
ClearPreCMD();
IsStartConnect = false;
reconnectTimer.Enabled = false;
URRobotClient.StopListen();
stopTcp();
}
catch (Exception ex)
{
LogUtil.error(LogName + "StopEpson出错啦" + ex.ToString());
}
}
/// <summary>
/// 释放所有轴,切换为手移方式
/// </summary>
public static void FreeAxis()
{
}
/// <summary>
/// 锁定轴,改为程序模式
/// </summary>
public static void LockAxis()
{
}
public static bool PointIsValid(URPointValue point)
{
if (point.X != 0 && point.Y != 0 && point.Z != 0)
{
return true;
}
return false;
}
public static void MoveTo(URPointValue point, bool isHigh)
{
if (!PointIsValid(point))
{
LogUtil.info(LogName + "试图移动到:" + point.ToShowStr() + ",此坐标无效,直接返回");
return;
}
//movep(p[0.062, -0.030, 0.325, 0.0299, 2.2263, 2.2171], a = 1.2, v = 0.25, r = 0)
//movej([0, 1.57, -1.57, 3.14, -1.57, 1.57],a = 1.4, v = 1.05, t = 0, r = 0)
string x = Math.Round(point.X / 1000F, 5).ToString();
string y = Math.Round(point.Y / 1000F, 5).ToString();
string z = Math.Round(point.Z / 1000F, 5).ToString();
string moveCmd = "movep(p[" + x + spiltStr + y + spiltStr + z + spiltStr + point.RX + spiltStr + point.RY + spiltStr + point.RZ + "],a=0.1, v=0.1, r=0)";
if (isHigh)
{
moveCmd = "movep(p[" + x + spiltStr + y + spiltStr + z + spiltStr + point.RX + spiltStr + point.RY + spiltStr + point.RZ + "],a=1.0, v=1.0, r=0)";
}
ClearPreCMD();
URRobotClient.LastMoveCMD = moveCmd;
LastSendPoint = point;
}
public static void ClearPreCMD()
{
if (!URRobotClient.LastMoveCMD.Equals(""))
{
LogUtil.info(LogName + ",清除上次未发送指令:【" + URRobotClient.LastMoveCMD + "】");
URRobotClient.LastMoveCMD = "";
}
}
public static void MoveTo(URPointValue point)
{
MoveTo(point, false);
}
public static bool MoveOK(URPointValue point, DateTime moveTime)
{
URPointValue lastPoint = GetLastPosition();
if (lastPoint.UpdateTime > moveTime)
{
if (IsSamePoint(point, lastPoint))
{
return true;
}
}
return false;
}
public static bool IsSamePoint(URPointValue p1, URPointValue p2)
{
if (p1 == null && p2 == null)
{
return false;
}
double xCha = Math.Abs(p1.X - p2.X);
double yCha = Math.Abs(p1.Y - p2.Y);
double zCha = Math.Abs(p1.Z - p2.Z);
//double rxCha = Math.Abs(p1.RX - p2.RX);
//double ryCha = Math.Abs(p1.RY - p2.RY);
//double rzCha = Math.Abs(p1.RZ - p2.RZ);
double RPow1 = Math.Pow(p1.RX, 2) + Math.Pow(p1.RY, 2) + Math.Pow(p1.RZ, 2);
double RPow2 = Math.Pow(p2.RX, 2) + Math.Pow(p2.RY, 2) + Math.Pow(p2.RZ, 2);
if (xCha < 1 && yCha < 1 && zCha < 1)
{
return true;
}
//if (xCha < 1 && yCha < 1 && zCha < 1 && rxCha < 0.02 && ryCha < 0.05 && rzCha < 0.05)
//{
// return true;
//}
return false;
}
public static URPointValue GetLastPosition()
{
return LastPoint;
}
}
public class URPointValue
{
public URPointValue()
{
this.X = 0;
this.Y = 0;
this.Z = 0;
this.RX = 0;
this.RY = 0;
this.RZ = 0;
this.UpdateTime = DateTime.Now;
}
public URPointValue(double x, double y, double z, double rx, double ry, double rz)
{
// TODO: Complete member initialization
this.X = Math.Round(x, 2);
this.Y = Math.Round(y, 2);
this.Z = Math.Round(z, 2);
this.RX = Math.Round(rx, 4);
this.RY = Math.Round(ry, 4);
this.RZ = Math.Round(rz, 4);
this.UpdateTime = DateTime.Now;
}
/// <summary>
/// 更新的时间
/// </summary>
public DateTime UpdateTime { get; set; }
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public double RX { get; set; }
public double RY { get; set; }
public double RZ { get; set; }
public string ToShowStr()
{
return "[X: " + X + ",Y: " + Y + ",Z: " + Z + ",RX: " + RX + ",RY:" + RY + ",RZ:" + RZ + "]";
}
public string ToJosonStr()
{
string jsonStr = JsonHelper.SerializeObject(this);
return jsonStr;
}
public static URPointValue ToObject(string json)
{
return JsonHelper.DeserializeJsonToObject<URPointValue>(json);
}
}
}
public struct URStatus
{
public static string NO_CONTROLLER = "NO_CONTROLLER";
public static string DISCONNECTED = "DISCONNECTED";
public static string CONFIRM_SAFETY = "CONFIRM_SAFETY";
public static string BOOTING = "BOOTING";
public static string POWER_OFF = "POWER_OFF";
public static string POWER_ON = "POWER_ON";
public static string IDLE = "IDLE";
public static string BACKDRIVE = "BACKDRIVE";
public static string RUNNING = "RUNNING";
/// <summary>
/// ROBOT_EMERGENCY_STOP 急停
/// </summary>
public static string ROBOT_EMERGENCY_STOP = "ROBOT_EMERGENCY_STOP";
/// <summary>
/// FAULT 急停
/// </summary>
public static string FAULT = "FAULT";
/// <summary>
/// POWER_OFF 外部急停
/// </summary>
public static string SFETY_POWER_OFF = "POWER_OFF";
}