Пример #1
0
        /* ********************************* */
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="pinTrig">Pin connected to the HC-SR04 Trig pin</param>
        /// <param name="pinEcho">Pin connected to the HC-SR04 Echo pin</param>
        public HC_SR04(int pinTrig, int pinEcho, int CounterInputPin)
        {
            var gpioController = GpioController.GetDefault();

            // Initialise count pin by opening GPIO as input
            countPin = gpioController.OpenPin(CounterInputPin);
            countPin.SetDriveMode(GpioPinDriveMode.InputPullUp);

            // Create a Counter passing in the GPIO pin
            gpcc          = new GpioChangeCounter(countPin);
            gpcc.Polarity = GpioChangePolarity.Rising;
            //{
            //    // Counter on raising edges
            //    Polarity = GpioChangePolarity.Rising
            //};
            gpcc.Start();
            //** Initialize HC-SR04 Related pins **
            portOut = gpioController.OpenPin(pinTrig);
            portOut.SetDriveMode(GpioPinDriveMode.Output);

            interIn = gpioController.OpenPin(pinEcho);
            interIn.SetDriveMode(GpioPinDriveMode.InputPullUp);

            interIn.ValueChanged += InterIn_OnInterrupt;

            LatencyTicks     = 0L; // Was 6200L -> Aprox. 92.57mm (5287L latency) + 16mm (913L) offset from sensor grill;
            ConversionFactor = 1;  //57.1056; // for Cm  (was 58.3) //1440.0;  for inches.

            Version = 1.6;
        }
Пример #2
0
 public WaterFlowSensorDriver(GpioController gpioController, GpioPins pin)
 {
     _gpioController = gpioController;
     _pin            = _gpioController.OpenPin((int)pin);
     _pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
     _pulseCounter = new GpioChangeCounter(_pin)
     {
         Polarity = GpioChangePolarity.Rising
     };
 }
 public SparkFunAnemometerDriver(GpioController gpioController, GpioPins pin)
 {
     _gpioController = gpioController;
     _pin            = _gpioController.OpenPin((int)pin);
     _pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
     _pulseCounter = new GpioChangeCounter(_pin)
     {
         Polarity = GpioChangePolarity.Falling
     };
 }
Пример #4
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");
            }
        }