Advertisement
rikisu_

Untitled

Jul 13th, 2023 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. import time
  2. import math
  3. import RPi.GPIO as GPIO
  4.  
  5. VL53LOX_InterruptPin = 6
  6. VL53LOX_ShutdownPin = 9
  7. VL53LOX_State = GPIO.LOW
  8.  
  9. NUM_SAMPLES = 100  # Number of samples for the EMA filter
  10. SMOOTHING_FACTOR = 0.1  # Smoothing factor for the EMA filter
  11. smoothed_value = 0.0
  12.  
  13. def VL53LOXISR(channel):
  14.     global VL53LOX_State
  15.     VL53LOX_State = GPIO.input(VL53LOX_InterruptPin)
  16.  
  17. GPIO.setmode(GPIO.BCM)
  18. GPIO.setup(VL53LOX_ShutdownPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  19. GPIO.setup(VL53LOX_InterruptPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  20. GPIO.add_event_detect(VL53LOX_InterruptPin, GPIO.BOTH, callback=VL53LOXISR)
  21.  
  22. print("VL53L0X API Interrupt Ranging example\n")
  23.  
  24. # Simulation of VL53L0X initialization
  25. time.sleep(1)
  26.  
  27. print("Start Measurement...")
  28.  
  29. while True:
  30.     if VL53LOX_State == GPIO.LOW:
  31.         # Simulation of VL53L0X measurement
  32.         range_mm = 40 + 40 * math.sin(time.time())
  33.  
  34.         # Apply EMA filter
  35.         smoothed_value = (SMOOTHING_FACTOR * range_mm) + ((1 - SMOOTHING_FACTOR) * smoothed_value)
  36.  
  37.         print(smoothed_value)
  38.  
  39.     time.sleep(0.01)  # Maintain the same output rate
  40.  
  41. GPIO.cleanup()
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement