-
Notifications
You must be signed in to change notification settings - Fork 12
/
CONTROLLER.cs
1739 lines (1256 loc) · 50.9 KB
/
CONTROLLER.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Ports;
using System.Reflection;
using System.Text;
using System.Threading;
using CNC_Assist.PlanetCNC;
using LibUsbDotNet;
using LibUsbDotNet.Main;
namespace CNC_Assist
{
/// <summary>
/// Êëàññ ðàáîòû ñ êîíòðîëëåðîì
/// </summary>
public static class Controller
{
#region Èíèöèàëèçàöèÿ ñîáûòèé êîíòðîëëåðà
public delegate void DeviceEventConnect(object sender); // óâåäîìëåíèå îá óñòàíîâêè ñâÿçè
public delegate void DeviceEventDisconnect(object sender, DeviceEventArgsMessage e); // óâåäîìëåíèå îá îáðûâå/ïðåêðàùåíèè ñâÿçè
public delegate void DeviceEventNewData(object sender); // óâåäîìëåíèå ÷òî ïîëó÷åíû íîâûå äàííûå êîíòðîëëåðîì
public delegate void DeviceEventNewMessage(object sender, DeviceEventArgsMessage e); // äëÿ ïîñûëêè óïðàâëÿþùåé ïðîãðàììå ñîîáùåíèé î äåéñòâèÿõ
/// <summary>
/// Ñîáûòèå ïðè óñïåøíîì ïîäêëþ÷åíèè ê êîíòðîëëåðó
/// </summary>
public static event DeviceEventConnect WasConnected;
/// <summary>
/// Ñîáûòèå ïðè îòêëþ÷åíèè îò êîíòðîëëåðà, èëè ðàçðûâå ñâÿçè ñ êîíòðîëëåðîì
/// </summary>
public static event DeviceEventDisconnect WasDisconnected;
/// <summary>
/// Ïîëó÷åíû íîâûå äàííûå îò êîíòðîëëåðà
/// </summary>
public static event DeviceEventNewData NewDataFromController;
/// <summary>
/// Ïîñûëêà ñòðîêè îïèñàíèÿ (äëÿ âåäåíèÿ ëîãîâ)
/// </summary>
public static event DeviceEventNewMessage Message;
#endregion
#region Îáùèå êîìàíäû, âíå çàâèñèìîñòè îò êîíòðîëëåðà
/// <summary>
/// Âîçâðàùàåò èíôîðìàöèþ î íàëè÷èè ñâÿçè
/// </summary>
public static bool Connected
{
get
{
return _connected;
}
}
/// <summary>
/// Äëÿ îïðåäåëåíèÿ òîãî èñïîëüçóåòñÿ, ëè êîíòðîëëåð êàêèì-ëèáî ïîòîêîì. ×èñòî ôîðìàëüíî.
/// ÷òî-áû â ýòîò ìîìåíò äðóãèå êëèåíòû íå ïîäêëþ÷àëèñü ê êîíòðîëëåðó
/// </summary>
public static bool Locked
{
get { return _lockIsSet; }
set { _lockIsSet = value; }
}
#region Êîìàíäû ïîäêëþ÷åíèÿ/îòêëþ÷åíèÿ îò êîíòðîëëåðà
/// <summary>
/// Óñòàíîâêà ñâÿçè ñ êîíòðîëëåðîì
/// </summary>
public static void Connect()
{
if (Connected)
{
AddMessage("Ñîåäèíåíèå óæå óñòàíîâëåíî!");
return;
}
if (_theads != null)
{
if (_theads.IsBusy)
{
AddMessage("Ïîâòîðíîå ïîäêëþ÷åíèå íåâîçìîæíî, ïîêà òåêóùåå íå áóäåò ïðåðâàíî!");
return;
}
}
_lockIsSet = false;
_connected = false;
//Ñîçäàåì ïîòîê
Thread workerThread = new Thread(TheadsStart);
workerThread.Start();
}
/// <summary>
/// Îòêëþ÷åíèå îò êîíòðîëëåðà
/// </summary>
public static void Disconnect()
{
AddMessage("Ïðåêðàùåíèå ñâÿçè ñ êîíòðîëëåðîì!");
_connected = false;
}
/// <summary>
/// Ïåðåïîäêëþ÷åíèå ê êîíòðîëëåðó
/// </summary>
public static void Reconnect()
{
AddMessage("Âûçîâ ìåòîäà ïåðåïîäêëþ÷åíèÿ ê êîíòðîëëåðó");
Disconnect();
Connect();
}
#endregion
#region Ïîòîê âûïîëíåíèÿ çàäàíèÿ
//Ïîòîê äëÿ âûïîëíåíèÿ çàäàíèé
private static void TheadsStart()
{
AddMessage("Çàïóñê ïîòîêà...............");
AddMessage("-Ïîäêëþ÷åíèå ê êîíòðîëëåðó " + GlobalSetting.AppSetting.Controller);
if (GlobalSetting.AppSetting.Controller == ControllerModel.PlanetCNC_MK1 ||
GlobalSetting.AppSetting.Controller == ControllerModel.PlanetCNC_MK2)
{
//ïîïûòêà ïîäêëþ÷åíèÿ
if (!Get_connect_PlanetCNC())
{
AddMessage("-ïîäêëþ÷åíèå íå óäàëîñü");
AddMessage("Çàâåðøåíèå ïîòîêà.................");
_connected = false;
return;
}
_connected = true;
if (WasConnected != null) WasConnected(null); //âûçîâåì ñîáûòèå ÷òî ïîäêëþ÷èëèñü
// Äëÿ îòñëåæèâàíèÿ èçìåíåíèé îò êîíòðîëëåðà
byte[] oldInfoFromController = new byte[64];
//int indexCommand = -1; // òåêóùàÿ êîìàíäà
// òóò ïîòîê âèñèò ïîêà íå ïðîèçîéäåò çàâåðøåíèå ðàáîòû
while (_connected)
{
//1) Ïðîâåðèì íå ïðèñëàë ëè íàì êîíòðîëëåð êàêèõ ëèáî äàííûõ
byte[] readBuffer = new byte[64];
int bytesRead;
_ec = _usbReader.Read(readBuffer, 2000, out bytesRead);
if (_ec != ErrorCode.None)
{
_connected = false;
if (WasDisconnected != null) WasDisconnected(null, new DeviceEventArgsMessage(@"Îøèáêà ïîëó÷åíèÿ äàííûõ ñ êîíòðîëëåðà, ñâÿçü ðàçîðâàíà!"));
}
if (bytesRead > 0 && readBuffer[0] == 0x01 && !CompareArray(oldInfoFromController, readBuffer))
{
INFO.rawData = readBuffer;
ParseInfo(readBuffer);
if (GlobalSetting.AppSetting.debugLevel == 1)
{
//äîáàâèì çàïèñü â ôàéë
string fileDebug = string.Format("{0}\\debug.log",
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
string ss = GetTextFromBinary(readBuffer);
File.AppendAllText(fileDebug, ss, Encoding.UTF8);
//File.AppendText() // fileDebug, ss, Encoding.UTF8);
//StreamWriter sw = new StreamWriter(fileDebug,Encoding.UTF8);
}
oldInfoFromController = readBuffer;
if (NewDataFromController != null) NewDataFromController(null); //ñîáûòèå î ïîëó÷åíèè íîâûõ äàííûõ
}
}
UsbDevice.Exit();
_usbReader = null;
_usbWriter = null;
_myUsbDevice = null;
//return;
}
//if (GlobalSetting.AppSetting.Controller == ControllerModel.Arduino_Gerber09)
//{
// string StringError = "Ïîääåðæêà äàííîãî êîíòðîëëåðà åù¸ íå ðåàëèçîâàíà.";
// _connected = false;
// AddMessage(StringError);
// //çàïóñòèì ñîáûòèå î ðàçðûâå ñâÿçè
// if (WasDisconnected != null) WasDisconnected(null, new DeviceEventArgsMessage(StringError));
//}
if (WasDisconnected != null) WasDisconnected(null, new DeviceEventArgsMessage("")); //ñîáûòèå çàâåðøåíèÿ ðàáîòû
AddMessage("Çàâåðøåíèå ïîòîêà.................");
} //îêîí÷àíèå ïîòîêà ðàáîòû ñ êîíòðîëëåðàìè
private static string GetTextFromBinary(byte[] _bytes)
{
string returnValue = @"[" + DateTime.Now.ToString("O") + "] ";
//è äîáàâèì áàéòû â âèäå ñòðîê
foreach (byte VARIABLE in _bytes)
{
string sByte = VARIABLE.ToString("X2");
returnValue += sByte + " ";
}
returnValue += Environment.NewLine;
return returnValue;
}
#endregion
/// <summary>
/// Ïðîâåðêà íàëè÷èÿ ñâÿçè, è íåçàíÿòîñòè êîíòðîëëåðà
/// </summary>
/// <returns>áóëåâî, âîçìîæíî ëè ïîñûëàòü êîíòðîëëåðó çàäà÷è</returns>
public static bool TestAllowActions()
{
if (!Connected) return false;
if (_lockIsSet) return false;
return true;
}
#endregion
#region ïàðàìåòðû
/// <summary>
/// Íàëè÷èå ñâÿçè ñ êîíòðîëëåðîì
/// </summary>
private static volatile bool _connected = false;
/// <summary>
/// Äëÿ îïðåäåëåíèÿ òîãî èñïîëüçóåòñÿ, ëè êîíòðîëëåð êàêèì-ëèáî ïîòîêîì. ×èñòî ôîðìàëüíî.
/// ÷òî-áû â ýòîò ìîìåíò äðóãèå êëèåíòû íå ïîäêëþ÷àëèñü ê êîíòðîëëåðó
/// </summary>
private static bool _lockIsSet;
/// <summary>
/// Ïîòîê äëÿ ïîëó÷åíèÿ, ïîñûëêè äàííûõ â êîíòðîëëåð
/// </summary>
private static BackgroundWorker _theads;
// äëÿ êîíòðîëëåðà planet-cnc
private static UsbDevice _myUsbDevice;
private static ErrorCode _ec;
private static UsbEndpointReader _usbReader;
private static UsbEndpointWriter _usbWriter;
// äëÿ àðäóèíî êîíòðîëëåðà
private static SerialPort _serialPort;
/// <summary>
/// Èíôîðìàöèÿ î ïàðàìåòðàõ êîíòðîëëåðà
/// </summary>
public static volatile deviceInfo INFO = new deviceInfo();
/// <summary>
/// Èíôîðìàöèÿ î ïðèìåíåíèè ñìåùåíèÿ
/// </summary>
public static volatile correctionPos CorrectionPos = new correctionPos();
#endregion
#region Planet-CNC
private static bool Get_connect_PlanetCNC()
{
//vid 2121 pid 2130 â äåñÿòè÷íîé ñèñòåìå áóäåò êàê 8481 è 8496 ñîîòâåòñòâåííî
UsbDeviceFinder myUsbFinder = new UsbDeviceFinder(8481, 8496);
// Ïîïûòàåìñÿ óñòàíîâèòü ñâÿçü
_myUsbDevice = UsbDevice.OpenUsbDevice(myUsbFinder);
if (_myUsbDevice == null)
{
string StringError = "Íå íàéäåí ïîäêëþ÷åííûé êîíòðîëëåð.";
_connected = false;
AddMessage(StringError);
//çàïóñòèì ñîáûòèå î ðàçðûâå ñâÿçè
if (WasDisconnected != null) WasDisconnected(null, new DeviceEventArgsMessage(StringError));
return false;
}
IUsbDevice wholeUsbDevice = _myUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// This is a "whole" USB device. Before it can be used,
// the desired configuration and interface must be selected.
// Select config #1
wholeUsbDevice.SetConfiguration(1);
// Claim interface #0.
wholeUsbDevice.ClaimInterface(0);
}
// open read endpoint 1.
_usbReader = _myUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
// open write endpoint 1.
_usbWriter = _myUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
return true;
}
#endregion
#region ðàçáîð
/// <summary>
/// Âûïîëíåíèå G-êîäà
/// </summary>
/// <param name="command">ñòðîêà ñ G-êîäîì</param>
public static void ExecuteCommand(string command)
{
DataRow dataRowNow = new DataRow(0, command);
DataLoader.FillStructure(PlanetCNC_Controller.LastStatus, ref dataRowNow);
if (dataRowNow.Machine.SpindelON != PlanetCNC_Controller.LastStatus.Machine.SpindelON || dataRowNow.Machine.SpeedSpindel != PlanetCNC_Controller.LastStatus.Machine.SpeedSpindel)
{
Controller.SendBinaryData(BinaryData.pack_B5(dataRowNow.Machine.SpindelON, 2, BinaryData.TypeSignal.Hz, dataRowNow.Machine.SpeedSpindel));
//çàôèêñèðóåì
PlanetCNC_Controller.LastStatus = dataRowNow;
}
if (dataRowNow.POS.X != PlanetCNC_Controller.LastStatus.POS.X || dataRowNow.POS.Y != PlanetCNC_Controller.LastStatus.POS.Y || dataRowNow.POS.Z != PlanetCNC_Controller.LastStatus.POS.Z || dataRowNow.POS.Z != PlanetCNC_Controller.LastStatus.POS.Z)
{
// int xtmp = Controller.INFO.NuberCompleatedInstruction + 10;
// íå áóäåì äàâàòü âîçìîæíîñòü ñëàòü áîëåå ÷åì äàííîå êîëè÷åñòâî êîììàíä
//if (dataRowNow.numberRow>xtmp) return;
Controller.SendBinaryData(BinaryData.pack_CA(Controller.INFO.CalcPosPulse("X", dataRowNow.POS.X),
Controller.INFO.CalcPosPulse("Y", dataRowNow.POS.Y),
Controller.INFO.CalcPosPulse("Z", dataRowNow.POS.Z),
Controller.INFO.CalcPosPulse("A", dataRowNow.POS.A),
dataRowNow.Machine.SpeedMañhine,
dataRowNow.numberRow));
//çàôèêñèðóåì
PlanetCNC_Controller.LastStatus = dataRowNow;
}
}
/// <summary>
/// Ïàðñèò ïîëó÷åííûå äàííûå ñ êîíòðîëëåðà
/// </summary>
/// <param name="readBuffer"></param>
private static void ParseInfo(IList<byte> readBuffer)
{
if (GlobalSetting.DISSABLE_CHECK != true)
{
// ïîëó÷èì çíà÷åíèå ñêîðîñòè äâèæåíèÿ
int ttm = (int)(((readBuffer[22] * 65536) + (readBuffer[21] * 256) + (readBuffer[20])) / 2.1);
if (ttm > 5000) return;
}
//TODO: èíîãäà â ÌÊ2 áûâàåò ãëþê, ïîýòîìó çàùèòèìñÿ îò íåãî, êîñòûëåì
//if (readBuffer[10] == 0x58 && readBuffer[11] == 0x02 && readBuffer[22] == 0x20 && readBuffer[23] == 0x02) return;
INFO.FreebuffSize = readBuffer[1];
INFO.shpindel_MoveSpeed = 0;
if (GlobalSetting.AppSetting.Controller == ControllerModel.PlanetCNC_MK1)
{
INFO.shpindel_MoveSpeed = (int)(((readBuffer[22] * 65536) + (readBuffer[21] * 256) + (readBuffer[20])) / 2.1);
}
if (GlobalSetting.AppSetting.Controller == ControllerModel.PlanetCNC_MK2)
{
INFO.shpindel_MoveSpeed = (int)(((readBuffer[22] * 65536) + (readBuffer[21] * 256) + (readBuffer[20])) / 1.341);
}
INFO.shpindel_MoveSpeedSumm = (INFO.shpindel_MoveSpeedSumm + INFO.shpindel_MoveSpeed)/2;
INFO.shpindel_STOPPED = INFO.shpindel_MoveSpeedSumm == 0;
INFO.AxesX_PositionPulse = (readBuffer[27] * 16777216) + (readBuffer[26] * 65536) + (readBuffer[25] * 256) + (readBuffer[24]);
INFO.AxesY_PositionPulse = (readBuffer[31] * 16777216) + (readBuffer[30] * 65536) + (readBuffer[29] * 256) + (readBuffer[28]);
INFO.AxesZ_PositionPulse = (readBuffer[35] * 16777216) + (readBuffer[34] * 65536) + (readBuffer[33] * 256) + (readBuffer[32]);
INFO.AxesA_PositionPulse = (readBuffer[39] * 16777216) + (readBuffer[38] * 65536) + (readBuffer[37] * 256) + (readBuffer[36]);
INFO.AxesX_LimitMax = (readBuffer[15] & (1 << 0)) != 0;
INFO.AxesX_LimitMin = (readBuffer[15] & (1 << 1)) != 0;
INFO.AxesY_LimitMax = (readBuffer[15] & (1 << 2)) != 0;
INFO.AxesY_LimitMin = (readBuffer[15] & (1 << 3)) != 0;
INFO.AxesZ_LimitMax = (readBuffer[15] & (1 << 4)) != 0;
INFO.AxesZ_LimitMin = (readBuffer[15] & (1 << 5)) != 0;
INFO.NuberCompleatedInstruction = readBuffer[9] * 16777216 + (readBuffer[8] * 65536) + (readBuffer[7] * 256) + (readBuffer[6]);
SuperByte bb = new SuperByte(readBuffer[19]);
INFO.shpindel_Enable = bb.Bit0;
SuperByte bb2 = new SuperByte(readBuffer[14]);
INFO.Estop = bb2.Bit7;
}
private static void AddMessage(string ss)
{
if (Message != null) Message(null, new DeviceEventArgsMessage(ss));
}
/// <summary>
/// Ôóíêöèÿ ñðàâíåíèÿ äâóõ ìàññèâîâ (âîçâðàò: Èñòèíà - îäèíàêîâûå, Ëîæü - ðàçëè÷àþòñÿ)
/// </summary>
/// <param name="arr1">ïåðâûé ìàññèâ</param>
/// <param name="arr2">âòîðîé ìàññèâ</param>
/// <returns>Èñòèíà - îäèíàêîâûå ìàññèâû, Ëîæü - ðàçëè÷àþùèåñÿ</returns>
private static bool CompareArray(byte[] arr1, byte[] arr2)
{
if (arr1 == null || arr2 == null) return false;
//ïðîâåðÿåì 64 áàéòà
bool value = true;
for (int i = 0; i < 64; i++)
{
if (arr1[i] != arr2[i])
{
value = false;
break;
}
}
return value;
}
#endregion
#region Ïîñëàíèÿ äàííûõ â êîíòðîëëåð
/// <summary>
/// Ïîñûëêà â êîíòðîëëåð áèíàðíûõ äàííûõ
/// </summary>
/// <param name="data">Íàáîð äàííûõ</param>
/// <param name="checkBuffSize">Ïðîâåðÿòü ëè ðàçìåð äîñòóïíîãî áóôôåðà êîíòðîëëåðà</param>
public static void SendBinaryData(byte[] data, bool checkBuffSize = true)
{
// if (checkBuffSize && (INFO.FreebuffSize < 2))
// {
// //òóò íóæíî çàâèñíóòü ïîêà áóôåð íå îñâîáîäèòüñÿ
// //TODO: ïåðåä âûïîëíåíèåì ïðîâåðÿòü áóôåð íà çàíÿòîñòü....
// }
// ReSharper disable once SuggestVarOrType_BuiltInTypes
// ReSharper disable once RedundantAssignment
int bytesWritten = 64;
//if (Setting.DeviceModel != DeviceModel.Emulator)
//{
_ec = _usbWriter.Write(data, 2000, out bytesWritten);
//}
// else
// {
//TODO: äîáàâèòü ïîñûëêó âèðòóàëüíîìó
// }
}
/// <summary>
/// Ïîñûëêà àâàðèéíîé îñòàíîâêè
/// </summary>
public static void EnergyStop()
{
SendBinaryData(BinaryData.pack_AA(), false);
}
/// <summary>
/// Çàïóñê äâèæåíèÿ áåç îñòàíîâêè
/// </summary>
/// <param name="x">Îñü Õ (äîñòóïíûå çíà÷åíèÿ "+" "0" "-")</param>
/// <param name="y">Îñü Y (äîñòóïíûå çíà÷åíèÿ "+" "0" "-")</param>
/// <param name="z">Îñü Z (äîñòóïíûå çíà÷åíèÿ "+" "0" "-")</param>
/// <param name="speed"></param>
public static void StartManualMove(string x, string y, string z, int speed)
{
if (!Connected)
{
//stringError = "Äëÿ âûêëþ÷åíèÿ øïèíäåëÿ, íóæíî âíà÷àëå óñòàíîâèòü ñâÿçü ñ êîíòðîëëåðîì";
//return false;
return;
}
//if (!IsFreeToTask)
//{
// return;
//}
SuperByte axesDirection = new SuperByte(0x00);
//ïîñòàâèì íóæíûå áèòû
if (x == "-") axesDirection.SetBit(0, true);
if (x == "+") axesDirection.SetBit(1, true);
if (y == "-") axesDirection.SetBit(2, true);
if (y == "+") axesDirection.SetBit(3, true);
if (z == "-") axesDirection.SetBit(4, true);
if (z == "+") axesDirection.SetBit(5, true);
//DataClear();
//DataAdd(BinaryData.pack_BE(axesDirection.valueByte, speed));
SendBinaryData(BinaryData.pack_BE(axesDirection.ValueByte, speed,x,y,z));
//Task_Start();
}
public static void StopManualMove()
{
if (!Connected)
{
//stringError = "Äëÿ âûêëþ÷åíèÿ øïèíäåëÿ, íóæíî âíà÷àëå óñòàíîâèòü ñâÿçü ñ êîíòðîëëåðîì";
//return false;
}
byte[] buff = BinaryData.pack_BE(0x00, 0);
//TODO: ðàçîáðàòüñÿ äëÿ ÷åãî, ýòîò áàéò
buff[22] = 0x01;
//DataClear();
//DataAdd(buff);
SendBinaryData(buff);
//Task_Start();
}
/// <summary>
/// Óñòàíîâêà â êîíòðîëëåð, íîâîãî ïîëîæåíèÿ ïî îñÿì
/// </summary>
/// <param name="x">Ïîëîæåíèå â èìïóëüñàõ</param>
/// <param name="y">Ïîëîæåíèå â èìïóëüñàõ</param>
/// <param name="z">Ïîëîæåíèå â èìïóëüñàõ</param>
/// <param name="a">Ïîëîæåíèå â èìïóëüñàõ</param>
public static void DeviceNewPosition(int x, int y, int z, int a)
{
if (!TestAllowActions()) return;
SendBinaryData(BinaryData.pack_C8(x, y, z,a));
}
/// <summary>
/// Óñòàíîâêà â êîíòðîëëåð, íîâîãî ïîëîæåíèÿ ïî îñÿì â ììèëèìåòðàõ
/// </summary>
/// <param name="x">â ìèëëèìåòðàõ</param>
/// <param name="y">â ìèëëèìåòðàõ</param>
/// <param name="z">â ìèëëèìåòðàõ</param>
// ReSharper disable once UnusedMember.Global
public static void DeviceNewPosition(decimal x, decimal y, decimal z, decimal a)
{
if (!TestAllowActions()) return;
SendBinaryData(BinaryData.pack_C8(INFO.CalcPosPulse("X", x), INFO.CalcPosPulse("Y", y), INFO.CalcPosPulse("Z", z), INFO.CalcPosPulse("A", a)));
}
public static void ResetToZeroAxes(string nameAxes)
{
switch (nameAxes)
{
case "X":
DeviceNewPosition(0, INFO.AxesY_PositionPulse, INFO.AxesZ_PositionPulse, INFO.AxesA_PositionPulse);
break;
case "Y":
DeviceNewPosition(INFO.AxesX_PositionPulse, 0, INFO.AxesZ_PositionPulse, INFO.AxesA_PositionPulse);
break;
case "Z":
DeviceNewPosition(INFO.AxesX_PositionPulse, INFO.AxesY_PositionPulse, 0, INFO.AxesA_PositionPulse);
break;
case "A":
DeviceNewPosition(INFO.AxesX_PositionPulse, INFO.AxesY_PositionPulse, INFO.AxesZ_PositionPulse, 0);
break;
}
}
#endregion
}
public class deviceInfo
{
/// <summary>
/// Ñûðûå äàííûå îò êîíòðîëëåðà
/// </summary>
public byte[] rawData = new byte[64];
/// <summary>
/// Ðàçìåð äîñòóïíîãî áóôåðà â êîíòðîëëåðå
/// </summary>
public byte FreebuffSize = 0;
/// <summary>
/// Íîìåð âûïîëíåííîé èíñòðóêöèè
/// </summary>
public int NuberCompleatedInstruction = 0;
/// <summary>
/// Òåêóùåå ïîëîæåíèå â èìïóëüñàõ
/// </summary>
public int AxesX_PositionPulse = 0;
/// <summary>
/// Òåêóùåå ïîëîæåíèå â èìïóëüñàõ
/// </summary>
public int AxesY_PositionPulse = 0;
/// <summary>
/// Òåêóùåå ïîëîæåíèå â èìïóëüñàõ
/// </summary>
public int AxesZ_PositionPulse = 0;
/// <summary>
/// Òåêóùåå ïîëîæåíèå â èìïóëüñàõ
/// </summary>
public int AxesA_PositionPulse = 0;
//public static int AxesX_PulsePerMm = 400;
//public static int AxesY_PulsePerMm = 400;
//public static int AxesZ_PulsePerMm = 400;
//ñðàáàòûâàíèå ñåíñîðà
public bool AxesX_LimitMax = false;
public bool AxesX_LimitMin = false;
public bool AxesY_LimitMax = false;
public bool AxesY_LimitMin = false;
public bool AxesZ_LimitMax = false;
public bool AxesZ_LimitMin = false;
public bool AxesA_LimitMax = false;
public bool AxesA_LimitMin = false;
public int shpindel_MoveSpeed = 0;
public bool shpindel_Enable = false;
public bool Estop = false;
/// <summary>
/// Äàííûé ïàðàìåòð âû÷èñëÿåòñÿ óñðåäíåííèåì çíà÷åíèé ñêîðîñòè, èç âûáîðêè ïîñëåäíèõ 10 çíà÷åíèåé
/// Åñëè ýòî çíà÷åíèå ðàâíî 0 òî "shpindel_STOPPED = true" èíà÷å çíà÷åíèå ðàâíî "ëîæü"
/// </summary>
public bool shpindel_STOPPED = true;
public int shpindel_MoveSpeedSumm = 0;
public decimal AxesX_PositionMM
{
get
{
return (decimal)Controller.INFO.AxesX_PositionPulse / GlobalSetting.ControllerSetting.AxleX.CountPulse;
}
}
public decimal AxesY_PositionMM
{
get
{
return (decimal)Controller.INFO.AxesY_PositionPulse / GlobalSetting.ControllerSetting.AxleY.CountPulse;
}
}
public decimal AxesZ_PositionMM
{
get
{
return (decimal)Controller.INFO.AxesZ_PositionPulse / GlobalSetting.ControllerSetting.AxleZ.CountPulse;
}
}
public decimal AxesA_PositionMM
{
get
{
return (decimal)Controller.INFO.AxesA_PositionPulse / GlobalSetting.ControllerSetting.AxleA.CountPulse;
}
}
/// <summary>
/// Âû÷èñëåíèå ïîëîæåíèÿ â èìïóëüñàõ, ïðè óêàçàíèè îñè, è ïîëîæåíèÿ â ìèëëèìåòðàõ
/// </summary>
/// <param name="axes">èìÿ îñè X,Y,Z</param>
/// <param name="posMm">ïîëîæåíèå â ìì</param>
/// <returns>Êîëè÷åñòâî èìïóëüñîâ</returns>
public int CalcPosPulse(string axes, decimal posMm)
{
if (axes == "X") return (int)(posMm * GlobalSetting.ControllerSetting.AxleX.CountPulse);
if (axes == "Y") return (int)(posMm * GlobalSetting.ControllerSetting.AxleY.CountPulse);
if (axes == "Z") return (int)(posMm * GlobalSetting.ControllerSetting.AxleZ.CountPulse);
if (axes == "A") return (int)(posMm * GlobalSetting.ControllerSetting.AxleA.CountPulse);
return 0;
}
}
/// <summary>
/// Àðãóìåíòû äëÿ ñîáûòèÿ
/// </summary>
public class DeviceEventArgsMessage
{
protected string _str;
public string Message
{
get { return _str; }
set { _str = value;}
}
public DeviceEventArgsMessage(string Str)
{
_str = Str;
}
}
/// <summary>
/// Ñòàòóñû ðàáîòû ñ óñòðîéñòâîì
/// </summary>
public enum EStatusDevice { Connect = 0, Disconnect };
/// <summary>
/// Êëàññ äëÿ ïîëó÷èíèÿ áèíàðíûõ äàííûõ
/// </summary>
public static class BinaryData
{
/// <summary>
/// Äàííàÿ êîìàíäà ïîêà íåïîíÿòíà....
/// </summary>
/// <param name="byte05"></param>
/// <returns></returns>
public static byte[] pack_C0(byte byte05)
{
byte[] buf = new byte[64];
buf[0] = 0xC0;
buf[5] = byte05;
return buf;
}
public static byte[] pack_C2()
{
byte[] buf = new byte[64];
buf[0] = 0xC2;
buf[4] = 0x80;
buf[5] = 0x03;
return buf;
}
public enum TypeSignal
{
None,
Hz,
RC
};
/// <summary>
/// Óïðàâëåíèå ðàáîòîé øïèíäåëÿ
/// </summary>
/// <param name="shpindelON">Âêë/Âûêëþ÷åí</param>
/// <param name="numShimChanel">íîìåð êàíàëà 1,2, èëè 3</param>
/// <param name="ts">Òèï ñèãíàëà</param>
/// <param name="SpeedShim">Çíà÷åíèå îïðåäåëÿþùåå ôîðìó ñèãíàëà</param>
/// <returns></returns>
public static byte[] pack_B5(bool shpindelON = false, int numShimChanel = 0, TypeSignal ts = TypeSignal.None, int SpeedShim = 0)
{
int tmpSpeed = SpeedShim;
//÷òî-áû íå ïðèâûñèòü ìàêñèìóì, ïîñëå êîòîðîãî êîíòðîëëåð âûðóáàåòñÿ ñ îøèáêîé...
if (tmpSpeed > 65000) tmpSpeed = 65000;
byte[] buf = new byte[64];
buf[0] = 0xB5;
buf[4] = 0x80;
if (shpindelON)
{
buf[5] = 0x02;
}
else
{
buf[5] = 0x01;
}
buf[6] = 0x01; //õ.ç.
switch (numShimChanel)
{
case 2:
{
buf[8] = 0x02;
break;
}
case 3:
{
buf[8] = 0x03;
break;
}
default:
{
buf[8] = 0x00; //äîñòóïåí òîëüêî 2 è 3 êàíàë, îñòàëüíûå íå ïîäõîäÿò....
break;
}
}
switch (ts)
{
case TypeSignal.Hz:
{
buf[9] = 0x01;
break;
}
case TypeSignal.RC:
{
buf[9] = 0x02;
break;
}
default:
{
buf[9] = 0x00;
break;
}
}
int itmp = tmpSpeed;
buf[10] = (byte)(itmp);
buf[11] = (byte)(itmp >> 8);
buf[12] = (byte)(itmp >> 16);
//buf[10] = 0xFF;
//buf[11] = 0xFF;
//buf[12] = 0x04;
//çàôèêñèðóåì
PlanetCNC_Controller.LastStatus.Machine.SpindelON = shpindelON;
return buf;
}
public static byte[] pack_B6(bool chanel2ON = false,bool chanel3ON = false)
{
byte[] buf = new byte[64];
buf[0] = 0xB6;
buf[4] = 0x80;
if (chanel2ON)
{
buf[5] = 0x02;
}
else
{
buf[5] = 0x01;
}
if (chanel3ON)
{
buf[7] = 0x02;
}
else
{
buf[7] = 0x01;
}
//çàôèêñèðóåì
PlanetCNC_Controller.LastStatus.Machine.Chanel2ON = chanel2ON;
PlanetCNC_Controller.LastStatus.Machine.Chanel3ON = chanel3ON;
return buf;
}
/// <summary>
/// Àâàðèéíàÿ îñòàíîâêà
/// </summary>
/// <returns></returns>
public static byte[] pack_AA()