Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Measuring the phase difference of an analog input and output signal using a USB-6211 DAQ and Python 3.5

Hi,

 

I am working on a project where I need to measure the phase difference of an input signal relative to an output signal along with the amplitude of the input signal. I am using a USB-6211 DAQ  and am communicating with it through Python 3.5 (on Win64) where I am using PyDAQmx. The idea is that I will loop though frequencies of signals I send to my system and read back a signal of the same frequency but the phase of the read signal will change relative to my written (output) signal as I change frequency. With some processing I would then pull out the phase difference.

 

I am developing the code now and have run into an issue with an initial test. I am generating an output square wave and inputting it into the DAQ on a read pin and trying to read the flats of the signal. I have been unsuccessful so far. I believe my issue is with the timing of tasks starting and closing (my two tasks being a read and write). Below is a snippet of my code where I believe the issue lies and my whole code is attached in a zip folder.

 

# code above that does not deal with the creation of tasks or DAQmx communication
.
.
.
            top = 2
            bottom = 1
            write_temp = [top, bottom]
            for x in range(0, 4):
                write_temp.append(top)
                write_temp.append(bottom)
            write_temp = [float(i) for i in write_temp]
            write_temp = np.array(write_temp)
            read_num_vals = 120
            read_vector_storage = np.array([float(3 * i) for i in range(0, read_num_vals)])
            samples_per_channel_read = ctypes.c_int(0)

            # Tasks
            # Create tasks that read and write voltages
#######################################################################################################################
            task_write_v_in_ac = Task()
            task_write_v_in_ac.CreateAOVoltageChan("Dev1/ao0", "", -10.0, 10.0, PyDAQmx.DAQmx_Val_Volts, None)
            task_write_v_in_ac.CfgSampClkTiming(None, 1, PyDAQmx.DAQmx_Val_Rising,
                                                PyDAQmx.DAQmx_Val_FiniteSamps, 10)
            samples_per_channel_written = ctypes.c_int(0)  # Place holder for a single number that shows samples written
            task_write_v_in_ac.WriteAnalogF64(10, 0, 0, PyDAQmx.DAQmx_Val_GroupByChannel,
                                              write_temp, byref(samples_per_channel_written), None)

#####################################################################################################################
            task_read_v_in_ac = Task()
            task_read_v_in_ac.CreateAIVoltageChan("Dev1/ai0", "", PyDAQmx.DAQmx_Val_RSE, -10.0, 10.0,
                                                  PyDAQmx.DAQmx_Val_Volts, None)
            task_read_v_in_ac.CfgSampClkTiming(None, 6, PyDAQmx.DAQmx_Val_Rising,
                                                PyDAQmx.DAQmx_Val_FiniteSamps, read_num_vals)

            task_read_v_in_ac.ReadAnalogF64(read_num_vals, 30, PyDAQmx.DAQmx_Val_GroupByChannel, read_vector_storage,
                                            read_num_vals, byref(samples_per_channel_read), None)
            t0 = time.clock()
            task_read_v_in_ac.StartTask()
            t1 = time.clock()
            print("read start took " + str(t1 - t0) + " s")

            task_write_v_in_ac.StartTask()
            t2 = time.clock()
            print("write start took " + str(t2 - t1) + " s")

            task_write_v_in_ac.WaitUntilTaskDone(30)
            t3 = time.clock()
            print("write wait took " + str(t3 - t2) + " s")

            task_write_v_in_ac.StopTask()
            t4 = time.clock()
            print("write stop took " + str(t4 - t3) + " s")

            task_read_v_in_ac.WaitUntilTaskDone(30)
            t5 = time.clock()
            print("read wait took " + str(t5 - t4) + " s")

            task_read_v_in_ac.StopTask()
            t6 = time.clock()
            print("read stop took " + str(t6 - t5) + " s")

            print(read_vector_storage)
            print(samples_per_channel_read)
            t7 = time.clock()
            print("read prints took " + str(t7 -t6) + " s")


Looking at the print outputs I get in my console:


read start took 0.014864053832196735 s
write start took 0.010374004568873515 s
write wait took 9.034804022749778 s
write stop took 0.00345286878108908 s
read wait took 10.784182502702937 s
read stop took 0.008181223916963631 s
[ 0.99944425  0.99944425  0.99977302  0.99944425  0.99944425  0.99944425
  0.99944425  0.99977302  0.99977302  1.00010179  1.00010179  0.99944425
  0.99977302  0.99977302  0.99977302  0.99944425  0.99944425  0.99977302
  0.99977302  0.99944425  0.99911548  0.99944425  0.99944425  0.99977302
  1.00010179  0.99944425  0.99944425  1.00010179  0.99977302  0.99944425
  0.99944425  0.99944425  0.99911548  0.99977302  0.99977302  0.99944425
  0.99977302  0.99977302  1.00010179  0.99944425  0.99977302  0.99977302
  1.00010179  0.99944425  0.99944425  0.99911548  0.99977302  1.00010179
  0.99944425  0.99944425  1.00010179  0.99977302  0.99944425  0.99977302
  0.99977302  0.99944425  0.99944425  0.99977302  0.99977302  0.99977302
  0.99944425  0.99944425  0.99944425  0.99944425  0.99977302  0.99944425
  0.99944425  0.99944425  0.99977302  0.99944425  1.00010179  1.00010179
  0.99977302  0.99977302  0.99944425  0.99911548  0.99911548  0.99944425
  0.99944425  0.99911548  0.99977302  0.99911548  0.99944425  0.99944425
  0.99878671  0.99944425  0.99944425  0.99944425  0.99977302  0.99944425
  0.99977302  0.99977302  0.99944425  0.99977302  0.99977302  0.99944425
  0.99977302  0.99944425  0.99944425  0.99944425  0.99944425  0.99944425
  0.99944425  0.99944425  0.99944425  0.99977302  0.99944425  0.99944425
  0.99944425  0.99977302  0.99944425  0.99944425  0.99944425  0.99977302
  0.99977302  0.99977302  0.99944425  0.99977302  0.99944425  0.99944425]
c_long(120)
read prints took 0.004617634561245865 s

What I was expecting from this output was for some of the numbers in the list to be ~2 and the rest to be ~1. Instead they are all ~1.

 

Let me know if you need anymore information and any help would be greatly appreciated!

-B

0 Kudos
Message 1 of 2
(2,722 Views)

Hello,

 

Unfortunately, PyDAQmx is not a National Instruments tool and since you have had no replies, it seems not to be widely used (I could be wrong there, I am just unfamiliar with it). The PyDAQmx site actually mentions providing feedback and bug reports to Pierre Cladé, but I don't know if he supports the tool actively.

 

The official National Instruments python tools and the NI-DAQmx Python documentation can be found in the links below.

 

NI Python and Resources

NI-DAQmx Python Documentation

NI-DAQmx Python Source

 

Hope it helps.

 

Regards,

Camilo V.
National Instruments
0 Kudos
Message 2 of 2
(2,612 Views)