LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic Duty Cycle Change for PWM

Solved!
Go to solution

Hello savvy LabView people out there!
I am trying to do PWM for servo control using LabView.
I have been able to manually move my servo with this: http://www.ni.com/white-paper/2991/en

 

My goal is to automate the servo position as a sinuisoidal movement, and to do so,

I need to automate the duty cycle to oscillate in 5%~10% automatically over time.
I have looked into some stuff as:

http://forums.ni.com/t5/Counter-Timer/PCI-6602-PWM-GENERATION-Dynamic-sine-duty-cycle/td-p/1522442

But it was not too helpful, as I was also getting Error -200301.

I want to be able to move the servo position at ~10Hz, if that is important, and the square wave freq. for the

servo signal (PWM) has to be 50Hz.

Any help? Thank you!

____________________________________
Attached is what I am trying to do. The sine motion of the servo is (supposed to be) actually paired up with

an analog output from a BNC-2120 board. The servo control part is separated on the top portion.

 

0 Kudos
Message 1 of 10
(6,463 Views)

I don't understand what you're trying to do in that code. In particular, you are running the iteration count wire out of the bottom loop and into a calculation that feeds the top loop. That wire will prevent the top loop from running until the bottom one terminates. Is that what you want? Why do you need two separate loops?

 

The -200301 error is easy to resolve - don't update the duty cycle so frequently. The easiest way to do this is to make sure you have a wait inside the loop that is at least as long as the PWM period.

 

Can you try to ask a more specific question than "Any help?"

0 Kudos
Message 2 of 10
(6,452 Views)

Please ignore the wire that is being fed from the bottom loop to the top loop.

I am generating PWM at 50Hz, with duty cycle that is 5Hz~10Hz.
I want to be able to update the duty cycle automatically (and sinusoidally) in that range at 50Hz as well.

As is shown in http://www.ni.com/white-paper/2991/en the duty cycle must be changed manually.

 

Can you help with automating the duty cycle change?

0 Kudos
Message 3 of 10
(6,438 Views)

Use the point-by-point sine wave generator VI with appropriate parameters to generate a sine wave in the range that you want, then connect the output to the duty cycle.

0 Kudos
Message 4 of 10
(6,429 Views)

Is something like this for the PWM look like what you had in mind?

(see attached)

 

This works, but I need something that responds faster.

For example, the PWM needs to be at 50Hz, which means the loop delay should be able to

go as low as 20ms

In the picture, it is currently 200ms, and this does not run. I found that with 50Hz, the minimum

delay that the Error 200301 doesn't pop up is with 500ms delay, at which point the

accuracy of PWM duty cycle in each period is compromised.

Is there a way to have the loop delay be as low as ~20ms?

 

0 Kudos
Message 5 of 10
(6,402 Views)

The PWM frequency is independent of the loop rate; the PWM generation occurs continuously in hardware using the last duty cycle and frequency that were set until a new value is written or the task is stopped. The PWM generator must output at least one complete period before it will accept a new value, otherwise the -200301 error occurs. If you're getting an error at 200ms, that sounds like you have the PWM frequency set to 5Hz and not 50Hz.

 

There's no need for the local variable and comparison against the shift register value.

0 Kudos
Message 6 of 10
(6,388 Views)

Thank you very much for the reply so far.

(I have the loop comparing to checking the value for the case when I want to change the loop into a manual duty-cycle change mode).

I am certain that I have the PWM freqeuency set to 50Hz, and to make this explicit, I took a snapshot with

the frequency set as a numerical constant at 50Hz.

The loop waits for 400ms, and I still get the -200301error.

Is there something that I could be missing?

0 Kudos
Message 7 of 10
(6,355 Views)
Solution
Accepted by topic author CalvinKim

Does the error occur on the first iteration of the loop? What's the initial delay value set to? If the initial delay is longer than the loop period, you could still get that error.

 

Does it matter if you use a Wait (ms) instead of Wait for Next Millisecond Multiple?

 

And the check against the duty cycle local variable is still useless. You should check against the previous duty cycle that was set, not the duty cycle local variable.

Message 8 of 10
(6,347 Views)

That was exactly it, thank you very much!
As for an improvement, do you know if there is a way to specify the duty cycle more precisely?

(currently, I can only specify two digits, so 11%, for example, and not 11.3%).

0 Kudos
Message 9 of 10
(6,323 Views)

@CalvinKim wrote:

As for an improvement, do you know if there is a way to specify the duty cycle more precisely?

(currently, I can only specify two digits, so 11%, for example, and not 11.3%).


I think this is a display problem, not a code problem. Change the format of the duty cycle control to display more digits.

0 Kudos
Message 10 of 10
(6,317 Views)