Raspberry Pi + Arduino + Python - 值错误:解压缩需要超过1个值

时间:2015-10-01 17:59:12

标签: python arduino raspberry-pi

我正致力于在被黑客MindFlex EEG读者与Arduino UnoRaspberry Pi工作之间进行整合。我按照指示来到来源的来信,即here。这是代码:

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

我通过Raspery Pi命令在sudo python flappybrain.py上执行此脚本。当然,我确保一切都正确连接。当我运行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 

我可以看到脚本正在尝试做什么,而不是为什么它不能这样做。非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

在这种情况下,您遇到了一行输入,其中不包含这些分隔符(逗号)。您可以ValueError处理continue(仅限此行!),或者设置一个检查逗号并执行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(",")

答案 1 :(得分:0)

确保type(data) <type 'str'>str(data).split(",") ,否则你应该:

int(result[i])

如果你需要和int类型:

private delegate DependencyObject PredictFocusedElement(DependencyObject sourceElement, FocusNavigationDirection direction, bool treeViewNavigation, bool considerDescendants);

// get the default KeyboardNavigation instance

KeyboardNavigation keyboardNavigation = (KeyboardNavigation)typeof(FrameworkElement).GetProperty("KeyboardNavigation", BindingFlags.NonPublic | BindingFlags.Static).GetMethod.Invoke(null, null);

// create a delegate for the PredictFocusedElement method

_predictFocusedElement = (PredictFocusedElement)typeof(KeyboardNavigation).GetMethod("PredictFocusedElement", BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder,
                                                                                        new Type[] { typeof(DependencyObject), typeof(FocusNavigationDirection), typeof(bool), typeof(bool) },
                                                                                        null).CreateDelegate(typeof(PredictFocusedElement), keyboardNavigation);