Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 轮询超声波传感器时出现故障';使用.net micro fw的s脉冲宽度_C#_Microcontroller_.net Micro Framework_Netduino - Fatal编程技术网

C# 轮询超声波传感器时出现故障';使用.net micro fw的s脉冲宽度

C# 轮询超声波传感器时出现故障';使用.net micro fw的s脉冲宽度,c#,microcontroller,.net-micro-framework,netduino,C#,Microcontroller,.net Micro Framework,Netduino,我下面是将C#和.net微框架与视差Ping传感器连接的众多示例之一 视差 教程1 教程2 我已将传感器正确连接到netduino plus的5伏输出、接地和gpio 0。(我尝试了不同的端口,但结果仍然相同 我遇到的问题是,我的程序检测到高脉冲,但从未检测到低脉冲。它在第二个脉冲中卡住(正确)每个教程中都有一个循环。另外,我的视差LED似乎一直亮着,根据文档,它应该随着每次ping脉冲而脉冲,而不是无限期地亮着。我用MS.SPOT.CPU.GPIO0和SecretLabs Pins.GP

我下面是将C#和.net微框架与视差Ping传感器连接的众多示例之一

视差

教程1

教程2

我已将传感器正确连接到netduino plus的5伏输出、接地和gpio 0。(我尝试了不同的端口,但结果仍然相同

我遇到的问题是,我的程序检测到高脉冲,但从未检测到低脉冲。它在第二个脉冲中卡住(正确)每个教程中都有一个循环。另外,我的视差LED似乎一直亮着,根据文档,它应该随着每次ping脉冲而脉冲,而不是无限期地亮着。我用MS.SPOT.CPU.GPIO0和SecretLabs Pins.GPIOD0枚举乱搞了一番。secret labs pin产生一个闪烁的LED,在第一次出现时永远不会出现(真)循环,而MS.SPOT端口产生一个永远的LED灯,永远不会退出第二个while(true)循环。这是我在MF中编程的第一天。有人能帮我指出可能存在的问题吗

它正在运行的视频

看起来我被连接到引脚10和13,但我没有。这个角度是误导性的。事实上我被连接到Gnd和11

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using Micro_Base_Lib;

namespace Parallax_Ping_Distance_Calculator
{
public class Program
{
    private static OutputPort onboardLed;
    private static InterruptPort onboardButton;
    public static void Main()
    {
        // write your code here
        //Cpu.GlitchFilterTime = new TimeSpan(0, 0, 0, 0, 5); //5 ms glitch filter

        onboardButton = new InterruptPort(Pins.ONBOARD_SW1, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLevelHigh);

        onboardButton.OnInterrupt += new NativeEventHandler(onboardButton_OnInterrupt);

        onboardLed = new OutputPort(Pins.ONBOARD_LED, false); //initial state off


        //go into powersave mode.
        Thread.Sleep(Timeout.Infinite);

    }

    static void onboardButton_OnInterrupt(uint data1, uint data2, DateTime time)
    {
        onboardLed.Write(true);
        onboardButton.ClearInterrupt();
        Ping myPinger = new Ping(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D11);
        var dist = myPinger.GetDistance();

        var convertedDistance = myPinger.Convert(dist, DistanceUnits.cm);
        onboardLed.Write(false);
    }

}



    public enum DistanceUnits
    {
        mm,
        cm,
        dm,
        m,
        feet,
        inch,
        yard
    }

    public class Ping
    {
        TristatePort _port;
        DistanceUnits _unit = DistanceUnits.mm;

        double _soundSpeed = 343, _convertion = (10000 / 343) * 2; // default values

        public Ping(Cpu.Pin pin)
        {

            _port = new TristatePort(pin, false, false, ResistorModes.Disabled);
        }

        /// <summary>
        /// Automaticly adjust the convertion factor depending on air temperature.
        /// </summary>
        /// <param name="degC">The temperature in degrees celsius</param>
        public void AdjustSoundSpeed(double degC)
        {
            /* Speed of Sound (at 20 degrees celsius): 343 m/s
             * or
             * _soundSpeed = 331.4 + 0.6 * degC
             * 
             * There are 10,000,000 ticks per second.
             * 10,000,000 / _soundSpeed * 1000 can be simplyfied into:
             * 10,000 / _soundSpeed
             * times it by 2 because of round trip
             * then you get about 58.309 ticks per mm
             * 
             * then multiply if other unit is needed
             * 
             */
            _soundSpeed = 331.4 + 0.6 * degC;
            _convertion = (10000 / _soundSpeed) * 2;
        }

        private void WriteToLog(string s)
        {
            Debug.Print(DateTime.Now.ToString() + " : " + s);
        }
        /// <summary>
        /// Return the Ping))) sensor's reading in millimeters.
        /// </summary>
        /// <param name="usedefault">Set true to return value in the unit specified by the "Unit" property.
        /// Set false to return value in mm.</param>
        public double GetDistance()
        {
            bool lineState = false;
            long t1, t2;

            // Set it to an output

            if (!_port.Active) 
            _port.Active = true;
            WriteToLog("Port is in output mode");
            WriteToLog(_port.Id + " : " + "Mode: " + (_port.Active == true ? "Output" : "Input"));
            WriteToLog(_port.Id + " : " + "Read: " + _port.Read());

            //10 micro seconds is long enough for the parallax microphone to pick up a a ping.
            //http://www.parallax.com/sites/default/files/downloads/28015-PING-Detect-Distance.pdf - figure 3
            WriteToLog(_port.Id + " : " + "Mode: " + (_port.Active == true ? "Output" : "Input"));
            WriteToLog(_port.Id + " : " + "Read: " + _port.Read());
            _port.Write(true); //turns the port to high 3.3v
            WriteToLog("Signal pulse started");
            WriteToLog(_port.Id + " : " + "Mode: " + (_port.Active == true ? "Output" : "Input"));
            WriteToLog(_port.Id + " : " + "Read: " + _port.Read());
            _port.Write(false); //turns the port to low 0v
            WriteToLog("Signal pulse ended");
            WriteToLog(_port.Id + " : " + "Mode: " + (_port.Active == true ? "Output" : "Input"));
            WriteToLog(_port.Id + " : " + "Read: " + _port.Read());

            // Set port it as an input to pick up sound
            _port.Active = false;
            WriteToLog("Port is in input mode");
            WriteToLog(_port.Id + " : " + "Mode: " + (_port.Active == true ? "Output" : "Input"));
            WriteToLog(_port.Id + " : " + "Read: " + _port.Read());
            //wait for linestate to change to true. indicating the start of the pulse
            WriteToLog("Waiting for line state to turn to true.");
            while (lineState == false)
            {

                lineState = _port.Read();
                WriteToLog(_port.Id + " : " + "Mode: " + (_port.Active == true ? "Output" : "Input"));
                WriteToLog(_port.Id + " : " + "Read: " + _port.Read());
            }

            t1 = System.DateTime.Now.Ticks;
            WriteToLog("Line state reached high at : " + t1.ToString());

            WriteToLog("Waiting for line state to turn to false.");
            //wait for linestate to change to false. indicating the end of the pulse
            while (lineState == true)
            {

                lineState = _port.Read();
                WriteToLog(_port.Id + " : " + "Mode: " + (_port.Active == true ? "Output" : "Input"));
                WriteToLog(_port.Id + " : " + "Read: " + _port.Read());
            }
            t2 = System.DateTime.Now.Ticks;
            WriteToLog("Line state reached low at : " + t1.ToString());
            return Convert(((t2 - t1) / _convertion), _unit);
        }

        /// <summary>
        /// Convert the millimeters into other units.
        /// </summary>
        /// <param name="millimeters">The Ping))) sensor's mm reading.</param>
        /// <param name="outputUnit">The desired output unit.</param>
        public double Convert(double millimeters, DistanceUnits outputUnit)
        {
            double result = millimeters;

            switch (outputUnit)
            {
                case DistanceUnits.cm:
                    result = millimeters * 0.1F;
                    break;
                case DistanceUnits.dm:
                    result = millimeters * 0.01F;
                    break;
                case DistanceUnits.m:
                    result = millimeters * 0.001F;
                    break;
                case DistanceUnits.inch:
                    result = millimeters * 0.0393700787;
                    break;
                case DistanceUnits.feet:
                    result = millimeters * 0.0032808399;
                    break;
                case DistanceUnits.yard:
                    result = millimeters * 0.0010936133;
                    break;
            }

            return result;
        }

        public DistanceUnits Unit
        {
            get { return _unit; }
            set { _unit = value; }
        }
    }

}
使用系统;
Net系统;
使用System.Net.Sockets;
使用系统线程;
使用Microsoft.SPOT;
使用Microsoft.SPOT.Hardware;
使用SecretLabs.NETMF.Hardware;
使用SecretLabs.NETMF.Hardware.NetduinoPlus;
使用Micro_Base_Lib;
名称空间视差\u平\u距离\u计算器
{
公共课程
{
车载专用静态输出端口;
专用静态中断端口板上按钮;
公共静态void Main()
{
//在这里编写代码
//Cpu.GlitchFilterTime=新的时间跨度(0,0,0,0,5);//5毫秒故障过滤器
onboardButton=新的中断端口(Pins.board_SW1,true,Port.ResistorMode.Disabled,Port.InterruptMode.InterruptEdgeLevelHigh);
onboardButton.OnInterrupt+=新的NativeEventHandler(onboardButton\u OnInterrupt);
onboardLed=新的输出端口(Pins.boarded_LED,false);//初始状态关闭
//进入省电模式。
Thread.Sleep(Timeout.Infinite);
}
静态无效onboardButton\u OnInterrupt(uint数据1、uint数据2、日期时间)
{
载入。写入(真);
onboardButton.ClearInterrupt();
Ping myPinger=new Ping(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D11);
var dist=myPinger.GetDistance();
var convertedDistance=myPinger.Convert(距离,距离单位.cm);
已加载。写入(错误);
}
}
公共枚举距离单位
{
嗯,
厘米
dm,
M
脚,
英寸
院子
}
公开课
{
三态端口_端口;
距离单位_单位=距离单位.mm;
double _soundSpeed=343,_conversion=(10000/343)*2;//默认值
公共Ping(Cpu.Pin)
{
_端口=新的三态端口(pin、false、false、ResistorModes.Disabled);
}
/// 
///根据空气温度自动调整转换系数。
/// 
///温度以摄氏度为单位
公共速度(双degC)
{
/*声速(20摄氏度时):343米/秒
*或
*_声速=331.4+0.6*degC
* 
*每秒有10000000个滴答声。
*10000000/_soundSpeed*1000可以简化为:
*10000/_音速
*由于往返的原因,它乘以2
*然后每毫米大约有58.309个滴答声
* 
*如果需要其他单位,则乘以
* 
*/
_声速=331.4+0.6*degC;
_转换=(10000/_音速)*2;
}
私有void WriteToLog(字符串s)
{
Debug.Print(DateTime.Now.ToString()+“:”+s);
}
/// 
///返回传感器的读数(以毫米为单位)。
/// 
///将true设置为以“unit”属性指定的单位返回值。
///将false设置为以mm为单位返回值。
公共双GetDistance()
{
bool-lineState=false;
长t1,t2;
//将其设置为输出
如果(!\u端口处于活动状态)
_port.Active=true;
WriteToLog(“端口处于输出模式”);
WriteToLog(_port.Id+”:“+”模式:“+”(_port.Active==true?“输出”:“输入”);
WriteToLog(_port.Id+”:“+”读取:“+_port.Read());
//10微秒的时间足以让视差话筒接收到“砰”的一声。
//http://www.parallax.com/sites/default/files/downloads/28015-PING-Detect-Distance.pdf -图3
WriteToLog(_port.Id+”:“+”模式:“+”(_port.Active==true?“输出”:“输入”);
WriteToLog(_port.Id+”:“+”读取:“+_port.Read());
_port.Write(true);//将端口设置为高3.3v
写入日志(“信号脉冲启动”);
WriteToLog(_port.Id+”:“+”模式:“+”(_port.Active==true?“输出”:“输入”);
WriteToLog(_port.Id+”:“+”读取:“+_port.Read());
_port.Write(false);//将端口设置为低0v
写入日志(“信号脉冲结束”);
WriteToLog(_port.Id+”:“+”模式:“+”(_port.Active==true?“输出”:“输入”);
WriteToLog(_port.Id+”:“+”读取:“+_port.Read());
//将端口设置为接收声音的输入
_port.Active=false;
WriteToLog(“端口处于输入模式”);
WriteToLog(_port.Id+”:“+”模式:“+”(_port.Active==true?“输出”:“输入”);
WriteToLog(_port.Id+”:“+”读取:“+_port.Read());
//等待linestate更改为true。指示星号
Cpu.Pin.GPIO_Pin0
Pins.GPIO_PIN_D0
Cpu.Pin.GPIO_Pin0
Pins.GPIO_PIN_D0
    pingTrig = new OutputPort(trig, false);
    pingEcho = new InterruptPort(echo, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);  // it is not really an echo
    pingEcho.OnInterrupt += (p, s, t) =>
    {
        if (s == 0)
            pingFall = t.Ticks;
        else
            pingRise = t.Ticks;
    };
    pingEcho.EnableInterrupt();
    pingTrig.Write(true);
    Thread.Sleep(1);
    pingTrig.Write(false);
    Thread.Sleep(35);      // I hate waiting
    pingEcho.DisableInterrupt();
    int Distance { get { return (int)((pingFall > pingRise) ? (pingFall - pingRise) : maxPingDistance); } }