在 Kivy 标签中显示来自 python 程序的心跳传感器读数

祖海

我试图在我的 Kivy 应用程序中显示从我的心跳传感器获得的值。我已经尝试使用应用于其他传感器的数据提取方法,但它不适用于该传感器,因为它不包含任何功能。

我尝试了多种不同的方法,但它们都没有返回所需的输出。有人可以看看传感器代码并建议一些方法来在我的 kivy 应用程序中显示输出。

心跳感应器.py


import time
# Import the ADS1x15 module.
import Adafruit_ADS1x15


if __name__ == '__main__':

    adc = Adafruit_ADS1x15.ADS1015()
    # initialization 
    GAIN = 2/3  
    curState = 0
    thresh = 525  # mid point in the waveform
    P = 512
    T = 512
    stateChanged = 0
    sampleCounter = 0
    lastBeatTime = 0
    firstBeat = True
    secondBeat = False
    Pulse = False
    IBI = 600
    rate = [0]*10
    amp = 100

    lastTime = int(time.time()*1000)

    # Main loop. use Ctrl-c to stop the code
    while True:
        # read from the ADC
        Signal = adc.read_adc(0, gain=GAIN)   #TODO: Select the correct ADC channel. I have selected A0 here
        curTime = int(time.time()*1000)

        sampleCounter += curTime - lastTime;      #                   # keep track of the time in mS with this variable
        lastTime = curTime
        N = sampleCounter - lastBeatTime;     #  # monitor the time since the last beat to avoid noise
        #print N, Signal, curTime, sampleCounter, lastBeatTime

        ##  find the peak and trough of the pulse wave
        if Signal < thresh and N > (IBI/5.0)*3.0 :  #       # avoid dichrotic noise by waiting 3/5 of last IBI
            if Signal < T :                        # T is the trough
              T = Signal;                         # keep track of lowest point in pulse wave 

        if Signal > thresh and  Signal > P:           # thresh condition helps avoid noise
            P = Signal;                             # P is the peak
                                                # keep track of highest point in pulse wave

          #  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
          # signal surges up in value every time there is a pulse
        if N > 250 :                                   # avoid high frequency noise
            if  (Signal > thresh) and  (Pulse == False) and  (N > (IBI/5.0)*3.0)  :       
              Pulse = True;                               # set the Pulse flag when we think there is a pulse
              IBI = sampleCounter - lastBeatTime;         # measure time between beats in mS
              lastBeatTime = sampleCounter;               # keep track of time for next pulse

              if secondBeat :                        # if this is the second beat, if secondBeat == TRUE
                secondBeat = False;                  # clear secondBeat flag
                for i in range(0,10):             # seed the running total to get a realisitic BPM at startup
                  rate[i] = IBI;                      

              if firstBeat :                        # if it's the first time we found a beat, if firstBeat == TRUE
                firstBeat = False;                   # clear firstBeat flag
                secondBeat = True;                   # set the second beat flag
                continue                              # IBI value is unreliable so discard it


              # keep a running total of the last 10 IBI values
              runningTotal = 0;                  # clear the runningTotal variable    

              for i in range(0,9):                # shift data in the rate array
                rate[i] = rate[i+1];                  # and drop the oldest IBI value 
                runningTotal += rate[i];              # add up the 9 oldest IBI values

              rate[9] = IBI;                          # add the latest IBI to the rate array
              runningTotal += rate[9];                # add the latest IBI to runningTotal
              runningTotal /= 10;                     # average the last 10 IBI values 
              BPM = 60000/runningTotal;               # how many beats can fit into a minute? that's BPM!
              print ('BPM: {}'.format(BPM))

        if Signal < thresh and Pulse == True :   # when the values are going down, the beat is over
            Pulse = False;                         # reset the Pulse flag so we can do it again
            amp = P - T;                           # get amplitude of the pulse wave
            thresh = amp/2 + T;                    # set thresh at 50% of the amplitude
            P = thresh;                            # reset these for next time
            T = thresh;

        if N > 2500 :                          # if 2.5 seconds go by without a beat
            thresh = 512;                          # set thresh default
            P = 512;                               # set P default
            T = 512;                               # set T default
            lastBeatTime = sampleCounter;          # bring the lastBeatTime up to date        
            firstBeat = True;                      # set these to avoid noise
            secondBeat = False;                    # when we get the heartbeat back
            print ("no beats found")

        time.sleep(0.005)

