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