ABBRobotManager.cs
6.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
using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.IOSystemDomain;
using ABB.Robotics.Controllers.RapidDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ABBRobotTest
{
public class ABBRobotManager
{
private NetworkScanner scanner = null;
// private Controller controller = null;
private Task[] tasks = null;
private NetworkWatcher networkwatcher = null;
private Dictionary<string, Controller> controllerMap = new Dictionary<string, Controller>();
public void LoadController()
{
this.scanner = new NetworkScanner();
this.scanner.Scan();
ControllerInfoCollection controllers = scanner.Controllers;
int i = 0;
foreach (ControllerInfo controllerInfo in controllers)
{
Controller con = ControllerFactory.CreateFrom(controllerInfo);
con.Logon(UserInfo.DefaultUser);
ControllerState state = con.State;
controllerMap.Add(con.IPAddress.ToString(), con);
// controller = con;
i++;
}
this.networkwatcher = new NetworkWatcher(scanner.Controllers);
this.networkwatcher.Found += new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
this.networkwatcher.Lost += new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
this.networkwatcher.EnableRaisingEvents = true;
}
private void HandleLostEvent(object sender, NetworkWatcherEventArgs e)
{
}
private void HandleFoundEvent(object sender, NetworkWatcherEventArgs e)
{
this.Invoke(new
EventHandler<NetworkWatcherEventArgs>(AddControllerToListView),
new Object[] { this, e });
}
private void Invoke(EventHandler<NetworkWatcherEventArgs> eventHandler, object[] v)
{
throw new NotImplementedException();
}
private void AddControllerToListView(object sender, NetworkWatcherEventArgs e)
{
//ControllerInfo controllerInfo = e.Controller; ListViewItem item
// = new ListViewItem(controllerInfo.IPAddress.ToString());
//item.SubItems.Add(controllerInfo.Id); item.SubItems.Add(controllerInfo.Availability.ToString()); item.SubItems.Add(controllerInfo.IsVirtual.ToString()); item.SubItems.Add(controllerInfo.SystemName); item.SubItems.Add(controllerInfo.Version.ToString()); item.SubItems.Add(controllerInfo.ControllerName); this.listView1.Items.Add(item); item.Tag
// = controllerInfo;
}
private void StartRobot(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 StopRobot(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);
}
}
private void SetRobotSignal(string ip, string name)
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
// string name = comboBox1.Text;
Signal singalValue = controller.IOSystem.GetSignal(name);
DigitalSignal digitalSig = (DigitalSignal)singalValue;
digitalSig.Set();
}
}
private void ResetRobotSignal(string ip, string name)
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
//string name = comboBox1.Text;
Signal singalValue = controller.IOSystem.GetSignal(name);
DigitalSignal digitalSig = (DigitalSignal)singalValue;
digitalSig.Reset();
}
}
public string GetRobotState(string ip)
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
ControllerState state = controller.State;
return state.ToString();
}
return "";
}
private Controller GetControllerByIP(string ip)
{
if (controllerMap.ContainsKey(ip))
{
return controllerMap[ip];
}
return null;
}
private void DisposeControllers()
{
foreach (Controller con in controllerMap.Values)
{
con.Logoff();
con.Dispose();
}
controllerMap.Clear();
}
}
}