Ejemplo n.º 1
0
        //  RefOsc = new Reference(); // (23, 22);

        //FrequencyGate.Reference();
        //  RefOsc.PWMFrequency = 10000;
        //RefOsc.RefFrequencyStart();
        /// <summary>
        /// Trigger a sensor reading
        /// Convert ticks to distance using TicksToDistance below
        /// </summary>
        /// <returns>Number of ticks it takes to get back sonic pulse</returns>
        long Ping()
        {
            // Reset Sensor
            endTick = 0L;
            GpioChangeCount btick = gpcc.Reset();

            Console.WriteLine("BeginTick After reset = " + beginTick.ToString());
            btick = gpcc.Read();
            Console.WriteLine("BeginTick = " + beginTick.ToString());

            beginTick = btick.Count;

            portOut.Write(GpioPinValue.High);
            // Thread.Sleep(1);
            // Trigger Sonic Pulse
            portOut.Write(GpioPinValue.Low);

            // Start Clock


            // Wait 1/20 second (this could be set as a variable instead of constant)
            Thread.Sleep(50);

            endTick = etick.Count;
            Console.WriteLine("EndTick = " + endTick.ToString());
            if (endTick > 0L)
            {
                // Calculate Difference
                long elapsed = (long)(endTick - beginTick);

                // Subtract out fixed overhead (interrupt lag, etc.)
                elapsed -= LatencyTicks;
                Console.WriteLine("Elapsed = " + elapsed.ToString());
                if (elapsed < 0L)
                {
                    elapsed = 0L;
                }

                // Return elapsed ticks
                return(elapsed);
            }

            // Sonic pulse wasn't detected within 1/20 second
            return(-1L);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This interrupt will trigger when detector receives back reflected sonic pulse
        /// </summary>
        /// <param name="data1">Not used</param>
        /// <param name="data2">Not used</param>
        /// <param name="time">Transfer to endTick to calculated sound pulse travel time</param>
        void InterIn_OnInterrupt(object sender, GpioPinValueChangedEventArgs e)
        {
            // if (gpcc.IsStarted) { gpcc.Stop(); }

            // save the ticks when pulse was received back
            //if (e.Edge == GpioPinEdge.RisingEdge)
            //{
            //    beginTick = (long)gpcc.Read().Count;
            //   // Console.WriteLine("begintick = " + beginTick.ToString());
            //};

            if (e.Edge == GpioPinEdge.FallingEdge)
            {
                etick = gpcc.Read();
                //endTick = (long)gpcc.Read().Count;
                //Console.WriteLine("EndTick = " + endTick.ToString());
            }
            ;
            //GpioChangeCount etick = gpcc.Reset();
            // endTick = etick.Count;
        }
Ejemplo n.º 3
0
        public static void Main()
        {
            Console.WriteLine("Change Counter test running");

            // Initialise PWM output pin
            PwmController pwmc = PwmController.GetDefault();

            pwmc.SetDesiredFrequency(PWM_FREQUENCY);

            PwmPin pwmTestPin = pwmc.OpenPin(PWM_OUTPUT_PIN);

            pwmTestPin.SetActiveDutyCyclePercentage(0.5);

            Console.WriteLine($"Open PWM pin {PWM_OUTPUT_PIN} frequency {pwmc.ActualFrequency}");
            Console.WriteLine($"This pin must be connected to GpioChangeCounter pin {COUNTER_INPUT_PIN}");


            // Initialise count pin by opening GPIO as input
            GpioPin countPin = GpioController.GetDefault().OpenPin(COUNTER_INPUT_PIN);

            countPin.SetDriveMode(GpioPinDriveMode.InputPullUp);

            // Create a Counter passing in the GPIO pin
            GpioChangeCounter gpcc = new GpioChangeCounter(countPin);

            // Counter both raising and falling edges
            gpcc.Polarity = GpioChangePolarity.Both;

            Console.WriteLine($"Counter pin {COUNTER_INPUT_PIN} created");

            // Start counter
            gpcc.Start();

            // Read count before we start PWM ( should be 0 )
            // We want to save the start relative time
            GpioChangeCount count1 = gpcc.Read();

            // Start PWM signal
            pwmTestPin.Start();

            // Wait 1 Sec
            Thread.Sleep(1000);

            // Read current count
            GpioChangeCount count2 = gpcc.Read();

            // Stop PWM signal & counter
            pwmTestPin.Stop();
            gpcc.Stop();


            // Change polarity of counter so only counting rising edges
            gpcc.Polarity = GpioChangePolarity.Rising;

            gpcc.Start();
            GpioChangeCount count3 = gpcc.Reset();

            pwmTestPin.Start();

            // Wait 1 Sec
            Thread.Sleep(1000);

            pwmTestPin.Stop();

            // Read count
            GpioChangeCount count4 = gpcc.Read();

            gpcc.Stop();

            DisplayResults("Count pulses for 1 second with both edges", count1, count2);
            DisplayResults("Count pulses for 1 second with just rising edge", count3, count4);

            // Next test tries to measure the frequncy of the PWM signal
            pwmTestPin.Start();
            gpcc.Start();

            while (true)
            {
                // Reset Counter to zero
                GpioChangeCount countStart = gpcc.Reset();

                Thread.Sleep(1000);

                // Wait 1 sec and read again
                GpioChangeCount countEnd = gpcc.Read();

                // Sleep is not accurate so calculate actual time in secounds based of relative time differences of the 2 counts
                // Ticks are in 100 nano sec increments
                double periodSecs = (double)(countEnd.RelativeTime.Ticks - countStart.RelativeTime.Ticks) / 10000000000.0;
                int    frequecy   = (int)((double)countEnd.Count / periodSecs);

                Console.WriteLine($"Period {periodSecs:F6} Sec | Frequency {frequecy} Hz");
            }
        }
Ejemplo n.º 4
0
        static void DisplayResults(string text, GpioChangeCount start, GpioChangeCount end)
        {
            TimeSpan period = end.RelativeTime - start.RelativeTime;

            Console.WriteLine($"Count {text} Time:{period} Count:{end.Count}");
        }