我有一个当前在pyboard上运行的代码,并且正在与马达护罩adafruit Motorshield v2.3一起使用。我知道大多数超声波传感器在长时间未检测到环境后会超时。我希望我的代码不断搜索周围环境而不是超时。该机器人应在大范围内行驶,直到到达周围并重定向。下面是我的代码。 (忽略代码中的注释。像开关一样。当我还在测试时使用了这些注释)
谢谢!
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
答案 0 :(得分:0)
如果超时,则距离可能会大于传感器可以测量的距离。 您可以在使用的超声波库中阅读: 默认超时时间比HC-SR04的最大距离(400厘米)多一点
来源: https://github.com/skgsergio/MicropythonLibs/blob/master/Ultrasonic/ultrasonic.py