Advertisement
silver2row

EZ2 SONAR and BBBW

Sep 20th, 2019
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. # This Code uses the:
  2. # * Maxbotic LV-EZ2 Ultrasonic Sensor
  3.  
  4. # Tested with the BBBW
  5. # This sketch reads the LV-EZ2 by pulse count
  6. # Then prints the distance to the python console
  7.  
  8. # The circuit:
  9. # * 3.3v on the BBBW to LV-EZ2 Pin 7 Vcc
  10. # * GND on BBBW to the LV-EZ2 Pin 6 GND
  11. # * LV-EZ1 Ultrasonic Sensor PW pin to BBBW P9_14
  12. # * LV-EZ2 Sensor PWM usage!
  13.  
  14. import time
  15. import Adafruit_BBIO.PWM as PWM
  16. #import Adafruit_BBIO.UART as UART
  17. import serial
  18.  
  19. # ez2pin = "P9_12"
  20. PWM2 = "P9_14"
  21. # UART.setup("UART2")
  22.  
  23. # GPIO.setup(ez2pin, "GPIO.OUT")
  24. PWM.start(PWM2, 50, 2000, 1)
  25.  
  26. # calculated mode or median distance
  27. mode_result = 0
  28.  
  29. # storing multiple pulses
  30. # read in time for pin to transition
  31. samples = 18
  32. pulses = (PWM2, maxlen=samples)
  33.  
  34. # sensor reads which are in range will be stored here
  35. rangevalue = [0, 0, 0, 0, 0, 0, 0, 0, 0]
  36.  
  37. # 1s sensor power up pause
  38. time.sleep(1)
  39.  
  40.  
  41. def tof_cm(time_of_flight):
  42.     # EZ1 ultrasonic sensor is measuring "time of flight"
  43.     # Converts time of flight into distance in centimeters
  44.     convert_to_cm = 58
  45.     cm = time_of_flight / convert_to_cm
  46.  
  47.     return cm
  48.  
  49.  
  50. def tof_inches(time_of_flight):
  51.     # EZ1 ultrasonic sensor is measuring "time of flight"
  52.     # Converts time of flight into distance in inches
  53.     convert_to_inches = 147
  54.     inches = time_of_flight / convert_to_inches
  55.  
  56.     return inches
  57.  
  58.  
  59. def find_mode(x):
  60.     # find the mode (most common value reported)
  61.     # will return median (center of sorted list)
  62.     # should mode not be found
  63.     n = len(x)
  64.  
  65.     max_count = 0
  66.     mode = 0
  67.     bimodal = 0
  68.     counter = 0
  69.     index = 0
  70.  
  71.     while index < (n - 1):
  72.         prev_count = counter
  73.         counter = 0
  74.  
  75.         while (x[index]) == (x[index + 1]):
  76.             counter += 1
  77.             index += 1
  78.  
  79.         if (counter > prev_count) and (counter > max_count):
  80.             mode = x[index]
  81.             max_count = counter
  82.             bimodal = 0
  83.  
  84.         if counter == 0:
  85.             index += 1
  86.  
  87.         # If the dataset has 2 or more modes.
  88.         if counter == max_count:
  89.             bimodal = 1
  90.  
  91.         # Return the median if there is no mode.
  92.         if (mode == 0) or (bimodal == 1):
  93.             mode = x[int(n / 2)]
  94.  
  95.         return mode
  96.  
  97.  
  98. while True:
  99.  
  100.     # wait between samples
  101.     time.sleep(.5)
  102.  
  103.     if len(pulses) == samples:
  104.         j = 0  # rangevalue array counter
  105.  
  106.         # only save the values within range
  107.         # range readings take 49mS
  108.         # pulse width is .88mS to 37.5mS
  109.         for i in range(0, samples):
  110.             tof = pulses[i]  # time of flight - PWM HIGH
  111.  
  112.             if 880 < tof < 37500:
  113.                 if j < len(rangevalue):
  114.                     rangevalue[j] = tof_cm(tof)
  115.                     j += 1
  116.  
  117.         # clear pulse samples
  118.         pulses.clear()  # clear all values in pulses[]
  119.  
  120.         # sort samples
  121.         rangevalue = sorted(rangevalue)
  122.  
  123.         # returns mode or median
  124.         mode_result = int(find_mode(rangevalue))
  125.  
  126.         # python console prints both centimeter and inches distance
  127.         cm2in = .393701
  128.         mode_result_in = mode_result * cm2in
  129.         print(mode_result, "cm", "\t\t", int(mode_result_in), "in")
  130.  
  131.         # result must be in char/string format for LCD printing
  132.         # digit_string = str(mode_result)
  133.  
  134.         time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement