我正在同时包含伺服电机和超声波传感器的项目中工作,但是当我在Direct Memory Mapped驱动程序中设置了树莓派时,伺服电机可以完美工作;可以,但是超声波检测无法用我想要的引脚将GpioChangeReader生成为新的。 .i尝试了所有可能的方式,但没有用...
using Microsoft.IoT.Lightning.Providers;
using System;
using System.Diagnostics;
using Windows.Devices;
using Windows.Devices.Gpio;
using Windows.Foundation;
namespace pi4
{
public class UltrasonicSensor
{
private const int PIN_DRIVER = 27;
private const int PIN_ECHO = 17;
private GpioPin driverPin;
private GpioPin echoPin;
private GpioController gpioController;
private GpioChangeReader changeReader;
private double lastDelay = 0.0;
private Stopwatch stopwatch;
public UltrasonicSensor()
{
InitGpioAsync();
}
private async System.Threading.Tasks.Task InitGpioAsync()
{
if (LightningProvider.IsLightningEnabled)
{
LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
}
// gpioController = await GpioController.GetDefaultAsync();
gpioController = GpioController.GetDefault();
if (gpioController == null)
{
return;
}
try
{
driverPin = gpioController.OpenPin(PIN_DRIVER,GpioSharingMode.Exclusive);
echoPin = gpioController.OpenPin(PIN_ECHO, GpioSharingMode.Exclusive);
driverPin.Write(GpioPinValue.Low);
driverPin.SetDriveMode(GpioPinDriveMode.Output);
if (echoPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
{
echoPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
}
else
{
echoPin.SetDriveMode(GpioPinDriveMode.Input);
}
changeReader = new GpioChangeReader(echoPin);//here is the error coming
changeReader.Polarity = GpioChangePolarity.Both; // one measurement results in one rising and one falling edge
changeReader.Start();
// we use the stopwatch to time the trigger pulse
stopwatch = Stopwatch.StartNew();
stopwatch.Start();
}
catch (Exception r1) {
}
}
public void Trigger()
{
changeReader.Clear();
var a = changeReader.WaitForItemsAsync(2); // two edges, measure the time between
a.Completed = GpioValueChanged;
// trigger an echo
driverPin.Write(GpioPinValue.High);
Wait(0.1); // datasheet says 10us would do but this too is fine
driverPin.Write(GpioPinValue.Low);
}
void GpioValueChanged(IAsyncAction action, AsyncStatus status)
{
var events = changeReader.GetAllItems();
if (events.Count < 2)
{
// fail silently
return;
}
var first = events[0];
var second = events[1];
lastDelay = (second.RelativeTime - first.RelativeTime).TotalSeconds;
}
public double GetCentimeters()
{
return 100.0 * lastDelay * 340.0 / 2.0; // sound speed 340m/s, divide by two as it's both ways
}
// busy wait for milliseconds
// source: http://www.iot-developer.net/windows-iot/uwp-programming-in-c/timer-and-timing/stopwatch
private void Wait(double milliseconds)
{
long initialTick = stopwatch.ElapsedTicks;
long initialElapsed = stopwatch.ElapsedMilliseconds;
double desiredTicks = milliseconds / 1000.0 * Stopwatch.Frequency;
double finalTick = initialTick + desiredTicks;
while (stopwatch.ElapsedTicks < finalTick) ; // busy wait
}
}
}
capabilities?
<iot:Capability Name="lowLevelDevices" />
<DeviceCapability Name="109b86ad-f53d-4b76-aa5f-821e2ddf2141"/>
请参考How to add the library LightingProvider to your application。如果仍然无法解决问题,则可以尝试使用默认的收件箱驱动程序来测试问题。