Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamically varying pulse train frequency

Hello. I'm trying to control a stepper motor with a USB-6221 Multifunction DAQ card. I've found many useful threads which have got me up and running by sending out a pulse train. I've even been able to vary the pulse train frequency as in this thread: http://forums.ni.com/ni/board/message?board.id=170&message.id=150471&query.id=95686#M150471.

What I really want, however, is a pulse train whose frequency varies with the time elapsed. (Specifically sinusoidally, but I'll take a linear ramp for now.) I can get the frequency to ramp with a constant slope, but when I try to link it to the elapsed time it just quits waiting and runs through the wait command.

Does anyone have any ideas or examples that might be useful?

Thanks!
0 Kudos
Message 1 of 5
(3,420 Views)

What do you need to control with this stepper?  Will you need the position to be sinusoidal as well, without any "creep?"  Or are you needing a sinusoidal variation superimposed on a nominal constant freq?

Here's the problem: you *do* have the ability to change output freq on-the-fly, but the exact timing of when those changes occur are at the mercy of your software code and OS.  At best, you'll create a piecewise-constant approximation of a sine wave as each programmed frequency is liable to generate multiple pulses before your next update.  Further, you won't be able to count on this stairstepping frequency to be entirely consistent.

So I'm going to point you in another direction that's a bigger hassle and more complicated to deal with, but which stands a chance of doing exactly what you need.  Instead of generating the step pulses with an onboard counter, generate them with your DIO port 0 which can generate hardware clocked digital states.

One thing working in your favor here is that you can define a buffer that represents 1 cycle of this sinusoidally-varying frequency and have the board keep regenerating.  This frees up your software from having to perform continuous computations on the fly.  However, defining that array of digital states isn't quite trivial.

Your board lets you generate digital patterns at up to 1 MHz, so you can define your step pulse edges with 1 microsec precision.  You just have to figure out where to place those state changes so that the intervals between them correspond to your sinusoidally-varying frequency.  This shouldn't be horrendously difficult, but it *does* require some careful attention to detail.  Think carefully about how the digital generation will wrap around from the last defined value in the buffer back to the first.  The interval between step pulse edges during this wraparound will also need to be the right duration.

-Kevin P.

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 2 of 5
(3,409 Views)
Add a shared variable to your program.  Use this variable to add the change in frequency to your center frequency (i.e. 500 Hz + shared variable).

Create a second timed loop in your program that will write new values to this shared variable.  Generate a sinusoid waveform and feed it into the loop.  Set up the loop to cycle through the array and write the values to the shared variable.  Scale the sinusoid to set the frequency deviation that you want (i.e. multiply by 10 to get +/- 10 Hz variation).  Set the timing of the timed loop to what value you need for you application.

If this doesn't make sense, post some code and we can go from there.
Randall Pursley
0 Kudos
Message 3 of 5
(3,408 Views)
Kevin,

That sounds exactly like what I'd like to do (write to a buffer, then dump an entire cycle out). I'm admittedly new to this, but I can't seem to figure out how to fill then empty the buffer. What I'd like is 500 pulses per cycle, so I guess I want a 1000 element array [101010...] that I can control the ouput frequency of or, more likely a much longer array with a fixed frequency where I vary the length of the pulses by setting the binary values [101100111000...].

Can you point me to any documentation/examples that show how to fill and empty a buffer? Or explain how it works?

I'm attaching what I have so far, but I'm afraid it probably doesn't help much.

Thanks a lot!
Anya
0 Kudos
Message 4 of 5
(3,392 Views)

For general help on setting up a hardware-clocked digital output task (see also "correlated" digital io), you can look over shipping examples and here on ni's site.  There should be some simple demonstrations of how you send a buffer of data to a digital task which will then generate the physical signals.

For specific help on defining the 000100000000000000000000001  array of bit values, well, I can give you a few general thoughts to get you started. 

Let's suppose your nominal desired speed is given by g(t) = A + B*sin(2*pi*f*t).  Well, you are only going to be able to define this at the discrete points in time corresponding to step pulse edges.  So you would integrate g(t) to get an expression for position as a function of time.  That'll be something like h(t) = C + At - (B / (2*pi*f))*cos(2*pi*f*t).    Then you could solve h(t) at the discrete positions corresponding to integer numbers of steps.  

That's a nice mathematical approach, but is a pain to implement.  Here's another way.  Assume an initial step rate to define your initial time interval.  This should correspond to A in the g(t) function above.  Well, that value A for step freq implicitly defines the next point in time where you will compute g(t).  So, at the time t=1/A, you can recompute g(t) to come up with your next step rate.  And so on until you've figured out a full sine wave period worth of step rates.

Either way, it'll probably still be tricky to define a a nice transition as you wrap around from the end of the buffer back to the beginning.  By defining an array of varying step rates, you've also defined your step pulse edge times.  Assuming an active high step pulse, put a 1 or a T into the positions corresponding to step times.  Put them into succeeding positions too until you've generated a wide enough step pulse for your hardware to recognize.

-Kevin P.

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 5 of 5
(3,377 Views)