Skip to content

Commit bba436e

Browse files
author
neil.hamilton
committed
Create block example using advanced triggers functions
1 parent cba48ed commit bba436e

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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 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.ps6000a import ps6000a as ps
11+
from picosdk.PicoDeviceEnums import picoEnum as enums
12+
from picosdk.PicoDeviceStructs import picoStruct as struct
13+
import matplotlib.pyplot as plt
14+
from picosdk.functions import adc2mV, assert_pico_ok, mV2adc
15+
16+
# Create chandle and status ready for use
17+
chandle = ctypes.c_int16()
18+
status = {}
19+
20+
# Open 6000 A series PicoScope
21+
# returns handle to chandle for use in future API functions
22+
resolution = enums.PICO_DEVICE_RESOLUTION["PICO_DR_8BIT"]
23+
status["openunit"] = ps.ps6000aOpenUnit(ctypes.byref(chandle), None, resolution)
24+
assert_pico_ok(status["openunit"])
25+
26+
# Set channel A on
27+
# handle = chandle
28+
channelA = enums.PICO_CHANNEL["PICO_CHANNEL_A"]
29+
coupling = enums.PICO_COUPLING["PICO_DC"]
30+
channelRange = 7
31+
# analogueOffset = 0 V
32+
bandwidth = enums.PICO_BANDWIDTH_LIMITER["PICO_BW_FULL"]
33+
status["setChannelA"] = ps.ps6000aSetChannelOn(chandle, channelA, coupling, channelRange, 0, bandwidth)
34+
assert_pico_ok(status["setChannelA"])
35+
36+
channelB = enums.PICO_CHANNEL["PICO_CHANNEL_B"]
37+
status["setChannelB"] = ps.ps6000aSetChannelOn(chandle, channelB, coupling, channelRange, 0, bandwidth)
38+
assert_pico_ok(status["setChannelB"])
39+
40+
# set channel C-H off
41+
for x in range (2, 7, 1):
42+
channel = x
43+
status["setChannel",x] = ps.ps6000aSetChannelOff(chandle,channel)
44+
assert_pico_ok(status["setChannel",x])
45+
46+
# get max ADC value
47+
# handle = chandle
48+
minADC = ctypes.c_int16()
49+
maxADC = ctypes.c_int16()
50+
status["getAdcLimits"] = ps.ps6000aGetAdcLimits(chandle, resolution, ctypes.byref(minADC), ctypes.byref(maxADC))
51+
assert_pico_ok(status["getAdcLimits"])
52+
53+
# use the trigger functions seperately
54+
# set up a simple edge trigger on channel A OR B with a 1 V threshold
55+
56+
conditions = (struct.PICO_CONDITION * 2)()
57+
conditions[0] = struct.PICO_CONDITION(channelA,enums.PICO_TRIGGER_STATE["PICO_CONDITION_TRUE"])
58+
conditions[1] = struct.PICO_CONDITION(channelB, enums.PICO_TRIGGER_STATE["PICO_CONDITION_TRUE"])
59+
nConditions = 2
60+
clear = enums.PICO_ACTION["PICO_CLEAR_ALL"]
61+
add = enums.PICO_ACTION["PICO_ADD"]
62+
action = clear|add # PICO_ACTION["PICO_CLEAR_WAVEFORM_CLEAR_ALL"] | PICO_ACTION["PICO_ADD"]
63+
status["setTriggerChannelConditions"] = ps.ps6000aSetTriggerChannelConditions(chandle, ctypes.byref(conditions), nConditions, action)
64+
assert_pico_ok(status["setTriggerChannelConditions"])
65+
66+
directions = (struct.PICO_DIRECTION * 2)()
67+
directions[0]= struct.PICO_DIRECTION(channelA, enums.PICO_THRESHOLD_DIRECTION["PICO_RISING"], enums.PICO_THRESHOLD_MODE["PICO_LEVEL"])
68+
directions[1]= struct.PICO_DIRECTION(channelB, enums.PICO_THRESHOLD_DIRECTION["PICO_RISING"], enums.PICO_THRESHOLD_MODE["PICO_LEVEL"])
69+
nDirections = 2
70+
status["setTriggerChannelDirections"] = ps.ps6000aSetTriggerChannelDirections(chandle,ctypes.byref(directions),nDirections)
71+
assert_pico_ok(status["setTriggerChannelDirections"])
72+
73+
channelProperties = (struct.PICO_TRIGGER_CHANNEL_PROPERTIES * 2)()
74+
channelProperties[0] = struct.PICO_TRIGGER_CHANNEL_PROPERTIES(mV2adc(1000,channelRange,maxADC), 0, 0, 0, channelA)
75+
channelProperties[1] = struct.PICO_TRIGGER_CHANNEL_PROPERTIES(mV2adc(1000,channelRange,maxADC), 0, 0, 0, channelB)
76+
nChannelProperties = 2
77+
autoTriggerMicroSeconds = 1000000
78+
status["setTriggerChannelProperties"] = ps.ps6000aSetTriggerChannelProperties(chandle, ctypes.byref(channelProperties),nChannelProperties,0,autoTriggerMicroSeconds)
79+
assert_pico_ok(status["setTriggerChannelProperties"])
80+
81+
# Get fastest available timebase
82+
# handle = chandle
83+
enabledChannelFlags = enums.PICO_CHANNEL_FLAGS["PICO_CHANNEL_A_FLAGS"] + enums.PICO_CHANNEL_FLAGS["PICO_CHANNEL_B_FLAGS"]
84+
timebase = ctypes.c_uint32(0)
85+
timeInterval = ctypes.c_double(0)
86+
# resolution = resolution
87+
status["getMinimumTimebaseStateless"] = ps.ps6000aGetMinimumTimebaseStateless(chandle, enabledChannelFlags, ctypes.byref(timebase), ctypes.byref(timeInterval), resolution)
88+
print("timebase = ", timebase.value)
89+
print("sample interval =", timeInterval.value, "s")
90+
91+
# Set number of samples to be collected
92+
noOfPreTriggerSamples = 500000
93+
noOfPostTriggerSamples = 1000000
94+
nSamples = noOfPostTriggerSamples + noOfPreTriggerSamples
95+
96+
# Create buffers
97+
bufferAMax = (ctypes.c_int16 * nSamples)()
98+
bufferAMin = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
99+
bufferBMax = (ctypes.c_int16 * nSamples)()
100+
bufferBMin = (ctypes.c_int16 * nSamples)() # used for downsampling which isn't in the scope of this example
101+
102+
# Set data buffers
103+
# handle = chandle
104+
# channel = channelA
105+
# bufferMax = bufferAMax
106+
# bufferMin = bufferAMin
107+
# nSamples = nSamples
108+
dataType = enums.PICO_DATA_TYPE["PICO_INT16_T"]
109+
waveform = 0
110+
downSampleMode = enums.PICO_RATIO_MODE["PICO_RATIO_MODE_RAW"]
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+
status["setDataBuffers"] = ps.ps6000aSetDataBuffers(chandle, channelB, ctypes.byref(bufferBMax), ctypes.byref(bufferBMin), nSamples, dataType, waveform, downSampleMode, action)
115+
assert_pico_ok(status["setDataBuffers"])
116+
117+
# Run block capture
118+
# handle = chandle
119+
# timebase = timebase
120+
timeIndisposedMs = ctypes.c_double(0)
121+
# segmentIndex = 0
122+
# lpReady = None Using IsReady rather than a callback
123+
# pParameter = None
124+
status["runBlock"] = ps.ps6000aRunBlock(chandle, noOfPreTriggerSamples, noOfPostTriggerSamples, timebase, ctypes.byref(timeIndisposedMs), 0, None, None)
125+
assert_pico_ok(status["runBlock"])
126+
127+
# Check for data collection to finish using ps6000aIsReady
128+
ready = ctypes.c_int16(0)
129+
check = ctypes.c_int16(0)
130+
while ready.value == check.value:
131+
status["isReady"] = ps.ps6000aIsReady(chandle, ctypes.byref(ready))
132+
133+
# Get data from scope
134+
# handle = chandle
135+
# startIndex = 0
136+
noOfSamples = ctypes.c_uint64(nSamples)
137+
# downSampleRatio = 1
138+
# segmentIndex = 0
139+
overflow = ctypes.c_int16(0)
140+
status["getValues"] = ps.ps6000aGetValues(chandle, 0, ctypes.byref(noOfSamples), 1, downSampleMode, 0, ctypes.byref(overflow))
141+
assert_pico_ok(status["getValues"])
142+
143+
# convert ADC counts data to mV
144+
adc2mVChAMax = adc2mV(bufferAMax, channelRange, maxADC)
145+
adc2mVChBMax = adc2mV(bufferBMax, channelRange, maxADC)
146+
147+
# Create time data
148+
time = np.linspace(0, (nSamples -1) * timeInterval.value * 1000000000, nSamples)
149+
150+
# plot data from channel A and B
151+
plt.plot(time, adc2mVChAMax[:])
152+
plt.plot(time, adc2mVChBMax[:])
153+
plt.xlabel('Time (ns)')
154+
plt.ylabel('Voltage (mV)')
155+
plt.show()
156+
157+
# Close the scope
158+
status["closeunit"] = ps.ps6000aCloseUnit(chandle)
159+
assert_pico_ok(status["closeunit"])
160+
161+
print(status)

0 commit comments

Comments
 (0)