Skip to content

Commit ddd796d

Browse files
author
neil.hamilton
committed
Add ps6000aRapidBlockExample.py to ps6000aExamples
1 parent 1bdf4f5 commit ddd796d

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
#
2+
# Copyright (C) 2020 Pico Technology Ltd. See LICENSE file for terms.
3+
#
4+
# PS6000 A BLOCK MODE EXAMPLE
5+
# This example opens a 6000a driver device, sets up two channels and a trigger then collects 10 blocks of data in rapid block mode.
6+
# This data is then plotted as mV against time in ns.
7+
8+
import ctypes
9+
import numpy as np
10+
from picosdk.ps6000a import ps6000a as ps
11+
from picosdk.PicoDeviceEnums import picoEnum as enums
12+
import matplotlib.pyplot as plt
13+
from picosdk.functions import adc2mV, assert_pico_ok
14+
15+
# Create chandle and status ready for use
16+
chandle = ctypes.c_int16()
17+
status = {}
18+
19+
# Open 6000 A series PicoScope
20+
# returns handle to chandle for use in future API functions
21+
resolution = enums.PICO_DEVICE_RESOLUTION["PICO_DR_8BIT"]
22+
status["openunit"] = ps.ps6000aOpenUnit(ctypes.byref(chandle), None, resolution)
23+
assert_pico_ok(status["openunit"])
24+
25+
# Set channel A on
26+
# handle = chandle
27+
channelA = enums.PICO_CHANNEL["PICO_CHANNEL_A"]
28+
coupling = enums.PICO_COUPLING["PICO_DC"]
29+
channelRange = 7
30+
# analogueOffset = 0 V
31+
bandwidth = enums.PICO_BANDWIDTH_LIMITER["PICO_BW_FULL"]
32+
status["setChannelA"] = ps.ps6000aSetChannelOn(chandle, channelA, coupling, channelRange, 0, bandwidth)
33+
assert_pico_ok(status["setChannelA"])
34+
35+
# set channel B-H off
36+
for x in range (1, 7, 1):
37+
channel = x
38+
status["setChannel",x] = ps.ps6000aSetChannelOff(chandle,channel)
39+
assert_pico_ok(status["setChannel",x])
40+
41+
# Set simple trigger on channel A, 1 V rising with 1 s autotrigger
42+
# handle = chandle
43+
# enable = 1
44+
source = channelA
45+
# threshold = 1000 mV
46+
direction = enums.PICO_THRESHOLD_DIRECTION["PICO_RISING"]
47+
# delay = 0 s
48+
# autoTriggerMicroSeconds = 1000000 us
49+
status["setSimpleTrigger"] = ps.ps6000aSetSimpleTrigger(chandle, 1, source, 1000, direction, 0, 1000000)
50+
assert_pico_ok(status["setSimpleTrigger"])
51+
52+
# Get fastest available timebase
53+
# handle = chandle
54+
enabledChannelFlags = enums.PICO_CHANNEL_FLAGS["PICO_CHANNEL_A_FLAGS"]
55+
timebase = ctypes.c_uint32(0)
56+
timeInterval = ctypes.c_double(0)
57+
# resolution = resolution
58+
status["getMinimumTimebaseStateless"] = ps.ps6000aGetMinimumTimebaseStateless(chandle, enabledChannelFlags, ctypes.byref(timebase), ctypes.byref(timeInterval), resolution)
59+
assert_pico_ok(status["getMinimumTimebaseStateless"])
60+
print("timebase = ", timebase.value)
61+
print("sample interval =", timeInterval.value, "s")
62+
63+
# Set number of samples to be collected
64+
noOfPreTriggerSamples = 500000
65+
noOfPostTriggerSamples = 1000000
66+
nSamples = noOfPostTriggerSamples + noOfPreTriggerSamples
67+
68+
# Set number of memory segments
69+
noOfCaptures = 10
70+
maxSegments = ctypes.c_uint64(10)
71+
status["memorySegments"] = ps.ps6000aMemorySegments(chandle, noOfCaptures, ctypes.byref(maxSegments))
72+
assert_pico_ok(status["memorySegments"])
73+
74+
# Set number of captures
75+
status["setNoOfCaptures"] = ps.ps6000aSetNoOfCaptures(chandle, noOfCaptures)
76+
assert_pico_ok(status["setNoOfCaptures"])
77+
78+
# Create buffers
79+
bufferAMax = (ctypes.c_int16 * nSamples)()
80+
bufferAMin = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
81+
bufferAMax1 = (ctypes.c_int16 * nSamples)()
82+
bufferAMin1 = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
83+
bufferAMax2 = (ctypes.c_int16 * nSamples)()
84+
bufferAMin2 = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
85+
bufferAMax3 = (ctypes.c_int16 * nSamples)()
86+
bufferAMin3 = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
87+
bufferAMax4 = (ctypes.c_int16 * nSamples)()
88+
bufferAMin4 = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
89+
bufferAMax5 = (ctypes.c_int16 * nSamples)()
90+
bufferAMin5 = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
91+
bufferAMax6 = (ctypes.c_int16 * nSamples)()
92+
bufferAMin6 = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
93+
bufferAMax7 = (ctypes.c_int16 * nSamples)()
94+
bufferAMin7 = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
95+
bufferAMax8 = (ctypes.c_int16 * nSamples)()
96+
bufferAMin8 = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
97+
bufferAMax9 = (ctypes.c_int16 * nSamples)()
98+
bufferAMin9 = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
99+
100+
# Set data buffers
101+
# handle = chandle
102+
# channel = channelA
103+
# bufferMax = bufferAMax
104+
# bufferMin = bufferAMin
105+
# nSamples = nSamples
106+
dataType = enums.PICO_DATA_TYPE["PICO_INT16_T"]
107+
waveform = 0
108+
downSampleMode = enums.PICO_RATIO_MODE["PICO_RATIO_MODE_RAW"]
109+
clear = enums.PICO_ACTION["PICO_CLEAR_ALL"]
110+
add = enums.PICO_ACTION["PICO_ADD"]
111+
action = clear|add # PICO_ACTION["PICO_CLEAR_WAVEFORM_CLEAR_ALL"] | PICO_ACTION["PICO_ADD"]
112+
status["setDataBuffers"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax), ctypes.byref(bufferAMin), nSamples, dataType, waveform, downSampleMode, action)
113+
assert_pico_ok(status["setDataBuffers"])
114+
waveform1 = 1
115+
status["setDataBuffers1"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax1), ctypes.byref(bufferAMin1), nSamples, dataType, waveform1, downSampleMode, add)
116+
assert_pico_ok(status["setDataBuffers1"])
117+
waveform2 = 2
118+
status["setDataBuffers2"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax2), ctypes.byref(bufferAMin2), nSamples, dataType, waveform2, downSampleMode, add)
119+
assert_pico_ok(status["setDataBuffers2"])
120+
waveform3 = 3
121+
status["setDataBuffers3"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax3), ctypes.byref(bufferAMin3), nSamples, dataType, waveform3, downSampleMode, add)
122+
assert_pico_ok(status["setDataBuffers3"])
123+
waveform4 = 4
124+
status["setDataBuffers4"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax4), ctypes.byref(bufferAMin4), nSamples, dataType, waveform4, downSampleMode, add)
125+
assert_pico_ok(status["setDataBuffers4"])
126+
waveform5 = 5
127+
status["setDataBuffers5"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax5), ctypes.byref(bufferAMin5), nSamples, dataType, waveform5, downSampleMode, add)
128+
assert_pico_ok(status["setDataBuffers5"])
129+
waveform6 = 6
130+
status["setDataBuffers6"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax6), ctypes.byref(bufferAMin6), nSamples, dataType, waveform6, downSampleMode, add)
131+
assert_pico_ok(status["setDataBuffers6"])
132+
waveform7 = 7
133+
status["setDataBuffers7"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax7), ctypes.byref(bufferAMin7), nSamples, dataType, waveform7, downSampleMode, add)
134+
assert_pico_ok(status["setDataBuffers7"])
135+
waveform8 = 8
136+
status["setDataBuffers8"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax8), ctypes.byref(bufferAMin8), nSamples, dataType, waveform8, downSampleMode, add)
137+
assert_pico_ok(status["setDataBuffers8"])
138+
waveform9 = 9
139+
status["setDataBuffers9"] = ps.ps6000aSetDataBuffers(chandle, channelA, ctypes.byref(bufferAMax9), ctypes.byref(bufferAMin9), nSamples, dataType, waveform9, downSampleMode, add)
140+
assert_pico_ok(status["setDataBuffers9"])
141+
142+
# Run block capture
143+
# handle = chandle
144+
# timebase = timebase
145+
timeIndisposedMs = ctypes.c_double(0)
146+
# segmentIndex = 0
147+
# lpReady = None Using IsReady rather than a callback
148+
# pParameter = None
149+
status["runBlock"] = ps.ps6000aRunBlock(chandle, noOfPreTriggerSamples, noOfPostTriggerSamples, timebase, ctypes.byref(timeIndisposedMs), 0, None, None)
150+
assert_pico_ok(status["runBlock"])
151+
152+
# Check for data collection to finish using ps6000aIsReady
153+
ready = ctypes.c_int16(0)
154+
check = ctypes.c_int16(0)
155+
while ready.value == check.value:
156+
status["isReady"] = ps.ps6000aIsReady(chandle, ctypes.byref(ready))
157+
158+
# Get data from scope
159+
# handle = chandle
160+
# startIndex = 0
161+
noOfSamples = ctypes.c_uint64(nSamples)
162+
# downSampleRatio = 1
163+
# segmentIndex = 0
164+
overflow = ctypes.c_int16(0)
165+
status["getValues"] = ps.ps6000aGetValuesBulk(chandle, 0, ctypes.byref(noOfSamples),0, 9, 1, downSampleMode, ctypes.byref(overflow))
166+
assert_pico_ok(status["getValues"])
167+
168+
# get max ADC value
169+
# handle = chandle
170+
minADC = ctypes.c_int16()
171+
maxADC = ctypes.c_int16()
172+
status["getAdcLimits"] = ps.ps6000aGetAdcLimits(chandle, resolution, ctypes.byref(minADC), ctypes.byref(maxADC))
173+
assert_pico_ok(status["getAdcLimits"])
174+
175+
# convert ADC counts data to mV
176+
adc2mVChAMax = adc2mV(bufferAMax, channelRange, maxADC)
177+
adc2mVChAMax1 = adc2mV(bufferAMax1, channelRange, maxADC)
178+
adc2mVChAMax2 = adc2mV(bufferAMax2, channelRange, maxADC)
179+
adc2mVChAMax3 = adc2mV(bufferAMax3, channelRange, maxADC)
180+
adc2mVChAMax4 = adc2mV(bufferAMax4, channelRange, maxADC)
181+
adc2mVChAMax5 = adc2mV(bufferAMax5, channelRange, maxADC)
182+
adc2mVChAMax6 = adc2mV(bufferAMax6, channelRange, maxADC)
183+
adc2mVChAMax7 = adc2mV(bufferAMax7, channelRange, maxADC)
184+
adc2mVChAMax8 = adc2mV(bufferAMax8, channelRange, maxADC)
185+
adc2mVChAMax9 = adc2mV(bufferAMax9, channelRange, maxADC)
186+
187+
# Create time data
188+
time = np.linspace(0, (nSamples) * timeInterval.value * 1000000000, nSamples)
189+
190+
# plot data from channel A and B
191+
plt.plot(time, adc2mVChAMax[:])
192+
plt.plot(time, adc2mVChAMax1[:])
193+
plt.plot(time, adc2mVChAMax2[:])
194+
plt.plot(time, adc2mVChAMax3[:])
195+
plt.plot(time, adc2mVChAMax4[:])
196+
plt.plot(time, adc2mVChAMax5[:])
197+
plt.plot(time, adc2mVChAMax6[:])
198+
plt.plot(time, adc2mVChAMax7[:])
199+
plt.plot(time, adc2mVChAMax8[:])
200+
plt.plot(time, adc2mVChAMax9[:])
201+
plt.xlabel('Time (ns)')
202+
plt.ylabel('Voltage (mV)')
203+
plt.show()
204+
205+
# Close the scope
206+
status["closeunit"] = ps.ps6000aCloseUnit(chandle)
207+
assert_pico_ok(status["closeunit"])
208+
209+
print(status)

0 commit comments

Comments
 (0)