Exemple #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);
        }
Exemple #2
0
        public void StartMeasurement(WaterFlowSensorMeasurementResolution measurementResolution)
        {
            if (_measuringThread.IsAlive)
            {
                //don't start new measuring when previous is pending
                throw new InvalidOperationException(
                          "Cannot start measurement because another measurement process is in progress.");
            }

            _isMeasuring = true;

            _measuringThread = new Thread(() =>
            {
                _pulseCounter.Start();

                while (_isMeasuring)
                {
                    Thread.Sleep((int)measurementResolution * 1000);
                    var pulseCount = _pulseCounter.Read();
                    _pulseCounter.Stop();
                    _pulseCounter.Reset();
                    _pulseCounter.Start();

                    _currentFlow =
                        (ushort)(pulseCount.Count / (double)measurementResolution);

                    if (_currentFlow > MAX_REAL_FLOW)
                    {
                        continue;
                    }

                    _totalFlow += (ushort)pulseCount.Count;

                    _averageFlow = (_averageFlow * _measurementCounter + _currentFlow) /
                                   ++_measurementCounter;

                    //Logger.Log(() => $"Current flow: {_currentFlow / 5f} [l/min]");
                    //Logger.Log(() => $"Total flow: {_totalFlow / 5f / 60f} [l]");

                    if (_currentFlow > _maxFlow)
                    {
                        _maxFlow = _currentFlow;
                    }

                    if (_currentFlow < _minFlow)
                    {
                        _minFlow = _currentFlow;
                    }
                }

                _pulseCounter.Stop();
                _pulseCounter.Reset();
            });

            _measuringThread.Start();
        }
        public void StartMeasurement(AnemometerMeasurementResolution measurementResolution)
        {
            if (_measuringThread.IsAlive)
            {
                //don't start new measuring when previous is pending
                throw new InvalidOperationException(
                          "Cannot start measurement because another measurement process is in progress.");
            }

            _isMeasuring = true;

            _measuringThread = new Thread(() =>
            {
                _pulseCounter.Start();

                while (_isMeasuring)
                {
                    _pulseCounter.Reset();
                    Thread.Sleep((int)measurementResolution * 1000);
                    var pulseCount = _pulseCounter.Read();

                    _currentWindSpeed = (pulseCount.Count / (double)measurementResolution * ONE_HZ_PULSE_SPEED);

                    if (_currentWindSpeed > MAX_REAL_WIND_SPEED)
                    {
                        continue;
                    }

                    _averageWindSpeed = (_averageWindSpeed * _measurementCounter + _currentWindSpeed) /
                                        ++_measurementCounter;

                    Logger.Log(() => $"Current wind tick: {pulseCount.Count}");
                    Logger.Log(() => $"Current wind speed: {_currentWindSpeed } [m/s]");

                    if (_currentWindSpeed > _maxWindSpeed)
                    {
                        _maxWindSpeed = _currentWindSpeed;
                    }

                    if (_currentWindSpeed < _minWindSpeed)
                    {
                        _minWindSpeed = _currentWindSpeed;
                    }
                }

                _pulseCounter.Stop();
                _pulseCounter.Reset();
            });

            _measuringThread.Start();
        }
Exemple #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");
            }
        }