|
| 1 | +# |
| 2 | +# Copyright (C) 2019 Pico Technology Ltd. See LICENSE file for terms. |
| 3 | +# |
| 4 | +# PS6000 Series STREAMING MODE EXAMPLE |
| 5 | +# This example demonstrates how to call the ps6000 driver API functions in order to open a device, setup 2 channels and collects streamed data (1 buffer). |
| 6 | +# This data is then plotted as mV against time in ns. |
| 7 | + |
| 8 | +import ctypes |
| 9 | +import numpy as np |
| 10 | +from picosdk.ps6000 import ps6000 as ps |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +from picosdk.functions import adc2mV, assert_pico_ok |
| 13 | +import time |
| 14 | + |
| 15 | +# Create chandle and status ready for use |
| 16 | +chandle = ctypes.c_int16() |
| 17 | +status = {} |
| 18 | + |
| 19 | +# Open PicoScope 6000 Series device |
| 20 | +# Returns handle to chandle for use in future API functions |
| 21 | +status["openunit"] = ps.ps6000OpenUnit(ctypes.byref(chandle), None) |
| 22 | +assert_pico_ok(status["openunit"]) |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +enabled = 1 |
| 27 | +disabled = 0 |
| 28 | +analogue_offset = 0.0 |
| 29 | + |
| 30 | +# Set up channel A |
| 31 | +# handle = chandle |
| 32 | +# channel = PS6000_CHANNEL_A = 0 |
| 33 | +# enabled = 1 |
| 34 | +# coupling type = PS6000_DC = 1 |
| 35 | +# range = PS6000_2V = 7 |
| 36 | +# analogue offset = 0 V |
| 37 | +channel_range = ps.PS6000_RANGE['PS6000_2V'] |
| 38 | +status["setChA"] = ps.ps6000SetChannel(chandle, |
| 39 | + ps.PS6000_CHANNEL['PS6000_CHANNEL_A'], |
| 40 | + enabled, |
| 41 | + ps.PS6000_COUPLING['PS6000_DC_1M'], |
| 42 | + channel_range, |
| 43 | + analogue_offset, |
| 44 | + ps.PS6000_BANDWIDTH_LIMITER['PS6000_BW_FULL']) |
| 45 | +assert_pico_ok(status["setChA"]) |
| 46 | + |
| 47 | +# Set up channel B |
| 48 | +# handle = chandle |
| 49 | +# channel = PS6000_CHANNEL_B = 1 |
| 50 | +# enabled = 1 |
| 51 | +# coupling type = PS6000_DC = 1 |
| 52 | +# range = PS6000_2V = 7 |
| 53 | +# analogue offset = 0 V |
| 54 | +status["setChB"] = ps.ps6000SetChannel(chandle, |
| 55 | + ps.PS6000_CHANNEL['PS6000_CHANNEL_B'], |
| 56 | + enabled, |
| 57 | + ps.PS6000_COUPLING['PS6000_DC_1M'], |
| 58 | + channel_range, |
| 59 | + analogue_offset, |
| 60 | + ps.PS6000_BANDWIDTH_LIMITER['PS6000_BW_FULL']) |
| 61 | +assert_pico_ok(status["setChB"]) |
| 62 | + |
| 63 | +# Size of capture |
| 64 | +sizeOfOneBuffer = 500 |
| 65 | +numBuffersToCapture = 10 |
| 66 | + |
| 67 | +totalSamples = sizeOfOneBuffer * numBuffersToCapture |
| 68 | + |
| 69 | +# Create buffers ready for assigning pointers for data collection |
| 70 | +bufferAMax = np.zeros(shape=sizeOfOneBuffer, dtype=np.int16) |
| 71 | +bufferBMax = np.zeros(shape=sizeOfOneBuffer, dtype=np.int16) |
| 72 | + |
| 73 | +# Set data buffer location for data collection from channel A |
| 74 | +# handle = chandle |
| 75 | +# source = PS6000_CHANNEL_A = 0 |
| 76 | +# pointer to buffer max = ctypes.byref(bufferAMax) |
| 77 | +# pointer to buffer min = ctypes.byref(bufferAMin) |
| 78 | +# buffer length = maxSamples |
| 79 | +# segment index = 0 |
| 80 | +# ratio mode = PS6000_RATIO_MODE_NONE = 0 |
| 81 | +status["setDataBuffersA"] = ps.ps6000SetDataBuffers(chandle, |
| 82 | + ps.PS6000_CHANNEL['PS6000_CHANNEL_A'], |
| 83 | + bufferAMax.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)), |
| 84 | + None, |
| 85 | + sizeOfOneBuffer, |
| 86 | + ps.PS6000_RATIO_MODE['PS6000_RATIO_MODE_NONE']) |
| 87 | +assert_pico_ok(status["setDataBuffersA"]) |
| 88 | + |
| 89 | +# Set data buffer location for data collection from channel B |
| 90 | +# handle = chandle |
| 91 | +# source = PS6000_CHANNEL_B = 1 |
| 92 | +# pointer to buffer max = ctypes.byref(bufferBMax) |
| 93 | +# pointer to buffer min = ctypes.byref(bufferBMin) |
| 94 | +# buffer length = maxSamples |
| 95 | +# segment index = 0 |
| 96 | +# ratio mode = PS6000_RATIO_MODE_NONE = 0 |
| 97 | +status["setDataBuffersB"] = ps.ps6000SetDataBuffers(chandle, |
| 98 | + ps.PS6000_CHANNEL['PS6000_CHANNEL_B'], |
| 99 | + bufferBMax.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)), |
| 100 | + None, |
| 101 | + sizeOfOneBuffer, |
| 102 | + ps.PS6000_RATIO_MODE['PS6000_RATIO_MODE_NONE']) |
| 103 | +assert_pico_ok(status["setDataBuffersB"]) |
| 104 | + |
| 105 | +# Begin streaming mode: |
| 106 | +sampleInterval = ctypes.c_int32(250) |
| 107 | +sampleUnits = ps.PS6000_TIME_UNITS['PS6000_US'] |
| 108 | +# We are not triggering: |
| 109 | +maxPreTriggerSamples = 0 |
| 110 | +autoStopOn = 1 |
| 111 | +# No downsampling: |
| 112 | +downsampleRatio = 1 |
| 113 | +status["runStreaming"] = ps.ps6000RunStreaming(chandle, |
| 114 | + ctypes.byref(sampleInterval), |
| 115 | + sampleUnits, |
| 116 | + maxPreTriggerSamples, |
| 117 | + totalSamples, |
| 118 | + autoStopOn, |
| 119 | + downsampleRatio, |
| 120 | + ps.PS6000_RATIO_MODE['PS6000_RATIO_MODE_NONE'], |
| 121 | + sizeOfOneBuffer) |
| 122 | +assert_pico_ok(status["runStreaming"]) |
| 123 | + |
| 124 | +actualSampleInterval = sampleInterval.value |
| 125 | +actualSampleIntervalNs = actualSampleInterval * 1000 |
| 126 | + |
| 127 | +print("Capturing at sample interval %s ns" % actualSampleIntervalNs) |
| 128 | + |
| 129 | +# We need a big buffer, not registered with the driver, to keep our complete capture in. |
| 130 | +bufferCompleteA = np.zeros(shape=totalSamples, dtype=np.int16) |
| 131 | +bufferCompleteB = np.zeros(shape=totalSamples, dtype=np.int16) |
| 132 | +nextSample = 0 |
| 133 | +autoStopOuter = False |
| 134 | +wasCalledBack = False |
| 135 | + |
| 136 | + |
| 137 | +def streaming_callback(handle, noOfSamples, startIndex, overflow, triggerAt, triggered, autoStop, param): |
| 138 | + global nextSample, autoStopOuter, wasCalledBack |
| 139 | + wasCalledBack = True |
| 140 | + destEnd = nextSample + noOfSamples |
| 141 | + sourceEnd = startIndex + noOfSamples |
| 142 | + bufferCompleteA[nextSample:destEnd] = bufferAMax[startIndex:sourceEnd] |
| 143 | + bufferCompleteB[nextSample:destEnd] = bufferBMax[startIndex:sourceEnd] |
| 144 | + nextSample += noOfSamples |
| 145 | + if autoStop: |
| 146 | + autoStopOuter = True |
| 147 | + |
| 148 | + |
| 149 | +# Convert the python function into a C function pointer. |
| 150 | +cFuncPtr = ps.StreamingReadyType(streaming_callback) |
| 151 | + |
| 152 | +# Fetch data from the driver in a loop, copying it out of the registered buffers and into our complete one. |
| 153 | +while nextSample < totalSamples and not autoStopOuter: |
| 154 | + wasCalledBack = False |
| 155 | + status["getStreamingLastestValues"] = ps.ps6000GetStreamingLatestValues(chandle, cFuncPtr, None) |
| 156 | + if not wasCalledBack: |
| 157 | + # If we weren't called back by the driver, this means no data is ready. Sleep for a short while before trying |
| 158 | + # again. |
| 159 | + time.sleep(0.01) |
| 160 | + |
| 161 | +print("Done grabbing values.") |
| 162 | + |
| 163 | +# Find maximum ADC count value |
| 164 | +# handle = chandle |
| 165 | +# pointer to value = ctypes.byref(maxADC) |
| 166 | +maxADC = ctypes.c_int16(32512) |
| 167 | + |
| 168 | +# Convert ADC counts data to mV |
| 169 | +adc2mVChAMax = adc2mV(bufferCompleteA, channel_range, maxADC) |
| 170 | +adc2mVChBMax = adc2mV(bufferCompleteB, channel_range, maxADC) |
| 171 | + |
| 172 | +# Create time data |
| 173 | +time = np.linspace(0, (totalSamples) * actualSampleIntervalNs, totalSamples) |
| 174 | + |
| 175 | +# Plot data from channel A and B |
| 176 | +plt.plot(time, adc2mVChAMax[:]) |
| 177 | +plt.plot(time, adc2mVChBMax[:]) |
| 178 | +plt.xlabel('Time (ns)') |
| 179 | +plt.ylabel('Voltage (mV)') |
| 180 | +plt.show() |
| 181 | + |
| 182 | +# Stop the scope |
| 183 | +# handle = chandle |
| 184 | +status["stop"] = ps.ps6000Stop(chandle) |
| 185 | +assert_pico_ok(status["stop"]) |
| 186 | + |
| 187 | +# Disconnect the scope |
| 188 | +# handle = chandle |
| 189 | +status["close"] = ps.ps6000CloseUnit(chandle) |
| 190 | +assert_pico_ok(status["close"]) |
| 191 | + |
| 192 | +# Display status returns |
| 193 | +print(status) |
0 commit comments