我正在努力獲得一個集成工作,在一個被黑客竊取的MindFlex EEG
閱讀器到Arduino Uno
到Raspberry Pi
工作。我已按照來自源代碼的信的說明操作,即here。下面是代碼:Raspberry Pi + Arduino + Python - 值錯誤:需要超過1個值才能打包
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
我可以看到腳本試圖做什麼,而不是爲什麼它不能這樣做。非常感謝您的幫助。
非常感謝!我是一個新手,你可以幫忙修改代碼嗎?我是一位與我的孩子一起工作的老師。我會努力嘗試和學習。 – Mike
只想跟進說聲謝謝!這使得代碼工作像一個魅力。看着我的學生用他們的想法來控制一個Flappy Brain遊戲,這有點兒有趣!沒有你的幫助不會發生。非常感激。 – Mike
很高興聽到它。如果你滿意,繼續接受這個答案。 –