0

I have a code currently running on a pyboard and is being used with a motor shield adafruit Motorshield v2.3. I know most ultrasonic sensors timeout after not detecting a surrounding for a long time. I want my code to continually search for a surrounding and not timeout. The robot is meant to drive in a large area until it reaches a surrounding and redirects. Below my code is attached. (Ignore the comments in the code. like the switches. these were used when i was testing still)

Thanks!!

i2c = machine.I2C(scl=machine.Pin('Y9'), sda=machine.Pin('Y10'))
motors = motor.DCMotors(i2c)
MOTOR1 = 2
MOTOR2 = 3
#Initiate Trigger and Echo Pin from Ultrasonic sensor
TRIGGER_PIN =pyb.Pin.board.X9
ECHO_PIN = pyb.Pin.board.X10
#Initiate Communication from Sonar sensor
sensor = Ultrasonic (TRIGGER_PIN, ECHO_PIN)
#Create minimum distance For Ultrasonic sensor
min_distance = sensor.distance_in_cm()
print("min_distance= ",min_distance)
#button = pyb.switch()
#def autonomy()
#no_problem = True
try:
    while (True):
    #if (button()):
        min_distance = sensor.distance_in_cm()
        #sensor_front = sensor.distance_in_cm(15)
        if min_distance >= 70:
            print(min_distance)
            motors.speed(MOTOR1, -3500)
            motors.speed(MOTOR2, -3500)
            # motors.speed(MOTOR1, 3500)
            # motors.speed(MOTOR2,-3500)

            #if something is in the way
        else:
            print(min_distance)
            motors.speed(MOTOR1, 0)
            motors.speed(MOTOR2, 0)
            time.sleep_us(10)
            #Turn around
            print("Do the pivot shuffle.")

            motors.speed(MOTOR1, 3500)
            motors.speed(MOTOR2, -3500)
            time.sleep_us(10)
        print(min_distance)
        time.sleep_us(10)

except KeyboardInterrupt:
    pass

1 Answers1

0

If you are getting the time out the distance is probably higher than the sensor can measure. You can read on the Ultrasonic library you are using: Default timeout is a bit more than the HC-SR04 max distance (400 cm)

Source: https://github.com/skgsergio/MicropythonLibs/blob/master/Ultrasonic/ultrasonic.py

Shark
  • 41
  • 3
  • Thats what I assumed. How do you think I could get my code to run continually without timeout? Just place a while loop after my code to reiterate itself? – Kyle Everett Shapen Apr 03 '19 at 18:17
  • Just treat a time out like the distance is bigger than 70. See what the function returns as a time out and use that on the if. – Shark Apr 04 '19 at 12:41