SHOW:
|
|
- or go back to the newest paste.
1 | - | On 7/13/15 John Bohlhuis contribute his own Python code that he is using to automate the reading of distance values. |
1 | + | # maxSonarTTY.py |
2 | - | We gladly share this for your use in your projects. |
2 | + | |
3 | - | Thank you John for sharing. |
3 | + | import serial |
4 | import re | |
5 | ||
6 | - | *************************************************************************** |
6 | + | class MaxSonar( serial.Serial ): |
7 | - | First, the Python module that does the actual work: |
7 | + | def __init__( self, port ): |
8 | - | *************************************************************************** |
8 | + | super().__init__( port=port, baudrate=9600, timeout=0.5 ) |
9 | ||
10 | - | #!/usr/bin/python3 |
10 | + | def measure( self ): |
11 | - | # Filename: maxSonarTTY.py |
11 | + | self.reset_input_buffer() |
12 | ||
13 | - | # Reads serial data from Maxbotix ultrasonic rangefinders |
13 | + | data = self.read_until( b'\r', size=5 ) |
14 | - | # Gracefully handles most common serial data glitches |
14 | + | |
15 | - | # Use as an importable module with "import MaxSonarTTY" |
15 | + | if re.fullmatch( rb'\d*\r', data ): |
16 | - | # Returns an integer value representing distance to target in millimeters |
16 | + | # partial line received, wait for next one |
17 | data = self.read_until( b'\r', size=5 ) | |
18 | - | from time import time |
18 | + | |
19 | - | from serial import Serial |
19 | + | if len( data ) < 5 and not data.endswith( b'\r' ): |
20 | raise RuntimeError( "Timeout while waiting for data" ) | |
21 | - | serialDevice = "/dev/ttyAMA0" # default for RaspberryPi |
21 | + | |
22 | - | maxwait = 3 # seconds to try for a good reading before quitting |
22 | + | m = re.fullmatch( rb'R(\d+)\r', data ) |
23 | if not m: | |
24 | - | def measure(portName): |
24 | + | raise RuntimeError( "Garbage data received: %r" % data ) |
25 | - | ser = Serial(portName, 9600, 8, 'N', 1, timeout=1) |
25 | + | data = m.group( 1 ) |
26 | - | timeStart = time() |
26 | + | |
27 | - | valueCount = 0 |
27 | + | return int( data ) # measurement in inches (0-255) |
28 | ||
29 | - | while time() < timeStart + maxwait: |
29 | + | |
30 | - | if ser.inWaiting(): |
30 | + | # maxSonarTTY-example.py |
31 | - | bytesToRead = ser.inWaiting() |
31 | + | |
32 | - | valueCount += 1 |
32 | + | |
33 | - | if valueCount < 2: # 1st reading may be partial number; throw it out |
33 | + | from maxSonarTTY import MaxSonar |
34 | - | continue |
34 | + | |
35 | - | testData = ser.read(bytesToRead) |
35 | + | sonar = MaxSonar( "/dev/ttyS2" ) |
36 | - | if not testData.startswith(b'R'): |
36 | + | |
37 | - | # data received did not start with R |
37 | + | try: |
38 | - | continue |
38 | + | while True: |
39 | - | try: |
39 | + | distance = sonar.measure() |
40 | - | sensorData = testData.decode('utf-8').lstrip('R') |
40 | + | print( "distance =", distance, "inch" ) |
41 | - | except UnicodeDecodeError: |
41 | + | |
42 | - | # data received could not be decoded properly |
42 | + | sleep( 1 ) |
43 | - | continue |
43 | + | |
44 | - | try: |
44 | + | except KeyboardInterrupt: |
45 | - | mm = int(sensorData) |
45 | + | pass |