I am building a robot based on Raspberry Pi 3 and want to implement distance sensor to avoid hitting walls etc...
I connected the sensor using this manual --> https://www.raspberrypi.org/learning/ph ... /distance/ and wrote my own Python class to control it. (attached at the end of this post). Distance function returns its value in cm.
When the robot is not moving everything works as expected, the distance is being measured and reported correctly.
The problem starts when the robot is in motion. In this case the real value is being reported only once per few outputs. The rest is like:
Code: Select all
Current distance = 133.7881088256836 # real value Current distance = 105.72613477706909 # real value Current distance = 0.33937692642211914 Current distance = 88.43835592269897 # real value Current distance = 0.2453327178955078 Current distance = 0.35164356231689453 Current distance = 0.261688232421875 Current distance = 38.378214836120605 # real value Current distance = 0.33119916915893555 Current distance = 0.2575993537902832 Current distance = 0.2575993537902832 Current distance = 0.2575993537902832 -------------- somewhere here the robot hit the wall and stopped ---------------- Current distance = 0.33937692642211914 Current distance = 0.2575993537902832 Current distance = 0.2575993537902832 Current distance = 0.261688232421875 Current distance = 6.615805625915527 # real value
Even when it hits the wall, some time is needed to get real value (for tests the measurment inteval is 1 second)
Probably, I should include the speed of the car which I do not know and which is not constant...
Has anyone seen such issue before or have any idea how to deal with it?
The way I am calling the measurement:
Code: Select all
while cur_distance > float(10) or cur_distance < float(5.5): cur_distance = ds.distance() time.sleep(1) print('Current distance = ' + str(cur_distance)) if cur_distance >= float(10) and motor.motors_in_motion != 'forward' : if MOTORS_ENABLE: motor.setSpeed(100) motor.forward()
Code: Select all
#!/usr/bin/python3 #Libraries import RPi.GPIO as GPIO import time class DistanceSensor: def __init__(self): GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) self.gpio_trigger = 18 # BCM PIN 24 self.gpio_echo = 22 # BCM PIN 25 GPIO.setup(self.gpio_trigger, GPIO.OUT) GPIO.setup(self.gpio_echo, GPIO.IN) def __test__(self): ''' Continuosly prints current distance form the sensor ''' try: while True: dist = self.distance() print("Measured Distance = %.1f cm" % dist) time.sleep(1) # Reset by pressing CTRL + C except KeyboardInterrupt: print("Measurement stopped by User") GPIO.cleanup() def distance(self): ''' Returns distance in cm''' GPIO.output(self.gpio_trigger, False) # set Trigger LOW time.sleep(0.00002) # wait 2ms GPIO.output(self.gpio_trigger, True) # set Trigger to HIGH time.sleep(0.00010) # after 10ms GPIO.output(self.gpio_trigger, False) # set Trigger to LOW StartTime = time.time() # init value for StartTime StopTime = time.time() # init value for StopTime while GPIO.input(self.gpio_echo) == 0: # save StartTime StartTime = time.time() while GPIO.input(self.gpio_echo) == 1: # save time of arrival StopTime = time.time() # time difference between start and arrival TimeElapsed = StopTime - StartTime # multiply with the sonic speed (34300 cm/s) # and divide by 2, because there and back distance = (TimeElapsed * 34300) / 2 return distance