gpt4 book ai didi

c# - 超声波传感器 raspberry pi 2 c# .net

转载 作者:太空狗 更新时间:2023-10-29 20:20:14 25 4
gpt4 key购买 nike

我正在尝试从超声波传感器 (HC-SR04) 读取距离,但我得到的唯一值是 0 和 265.xx。

我正在使用安装了 Windows 10 IoT 核心版的 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): Loaded 'C:\Program Files\WindowsApps\Microsoft.NET.CoreRuntime.1.0_1.0.22816.1_arm__8wekyb3d8bbwe\mscorlib.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\RaspiCar.winmd'. Symbols loaded. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\WinMetadata\Windows.winmd'. Module was built without symbols. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.InteropServices.WindowsRuntime.dll'. Module was built without symbols. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Threading.dll'. Module was built without symbols. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Diagnostics.Debug.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.WindowsRuntime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. Distance: 265.7457 Distance: 0 Distance: 0 Distance: 0 The program '[2508] BACKGROUNDTASKHOST.EXE' has exited with code 0 (0x0).

最佳答案

感谢您的 react 。 DateTime 是我现在使用秒表类的问题,现在它可以工作了。非常感谢!

worker 类(Class):

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;
}

}
}

关于c# - 超声波传感器 raspberry pi 2 c# .net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30124861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com