Difference between revisions of "Python:DAQ 2"

From PrattWiki
Jump to navigation Jump to search
 
(Notes)
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
This page contains pictures and graphs related to DAQ 2 for [[EGR 103]].  It has been updated for Fall 2018 and Python.
+
This page contains pictures and graphs related to DAQ 2 for [[EGR 103]].  It has been updated for Fall 2019 and <code>nidaqmx</code>.
  
==Notes ==
+
== Notes ==
 
* As before, if the system seems to not recognize Dev1, try Dev2 instead.
 
* As before, if the system seems to not recognize Dev1, try Dev2 instead.
 +
 +
=== Channel 0 Failure ===
 +
A few boards seemed to have a failed Channel 0; you can still do the lab with the following modifications:
 +
==== Hardware ====
 +
The measurement wires will need to be changed as follows:
 +
$$\begin{array}{cccc}
 +
Purple & Pin B7 & Line 33 & ACH1\\
 +
Yellow & Pin Y5 & Line 66 & ACH9\\ 
 +
Blue & Pin C7 & Line 65 & ACH2\\
 +
Orange & Pin H7 & Line 31 & ACH10\\
 +
Green & Pin I7 & Line 30 & ACH3\\
 +
Brown & Pin Y3 & Line 63 & ACH11
 +
\end{array}$$
 +
==== Software ====
 +
For a single measurement, change the end of line 22 to from <code>ai0</code> to <code>ai1</code>; for multiple measurements, change the end of line 22 from <code>ai0:2</code> to <code>ai1:3</code>
  
 
=== Pauses ===
 
=== Pauses ===
There are <code>intput</code> 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.
+
There are <code>input</code> 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.  If that does not work, try restarting the kernal.
  
== Circuit for basic_aoutput ==
+
=== Typographical Errors ===
Circuit layout for BasicAOutput - '''You only need the left-most circuit, however'''
+
* Before section 2.5, you may need to install nidaqmx on the DAQ computer.  To do that:
<center>
+
** Go to the Start menu and find the Anaconda3 folder
[[Image:Circuit1.jpg|400px]]
+
** Start the Anaconda Prompt
</center>
+
** In the Anaconda Prompt, type
 +
:: <code>pip install nidaqmx</code>
 +
:* If it asks you if that is OK - it is
  
== Circuit for basic_aio ==
+
== Circuits ==
Circuit layout for BasicAIO - '''You only need the left-most circuit, however'''
+
* aio_output
<center>
+
* aio_meas1
[[Image:Circuit2.jpg|400px]]
+
* aio_meas3 and aio_get_data
</center>
 
  
== Graph from basic_aio ==
+
== Graph from aio_meas1 ==
Graph showing output when
+
Graph showing single measurement when the overall voltage is calculated with:
<syntaxhighlight line lang="python">
+
<syntaxhighlight lang="python">
v_out = 2.5+2.5*npsin(2*np.pi*k/100);
+
    v_out = 2.5 + 2.5 * np.sin(6 * np.pi * k / N)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
That is,
 
That is,
Line 29: Line 47:
 
<math>
 
<math>
 
\begin{align}
 
\begin{align}
V_{out}=2.5+2.5\sin\left(\frac{2\pi k}{100}\right)
+
V_{out}=2.5+2.5\sin\left(\frac{6\pi k}{N}\right)
 
\end{align}
 
\end{align}
 
</math>
 
</math>
 
</center>
 
</center>
 
<center>
 
<center>
[[Image:Circuit2PlotX.png|500px]]
+
[[File:Meas1 plot.png|300px]]
 
</center>
 
</center>
  
== Circuit for aio ==
+
== Graph from aio_meas3 ==
Circuit layout for aio - '''You only need the left-most circuit, however'''
+
Graph showing all three measurements when the overall voltage is calculated with:
 +
<syntaxhighlight lang="python">
 +
    v_out = 2.5 + 2.5 * np.sin(6 * np.pi * k / N)
 +
</syntaxhighlight>
 
<center>
 
