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