silver2row

More MaxBotix.com Source/John

Sep 25th, 2020
1,341
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #On 7/13/15 John Bohlhuis contribute his own Python code that he is using to automate the reading of distance values.
  2. #We gladly share this for your use in your projects.
  3. #Thank you John for sharing.
  4.  
  5.  
  6. #***************************************************************************
  7. #First, the Python module that does the actual work:
  8. #***************************************************************************
  9.  
  10. #!/usr/bin/python3
  11. # Filename: range.py
  12.  
  13. # Reads serial data from Maxbotix ultrasonic rangefinders
  14. # Gracefully handles most common serial data glitches
  15. # Use as an importable module with "import MaxSonarTTY"
  16. # Returns an integer value representing distance to target in millimeters
  17.  
  18. from time import time
  19. from serial import Serial
  20.  
  21. serialDevice = "/dev/ttyS2" # Used for the BeagleBone Black Wireless (UART 2)
  22. maxwait = 3 # seconds to try for a good reading before quitting
  23.  
  24. def measure(portName):
  25.     ser = Serial(portName, 9600, 8, 'N', 1, timeout=1)
  26.     timeStart = time()
  27.     valueCount = 0
  28.  
  29.     while time() < timeStart + maxwait:
  30.         if ser.inWaiting():
  31.             bytesToRead = ser.inWaiting()
  32.             valueCount += 1
  33.             if valueCount < 2: # 1st reading may be partial number; throw it out
  34.                 continue
  35.             testData = ser.read(bytesToRead)
  36.             if not testData.startswith(b'R'):
  37.                 # data received did not start with R
  38.                 continue
  39.             try:
  40.                 sensorData = testData.decode('utf-8').lstrip('R')
  41.             except UnicodeDecodeError:
  42.                 # data received could not be decoded properly
  43.                 continue
  44.             try:
  45.                 mm = int(sensorData)
  46.             except ValueError:
  47.                 # value is not a number
  48.                 continue
  49.             ser.close()
  50.             return(mm)
  51.  
  52.     ser.close()
  53.     raise RuntimeError("Expected serial data not received")
  54.  
  55. if __name__ == '__main__':
  56.     measurement = measure(serialDevice)
  57.     print("distance =", measurement)
  58.  
  59.  
  60.  
  61.  
  62. #***************************************************************************
  63. #Also, here is a sample Python script that shows how the module may be used:
  64. #***************************************************************************
  65.  
  66.  
  67. #!/usr/bin/python3
  68. # Filename: UseRange.py
  69.  
  70. # sample script to read range values from Maxbotix ultrasonic rangefinder
  71.  
  72. from time import sleep
  73. import range
  74.  
  75. serialPort = "/dev/ttyS2"
  76. maxRange = 5000  # change for 5m opposed to 10m sensor
  77. sleepTime = 5
  78. minMM = 9999
  79. maxMM = 0
  80.  
  81. while True:
  82.     mm = range.measure(serialPort)
  83.     if mm >= maxRange:
  84.         print("no target")
  85.         sleep(sleepTime)
  86.         continue
  87.     if mm < minMM:
  88.         minMM = mm
  89.     if mm > maxMM:
  90.         maxMM = mm
  91.  
  92.     print("distance:", mm, "  min:", minMM, "max:", maxMM)
  93.     sleep(sleepTime)
RAW Paste Data