I would like to get the frequency and duty cycle of two PWM signals (i.e. PWM inputs) and set them for another (i.e. PWM output) depending on the inputs. These PWM signals have a duty cycle of 50%, and their frequency range is from 1 kHz to 20 kHz.
I checked the network a bit and I found the Microsoft IoT Lightning Library (i.e. bus providers) from Windows IO I Core. I seem to need this library, even with the PWM Consumer example!
However, while I was testing my first example based on the PWM Consumer , I noticed that the frequency range of the PWM controller is limited from 40 Hz to 1 kHz. Therefore, the first question: the frequency range is not supported.
Moreover, while the PWM Controller's “ActualFrequency” property returns the frequency set using the “SetDesiredFrequencyMethod”, PWMPin objects provide only information about the current duty cycle.
Therefore, I googled looking for some kind of answer, and I found this question that bothers me even more than the two previous questions.
Do you know if it is possible and how to use the MS-IoT Lightning Library to set / receive PWM signals from 1 kHz to 20 kHz on raspberry Pi2?
Here are a few lines of code from the example:
public async void Run(IBackgroundTaskInstance taskInstance)
{
if (!LightningProvider.IsLightningEnabled)
{
return;
}
var deferral = taskInstance.GetDeferral();
pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[0];
motorPin = pwmController.OpenPin(0);
secondMotorPin = pwmController.OpenPin(1);
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);
}
All the best, Lorenzo
source
share