-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathProgram.cs
272 lines (217 loc) · 11.6 KB
/
Program.cs
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
using EtherCAT.NET;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using EtherCAT.NET.Infrastructure;
using EtherCAT.NET.Extension;
namespace SampleMaster
{
class Program
{
static async Task Main(string[] args)
{
/* Set interface name. Edit this to suit your needs. */
var interfaceName = "eth0";
/* Set ESI location. Make sure it contains ESI files! The default path is /home/{user}/.local/share/ESI */
var localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var esiDirectoryPath = Path.Combine(localAppDataPath, "ESI");
Directory.CreateDirectory(esiDirectoryPath);
/* Copy native file. NOT required in end user scenarios, where EtherCAT.NET package is installed via NuGet! */
var codeBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Directory.EnumerateFiles(Path.Combine(codeBase, "runtimes"), "*soem_wrapper.*", SearchOption.AllDirectories).ToList().ForEach(filePath =>
{
if (filePath.Contains(RuntimeEnvironment.RuntimeArchitecture))
File.Copy(filePath, Path.Combine(codeBase, Path.GetFileName(filePath)), true);
});
/* create logger */
var loggerFactory = LoggerFactory.Create(loggingBuilder =>
{
loggingBuilder.SetMinimumLevel(LogLevel.Debug);
loggingBuilder.AddConsole();
});
var logger = loggerFactory.CreateLogger("EtherCAT Master");
/* create EtherCAT master settings (with 10 Hz cycle frequency) */
var settings = new EcSettings(cycleFrequency: 10U, esiDirectoryPath, interfaceName);
/* scan available slaves */
var rootSlave = EcUtilities.ScanDevices(settings.InterfaceName);
rootSlave.Descendants().ToList().ForEach(slave =>
{
// If you have special extensions for this slave, add it here:
// slave.Extensions.Add(new MyFancyExtension());
/*################ Sample code START ##################
// Example code to add SDO write request during initialization
// to Beckhoff "EL3021"
if (slave.ProductCode == 0xBCD3052)
{
var dataset = new List<object>();
dataset.Add((byte)0x01);
var requests = new List<SdoWriteRequest>()
{
// Index 0x8000 sub index 6: Filter on
new SdoWriteRequest(0x8000, 0x6, dataset)
};
slave.Extensions.Add(new InitialSettingsExtension(requests));
}
################## Sample code END #################*/
EcUtilities.CreateDynamicData(settings.EsiDirectoryPath, slave);
});
/* print list of slaves */
var message = new StringBuilder();
var slaves = rootSlave.Descendants().ToList();
message.AppendLine($"Found {slaves.Count()} slaves:");
foreach (var slave in slaves)
{
message.AppendLine($"{slave.DynamicData.Name} (PDOs: {slave.DynamicData.Pdos.Count} - CSA: {slave.Csa})");
}
logger.LogInformation(message.ToString().TrimEnd());
/* create variable references for later use */
var variables = slaves.SelectMany(child => child.GetVariables()).ToList();
/* create EC Master (short sample) */
using (var master = new EcMaster(settings, logger))
{
try
{
master.Configure(rootSlave);
}
catch (Exception ex)
{
logger.LogError(ex.Message);
throw;
}
/* start master */
var random = new Random();
var cts = new CancellationTokenSource();
var task = Task.Run(() =>
{
var sleepTime = 1000 / (int)settings.CycleFrequency;
while (!cts.IsCancellationRequested)
{
master.UpdateIO(DateTime.UtcNow);
unsafe
{
if (variables.Any())
{
var myVariableSpan = new Span<int>(variables.First().DataPtr.ToPointer(), 1);
myVariableSpan[0] = random.Next(0, 100);
}
}
Thread.Sleep(sleepTime);
}
}, cts.Token);
/* wait for stop signal */
Console.ReadKey(true);
cts.Cancel();
await task;
}
return; /* remove this to run real world sample*/
/* create EC Master (real world sample) */
using (var master = new EcMaster(settings, logger))
{
try
{
master.Configure(rootSlave);
}
catch (Exception ex)
{
logger.LogError(ex.Message);
throw;
}
/*################ Sample code START ##################
// Beckhoff EL2004 (4 channel digital output)
var eL2004 = new DigitalOut(slaves[1]);
eL2004.SetChannel(1, false);
eL2004.SetChannel(2, true);
eL2004.SetChannel(3, false);
eL2004.SetChannel(4, true);
// Beckhoff EL1014 (4 channel digital input)
var eL1014 = new DigitalIn(slaves[2]);
// Beckhoff EL3021 (1 channel analog input - 16bit)
var pdoAnalogIn = slaves[3].DynamicData.Pdos;
var varAnalogIn = pdoAnalogIn[0].Variables.Where(x => x.Name == "Value").First();
var varUnderrange = pdoAnalogIn[0].Variables.Where(x => x.Name == "Status__Underrange").First();
var varOverrange = pdoAnalogIn[0].Variables.Where(x => x.Name == "Status__Overrange").First();
// Beckhoff EL3021 SDO read (index: 0x8000 sub index: 0x6)
var datasetFilter = new byte[2];
EcUtilities.SdoRead(master.Context, 4, 0x8000, 6, ref datasetFilter);
var filterOn = BitConverter.ToBoolean(datasetFilter, 0);
logger.LogInformation($"EL3021 filter on: {filterOn}");
################## Sample code END #################*/
/* start master */
var random = new Random();
var cts = new CancellationTokenSource();
var task = Task.Run(() =>
{
var sleepTime = 1000 / (int)settings.CycleFrequency;
while (!cts.IsCancellationRequested)
{
master.UpdateIO(DateTime.UtcNow);
/*################ Sample code START ##################
// Beckhoff EL2004 toggle digital output for ch1 and ch3
eL2004.ToggleChannel(2);
eL2004.ToggleChannel(4);
// Beckhoff EL1014 read digital input state
logger.LogInformation($"EL1014 channel 1 input: {eL1014.GetChannel(1)}");
logger.LogInformation($"EL1014 channel 2 input: {eL1014.GetChannel(2)}");
logger.LogInformation($"EL1014 channel 3 input: {eL1014.GetChannel(3)}");
logger.LogInformation($"EL1014 channel 4 input: {eL1014.GetChannel(4)}");
// Beckhoff EL2004 read digital output state
logger.LogInformation($"EL1014 channel 1 output: {eL2004.GetChannel(1)}");
logger.LogInformation($"EL1014 channel 2 output: {eL2004.GetChannel(2)}");
logger.LogInformation($"EL1014 channel 3 output: {eL2004.GetChannel(3)}");
logger.LogInformation($"EL1014 channel 4 output: {eL2004.GetChannel(4)}");
// Beckhoff EL3021 SDO read (index: 0x6000 sub index: 0x2)
// overrange of 12 bit analog input.
var slaveIndex = (ushort)(Convert.ToUInt16(slaves.ToList().IndexOf(slaves[3])) + 1);
var dataset1 = new byte[2];
EcUtilities.SdoRead(master.Context, slaveIndex, 0x6000, 2, ref dataset1);
bool overrange = BitConverter.ToBoolean(dataset1, 0);
logger.LogInformation($"EL3021 overrange: {overrange}");
// Beckhoff EL3021 SDO read (index: 0x6000 sub index: 0x1)
// underrange of 12 bit analog input.
var dataset2 = new byte[2];
EcUtilities.SdoRead(master.Context, slaveIndex, 0x6000, 1, ref dataset2);
bool underrange = BitConverter.ToBoolean(dataset2, 0);
logger.LogInformation($"EL3021 underrange: {underrange}");
################## Sample code END #################*/
unsafe
{
if (variables.Any())
{
/*################ Sample code START ##################
// Read analog current from EL3021 (16 bit - PDO)
void* data = varAnalogIn.DataPtr.ToPointer();
int bitmask = (1 << varAnalogIn.BitLength) - 1;
int shift = (*(int*)data >> varAnalogIn.BitOffset) & bitmask;
short analogIn = (short)shift;
logger.LogInformation($"EL3021 analog current in: {analogIn}");
// Read analog current underrange status (1 bit - PDO)
void* dataUnder = varUnderrange.DataPtr.ToPointer();
bitmask = (1 << varUnderrange.BitLength) - 1;
int under = (*(int*)dataUnder >> varUnderrange.BitOffset) & bitmask;
logger.LogInformation($"EL3021 underrange: {under}");
// Read analog current overrange status (1 bit - PDO)
void* dataOver = varOverrange.DataPtr.ToPointer();
bitmask = (1 << varOverrange.BitLength) - 1;
int over = (*(int*)dataOver >> varOverrange.BitOffset) & bitmask;
logger.LogInformation($"EL3021 overrange: {over}");
################## Sample code END #################*/
}
}
Thread.Sleep(sleepTime);
}
}, cts.Token);
/* wait for stop signal */
Console.ReadKey(true);
cts.Cancel();
await task;
}
}
}
}