** 我正在尝试添加读数的 kivy 应用程序**

另一个.py文件

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.lang import Builder






class MainScreen(Screen):
   pass

class ScreenThermo(Screen):

    def __init__(self,**kwargs):
        super(ScreenThermo, self).__init__(**kwargs)
        Clock.schedule_interval(self.displayHR, 10)


    def displayHR(self,dt):
        heartbeat = 'BPM: {}'.format(BPM)
        pulse = heartbeat
        self.manager.screen_thermo.ids.TempLabel.text = str(pulse)


    def on_enter(self, *args):
        self.__init__()
        pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("heartk.kv")

class MenuApp(App):
    def build(self):
       return presentation 

if __name__ == '__main__':
    MenuApp().run()

.kv 文件

ScreenManagement:    
    id: screen_manager
    screen_thermo: screen_thermo
    MainScreen:
    ScreenThermo:
        id: screen_thermo
        name: 'thermo'
        manager: screen_manager

<MainScreen>:
    name: "main"
    Label:
        text: "Welcome"
        font_size: 60
        halign: 'center'
        valign: 'middle'
        pos_hint: {'x': .01, 'y': .05}
        on_touch_down: app.root.current = "thermo"



<ScreenThermo>:
    Label:
        text: " Pulse rate"
        font_size: 50
        pos: (35, 100)


    Label:
        id: TempLabel
        font_size: 60
        halign: 'center'
        valign: 'middle'

约翰·安德森

我认为你可以通过一些小的修改来完成你想要的。首先将 a 添加StringProperty到您的ScreenThermo类中,并在该on_enter()方法中启动一个线程来运行您的Heartbeatsensot代码:

from Heartbeatsensot import hearbeatsensot

class ScreenThermo(Screen):
    BPM_string = StringProperty('BPM: Not Detected')

    def on_enter(self, *args):
        Thread(target=hearbeatsensot, args=(self,)).start()

在您的kv文件中,添加对 new 的引用StringProperty

<ScreenThermo>:
    Label:
        text: " Pulse rate"
        font_size: 50
        pos: (35, 100)


    Label:
        id: TempLabel
        text: root.BPM_string  # references new StringProperty
        font_size: 60
        halign: 'center'
        valign: 'middle'

现在你只需要把你想显示的任何东西TempLabel放到BPM_string属性中。为此,请更改Heartbeatsensot.py为定义可在Thread. 只需if __name__ == '__main__':将该文件中的替换def hearbeatsensot(screenThermo):如下所示:

import time
# Import the ADS1x15 module.
import Adafruit_ADS1x15


def hearbeatsensot(screenThermo):

    adc = Adafruit_ADS1x15.ADS1015()
    # initialization
    GAIN = 2/3
    .
    .
    .
    screenThermo.BPM_string = 'BPM: 65'
    .
    .
    .

然后,在该方法中,只需使用 somethink like screenThermo.BPM_string = 'BPM: 65'(或您想将其设置为的任何内容)。到基准BPM_stringkv的文件将自动建立一个结合更新TempLabel每当BPM_string被修改。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在IBM bluemix中添加传感器标签?

来自分类Dev

Moto 360中的传感器列表

来自分类Dev

在Kivy for Python中按下按钮时更新标签的文本

来自分类Dev

速度传感器程序,循环,python 3,A级

来自分类Dev

转换来自连接到XBee的温度传感器的读数

来自分类Dev

Kivy标签中的折线不起作用(Python)

