Skip to content

Commit f0c6b90

Browse files
author
neil.hamilton
committed
Add additional lecture note companion scripts
1 parent 6d40639 commit f0c6b90

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

ps2000Examples/block_read.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from ctypes import byref, c_byte, c_int16, c_int32, sizeof
2+
from time import sleep
3+
4+
from picosdk.ps2000 import ps2000
5+
from picosdk.functions import assert_pico2000_ok, adc2mV
6+
from picosdk.PicoDeviceEnums import picoEnum
7+
8+
import matplotlib.pyplot as plt
9+
10+
11+
SAMPLES = 2000
12+
OVERSAMPLING = 1
13+
14+
15+
def get_timebase(device, wanted_time_interval):
16+
current_timebase = 1
17+
18+
old_time_interval = None
19+
time_interval = c_int32(0)
20+
time_units = c_int16()
21+
max_samples = c_int32()
22+
23+
while ps2000.ps2000_get_timebase(
24+
device.handle,
25+
current_timebase,
26+
2000,
27+
byref(time_interval),
28+
byref(time_units),
29+
1,
30+
byref(max_samples)) == 0 \
31+
or time_interval.value < wanted_time_interval:
32+
33+
current_timebase += 1
34+
old_time_interval = time_interval.value
35+
36+
if current_timebase.bit_length() > sizeof(c_int16) * 8:
37+
raise Exception('No appropriate timebase was identifiable')
38+
39+
return current_timebase - 1, old_time_interval
40+
41+
42+
with ps2000.open_unit() as device:
43+
print('Device info: {}'.format(device.info))
44+
45+
res = ps2000.ps2000_set_channel(
46+
device.handle,
47+
picoEnum.PICO_CHANNEL['PICO_CHANNEL_A'],
48+
True,
49+
picoEnum.PICO_COUPLING['PICO_DC'],
50+
ps2000.PS2000_VOLTAGE_RANGE['PS2000_500MV'],
51+
)
52+
assert_pico2000_ok(res)
53+
54+
res = ps2000.ps2000_set_channel(
55+
device.handle,
56+
picoEnum.PICO_CHANNEL['PICO_CHANNEL_B'],
57+
True,
58+
picoEnum.PICO_COUPLING['PICO_DC'],
59+
ps2000.PS2000_VOLTAGE_RANGE['PS2000_50MV'],
60+
)
61+
assert_pico2000_ok(res)
62+
63+
timebase_a, interval = get_timebase(device, 4_000)
64+
65+
collection_time = c_int32()
66+
67+
res = ps2000.ps2000_run_block(
68+
device.handle,
69+
SAMPLES,
70+
timebase_a,
71+
OVERSAMPLING,
72+
byref(collection_time)
73+
)
74+
assert_pico2000_ok(res)
75+
76+
while ps2000.ps2000_ready(device.handle) == 0:
77+
sleep(0.1)
78+
79+
times = (c_int32 * SAMPLES)()
80+
81+
buffer_a = (c_int16 * SAMPLES)()
82+
buffer_b = (c_int16 * SAMPLES)()
83+
84+
overflow = c_byte(0)
85+
86+
res = ps2000.ps2000_get_times_and_values(
87+
device.handle,
88+
byref(times),
89+
byref(buffer_a),
90+
byref(buffer_b),
91+
None,
92+
None,
93+
byref(overflow),
94+
2,
95+
SAMPLES,
96+
)
97+
assert_pico2000_ok(res)
98+
99+
channel_a_overflow = (overflow.value & 0b0000_0001) != 0
100+
101+
ps2000.ps2000_stop(device.handle)
102+
103+
channel_a_mv = adc2mV(buffer_a, ps2000.PS2000_VOLTAGE_RANGE['PS2000_500MV'], c_int16(32767))
104+
channel_b_mv = adc2mV(buffer_b, ps2000.PS2000_VOLTAGE_RANGE['PS2000_50MV'], c_int16(32767))
105+
106+
fig, ax = plt.subplots()
107+
ax.set_xlabel('time/ms')
108+
ax.set_ylabel('voltage/mV')
109+
ax.plot(list(map(lambda x: x * 1e-6, times[:])), channel_a_mv[:])
110+
ax.plot(list(map(lambda x: x * 1e-6, times[:])), channel_b_mv[:])
111+
112+
if channel_a_overflow:
113+
ax.text(0.01, 0.01, 'Overflow present', color='red', transform=ax.transAxes)
114+
115+
plt.show()

ps2000Examples/device_info.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from picosdk.ps2000 import ps2000
2+
3+
with ps2000.open_unit() as device:
4+
print('Device info: {}'.format(device.info))

0 commit comments

Comments
 (0)