如何使用MS-IoT闪电与树莓Pi2设置/获取PWM

本文关键字:设置 Pi2 获取 PWM MS-IoT 何使用 | 更新日期: 2023-09-27 18:12:44

我想获得两个PWM信号(即PWM输入)的频率和占空比,并根据输入将它们设置为另一个(即PWM输出)。这些PWM信号的占空比为50%,而其频率范围为1kHz至20kHz。

我在网上查了一下,我从Windows 10物联网核心找到了微软物联网闪电库(即总线提供商)。这个库似乎是我所需要的,即使是PWM消费者的例子!
然而,当我测试基于PWM消费者的第一个例子时,我注意到PWM控制器的频率范围被限制在40Hz到1kHz之间。因此,第一个问题是:频率范围似乎不支持。
此外,虽然PWM控制器属性"ActualFrequency"返回通过"SetDesiredFrequencyMethod"设置的频率,但PWMPin对象仅提供有关当前占空比的信息。

因此,我在谷歌上寻找一些答案,我发现这个问题比前两个问题更让我困惑。

你知道是否有可能以及如何使用MS-IoT闪电库在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);
    }

一切顺利,洛伦佐

如何使用MS-IoT闪电与树莓Pi2设置/获取PWM

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

不确定这是否会起作用,但值得一试的是克隆闪电存储库,修改Max/MinFrequency常量,构建项目,并直接在源项目中引用它,而不是引用nuget包。

我期待着用作用域测试这种方法。

或者,而不是使用闪电驱动程序,使用默认的收件箱驱动程序,pwm设备驱动程序可以在这里找到,试着看看你是否可以使用高于1kHz的更高频率。

树莓派不是一个实时系统(它有缓存和分支预测),所以它不能处理PWM信号就像那样。当一条指令导致缓存刷新或太多if语句预测失败时,它无法准确测量微秒计时。微软物联网闪电以arduino为例,它是实时的。