Controlling 2 independent Stepper motors with variable speed and direction using U3 and Labview | LabJack
 

Controlling 2 independent Stepper motors with variable speed and direction using U3 and Labview

17 posts / 0 new
Last post
Puya
curiouspuya's picture
Controlling 2 independent Stepper motors with variable speed and direction using U3 and Labview

Hi Forum,

I have an application with my U3-LV where i need to control 2 stepper motors that should be completely independent.  Each stepper motor is controlled using dedicated hardware drivers that require pulse input and direction.  So i only need a timed square wave (duty cycle not important) and a digital line to indicate direction for each.  

Here are the specs:

- maximum speed 2000 pulses per sec

- require smooth frequency shift from 0 - 2kHz

- the ability to change frequency without noticeable glitch

- independent frequencies on both drivers

- the ability to count back the pulses or know how many steps I have sent.  not hugely important that I have the ability to stop exactly at a particular count, but it is critical that i dont lose count.  

What I would like to know:  whats the best config to use for my application.  do i use the two timers, and counters? do i do it through the Digital IO?  are there any examples in labview that I can quickly use to save time programming from scratch?

Any help or insight would be greatly appreciated.

thanks

Puya

LabJack Support
labjack support's picture
To really be independent you

To really be independent you might need 2 U3s, but generally based on your description I think trying it with 1 U3 is worth a shot.

To create your output waveforms you will use 2 timers where each is in 1 or 3 modes:  PWM 16, PWM 8, or Frequency Output.  The key limitation with 1 U3 is that all timers use the same clock source so you are limited in the difference in frequency you can get from 2 different timers.  Knowing that TimerClockDivisor and TimerClockBase will be the same for both timers, and only TimerValue can be different, look at the frequency formulas for those 3 modes and you will see the limited options.

As for tracking the number of steps, you can use the 2 counters on the U3 to count the pulses from each timer (jumper each timer output to a counter input), but the counters will not track direction ... they will simply increment by 1 for each timer pulse.

Puya
curiouspuya's picture
Thanks Labjack Support!!

Thanks Labjack Support!!

from what i saw in other posts, its not easy to change PWM frequency without stopping the timer and starting again. is that right?  i would like to be able to smoothly change freq for each output.  from what i understand i will have to use the look up table for the frequency output to get the closest value to the frequency i need.  That should be ok but what i dont understand is if i need to use a look up table for this or if there is a formula i can apply to programmatically generate the inputs i need for the closest freq.   also, will i be able to change the frequencies of each timer independently or will one affect the other?  i didnt quite understand this in your post.

As for counting, i get what you are saying about not being able to detect direction.  thats ok.  i should be able handle that as I will never be changing direction mid movement, ie. i will first stop the motor, then change direction.  however, i do want to know if its worthwhile using the stop on count feature (Firmware counter input?).  can this be done on both timers or just one at a time.

Finally, if you could kindly provide an example in Labview to help me get started i would be grateful.  Im still trying to get my head around the user manual and i wish there were some application notes added in the document to help answer the many questions that pop into my head.

Thanks again,

Puya

LabJack Support
labjack support's picture
"from what i saw in other

"from what i saw in other posts, its not easy to change PWM frequency without stopping the timer and starting again. is that right?"

That is correct. The U3 has one clock that is used to drive both timers. That means that the frequencies of the the two are linked. In the case of PWM one can be on 16-bit and the other on 8-bit, in which case the 16-bit timer will run at 1/256th of the 8-bit timer's frequency. The other option is the frequency output mode. Frequency output mode still depends on the main clock, but has more variability. Here's a like to some more details: https://labjack.com/support/datasheets/u3/hardware-description/timers-co...

The timer stop featureis a timer mode, so it will use one of the two timers available on the U3. When enabled, timer-stop will turn off the other timer after a specified number of pulses. This can be very useful when you want to send a specific number of pulses to a stepper controller.

