Difference between revisions of "Python:DAQ 2"
Line 13: | Line 13: | ||
* None yet! | * None yet! | ||
− | == | + | == Circuits == |
− | + | * aio_output | |
− | + | * aio_meas1 | |
− | + | * aio_meas3 and aio_get_data | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
== Graph from aio_meas1 == | == Graph from aio_meas1 == | ||
− | Graph showing | + | Graph showing single measurement when the overall voltage is calculated with: |
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100) | v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100) | ||
Line 39: | Line 32: | ||
</center> | </center> | ||
<center> | <center> | ||
− | [[File:Meas1 plot.png| | + | [[File:Meas1 plot.png|300px]] |
</center> | </center> | ||
− | == | + | == Graph from aio_meas3 == |
− | + | Graph showing all three measurements when the overall voltage is calculated with: | |
+ | <syntaxhighlight lang="python"> | ||
+ | v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100) | ||
+ | </syntaxhighlight> | ||
<center> | <center> | ||
− | [[ | + | [[File:Meas3 plot.png|300px]] |
</center> | </center> | ||
− | == Graph from | + | |
− | Graph showing | + | == Graph from aio_get_data == |
− | Note at the far left that they all start at either exactly 0 V! | + | Graph showing all three measurements when the overall voltage is calculated with: |
+ | <syntaxhighlight lang="python"> | ||
+ | v_out = 5 * (k / (N - 1)) | ||
+ | </syntaxhighlight> | ||
+ | assuming <code>n = 300</code>. Note at the far left that they all start at either exactly 0 V! | ||
<center> | <center> | ||
− | [[ | + | [[File:Get data plot.png|300 px]]<br> |
</center> | </center> | ||
== Codes == | == Codes == | ||
− | Here are the codes references in the assignment. | + | Here are the codes references in the assignment. Note that the extra blank spaces are in earlier programs to make room for the code added to later programs. For the second and later codes, the lines that were added or changed from the script listed immediately above it will be indicated with yellow highlighting. |
− | == | + | <div class="toccolours mw-collapsible mw-collapsed"> |
+ | <div style="font-weight:bold;line-height:1.6;">aio_output</div> | ||
+ | <div class="mw-collapsible-content"> | ||
<syntaxhighlight line lang="python"> | <syntaxhighlight line lang="python"> | ||
− | #%% Import modules | + | # %% Import modules |
import numpy as np | import numpy as np | ||
import time | import time | ||
− | import | + | import nidaqmx as daq |
− | + | # %% Create a task | |
− | #%% Create a task | ||
taskout = daq.Task() | taskout = daq.Task() | ||
− | #%% | + | # %% Add analog output lines |
− | + | taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0") | |
− | |||
− | |||
− | |||
+ | # %% Check lights | ||
+ | taskout.write(5) | ||
+ | input("PAUSED - Hit return to continue ") | ||
+ | taskout.write(0) | ||
+ | # %% Write values to output | ||
− | + | for k in range(300): | |
− | + | v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100) | |
− | + | taskout.write(v_out) | |
− | taskout. | + | time.sleep(0.01) |
+ | # %% Turn all off when finished and close task | ||
+ | taskout.write(0) | ||
+ | taskout.close() | ||
+ | </syntaxhighlight> | ||
+ | </div></div> | ||
+ | <div class="toccolours mw-collapsible mw-collapsed"> | ||
+ | <div style="font-weight:bold;line-height:1.6;">aio_meas1</div> | ||
+ | <div class="mw-collapsible-content"> | ||
+ | <syntaxhighlight line lang="python" highlight=5,9,14,15,23,24,30,35,37-40> | ||
+ | # %% Import modules | ||
+ | import numpy as np | ||
+ | import time | ||
+ | import nidaqmx as daq | ||
+ | import matplotlib.pyplot as plt | ||
+ | # %% Create a task | ||
+ | taskout = daq.Task() | ||
+ | taskin = daq.Task() | ||
+ | # %% Add analog output lines | ||
+ | taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0") | ||
+ | # %% Add analog input lines | ||
+ | taskin.ai_channels.add_ai_voltage_chan("Dev1/ai0") | ||
+ | # %% Check lights | ||
+ | taskout.write(5) | ||
+ | input("PAUSED - Hit return to continue ") | ||
+ | taskout.write(0) | ||
+ | # %% Write and read values | ||
+ | N = 300 | ||
+ | meas = np.zeros((N, 1)) | ||
− | + | for k in range(N): | |
− | + | v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100) | |
− | taskout. | + | taskout.write(v_out) |
− | + | time.sleep(0.01) | |
− | + | meas[k] = taskin.read() | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | #%% | + | # %% Turn all off when finished and close task |
+ | taskout.write(0) | ||
+ | taskout.close() | ||
+ | taskin.close() | ||
− | + | # %% Make plots | |
− | + | fig, ax = plt.subplots(num=1, clear=True) | |
− | + | total = meas[:, 0] | |
− | + | ax.plot(total, "r") | |
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | </div></div> | ||
− | == | + | <div class="toccolours mw-collapsible mw-collapsed"> |
− | <syntaxhighlight line lang="python"> | + | <div style="font-weight:bold;line-height:1.6;">aio_meas3</div> |
− | #%% Import modules | + | <div class="mw-collapsible-content"> |
+ | <syntaxhighlight line lang="python" highlight=15,24,30,40-43> | ||
+ | # %% Import modules | ||
import numpy as np | import numpy as np | ||
import time | import time | ||
− | import | + | import nidaqmx as daq |
− | |||
import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
− | #%% Create a task | + | # %% Create a task |
taskout = daq.Task() | taskout = daq.Task() | ||
− | taskin | + | taskin = daq.Task() |
− | + | # %% Add analog output lines | |
− | + | taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0") | |
− | |||
− | |||
− | #%% Add analog output lines | ||
− | taskout. | ||
− | |||
− | #%% Add analog input lines | + | # %% Add analog input lines |
− | taskin. | + | taskin.ai_channels.add_ai_voltage_chan("Dev1/ai0:2") |
− | |||
− | |||
− | #%% | + | # %% Check lights |
− | + | taskout.write(5) | |
− | + | input("PAUSED - Hit return to continue ") | |
− | + | taskout.write(0) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | #%% | + | # %% Write and read values |
− | + | N = 300 | |
− | + | meas = np.zeros((N, 3)) | |
− | |||
− | |||
− | + | for k in range(N): | |
− | + | v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100) | |
− | + | taskout.write(v_out) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | for k in range( | ||
− | v_out = 2.5+2.5*np.sin(2*np.pi*k/100) | ||
− | |||
time.sleep(0.01) | time.sleep(0.01) | ||
− | meas[k,:] = | + | meas[k, :] = taskin.read() |
− | #%% Turn all off when finished and | + | # %% Turn all off when finished and close task |
− | + | taskout.write(0) | |
− | taskout. | + | taskout.close() |
− | taskin. | + | taskin.close() |
− | #%% Make plots | + | # %% Make plots |
− | plt. | + | fig, ax = plt.subplots(num=1, clear=True) |
− | + | total = meas[:, 0] | |
− | total = meas[:,0] | + | resv = meas[:, 1] |
− | + | ledv = meas[:, 2] | |
− | + | ax.plot(total, "m-", resv, "b-.", ledv, "g:") | |
− | + | ax.legend(["$v_s$", "$v_R$", "$v_{LED}$"]) | |
</syntaxhighlight> | </syntaxhighlight> | ||
+ | </div></div> | ||
− | === | + | <div class="toccolours mw-collapsible mw-collapsed"> |
− | <syntaxhighlight line lang="python"> | + | <div style="font-weight:bold;line-height:1.6;">aio_get_data</div> |
− | #%% Import modules | + | <div class="mw-collapsible-content"> |
+ | <syntaxhighlight line lang="python" highlight=27,45-52> | ||
+ | # %% Import modules | ||
import numpy as np | import numpy as np | ||
import time | import time | ||
− | import | + | import nidaqmx as daq |
− | |||
import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
− | #%% Create a task | + | # %% Create a task |
taskout = daq.Task() | taskout = daq.Task() | ||
− | taskin | + | taskin = daq.Task() |
− | + | # %% Add analog output lines | |
− | + | taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0") | |
− | |||
− | |||
− | #%% Add analog output lines | ||
− | taskout. | ||
− | |||
− | #%% Add analog input lines | + | # %% Add analog input lines |
− | taskin. | + | taskin.ai_channels.add_ai_voltage_chan("Dev1/ai0:2") |
− | |||
− | |||
− | #%% | + | # %% Check lights |
− | + | taskout.write(5) | |
− | + | input("PAUSED - Hit return to continue ") | |
− | + | taskout.write(0) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | #%% | + | # %% Write and read values |
− | + | N = 300 | |
− | + | meas = np.zeros((N, 3)) | |
− | |||
− | |||
− | + | for k in range(N): | |
− | + | v_out = 5 * (k / (N - 1)) | |
− | + | taskout.write(v_out) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | for k in range( | ||
− | v_out = (k/ | ||
− | |||
time.sleep(0.01) | time.sleep(0.01) | ||
− | meas[k,:] = | + | meas[k, :] = taskin.read() |
− | #%% Turn all off when finished and | + | # %% Turn all off when finished and close task |
− | + | taskout.write(0) | |
− | taskout. | + | taskout.close() |
− | taskin. | + | taskin.close() |
− | #%% Make plots | + | # %% Make plots |
− | plt. | + | fig, ax = plt.subplots(num=1, clear=True) |
− | + | total = meas[:, 0] | |
− | total = meas[:,0] | + | resv = meas[:, 1] |
− | resv = meas[:,1] | + | ledv = meas[:, 2] |
− | ledv = meas[:,2] | + | ax.plot(total, "m-", resv, "b-.", ledv, "g--") |
− | + | ax.legend(["$v_s$", "$v_R$", "$v_{LED}$"]) | |
− | |||
− | #%% Save values and figure | + | # %% Save values and figure |
− | color = input( | + | color = input("Color: ") |
− | eval(" | + | eval("ax.set_title('{:s}')".format(color)) |
− | fid=eval("open('{:s}_data.dat', 'w')".format(color)) | + | fid = eval("open('{:s}_data.dat', 'w')".format(color)) |
for k in range(300): | for k in range(300): | ||
− | fid.write( | + | fid.write("{:.4e} {:.4e} {:.4e}\n".format(*meas[k, :])) |
fid.close() | fid.close() | ||
− | eval(" | + | eval("fig.savefig('{:s}_plot.png')".format(color)) |
+ | |||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | </div></div> | ||
+ | |||
== Questions == | == Questions == |
Revision as of 19:09, 7 October 2019
This page contains pictures and graphs related to DAQ 2 for EGR 103. It has been updated for Fall 2019 and nidaqmx
.
Contents
Notes
- As before, if the system seems to not recognize Dev1, try Dev2 instead.
Pauses
There are intput
commands in the code which will cause the program to wait for an input - specifically when the program first runs to check the lights. You will need to hit return to un-pause the program. The way to see if the program is paused is to look in the console to see if it is waiting for an input.
Resource Reserved / Task Cannot Start
If you get an error that a resources is reserved or that a task can't start, clear all the variables and re-run the script. Clearing the variables should clear all reservations to the DAQ card.
Typographical Errors
- None yet!
Circuits
- aio_output
- aio_meas1
- aio_meas3 and aio_get_data
Graph from aio_meas1
Graph showing single measurement when the overall voltage is calculated with:
v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100)
That is,
\( \begin{align} V_{out}=2.5+2.5\sin\left(\frac{2\pi k}{100}\right) \end{align} \)
Graph from aio_meas3
Graph showing all three measurements when the overall voltage is calculated with:
v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100)
Graph from aio_get_data
Graph showing all three measurements when the overall voltage is calculated with:
v_out = 5 * (k / (N - 1))
assuming n = 300
. Note at the far left that they all start at either exactly 0 V!
Codes
Here are the codes references in the assignment. Note that the extra blank spaces are in earlier programs to make room for the code added to later programs. For the second and later codes, the lines that were added or changed from the script listed immediately above it will be indicated with yellow highlighting.
1 # %% Import modules
2 import numpy as np
3 import time
4 import nidaqmx as daq
5
6
7 # %% Create a task
8 taskout = daq.Task()
9
10
11 # %% Add analog output lines
12 taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0")
13
14
15
16
17 # %% Check lights
18 taskout.write(5)
19 input("PAUSED - Hit return to continue ")
20 taskout.write(0)
21
22 # %% Write values to output
23
24
25
26 for k in range(300):
27 v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100)
28 taskout.write(v_out)
29 time.sleep(0.01)
30
31
32 # %% Turn all off when finished and close task
33 taskout.write(0)
34 taskout.close()
1 # %% Import modules
2 import numpy as np
3 import time
4 import nidaqmx as daq
5 import matplotlib.pyplot as plt
6
7 # %% Create a task
8 taskout = daq.Task()
9 taskin = daq.Task()
10
11 # %% Add analog output lines
12 taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0")
13
14 # %% Add analog input lines
15 taskin.ai_channels.add_ai_voltage_chan("Dev1/ai0")
16
17 # %% Check lights
18 taskout.write(5)
19 input("PAUSED - Hit return to continue ")
20 taskout.write(0)
21
22 # %% Write and read values
23 N = 300
24 meas = np.zeros((N, 1))
25
26 for k in range(N):
27 v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100)
28 taskout.write(v_out)
29 time.sleep(0.01)
30 meas[k] = taskin.read()
31
32 # %% Turn all off when finished and close task
33 taskout.write(0)
34 taskout.close()
35 taskin.close()
36
37 # %% Make plots
38 fig, ax = plt.subplots(num=1, clear=True)
39 total = meas[:, 0]
40 ax.plot(total, "r")
1 # %% Import modules
2 import numpy as np
3 import time
4 import nidaqmx as daq
5 import matplotlib.pyplot as plt
6
7 # %% Create a task
8 taskout = daq.Task()
9 taskin = daq.Task()
10
11 # %% Add analog output lines
12 taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0")
13
14 # %% Add analog input lines
15 taskin.ai_channels.add_ai_voltage_chan("Dev1/ai0:2")
16
17 # %% Check lights
18 taskout.write(5)
19 input("PAUSED - Hit return to continue ")
20 taskout.write(0)
21
22 # %% Write and read values
23 N = 300
24 meas = np.zeros((N, 3))
25
26 for k in range(N):
27 v_out = 2.5 + 2.5 * np.sin(2 * np.pi * k / 100)
28 taskout.write(v_out)
29 time.sleep(0.01)
30 meas[k, :] = taskin.read()
31
32 # %% Turn all off when finished and close task
33 taskout.write(0)
34 taskout.close()
35 taskin.close()
36
37 # %% Make plots
38 fig, ax = plt.subplots(num=1, clear=True)
39 total = meas[:, 0]
40 resv = meas[:, 1]
41 ledv = meas[:, 2]
42 ax.plot(total, "m-", resv, "b-.", ledv, "g:")
43 ax.legend(["$v_s$", "$v_R$", "$v_{LED}$"])
1 # %% Import modules
2 import numpy as np
3 import time
4 import nidaqmx as daq
5 import matplotlib.pyplot as plt
6
7 # %% Create a task
8 taskout = daq.Task()
9 taskin = daq.Task()
10
11 # %% Add analog output lines
12 taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0")
13
14 # %% Add analog input lines
15 taskin.ai_channels.add_ai_voltage_chan("Dev1/ai0:2")
16
17 # %% Check lights
18 taskout.write(5)
19 input("PAUSED - Hit return to continue ")
20 taskout.write(0)
21
22 # %% Write and read values
23 N = 300
24 meas = np.zeros((N, 3))
25
26 for k in range(N):
27 v_out = 5 * (k / (N - 1))
28 taskout.write(v_out)
29 time.sleep(0.01)
30 meas[k, :] = taskin.read()
31
32 # %% Turn all off when finished and close task
33 taskout.write(0)
34 taskout.close()
35 taskin.close()
36
37 # %% Make plots
38 fig, ax = plt.subplots(num=1, clear=True)
39 total = meas[:, 0]
40 resv = meas[:, 1]
41 ledv = meas[:, 2]
42 ax.plot(total, "m-", resv, "b-.", ledv, "g--")
43 ax.legend(["$v_s$", "$v_R$", "$v_{LED}$"])
44
45 # %% Save values and figure
46 color = input("Color: ")
47 eval("ax.set_title('{:s}')".format(color))
48 fid = eval("open('{:s}_data.dat', 'w')".format(color))
49 for k in range(300):
50 fid.write("{:.4e} {:.4e} {:.4e}\n".format(*meas[k, :]))
51 fid.close()
52 eval("fig.savefig('{:s}_plot.png')".format(color))
Questions
Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.