[英]ultrasonic sensor raspberry pi 2 c# .net
我正在嘗試讀取超聲波傳感器(HC-SR04)的距離,但我得到的唯一值是0和265.xx.
我正在使用安裝了Windows 10 IoT Core的Raspberry Pi 2。
我用C#編寫了代碼。
這是超聲波傳感器類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Windows.Devices.Gpio;
namespace RaspberryPi
{
class UcSensor
{
GpioController gpio = GpioController.GetDefault();
GpioPin TriggerPin;
GpioPin EchoPin;
//Contructor
public UcSensor(int TriggerPin, int EchoPin)
{
//Setting up gpio pin's
this.TriggerPin = gpio.OpenPin(TriggerPin);
this.EchoPin = gpio.OpenPin(EchoPin);
this.TriggerPin.SetDriveMode(GpioPinDriveMode.Output);
this.EchoPin.SetDriveMode(GpioPinDriveMode.Input);
this.TriggerPin.Write(GpioPinValue.Low);
}
public double GetDistance()
{
ManualResetEvent mre = new ManualResetEvent(false);
mre.WaitOne(500);
//Send pulse
this.TriggerPin.Write(GpioPinValue.High);
mre.WaitOne(TimeSpan.FromMilliseconds(0.01));
this.TriggerPin.Write(GpioPinValue.Low);
//Recieve pusle
while (this.EchoPin.Read() == GpioPinValue.Low)
{
}
DateTime start = DateTime.Now;
while (this.EchoPin.Read() == GpioPinValue.High)
{
}
DateTime stop = DateTime.Now;
//Calculating distance
double timeBetween = (stop - start).TotalSeconds;
double distance = timeBetween * 17000;
return distance;
}
}
}
我還在python中編寫了一個腳本來讀取超聲波傳感器的值,然后它可以正常工作但是在c#中我無法正常工作。
在底部,您可以找到調試日志:
'BACKGROUNDTASKHOST.EXE'(CoreCLR:DefaultDomain):已加載'C:\\ Program Files \\ WindowsApps \\ Microsoft.NET.CoreRuntime.1.0_1.0.22816.1_arm__8wekyb3d8bbwe \\ mscorlib.ni.dll'。 跳過加載符號。 模塊已經過優化,調試器選項“Just My Code”已啟用。 'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加載'C:\\ Users \\ DefaultAccount \\ AppData \\ Local \\ DevelopmentFiles \\ RaspiCarVS.Debug_ARM.chris \\ RaspiCar.winmd'。 符號已加載。 'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加載'C:\\ Users \\ DefaultAccount \\ AppData \\ Local \\ DevelopmentFiles \\ RaspiCarVS.Debug_ARM.chris \\ System.Runtime.dll'。 跳過加載符號。 模塊已經過優化,調試器選項“Just My Code”已啟用。 'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加載'C:\\ Users \\ DefaultAccount \\ AppData \\ Local \\ DevelopmentFiles \\ RaspiCarVS.Debug_ARM.chris \\ WinMetadata \\ Windows.winmd'。 模塊沒有符號。 'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加載'C:\\ Users \\ DefaultAccount \\ AppData \\ Local \\ DevelopmentFiles \\ RaspiCarVS.Debug_ARM.chris \\ System.Runtime.InteropServices.WindowsRuntime.dll'。 模塊沒有符號。 'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加載'C:\\ Users \\ DefaultAccount \\ AppData \\ Local \\ DevelopmentFiles \\ RaspiCarVS.Debug_ARM.chris \\ System.Threading.dll'。 模塊沒有符號。 'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加載'C:\\ Users \\ DefaultAccount \\ AppData \\ Local \\ DevelopmentFiles \\ RaspiCarVS.Debug_ARM.chris \\ System.Diagnostics.Debug.dll'。 跳過加載符號。 模塊已經過優化,調試器選項“Just My Code”已啟用。 'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加載'C:\\ Users \\ DefaultAccount \\ AppData \\ Local \\ DevelopmentFiles \\ RaspiCarVS.Debug_ARM.chris \\ System.Runtime.WindowsRuntime.dll'。 跳過加載符號。 模塊已經過優化,調試器選項“Just My Code”已啟用。 距離:265.7457距離:0距離:0距離:0節目'[2508] BACKGROUNDTASKHOST.EXE'已退出,代碼為0(0x0)。
謝謝你的反應。 DateTime是我現在使用秒表類的問題,現在它可以工作了。 非常感謝!
工人階級:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Windows.Devices.Gpio;
namespace RaspberryPi
{
class UcSensor
{
GpioController gpio = GpioController.GetDefault();
GpioPin TriggerPin;
GpioPin EchoPin;
public UcSensor(int TriggerPin, int EchoPin)
{
this.TriggerPin = gpio.OpenPin(TriggerPin);
this.EchoPin = gpio.OpenPin(EchoPin);
this.TriggerPin.SetDriveMode(GpioPinDriveMode.Output);
this.EchoPin.SetDriveMode(GpioPinDriveMode.Input);
this.TriggerPin.Write(GpioPinValue.Low);
}
public double GetDistance()
{
ManualResetEvent mre = new ManualResetEvent(false);
mre.WaitOne(500);
Stopwatch pulseLength = new Stopwatch();
//Send pulse
this.TriggerPin.Write(GpioPinValue.High);
mre.WaitOne(TimeSpan.FromMilliseconds(0.01));
this.TriggerPin.Write(GpioPinValue.Low);
//Recieve pusle
while (this.EchoPin.Read() == GpioPinValue.Low)
{
}
pulseLength.Start();
while (this.EchoPin.Read() == GpioPinValue.High)
{
}
pulseLength.Stop();
//Calculating distance
TimeSpan timeBetween = pulseLength.Elapsed;
Debug.WriteLine(timeBetween.ToString());
double distance = timeBetween.TotalSeconds * 17000;
return distance;
}
}
}
有一個更好的解決方案,因為目前提出的答案有時會在獲得距離時鎖定。 代碼的改進版本,在100毫秒(硬編碼)后超時。 您可以返回null或0.0。 用雙? 如果你想返回null。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Gpio;
namespace MTP.IoT.Devices.Sensors
{
public class HCSR04
{
private GpioPin triggerPin { get; set; }
private GpioPin echoPin { get; set; }
private Stopwatch timeWatcher;
public HCSR04(int triggerPin, int echoPin)
{
GpioController controller = GpioController.GetDefault();
timeWatcher = new Stopwatch();
//initialize trigger pin.
this.triggerPin = controller.OpenPin(triggerPin);
this.triggerPin.SetDriveMode(GpioPinDriveMode.Output);
this.triggerPin.Write(GpioPinValue.Low);
//initialize echo pin.
this.echoPin = controller.OpenPin(echoPin);
this.echoPin.SetDriveMode(GpioPinDriveMode.Input);
}
public double GetDistance()
{
ManualResetEvent mre = new ManualResetEvent(false);
mre.WaitOne(500);
timeWatcher.Reset();
//Send pulse
this.triggerPin.Write(GpioPinValue.High);
mre.WaitOne(TimeSpan.FromMilliseconds(0.01));
this.triggerPin.Write(GpioPinValue.Low);
return this.PulseIn(echoPin, GpioPinValue.High);
}
private double PulseIn(GpioPin echoPin, GpioPinValue value)
{
var t = Task.Run(() =>
{
//Recieve pusle
while (this.echoPin.Read() != value)
{
}
timeWatcher.Start();
while (this.echoPin.Read() == value)
{
}
timeWatcher.Stop();
//Calculating distance
double distance = timeWatcher.Elapsed.TotalSeconds * 17000;
return distance;
});
bool didComplete = t.Wait(TimeSpan.FromMilliseconds(100));
if(didComplete)
{
return t.Result;
}
else
{
return 0.0;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.