-
Notifications
You must be signed in to change notification settings - Fork 16
/
qt-testgui.py
1147 lines (1010 loc) · 39.3 KB
/
qt-testgui.py
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
import argparse
import sys
import time
from functools import partial
from struct import *
import traceback
from PyQt5 import QtGui
from PyQt5.QtCore import QDateTime, Qt, QTimer
from PyQt5.QtWidgets import (
QApplication,
QCheckBox,
QGridLayout,
QLabel,
QListWidget,
QPushButton,
QSlider,
QWidget,
)
import projectLoader
parser = argparse.ArgumentParser()
parser.add_argument("json", help="json config", type=str, default=None)
parser.add_argument("--graph", "-g", help="graph", type=bool, default=False)
parser.add_argument("--baud", "-b", help="baudrate", type=int, default=1000000)
parser.add_argument("--port", "-p", help="udp port", type=int, default=2390)
parser.add_argument("--debug", "-d", help="debug", type=bool, default=False)
parser.add_argument(
"device",
help="device like: /dev/ttyUSB0 | 192.168.10.13",
nargs="?",
type=str,
default=None,
)
args = parser.parse_args()
class CH341():
DEFAULT_TIMEOUT = 1000 # 1000mS for USB timeouts
BULK_WRITE_ENDPOINT = 0x02
BULK_READ_ENDPOINT = 0x82
PACKET_LENGTH = 0x20
MAX_PACKETS = 256
MAX_PACKET_LEN = (PACKET_LENGTH * MAX_PACKETS)
USB_VENDOR = 0x1A86
USB_PRODUCT = 0x5512
CMD_SET_OUTPUT = 0xA1
CMD_IO_ADDR = 0xA2
CMD_PRINT_OUT = 0xA3
CMD_SPI_STREAM = 0xA8
CMD_SIO_STREAM = 0xA9
CMD_I2C_STREAM = 0xAA
CMD_UIO_STREAM = 0xAB
CMD_I2C_STM_STA = 0x74
CMD_I2C_STM_STO = 0x75
CMD_I2C_STM_OUT = 0x80
CMD_I2C_STM_IN = 0xC0
CMD_I2C_STM_MAX = min(0x3F, PACKET_LENGTH)
CMD_I2C_STM_SET = 0x60
CMD_I2C_STM_US = 0x40
CMD_I2C_STM_MS = 0x50
CMD_I2C_STM_DLY = 0x0F
CMD_I2C_STM_END = 0x00
CMD_UIO_STM_IN = 0x00
CMD_UIO_STM_DIR = 0x40
CMD_UIO_STM_OUT = 0x80
CMD_UIO_STM_US = 0xC0
CMD_UIO_STM_END = 0x20
STM_I2C_20K = 0x00
STM_I2C_100K = 0x01
STM_I2C_400K = 0x02
STM_I2C_750K = 0x03
STM_SPI_DBL = 0x04
reverse_table = [
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
]
def __init__(self, vid=USB_VENDOR, pid=USB_PRODUCT):
dev = usb.core.find(idVendor=vid, idProduct=pid)
if dev is None:
raise ConnectionError("Device not found (%x:%x)" % (vid, pid))
print(f'Found CH341 device ({vid:x}:{pid:x})')
if (dev.bNumConfigurations != 1):
raise ConnectionError("Device configuration error")
dev.set_configuration()
self.dev = dev
#cmd = [self.CMD_I2C_STREAM, self.CMD_UIO_STM_US | 100, self.CMD_UIO_STM_END]
#cnt = self.dev.write(self.BULK_WRITE_ENDPOINT, cmd)
#if (cnt != len(cmd)):
# raise ConnectionError("Failed to issue Command")
cmd = [self.CMD_UIO_STREAM, self.CMD_UIO_STM_DIR | 0x3F, self.CMD_UIO_STM_END]
cnt = self.dev.write(self.BULK_WRITE_ENDPOINT, cmd)
if (cnt != len(cmd)):
raise ConnectionError("Failed to issue dir command")
def spi_trans(self, data):
cmd = [self.CMD_UIO_STREAM, self.CMD_UIO_STM_OUT | 0x36, self.CMD_UIO_STM_END]
cnt = self.dev.write(self.BULK_WRITE_ENDPOINT, cmd)
if (cnt != len(cmd)):
raise ConnectionError("Failed to issue select cs")
cmd = [self.CMD_SPI_STREAM]
for c in data:
cmd.append(self.reverse_table[c])
cnt = self.dev.write(self.BULK_WRITE_ENDPOINT, cmd)
#if (cnt != len(cmd)):
# raise ConnectionError("Failed to write data")
data = self.dev.read(self.BULK_READ_ENDPOINT, cnt-1)
cmd = [self.CMD_UIO_STREAM, self.CMD_UIO_STM_OUT | 0x37, self.CMD_UIO_STM_END]
cnt = self.dev.write(self.BULK_WRITE_ENDPOINT, cmd)
if (cnt != len(cmd)):
raise ConnectionError("Failed to issue unselect cs")
ret = []
for c in data:
ret.append(self.reverse_table[c])
return list(ret)
project = projectLoader.load(args.json)
SHM_FILE = ""
SERIAL = ""
NET_IP = ""
SPI_CH341 = None
SPI_FTDI = None
if args.device and args.device.startswith("/dev/tty"):
import serial
SERIAL = args.device
BAUD = args.baud
ser = serial.Serial(SERIAL, BAUD, timeout=0.01)
elif args.device and args.device.startswith("/dev/shm/"):
SHM_FILE = args.device
fd = open(f"{SHM_FILE}.rx", "wb")
fd.write(b'0'*10)
fd.close()
elif args.device and args.device.startswith("CH341"):
import usb.core
import usb.util
SPI_CH341 = CH341()
elif args.device and args.device.startswith("FTDI"):
from pyftdi.spi import SpiController
spi = SpiController(cs_count=2)
spi.configure('ftdi://ftdi:2232h/2')
SPI_FTDI = spi.get_port(cs=0, freq=1E6, mode=0)
elif args.device and args.device != "":
NET_IP = args.device
NET_PORT = args.port
print("IP:", NET_IP)
import socket
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPClientSocket.bind(('0.0.0.0', args.port))
# clear buffer
try:
UDPClientSocket.settimeout(0.2)
UDPClientSocket.recvfrom(100000)
except:
pass
else:
import spidev
bus = 0
device = 1
spi = spidev.SpiDev()
spi.open(bus, device)
spi.max_speed_hz = project["jdata"]["interface"][0].get("max", args.baud)
spi.mode = 0
spi.lsbfirst = False
INTERVAL = 100
data = [0] * (project["data_size"] // 8)
data[0] = 0x74
data[1] = 0x69
data[2] = 0x72
data[3] = 0x77
JOINTS = project["joints"]
VOUTS = project["vouts"]
VINS = project["vins"]
DOUTS = project["douts"]
DINS = project["dins"]
VIN_NAMES = project["vinnames"]
VOUT_NAMES = project["voutnames"]
DIN_NAMES = project["dinnames"]
DOUT_NAMES = project["doutnames"]
JOINT_TYPES = project["jointtypes"]
joints = [0] * project["joints"]
vouts = [0] * project["vouts"]
douts = [0] * project["douts"]
PRU_OSC = int(project["jdata"]["clock"]["speed"])
vout_types = []
for num, vout in enumerate(project["voutnames"]):
vout_types.append(vout["type"])
vin_types = []
for num, vin in enumerate(project["vinnames"]):
vin_types.append(vin["type"])
JOINT_ENABLE_BYTES = project["joints_en_total"] // 8
DIGITAL_OUTPUT_BYTES = project["douts_total"] // 8
DIGITAL_INPUT_BYTES = project["dins_total"] // 8
class modbus_vfd:
FUNCTION_READ = 0x01
FUNCTION_WRITE = 0x02
WRITE_CONTROL_DATA = 0x03
READ_CONTROL_STATUS = 0x04
WRITE_FREQ_DATA = 0x05
LOOP_TEST = 0x08
FUNCTION_PD004 = 4
FUNCTION_PD005 = 5
FUNCTION_PD011 = 11
FUNCTION_PD141 = 141
FUNCTION_PD142 = 142
FUNCTION_PD143 = 143
FUNCTION_PD144 = 144
CONTROL_Run_Fwd = 0x01
CONTROL_Run_Rev = 0x11
CONTROL_Stop = 0x08
CONTROL_Run = 0x01
CONTROL_Jog = 0x02
CONTROL_Command_rf = 0x04
CONTROL_Running = 0x08
CONTROL_Jogging = 0x10
CONTROL_Running_rf = 0x20
CONTROL_Bracking = 0x40
CONTROL_Track_Start = 0x80
speed_command = 0
spindle_speed_fb = 0
freq_cmd = 0
modbus_interval = 1
modbus_counter = 0
cmd_counter = 0
rated_motor_voltage = 0
rated_motor_current = 0
base_freq = 0
max_freq = 0
freq_lower_limit = 0
min_rpm = 0
max_rpm = 0
rated_motor_rev = 0
rpm_at_50hz = 0
motor_poles = 0
SetF = 0
OutF = 0
OutA = 0
RoTT = 0
DCV = 0
ACV = 0
Cont = 0
Tmp = 0
addr = 1
def __init__(self, addr):
self.spindle_start_fwd = [
0x01,
self.WRITE_CONTROL_DATA,
0x01,
self.CONTROL_Run_Fwd,
]
self.spindle_start_rev = [
0x01,
self.WRITE_CONTROL_DATA,
0x01,
self.CONTROL_Run_Rev,
]
self.spindle_stop = [0x01, self.WRITE_CONTROL_DATA, 0x01, self.CONTROL_Stop]
self.spindle_speed = [0x01, self.WRITE_FREQ_DATA, 0x02, 0x0, 0x0]
self.spindle_PD005 = [0x01, self.FUNCTION_READ, 0x03, 5, 0x00, 0x00]
self.spindle_PD011 = [0x01, self.FUNCTION_READ, 0x03, 11, 0x00, 0x00]
self.spindle_PD141 = [0x01, self.FUNCTION_READ, 0x03, 141, 0x00, 0x00]
self.spindle_PD142 = [0x01, self.FUNCTION_READ, 0x03, 142, 0x00, 0x00]
self.spindle_PD143 = [0x01, self.FUNCTION_READ, 0x03, 143, 0x00, 0x00]
self.spindle_PD144 = [0x01, self.FUNCTION_READ, 0x03, 144, 0x00, 0x00]
self.spindle_status_SetF = [
0x01,
self.READ_CONTROL_STATUS,
0x03,
0x00,
0x00,
0x00,
]
self.spindle_status_OutF = [
0x01,
self.READ_CONTROL_STATUS,
0x03,
0x01,
0x00,
0x00,
]
self.spindle_status_OutA = [
0x01,
self.READ_CONTROL_STATUS,
0x03,
0x02,
0x00,
0x00,
]
self.spindle_status_RoTT = [
0x01,
self.READ_CONTROL_STATUS,
0x03,
0x03,
0x00,
0x00,
]
self.spindle_status_DCV = [
0x01,
self.READ_CONTROL_STATUS,
0x03,
0x04,
0x00,
0x00,
]
self.spindle_status_ACV = [
0x01,
self.READ_CONTROL_STATUS,
0x03,
0x05,
0x00,
0x00,
]
self.spindle_status_Cont = [
0x01,
self.READ_CONTROL_STATUS,
0x03,
0x06,
0x00,
0x00,
]
self.spindle_status_Tmp = [
0x01,
self.READ_CONTROL_STATUS,
0x03,
0x07,
0x00,
0x00,
]
self.run_cmd = self.spindle_stop
self.addr = addr
def crc16(self, data: bytearray, offset, length):
if (
data is None
or offset < 0
or offset > len(data) - 1
and offset + length > len(data)
):
return 0
crc = 0xFFFF
for i in range(length):
crc ^= data[offset + i]
for j in range(8):
if (crc & 0x1) == 1:
crc = int((crc / 2)) ^ 40961
else:
crc = int(crc / 2)
return crc & 0xFFFF
def set_speed(self, rpm):
self.speed_command = abs(rpm)
if rpm > 0:
self.run_cmd = self.spindle_start_fwd
elif rpm < 0:
self.run_cmd = self.spindle_start_rev
else:
self.run_cmd = self.spindle_stop
def transmit(self):
data = [0] * 9
if self.modbus_counter > self.modbus_interval:
self.modbus_counter = 0
cmds = [
self.spindle_PD005,
self.spindle_PD011,
self.spindle_PD141,
self.spindle_PD142,
self.spindle_PD143,
self.spindle_PD144,
self.spindle_speed,
self.run_cmd,
self.spindle_status_OutA,
self.spindle_status_RoTT,
self.spindle_status_SetF,
self.spindle_status_OutF,
self.spindle_speed,
self.run_cmd,
self.spindle_status_ACV,
self.spindle_status_DCV,
self.spindle_status_Cont,
self.spindle_status_Tmp,
self.spindle_speed,
self.run_cmd,
]
cmd = cmds[self.cmd_counter]
cmd[0] = self.addr
if (
cmd == self.spindle_speed
and self.rpm_at_50hz > 0
and self.rated_motor_rev > 0
):
freq_comp = 0
hz_per_rpm = self.max_freq / self.rated_motor_rev
self.freq_cmd = int(
abs((self.speed_command + freq_comp) * hz_per_rpm * 100)
)
cmd[3] = (self.freq_cmd >> 8) & 0xFF
cmd[4] = self.freq_cmd & 0xFF
if self.cmd_counter < len(cmds) - 1:
self.cmd_counter += 1
else:
self.cmd_counter = 0
crc = self.crc16(cmd, 0, len(cmd))
crcH = crc & 0xFF
crcL = crc >> 8 & 0xFF
# print("#########", cmd, crcH, crcL, len(cmd) + 2)
data[0] = len(cmd) + 2
for n in range(len(cmd)):
data[n + 1] = cmd[n]
print("### ", data[n + 1])
data[n + 2] = crcH
print("### ", data[n + 2])
data[n + 3] = crcL
print("### ", data[n + 3])
else:
self.modbus_counter += 1
return data
def feedback(self):
feedback = {
"speed_command": self.speed_command,
"spindle_speed_fb": self.spindle_speed_fb,
"freq_cmd": self.freq_cmd,
"base_freq": self.base_freq,
"max_freq": self.max_freq,
"freq_lower_limit": self.freq_lower_limit,
"min_rpm": self.min_rpm,
"max_rpm": self.max_rpm,
"rpm_at_50hz": self.rpm_at_50hz,
"rated_motor_rev": self.rated_motor_rev,
"SetF": self.SetF,
"OutF": self.OutF,
"OutA": self.OutA,
"RoTT": self.RoTT,
"DCV": self.DCV,
"ACV": self.ACV,
"motor_poles": self.motor_poles,
"Cont": self.Cont,
"Tmp": self.Tmp,
"addr": self.addr,
}
return feedback
def receive(self, data):
pkglen = data[0]
if self.addr != data[1]:
print("WRONG ADDR: ", self.addr, data[1])
return
crc = self.crc16(data[1 : pkglen - 1], 0, pkglen - 2)
crcH = crc & 0xFF
crcL = crc >> 8 & 0xFF
if data[pkglen - 1] != crcH or data[pkglen] != crcL:
print("CSUM ERROR")
return
if data[2] == self.READ_CONTROL_STATUS and data[3] == 0x03:
value = (data[5] << 8) | data[6]
print(value)
if data[4] == 0x00:
self.SetF = value * 0.01
elif data[4] == 0x01:
self.OutF = value * 0.01
self.spindle_speed_fb = (
self.OutF / self.max_freq
) * self.rated_motor_rev
self.spindle_speed_fb_rps = self.spindle_speed_fb / 60.0
elif data[4] == 0x02:
self.OutA = value * 0.1
elif data[4] == 0x03:
self.RoTT = value
elif data[4] == 0x04:
self.DCV = value
elif data[4] == 0x05:
self.ACV = value
elif data[4] == 0x06:
self.Cont = value
elif data[4] == 0x07:
self.Tmp = value
elif data[2] == self.FUNCTION_READ and data[3] == 0x03:
value = (data[5] << 8) | data[6]
if data[4] == self.FUNCTION_PD005:
self.max_freq = value * 0.01
elif data[4] == self.FUNCTION_PD004:
self.base_freq = value * 0.01
elif data[4] == self.FUNCTION_PD011:
self.freq_lower_limit = value * 0.01
elif data[4] == self.FUNCTION_PD141:
self.rated_motor_voltage = value
elif data[4] == self.FUNCTION_PD142:
self.rated_motor_current = value
elif data[4] == self.FUNCTION_PD143:
self.motor_poles = value
elif data[4] == self.FUNCTION_PD144:
self.rpm_at_50hz = value
self.rated_motor_rev = (self.rpm_at_50hz / 50.0) * self.max_freq
if self.freq_lower_limit > self.max_freq:
self.freq_lower_limit = self.max_freq
class WinForm(QWidget):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.setWindowTitle(f"SPI-Test ({project['jdata']['name']} @ {args.device})")
self.listFile = QListWidget()
layout = QGridLayout()
self.widgets = {}
self.vbuffer = {}
self.vminmax = {}
self.animation = 0
self.doutcounter = 0
self.error_counter_spi = 0
self.error_counter_net = 0
self.time_trx = 0
self.time_trx_max = 0
self.vfd = {}
self.pkg_in = 1
self.pkg_out = 1
# boutnames
for num, bout in enumerate(project["boutnames"]):
boutsize = bout["size"]
if bout["type"] == "modbus":
name = bout["name"]
for protocol in bout["protocols"]:
if protocol["type"] == "hyvfd":
addr = protocol["addr"]
self.vfd[addr] = modbus_vfd(addr)
gpy = 0
self.widgets["connection"] = QLabel(f"CONNECTION:")
layout.addWidget(self.widgets["connection"], gpy, 0)
gpy += 1
layout.addWidget(QLabel(f"JOINTS:"), gpy, 0)
for jn in range(JOINTS):
layout.addWidget(QLabel(f"JOINT{jn}"), gpy, jn + 3)
gpy += 1
for jn in range(JOINTS):
layout.addWidget(QLabel(JOINT_TYPES[jn]), gpy, jn + 3)
gpy += 1
for jn in range(JOINTS):
key = f"jcs{jn}"
self.widgets[key] = QSlider(Qt.Horizontal)
if JOINT_TYPES[jn] == "joint_rcservo":
self.widgets[key].setMinimum(-100)
self.widgets[key].setMaximum(100)
else:
self.widgets[key].setMinimum(-2000)
self.widgets[key].setMaximum(2000)
self.widgets[key].setValue(0)
layout.addWidget(self.widgets[key], gpy, jn + 3)
gpy += 1
for jn in range(JOINTS):
key = f"jcs_reset{jn}"
self.widgets[key] = QPushButton("0")
self.widgets[key].clicked.connect(partial(self.slider_reset, f"jcs{jn}"))
layout.addWidget(self.widgets[key], gpy, jn + 3)
gpy += 1
layout.addWidget(QLabel(f"SET"), gpy, 1)
for jn in range(JOINTS):
key = f"jcraw{jn}"
self.widgets[key] = QLabel(f"cmd: {jn}")
layout.addWidget(self.widgets[key], gpy, jn + 3)
gpy += 1
layout.addWidget(QLabel(f"OUT"), gpy, 1)
for jn in range(JOINTS):
key = f"jc{jn}"
self.widgets[key] = QLabel(f"cmd: {jn}")
layout.addWidget(self.widgets[key], gpy, jn + 3)
gpy += 1
layout.addWidget(QLabel(f"FB"), gpy, 1)
for jn in range(JOINTS):
key = f"jf{jn}"
self.widgets[key] = QLabel(f"joint: {jn}")
layout.addWidget(self.widgets[key], gpy, jn + 3)
gpy += 1
layout.addWidget(QLabel(""), gpy, 1)
gpy += 1
layout.addWidget(QLabel(f"VOUT:"), gpy, 0)
for vn in range(VOUTS):
layout.addWidget(QLabel(VOUT_NAMES[vn]["_name"]), gpy, vn + 3)
gpy += 1
for vn in range(VOUTS):
layout.addWidget(QLabel(vout_types[vn]), gpy, vn + 3)
gpy += 1
for vn in range(VOUTS):
plugin_data = VOUT_NAMES[vn]
plugin_name = plugin_data["_plugin"]
vmin = 0
vmax = 100
if hasattr(project["plugins"][plugin_name], "vminmax"):
(vmin, vmax) = project["plugins"][plugin_name].vminmax(plugin_data)
key = f"vos{vn}"
self.widgets[key] = QSlider(Qt.Horizontal)
self.widgets[key].setMinimum(int(vmin))
self.widgets[key].setMaximum(int(vmax))
self.widgets[key].setValue(0)
layout.addWidget(self.widgets[key], gpy, vn + 3)
gpy += 1
for vn in range(VOUTS):
key = f"vos_reset{vn}"
self.widgets[key] = QPushButton("0")
self.widgets[key].clicked.connect(partial(self.slider_reset, f"vos{vn}"))
layout.addWidget(self.widgets[key], gpy, vn + 3)
gpy += 1
layout.addWidget(QLabel(f"SET"), gpy, 1)
for vn in range(VOUTS):
key = f"vo{vn}"
self.widgets[key] = QLabel(f"vo: {vn}")
layout.addWidget(self.widgets[key], gpy, vn + 3)
gpy += 1
layout.addWidget(QLabel(""), gpy, 1)
gpy += 1
layout.addWidget(QLabel(f"VIN:"), gpy, 0)
for vn in range(VINS):
layout.addWidget(QLabel(VIN_NAMES[vn]["_name"]), gpy, vn + 3)
gpy += 1
for vn in range(VINS):
layout.addWidget(QLabel(vin_types[vn]), gpy, vn + 3)
gpy += 1
layout.addWidget(QLabel(f"IN"), gpy, 1)
for vn in range(VINS):
key = f"vi{vn}"
self.widgets[key] = QLabel(f"vin: {vn}")
layout.addWidget(self.widgets[key], gpy, vn + 3)
gpy += 1
if args.graph:
layout.addWidget(QLabel(f"Graph"), gpy, 1)
for vn in range(VINS):
key = f"vi{vn}_g"
self.widgets[key] = QLabel()
self.vbuffer[key] = [0] * 100
self.vminmax[key] = [100000000, -100000000]
layout.addWidget(self.widgets[key], gpy, vn + 3)
gpy += 1
layout.addWidget(QLabel(""), gpy, 1)
gpy += 1
if project["douts"]:
for dbyte in range(DIGITAL_OUTPUT_BYTES):
if dbyte == 0:
layout.addWidget(QLabel(f"DOUT:"), gpy, 0)
self.widgets["dout_auto"] = QCheckBox()
layout.addWidget(self.widgets["dout_auto"], gpy, 2)
for dn in range(8):
key = f"doc{dbyte}{dn}"
self.widgets[key] = QCheckBox(DOUT_NAMES[dbyte * 8 + dn]["_name"])
self.widgets[key].setChecked(False)
layout.addWidget(self.widgets[key], gpy, dn + 3)
if dbyte * 8 + dn == DOUTS - 1:
break
gpy += 1
layout.addWidget(QLabel(""), gpy, 1)
gpy += 1
for dbyte in range(DIGITAL_INPUT_BYTES):
if dbyte == 0:
layout.addWidget(QLabel(f"DIN:"), gpy, 0)
for dn in range(8):
if dbyte * 8 + dn < len(DIN_NAMES):
layout.addWidget(
QLabel(DIN_NAMES[dbyte * 8 + dn]["_name"]), gpy, dn + 3
)
if dbyte * 8 + dn == DINS - 1:
break
gpy += 1
for dn in range(8):
key = f"dic{dbyte}{dn}"
self.widgets[key] = QLabel("0")
layout.addWidget(self.widgets[key], gpy, dn + 3)
if dbyte * 8 + dn == DINS - 1:
break
gpy += 1
# boutnames
for num, bout in enumerate(project["boutnames"]):
boutsize = bout["size"]
if bout["type"] == "modbus":
name = bout["name"]
for protocol in bout["protocols"]:
if protocol["type"] == "hyvfd":
layout.addWidget(QLabel(f"HYVFD:"), gpy, 0)
self.widgets[f"{name}-hyvfd"] = QSlider(Qt.Horizontal)
self.widgets[f"{name}-hyvfd"].setMinimum(-20000)
self.widgets[f"{name}-hyvfd"].setMaximum(20000)
self.widgets[f"{name}-hyvfd"].setValue(0)
layout.addWidget(self.widgets[f"{name}-hyvfd"], gpy, 3)
self.widgets[f"{name}-hyvfd-rst"] = QPushButton("0")
self.widgets[f"{name}-hyvfd-rst"].clicked.connect(
partial(self.slider_reset, f"{name}-hyvfd")
)
layout.addWidget(self.widgets[f"{name}-hyvfd-rst"], gpy, 4)
gpy += 1
for vn, vname in enumerate(
[
"speed_command",
"spindle_speed_fb",
"freq_cmd",
"max_freq",
"freq_lower_limit",
"min_rpm",
"max_rpm",
"rpm_at_50hz",
"rated_motor_rev",
"SetF",
"OutF",
"OutA",
"RoTT",
"DCV",
"ACV",
"Cont",
"Tmp",
"addr",
"motor_poles",
]
):
layout.addWidget(QLabel(vname), gpy, 3)
self.widgets[f"{name}-hyvfd-{vname}"] = QLabel(vname)
layout.addWidget(
self.widgets[f"{name}-hyvfd-{vname}"], gpy, 4
)
gpy += 1
layout.addWidget(QLabel(""), gpy, 0)
gpy += 1
layout.addWidget(QLabel(f"ERRORS:"), gpy, 0)
layout.addWidget(QLabel(f"SPI:"), gpy, 1)
self.widgets["errors_spi"] = QLabel("0")
layout.addWidget(self.widgets["errors_spi"], gpy, 2)
layout.addWidget(QLabel(f"NET:"), gpy, 3)
self.widgets["errors_net"] = QLabel("0")
layout.addWidget(self.widgets["errors_net"], gpy, 4)
layout.addWidget(QLabel(f"TIME:"), gpy, 5)
self.widgets["time_trx"] = QLabel("0")
layout.addWidget(self.widgets["time_trx"], gpy, 6)
layout.addWidget(QLabel(f"MAX:"), gpy, 7)
self.widgets["time_trx_max"] = QLabel("0")
layout.addWidget(self.widgets["time_trx_max"], gpy, 8)
layout.addWidget(QLabel(f"RATE:"), gpy, 9)
self.widgets["error_rate"] = QLabel("0")
layout.addWidget(self.widgets["error_rate"], gpy, 10)
gpy += 1
self.setLayout(layout)
self.timer = QTimer()
self.timer.timeout.connect(self.runTimer)
self.timer.start(INTERVAL)
def slider_reset(self, key):
self.widgets[key].setValue(0)
def runTimer(self):
data[0] = 0x74
data[1] = 0x69
data[2] = 0x72
data[3] = 0x77
try:
for jn in range(JOINTS):
key = f"jcs{jn}"
joints[jn] = int(self.widgets[key].value())
key = f"jcraw{jn}"
self.widgets[key].setText(str(joints[jn]))
for vn in range(VOUTS):
key = f"vos{vn}"
vouts[vn] = int(self.widgets[key].value())
key = f"vo{vn}"
self.widgets[key].setText(str(vouts[vn]))
douts = []
if project["douts"]:
if self.widgets["dout_auto"].isChecked():
for dbyte in range(DIGITAL_OUTPUT_BYTES):
for dn in range(8):
key = f"doc{dbyte}{dn}"
stat = (self.animation - 1) == dbyte * 8 + dn
self.widgets[key].setChecked(stat)
if dbyte * 8 + dn == DOUTS - 1:
break
if self.doutcounter > 3:
self.doutcounter = 0
if self.animation >= DOUTS:
self.animation = 0
else:
self.animation += 1
else:
self.doutcounter += 1
for dbyte in range(DIGITAL_OUTPUT_BYTES):
douts.append(0)
for dn in range(8):
key = f"doc{dbyte}{dn}"
if self.widgets[key].isChecked():
douts[dbyte] |= 1 << (7 - dn)
if dbyte * 8 + dn == DOUTS - 1:
break
bn = 4
for jn, value in enumerate(joints):
# precalc
if value == 0:
value = 0
else:
if JOINT_TYPES[jn] == "joint_pwmdir":
value = value
else:
value = int(PRU_OSC / value / 2)
key = f"jc{jn}"
self.widgets[key].setText(str(value))
joint = list(pack("<i", value))
for byte in range(4):
data[bn + byte] = joint[byte]
bn += 4
for vn, value in enumerate(vouts):
plugin_data = VOUT_NAMES[vn]
plugin = plugin_data["type"]
if hasattr(project["plugins"][plugin], "calculation_vout"):
value = int(project["plugins"][plugin].calculation_vout(plugin_data, value))
vout = list(pack("<i", (value)))
for byte in range(4):
data[bn + byte] = vout[byte]
bn += 4
# boutnames
for num, bout in enumerate(project["boutnames"]):
boutsize = bout["size"]
if bout["type"] == "modbus":
name = bout["name"]
for protocol in bout["protocols"]:
if protocol["type"] == "hyvfd":
speed = self.widgets[f"{name}-hyvfd"].value()
addr = protocol["addr"]
self.vfd[addr].set_speed(speed)
package = self.vfd[addr].transmit()
for boutn in range(boutsize // 8):
data[bn] = package[boutn]
bn += 1
# jointEnable and dout (TODO: split in bits)
for dbyte in range(JOINT_ENABLE_BYTES):
data[bn] = 0xFF
bn += 1
# dout
if project["douts"]:
for dbyte in range(DIGITAL_OUTPUT_BYTES):
data[bn] = douts[dbyte]
bn += 1
self.pkg_out += 1
if args.debug:
print("")
print(f"tx ({self.pkg_out}): {data}")
start = time.time()
if NET_IP:
UDPClientSocket.sendto(bytes(data), (NET_IP, NET_PORT))
UDPClientSocket.settimeout(0.2)
msgFromServer = UDPClientSocket.recvfrom(len(data)*4)
if len(msgFromServer[0]) == len(data):
rec = list(msgFromServer[0])
else:
print(f"{self.pkg_out}/{self.pkg_in} WRONG DATASIZE: {len(msgFromServer[0])} / {len(data)}")
rec = list(msgFromServer[0])
elif SERIAL:
# clean_buffer
while ser.inWaiting() > 0:
ser.read(1)
ser.write(bytes(data))
msgFromServer = ser.read(len(data))
rec = list(msgFromServer)
elif SPI_CH341 is not None:
rec = SPI_CH341.spi_trans(data)
elif SPI_FTDI is not None:
rec = list(SPI_FTDI.exchange(data, duplex=True))
elif SHM_FILE:
fd = open(f"{SHM_FILE}.tx", "wb")
fd.write(bytes(data))
fd.close()
fd = open(f"{SHM_FILE}.rx", "rb")
msgFromServer = fd.read(len(data))
fd.close()
rec = list(msgFromServer)
else:
rec = spi.xfer2(data)
self.time_trx = time.time() - start
self.time_trx_max = max(self.time_trx, self.time_trx_max)
pos = 0
header = unpack("<i", bytes(rec[pos : pos + 4]))[0]