我正在尝试使用raspberry pi制作一个使用心率日期作为触发器的动画。
我使用了github代码https://github.com/tutRPi/Raspberry-Pi-Heartbeat-Pulse-Sensor并使用example.py
作为我工作的基础。
我还使用了一个很棒的教程,可以使用tkinter在屏幕上移动。
我对python完全不熟悉,并且无法在if语句中播放动画。理想情况下,我希望球能够以bmp数据生成的速度移动,这样更快的心率将使它们更快地移动。此刻我似乎离这个很远。 如果有人能帮助我实现这一目标,我将永远感激不尽。就目前而言,球出现但不动。我认为与运动和bpm更新存在冲突......
以下是代码:
from pulsesensor import Pulsesensor
import time
from tkinter import *
import random
tk = Tk()
WIDTH=1500
HEIGHT=800
canvas = Canvas(tk, bg="brown4", height=HEIGHT, width= WIDTH)
tk.title("drawing")
canvas.pack()
##below is the class to create multiple balls that are coloured
##and move and detect the edge and bounce
class Ball:
def __init__(self, color, size):
self.shape = canvas.create_oval (10, 10, size, size, fill=color,
outline=color, stipple="gray25")
self.xspeed = random.randrange(-1,5)
self.yspeed = random.randrange(-1,5)
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
pos = canvas.coords(self.shape)
if pos[3]>=HEIGHT or pos[1]<=0:
self.yspeed=-self.yspeed
if pos[2] >=WIDTH or pos[0] <=0:
self.xspeed=-self.xspeed
colors=["red4", "red3", "OrangeRed2","OrangeRed4","firebrick3"]
##this is make 100 balls
balls=[]
##this is to set the colour and size of the balls which is randomised:
for i in range (100):
balls.append(Ball(random.choice(colors), random.randrange(150, 200)))
##this is to call the balls
##while True:
p = Pulsesensor()
p.startAsyncBPM()
try:
while True:
bpm = p.BPM
if bpm > 0:
print("BPM: %d" % bpm)
for ball in balls:
ball.move()
tk.update()
time.sleep(0.02)
tk.mainloop()
else:
print("No Heartbeat found")
time.sleep(1)
except:
p.stopAsyncBPM()
答案 0 :(得分:1)
我没有Raspberry-Pi或脉冲传感器,所以以下只能在一定程度上进行测试 - 但希望它能为您提供更好的基础代码。我减少了球的数量,以便更容易看到发生了什么。我真的不明白他们的动作应该与脉搏率有什么关系,所以你需要自己充实...
使用tkinter时,了解所发生的一切必须通过mainloop()
这一点非常重要,Canvas
通常不会返回,直到应用程序脚本退出(因此它本质上是一个无尽的循环)。这意味着您通常无法随时调用它。在这种情况下,&#34;轮询&#34;函数被使用,并以设定的间隔调用它来更新mainloop()
对象 - 当<{em} from pulsesensor import Pulsesensor
import random
from tkinter import *
WIDTH=1500
HEIGHT=800
COLORS = ["red4", "red3", "OrangeRed2", "OrangeRed4", "firebrick3"]
DELAY = 200 # In millisecs
NUMBALLS = 5
class Ball:
def __init__(self, color, size):
self.shape = canvas.create_oval(10, 10, size, size, fill=color,
outline=color, stipple="gray25")
self.xspeed = random.randrange(-1, 5)
self.yspeed = random.randrange(-1, 5)
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
pos = canvas.coords(self.shape)
if pos[3] >= HEIGHT or pos[1] <= 0:
self.yspeed = -self.yspeed
if pos[2] >= WIDTH or pos[0] <= 0:
self.xspeed = -self.xspeed
def poll(p):
try:
# bpm = p.BPM
bpm = random.randint(0, 200) # Random value for testing.
if bpm < 1:
# print("No Heartbeat found")
pass
else:
# print("BPM: %d" % bpm)
for ball in balls:
ball.move()
except Exception as exc:
print('Exception raised: {}'.format(exc))
p.stopAsyncBPM()
root.quit()
root.after(DELAY, poll, p) # Call this function again after delay.
if __name__ == '__main__':
root = Tk()
root.title("Beating Heart")
canvas = Canvas(root, bg="brown4", height=HEIGHT, width=WIDTH)
canvas.pack()
balls = [Ball(random.choice(COLORS), random.randrange(150, 200))
for _ in range(NUMBALLS)]
p = Pulsesensor()
p.startAsyncBPM()
poll(p) # Start polling.
root.mainloop()
正在运行时,它将全部发生。
{{1}}