ABBRobotManager.cs
11.2 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
using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.IOSystemDomain;
using ABB.Robotics.Controllers.RapidDomain;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ABBRobotTest
{
internal class ABBRobotManager
{
internal static string DI_Start = "di_start";//启动程序
internal static string DI_Stop = "di_stop";//停止程序
internal static string DO_ES = "do_ES";//急停状态
internal static string DO_IsRun= "do_IsRun";//机器人程序运行状态
internal static string Do_AutoOn = "do_AutoOn";//是否自动状态
private static NetworkScanner scanner = null;
// private Controller controller = null;
private static Task[] tasks = null;
private static NetworkWatcher networkwatcher = null;
internal static Dictionary<string, Controller> controllerMap = new Dictionary<string, Controller>();
internal static Dictionary<string, ControllerInfo> controllerInfoMap = new Dictionary<string, ControllerInfo>();
internal delegate void ControllerAddDelegate(ControllerInfo controller);
internal static event ControllerAddDelegate ControllerAddEvent;
internal static void LoadController()
{
controllerMap = new Dictionary<string, Controller>();
controllerInfoMap = new Dictionary<string, ControllerInfo>();
scanner = new NetworkScanner();
scanner.Scan();
ControllerInfoCollection controllers = scanner.Controllers;
int i = 0;
foreach (ControllerInfo controllerInfo in controllers)
{
AddControllerInfo(controllerInfo);
i++;
}
networkwatcher = new NetworkWatcher(scanner.Controllers);
networkwatcher.Found += new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
networkwatcher.Lost += new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
networkwatcher.EnableRaisingEvents = true;
}
private static bool AddControllerInfo(ControllerInfo controllerInfo)
{
try
{
Controller con = ControllerFactory.CreateFrom(controllerInfo);
con.Logon(UserInfo.DefaultUser);
ControllerState state = con.State;
string ip = con.IPAddress.ToString();
RemoveController(ip);
controllerMap.Add(ip, con);
// controller = con;
controllerInfoMap.Add(ip, controllerInfo);
LogUtil.info("成功添加机器人【" + ip + "】");
return true;
}catch(Exception ex)
{
LogUtil.error("添加机器人【" + controllerInfo.IPAddress.ToString() + "】失败:" + ex.ToString());
}return false;
}
private static void RemoveController(string ip)
{
if (controllerInfoMap.ContainsKey(ip))
{
controllerInfoMap.Remove(ip);
}
if (controllerMap.ContainsKey(ip))
{
controllerMap.Remove(ip);
}
}
private static void HandleLostEvent(object sender, NetworkWatcherEventArgs e)
{
}
private static void HandleFoundEvent(object sender, NetworkWatcherEventArgs e)
{
//Invoke(new
//EventHandler<NetworkWatcherEventArgs>(AddControllerToListView),
//new Object[] { null, e });
AddControllerToListView(null, e);
}
private static void Invoke(EventHandler<NetworkWatcherEventArgs> eventHandler, object[] v)
{
}
private static void AddControllerToListView(object sender, NetworkWatcherEventArgs e)
{
ControllerInfo controllerInfo = e.Controller;
if (controllerInfo != null)
{
if (AddControllerInfo(controllerInfo))
{
ControllerAddEvent?.Invoke(controllerInfo);
}
}
}
private void StartRobotByTask(string ip)
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null && controller.OperatingMode == ControllerOperatingMode.Auto)
{
tasks = controller.Rapid.GetTasks();
using (Mastership m = Mastership.Request(controller.Rapid))
{
tasks[0].SetProgramPointer("aaa", "main");
controller.State = ControllerState.MotorsOn;
controller.Rapid.Start();
//Perform operation
//tasks[0].Start();
m.Dispose();
}
}
else
{
Console.WriteLine("Automatic mode is required to start execution from a remote client.");
}
}
catch (System.InvalidOperationException ex)
{
Console.WriteLine("Mastership is held by another client." + ex.Message);
}
catch (System.Exception ex)
{
Console.WriteLine("Unexpected error occurred: " + ex.Message);
}
}
private void StopRobotByTask(string ip)
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null && controller.OperatingMode == ControllerOperatingMode.Auto)
{
tasks = controller.Rapid.GetTasks();
using (Mastership m = Mastership.Request(controller.Rapid))
{
//tasks[0].SetProgramPointer("aaa", "main");
//controller.State = ControllerState.MotorsOn;
//controller.Rapid.Start();
//Perform operation
//tasks[0].Start();
controller.Rapid.Stop();
m.Dispose();
}
}
else
{
Console.WriteLine("Automatic mode is required to start execution from a remote client.");
}
}
catch (System.InvalidOperationException ex)
{
Console.WriteLine("Mastership is held by another client." + ex.Message);
}
catch (System.Exception ex)
{
Console.WriteLine("Unexpected error occurred: " + ex.Message);
}
}
internal static void StartRobotBySingle(string ip,string signalName = "di_start",bool isReset=true )
{
if (isReset)
{
StopRobotBySingle(ip);
}
SetRobotSignal(ip, signalName);
Thread.Sleep(100);
ResetRobotSignal(ip, signalName);
}
internal static void StopRobotBySingle(string ip,string signalName = "di_stop")
{
SetRobotSignal(ip, signalName);
Thread.Sleep(100);
ResetRobotSignal(ip, signalName);
}
internal static void SetRobotSignal(string ip, string name)
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
// string name = comboBox1.Text;
Signal singalValue = controller.IOSystem.GetSignal(name);
if (singalValue == null)
{
LogUtil.error("ABBRobot[" + ip + "] GetSignal [" + name + "] =null");
}
else
{
DigitalSignal digitalSig = (DigitalSignal)singalValue;
digitalSig.Set();
}
}
}
catch (Exception ex)
{
LogUtil.error("ABBRobot[" + ip + "] SetRobotSignal [" + name + "] Error:" + ex.ToString());
}
}
internal static void ResetRobotSignal(string ip, string name)
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
//string name = comboBox1.Text;
Signal singalValue = controller.IOSystem.GetSignal(name);
if (singalValue == null)
{
LogUtil.error("ABBRobot[" + ip + "] GetSignal [" + name + "]= null");
}
else
{
DigitalSignal digitalSig = (DigitalSignal)singalValue;
digitalSig.Reset();
}
}
}
catch (Exception ex)
{
LogUtil.error("ABBRobot[" + ip + "] ResetRobotSignal [" + name + "] Error:" + ex.ToString());
}
}
internal static int GetSingalState(string ip, string name = "do_ES")
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
//string name = comboBox1.Text;
Signal singalValue = controller.IOSystem.GetSignal(name);
if (singalValue == null)
{
LogUtil.error("ABBRobot[" + ip + "] GetSingal [" + name + "]= null");
}
else
{
DigitalSignal digitalSig = (DigitalSignal)singalValue;
return (int)digitalSig.Value;
}
}
}
catch (Exception ex)
{
LogUtil.error("ABBRobot[" + ip + "] GetSingalState [" + name + "] Error:" + ex.ToString());
}
return 0;
}
internal static string GetRobotState(string ip)
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
ControllerState state = controller.State;
return state.ToString();
}
}catch(Exception ex)
{
LogUtil.error("ABBRobot[" + ip + "] GetRobotState" +"error:"+ex.ToString());
}
return "";
}
internal static Controller GetControllerByIP(string ip)
{
if (controllerMap.ContainsKey(ip))
{
return controllerMap[ip];
}
LogUtil.error("ABBRobot [" + ip + "] GetControllerByIP= null");
return null;
}
internal static void DisposeControllers()
{
foreach (Controller con in controllerMap.Values)
{
con.Logoff();
con.Dispose();
}
controllerMap.Clear();
}
}
}