raspi SerialException:设备报告准备读取,但未返回任何数据。对于自动化RC

2024-10-01 07:30:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在遵循“开始机器人与覆盆子皮和Arduino”的书,并遇到了一个问题的代码。我对编程非常陌生,我正在用这本书来熟悉Pi、Arduino和Python。这本书有点含糊不清,没有提供解决问题的方法。你知道吗

Traceback (most recent call last):
  File "/home/Danberry/Roject/pi_roamer_01.py", line 80, in <module>
    val = ser.readline().decode('utf-8')
  File "/home/Danberry/.local/lib/python2.7/site-packages/serial/serialposix.py", line 501, in read
    'device reports readiness to read but returned no data '
SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

我已经确认,一切都连接正确,并单独工作。现在的问题是让树莓派去读它。你知道吗

import serial
import time
import random

from Adafruit_MotorHAT import Adafruit_MotorHAT as amhat
from Adafruit_MotorHAT import Adafruit_DCMotor as adamo

# create motor objects
motHAT = amhat(addr=0x60)
mot1 = motHAT.getMotor(1)
mot2 = motHAT.getMotor(2)
mot3 = motHAT.getMotor(3)
mot4 = motHAT.getMotor(4)

# open serial port
ser = serial.Serial('/dev/ttyACM0', 115200)

# create variables
# sensors
distMid = 0.0
distLeft = 0.0
distRight = 0.0

# motor multipliers
m1Mult = 1.0
m2Mult = 1.0
m3Mult = 1.0
m4Mult = 1.0

# distance threshold
distThresh = 12.0
distCutOff = 30.0

# speeds
speedDef = 200
leftSpeed = speedDef
rightSpeed = speedDef
turnTime = 1.0
defTime = 0.1
driveTime = defTime

def driveMotors(leftChnl = speedDef, rightChnl = speedDef, duration = defTime):
    # determine the speed of each motor by multiplying
    # the channel by the motors multiplier
    m1Speed = leftChnl * m1Mult
    m2Speed = leftChnl * m2Mult
    m3Speed = rightChnl * m3Mult
    m4Speed = rightChnl * m4Mult

    # set each motor speed. Since the speed can be a
    # negative number, we take the absolute value
    mot1.setSpeed(abs(int(m1Speed)))
    mot2.setSpeed(abs(int(m2Speed)))
    mot3.setSpeed(abs(int(m3Speed)))
    mot4.setSpeed(abs(int(m4Speed)))

    # run the motors. if the channel is negative, run
    # reverse. else run forward
    if(leftChnl < 0):
        mot1.run(amhat.BACKWARD)
        mot2.run(amhat.BACKWARD)
    else:
        mot1.run(amhat.FORWARD)
        mot2.run(amhat.FORWARD)

    if (rightChnl > 0):
        mot3.run(amhat.BACKWARD)
        mot4.run(amhat.BACKWARD)
    else:
        mot3.run(amhat.FORWARD)
        mot4.run(amhat.FORWARD)

    # wait for duration
    time.sleep(duration)

try:
    while 1:
        # read the serial port
        val = ser.readline().decode('utf=8')
        print val

        # parse the serial string
        parsed = val.split(',')
        parsed = [x.rstrip() for x in parsed]

        # only assign new values if there are
        # three or more available
        if(len(parsed)>2):
            distMid = float(parsed[0] + str(0))
            distLeft = float(parsed[1] + str(0))
            distRight = float(parsed[2] + str(0))

        # apply cutoff distance
        if(distMid > distCutOff):
            distMid = distCutOff
        if(distLeft > distCutOff):
            distLeft = distCutOff
        if(distRight > distCutOff):
            distRight = distCutOff

        # reset driveTime
        driveTime = defTime

        # if obstacle to left, steer right by increasing
        # leftSpeed and running rightSpeed negative defSpeed
        # if obstacle to right, steer to left by increasing
        # rightSpeed and running leftSpeed negative
        if(distLeft <= distThresh):
            leftSpeed = speedDef
            rightSpeed = -speedDef
        elif (distRight <= distThresh):
            leftSpeed = -speedDef
            rightSpeed = speedDef
        else:
            leftSpeed = speedDef
            rightSpeed = speedDef

        # if obstacle dead ahead, stop then turn toward most
        # open direction. if both directions open, turn random
        if(distMid <= distThresh):
            # stop
            leftSpeed = 0
            rightSpeed = 0
            driveMotors(leftSpeed, rightSpeed, 1)
            time.sleep(1)
            leftSpeed = -150
            rightSpeed = -150
            driveMotors(leftSpeed, rightSpeed, 1)
            # determine preferred direction. if distLeft >
            # distRight, turn left. if distRight > distLeft,
            # turn right. if equal, turn random
            dirPref = distRight - distLeft
            if(dirPref == 0):
                dirPref = random.random()
            if(dirPref < 0):
                leftSpeed = -speedDef
                rightSpeed = speedDef
            elif(dirPref > 0):
                leftSpeed = speedDef
                rightSpeed = -speedDef
            driveTime = turnTime

        # drive the motors
        driveMotors(leftSpeed, rightSpeed, driveTime)
        ser.flushInput()

except KeyboardInterrupt:
    mot1.run(amhat.RELEASE)
    mot2.run(amhat.RELEASE)
    mot3.run(amhat.RELEASE)
    mot4.run(amhat.RELEASE)

也许是某个地方的压痕问题?我已经花了几个小时玩弄这些代码,但还没有真正得到任何进展。如果你们能帮上忙那就太棒了!你知道吗


Tags: thetorunimportifserialrandomparsed
1条回答
网友
1楼 · 发布于 2024-10-01 07:30:41

这听起来像是什么应该提供数据通过串行是不是提供它的频率不够,你的程序。我很想抓住这个错误再试一次(也许先等一等)。所以

try:
    while 1:
        # read the serial port
        val = ser.readline().decode('utf=8')
        print val

from time import sleep
...
try:
    while 1:
        # read the serial port
        try:
            val = ser.readline().decode('utf=8')
            print val
        except SerialException:
            sleep(0.01)  # Maybe don't do this, or mess around with the interval
            continue

相关问题 更多 >