Skip to content

Commit f1735a3

Browse files
author
neil.hamilton
committed
Create ps5000aExamples/ps5000aBlockAdvancedTriggerExample.py example
1 parent 02ea689 commit f1735a3

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#
2+
# Copyright (C) 2018 Pico Technology Ltd. See LICENSE file for terms.
3+
#
4+
# PS5000A BLOCK MODE EXAMPLE
5+
# This example opens a 5000a driver device, sets up two channels and a trigger then collects a block of data.
6+
# This data is then plotted as mV against time in ns.
7+
8+
import ctypes
9+
import numpy as np
10+
from picosdk.ps5000a import ps5000a as ps
11+
import matplotlib.pyplot as plt
12+
from picosdk.functions import adc2mV, assert_pico_ok, mV2adc
13+
14+
# Create chandle and status ready for use
15+
chandle = ctypes.c_int16()
16+
status = {}
17+
18+
# Open 5000 series PicoScope
19+
# Resolution set to 12 Bit
20+
resolution =ps.PS5000A_DEVICE_RESOLUTION["PS5000A_DR_12BIT"]
21+
# Returns handle to chandle for use in future API functions
22+
status["openunit"] = ps.ps5000aOpenUnit(ctypes.byref(chandle), None, resolution)
23+
24+
try:
25+
assert_pico_ok(status["openunit"])
26+
except: # PicoNotOkError:
27+
28+
powerStatus = status["openunit"]
29+
30+
if powerStatus == 286:
31+
status["changePowerSource"] = ps.ps5000aChangePowerSource(chandle, powerStatus)
32+
elif powerStatus == 282:
33+
status["changePowerSource"] = ps.ps5000aChangePowerSource(chandle, powerStatus)
34+
else:
35+
raise
36+
37+
assert_pico_ok(status["changePowerSource"])
38+
39+
# Set up channel A
40+
# handle = chandle
41+
channel = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"]
42+
# enabled = 1
43+
coupling_type = ps.PS5000A_COUPLING["PS5000A_DC"]
44+
chARange = ps.PS5000A_RANGE["PS5000A_20V"]
45+
# analogue offset = 0 V
46+
status["setChA"] = ps.ps5000aSetChannel(chandle, channel, 1, coupling_type, chARange, 0)
47+
assert_pico_ok(status["setChA"])
48+
49+
# Set up channel B
50+
# handle = chandle
51+
channel = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_B"]
52+
# enabled = 1
53+
# coupling_type = ps.PS5000A_COUPLING["PS5000A_DC"]
54+
chBRange = ps.PS5000A_RANGE["PS5000A_2V"]
55+
# analogue offset = 0 V
56+
status["setChB"] = ps.ps5000aSetChannel(chandle, channel, 1, coupling_type, chBRange, 0)
57+
assert_pico_ok(status["setChB"])
58+
59+
# find maximum ADC count value
60+
# handle = chandle
61+
# pointer to value = ctypes.byref(maxADC)
62+
maxADC = ctypes.c_int16()
63+
status["maximumValue"] = ps.ps5000aMaximumValue(chandle, ctypes.byref(maxADC))
64+
assert_pico_ok(status["maximumValue"])
65+
66+
# Set up an advanced trigger
67+
68+
69+
70+
# Set number of pre and post trigger samples to be collected
71+
preTriggerSamples = 2500
72+
postTriggerSamples = 2500
73+
maxSamples = preTriggerSamples + postTriggerSamples
74+
75+
# Get timebase information
76+
# handle = chandle
77+
timebase = 8
78+
# noSamples = maxSamples
79+
# pointer to timeIntervalNanoseconds = ctypes.byref(timeIntervalns)
80+
# pointer to maxSamples = ctypes.byref(returnedMaxSamples)
81+
# segment index = 0
82+
timeIntervalns = ctypes.c_float()
83+
returnedMaxSamples = ctypes.c_int32()
84+
status["getTimebase2"] = ps.ps5000aGetTimebase2(chandle, timebase, maxSamples, ctypes.byref(timeIntervalns), ctypes.byref(returnedMaxSamples), 0)
85+
assert_pico_ok(status["getTimebase2"])
86+
87+
# Run block capture
88+
# handle = chandle
89+
# number of pre-trigger samples = preTriggerSamples
90+
# number of post-trigger samples = PostTriggerSamples
91+
# timebase = 8 = 80 ns (see Programmer's guide for mre information on timebases)
92+
# time indisposed ms = None (not needed in the example)
93+
# segment index = 0
94+
# lpReady = None (using ps5000aIsReady rather than ps5000aBlockReady)
95+
# pParameter = None
96+
status["runBlock"] = ps.ps5000aRunBlock(chandle, preTriggerSamples, postTriggerSamples, timebase, None, 0, None, None)
97+
assert_pico_ok(status["runBlock"])
98+
99+
# Check for data collection to finish using ps5000aIsReady
100+
ready = ctypes.c_int16(0)
101+
check = ctypes.c_int16(0)
102+
while ready.value == check.value:
103+
status["isReady"] = ps.ps5000aIsReady(chandle, ctypes.byref(ready))
104+
105+
106+
# Create buffers ready for assigning pointers for data collection
107+
bufferAMax = (ctypes.c_int16 * maxSamples)()
108+
bufferAMin = (ctypes.c_int16 * maxSamples)() # used for downsampling which isn't in the scope of this example
109+
bufferBMax = (ctypes.c_int16 * maxSamples)()
110+
bufferBMin = (ctypes.c_int16 * maxSamples)() # used for downsampling which isn't in the scope of this example
111+
112+
# Set data buffer location for data collection from channel A
113+
# handle = chandle
114+
source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"]
115+
# pointer to buffer max = ctypes.byref(bufferAMax)
116+
# pointer to buffer min = ctypes.byref(bufferAMin)
117+
# buffer length = maxSamples
118+
# segment index = 0
119+
# ratio mode = PS5000A_RATIO_MODE_NONE = 0
120+
status["setDataBuffersA"] = ps.ps5000aSetDataBuffers(chandle, source, ctypes.byref(bufferAMax), ctypes.byref(bufferAMin), maxSamples, 0, 0)
121+
assert_pico_ok(status["setDataBuffersA"])
122+
123+
# Set data buffer location for data collection from channel B
124+
# handle = chandle
125+
source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_B"]
126+
# pointer to buffer max = ctypes.byref(bufferBMax)
127+
# pointer to buffer min = ctypes.byref(bufferBMin)
128+
# buffer length = maxSamples
129+
# segment index = 0
130+
# ratio mode = PS5000A_RATIO_MODE_NONE = 0
131+
status["setDataBuffersB"] = ps.ps5000aSetDataBuffers(chandle, source, ctypes.byref(bufferBMax), ctypes.byref(bufferBMin), maxSamples, 0, 0)
132+
assert_pico_ok(status["setDataBuffersB"])
133+
134+
# create overflow loaction
135+
overflow = ctypes.c_int16()
136+
# create converted type maxSamples
137+
cmaxSamples = ctypes.c_int32(maxSamples)
138+
139+
# Retried data from scope to buffers assigned above
140+
# handle = chandle
141+
# start index = 0
142+
# pointer to number of samples = ctypes.byref(cmaxSamples)
143+
# downsample ratio = 0
144+
# downsample ratio mode = PS5000A_RATIO_MODE_NONE
145+
# pointer to overflow = ctypes.byref(overflow))
146+
status["getValues"] = ps.ps5000aGetValues(chandle, 0, ctypes.byref(cmaxSamples), 0, 0, 0, ctypes.byref(overflow))
147+
assert_pico_ok(status["getValues"])
148+
149+
150+
# convert ADC counts data to mV
151+
adc2mVChAMax = adc2mV(bufferAMax, chARange, maxADC)
152+
adc2mVChBMax = adc2mV(bufferBMax, chBRange, maxADC)
153+
154+
# Create time data
155+
time = np.linspace(0, (cmaxSamples.value) * timeIntervalns.value, cmaxSamples.value)
156+
157+
# plot data from channel A and B
158+
plt.plot(time, adc2mVChAMax[:])
159+
plt.plot(time, adc2mVChBMax[:])
160+
plt.xlabel('Time (ns)')
161+
plt.ylabel('Voltage (mV)')
162+
plt.show()
163+
164+
# Stop the scope
165+
# handle = chandle
166+
status["stop"] = ps.ps5000aStop(chandle)
167+
assert_pico_ok(status["stop"])
168+
169+
# Close unit Disconnect the scope
170+
# handle = chandle
171+
status["close"]=ps.ps5000aCloseUnit(chandle)
172+
assert_pico_ok(status["close"])
173+
174+
# display status returns
175+
print(status)

0 commit comments

Comments
 (0)