def read(self): ''' Limit the ultrasonic sensor to a distance of 5m. Take 3 readings, discard any that's higher than 5m If we discard 5 times, then assume there's nothing in front and return 501 ''' return_reading = 0 readings =[] skip = 0 while len(readings) < 3: _wait_for_read() _grab_read() value = gopigo.corrected_us_dist(PORTS[self.port]) _release_read() if value < 501 and value > 0: readings.append(value) else: skip +=1 if skip > 5: break if skip > 5: return(501) for reading in readings: return_reading += reading return_reading = int(return_reading // len(readings)) return (return_reading)
def read(self): ''' Limit the ultrasonic sensor to a distance of 5m. Take 3 readings, discard any that's higher than 3m If we discard 5 times, then assume there's nothing in front and return 501 ''' return_reading = 0 readings = [] skip = 0 while len(readings) < 3: _grab_read() try: value = gopigo.corrected_us_dist(PORTS[self.port]) except Exception as e: print("UltraSonicSensor read(): {}".format(e)) pass _release_read() if value < 300 and value > 0: readings.append(value) else: skip += 1 if skip > 5: break time.sleep(0.05) if skip > 5: return (501) for reading in readings: return_reading += reading return_reading = int(return_reading // len(readings)) return (return_reading)
def read(self): ''' Limit the ultrasonic sensor to a distance of 5m. Take 3 readings, discard any that's higher than 5m If we discard 5 times, then assume there's nothing in front and return 501 ''' return_reading = 0 readings = [] skip = 0 while len(readings) < 3: _wait_for_read() _grab_read() value = gopigo.corrected_us_dist(PORTS[self.port]) _release_read() if value < 501 and value > 0: readings.append(value) else: skip += 1 if skip > 5: break if skip > 5: return (501) for reading in readings: return_reading += reading return_reading = int(return_reading // len(readings)) return (return_reading)
def read(self): ''' Limit the ultrasonic sensor to a distance of 5m. Take 3 readings, discard any that's higher than 3m If we discard 5 times, then assume there's nothing in front and return 501 ''' return_reading = 0 readings =[] skip = 0 while len(readings) < 3: _ifMutexAcquire(self.use_mutex) try: value = gopigo.corrected_us_dist(PORTS[self.port]) except Exception as e: print("UltraSonicSensor read(): {}".format(e)) pass finally: _ifMutexRelease(self.use_mutex) if value < 300 and value > 0: readings.append(value) else: skip +=1 if skip > 5: break time.sleep(0.05) if skip > 5: return(501) for reading in readings: return_reading += reading return_reading = int(return_reading // len(readings)) return (return_reading)