我想獲得兩個PWM信號(即PWM輸入)的頻率和佔空比,並根據輸入將它們設置爲另一個(即PWM輸出)。這些PWM信號的佔空比爲50%,頻率範圍爲1kHz至20kHz。如何使用MS-IoT Lightning設置/獲取帶有Raspberry Pi2的PWM?
我檢查了一下網頁,發現Windows 10 IoT Core的Microsoft IoT Lightning庫(即總線提供程序)。這個庫似乎是我所需要的,即使是PWM消費者的例子!
但是,當我測試基於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);
}
一切順利,洛倫佐
Ok @Servé,我很喜歡那個RPi(即樹莓派)不是一個實時系統。那麼,您將如何使用MS-IoT(即Microsoft IoT Lightning)閃電?它的目的是什麼? –
我不知道那個圖書館。通過查看源代碼看起來它是爲Arduino製作的。也許別人可以幫助 – Laurijssen
讓我們來看看是否有人可以幫助;) –