<center>
[[Image:Circuit3.jpg|400px]]
+
[[File:Meas3 plot.png|300px]]
 
</center>
 
</center>
  
== Graph from aio ==
+
== Graph from aio_get_data ==
Graph showing outputs from three measurement channels.   
+
Graph showing all three measurements when the overall voltage is calculated with:
Note at the far left that they all start at either exactly -1 V or 0 V!
+
<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>
[[Image:VoltagesFigureRed.png|300 px]]<br>
+
[[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.
  
=== basic_aoutput.py===
+
<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 PyDAQmx as daq
+
import nidaqmx as daq
 
 
  
  
#%% Create a task
+
# %% Create a task
 
taskout = daq.Task()
 
taskout = daq.Task()
  
  
#%% Set Channel Counts
+
# %% Add analog output lines
a_out  = 1
+
taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0")
  
  
#%% Add analog output lines
 
taskout.CreateAOVoltageChan('Dev1/ao0:{}'.format(a_out-1),'',
 
                        -10.0,10.0,daq.DAQmx_Val_Volts,None)
 
  
  
 +
# %% Check lights
 +
taskout.write(5)
 +
input("PAUSED - Hit return to continue ")
 +
taskout.write(0)
  
 +
# %% Write values to output
 +
N = 300
  
 +
for k in range(300):
 +
    v_out = 2.5 + 2.5 * np.sin(6 * np.pi * k / N)
 +
    taskout.write(v_out)
 +
    time.sleep(0.01)
  
  
#%% Define useful functions
+
# %% Turn all off when finished and close task
# Function to change daq outputs
+
taskout.write(0)
def write_volts(data):
+
taskout.close()
    taskout.WriteAnalogScalarF64(1,10.0,data,None)
 
  
 +
</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,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(6 * np.pi * k / N)
 +
    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()
  
#%% Stop and start tasks
+
# %% Make plots
taskout.StopTask()
+
fig, ax = plt.subplots(num=1, clear=True)
taskout.StartTask()
+
total = meas[:, 0]
 +
ax.plot(total, "r")
  
 
 
#%% Check lights
 
write_volts(5)
 
input('PAUSED - Hit return to continue ')
 
print('Running')
 
write_volts(0)
 
 
#%% Write values to output
 
 
for k in range(300):
 
    v_out = 2.5+2.5*np.sin(2*np.pi*k/100)
 
    write_volts(v_out)
 
    time.sleep(0.01)
 
 
 
#%% Turn all off when finished and stop task
 
write_volts(0)
 
taskout.StopTask()
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</div></div>
  
=== basic_aio.py ===
+
<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 PyDAQmx as daq
+
import nidaqmx as daq
import ctypes
 
 
import matplotlib.pyplot as plt
 
import matplotlib.pyplot as plt
  
#%% Create a task
+
# %% Create a task
 
taskout = daq.Task()
 
taskout = daq.Task()
taskin = daq.Task()
+
taskin = daq.Task()
  
#%% Set Channel Counts
+
# %% Add analog output lines
a_out = 1
+
taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0")
a_in  = 1
 
 
#%% Add analog output lines
 
taskout.CreateAOVoltageChan('Dev1/ao0:{}'.format(a_out-1),'',
 
                        -10.0,10.0,daq.DAQmx_Val_Volts,None)
 
  
#%% Add analog input lines
+
# %% Add analog input lines
taskin.CreateAIVoltageChan('Dev1/ai0:{}'.format(a_in-1),'',
+
taskin.ai_channels.add_ai_voltage_chan("Dev1/ai0:2")
                          daq.DAQmx_Val_Cfg_Default,
 
                        -10.0,10.0,daq.DAQmx_Val_Volts,None)
 
  
#%% Define useful functions
+
# %% Check lights
# Function to change daq outputs
+
taskout.write(5)
def write_volts(data):
+
input("PAUSED - Hit return to continue ")
    taskout.WriteAnalogScalarF64(1,10.0,data,None)
+
taskout.write(0)
   
 
# Function to read daq outputs
 
def read_volts():
 
    data = np.zeros((1,a_in), dtype=np.float64)
 
    read = ctypes.c_int32()
 
    taskin.ReadAnalogF64(1,10.0,
 
                        daq.DAQmx_Val_GroupByChannel,
 
                        data, a_in, ctypes.byref(read), None)
 
    return data
 
  
#%% Stop and start tasks
+
# %% Write and read values
taskout.StopTask()
+
N = 300
taskin.StopTask()
+
meas = np.zeros((N, 3))
taskout.StartTask()
 
taskin.StartTask()
 
  
#%% Check lights
+
for k in range(N):
write_volts(5)
+
     v_out = 2.5 + 2.5 * np.sin(6 * np.pi * k / N)
input('PAUSED - Hit return to continue ')
+
     taskout.write(v_out)
print('Running')
 
write_volts(0)
 
 
 
#%% Write and read values
 
meas = np.zeros((300,3), dtype=np.float64)
 
for k in range(300):
 
     v_out = 2.5+2.5*np.sin(2*np.pi*k/100)
 
     write_volts(v_out)
 
 
     time.sleep(0.01)
 
     time.sleep(0.01)
     meas[k,:] = read_volts()
+
     meas[k, :] = taskin.read()
 
 
#%% Turn all off when finished and stop task
 
write_volts(0)
 
taskout.StopTask()
 
taskin.StopTask()
 
 
 
#%% Make plots
 
plt.figure(1)
 
plt.clf()
 
total = meas[:,0]
 
  
 +
# %% Turn all off when finished and close task
 +
taskout.write(0)
 +
taskout.close()
 +
taskin.close()
  
plt.plot(total, 'b')
+
# %% Make plots
 +
fig, ax = plt.subplots(num=1, clear=True)
 +
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>
  
=== aio script ===
+
<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 PyDAQmx as daq
+
import nidaqmx as daq
import ctypes
 
 
import matplotlib.pyplot as plt
 
import matplotlib.pyplot as plt
  
#%% Create a task
+
# %% Create a task
 
taskout = daq.Task()
 
taskout = daq.Task()
taskin = daq.Task()
+
taskin = daq.Task()
 
 
#%% Set Channel Counts
 
a_in  = 3
 
a_out = 1
 
 
#%% Add analog output lines
 
taskout.CreateAOVoltageChan('Dev1/ao0:{}'.format(a_out-1),'',
 
                        -10.0,10.0,daq.DAQmx_Val_Volts,None)
 
  
#%% Add analog input lines
+
# %% Add analog output lines
taskin.CreateAIVoltageChan('Dev1/ai0:{}'.format(a_in-1),'',
+
taskout.ao_channels.add_ao_voltage_chan("Dev1/ao0")
                          daq.DAQmx_Val_Cfg_Default,
 
                        -10.0,10.0,daq.DAQmx_Val_Volts,None)
 
  
#%% Define useful functions
+
# %% Add analog input lines
# Function to change daq outputs
+
taskin.ai_channels.add_ai_voltage_chan("Dev1/ai0:2")
def write_volts(data):
 
    taskout.WriteAnalogScalarF64(1,10.0,data,None)
 
   
 
# Function to read daq outputs
 
def read_volts():
 
    data = np.zeros((1,a_in), dtype=np.float64)
 
    read = ctypes.c_int32()
 
    taskin.ReadAnalogF64(1,10.0,
 
                        daq.DAQmx_Val_GroupByChannel,
 
                        data, a_in, ctypes.byref(read), None)
 
    return data
 
  
#%% Stop and start task
+
# %% Check lights
taskout.StopTask()
+
taskout.write(5)
taskin.StopTask()
+
input("PAUSED - Hit return to continue ")
taskout.StartTask()
+
taskout.write(0)
taskin.StartTask()
 
  
#%% Check lights
+
# %% Write and read values
write_volts(5)
+
N = 300
input('PAUSED - Hit return to continue ')
+
meas = np.zeros((N, 3))
print('Running')
 
write_volts(0)
 
  
#%% Write and read values
+
for k in range(N):
meas = np.zeros((300,3), dtype=np.float64)
+
     v_out = 5 * (k / (N - 1))
for k in range(300):
+
     taskout.write(v_out)
     v_out = -1+(k/299)*6
 
     write_volts(v_out)
 
 
     time.sleep(0.01)
 
     time.sleep(0.01)
     meas[k,:] = read_volts()
+
     meas[k, :] = taskin.read()
  
#%% Turn all off when finished and stop task
+
# %% Turn all off when finished and close task
write_volts(0)
+
taskout.write(0)
taskout.StopTask()
+
taskout.close()
taskin.StopTask()
+
taskin.close()
  
#%% Make plots
+
# %% Make plots
plt.figure(1)
+
fig, ax = plt.subplots(num=1, clear=True)
plt.clf()
+
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--")
plt.plot(total, 'b', resv, 'g', ledv, 'r')
+
ax.legend(["$v_s$", "$v_R$", "$v_{LED}$"])
plt.legend(['$v_s$', '$v_R$', '$v_{LED}$'])
 
  
#%% Save values and figure
+
# %% Save values and figure
color = input('Color: ')
+
color = input("Color: ")
exec("plt.title('{:s}')".format(color))
+
eval("ax.set_title('{:s}')".format(color))
exec("fid=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('{:.4e} {:.4e} {:.4e}\n'.format(*meas[k,:]))
+
     fid.write("{:.4e} {:.4e} {:.4e}\n".format(*meas[k, :]))
 
fid.close()
 
fid.close()
exec("plt.savefig('{:s}_plot.png')".format(color))
+
eval("fig.savefig('{:s}_plot.png')".format(color))
 +
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
</div></div>
  
 
== Questions ==
 
== Questions ==

Latest revision as of 13:44, 22 October 2019

This page contains pictures and graphs related to DAQ 2 for EGR 103. It has been updated for Fall 2019 and nidaqmx.

Notes

  • As before, if the system seems to not recognize Dev1, try Dev2 instead.

Channel 0 Failure

A few boards seemed to have a failed Channel 0; you can still do the lab with the following modifications:

Hardware

The measurement wires will need to be changed as follows: $$\begin{array}{cccc} Purple & Pin B7 & Line 33 & ACH1\\ Yellow & Pin Y5 & Line 66 & ACH9\\ Blue & Pin C7 & Line 65 & ACH2\\ Orange & Pin H7 & Line 31 & ACH10\\ Green & Pin I7 & Line 30 & ACH3\\ Brown & Pin Y3 & Line 63 & ACH11 \end{array}$$

Software

For a single measurement, change the end of line 22 to from ai0 to ai1; for multiple measurements, change the end of line 22 from ai0:2 to ai1:3

Pauses

There are input 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. If that does not work, try restarting the kernal.

Typographical Errors

  • Before section 2.5, you may need to install nidaqmx on the DAQ computer. To do that:
    • Go to the Start menu and find the Anaconda3 folder
    • Start the Anaconda Prompt
    • In the Anaconda Prompt, type
pip install nidaqmx
  • If it asks you if that is OK - it is

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(6 * np.pi * k / N)

That is,

\( \begin{align} V_{out}=2.5+2.5\sin\left(\frac{6\pi k}{N}\right) \end{align} \)

Meas1 plot.png

Graph from aio_meas3

Graph showing all three measurements when the overall voltage is calculated with:

    v_out = 2.5 + 2.5 * np.sin(6 * np.pi * k / N)

Meas3 plot.png

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!

Get data plot.png

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.

aio_output
 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 N = 300
24 
25 for k in range(300):
26     v_out = 2.5 + 2.5 * np.sin(6 * np.pi * k / N)
27     taskout.write(v_out)
28     time.sleep(0.01)
29 
30 
31 # %% Turn all off when finished and close task
32 taskout.write(0)
33 taskout.close()
aio_meas1
 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(6 * np.pi * k / N)
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")
aio_meas3
 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(6 * np.pi * k / N)
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}$"])
aio_get_data
 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.

External Links

References