python - Raspberry Pi + Arduino + Python - 值错误 : Need more than 1 value to unpack

标签 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

我通过 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(",")

关于python - Raspberry Pi + Arduino + Python - 值错误 : Need more than 1 value to unpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32893959/

相关文章:

python - 具有不连续单调函数的二等分 : Find root using bisection for weakly monotonous function allowing for jumps

python - 将多个文件合并到一个新文件中

c# - Arch linux 启动应用程序(脚本)Raspberry Pi

linux - sqlcipher ./配置linux

python - 使用 virtualenvwrapper (& virtualenv) 改变默认的 python 版本

Python 请求 .iter_content() 生成器生成二进制数据?

android - 在 Kotlin Android 应用程序中处理传入的蓝牙数据流

embedded - 使用 Arduino 向 USB 发送 MIDI

java - setText() 显示和串行通信 (jSSC)

python - 如何使用 systemd 永远运行 python 脚本并在树莓派 3 上中途死掉时重新启动?