-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pico_3a_2w_6d_spi_v1_Interface_Level.py
1005 lines (976 loc) · 33.5 KB
/
Pico_3a_2w_6d_spi_v1_Interface_Level.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
#
# Hardware specific interface functions
# For Arduino pi pico Three analog + 2 AWG + 6 digital channel scope (9-18-2023)
# Written using Python version 3.10, Windows OS
#
try:
import serial
import serial.tools.list_ports
except:
root.update()
showwarning("WARNING","Serial Library not installed?!")
root.destroy()
exit()
#
# adjust for your specific hardware by changing these values in the alice.init file
CHANNELS = 3 # Number of supported Analog input channels
AWGChannels = 2 # Number of supported Analog output channels
PWMChannels = 1 # Number of supported PWM output channels
DigChannels = 6 # Number of supported Dig channels
LogicChannels = 6 # Number of supported Logic Analyzer channels
EnablePGAGain = 0 #
EnableAWGNoise = 0 #
Tdiv.set(10)
AWG_Amp_Mode.set(0)
AWGPeakToPeak = 3.29
DevID = "Pico 3"
SerComPort = 'Auto'
TimeSpan = 0.01
ADC_Cal = 3.29
AWGRes = 255 # For 8 bits, 4095 for 12 bits, 1023 for 10 bits
InterpRate = 4
MaxSampleRate = SAMPLErate = 12500*InterpRate
AWGSampleRate = 50000 # 20 uSec
LSBsizeA = LSBsizeB = LSBsizeC = LSBsize = ADC_Cal/4096.0
PhaseOffset = 12.5
MinSamples = 1024
AWGBuffLen = 2048
Cycles = 1
SMPfft = MinSamples*InterpRate # Set FFT size based on fixed acquisition record length
#
VBuffA = numpy.ones(MinSamples*InterpRate)
VBuffB = numpy.ones(MinSamples*InterpRate)
VBuffC = numpy.ones(MinSamples*InterpRate)
VBuffD = numpy.ones(MinSamples*InterpRate)
VBuffG = numpy.ones(MinSamples*InterpRate)
MBuff = numpy.ones(MinSamples*InterpRate)
MBuffX = numpy.ones(MinSamples*InterpRate)
MBuffY = numpy.ones(MinSamples*InterpRate)
VmemoryA = numpy.ones(MinSamples*InterpRate) # The memory for averaging
VmemoryB = numpy.ones(MinSamples*InterpRate) # The memory for averaging
VmemoryC = numpy.ones(MinSamples*InterpRate)
VmemoryD = numpy.ones(MinSamples*InterpRate)
#
#
## hardware specific Fucntion to close and exit ALICE
def Bcloseexit():
global RUNstatus, Closed, ser
RUNstatus.set(0)
Closed = 1
#
try:
ser.write(b'Gx\n') # Turn off AWG
ser.write(b'sx\n') # turn off PWM
# try to write last config file, Don't crash if running in Write protected space
BSaveConfig("alice-last-config.cfg")
# May need to be changed for specific hardware port
ser.close()
# exit
except:
donothing()
root.destroy()
exit()
#
# Set Scope Sample Rate based on Horz Time Scale
#
def DummySetSampleRate():
global TimeSpan, MaxSampleRate, SHOWsamples, InterpRate, Tdiv
global TrigSource, TriggerEdge, TriggerInt, SAMPLErate, TimeDiv, ser
TimeDiv = UnitConvert(TMsb.get())
#
def SetSampleRate():
global TimeSpan, SHOWsamples, InterpRate, Tdiv
global MaxSampleRate, SAMPLErate, TimeDiv, ser
try:
TimeDiv = UnitConvert(TMsb.get())
except:
pass
#print("TimeDiv = ", TimeDiv)
if TimeDiv < 0.000099:
ser.write(b't9\n') # 90.909 KSPS
MaxSampleRate = SAMPLErate = 90909*InterpRate
elif TimeDiv > 0.000099 and TimeDiv < 0.000199:
ser.write(b't10\n') # 90.909 KSPS
MaxSampleRate = SAMPLErate = 90909*InterpRate
elif TimeDiv > 0.000199 and TimeDiv < 0.0005:
ser.write(b't12\n') # 90.909KSPS
MaxSampleRate = SAMPLErate = 90909*InterpRate
elif TimeDiv >= 0.0005 and TimeDiv < 0.001:
ser.write(b't14\n') # 90.909 KSPS
MaxSampleRate = SAMPLErate = 90909*InterpRate
elif TimeDiv >= 0.001 and TimeDiv < 0.002:
ser.write(b't16\n') # 62.5 KSPS
MaxSampleRate = SAMPLErate = 62500*InterpRate
elif TimeDiv >= 0.002 and TimeDiv < 0.005:
ser.write(b't32\n') # 31.250 KSPS
MaxSampleRate = SAMPLErate = 31250*InterpRate
elif TimeDiv >= 0.005 and TimeDiv < 0.01:
ser.write(b't64\n') # 15.625 KSPS
MaxSampleRate = SAMPLErate = 15625*InterpRate
elif TimeDiv >= 0.01 and TimeDiv < 0.02:
ser.write(b't100\n') # 10 KSPS
MaxSampleRate = SAMPLErate = 10000*InterpRate
else:
ser.write(b't200\n') # 5 KSPS
MaxSampleRate = SAMPLErate = 5000*InterpRate
#
time.sleep(0.005)
#
#
def only_numerics(seq):
seq_type= type(seq)
return seq_type().join(filter(seq_type.isdigit, seq))
#
# Main function to request and receive a set of ADC samples
#
def Get_Data():
global VBuffA, VBuffB, VBuffC, VBuffG
global ShowC1_V, ShowC2_V, ShowC3_V, ShowC4_V
global LSBsizeA, LSBsizeB, LSBsizeC
global MaxSampleRate, SAMPLErate
global ser, SHOWsamples, TRIGGERsample, TgInput, TimeSpan
global TrigSource, TriggerEdge, TriggerInt, Is_Triggered
global vct_btn, vdt_btn, HoldOff, MinSamples, Interp4Filter
global D0_is_on, D1_is_on, D2_is_on, D3_is_on
global D4_is_on, D5_is_on, D6_is_on, D7_is_on
global DBuff0, DBuff1, DBuff2, DBuff3, DBuff4, DBuff5, DBuff6, DBuff7
global D0line, D1line, D2line, D3line, D4line, D5line, D6line, D7line
# Get data from pi pico
#
SaveDig = False
if D0_is_on or D1_is_on or D2_is_on or D3_is_on or D4_is_on or D5_is_on or D6_is_on:
SaveDig = True
else:
SaveDig = False
#
Wait = 0.02
if SAMPLErate <= 4000:
Wait = 0.08
#
## send command to pico to readout data
ser.write(b'3')
time.sleep(Wait)
ratestring = str(ser.readline())
# print("Raw string ", ratestring)
if "stReal=" in ratestring: #
DTime = ratestring.replace("b'stReal=","")
DTime = DTime.replace("\\\\","")
DTime = DTime.replace("r","")
DTime = DTime.replace("n","")
DTime = DTime.replace("\\","")
DTime = DTime.replace("'","")
# print(DTime)
SampleTime = UnitConvert(DTime) * 1.0e-6 # convert to uSec
MaxSampleRate = SAMPLErate = (1.0/SampleTime)*InterpRate
# print("Sample Time: ", SampleTime)
# print("Sample Rate = ", SAMPLErate )
#
iterCount = (MinSamples * 8) # 7 bytes for three channels plus framing byte
#
VBuffRaw = []
VBuff1=[]
VBuff2=[]
VBuff3=[]
BuffD=[]
time.sleep(Wait)
### Wait to buffer enough samples to satisfy the entire frame, and then
## toss anything left over
# print("iterCount = ", iterCount)
Count = 1
waiting0 = 0
waiting0 = ser.in_waiting
while waiting0 >= 1:
# print("Number Bytes waiting = ", waiting0)
# read in chunks divisible by 8
# Read an integer as two bytes, big-endian
time.sleep(0.01)
if waiting0 > 4096:
VBuffRaw = ser.read(4096)
Count = Count + 4096
elif waiting0 > 2048:
VBuffRaw = ser.read(2048)
Count = Count + 2048
elif waiting0 > 1024:
VBuffRaw = ser.read(1024)
Count = Count + 1024
elif waiting0 > 512:
VBuffRaw = ser.read(512)
Count = Count + 512
elif waiting0 > 256:
VBuffRaw = ser.read(256)
Count = Count + 256
else:
VBuffRaw = ser.read(128)
Count = Count + 128
index = 0
# Count = Count + waiting0
while index < len(VBuffRaw)-7:
# print("Length: ", len(VBuff1))
if VBuffRaw[index] == 0: # find first value = 0 framing byte
index = index + 1
# Frams = Frams + 1
# Get CH A data
inputHigh = VBuffRaw[index]
index = index +1
inputLow = VBuffRaw[index]
data = ((inputHigh*256)+inputLow)
VBuff1.append(data) #*LSBsizeA)
# Get CH B data
index = index + 1
inputHigh = VBuffRaw[index]
index = index + 1
inputLow = VBuffRaw[index]
data = ((inputHigh*256)+inputLow)
VBuff2.append(data) #*LSBsizeB)
# Get CH C data
index = index + 1
inputHigh = VBuffRaw[index]
index = index + 1
inputLow = VBuffRaw[index]
data = ((inputHigh*256)+inputLow)
VBuff3.append(data) #*LSBsizeC)
# Get digital inputs data
index = index + 1
data = VBuffRaw[index]
BuffD.append(data*1)
index = index + 1
else:
index = index + 1
waiting0 = ser.in_waiting
# print("Serial Length:", waiting0)
# time.sleep(Wait)
if Count >= iterCount:
break
#
# print("received Samples = ", Count)
# print("Length: ", len(VBuff1))
#
##
# print("Frams = ", Frams)
#
VBuffA=[]
VBuffB=[]
VBuffC=[]
VBuffG=[]
#
# Interpolate data samples by 4X
index = 0
while index < len(VBuff1): # build array
pointer = 0
while pointer < 4:
samp = VBuff1[index]
VBuffA.append(float(samp) * LSBsizeA)
samp = VBuff2[index]
VBuffB.append(float(samp) * LSBsizeB)
samp = VBuff3[index]
VBuffC.append(float(samp) * LSBsizeC)
if SaveDig:
VBuffG.append(BuffD[index])
pointer = pointer + 1
index = index + 1
SHOWsamples = len(VBuffA)
VBuffA = numpy.pad(VBuffA, (4, 0), "edge")
VBuffA = numpy.convolve(VBuffA, Interp4Filter )
VBuffB = numpy.pad(VBuffB, (4, 0), "edge")
VBuffB = numpy.convolve(VBuffB, Interp4Filter )
VBuffC = numpy.pad(VBuffC, (4, 0), "edge")
VBuffC = numpy.convolve(VBuffC, Interp4Filter )
#
VBuffA = VBuffA[4:SHOWsamples+4]
VBuffB = VBuffB[4:SHOWsamples+4]
VBuffC = VBuffC[4:SHOWsamples+4]
VBuffG = numpy.array(VBuffG) * 1
# Extract Digital buffers if needed
if SaveDig:
VBuffG = VBuffG.astype(int)
if D0_is_on:
DBuff0 = VBuffG & 1
if D1_is_on:
DBuff1 = VBuffG & 2
DBuff1 = DBuff1 / 2
if D2_is_on:
DBuff2 = VBuffG & 4
DBuff2 = DBuff2 / 4
if D3_is_on:
DBuff3 = VBuffG & 8
DBuff3 = DBuff3 / 8
if D4_is_on:
DBuff4 = VBuffG & 16
DBuff4 = DBuff4 / 16
if D5_is_on:
DBuff5 = VBuffG & 32
DBuff5 = DBuff5 / 32
if D6_is_on:
DBuff6 = VBuffG & 64
DBuff6 = DBuff6 / 64
if D7_is_on:
DBuff7 = VBuffG & 128
DBuff7 = DBuff7 / 128
#
else:
SaveDig = False
DBuff0 = []
DBuff1 = []
DBuff2 = []
DBuff3 = []
DBuff4 = []
DBuff5 = []
DBuff6 = []
DBuff7 = []
# Find trigger sample point if necessary
# print("Array Len ",len(VBuffA), "SHOWsamples ", SHOWsamples)
LShift = 0
if TgInput.get() == 1:
FindTriggerSample(VBuffA)
if TgInput.get() == 2:
FindTriggerSample(VBuffB)
if TgInput.get() == 3:
FindTriggerSample(VBuffC)
if TgInput.get() > 4:
TRIGGERentry.delete(0,"end")
TRIGGERentry.insert(0,0.5)
if TgInput.get() == 5:
FindTriggerSample(DBuff0)
if TgInput.get() == 6:
FindTriggerSample(DBuff1)
if TgInput.get() == 7:
FindTriggerSample(DBuff2)
if TgInput.get() == 8:
FindTriggerSample(DBuff3)
if TgInput.get() == 9:
FindTriggerSample(DBuff4)
if TgInput.get() == 10:
FindTriggerSample(DBuff5)
if TgInput.get() == 11:
FindTriggerSample(DBuff6)
if TgInput.get() > 0: # if triggering left shift all arrays such that trigger point is at index 0
LShift = 0 - TRIGGERsample
if ShowC1_V.get() > 0:
VBuffA = numpy.roll(VBuffA, LShift)
if ShowC2_V.get() > 0:
VBuffB = numpy.roll(VBuffB, LShift+1)
if ShowC3_V.get() > 0:
VBuffC = numpy.roll(VBuffC, LShift+2)
if SaveDig:
VBuffG = numpy.roll(VBuffG, LShift)
if D0_is_on:
DBuff0 = numpy.roll(DBuff0, LShift)
if D1_is_on:
DBuff1 = numpy.roll(DBuff1, LShift)
if D2_is_on:
DBuff2 = numpy.roll(DBuff2, LShift)
if D3_is_on:
DBuff3 = numpy.roll(DBuff3, LShift)
if D4_is_on:
DBuff4 = numpy.roll(DBuff4, LShift)
if D5_is_on:
DBuff5 = numpy.roll(DBuff5, LShift)
if D6_is_on:
DBuff6 = numpy.roll(DBuff6, LShift)
if D7_is_on:
DBuff7 = numpy.roll(DBuff7, LShift)
else:
VBuffA = numpy.roll(VBuffA, -8)
VBuffB = numpy.roll(VBuffB, -7)
VBuffC = numpy.roll(VBuffC, -6)
#
# Hardware Help
#
## try to connect to Arduino Pi Pico board
#
def ConnectDevice():
global SerComPort, DevID, MaxSamples, SAMPLErate, MinSamples, AWGSampleRate
global bcon, FWRevOne, HWRevOne, MaxSampleRate, ser, SHOWsamples
global CH1Probe, CH2Probe, CH1VRange, CH2VRange, TimeDiv
global CHAsb, CHBsb, TMsb, LSBsizeA, LSBsizeB, ADC_Cal, LSBsize
global d0btn, d1btn, d2btn, d3btn, d4btn, d5btn, d6btn, d7btn
# print("SerComPort: ", SerComPort)
if DevID == "No Device" or DevID == "Pico 3":
#
if SerComPort == 'Auto':
ports = serial.tools.list_ports.comports()
for port in ports: # ports:
# looking for this ID: USB\VID_2E8A&PID_000A
if "VID:PID=2E8A:000A" in port[2]:
print("Found: ", port[0])
SerComPort = port[0]
# Setup instrument connection
print("Trying to open ", SerComPort)
ser = serial.Serial(SerComPort) # open serial port
if ser is None:
print('Device not found!')
Bcloseexit()
#exit()
#
ser.baudrate = 2000000 # Dummy number USB runs at max supported speed
#
ser.write(b'I\n') # request board ID
time.sleep(0.005)
IDstring = str(ser.readline())
ID = IDstring.replace("b'","")
ID = ID.replace("\\\\","")
ID = ID.replace("r","")
ID = ID.replace("n","")
ID = ID.replace("\\","")
ID = ID.replace("'","")
print("ID string ", ID)
#
ser.write(b'V\n') # Read Bacl VDD (.3.) supply voltage
time.sleep(0.005)
VDDstring = str(ser.readline())
# print("VDD string ", VDDstring)
if "V=" in VDDstring: #
VDD = VDDstring.replace("b'V=","")
VDD = VDD.replace("\\\\","")
VDD = VDD.replace("r","")
VDD = VDD.replace("n","")
VDD = VDD.replace("\\","")
VDD = VDD.replace("'","")
Vsys = (int(VDD) * LSBsize) * 3.0 # 1/3 voltage divider
print("Board Vsys = ", Vsys)
#
ser.write(b't50\n') # send Scope sample time in uSec
time.sleep(0.005)
print("set dt: 50 uSec")
MaxSampleRate = SAMPLErate = 20000*InterpRate
#
ser.write(b'T10\n') # send AWG sample time in uSec
time.sleep(0.005)
print("set at: 24 uSec")
AWGSampleRate = 66666.7
MinSamples = 1024 #
#
ser.write(b'b1024\n') # send Scope Buffer Length
time.sleep(0.005)
print("set Scope Samples: 1024")
#
ser.write(b'N1024\n') # send AWG A Buffer Length
ser.write(b'M1024\n') # send AWG B Buffer Length
time.sleep(0.005)
print("set AWG Samples: 1024")
#
# ser.write(b'p500000\n')
ser.write(b'Rx\n') # default with AWG reset off
#
ser.write(b'Sx\n') # turn off AWG A by default
ser.write(b'Sz\n') # turn off AWG B by default
MaxSamples = 4096 # assume 4X interpolation
#
print("Get a sample: ")
Get_Data() # grap a check set of samples
print("After Interp ", len(VBuffA), len(VBuffB))
SHOWsamples = len(VBuffA)
return(True) # return a logical true if sucessful!
else:
return(False)
#
# AWG Stuff
#
def AWGASendWave(AWG3):
global ser, AWGARecLength, AWGBuffLen, AWGRes
global AWGAAmplvalue, AWGAOffsetvalue, AWGPeakToPeak
# Expect array values normalized from -1 to 1
# scale values to send to 0 to 255 8 bits
AWG3 = AWG3 * 0.5 # scale by 1/2
# Get Low and High voltage levels
MinCode = int((AWGAAmplvalue / AWGPeakToPeak) * AWGRes)
if MinCode < 0:
MinCode = 0
if MinCode > AWGRes:
MinCode = AWGRes
MaxCode = int((AWGAOffsetvalue / AWGPeakToPeak) * AWGRes)
if MaxCode < 0:
MaxCode = 0
if MaxCode > AWGRes:
MaxCode = AWGRes
# print("MaxCode = ", MaxCode, "MinCode = ", MinCode)
# Scale to high and low voltage values
Gain = MaxCode - MinCode
Offset = int((MaxCode + MinCode)/2)
AWG3 = (AWG3 * Gain) + Offset
n = 0
AWG1 = []
while n < len(AWG3):
AWG1.append(int(AWG3[n]))
n = n + 1
AWG1 = numpy.array(AWG1)
#
AWGARecLength = len(AWG1)
if AWGARecLength > AWGBuffLen:
AWGARecLength = AWGBuffLen
if len(AWG1) < AWGBuffLen:
# ser.write(b'B1024\n') # send AWG Buffer Length
SendStr = 'N' + str(len(AWG1)) + '\n'
#
SendByt = SendStr.encode('utf-8')
ser.write(SendByt)
else:
SendStr = 'N' + str(AWGBuffLen) + '\n'
#
SendByt = SendStr.encode('utf-8')
ser.write(SendByt)
#
#AWG1 = AWG1.tobytes()
index = 0
while index < AWGARecLength:
data = AWG1[index]
# send buffer index and waveform sample data
SendStr = 'L' + str(index) + 'D' + str(data) + '\n'
#
SendByt = SendStr.encode('utf-8')
ser.write(SendByt)
index = index + 1
#
##
def AWGBSendWave(AWG3):
global ser, AWGBLastWave, AWGBRecLength, AWGBuffLen, AWGRes
global AWGBAmplvalue, AWGBOffsetvalue, AWGPeakToPeak
# Expect array values normalized from -1 to 1
# AWG3 = numpy.roll(AWG3, -68)
AWGBLastWave = AWG3
AWG3 = AWG3 * 0.5 # scale by 1/2
# Get Low and High voltage levels
MinCode = int((AWGBAmplvalue / AWGPeakToPeak) * AWGRes)
if MinCode < 0:
MinCode = 0
if MinCode > AWGRes:
MinCode = AWGRes
MaxCode = int((AWGBOffsetvalue / AWGPeakToPeak) * AWGRes)
if MaxCode < 0:
MaxCode = 0
if MaxCode > AWGRes:
MaxCode = AWGRes
#
Gain = MaxCode - MinCode
Offset = int((MaxCode + MinCode)/2)
AWG3 = (AWG3 * Gain) + Offset
n = 0
AWG1 = []
while n < len(AWG3):
AWG1.append(int(AWG3[n]))
n = n + 1
AWG1 = numpy.array(AWG1)
#
AWGBRecLength = len(AWG1)
if AWGBRecLength > AWGBuffLen:
AWGBRecLength = AWGBuffLen
if len(AWG1) < AWGBuffLen:
SendStr = 'M' + str(len(AWG1)) + '\n'
#
SendByt = SendStr.encode('utf-8')
ser.write(SendByt)
else:
SendStr = 'M' + str(AWGBuffLen) + '\n'
#
SendByt = SendStr.encode('utf-8')
ser.write(SendByt)
#
#AWG1 = AWG1.tobytes()
index = 0
while index < AWGBRecLength:
data = AWG1[index]
#
SendStr = 'l' + str(index) + 'D' + str(data) + '\n'
#
SendByt = SendStr.encode('utf-8')
ser.write(SendByt)
index = index + 1
#
#
def BAWGSync():
global AWGSync
if AWGSync.get() > 0:
ser.write(b'Ro\n') # turn on sync
else:
ser.write(b'Rx\n') # turn off sync
#
def SetAwgSampleRate():
global AWGAFreqEntry, AWGBFreqEntry, FSweepMode
global AWGBuffLen, AWGSampleRate, AWGBuffLen
BAWGAFreq()
BAWGBFreq()
MaxRepRate = numpy.ceil(AWGSampleRate / AWGBuffLen)
FreqA = UnitConvert(AWGAFreqEntry.get())
FreqB = UnitConvert(AWGBFreqEntry.get())
Cycles = 1
if FSweepMode.get() == 1: # If doing a frequency sweep only make new AWG A sine wave
if FreqA > MaxRepRate:
Cycles = numpy.ceil(FreqA/MaxRepRate)
if Cycles <= 0: # check if divide by zero
Cycles = 1
FreqA = FreqA/Cycles
# Set the AWG buffer Rep rate (Freq for one cycle of buffer)
SetAwgSampleFrequency(FreqA)
AWGRepRate = FreqA
# AWGSampleRate = FreqA * AWGBuffLen
else:
if FreqA <= FreqB:
if FreqA > MaxRepRate:
Cycles = numpy.ceil(FreqA/MaxRepRate)
if Cycles <= 0: # check if divide by zero
Cycles = 1
FreqA = FreqA/Cycles
# Set the AWG buffer Rep rate (Freq for one cycle of buffer)
SetAwgSampleFrequency(FreqA)
AWGRepRate = FreqA
# AWGSampleRate = FreqA * MaxSamples
else:
if FreqB > MaxRepRate:
Cycles = numpy.ceil(FreqB/MaxRepRate)
if Cycles <= 0: # check if divide by zero
Cycles = 1
FreqB = FreqB/Cycles
# Set the AWG buffer Rep rate (Freq for one cycle of buffer)
AWGRepRate = FreqB
SetAwgSampleFrequency(FreqB)
# AWGSampleRate = FreqB * MaxSamples
#
def SetAwgSampleFrequency(FreqANum):
global AWGBuffLen
#
NewSampleRate = FreqANum * AWGBuffLen # Samples per second
NewAT = int(1000000/NewSampleRate) # in uSec
NewAT = NewAT - 5
SendStr = 'T' + str(NewAT) + '\n'
print(SendStr)
SendByt = SendStr.encode('utf-8')
# print(SendByt)
ser.write(SendByt) #
#
# for built in firmware waveforms...
#
def SetAwgA_Ampl(Ampl): # used to toggle on / off AWG output
global ser, AwgBOnOffBt, AwgaOnOffLb, AwgbOnOffLb
AwgBOnOffBt.config(state=DISABLED)
AwgaOnOffLb.config(text="AWG Output ")
AwgbOnOffLb.config(text=" ")
if Ampl == 0:
ser.write(b'Gx\n')
else:
ser.write(b'Go\n')
#
def SetAwgB_Ampl(Ampl): # used to toggle on / off AWG output
global ser, AwgBOnOffBt, AwgAOnOffBt, AwgaOnOffLb, AwgbOnOffLb
AwgBOnOffBt.config(state=DISABLED)
AwgaOnOffLb.config(text="AWG Output ")
AwgbOnOffLb.config(text=" ")
if Ampl == 0:
ser.write(b'Gx\n')
else:
AwgAOnOffBt.config(text='ON', style="Run.TButton")
ser.write(b'Go\n')
#
def SetAWG_Ampla():
global AWGAAmplEntry, ADC_Cal, ser, AWGRes
MaxLimit = int(AWGRes/2)
Vampl = float(AWGAAmplEntry.get())
Bampl = int((Vampl/ADC_Cal)*MaxLimit)
if Bampl > MaxLimit:
Bampl = MaxLimit
if Bampl < 0:
Bampl = 0
# print("Bampl = ", Bampl)
ByteStr = 'A' + str(Bampl) + "\n"
SendByt = ByteStr.encode('utf-8')
ser.write(SendByt) #
#
def SetAWG_Amplb():
global AWGBAmplEntry, ADC_Cal, ser, AWGRes
MaxLimit = int(AWGRes/2)
Vampl = float(AWGBAmplEntry.get())
Bampl = int((Vampl/ADC_Cal)*MaxLimit)
if Bampl > MaxLimit:
Bampl = MaxLimit
if Bampl < 0:
Bampl = 0
# print("Bampl = ", Bampl)
ByteStr = 'a' + str(Bampl) + "\n"
SendByt = ByteStr.encode('utf-8')
ser.write(SendByt) #
#
def SetAWG_Offseta():
global AWGAOffsetEntry, ADC_Cal, ser, AWGRes
Voffset = float(AWGAOffsetEntry.get())
Boffset = int((Voffset/ADC_Cal)*(AWGRes+1))
# print("Boffset = ", Boffset)
ByteStr = 'O' + str(Boffset) + "\n"
SendByt = ByteStr.encode('utf-8')
ser.write(SendByt) #
#
def SetAWG_Offsetb():
global AWGBOffsetEntry, ADC_Cal, ser, AWGRes
Voffset = float(AWGBOffsetEntry.get())
Boffset = int((Voffset/ADC_Cal)*(AWGRes+1))
# print("Boffset = ", Boffset)
ByteStr = 'o' + str(Boffset) + "\n"
SendByt = ByteStr.encode('utf-8')
ser.write(SendByt) #
#
#
## Make the current selected AWG waveform
#
##AwgString1 = "Sine"
##AwgString2 = "Triangle"
##AwgString3 = "Ramp Up"
##AwgString4 = "Ramp Down"
##AwgString5 = "Stair Up"
##AwgString6 = "Stair Down"
##AwgString7 = "Stair Up-Down"
AwgString9 = "Full Wave Sine"
AwgString10 = "Half Wave Sine"
AwgString11 = "Fourier Series"
AwgString12 = "Schroeder Chirp"
#
## Make or update the current selected AWG waveform
def MakeAWGwaves(): # re make awg waveforms in case something changed
global AWGAShape, AWGAShapeLabel, AWGBShape, AWGBShapeLabel
global AWGAAmplEntry, AWGAOffsetEntry, AWGAFreqEntry, AWGASymmetryEntry, AWGADutyCycleEntry
global AWGAAmplvalue, AWGBOffsetvalue, AWGBAmplvalue, AWGBOffsetvalue, AWGAFreqvalue
global AWGBAmplEntry, AWGBOffsetEntry, AWGBFreqEntry, AWGBSymmetryEntry, AWGBDutyCycleEntry
global FSweepMode, MaxSampleRate, BisCompA
global AwgString1, AwgString2, AwgString3, AwgString4, AwgString5, AwgString6
global AwgString7, AwgString8, AwgString9, AwgString10, AwgString11, AwgString12
global AwgString13, AwgString14, AwgString15, AwgString16
if FSweepMode.get() == 1: # If doing a frequency sweep only make new AWG A sine wave
if AWGAShape.get()==1:
AWGAMakeSine()
AWGAShapeLabel.config(text = AwgString1) # change displayed value
return
# Shape list
if AWGAShape.get()== 0:
AWGAMakeDC()
AWGAShapeLabel.config(text = "DC") # change displayed value
elif AWGAShape.get()==1:
AWGAMakeSine()
AWGAShapeLabel.config(text = AwgString1) # change displayed value
elif AWGAShape.get()==2:
AWGAMakeSquare()
AWGAShapeLabel.config(text = AwgString2) # change displayed value
elif AWGAShape.get()==3:
AWGAMakeTriangle()
AWGAShapeLabel.config(text = AwgString3) # change displayed value
elif AWGAShape.get()==4:
AWGAMakePulse()
AWGAShapeLabel.config(text = AwgString4) # change displayed value
elif AWGAShape.get()==5:
AWGAMakeRampDn()
AWGAShapeLabel.config(text = AwgString5) # change displayed value
elif AWGAShape.get()==6:
AWGAMakeRampUp()
AWGAShapeLabel.config(text = AwgString6) # change displayed value
elif AWGAShape.get()==7:
AWGAMakeStair()
AWGAShapeLabel.config(text = AwgString7) # change displayed value
elif AWGAShape.get()==8:
AWGAMakeSinc()
AWGAShapeLabel.config(text = AwgString8) # change displayed value
elif AWGAShape.get()==9:
AWGAMakeFullWaveSine()
AWGAShapeLabel.config(text = AwgString9) # change displayed value
elif AWGAShape.get()==10:
AWGAMakeHalfWaveSine()
AWGAShapeLabel.config(text = AwgString10) # change displayed value
elif AWGAShape.get()==11:
AWGAMakeFourier()
AWGAShapeLabel.config(text = AwgString11) # change displayed value
elif AWGAShape.get()==12:
SetAwgSampleRate()
AWGAAmplvalue = float(eval(AWGAAmplEntry.get()))
AWGAOffsetvalue = float(eval(AWGAOffsetEntry.get()))
AWGAFreqvalue = UnitConvert(AWGAFreqEntry.get())
NrTones = int(eval(AWGADutyCycleEntry.get()))
ampl = 3.0/NrTones
if ampl > 0.25:
ampl = 0.25
AWGASendWave(SchroederPhase(MaxSamples, NrTones, ampl))
AWGAShapeLabel.config(text = AwgString12) # change displayed value
else:
AWGAShapeLabel.config(text = "Other Shape") # change displayed value
#
if BisCompA.get() == 1:
SetBCompA()
#
if AWGBShape.get() == 0:
AWGBMakeDC()
AWGBShapeLabel.config(text = "DC") # change displayed value
elif AWGBShape.get() == 1:
AWGBMakeSine()
AWGBShapeLabel.config(text = AwgString1) # change displayed value
elif AWGBShape.get() == 2:
AWGBMakeSquare()
AWGBShapeLabel.config(text = AwgString2) # change displayed value
elif AWGBShape.get() == 3:
AWGBMakeTriangle()
AWGBShapeLabel.config(text = AwgString3) # change displayed value
elif AWGBShape.get() == 4:
AWGBMakePulse()
AWGBShapeLabel.config(text = AwgString4) # change displayed value
elif AWGBShape.get()==5:
AWGBMakeRampDn()
AWGBShapeLabel.config(text = AwgString5) # change displayed value
elif AWGBShape.get()==6:
AWGBMakeRampUp()
AWGBShapeLabel.config(text = AwgString6) # change displayed value
elif AWGBShape.get()==7:
AWGBMakeStair()
AWGBShapeLabel.config(text = AwgString7) # change displayed value
elif AWGBShape.get()==8:
AWGBMakeSinc()
AWGBShapeLabel.config(text = AwgString8) # change displayed value
elif AWGBShape.get()==9:
AWGBMakeFullWaveSine()
AWGBShapeLabel.config(text = AwgString9) # change displayed value
elif AWGBShape.get()==10:
AWGBMakeHalfWaveSine()
AWGBShapeLabel.config(text = AwgString10) # change displayed value
elif AWGBShape.get()==11:
AWGBMakeFourier()
AWGBShapeLabel.config(text = AwgString11) # change displayed value
elif AWGBShape.get()==12:
SetAwgSampleRate()
AWGBAmplvalue = float(eval(AWGBAmplEntry.get()))
AWGBOffsetvalue = float(eval(AWGBOffsetEntry.get()))
AWGBFreqvalue = UnitConvert(AWGBFreqEntry.get())
NrTones = int(eval(AWGBDutyCycleEntry.get()))
ampl = 3.0/NrTones
if ampl > 0.25:
ampl = 0.25
AWGBSendWave(SchroederPhase(MaxSamples, NrTones, ampl))
AWGBShapeLabel.config(text = AwgString12) # change displayed value
else:
AWGBShapeLabel.config(text = "Other Shape") # change displayed value
#
time.sleep(0.01)
#
def MakeAWG_internal_waves(): # re make awg waveforms in case something changed
global ser, AWGAShape, AWGAShapeLabel, AWGBShape, AWGBShapeLabel
global AWGAAmplEntry, AWGAOffsetEntry, AWGAFreqEntry, AWGASymmetryEntry, AWGADutyCycleEntry
global AWGAAmplvalue, AWGBOffsetvalue, AWGBAmplvalue, AWGBOffsetvalue
global AWGBAmplEntry, AWGBOffsetEntry, AWGBFreqEntry, AWGBSymmetryEntry, AWGBDutyCycleEntry
global FSweepMode, MaxSampleRate
global AwgString1, AwgString2, AwgString3, AwgString4, AwgString5, AwgString6
global AwgString7, AwgString8, AwgString9, AwgString10, AwgString11, AwgString12
global AwgString13, AwgString14, AwgString15, AwgString16
#
time.sleep(0.01)
#
if AWGAShape.get()==0:
ser.write(b'W0\n')
AWGAShapeLabel.config(text = AwgString0) # change displayed value
elif AWGAShape.get()==1:
ser.write(b'W1\n')
AWGAShapeLabel.config(text = AwgString1) # change displayed value
elif AWGAShape.get()==2:
ser.write(b'W2\n')
AWGAShapeLabel.config(text = AwgString2) # change displayed value
elif AWGAShape.get()==3:
ser.write(b'W3\n')
AWGAShapeLabel.config(text = AwgString3) # change displayed value
elif AWGAShape.get()==4:
ser.write(b'W4\n')
AWGAShapeLabel.config(text = AwgString4) # change displayed value
elif AWGAShape.get()==5:
ser.write(b'W5\n')
AWGAShapeLabel.config(text = AwgString5) # change displayed value
elif AWGAShape.get()==6:
ser.write(b'W6\n')
AWGAShapeLabel.config(text = AwgString6) # change displayed value
elif AWGAShape.get()==7:
ser.write(b'W7\n')
AWGAShapeLabel.config(text = AwgString7) # change displayed value
#
if AWGBShape.get()==0:
ser.write(b'w0\n')
AWGBShapeLabel.config(text = AwgString0) # change displayed value
elif AWGBShape.get()==1:
ser.write(b'w1\n')
AWGBShapeLabel.config(text = AwgString1) # change displayed value
elif AWGBShape.get()==2:
ser.write(b'w2\n')
AWGBShapeLabel.config(text = AwgString2) # change displayed value
#SetAwgFrequency()
SetAWG_Ampla()
SetAWG_Offseta()
SetAWG_Amplb()
SetAWG_Offsetb()
time.sleep(0.1)
#
# Hardware Specific PWM control functions
#
def PWM_On_Off():
global PWM_is_on, ser
if PWM_is_on:
#print("Set pwm on")
ser.write(b'so\n')
else:
#print("Set pwm off")
ser.write(b'sx\n')
#
def UpdatePWM():
global PWMDivEntry, PWMWidthEntry, PWMLabel, ser
PWMLabel.config(text = "PWM Frequency")
FreqValue = int(UnitConvert(PWMDivEntry.get()))
#PeriodValue = int(( 133e6 / 256 ) / FreqValue)
#print("FreqValue = ", FreqValue, "PeriodValue = ",PeriodValue)
ByteStr = 'p' + str(FreqValue) + "\n"
SendByt = ByteStr.encode('utf-8')
ser.write(SendByt)
time.sleep(0.1)
DutyCycle = int(PWMWidthEntry.get())
#WidthFraction = float((DutyCycle/100.0))
#Width = int(PeriodValue * WidthFraction)
ByteStr = 'm' + str(DutyCycle) + "\n"
SendByt = ByteStr.encode('utf-8')
ser.write(SendByt)
time.sleep(0.1)
#
# Hardware Specific Trigger functions
#
def SendTriggerLevel():
global TRIGGERlevel, ser, RUNstatus, AWGPeakToPeak
# Min and Max trigger level specific to hardware
if TRIGGERlevel > 4.0:
TRIGGERlevel = 4.0
TRIGGERentry.delete(0,"end")
TRIGGERentry.insert(0, ' {0:.2f} '.format(4.0))
if TRIGGERlevel < 0.0:
TRIGGERlevel = 0.0
TRIGGERentry.delete(0,"end")
TRIGGERentry.insert(0, ' {0:.2f} '.format(0.0))
# Adjust trigger level.
# representa a 8-bit number (0-255) sent to DAC
# Set Horz possition from entry widget
def SetHorzPoss():
global HozPossentry, ser
HzSteps = int(float(HozPossentry.get()))
Hz_Value = 511-HzSteps
if Hz_Value > 256:
T_High = 1
T_Low = Hz_Value - 256
else:
T_High = 0
T_Low = Hz_Value
#
# Set Internal / External triggering bits
def BTrigIntExt():
global TgSource, TriggerInt
if TgSource.get() == 0:
TriggerInt = 0x00 # bit 7 0x00 = Internal
if TgSource.get() == 1:
TriggerInt = 0x80 # bit 7 0x80 = External
# Set Triggering source bits
def BSetTriggerSource():
global TgInput, TrigSource, TriggerInt
if TgInput.get() == 1:
TrigSource = 0x00 # bit 4 0x00 = Channel A
if TgInput.get() == 2:
TrigSource = 0x10 # bit 4 0x10 = Channel B
if TgInput.get() == 0:
TriggerInt = 0x40 # bit 6 0x40 No Triggers (free-run)?
# Set Trigger edge bits
def BSetTrigEdge():
global TgEdge, TriggerEdge