c# – 如何使用MS-IoT Lightning使用Raspberry Pi2设置/获取PWM?

前端之家收集整理的这篇文章主要介绍了c# – 如何使用MS-IoT Lightning使用Raspberry Pi2设置/获取PWM?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想获得两个PWM信号(即PWM输入)的频率和占空比,并根据输入将它们设置为另一个(即PWM输出).这些PWM信号的占空比为50%,而其频率范围为1kHz至20kHz.

我检查了一下网页,我从Windows 10 IoT Core找到了Microsoft IoT Lightning库(即总线提供商).即使使用PWM Consumer示例,这个库似乎也是我需要的!
然而,当我在测试基于PWM Consumer的第一个示例时,我注意到PWM控制器的频率范围限制在40Hz到1kHz之间.因此,第一个问题:似乎不支持频率范围.
此外,当PWM控制器属性“ActualFrequency”返回通过“SetDesiredFrequencyMethod”设置的频率时,PWMPin对象仅提供有关当前占空比的信息.

因此,我用谷歌搜索寻找答案,我发现this question比以前的两个问题更让我困惑.

您知道是否可以以及如何使用MS-IoT Lightning Library在Raspberry Pi2上设置/获取1kHz至20kHz的PWM信号?

这里,示例中的几行代码

public async void Run(IBackgroundTaskInstance taskInstance)
    {
        if (!LightningProvider.IsLightningEnabled)
        {
            // Lightning provider is required for this sample
            return;
        }

        var deferral = taskInstance.GetDeferral();

        // Use the PAC9685 PWM provider,LightningPCA9685PwmControllerProvider
        pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[0];
        motorPin = pwmController.OpenPin(0);
        secondMotorPin = pwmController.OpenPin(1);

        //// To use the software PWM provider,LightningSoftwarePwmControllerProvider,with GPIO pins 5 and 6,//// uncomment the following lines and comment the ones above
        //pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[1];
        //motorPin = pwmController.OpenPin(5);
        //secondMotorPin = pwmController.OpenPin(6);

        pwmController.SetDesiredFrequency(50);
        motorPin.SetActiveDutyCyclePercentage(RestingPulseLegnth);
        motorPin.Start();
        secondMotorPin.SetActiveDutyCyclePercentage(RestingPulseLegnth);
        secondMotorPin.Start();

        timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick,TimeSpan.FromMilliseconds(500));
    }

    private void Timer_Tick(ThreadPoolTimer timer)
    {
        iteration++;
        if (iteration % 3 == 0)
        {
            currentPulseLength = ClockwisePulseLength;
            secondPulseLength = CounterClockwisePulseLegnth;
        }
        else if (iteration % 3 == 1)
        {
            currentPulseLength = CounterClockwisePulseLegnth;
            secondPulseLength = ClockwisePulseLength;
        }
        else
        {
            currentPulseLength = 0;
            secondPulseLength = 0;
        }

        double desiredPercentage = currentPulseLength / (1000.0 / pwmController.ActualFrequency);
        motorPin.SetActiveDutyCyclePercentage(desiredPercentage);

        double secondDesiredPercentage = secondPulseLength / (1000.0 / pwmController.ActualFrequency);
        secondMotorPin.SetActiveDutyCyclePercentage(secondDesiredPercentage);
    }

一切顺利,洛伦佐

解决方法

物联网闪电框架似乎对PWM控制器输出频率有软件限制,请参见 file.

不确定这是否有效,但值得一试的是复制闪电repository,修改Max / MinFrequency常量,构建项目,并直接在源项目中引用它,而不是引用nuget包.

我期待这种方法用范围进行测试.

或者,使用默认的收件箱驱动程序,而不是使用Lightning驱动程序,可以在here找到pwm设备驱动程序,尝试查看是否可以使用1kHz以上的更高频率.

原文链接:https://www.f2er.com/csharp/707036.html

猜你在找的C#相关文章