首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >阿杜诺超声传感器与Pyfirmata的应用

阿杜诺超声传感器与Pyfirmata的应用
EN

Stack Overflow用户
提问于 2022-11-15 09:41:28
回答 3查看 55关注 0票数 0

我试着用吡咯烷酮来用阿杜诺超声波传感器。我使用了Arduino Uno板和HC 04超声波传感器.这是我正在使用的代码。代码运行顺利,只是回音引脚似乎没有从触发超声波的声音中得到一个冲动,所以它一直在变假(低读数),从而给了我错误的距离读数。有谁能解决这个问题吗?

代码语言:javascript
复制
import pyfirmata
import time

board = pyfirmata.Arduino('COM16')

start = 0
end = 0

echo = board.get_pin('d:11:i')
trig = board.get_pin('d:12:o')
LED = board.get_pin('d:13:o')

it = pyfirmata.util.Iterator(board)
it.start()

trig.write(0)
time.sleep(2)

while True:
    time.sleep(0.5)

    trig.write(1)   
    time.sleep(0.00001)
    trig.write(0)
    
    print(echo.read())
    while echo.read() == False:
        start = time.time()

    while echo.read() == True:
        end = time.time()
    
    TimeElapsed = end - start
    distance = (TimeElapsed * 34300) / 2

    print("Measured Distance = {} cm".format(distance) )

我尝试过将time.sleep()更改为几个值,但它仍然不起作用。当我从Arduino IDE肮脏地使用Arduino代码时,它工作得很好。

EN

回答 3

Stack Overflow用户

发布于 2022-11-17 09:23:35

我还没有做过精确的计算,但是给出了50厘米的范围,你的旅行时间大约是3ms。这意味着你需要关闭脉冲和轮询引脚状态在这段时间内。

那是不可能的。回声可能会在你通过PyFirmata关闭发射器之前到达。你应该在Arduino号上做延迟测量。

票数 0
EN

Stack Overflow用户

发布于 2022-11-26 13:12:35

我通过计数来解决这个错误的数据问题。我观察到错误的数据出现在2秒或3秒之后。因此,如果超过2秒或3秒,我清除计数并从0重新启动;

代码语言:javascript
复制
Sudo code:

cnt = 0;

if sensorvalue <= 20 && sensorvalue <= 30:
   cnt++;
if cnt>=5:
  detected = true;
  cnt =0;

if cnt<5 && lastDecttime>2 (2 sec):
  cnt = 0; // Here we handle the false value and clear the data
票数 0
EN

Stack Overflow用户

发布于 2022-12-02 23:42:41

我现在正试着解决这个问题。我可以让传感器直接使用Arduino IDE,但不能使用python和pyfirmata。我得到了一些输出,但这大多是荒谬的。下面是我得到的一个示例输出,同时将传感器保持在与我的对象相同的距离上:

代码语言:javascript
复制
817.1010613441467
536.828875541687
0.0
546.0820078849792
0.0
0.0
1060.0213408470154

对于您的代码,我看到的唯一不同之处是使用board.pass_time函数而不是time.sleep()。如果你有进展就告诉我!

代码语言:javascript
复制
import pyfirmata as pyf
import time

def ultra_test():

board = pyf.Arduino("COM10")
it = pyf.util.Iterator(board)
it.start()
trigpin = board.get_pin("d:7:o")
echopin = board.get_pin("d:8:i")
while True:
    trigpin.write(0)
    board.pass_time(0.5)
    trigpin.write(1)
    board.pass_time(0.00001)
    trigpin.write(0)
    limit_start = time.time()
    
    while echopin.read() != 1:
        if time.time() - limit_start > 1:
            break
        pass
    
    start = time.time()
    while echopin.read() != 0:
        pass
    stop = time.time()
    time_elapsed = stop - start
    print((time_elapsed) * 34300 / 2)
    board.pass_time(1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74443453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档