我有一个代码目前运行在一个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
发布于 2019-04-03 20:59:09
如果你得到的是超时,那么这个距离可能比传感器所能测量的要高。您可以在您正在使用的超声波资料库上阅读:默认超时略大于HC-SR04最大距离(400 cm)
来源:https://github.com/skgsergio/MicropythonLibs/blob/master/Ultrasonic/ultrasonic.py
https://stackoverflow.com/questions/55485369
复制相似问题