树莓派+ Arduino + Python -值错误:需要多个值来解包



我正在努力在被黑的MindFlex EEG阅读器到Arduino UnoRaspberry Pi工作之间获得集成工作。我已经按照信中的说明做了,就在这里。下面是代码:

from pygame import *
import random
import time
import serial
#   set up the serial port
ser = serial.Serial('/dev/ttyACM0',9600)
# Clear the serial input buffer
ser.flushInput()
# variables for colours
black = [ 0, 0, 0]
white = [255,255,255]
red = [255, 0, 0]
blue = [0,0,255]
darkBlue = [0,0,128]
pink = [255,200,255]
green = [0,255,0]
orange = [255,102,0]
brown = [153,102,0]
yellow = [255,255,0]
purple = [128,0,128]
# gap in wall
gap  = 200
# width and height of screen
width = 1000
height = 600
count_gap = int(height/gap)
# 0 = hard, 1 = easier, 2 is easiest
difficulty = 2
# class to create sprites and render them
class Sprite:
def __init__(self,xpos,ypos,filename):
    self.x = xpos
    self.y = ypos
    self.bitmap = image.load(filename)
def render(self):
    screen.blit(self.bitmap,(self.x,self.y))
# screen size
size=[width,height]
# initialise pygame
init()
# create the screen (window)
screen = display.set_mode(size)
# Caption to go at the top of the window
display.set_caption("Flappy EEG 2")
# set the music, its volume and start to play. -1 means infinite loop
mixer.music.load("Pinky_and_the_Brain.ogg")
mixer.music.set_volume(0.5)
mixer.music.play(-1)
# create sound for crash
crasheffect = mixer.Sound("ouch.ogg")

# fill the screen with blue, update the screen
# not really required but I like it
screen.fill(blue)
display.update()
#time.sleep(1)
# create the sprites for the brain, columns and the background image
playerbrain = Sprite(20,200,"brain_75.png")
column1 = Sprite(1200,100,"column1.png")
column2 = Sprite(1200,100,"column2.png")
background = Sprite(0,0,"background.png")
# set fonts for different purposes
scorefont = font.Font(None,60)
datafont = font.Font(None,20)
overfont = font.Font(None,100)
# set default values for some variables
score = 0
maxscore = 0
quit = 0
gameover = 0
# master loop for the program. If quit == 0 then exit program
while quit == 0:
# flush the serial port of all data to begin fresh
ser.flushInput()
gameover = 0
# set the height of top column
column1.y = (random.randrange(0,(count_gap))*gap)-800
# set the height of the bottom column
column2.y = column1.y + 800 + gap
# start of loop (using while) to move the columns
# start off screen to the right
x = width +50
# x>-100 means still valid (yes there is a minus in there)
# gameover when collision
# quit selected. either pressed q or click x on window
while x >-100 and gameover == 0 and quit == 0:
# increment the score and if higher than maxscore make maxscore = score
    score = score + 1
    if score > maxscore:
        maxscore = score
# update columns location and set x positions
    x = x - 50
    column1.x = x
    column2.x =x
    data = ser.readline()
# print data
    signal,att,med,delta,theta,lalpha,halpha,lbeta,hbeta,lgamma,hgamma
= data.split(",")
    print "signal: " + signal
    print "attention: " + att
    print "data: " + data
    intatt = int(att)
    if intatt > 90:
        intatt = 90
    brainpos = intatt * 6
# set brain location based att (attention)
# is intatt near the gap above
    if brainpos < column1.y +800 and brainpos > column1.y + 800 -
(difficulty * 10):
        playerbrain.y = column1.y +800 +70
        print "brain near top and moved down!"
# is intatt near gap bottom
    elif brainpos > column2.y-75 and brainpos < column2.y +
(difficulty * 10):
        playerbrain.y = column1.y +800 +70
        print "brain near bottom and moved up!"
    else:
        playerbrain.y = brainpos
        print "brain where is should be"

