-
Notifications
You must be signed in to change notification settings - Fork 26
/
FanucExtendedStrategy.cs
708 lines (565 loc) · 27.5 KB
/
FanucExtendedStrategy.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
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
using System.Diagnostics;
using l99.driver.@base;
namespace l99.driver.fanuc.strategies
{
public class FanucExtendedStrategy : FanucStrategy
{
private enum StrategyStateEnum
{
UNKNOWN,
OK,
FAILED
}
private enum SegmentEnum
{
NONE,
BEGIN,
ROOT,
PATH,
AXIS,
SPINDLE,
END
}
protected Dictionary<string, dynamic> propertyBag;
protected List<dynamic> focasInvocations = new List<dynamic>();
protected int failedInvocationCountDuringSweep = 0;
protected Stopwatch sweepWatch = new Stopwatch();
protected int sweepRemaining = 1000;
private SegmentEnum _currentInitSegment = SegmentEnum.NONE;
private SegmentEnum _currentCollectSegment = SegmentEnum.NONE;
private IntermediateModelGenerator _intermediateModel;
private StrategyStateEnum _strategyState = StrategyStateEnum.UNKNOWN;
public FanucExtendedStrategy(Machine machine, object cfg) : base(machine, cfg)
{
sweepRemaining = sweepMs;
propertyBag = new Dictionary<string, dynamic>();
_intermediateModel = new IntermediateModelGenerator();
}
protected void catchFocasPerformance(dynamic focasNativeReturnObject)
{
focasInvocations.Add(new
{
focasNativeReturnObject.method,
focasNativeReturnObject.invocationMs,
focasNativeReturnObject.rc
});
if (focasNativeReturnObject.rc != 0)
{
failedInvocationCountDuringSweep++;
}
}
private dynamic? getCurrentPropertyBagKey()
{
switch (_currentCollectSegment)
{
case SegmentEnum.NONE:
case SegmentEnum.BEGIN:
case SegmentEnum.ROOT:
case SegmentEnum.END:
return "none";
case SegmentEnum.PATH:
return Get("current_path");
case SegmentEnum.AXIS:
return string.Join("/", Get("axis_split"));
case SegmentEnum.SPINDLE:
return string.Join("/", Get("spindle_split"));
}
return "none";
}
public dynamic? Get(string propertyBagKey)
{
if (propertyBag.ContainsKey(propertyBagKey))
{
return propertyBag[propertyBagKey];
}
else
{
return null;
}
}
public dynamic? GetKeyed(string propertyBagKey)
{
return Get($"{propertyBagKey}+{getCurrentPropertyBagKey()}");
}
public bool Has(string propertyBagKey)
{
return propertyBag.ContainsKey(propertyBagKey);
}
public bool HasKeyed(string propertyBagKey)
{
return Has($"{propertyBagKey}+{getCurrentPropertyBagKey()}");
}
public async Task<dynamic?> Set(string propertyBagKey, dynamic? value)
{
return await set(propertyBagKey, value, false, false);
}
public async Task<dynamic?> SetKeyed(string propertyBagKey, dynamic? value)
{
return await set($"{propertyBagKey}+{getCurrentPropertyBagKey()}", value, false, false);
}
public async Task<dynamic?> SetNative(string propertyBagKey, dynamic? value)
{
return await set(propertyBagKey, value, true, false);
}
public async Task<dynamic?> SetNativeKeyed(string propertyBagKey, dynamic? value)
{
return await set($"{propertyBagKey}+{getCurrentPropertyBagKey()}", value, true, false);
}
public async Task<dynamic?> SetNativeAndPeel(string propertyBagKey, dynamic? value)
{
return await set(propertyBagKey, value, true, true);
}
private async Task<dynamic?> set(string propertyBagKey, dynamic? value, bool nativeResponse = true, bool peel = true)
{
if (propertyBag.ContainsKey(propertyBagKey))
{
propertyBag[propertyBagKey] = value;
if (nativeResponse == true)
return await handleNativeResponsePropertyBagAssignment(propertyBagKey, value, peel);
}
else
{
propertyBag.Add(propertyBagKey, value);
if (nativeResponse == true)
return await handleNativeResponsePropertyBagAssignment(propertyBagKey, value, peel);
}
return value;
}
private async Task<dynamic?> handleNativeResponsePropertyBagAssignment(string key, dynamic value, bool peel)
{
catchFocasPerformance(value);
if (!peel)
return value;
return await this.peel(key, value);
}
private async Task<dynamic?> peel(string veneerKey, dynamic input, params dynamic?[] additionalInputs)
{
switch (_currentCollectSegment)
{
case SegmentEnum.NONE:
break;
case SegmentEnum.BEGIN:
return await machine.PeelVeneerAsync(veneerKey, input, additionalInputs);
case SegmentEnum.ROOT:
return await machine.PeelVeneerAsync(veneerKey, input, additionalInputs);
case SegmentEnum.PATH:
return await machine.PeelAcrossVeneerAsync(Get("current_path"),veneerKey, input, additionalInputs);
case SegmentEnum.AXIS:
return await machine.PeelAcrossVeneerAsync(Get("axis_split"), veneerKey, input, additionalInputs);
case SegmentEnum.SPINDLE:
return await machine.PeelAcrossVeneerAsync(Get("spindle_split"), veneerKey, input, additionalInputs);
case SegmentEnum.END:
return await machine.PeelVeneerAsync(veneerKey, input, additionalInputs);
}
return null;
}
public async Task<dynamic?> Peel(string veneerKey, params dynamic[] inputs)
{
if (inputs.Length == 0)
{
return null;
}
else if (inputs.Length == 1)
{
return await peel(veneerKey, inputs[0]);
}
else
{
return await peel(veneerKey, inputs[0], inputs.Skip(1).Take(inputs.Length - 1).ToArray());
}
}
public async Task Apply(string veneerType, string veneerName, bool isCompound = false, bool isInternal = false)
{
Type t = Type.GetType($"l99.driver.fanuc.veneers.{veneerType}");
await Apply(t, veneerName, isCompound, isInternal);
}
public async Task Apply(Type veneerType, string veneerName, bool isCompound = false, bool isInternal = false)
{
switch (_currentInitSegment)
{
case SegmentEnum.NONE:
break;
case SegmentEnum.BEGIN:
break;
case SegmentEnum.ROOT:
machine.ApplyVeneer(veneerType, veneerName, isCompound, isInternal);
_intermediateModel.AddRootItem(veneerName, veneerType);
break;
case SegmentEnum.PATH:
machine.ApplyVeneerAcrossSlices(veneerType, veneerName, isCompound, isInternal);
_intermediateModel.AddPathItem(veneerName, veneerType);
break;
case SegmentEnum.AXIS:
machine.ApplyVeneerAcrossSlices(Get("current_path"), veneerType, veneerName, isCompound, isInternal);
_intermediateModel.AddAxisItem(veneerName, veneerType);
break;
case SegmentEnum.SPINDLE:
machine.ApplyVeneerAcrossSlices(Get("current_path"), veneerType, veneerName, isCompound, isInternal);
_intermediateModel.AddSpindleItem(veneerName, veneerType);
break;
case SegmentEnum.END:
break;
}
}
public override async Task SweepAsync(int delayMs = -1)
{
sweepRemaining = sweepMs - (int)sweepWatch.ElapsedMilliseconds;
if (sweepRemaining < 0)
{
sweepRemaining = sweepMs;
}
logger.Trace($"[{machine.Id}] Sweep delay: {sweepRemaining}ms");
await base.SweepAsync(sweepRemaining);
}
public override async Task<dynamic?> InitializeAsync()
{
int initMinutes = 0;
var initStopwatch = new Stopwatch();
initStopwatch.Start();
logger.Info($"[{machine.Id}] Strategy initializing.");
try
{
_currentInitSegment = SegmentEnum.NONE;
while (!machine.VeneersApplied)
{
// connect focas
dynamic connect = await platform.ConnectAsync();
// init strategy if able to connect
if (connect.success)
{
// build intermediate model
_intermediateModel.Start(machine);
#region init root veneers
_currentInitSegment = SegmentEnum.ROOT;
await Apply(typeof(veneers.FocasPerf), "focas_perf", isInternal:true, isCompound: true);
await Apply(typeof(veneers.Connect), "connect", isInternal: true);
await Apply(typeof(veneers.GetPath), "paths", isInternal: true);
// init root veneers in user strategy
await InitRootAsync();
#endregion
#region init path veneers
_currentInitSegment = SegmentEnum.PATH;
// retrieve controller paths
var paths = await platform.GetPathAsync();
var path_numbers = Enumerable
.Range(
(int) paths.response.cnc_getpath.path_no,
(int) paths.response.cnc_getpath.maxpath_no)
.ToList()
.ConvertAll(x => (short)x);
// following veneers will be applied over each path
machine.SliceVeneer(path_numbers.Cast<dynamic>());
await Apply(typeof(veneers.RdAxisname), "axis_names", isInternal: true);
await Apply(typeof(veneers.RdSpindlename), "spindle_names", isInternal: true);
// init path veneers in user strategy
await InitPathsAsync();
#endregion
#region init axis+spindle veneers
//_currentInitSegment = SegmentEnum.AXIS;
// iterate paths
foreach(var current_path in path_numbers)
{
// build intermediate model
_intermediateModel.AddPath(current_path);
// set current path
dynamic path = await platform.SetPathAsync(current_path);
// read axes and spindles for current path
dynamic axes = await platform.RdAxisNameAsync();
dynamic spindles = await platform.RdSpdlNameAsync();
dynamic axis_spindle_slices = new List<dynamic> { };
// axes - get fields from focas response
var fields_axes = axes.response
.cnc_rdaxisname.axisname.GetType().GetFields();
for (int x = 0; x <= axes.response.cnc_rdaxisname.data_num - 1; x++)
{
// get axis name
var axis = fields_axes[x]
.GetValue(axes.response.cnc_rdaxisname.axisname);
// build intermediate model
_intermediateModel.AddAxis(current_path, axisName(axis));
axis_spindle_slices.Add(axisName(axis));
}
// spindles - get fields from focas response
var fields_spindles = spindles.response
.cnc_rdspdlname.spdlname.GetType().GetFields();
for (int x = 0; x <= spindles.response.cnc_rdspdlname.data_num - 1; x++)
{
// get spindle name
var spindle = fields_spindles[x]
.GetValue(spindles.response.cnc_rdspdlname.spdlname);
// build intermediate model
_intermediateModel.AddSpindle(current_path, spindleName(spindle));
axis_spindle_slices.Add(spindleName(spindle));
};
// following veneers will be applied over axes+spindles
machine.SliceVeneer(
current_path,
axis_spindle_slices.ToArray());
// store current path
await Set("current_path", current_path);
// init axis veneers in user strategy
_currentInitSegment = SegmentEnum.AXIS;
await InitAxisAsync();
// init spindle veneers in user strategy
_currentInitSegment = SegmentEnum.SPINDLE;
await InitSpindleAsync();
}
#endregion
await PostInitAsync();
// disconnect focas
dynamic disconnect = await platform.DisconnectAsync();
machine.VeneersApplied = true;
_currentInitSegment = SegmentEnum.NONE;
// build intermediate model
_intermediateModel.Finish();
await machine.Handler.OnGenerateIntermediateModelAsync(_intermediateModel.Model);
await machine.Transport.OnGenerateIntermediateModelAsync(_intermediateModel.Model);
logger.Info($"[{machine.Id}] Strategy initialized.");
}
else
{
if (initMinutes == 0 || initStopwatch.ElapsedMilliseconds > 60000)
{
logger.Warn($"[{machine.Id}] Strategy initialization pending ({initMinutes} min).");
initMinutes++;
initStopwatch.Restart();
}
await Task.Delay(sweepMs);
}
}
}
catch (Exception ex)
{
logger.Error(ex, $"[{machine.Id}] Strategy initialization failed.");
}
initStopwatch.Stop();
return null;
}
public virtual async Task PostInitAsync()
{
}
/// <summary>
/// Applied Veneers:
/// FocasPerf as "focas_perf",
/// Connect as "connect",
/// GetPath as "paths"
/// </summary>
public virtual async Task InitRootAsync()
{
}
public virtual async Task InitUserRootAsync()
{
}
/// <summary>
/// Applied Veneers:
/// FocasPerf as "focas_perf",
/// Connect as "connect",
/// GetPath as "paths",
/// RdAxisname as "axis_names",
/// RdSpindlename as "spindle_names"
/// </summary>
public virtual async Task InitPathsAsync()
{
}
public virtual async Task InitUserPathsAsync()
{
}
/// <summary>
/// Applied Veneers:
/// FocasPerf as "focas_perf",
/// Connect as "connect",
/// GetPath as "paths",
/// RdAxisname as "axis_names",
/// RdSpindlename as "spindle_names"
/// </summary>
public virtual async Task InitAxisAsync()
{
}
public virtual async Task InitSpindleAsync()
{
}
public virtual async Task InitUserAxisAndSpindleAsync(short current_path)
{
}
public override async Task<dynamic?> CollectAsync()
{
try
{
_currentInitSegment = SegmentEnum.NONE;
focasInvocations.Clear();
failedInvocationCountDuringSweep = 0;
_currentCollectSegment = SegmentEnum.BEGIN;
if(await CollectBeginAsync())
{
if (_strategyState == StrategyStateEnum.UNKNOWN)
{
logger.Info($"[{machine.Id}] Strategy started.");
_strategyState = StrategyStateEnum.OK;
}
else if (_strategyState == StrategyStateEnum.FAILED)
{
logger.Info($"[{machine.Id}] Strategy recovered.");
_strategyState = StrategyStateEnum.OK;
}
_currentInitSegment = SegmentEnum.ROOT;
_currentCollectSegment = SegmentEnum.ROOT;
await SetNativeAndPeel("paths",
await platform.GetPathAsync());
await InitUserRootAsync();
await CollectRootAsync();
_currentInitSegment = SegmentEnum.PATH;
await InitUserPathsAsync();
_currentInitSegment = SegmentEnum.AXIS;
for (short current_path = Get("paths").response.cnc_getpath.path_no;
current_path <= Get("paths").response.cnc_getpath.maxpath_no;
current_path++)
{
_currentCollectSegment = SegmentEnum.PATH;
await Set("current_path", current_path);
await InitUserAxisAndSpindleAsync(current_path);
await Set("path", await platform.SetPathAsync(current_path));
dynamic path_marker = PathMarker(Get("path").request.cnc_setpath.path_no);
dynamic path_marker_full = new[] {path_marker};
machine.MarkVeneer(current_path, path_marker_full);
await SetNativeKeyed($"axis_names",
await platform.RdAxisNameAsync());
await Peel("axis_names",
GetKeyed($"axis_names"));
var fields_axes = GetKeyed($"axis_names")
.response.cnc_rdaxisname.axisname.GetType().GetFields();
short axis_count = GetKeyed($"axis_names")
.response.cnc_rdaxisname.data_num;
string[] axis_names = new string[axis_count];
for (short i = 0; i < axis_count; i++)
{
axis_names[i] = axisName(fields_axes[i]
.GetValue(GetKeyed($"axis_names")
.response.cnc_rdaxisname.axisname));
}
await SetNativeKeyed($"spindle_names",
await platform.RdSpdlNameAsync());
await SetNativeAndPeel("spindle_names",
GetKeyed($"spindle_names"));
var fields_spindles = GetKeyed($"spindle_names")
.response.cnc_rdspdlname.spdlname.GetType().GetFields();
short spindle_count = GetKeyed($"spindle_names")
.response.cnc_rdspdlname.data_num;
string[] spindle_names = new string[spindle_count];
for (short i = 0; i < spindle_count; i++)
{
spindle_names[i] = spindleName(fields_spindles[i]
.GetValue(GetKeyed($"spindle_names")
.response.cnc_rdspdlname.spdlname));
}
await CollectForEachPathAsync(current_path, axis_names, spindle_names, path_marker_full);
for (short current_axis = 1; current_axis <= axis_names.Length; current_axis ++)
{
_currentCollectSegment = SegmentEnum.AXIS;
dynamic axis_name = axis_names[current_axis-1];
//Debug.Print($"PATH:{current_path} AXIS:{axis_name}");
dynamic axis_marker = axisMarker(current_axis, axis_name);
dynamic axis_marker_full = new[] {path_marker, axis_marker};
await Set("axis_split", new[] {current_path.ToString(), axis_name});
machine.MarkVeneer(Get("axis_split"), axis_marker_full);
await CollectForEachAxisAsync(current_path, current_axis, axis_name, Get("axis_split"), axis_marker_full);
}
for (short current_spindle = 1; current_spindle <= spindle_names.Length; current_spindle ++)
{
_currentCollectSegment = SegmentEnum.SPINDLE;
dynamic spindle_name = spindle_names[current_spindle-1];
//Debug.Print($"PATH:{current_path} SPINDLE:{spindle_name}");
dynamic spindle_marker = spindleMarker(current_spindle, spindle_name);
dynamic spindle_marker_full = new[] {path_marker, spindle_marker};
await Set("spindle_split", new[] {current_path.ToString(), spindle_name});
machine.MarkVeneer(Get("spindle_split"), spindle_marker_full);
await CollectForEachSpindleAsync(current_path, current_spindle, spindle_name, Get("spindle_split"), spindle_marker_full);
};
}
}
else
{
if (_strategyState == StrategyStateEnum.UNKNOWN || _strategyState == StrategyStateEnum.OK)
{
logger.Warn($"[{machine.Id}] Strategy failed to connect.");
_strategyState = StrategyStateEnum.FAILED;
}
}
_currentInitSegment = SegmentEnum.NONE;
_currentCollectSegment = SegmentEnum.END;
await CollectEndAsync();
}
catch (Exception ex)
{
logger.Error(ex, $"[{machine.Id}] Strategy sweep failed at segment {_currentCollectSegment}.");
}
return null;
}
/// <summary>
/// Available Data:
/// Connect => get("connect") (after base is called)
/// </summary>
public virtual async Task<bool> CollectBeginAsync()
{
sweepWatch.Restart();
await SetNativeAndPeel("connect", await platform.ConnectAsync());
return Get("connect").success;
}
/// <summary>
/// Available Data:
/// Connect => get("connect"),
/// GetPath => get("paths")
/// </summary>
public virtual async Task CollectRootAsync()
{
}
/// <summary>
/// Available Data:
/// Connect => get("connect"),
/// GetPath => get("paths"),
/// RdAxisName => get("axis_names"),
/// RdSpdlName => get("spindle_names")
/// </summary>
public virtual async Task CollectForEachPathAsync(short current_path, string[] axis, string[] spindle, dynamic path_marker)
{
}
/// <summary>
/// Available Data:
/// Connect => get("connect"),
/// GetPath => get("paths"),
/// RdAxisName => get("axis_names"),
/// RdSpdlName => get("spindle_names")
/// </summary>
public virtual async Task CollectForEachAxisAsync(short current_path, short current_axis, string axis_name, dynamic axis_split, dynamic axis_marker)
{
}
/// <summary>
/// Available Data:
/// Connect => get("connect"),
/// GetPath => get("paths"),
/// RdAxisName => get("axis_names"),
/// RdSpdlName => get("spindle_names")
/// </summary>
public virtual async Task CollectForEachSpindleAsync(short current_path, short current_spindle, string spindle_name, dynamic spindle_split, dynamic spindle_marker)
{
}
/// <summary>
/// Available Data:
/// Connect => get("connect"),
/// GetPath => get("paths"),
/// RdAxisName => get("axis_names"),
/// RdSpdlName => get("spindle_names"),
/// Disconnect => get("disconnect") (after base is called)
/// </summary>
public virtual async Task CollectEndAsync()
{
await SetNative("disconnect", await platform.DisconnectAsync());
await machine.PeelVeneerAsync("focas_perf", new
{
sweepMs = sweepWatch.ElapsedMilliseconds, focas_invocations = focasInvocations
});
//TODO: make veneer
LastSuccess = Get("connect").success;
IsHealthy = failedInvocationCountDuringSweep == 0;
}
}
}