LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

PWM timing issue

Solved!
Go to solution

Hi,

 

What I want to do:

I need to output a PWM signal with varying frequency and duty cycle using DAQ assistant to a card with digital out. The max frequency I should* need is roughly 500 Hz. 

 

What's stopping me:

I can't figure out how my frequency slider relates to an actual Hz frequency. 

 

What's next:

Well, I would like a simplified approach to outputting a pwm signal to a digital output on a card or be able to figure out the frequency/timeing/delays out more. I just guessed to get a wave that looked nice on the chart, but I have no idea on what the actual frequency is. (How could I measure it, so I can guess and check to figure out a range I need?) Any modifications to my attached code, or brand new ideas would be great! I am still pretty new to labview so more detail than less is appreciated.

 

Hopefully this makes sense what I'm trying to do. 

Thank you! 

 

0 Kudos
Message 1 of 22
(4,211 Views)

This is a poor way to generate a PWM signal, and you are unlikely to achieve a consistent 500Hz since you are doing all the timing in software.  Which DAQ hardware are you using?  To get an accurate PWM signal, use the counter/timer channels on your DAQ board if it has them (and if it doesn't, you may want to get one that does).  There are DAQmx examples for creating a PWM signal this way.  You won't be able to see it on a chart - you'll need to connect the board to an oscilloscope - but it will be reliable and consistent.

 

Don't use Number to Boolean Array followed by Index Array to convert to a boolean.  Just check if the value is greater than 0.

 

Your frequency right now has little relation to an actual frequency.  Take a look at the help for the square wave function, which shows the equation used to generate the output.  Then look at what your code does.  You have what you hope is a 10ms loop, although the timing may vary depending on what else your operating system is doing.  Your time input is the loop iteration.  If you want something close to real timing, you'll need to feed in real time units, using a function from the timing palette.  Following the math, if you have a frequency in cycles/second, then the "time" input should be in seconds, so try using the function "Get Date/Time in Seconds."

0 Kudos
Message 2 of 22
(4,203 Views)

Hi nathand,

 

Im using a 9472 card with dsub connector. I believe it has HW timing, but it only lets me select one sample on demand, not  one sample hw timed in the DAQ assistant. 

 

Comparing makes a lot more sense than using all those conversions I was doing. Thanks - I knew their had to be an easier way. 

 

Using the "Get Date/Time in Seconds" function, I cannot connect this to my time input on the Square Wave PtByPt.vi, because the a the source is timestamp and sink is a 64bit double. I guess I'm not sure how to implement your suggestion in your 3rd paragraph? Could you show me what you mean or explain this further? 

 

Thank you. 

 

EDIT: I can use the tic count (ms) and this work for the time input. 

0 Kudos
Message 3 of 22
(4,180 Views)

Hi gemini,

 

the NI9472 is a digital output module either for cRIO or for cDAQ. Judging from your VI you're using a cDAQ...

 

- The 9472 has no HW timing, you have to do that in software (AFAIK).

- Controlling a loop with 10ms loop rate is nearly impossible with Windows, when you want accurate timings

- using a timed while loop may help to get better results

- don't using the DAQ assistent will definetely help!!!

- maybe it also helps to calculate the PWM output on your own, it's usually very easy...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 22
(4,177 Views)
Solution
Accepted by topic author g_e_m_i_n_i

GerdW,

 

This seems to be a lot more accurate. I used the ms timer as my input, so I scaled my frequency input by .001 to make up for the thousand difference on my timer. This seems a lot more accurate. 

 

Thanks. 

 

Edit: yes its cDAQ by the way. 

 

0 Kudos
Message 5 of 22
(4,174 Views)

Hi gemini,

 

- instead of scaling the frequency I would scale the ms timer value...

- there's a function, which already compares to zero - no additional constant needed...

- get rid of the DAQ-Assi to get even better results...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 6 of 22
(4,170 Views)

GerdW hello again!

 

-I scaled the freq first, because I would have to do a data type conversion on the (ms) timer. Which is better? I scaled the timer in this .vi

-good to know, i didnt see that origionally. I guess I want a 0-5 pwm wave, so I'll be comparing at 2.5 now, but that is still useful. 

-I need to output this wave on a card, dont I need a DAQ assistant? 😛

 

Or is this where those task things come into play?

0 Kudos
Message 7 of 22
(4,164 Views)

Using the millisecond timer as you've done is fine, I don't think it matters which direction you do the scaling.  If for some reason you want to use the Get Date/Time in Seconds function I mentioned earlier, insert "To Double Precision Float" function from the Numeric -> Conversion palette to convert the timestamp to a floating-point.

 

You can use any value that has an absolute value less than the amplitude for the comparison.  Since the square wave generation function gives you an output from -amplitude to +amplitude, 0 is easy.  Your eventual output is a boolean - either on or off - so the amplitude doesn't actually matter.

 

The DAQ assistant is hiding the slightly lower-level DAQ functions from you, but those functions are not hard to use, especially for a simple individual output like this.  Take a look at some of the DAQmx examples.  You can set up a task in Measurement and Automation Explorer, or you can configure it all yourself in code - either approach will work and neither is too complicated.

Message 8 of 22
(4,154 Views)

Hi Gemini,

 

To supplement nathand's comments,

 

You can use the following VIs from the NI example finder:
Create a PWM signal:
Hardware input and output -> DAQmx-> Generating digital pulses -> Gen Dig Pulse Train-Finite.vi

Read the PWM signal, use one of the following examples:
Hardware input and output -> DAQmx-> Counter Measurement -> Period or pulse with -> Meas Pulse Width.vi

 

You may find Pulse Width Modulation (PWM) Using NI-DAQmx and LabVIEW useful as well.

Yupeng J.
0 Kudos
Message 9 of 22
(4,149 Views)

That would all be useful information except that it appears that Gemini is using a system that has no counter/timer channels so those examples will not work.

0 Kudos
Message 10 of 22
(4,145 Views)