LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Varying PWM Frequency in Real Time

Hello,

 

I'm working on a research project where I need to speed match a stepper motor to another motor (I realize that the stepper motor isn't the best choice for smooth operation but it's fine). I'm using the myRIO module as the microcontroller to ouptut the step and direction controls for my stepper motor driver. I have my code written to drive my stepper motor working fine and I have attached it (I repurposed the code from a different project so I've been using NaN as the input to the number of steps to give me continuous rotation since my limit switches are unattached, feel free to ignore all that).

 

The issue I'm having is that the myRIO's PWM can only support certain frequencies. For instance, when inputting a frequency into the properties of the PWM subvi, you must click validate and Labview will coerce your value to the nearest supported frequency. I know how to create a control for the frequency input of the PWM subvi but when I vary the frequency while the stepper is running, I inevitably hit a frequency that is not supported by the myRIO and the stepper motor stops running.

 

Does anybody have a suggestion on how to vary the frequency of the PWM in real time or perhaps a work around to create a square wave on a digital output pin that I can vary the frequency of in real time?

 

Thanks in advance for the help.

 

 

0 Kudos
Message 1 of 7
(4,303 Views)

Hello mcbriley,

 

Since the myRIO PWM express VIs and myRIO PWM Advanced IO VIs are hard coded to support frequencies of 40Hz to 40KHz, to achieve higher frequencies you will have to program the FPGA. Essentially you will have to create your own PWN signal. Below is a link to an example of how to accomplish this.


PWM Output With LabVIEW FPGA:
http://www.ni.com/example/26499/en/

 

There are a multitude of different ways of implementing a FPGA program that generates a PWN signal of a certain frequency and duty cycle. The white paper below is an excellent resource for developing a PWM interface using LabVIEW FPGA.

 

Developing a PWM Interface using LabVIEW FPGA:

http://www.ni.com/white-paper/3254/en/

 

Regards,

 

J_Bou

0 Kudos
Message 2 of 7
(4,241 Views)

J_Bou,

 

I do not need to acheive any higher frequency than what the PWM Sub VI can produce. In fact, the frequency range I need to sweep is from 1.5KHz to 2KHz and is easily handled by the PWM Sub VI.  If you'll reread my post, I simply need to know how to vary the frequency of the PWM Sub VI in real time (while the VI is running).

 

I am attempting to speed match two motors, thus I need all the resolution the PWM Sub VI can offer (.01Hz) so generating a list of descrete frequencies that the Sub VI supports to sweep through would take much too long (that would be 50,000 descrete frequencies to validate).

 

I have been able to accomplish varying the speed of my stepper motor in real time with the code I have attached to this response. However, the Wait (ms) function does not provide frequencies fast enough (e.g. 2KHz has a period of .5 ms and the Wait (ms) function has a resolution of +/- 1 ms).

 

Does that clarify what I'm trying to accomplish and the issues I am incurring?

 

I'll look into generating the PWM via FPGA and see if I can find a solution there.

 

Thanks,

 

Matt

0 Kudos
Message 3 of 7
(4,216 Views)

Hello mcbriley,

 

Sorry for the misunderstanding. If I understand your question correctly, you simply want to be able to change the frequency of your PWM signal on the fly. It seems like your application requires a PWM signal that can be customized to a higher degree than the PWM Express VI since using a control to modify the frequency input of the PWM is not adequate. Therefore, I would suggest taking a similar approach to the VI you posted (Stepper_Test.vi) but implement it on FPGA. You will be able to use more precise timing VIs such as Loop Timer.vi which may be what you are looking for.

 

Regards,

 

J_Bou

0 Kudos
Message 4 of 7
(4,190 Views)

Just a quick question on the PWM. I have my PWM within a real-time timed loop and I was concerned that the PWM frequency maybe cannot be higher than half the frequency of the timed loop but you seem to be saying that it is fixed in hardware so I can make it anything up to 40kHz - is this right?

0 Kudos
Message 5 of 7
(3,476 Views)

The loop will run at 40 kHz. As long as the PWM portion of the code can run that fast, you shouldn't see any issues.

0 Kudos
Message 6 of 7
(3,457 Views)

You mean the PWM clock is hardware defined and nothing to do with your own infinite while loop around it.

0 Kudos
Message 7 of 7
(3,455 Views)