From edaddb74f4801b890bb6fb66535ecb8b40ca0a1d Mon Sep 17 00:00:00 2001 From: Forrest Beck Date: Wed, 8 May 2024 18:11:06 -0400 Subject: [PATCH] arduino fixes --- arduino.py | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/arduino.py b/arduino.py index 8d50fee..b4bc147 100644 --- a/arduino.py +++ b/arduino.py @@ -23,27 +23,24 @@ def __init__(self): self.ultrasonic_last_check = 0.00 def read_data(self, val): - # don't try more than 3 times - for i in range(0, 2, 1): - self.ser.reset_input_buffer() + # don't try more than 4 times + for i in range(0, 3, 1): try: - count = 0 - data = None - while data is None and count <= 3: - count = count + 1 - data = self.ser.readline() - except: - return None - d = data.decode() - if d[0] == "$": - d = d[1:] - dd = d.split("|") - if val == "ultrasonic": - return [dd[0], dd[1], dd[2], dd[3]] + # is this needed? + self.ser.reset_input_buffer() + data = self.ser.readline() + d = data.decode() + if d[0] == "$": + d = d[1:] + dd = d.split("|") + if val == 0: + return [dd[0], dd[1], dd[2], dd[3]] + else: + return dd[int(val)] else: - return dd[int(val)] - else: - # not a new line from arduino, try again + # not a new line from arduino, try again + continue + except: continue return "error" @@ -73,9 +70,6 @@ def get_ultrasonic(self): Reads the ultrasonic sensors from the Arduino. Returns the ultrasonic as :return: list """ - try: - self.ultrasonic_last_check = time.time() - ints = [int(x) for x in self.read_data("ultrasonic")] - return ints - except: - return [0, 0, 0, 0] + self.ultrasonic_last_check = time.time() + ints = [int(x) for x in self.read_data(0)] + return ints