在 raspberry pi 中使用超声波传感器测量距离
Measuring Distance using ultrasound sensors in raspberry pi
我正在尝试使用超声波传感器测量从传感器(S) 到障碍物(X) 的距离(D)。基本原理是我将发送一个声音脉冲并接收回来,并利用它从
S 到 X 并返回(例如,T),我将使用以下公式计算距离:
D = (V * T) / 2。 (V 是声音在空气中的速度)。
以下是一些 python 实现相同目的的代码:
#Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO = 24
print "Ultrasonic Measurement"
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
# Allow module to settle
time.sleep(0.5)
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34300
# That was the distance there and back so halve the value
distance = distance / 2
我很难理解为什么开始和停止时间是这样计算的。对我来说,开始时间似乎是时间 "when we first get a high signal" ,停止时间是时间 "when we last get the high signal" ,因此它们的差异将成为“脉冲高”的时间,我认为这与距离无关,因为每次发送高电平的持续时间相同。我试图查看其他来源,他们似乎都只考虑这个时间,即 ECHO 传感器输入高的时间。
然而,我不同意。
它认为代码应该是这样的:
# start time is time when we start sending the signal
start = time.time()
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
while GPIO.input(GPIO_ECHO)==0:
pass
# stop time is the time when we first get a high on the output.
stop = time.time()
while GPIO.input(GPIO_ECHO)==1:
pass
# Calculate pulse length
elapsed = stop-start
在我看来,我遗漏了一些明显的东西。如果有人能指出它是什么,我将不胜感激。
这是因为超声波回波传感器就是这样工作的。您向 GPIO_TRIGGER
行发送一个脉冲。这会导致传感器开始发送短暂的声音脉冲。然而,这本身需要一些时间。然后它必须等到收到该脉冲的回波。当它 完成发送 声音脉冲时,传感器的输出变高,当它 完成接收 声音脉冲时,它再次变低。中间的时间是声音脉冲到达某个物体并被反射回来所花费的时间。
我正在尝试使用超声波传感器测量从传感器(S) 到障碍物(X) 的距离(D)。基本原理是我将发送一个声音脉冲并接收回来,并利用它从 S 到 X 并返回(例如,T),我将使用以下公式计算距离: D = (V * T) / 2。 (V 是声音在空气中的速度)。 以下是一些 python 实现相同目的的代码:
#Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO = 24
print "Ultrasonic Measurement"
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
# Allow module to settle
time.sleep(0.5)
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34300
# That was the distance there and back so halve the value
distance = distance / 2
我很难理解为什么开始和停止时间是这样计算的。对我来说,开始时间似乎是时间 "when we first get a high signal" ,停止时间是时间 "when we last get the high signal" ,因此它们的差异将成为“脉冲高”的时间,我认为这与距离无关,因为每次发送高电平的持续时间相同。我试图查看其他来源,他们似乎都只考虑这个时间,即 ECHO 传感器输入高的时间。 然而,我不同意。
它认为代码应该是这样的:
# start time is time when we start sending the signal
start = time.time()
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
while GPIO.input(GPIO_ECHO)==0:
pass
# stop time is the time when we first get a high on the output.
stop = time.time()
while GPIO.input(GPIO_ECHO)==1:
pass
# Calculate pulse length
elapsed = stop-start
在我看来,我遗漏了一些明显的东西。如果有人能指出它是什么,我将不胜感激。
这是因为超声波回波传感器就是这样工作的。您向 GPIO_TRIGGER
行发送一个脉冲。这会导致传感器开始发送短暂的声音脉冲。然而,这本身需要一些时间。然后它必须等到收到该脉冲的回波。当它 完成发送 声音脉冲时,传感器的输出变高,当它 完成接收 声音脉冲时,它再次变低。中间的时间是声音脉冲到达某个物体并被反射回来所花费的时间。