首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用光轮编码器的不正确行程

使用光轮编码器的不正确行程
EN

Stack Overflow用户
提问于 2022-03-31 18:27:26
回答 1查看 92关注 0票数 0

大约两个月前,我开始使用树莓pi 3b+,所以我是相当新的。我正在做一个项目,在这个项目中,我组装了一辆四轮小车,它有4个直流电动机,一个3b+,一个L289D H桥和一个光学轮编码器。

我是在我的项目的初步阶段,我正在测试的工具包,无论它是否旅行正确的距离。下面是我用来测试流程的代码:

代码语言:javascript
复制
import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(13, GPIO.OUT)   #clockwise left
GPIO.setup(15, GPIO.OUT)   #anticlockwise right
GPIO.setup(16, GPIO.OUT)   #anticlockwise left
GPIO.setup(18, GPIO.OUT)   #clockwise right

stateLast = GPIO.input(11)
rotationCount=0
stateCount=0
stateCountTotal=0
flag=0

circ=62.4*3.14 #mm
statesPerRotation=20
distancePerStep= circ/statesPerRotation

GPIO.output(13, GPIO.HIGH)
GPIO.output(18, GPIO.HIGH)
flag="true"
while flag=="true":
    stateCurrent = GPIO.input(11)
    if stateCurrent != stateLast:
        stateLast = stateCurrent
        stateCount += 1
        stateCountTotal += 1
        
    if stateCount == statesPerRotation:
        rotationCount += 1
        stateCount = 0
        
    distance = distancePerStep * stateCountTotal
    print("Distance",distance)
    if distance > 50:
        flag = "false"

GPIO.output(13, GPIO.LOW)
GPIO.output(18, GPIO.LOW)

正在发生的事情是,在运行代码时,工具包并不是我在上面的代码中输入的距离。当我使用较低的距离值时,试剂盒会准确地运行,但当我将距离增加到200毫米以上时,试剂盒就不能达到它应该运行的实际距离。对于300毫米,它停留在250毫米左右的地面,但有趣的是,在打印距离的代码,它运行到300毫米。我无法理解为什么代码显示该工具包已行驶300毫米,但在现实中,它总是停留在220毫米-250毫米左右。

我注意到的另一件有趣的事情是,当我使用Thonny运行代码时,所走的距离总是不一致的,不管距离的值如何。例如,如果我使用距离20毫米,有时它运行10毫米,在其他时刻30毫米,而且永远不准确。当我从终端运行code.py文件时,它所走的距离变得一致,但仍然不准确,如上面一段所述。

我使用远程桌面连接来访问3b+。我使用了另一个车轮编码器,以确保它没有问题,也检查了电池电压,这是很好的。除此之外,我还在不同的表面上测试过试剂盒,但是到目前为止还没有什么效果。

如果有人能帮我解决这个问题,我会很感激的。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-03-31 19:09:08

代码语言:javascript
复制
# real-time processing requires fast non blocking code. Try the following 
# and see if the performance improves.
import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(13, GPIO.OUT)  # clockwise left
GPIO.setup(15, GPIO.OUT)  # anticlockwise right
GPIO.setup(16, GPIO.OUT)  # anticlockwise left
GPIO.setup(18, GPIO.OUT)  # clockwise right

stateLast = GPIO.input(11)
rotationCount = 0
stateCount = 0
stateCountTotal = 0
flag = 0

circ = 62.4 * 3.14  # mm
statesPerRotation = 20
distancePerStep = circ / statesPerRotation

GPIO.output(13, GPIO.HIGH)
GPIO.output(18, GPIO.HIGH)
# flag = "true"

while True: # Use boolean logic not string evaluation
    stateCurrent = GPIO.input(11)
    if stateCurrent != stateLast:
        stateLast = stateCurrent
        stateCount += 1
        stateCountTotal += 1

    if stateCount == statesPerRotation:
        rotationCount += 1
        stateCount = 0

    distance = distancePerStep * stateCountTotal
    # print("Distance", distance)  # Do not print in th e loop
    if distance > 50:
        break
print("Distance", distance)
GPIO.output(13, GPIO.LOW)
GPIO.output(18, GPIO.LOW)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71697482

复制
相关文章

相似问题

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