# print playerbrain.y
    background.render()
    playerbrain.render()
    column1.render()
    column2.render()
# display some information on screen
    screen.blit(scorefont.render("Score: "+ str(score),1,white), (100, 5))
    screen.blit(scorefont.render("High Score: "+
str(maxscore),1,white), (400, 5))
    screen.blit(datafont.render("signal: "+ signal,1,white), (5, 570))
    screen.blit(datafont.render("attention: "+ att,1,white), (150, 570))
    screen.blit(datafont.render("playerbrain.y: "+
str(brainpos),1,white), (250, 570))
    screen.blit(datafont.render("column1.y: "+
str(column1.y+800),1,white), (500, 570))
    screen.blit(datafont.render("difficulty: "+
str(difficulty),1,white), (650, 570))
    display.update()
# print playerbrain.y
# collision dection
    if ((playerbrain.y < column1.y+801 or playerbrain.y >
column2.y-74) and (x <150 and x > 20)):
        mixer.music.stop()
        mixer.Sound.play(crasheffect)
        print "BUMP"
        gameover = 1
# check if clicked x on window to exit
    for ourevent in event.get():
        if ourevent.type == QUIT:
            quit = 1
# has key been pressed. K_q is to quit
        if ourevent.type == KEYDOWN:
            if ourevent.key == K_DOWN:
                playerbrain.y = playerbrain.y+10
            if ourevent.key == K_UP:
                playerbrain.y = playerbrain.y-10
            if ourevent.key == K_q:
                quit = 1
# if game over show message
while gameover == 1 and quit == 0:
    screen.blit(overfont.render("GAME OVER",1,yellow), (380, 260))
    display.update()
# then wait for a key to pressed  before starting again
    for ourevent in event.get():
        if ourevent.type == KEYDOWN:
            if ourevent.key == K_0:
                difficulty = 0
                score = 0
                gameover = 0
                mixer.music.play(-1)
            if ourevent.key == K_1:
                difficulty = 1
                score = 0
                gameover = 0
                mixer.music.play(-1)
            if ourevent.key == K_2:
                difficulty = 2
                score = 0
                gameover = 0
                mixer.music.play(-1)
            if ourevent.key == K_SPACE:
                score = 0
                gameover = 0
                mixer.music.play(-1)
            if ourevent.key == K_q:
                quit = 1
                score = 0
                gameover = 0

我通过sudo python flappybrain.py命令在Raspery Pi上执行此脚本。当然,我会确保每件东西都正确连接。当我运行Arduino IDE时,我可以看到EEG的良好输出。

但是,当我执行脚本时,它返回:

Traceback (most recent call last):
  File "flappybrain.py", line 125, in <module>
    signal,att,med,delta,theta,lalpha,halpha,lbeta,hbeta,lgamma,hgamma
= data.split(",")
ValueError: need more than 1 value to unpack

MindFlex没有连接时,我没有得到这个错误(它只是挂起,然后)。看了MindFlex的原始输出,它是数字行;EEG中的值。显然,剧本在这方面有问题。典型的行可能像这样:

20010,2140,43234,345,2342,2342,4534,5643,564,3244,7865 

我能看到脚本想要做什么,只是不知道为什么它不能这样做。非常感谢您的帮助。

在本例中,您遇到了不包含这些分隔符(逗号)的一行输入。您可以通过continue处理ValueError(仅在这一行!),或者放置一个检查逗号并执行continue的保护。或者你可以过滤掉不包含逗号的行,如果有意义的话。

如果输入无效,则必须拒绝该输入。

例如:

    data = ser.readline()
# print data
    EXPECTED_FIELD_COUNT = 11 
    if len(data.split(',')) != EXPECTED_FIELD_COUNT:
        continue 
    signal,att,med,delta,theta,lalpha,halpha,lbeta,hbeta,lgamma,hgamma = data.split(",")

确保type(data)<type 'str'>,如果不是,你应该:

str(data).split(",")

和int类型

int(result[i])

最新更新