Have you seen our labview example archive: https://labjack.com/support/software/examples/ud/labview

Puya
curiouspuya's picture
Thank you.  sounds like for

Thank you.  sounds like for my application I cannot use the PWM method for both as it will make the timing of the pulses linked.  

Can I use two Frequency outputs independently or will it be the same issue as two PWMs?   Can I use one PWM and one Freq output?  

I have been going over the Labview examples.  From what i can see, I can only set the frequency using the config functions.  Does that mean I will get a glitch of sorts every time i change the frequency?

Finally, if you could suggest:  If i forgo the HW timed option and go for the I/O output (i know this is software timed), what do you think the fastest rate of pulses I can send out of two I/O?  

thanks in advance.  its been very helpful to hear your advice.

LabJack Support
labjack support's picture
Can I use two Frequency

Can I use two Frequency outputs independently or will it be the same issue as two PWMs?

From Sections 2.9.1.1, 2.9.1.2, and 2.9.1.7, here are the formulas for frequency:

Hz (PWM16) = (TimerClockBase/TimerClockDivisor) / (2*16)
Hz (PWM8) = (TimerClockBase/TimerClockDivisor) / (2*8)
Hz (Frequency Output) = (TimerClockBase/TimerClockDivisor) / (2*Timer#Value)

1.  As they all are based on the same TimerClockBase and TimerClockDivisor, they are are linked to some extent.

2. Changing TimerClockBase or TimerClockDivisor requires re-configuring the whole timer system and thus is not necessarily super smooth.  Changing Timer#Value can be done much more smoothly ... for example with eTCValues.

So probably about the best you can do with 1 U3 is to use 2 timers in Frequency Output mode, set TimerClockBase and TimerClockDivisor to provide the range of frequency you want and never change them, and then just use Timer#Value to set each timer to one of 256 frequency options at any time.

Finally, if you could suggest:  If i forgo the HW timed option and go for the I/O output (i know this is software timed), what do you think the fastest rate of pulses I can send out of two I/O?

Figure it takes about 1 ms per packet you send to the U3.  That packet could set the state of multiple I/O, it could set them to one state and then change them to another (make a pulse), and it could even contain a whole bunch of updates with WAITs in between.  See "Software Timing ..." and "WAIT Technique ..." here:

https://labjack.com/support/app-notes/waveform-generation

Puya
curiouspuya's picture
So probably about the best

So probably about the best you can do with 1 U3 is to use 2 timers in Frequency Output mode, set TimerClockBase and TimerClockDivisor to provide the range of frequency you want and never change them, and then just use Timer#Value to set each timer to one of 256 frequency options at any time.

Got this.  Thanks.  Ill look into it and see if i can find the right combination of values that could give me my full range of frequencies smoothly.

Figure it takes about 1 ms per packet you send to the U3.  That packet could set the state of multiple I/O, it could set them to one state and then change them to another (make a pulse), and it could even contain a whole bunch of updates with WAITs in between.  See "Software Timing ..." and "WAIT Technique ..." here:

Interesting.  I didnt know about the wait function.  Does that mean i can potentially prepare connected intervals of wait and pulses so that the whole thing looks smooth?  Will it just buffer them?  For examples if I want to send blocks of 100 pulses each time buffered by WAIT, how would i control the frequency at which the backed up pulses play out on the IO?  will it just execute the commands as fast as it can?

The link you sent says: ".....the multiple commands within the packet are executed in hardware without any communication delays."

It then goes on to say: 

"The value range you can pass to LJ_ioPUT_WAIT is 0-4000000 microseconds, and the resolution is 128 µs on a U3 and 64 µs on a U6.  To determine how many commands can be fit in a single packet on the U3/U6, consider that the low-level Feedback packet has room for 57 bytes of data in the command and 55 bytes of data in the response.  Section 5.2.5 of either User's Guide (U3/U6) specifies the command and response bytes needed for each IOType."

Hard for me to decipher the above explanation in the context of what i am trying to do.  

thanks

p

LabJack Support
labjack support's picture
A typical example would be to

A typical example would be to send a packet such as:

FIO0=0
WAIT=5000
FIO0=1
WAIT=5000
FIO0=0
WAIT=5000
FIO0=1

This would take roughly 500us to get the packet from your software to the U3.  Then in the U3 it would set FIO0 to output-low, wait 5ms, set FIO0 to output-high, wait 5ms, set FIO0 to output-low, wait 5ms, and finally set FIO0 to output-high.  Then it would take about 500us for the response (e.g. no error) to go from the U3 back to your software.

I'm not sure this feature will help you but something to consider if you are looking at software control.

Puya
curiouspuya's picture
Thanks.  That makes sense in

Thanks.  That makes sense in principal.  2 things Im still not sure about:

1 - if i have a packet with say 7 commands as you have above, and if this command takes ~20ms to complete, can I send another packet with another 7 commands during the 20ms?  if so, will it finish the first 7 before doing the new 7? or will a new command interrupt the first packet execution?  ie does it work like a buffered FIFO command list?

2 - how do I stack commands like you have presented into a single packet using Labview?  In the labview examples I have seen, there are always one command at a time.  i havent seen anything akin to an array of commands.  

Thanks again,

Puya

LabJack Support
labjack support's picture
1.  No, the U3 can only

1.  No, the U3 can only process 1 command-response at a time.  It must send out the response before it will handle another command.

2.  You want to use the add-go-get technique described in Section 4.1 of the U3 User's Guide.  See "U3 Multiple IO Example.vi" or "U3 Single Pulse Bit Ouput (no-timer).vi".

jsimeroth
jsimeroth's picture
Hello. I'm trying to figure

Hello. I'm trying to figure out the maximum rate that I could successfully send digital 3.3V pulses to a stepper motor, and I've already looked at the waveform generation page and several others. Having looked at section 5.2.5 of the U3 User's guide (U3/U6) regarding feedback packets, I am still unsure how many bytes would be used by the packet suggested above by LabJack support:

"FIO0=0
WAIT=5000
FIO0=1
WAIT=5000
FIO0=0
WAIT=5000
FIO0=1"

Can someone please help me understand how many bytes each of these commands requires based on 5.2.5 and how many bytes would be required for the response? My hope is that understanding this will help me understand how many commands I can fit into a single packet, which will tell me how many pulses I can send per unit time based on how long it takes to send, execute, and return each packet.

Any help would be appreciated.

LabJack Support
labjack support's picture
For efficiency and when

For efficiency and when setting one digital I/O line, use the UD IOType LJ_ioPUT_DIGITAL_BIT to set FIO0 and LJ_ioPUT_WAIT to set the WAIT delay. If you need to set more than 2 digital lines at the same time use LJ_ioPUT_DIGITAL_PORT as it will use less bytes then 2+ LJ_ioPUT_DIGITAL_BITs in most cases.

These UD IOTypes will build a low-level Feedback command with a max. size of 64-bytes. Feedback is documented here (5.2.5):

https://labjack.com/support/datasheets/u3/low-level-function-reference/f...

The base Feedback command is 7 bytes. The following is how the UD IOTypes map to low-level IOTypes and their command data bytes:

LJ_ioPUT_DIGITAL_BIT = 4-bytes:
    BitDirWrite = 2-bytes
    BitStateWrite = 2-bytes

LJ_ioPUT_WAIT = 2- or 4-bytes:
    WaitLong = 2-bytes
    WaitShort = 2-bytes
    Notes: WaitLong is used when: microseconds / 16384 > 0
           WaitShort is used when: (microseconds % 16384) / 128 > 0
           Integer division is used.

LJ_ioPUT_DIGITAL_PORT = 7- or 14-bytes
    PortDirWrite = 7-bytes
    PortStateRead = 7-bytes
    Note: PortDirWrite is conditionally used when the UD driver detects a change in direction (input to output).

For example:

AddRequest: LJ_ioPUT_DIGITAL_BIT=0 (4-bytes)
AddRequest: LJ_ioPUT_WAIT=5000 (2-bytes)
AddRequest: LJ_ioPUT_DIGITAL_BIT=1 (4-bytes)
AddRequest: LJ_ioPUT_WAIT=5000 (2-bytes)
AddRequest: LJ_ioPUT_DIGITAL_BIT=0 (4-bytes)
AddRequest: LJ_ioPUT_WAIT=5000 (2-bytes)
AddRequest: LJ_ioPUT_DIGITAL_BIT=1 (4-bytes)
Go

Would be a 29-byte command Feedback command (7 + 22), and the driver would add an extra dummy byte to the end to make it size 30-bytes since commands need to be an even number in size.

jsimeroth
jsimeroth's picture
Support,

Support,

I see now that hardware timing is the way that I want to go. This leaves me with the following question: I am using LabVIEW 2015, and the Frequency Output mode and Stop Timer mode on Timer0 and Timer1, respectively to execute a pre-defined number of steps. How then, can I use a limit switch or prox sensor to interrupt the process before the specified number of steps has occurred (without an irreversible shut down of power)?  

What I mean is this: Ideally, the switch would be wired to some input so that it could sometimes stop all motion with minimal software delays, and in other cases simply be a way that the program can check that the motor is in fact in the right position (i.e. is at the limit switch position) before beginning motion. Is there any way to do this? I'm imagining use of a Stop Timer mode that waits for a single pulse from the limit switch, and then in other scenarios is reconfigured to be a simple digital input. The problem I see with this is that I am already using both my Timer0 (in Freq. output) and Timer1 (in Stop Timer mode to count pulses sent out by the timer), and can't use Timer1 for more than one thing at the same time. Does this make sense?

I appreciate your help,

Johnathan

LabJack Support
labjack support's picture
All I can think of is to have

All I can think of is to have software poll the state of the switch so you can make your desired decision.  That means perhaps 1 ms to poll the switch and then at the limit another 1 ms to tell the U3 to stop pulsing.

On the T7 you could have this in a script that should be able to do this simple feedback much faster than the 2 ms loop above.

jsimeroth
jsimeroth's picture
Thank you LabJack Support.

Thank you LabJack Support. Yes, I was hoping to avoid slowing down the process with polling every iteration, but I think the additional 2 ms is tolerable. I have a couple more questions for this stepper motor application:

  1. If I am running two stepper motors at different (and varying) rates, AND I want to count the # of steps with Stop Timer mode, am I correct in thinking I would need two U3s to do this? i.e. Is there no other way to accurately control # of steps that doesn't consume both timers for one motor?
  2. Is it possible to output pulses with Timer0, use Stop Timer mode with Timer1, AND still use the Stream Mode to read other digital or analog input signals at the same time with one U3? I just noticed that eTCConfig() seems to be used in both scenarios.
  3. Maybe this is silly, but it doesn't seem obvious to me from what I've found online so far: eTCConfig(), when configured in PWM16 or Frequency Output modes for example, actually initiates pulses from the U3, right? Or are they initiated from eTCValues()?

Thank you again,

Johnathan

jsimeroth
jsimeroth's picture
Never mind Question 3; it is

Never mind Question 3; it is clear to me now that eTCConfig() does, in fact, initiate the pulses, whereas eTCValues() only reads them. My first two questions remain, however.

LabJack Support
labjack support's picture
1.  Yes, 2 devices is about

1.  Yes, 2 devices is about the only way to get different pulse rates, and for sure the only way to use 2 stop timers.

2.  Yes, that sounds fine.  I can't think of a problem with what you describe.

3.  Correct.  eTCConfig does initiate the pulses assuming you pass values that would cause pulses to occur.