5

私は、C# と .net マイクロ フレームワークを Parallax Ping センサーとインターフェイスさせる多くの例の 1 つに従っています。

パララックス
http://learn.parallax.com/KickStart/28015

チュートリアル 1
https://www.ghielectronics.com/community/codeshare/entry/123

チュートリアル 2
http://forums.netduino.com/index.php?/topic/1721-parallax-ping-ultrasonic-sensor/

センサーを netduino plus の 5v 出力、アース、および gpio 0 に正しく配線しました (別のポートを試しましたが、結果は同じです。

私が抱えている問題は、プログラムが高パルスを検出するが、低パルスを検出しないことです。これらの各チュートリアルにある 2 番目の while(true) ループでスタックします。また、私の視差の LED は点灯したままのようで、ドキュメントによると、無期限に点灯するのではなく、各 ping バーストでパルスする必要があります。私は MS.SPOT.CPU.GPIO0 と SecretLabs Pins.GPIOD0 列挙型をいじりました。secret labs ピンは、最初の while(true) ループを終了しない LED の点滅を生成し、MS.SPOT ポートは、2 番目の while(true) ループを終了しない永続的な LED ライトを生成します。これは、MF でのプログラミング初日です。何が問題なのかを指摘するのを手伝ってくれる人はいますか?

配線

実際のビデオhttps://dl.dropboxusercontent.com/u/62170850/20140606_153029.mp4

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

}

06/01/2011 00:00:34: ポートは出力モードです
06/01/2011 00:00:34: 17: モード: 出力
06/01/2011 00:00:34: 17: 読み取り: False
06/ 01/2011 00:00:34 : 17 : モード: 出力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 信号パルス開始 06/01/2011
00 :00:34 : 17 : モード: 出力
06/01/2011 00:00:34 : 17 : 読み取り: True
06/01/2011 00:00:34 : 信号パルス終了
06/01/2011 00:00:34 : 17 : モード: 出力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : ポートは入力モード
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34: 17: 読み取り: False
06/01/2011 00:00:34: ライン状態が真に変わるのを待っています。
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/ 01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/ 2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力 06/01/2011
00 :00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False 06/01/2011
00:00 :34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/ 01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/ 2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力 06/01/2011
00 :00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False 06/01/2011
00:00 :34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:34 : 17 : モード: 入力
06/ 01/2011 00:00:34 : 17 : 読み取り: False
06/01/2011 00:00:35 : 17 : モード: 入力

4

4 に答える 4

1

トリックは、実際にはエコー信号ではなく、返されたエコーを表すタイミング信号であるということです。実際には、平均で 8 つの個別のエコーがあると思います。

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

どこか。

netduino では、ハードウェアが割り込みを受け取りますが、後で受け取るイベントをキューに入れます。幸いタイムスタンプ付きです。

また、別の割り込みルーチンからそれを行うのは非常に悪い考えです。ボタンの押下を処理することを示すフラグを設定し、割り込みから抜け出します。次に、通常のメイン ループで、ping 作業を行います (パルス トリガー、エコーを待ちます)。技術的には、実際には割り込みルーチンにいるわけではありませんが、「グッド プラクティス」の観点からは、割り込みルーチンにいるように扱う方が理にかなっていると思います。

于 2014-09-11T19:58:21.323 に答える
1

おそらく少し遅れていますが、最初のリンクにある SIG に 1K の抵抗が見当たりません。アンペア数が原因で問題が発生する可能性があります。

于 2015-03-17T23:15:24.567 に答える
0

Pingセンサーでプルアップ抵抗を使用することをお勧めします.これらの超音波センサーはすべてほぼ同じデバイスです.1970年代のポラロイドSX-70ランドカメラは距離を決定するために使用していました.

于 2015-05-31T18:28:08.040 に答える