Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

NIDaqmx Python: Configure Analog Function Generation

Solved!
Go to solution

Hello,

 

I am attempting to program a myDAQ device to generate square waves of various duty cycles using the nidaqmx library in Python. The library features a number of properties for a function generation channel, including 'add_ao_func_gen_channel' (found in 'ao_channel_collection'). 

 

Here is a test code I have set up:

test_Task = nidaqmx.Task()
test_Task.ao_channels.add_ao_func_gen_chan("myDAQ1/ao0", type= FuncGenType.SQUARE, freq= 0.2, amplitude= 10)

for i in range(1, 10):
    test_Task.channels.ao_func_gen_square_duty_cycle = i /10
    test_Task.start()
    time.sleep(5)
    test_Task.stop()

Which gives the error:

nidaqmx.errors.DaqError: Selected physical channel does not support the output type required by the virtual channel you are creating.

Create a channel of an output type that is supported by the physical channel, or select a physical channel that supports the output type.
Property: DAQmx_AO_OutputType
Requested Value: DAQmx_Val_FuncGen
Possible Values: DAQmx_Val_Voltage

Task Name: _unnamedTask<0>

Status Code: -200432

I'm not sure if this is a problem with the code or with the hardware. As far as I understand, any channel that can output voltage should be able to output a waveform. Are there other NI devices that would support a function generation channel?

0 Kudos
Message 1 of 12
(13,768 Views)
Solution
Accepted by topic author aisaly

The error states that the channel selected does not support the Analog Output Function Generator type. Based on your current setup, you can use the Analog Output Voltage channel type. 

 

The NI ELVIS II boards support the AO-FuncGen channel type. I understand you are using Python, but the following document provides an overview on how to use the myDAQ and the NI ELVISmx Function Generator: http://www.ni.com/tutorial/11503/en/. 

Brian.D
NI Product Owner
VeriStand
0 Kudos
Message 2 of 12
(13,722 Views)

Hi,

 

Continuing this topic. Brian, you say that "you can use the Analog Output Voltage channel type". Could you please give an example of the Python code that will create periodical signal at the Analog Output pin.

 

I'm using USB-6361 (BNC) device.

 

Thanks!

0 Kudos
Message 3 of 12
(13,541 Views)

Here is some code I'm running to generate waveforms:

 

test_Task = nidaqmx.Task()
test_Task.ao_channels.add_ao_voltage_chan('myDAQ1/ao1')
test_Task.timing.cfg_samp_clk_timing(rate= 80, sample_mode= AcquisitionType.FINITE, samps_per_chan= 40)

test_Writer = nidaqmx.stream_writers.AnalogSingleChannelWriter(test_Task.out_stream, auto_start=True)

samples = np.append(5*np.ones(30), np.zeros(10))

test_Writer.write_many_sample(samples)
test_Task.wait_until_done()
test_Task.stop()
test_Task.close()

This will generate a single cycle of a square wave with 75% duty cycle and 0.5 sec period. The 'samples' could be anything you want and increasing the 'samps_per_chan' parameter will allow you to write for a longer duration. Keeping the same array but increasing 'samps_per_chan' will actually regenerate the 75% square wave until the specified samples have been generated.

0 Kudos
Message 4 of 12
(13,527 Views)

Dear aisaly,

 

Thanks, it works for me. Do you know if there is a way to loop this indefinitely?

0 Kudos
Message 5 of 12
(13,523 Views)

You could just change the 'FINITE' Acquisition Type to 'CONTINUOUS'. 

0 Kudos
Message 6 of 12
(13,506 Views)

Of course, thanks! I really appreciate your fast responses.

0 Kudos
Message 7 of 12
(13,497 Views)

Hi,

I just ran into the same error

ao_task.ao_channels.add_ao_func_gen_chan('Dev1/ao1',type=FuncGenType.SAWTOOTH,freq=1000,amplitude=9,offset=1)

I use the code to generate sawtooth,but i got the same error you got , could you please tell me how to generate a sawtooth wave? 

Thanks

0 Kudos
Message 8 of 12
(13,331 Views)

You'll have to generate an array of the samples yourself. Numpy has a function for making sawtooth waves. You can buffer them using the 'write_many_samples' method, in the same way I did above.

0 Kudos
Message 9 of 12
(13,319 Views)

Dear Anticode,

 

I did this with numpy according to aisaly's instructions. Here is code that works at least for NI USB 6361 device.

 

import nidaqmx
import numpy as np
from nidaqmx import *

test_Task = nidaqmx.Task()
test_Task.ao_channels.add_ao_voltage_chan('Dev1/ao1')
test_Task.timing.cfg_samp_clk_timing(rate= 10, sample_mode= nidaqmx.constants.AcquisitionType.CONTINUOUS, samps_per_chan= 5)
test_Writer = nidaqmx.stream_writers.AnalogSingleChannelWriter(test_Task.out_stream, auto_start=True)
samples = np.append(5*np.ones(10), np.zeros(10))
test_Writer.write_many_sample(samples)
test_Task.wait_until_done(timeout=100)

Message 10 of 12
(13,310 Views)