2016-10-12 32 views
4

我想獲得兩個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); 
    } 

一切順利,洛倫佐

回答

0

樹莓PI是不是一個實時系統(它具有高速緩存和分支預測),因此它不能像這樣處理PWM信號。當指令導致緩存刷新時,它不能準確測量微秒時間,或者如果語句未能預測它們,它就不能準確測量微秒時間。 微軟IoT Lightning以arduino爲例子是實時的。

+0

Ok @Servé,我很喜歡那個RPi(即樹莓派)不是一個實時系統。那麼,您將如何使用MS-IoT(即Microsoft IoT Lightning)閃電?它的目的是什麼? –

+0

我不知道那個圖書館。通過查看源代碼看起來它是爲Arduino製作的。也許別人可以幫助 – Laurijssen

+0

讓我們來看看是否有人可以幫助;) –

1

物聯網防雷架構似乎對PWM控制器輸出頻率有軟件限制,請參閱此file

不知道這是否可行,但值得一試的是製作閃電的克隆repository,修改最大/最小頻率常數,構建項目並在源項目中直接引用它,而不是引用nuget包。

我期待這種方法正在與範圍進行測試。

或者,使用默認的收件箱驅動程序(可在here中找到該驅動程序的pwm設備驅動程序),而不是使用Lightning驅動程序,試着查看是否可以使用1kHz以上的更高頻率。

+0

感謝您的BCM驅動程序鏈接! 現在,我通過傳遞給linux並利用[PiGPIO庫](http://abyz.co.uk/rpi/pigpio/),守護進程和python接口來解決我的問題。因此,我能夠從外部源讀取並生成PWM。 –

相關問題