我想获取两个 PWM 信号(即 PWM 输入)的频率和占空比,并根据输入为另一个信号(即 PWM 输出)设置它们。这些 PWM 信号的占空比为 50%,而它们的频率范围为 1kHz 至 20kHz。
我上网查了一下,找到了 Microsoft IoT Lightning来自 Windows 10 IoT 核心版的库(即总线提供程序)。这个库似乎是我所需要的,即使是 PWM Consumer 示例也是如此!
但是,当我基于 PWM Consumer 测试我的第一个示例时第一,我注意到 PWM Controller 的频率范围被限制在 40Hz 到 1kHz 之间。因此,第一个问题:频率范围似乎不支持。
此外,虽然 PWM Controller 属性“ActualFrequency”返回通过“SetDesiredFrequencyMethod”设置的频率,但 PWMPin 对象仅提供有关当前占空比的信息。
因此,我用谷歌搜索寻找一些答案,我找到了 this question这比前两个问题更让我困惑。
您知道是否可以以及如何使用 MS-IoT Lightning 库在 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 Controller 输出频率有软件限制,请参阅此 file .
不确定这是否有效,但值得一试的是复制闪电 repository ,修改 Max/MinFrequency 常量,构建项目,并在源项目中直接引用它,而不是引用 nuget 包。
我期待在范围内测试这种方法。
或者,不使用 Lightning 驱动程序,而是使用默认的收件箱驱动程序,其 pwm 设备驱动程序可以在 here 中找到。 ,试试看能不能用1kHz以上的更高频率。
关于c# - 如何使用 MS-IoT Lightning 设置/获取 Raspberry Pi2 的 PWM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39992201/