8

我正在尝试从超声波传感器(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”。跳过加载符号。模块已优化,调试器选项“仅我的代码”已启用。“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”。跳过加载符号。模块已优化,调试器选项“仅我的代码”已启用。'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”。跳过加载符号。模块已优化,调试器选项“仅我的代码”已启用。“BACKGROUNDTASKHOST.EXE”(CoreCLR:CoreCLR_UAP_Domain):已加载“C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.WindowsRuntime.dll”。跳过加载符号。模块已优化,调试器选项“仅我的代码”已启用。距离:265.7457 距离:0 距离:0 距离:0 程序“[2508] BackgroundTASKHOST.EXE”已退出,代码为 0 (0x0)。dll'。跳过加载符号。模块已优化,调试器选项“仅我的代码”已启用。距离:265.7457 距离:0 距离:0 距离:0 程序“[2508] BackgroundTASKHOST.EXE”已退出,代码为 0 (0x0)。dll'。跳过加载符号。模块已优化,调试器选项“仅我的代码”已启用。距离:265.7457 距离:0 距离:0 距离:0 程序“[2508] BackgroundTASKHOST.EXE”已退出,代码为 0 (0x0)。

4

2 回答 2

7

感谢您的反应。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;
        }

    }
}
于 2015-05-08T13:49:54.593 回答
1

有一个更好的解决方案,因为当前提出的答案在获得距离时偶尔会锁定。代码的改进版本,在 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;                
        }
    }

}
}
于 2015-11-24T01:22:19.123 回答