Discussion:
Trying to change (on-the-fly) the amplitude of a analog output waveform
(too old to reply)
j***@gmail.com
2008-07-03 10:49:19 UTC
Permalink
I've been crawling these forums and google groups for a while now, and
found some really interesting info, but I still can't get my analog
output to work. What I want to do is simple : I have a 1KHz waveform
output on Ao0, and want to change the amplitude of that waveform on
the fly when I press an "up" or "down" button.

Here is my sourcecode :


ampl = 4.0;
for(int i=0;i<1000;i++)
data[i] = ampl*sin((double)i*2.0*PI/1000.0);

error = DAQmxCreateTask("",&taskHandle);
error = DAQmxCreateAOVoltageChan(taskHandle,"Dev1/
ao0","",-5.0,5.0,DAQmx_Val_Volts,NULL);
error = DAQmxCfgSampClkTiming(taskHandle,"",
1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000);

error = DAQmxSetWriteRegenMode(taskHandle, DAQmx_Val_AllowRegen);
error = DAQmxSetAOUseOnlyOnBrdMem (taskHandle, NULL, 0);
error = DAQmxWriteAnalogF64(taskHandle,
1000,0,0.0,DAQmx_Val_GroupByChannel,data,&res,NULL);
error = DAQmxStartTask(taskHandle);

and this is the code for the "up" button (the "down button is
identical, except for the increment on the first line) :


ampl += 1.0;
if (ampl > 5.0)
ampl = 5.0;

for(int i=0;i<1000;i++)
data[i] = ampl*sin((double)i*2.0*PI/1000.0);

int32 res;
int32 x = DAQmxWriteAnalogF64(taskHandle,
1000,0,1.0,DAQmx_Val_GroupByChannel,data,&res,NULL);
if (x < 0)
{
char buf[4097];
DAQmxGetErrorString (x, buf, 4096);
AfxMessageBox(buf);
}

This code works perfectly, but has a very bizarre delay : when
pressing the up or down button, it takes about 7 re-generations before
the result is seen on the output pins (got a scope attached) ! So on a
1KHz sample rate, it takes about 7 seconds before my output amplitude
increases or decreases

If I up the samplerate to 10KHz, it still takes 7 regenerations, but
now they are done much faster ofcourse

does anyone have an idea on how to fix this ? I really really need the
output to change as fast as possible (the final code will not use user-
clicked buttons, but input from a TTL signal, which is then processed
into a new amplitude)

many many thanks in advance!

Jeroen
andre.buurman@carya
2008-07-03 13:10:13 UTC
Permalink
Start by disallowing regeneration and calculate small pieces of sine wave each iteration. Try to make the generation loop cycle time match the generated amount of samples each cycle.
dansch
2008-07-08 03:40:08 UTC
Permalink
Hi Jeroen,


I understand that you are having trouble updating your
analog output waveform?s amplitude on-the-fly. Which specific language and ADE
are you using? Also, which actual device are you using for your analog output?


I actually modified
an example from CVI 8.5 that performs continuous generation using the internal
clock by replacing all of the DAQmx functions to match those which you included
above. I then added a button to the user interface and created a callback for
the button that would recalculate the data with the new amplitude and write
this new array. I did not experience the same delay that you are experiencing
when reading the output channel with an analog input channel. &nbsp;As André stated above, you
may want to try disallowing regeneration and simply perform a DAQmx Write for
each cycle of the sine wave. You will have to make sure that the cycle time of
the loop matches the amount of time that it takes the device to generate the
samples. I hope this helps,

Loading...