Skip to content

Commit f95053e

Browse files
author
neil.hamilton
committed
Add PS6000A_ID_Rapid_Block_Example.m
1 parent 91808fa commit f95053e

File tree

1 file changed

+311
-0
lines changed

1 file changed

+311
-0
lines changed
Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
%% PicoScope 6000 Series (A API) Instrument Driver Oscilloscope Rapid Block Data Capture Example
2+
% This is an example of an instrument control session using a device
3+
% object. The instrument control session comprises all the steps you
4+
% are likely to take when communicating with your instrument.
5+
%
6+
% These steps are:
7+
%
8+
% # Create a device object
9+
% # Connect to the instrument
10+
% # Configure properties
11+
% # Invoke functions
12+
% # Disconnect from the instrument
13+
%
14+
% To run the instrument control session, type the name of the file,
15+
% PS6000A_ID_Rapid_Block_Example, at the MATLAB command prompt.
16+
%
17+
% The file, PS6000A_ID_RAPID_BLOCK_EXAMPLE.M must be on your MATLAB PATH. For
18+
% additional information on setting your MATLAB PATH, type 'help addpath'
19+
% at the MATLAB command prompt.
20+
%
21+
% *Example:*
22+
% PS6000A_ID_Rapid_Block_Example;
23+
%
24+
% *Description:*
25+
% Demonstrates how to set properties and call functions in order to
26+
% capture a rapid block run from a PicoScope 6000 (A API) Series Oscilloscope.
27+
%
28+
% *See also:* <matlab:doc('icdevice') |icdevice|> | <matlab:doc('instrument/invoke') |invoke|>
29+
%
30+
% *Copyright:* © 2020-2021 Pico Technology Ltd. See LICENSE file for terms.
31+
32+
%% Suggested Input Settings
33+
34+
%% Clear Command Window and Close Figures
35+
36+
clc;
37+
close all;
38+
39+
%% Load Configuration Information
40+
41+
[ps6000aStructs, ps6000aEnumInfo]=PS6000aSetConfig();
42+
43+
%% Device Connection
44+
45+
% Check if an Instrument session using the device object 'ps6000DeviceObj'
46+
% is still open, and if so, disconnect if the User chooses 'Yes' when prompted.
47+
if (exist('ps6000aDeviceObj', 'var') && ps6000aDeviceObj.isvalid && strcmp(ps6000aDeviceObj.status, 'open'))
48+
49+
openDevice = questionDialog(['Device object ps6000aDeviceObj has an open connection. ' ...
50+
'Do you wish to close the connection and continue?'], ...
51+
'Device Object Connection Open');
52+
53+
if (openDevice == PicoConstants.TRUE)
54+
55+
% Close connection to device
56+
disconnect(ps6000aDeviceObj);
57+
delete(ps6000aDeviceObj);
58+
59+
else
60+
61+
% Exit script if User selects 'No'
62+
return;
63+
64+
end
65+
66+
end
67+
68+
%% Create a device object.
69+
% The serial number can be specified as a second input parameter.
70+
71+
ps6000aDeviceObj = icdevice('picotech_ps6000a_generic.mdd','');
72+
73+
%% Connect scope
74+
75+
connect(ps6000aDeviceObj)
76+
77+
%% Set Device Resolution
78+
79+
resolution = ps6000aEnumInfo.enPicoDeviceResolution.PICO_DR_10BIT;
80+
81+
[status.setResolution] = invoke(ps6000aDeviceObj, 'ps6000aSetDeviceResolution', resolution);
82+
83+
disp('Device Resolution set to 10 bits')
84+
85+
%% Enable Channel A + B
86+
% Disable other channels
87+
for i = (0:7)
88+
try
89+
[status.setChannelOff] = invoke(ps6000aDeviceObj, 'ps6000aSetChannelOff', i);
90+
catch
91+
92+
end
93+
end
94+
95+
for j = (128:1:131)
96+
try
97+
[status.turnDigitalPortOff] = invoke(ps6000aDeviceObj, 'ps6000aDigitalPortOff',j);
98+
catch
99+
end
100+
end
101+
102+
% Enable channels A + B with +-5 V range with DC coupling and full bandwidth
103+
104+
channelA = ps6000aEnumInfo.enPicoChannel.PICO_CHANNEL_A;
105+
channelB = ps6000aEnumInfo.enPicoChannel.PICO_CHANNEL_B;
106+
couplingDC = ps6000aEnumInfo.enPicoCoupling.PICO_DC;
107+
range = ps6000aEnumInfo.enPicoConnectProbeRange.PICO_X1_PROBE_5V;
108+
bandwidth = ps6000aEnumInfo.enPicoBandwidthLimiter.PICO_BW_FULL;
109+
110+
111+
[status.setChannelOn.A] = invoke(ps6000aDeviceObj, 'ps6000aSetChannelOn', channelA, couplingDC, range, 0, bandwidth);
112+
[status.setChannelOn.B] = invoke(ps6000aDeviceObj, 'ps6000aSetChannelOn', channelB, couplingDC, range, 0, bandwidth);
113+
114+
disp('Channels A and B set')
115+
116+
%% Set Simple Trigger
117+
118+
enable = 1;
119+
source = channelA;
120+
threshold = 1000; %mV
121+
direction = ps6000aEnumInfo.enPicoThresholdDirection.PICO_RISING;
122+
delay = 0;
123+
autoTriggerMicroSeconds = 1000000; %us
124+
125+
[status.setSimpleTrigger] = invoke(ps6000aDeviceObj, 'ps6000aSetSimpleTrigger', enable, source, threshold, direction,...
126+
delay, autoTriggerMicroSeconds);
127+
128+
disp('Simple Trigger set')
129+
130+
131+
%% Get Fastest Timebase
132+
133+
enabledChannelFlags= ps6000aEnumInfo.enPicoChannelFlags.PICO_CHANNEL_A_FLAGS + ps6000aEnumInfo.enPicoChannelFlags.PICO_CHANNEL_B_FLAGS;
134+
pTimebase = libpointer('uint32Ptr',0);
135+
pTimeInterval = libpointer('doublePtr',0);
136+
137+
[status.getMinimumTimebaseStateless] = invoke(ps6000aDeviceObj, 'ps6000aGetMinimumTimebaseStateless', enabledChannelFlags,...
138+
pTimebase, pTimeInterval, resolution);
139+
140+
timebase = pTimebase.Value;
141+
timeInterval = pTimeInterval.Value;
142+
143+
%% Set memory segments
144+
145+
nSegments = 10;
146+
nMaxSamples = 10000000;
147+
pnMaxSamples = libpointer('uint64Ptr', nMaxSamples);
148+
[status.memorySegments] = invoke(ps6000aDeviceObj, 'ps6000aMemorySegments', nSegments, pnMaxSamples);
149+
150+
%% Set number of samples to be collected
151+
152+
numPreTriggerSamples = 1000000;
153+
numPostTriggerSamples = 9000000;
154+
totalSamples = numPreTriggerSamples + numPostTriggerSamples;
155+
156+
%% Set number of captures
157+
158+
[status.setNoOfCaptures] = invoke(ps6000aDeviceObj, 'ps6000aSetNoOfCaptures', nSegments);
159+
160+
%% Create Buffers
161+
162+
bufferAMax = zeros(totalSamples, 1, 'int16');
163+
bufferBMax = zeros(totalSamples, 1, 'int16');
164+
165+
for i=(1:10)
166+
pBufferAMax(i) =libpointer('int16Ptr', bufferAMax);
167+
pBufferBMax(i) =libpointer('int16Ptr', bufferBMax);
168+
end
169+
170+
dataType = ps6000aEnumInfo.enPicoDataType.PICO_INT16_T;
171+
downSampleRatioMode = ps6000aEnumInfo.enPicoRatioMode.PICO_RATIO_MODE_AVERAGE;
172+
actionA = bitor(ps6000aEnumInfo.enPicoAction.PICO_CLEAR_ALL, ps6000aEnumInfo.enPicoAction.PICO_ADD);
173+
actionB = ps6000aEnumInfo.enPicoAction.PICO_ADD;
174+
175+
[status.setBufferA.zero] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(1), ...
176+
totalSamples, dataType, 0, downSampleRatioMode, actionA);
177+
[status.setBufferB.zero] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(1), ...
178+
totalSamples, dataType, 0, downSampleRatioMode, actionB);
179+
180+
[status.setBufferA.one] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(2), ...
181+
totalSamples, dataType, 1, downSampleRatioMode, actionB);
182+
[status.setBufferB.one] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(2), ...
183+
totalSamples, dataType, 1, downSampleRatioMode, actionB);
184+
185+
[status.setBufferA.two] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(3), ...
186+
totalSamples, dataType, 2, downSampleRatioMode, actionB);
187+
[status.setBufferB.two] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(3), ...
188+
totalSamples, dataType, 2, downSampleRatioMode, actionB);
189+
190+
[status.setBufferA.three] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(4), ...
191+
totalSamples, dataType, 3, downSampleRatioMode, actionB);
192+
[status.setBufferB.three] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(4), ...
193+
totalSamples, dataType, 3, downSampleRatioMode, actionB);
194+
195+
[status.setBufferA.four] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(5), ...
196+
totalSamples, dataType, 4, downSampleRatioMode, actionB);
197+
[status.setBufferB.four] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(5), ...
198+
totalSamples, dataType, 4, downSampleRatioMode, actionB);
199+
200+
[status.setBufferA.five] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(6), ...
201+
totalSamples, dataType, 5, downSampleRatioMode, actionB);
202+
[status.setBufferB.five] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(6), ...
203+
totalSamples, dataType, 5, downSampleRatioMode, actionB);
204+
205+
[status.setBufferA.six] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(7), ...
206+
totalSamples, dataType, 6, downSampleRatioMode, actionB);
207+
[status.setBufferB.six] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(7), ...
208+
totalSamples, dataType, 6, downSampleRatioMode, actionB);
209+
210+
[status.setBufferA.seven] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(8), ...
211+
totalSamples, dataType, 7, downSampleRatioMode, actionB);
212+
[status.setBufferB.seven] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(8), ...
213+
totalSamples, dataType, 7, downSampleRatioMode, actionB);
214+
215+
[status.setBufferA.eight] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(9), ...
216+
totalSamples, dataType, 8, downSampleRatioMode, actionB);
217+
[status.setBufferB.eight] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(9), ...
218+
totalSamples, dataType, 8, downSampleRatioMode, actionB);
219+
220+
[status.setBufferA.nine] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelA, pBufferAMax(10), ...
221+
totalSamples, dataType, 9, downSampleRatioMode, actionB);
222+
[status.setBufferB.nine] = invoke(ps6000aDeviceObj, 'ps6000aSetDataBuffer', channelB, pBufferBMax(10), ...
223+
totalSamples, dataType, 9, downSampleRatioMode, actionB);
224+
225+
%% Run Block Capture
226+
227+
pTimeIndisposedMs = libpointer('doublePtr',0);
228+
segmentIndex = 0;
229+
230+
disp('Collection starting...')
231+
232+
[status.runBlock] = invoke(ps6000aDeviceObj, 'ps6000aRunBlock', numPreTriggerSamples, numPostTriggerSamples,...
233+
timebase, pTimeIndisposedMs, segmentIndex);
234+
235+
pReady = libpointer('int16Ptr',0);
236+
237+
while pReady.Value == 0
238+
[status.IsReady] = invoke(ps6000aDeviceObj,'ps6000aIsReady',pReady);
239+
end
240+
241+
disp('Collection finished')
242+
243+
%% Retrieve Data
244+
245+
startIndex = 0;
246+
pSamplesCollected = libpointer('uint64Ptr',totalSamples);
247+
downSampleRatio = 1;
248+
segmentIndex = 0;
249+
pOverflow = libpointer('int16Ptr',zeros(10,1));
250+
fromSegmentIndex = 0;
251+
toSegmentIndex = 9;
252+
253+
[status.getValuesBulk] = invoke(ps6000aDeviceObj, 'ps6000aGetValuesBulk', startIndex,...
254+
pSamplesCollected, fromSegmentIndex, toSegmentIndex, downSampleRatio, downSampleRatioMode, pOverflow);
255+
256+
samplesCollected = pSamplesCollected.Value;
257+
258+
disp('Data Retrieved')
259+
260+
%% Convert Data from ADC counts to mV
261+
262+
BufferAMax={};
263+
BufferBMax={};
264+
for i=(1:10)
265+
BufferAMax{i} = pBufferAMax(i).Value;
266+
BufferBMax{i} = pBufferBMax(i).Value;
267+
end
268+
269+
pMinValue = libpointer('int16Ptr',0);
270+
pMaxValue = libpointer('int16Ptr',0);
271+
272+
[status.getAdcLimits] = invoke(ps6000aDeviceObj, 'ps6000aGetAdcLimits', resolution, pMinValue, pMaxValue);
273+
274+
minValue = pMinValue.Value;
275+
maxValue = pMaxValue.Value;
276+
277+
voltageRange = 5000; %mV
278+
279+
bufferAMax={};
280+
bufferBMax={};
281+
282+
for i=(1:10)
283+
bufferAMax{i} = adc2mv(BufferAMax{i},voltageRange,double(maxValue));
284+
bufferBMax{i} = adc2mv(BufferBMax{i},voltageRange,double(maxValue));
285+
end
286+
287+
disp('Data converted to mV')
288+
289+
%% Plot Collected Data
290+
291+
maxTime = (double(samplesCollected) * timeInterval);
292+
time = linspace(0,maxTime,samplesCollected);
293+
294+
figure(1)
295+
%xlim([min(time) max(time)])
296+
%ylim([-voltageRange voltageRange])
297+
hold on
298+
for i=(1:10)
299+
plot(time,bufferAMax{i});
300+
plot(time,bufferBMax{i});
301+
end
302+
ylabel('Voltage (mV)');
303+
xlabel('Time (s)');
304+
hold off
305+
306+
%% Disconnect scope
307+
308+
disconnect(ps6000aDeviceObj);
309+
310+
%%
311+
delete(ps6000aDeviceObj);

0 commit comments

Comments
 (0)