Issues with 4.2 and PWM

I’m having some problems with PWM and NETMF 4.2 on the Hydra.

One problem is that it looks like the Invert parameter of the PWM instantiation doesn’t have any effect.

Another is the Gadgeteer code for PWM.

Gadgeteer 4.1 had this code for PWM:

public void Set(int frequency, byte dutyCycle)
{
    this.pwm.Set(frequency, dutyCycle);
}

This was fine and worked like a charm.

But Gadgeteer 4.2 has this code:

public void Set(int frequency, double dutyCycle)
{
    if (frequency < 0) throw new ArgumentException("frequency");
    if (dutyCycle < 0 || dutyCycle > 1) throw new ArgumentException("dutyCycle");
    if (pwm == null)
    {
        pwm = new PWM(pwmChannel, frequency, dutyCycle, invert);
        pwm.Start();
        started = true;
    }
    else
    {
        if (started) pwm.Stop();
        pwm.Frequency = frequency;
        pwm.DutyCycle = dutyCycle;
        pwm.Start();
        started = true;
    }
}

My problem is the pwm.Stop() and pwm.Start(). This causes BAD glitches on the PWM in that the PWM pin goes low for a few milliseconds everytime the Set is called. On my MegaMoto module a low on the PWM pin means full speed. This causes audible ticks.

Hi GMod,

The issue with needing to stop and start the PWM is due to a bug found in the Frequency/DutyCycle variables in native 4.2 PWM. If you change the Frequency manually, the Duty Cycle does not reflect the new Frequency. Here is a link to the issue on CodePlex: http://netmf.codeplex.com/workitem/1749 . Vote if you would like to see changed.

A possible fix for you could be to temporarily modify your driver to use the period/duration to manually change it that way based on the frequency and duty cycle the user wants. As for the Inverted pulse, we will be working to fix that issue for the next release.

Wow, how do you guys remember all this? :slight_smile:

I worked around the start/stop thing by only calling Set when the duty cycle actually changes, as the user is likely to call my driver in a timer, setting the same speed over and over.

And I worked around the invert bug by just subtracting the duty cycle from 1.

Thank you for the feedback. Much appreciated.