Main Site Documentation

Smooth PWM servo control?


Hello, I’m a couple days in working with my shiny new Panda board. It rocks.

I’m building an autopilot and need smooth clean servo control. I have used the sample code provided somewhere using the PWM driver. I don’t even remeber where I got the same because it’s the most common PWM demo.

That works, but my code loop controls the servo on a timed loop that’s independent of the PWM clock cycle. Instead of smooth steady movement, the servo is jittery, even when I generate a smooth sweep with a loop in my code.

I think my call to SetPulse in the control loop interrupts the PWM signal and sometimes sends the sends the servo only part of the high pulse, basically giving it an error value. I don’t have a scope so I have no good way to confirm this.

Is there a way to use hardware PWM but make sure that a cycle is never interrupted? I imageI’d have the same problem with OutputCompare. I could write my own software driver but I’d rather use h/w PWM. I know I can not call SetPulse if my value hasn’t changed, but I will be making small changes to the values and need smooth output.

Has anyone ran into this? I can paste code here but it’s spread across several classes so I’d have to write a simple demo function, but I can do that.


Does your code use real PWM or OC (software PWM)?

We had a bug before but that was fixed. What is your firmware version?


Are you running the beta? IF you aren’t, there was a bug that got fixed a long time ago that made servos and ESCs go nuts if they were controlled by a PWM pin.



I am a little confused by your statement. If the bug was fixed a long time ago, what does it have to do with the current beta?


Chris is right :smiley: It was long time ago but apparently haven’t made it to the production release yet.
See this

PWM fix was in previous beta version. I think this will be released this week so no need to use beta.


I figured it was best to post the code. I sounds like all will be good with the next release so if I’m doing this the right way I’ll move on to other things and come back to servos.

            PWM outputPort = new PWM((PWM.Pin)FEZ_Pin.PWM.Di10);

            int delta = 5;
            int value = 1250;

                outputPort.SetPulse(20000000, (uint)value * 1000);

                value += delta;

                if (value >= 1750 || value <= 1250)
                    delta = -delta;


version stamp:
ClrInfo.clrVersion: 4.1.2821.0
ClrInfo.clrVendorInfo: Microsoft Copyright © Microsoft Corporation. All rig
ClrInfo.targetFrameworkVersion: 4.1.2821.0
SolutionReleaseInfo.solutionVendorInfo: GHI Electronics, LLC
SoftwareVersion.BuildDate: Aug 9 2010
SoftwareVersion.CompilerVersion: 310739


Talk to Gus, he can get you hooked up with the beta. See the contact page for GHI’s email.


No need to bother with beta since release is coming very shortly


[quote]No need to bother with beta since release is coming very shortly

Agreed… I’m going to work on other aspects and go back to servos after the update.