Convert to more generic Pythonic wrapper. Not everything works yet.
[pycomedi.git] / doc / software_timed_analog_IO.txt
1 Import required modules.
2
3 >>> from pycomedi.classes import Device, DataChannel
4 >>> from pycomedi.constants import SUBDEVICE_TYPE, AREF, UNIT
5
6 Open a device.
7
8 >>> device = Device('/dev/comedi0')
9 >>> device.open()
10
11 Get your I/O subdevice (alternatively, use `device.subdevice()` to
12 select the subdevice directly by index).
13
14 >>> subdevice = device.find_subdevice_by_type(SUBDEVICE_TYPE.ai)
15
16 Generate a list of channels you wish to control.
17
18 >>> channels = [subdevice.channel(i, factory=DataChannel, aref=AREF.diff)
19 ...             for i in (0, 1, 2, 3)]
20
21 Configure the channels.
22
23 >>> for chan in channels:
24 ...     chan.range = chan.find_range(unit=UNIT.volt, min=0, max=5)
25
26 Read/write sequentially.
27
28 >>> value = [c.data_read_delayed(nano_sec=1e3) for c in channels]
29 >>> value  # doctest: +SKIP
30 [0, 9634, 0, 15083]
31
32 TODO: convert to physical values.
33
34 Close the device when you're done.
35
36 >>> device.close()
37
38
39
40 As a more elaborate test, we can cable AO0 into AI0 and sweep the
41 voltage.
42
43 >>> from numpy import linspace
44 >>> from scipy.stats import linregress
45 >>> device.open()
46 >>> ai_subdevice = device.find_subdevice_by_type(SUBDEVICE_TYPE.ai)
47 >>> ao_subdevice = device.find_subdevice_by_type(SUBDEVICE_TYPE.ao)
48 >>> ai_channel = ai_subdevice.channel(0, factory=DataChannel, aref=AREF.diff)
49 >>> ao_channel = ao_subdevice.channel(0, factory=DataChannel, aref=AREF.diff)
50 >>> ai_channel.range = ai_channel.find_range(unit=UNIT.volt, min=0, max=5)
51 >>> ao_channel.range = ao_channel.find_range(unit=UNIT.volt, min=0, max=5)
52 >>> ao_maxdata = ao_channel.get_maxdata()
53 >>> ai_maxdata = ai_channel.get_maxdata()
54 >>> ao_start = 0.3 * ao_maxdata
55 >>> ao_stop = 0.7 * ao_maxdata
56 >>> points = 10
57 >>> ao_data = linspace(ao_start, ao_stop, points)
58 >>> ai_data = []
59 >>> for i in range(points):
60 ...     written = ao_channel.data_write(ao_data[i])
61 ...     assert written == 1, written
62 ...     ai_data.append(ai_channel.data_read_delayed(nano_sec=1e3))
63 >>> ao_data
64 >>> ai_data
65 >>> scaled_ao_data = [d/float(ao_maxdata) for d in ao_data]
66 >>> scaled_ai_data = [d/float(ai_maxdata) for d in ai_data]
67 >>> gradient,intercept,r_value,p_value,std_err = linregress(
68 ...     scaled_ao_data, scaled_ai_data)
69 >>> gradient
70 >>> intercept
71 >>> r_value
72 >>> p_value
73 >>> std_err
74 >>> device.close()