1 Import required modules.
3 >>> from numpy import arange, linspace, zeros, sin, pi, float32
4 >>> from pycomedi.device import Device
5 >>> from pycomedi.subdevice import StreamingSubdevice
6 >>> from pycomedi.channel import AnalogChannel
7 >>> from pycomedi.chanspec import ChanSpec
8 >>> from pycomedi.constant import (AREF, CMDF, INSN, SUBDEVICE_TYPE, TRIG_SRC,
10 >>> from pycomedi.utility import inttrig_insn, Reader, Writer
14 >>> device = Device('/dev/comedi0')
17 Get your I/O subdevices (alternatively, use `device.subdevice()` to
18 select the subdevice directly by index).
20 >>> ai_subdevice = device.find_subdevice_by_type(SUBDEVICE_TYPE.ai,
21 ... factory=StreamingSubdevice)
22 >>> ao_subdevice = device.find_subdevice_by_type(SUBDEVICE_TYPE.ao,
23 ... factory=StreamingSubdevice)
25 Generate a list of channels you wish to control.
28 ... ai_subdevice.channel(i, factory=AnalogChannel, aref=AREF.diff)
29 ... for i in (0, 1, 2, 3)]
31 ... ao_subdevice.channel(i, factory=AnalogChannel, aref=AREF.diff)
34 Configure the channels.
36 >>> for chan in ai_channels + ao_channels:
37 ... chan.range = chan.find_range(unit=UNIT.volt, min=-10, max=10)
39 Use the subdevice flags to determine data types.
41 >>> ai_dtype = ai_subdevice.get_dtype()
42 >>> ao_dtype = ao_subdevice.get_dtype()
44 Allocate buffers (with channel data interleaved).
47 >>> ai_buffer = zeros((n_samps, len(ai_channels)), dtype=ai_dtype)
48 >>> ao_buffer = zeros((n_samps, len(ao_channels)), dtype=ao_dtype)
50 Initialize output buffer.
52 >>> midpoint = int(ao_channels[0].get_maxdata()/2)
53 >>> bitrange = float(midpoint/2)
54 >>> ao_buffer[:,0] = midpoint+bitrange*sin(2*pi*arange(n_samps)/(n_samps-1.0))
55 >>> ao_buffer[:,1] = linspace(0, midpoint, n_samps)
56 >>> ao_buffer[-1,:] = midpoint
60 >>> frequency = 1000.0 # Hz
61 >>> scan_period_ns = int(1e9 / frequency) # nanosecond period
62 >>> ai_cmd = ai_subdevice.get_cmd_generic_timed(
63 ... len(ai_channels), scan_period_ns)
64 >>> ao_cmd = ao_subdevice.get_cmd_generic_timed(
65 ... len(ao_channels), scan_period_ns)
67 Setup multi-scan run triggering.
69 >>> ai_cmd.start_src = TRIG_SRC.int
70 >>> ai_cmd.start_arg = 0
71 >>> ai_cmd.stop_src = TRIG_SRC.count
72 >>> ai_cmd.stop_arg = n_samps
73 >>> ao_cmd.start_src = TRIG_SRC.ext
74 >>> ao_cmd.start_arg = 18 # NI card AI_START1 internal AI start signal
75 >>> ao_cmd.stop_src = TRIG_SRC.count
76 >>> ao_cmd.stop_arg = n_samps
80 >>> ai_cmd.chanlist = ai_channels
81 >>> ao_cmd.chanlist = ao_channels
85 >>> ai_subdevice.cmd = ai_cmd
86 >>> ao_subdevice.cmd = ao_cmd
87 >>> for i in range(3):
88 ... rc = ai_subdevice.command_test()
89 ... if rc == None: break
91 >>> for i in range(3):
92 ... rc = ao_subdevice.command_test()
93 ... if rc == None: break
98 >>> ao_subdevice.command()
99 >>> ai_subdevice.command()
101 Start the reader and writer threads.
103 >>> writer = Writer(ao_subdevice, ao_buffer)
105 >>> reader = Reader(ai_subdevice, ai_buffer)
110 >>> device.do_insn(inttrig_insn(ao_subdevice))
113 Trigger AI aquisition, which in turn triggers the AO.
115 >>> device.do_insn(inttrig_insn(ai_subdevice))
118 Join the reader and writer threads when they finish.
123 >>> ai_buffer # doctest: +SKIP
124 array([[32669, 27117, 24827, 23111],
125 [33711, 27680, 25273, 23453],
127 [31627, 24590, 22482, 22045],
128 [32668, 25381, 22937, 22189]], dtype=uint16)
130 array([[32342, 31572, 30745, 31926],
131 [33376, 31797, 30904, 31761],
133 [31308, 24246, 22215, 21824],
134 [32343, 25044, 22659, 21959]], dtype=uint16)
136 Use a converter to convert these to physical values
138 >>> converters = [c.get_converter() for c in ai_channels]
139 >>> ai_physical = zeros(ai_buffer.shape, dtype=float32)
140 >>> for i,c in enumerate(converters):
141 ... ai_physical[:,i] = c.to_physical(ai_buffer[:,i])
142 >>> ai_physical # doctest: +SKIP
143 array([[ -3.00602727e-02, -1.72442210e+00, -2.42328525e+00,
145 [ 2.87937731e-01, -1.55260551e+00, -2.28717470e+00,
148 [ -3.48058283e-01, -2.49561310e+00, -3.13893342e+00,
150 [ -3.03654540e-02, -2.25421524e+00, -3.00007629e+00,
151 -3.22835135e+00]], dtype=float32)
152 >>> [c.range.unit for c in ai_channels]
153 [<_NamedInt volt>, <_NamedInt volt>, <_NamedInt volt>, <_NamedInt volt>]
155 Close the device when you're done.