来自分类Dev

python中的传感器数据日志csv

来自分类Dev

如何在Kivy + Python中读取NFC标签?

来自分类Dev

Webots与Pycharm中的马达/传感器

来自分类Dev

如何在Python Kivy应用程序中打开pdf?

来自分类Dev

消除传感器测量中的峰值

来自分类Dev

如何在kivy应用程序的KV文件中显示来自Python的变量值

来自分类Dev

在python中获取kivy小部件的类型(标签,按钮等)

来自分类Dev

使用Python Matplotlib在3D轴上为振动传感器读数创建动画散点图

来自分类Dev

当脚本在tmux中运行时使用python写入传感器读取

来自分类Dev

如何在IBM bluemix中添加传感器标签?

来自分类Dev

获取Android中的传感器列表

来自分类Dev

同步来自不同传感器的数据

来自分类Dev

Kivy-从python端更新kv代码中的标签

来自分类Dev

Kivy用python更改标签文本

来自分类Dev

无法在Kivy Python中动态更改标签文本

来自分类Dev

覆盆子中的传感器读数

来自分类Dev

如何使用 Kivy 在 Python 中的 TabbedPanel 中排列标签、条目

来自分类Dev

Kivy - 用传感器数据更新标签?

来自分类Dev

带有文本变量的 Python Kivy 标签

来自分类Dev

Python 在传感器的 DLL 中调用 SDK 函数,该函数采用指针 IP 和端口并返回 void*(void* 配置文件传感器的句柄。)

来自分类Dev

通过 numpy 将传感器读数存储到数据文件(python 2.7)

来自分类Dev

将 IMU 传感器(加速度计、陀螺仪)读数及其时间戳导出到数据文件中

来自分类Dev

实时传感器读数存储在数据文件中

Related 相关文章

  1. 1

    如何在IBM bluemix中添加传感器标签?

  2. 2

    Moto 360中的传感器列表

  3. 3

    在Kivy for Python中按下按钮时更新标签的文本

  4. 4

    速度传感器程序,循环,python 3,A级

  5. 5

    转换来自连接到XBee的温度传感器的读数

  6. 6

    Kivy标签中的折线不起作用(Python)

  7. 7

    python中的传感器数据日志csv

  8. 8

    如何在Kivy + Python中读取NFC标签?

  9. 9

    Webots与Pycharm中的马达/传感器

  10. 10

    如何在Python Kivy应用程序中打开pdf?

  11. 11

    消除传感器测量中的峰值

  12. 12

    如何在kivy应用程序的KV文件中显示来自Python的变量值

  13. 13

    在python中获取kivy小部件的类型(标签,按钮等)

  14. 14

    使用Python Matplotlib在3D轴上为振动传感器读数创建动画散点图

  15. 15

    当脚本在tmux中运行时使用python写入传感器读取

  16. 16

    如何在IBM bluemix中添加传感器标签?

  17. 17

    获取Android中的传感器列表

  18. 18

    同步来自不同传感器的数据

  19. 19

    Kivy-从python端更新kv代码中的标签

  20. 20

    Kivy用python更改标签文本

  21. 21

    无法在Kivy Python中动态更改标签文本

  22. 22

    覆盆子中的传感器读数

  23. 23

    如何使用 Kivy 在 Python 中的 TabbedPanel 中排列标签、条目

  24. 24

    Kivy - 用传感器数据更新标签?

  25. 25

    带有文本变量的 Python Kivy 标签

  26. 26

    Python 在传感器的 DLL 中调用 SDK 函数,该函数采用指针 IP 和端口并返回 void*(void* 配置文件传感器的句柄。)

  27. 27

    通过 numpy 将传感器读数存储到数据文件(python 2.7)

  28. 28

    将 IMU 传感器(加速度计、陀螺仪)读数及其时间戳导出到数据文件中

  29. 29

    实时传感器读数存储在数据文件中

热门标签

归档