将模拟读数转换为心跳?

时间:2016-10-16 00:53:39

标签: python raspberry-pi raspberry-pi3

我将mcp3008 ADC连接到我的Raspberry Pi。 我从这里按照Adafruit的教程, https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008

并成功测试了此处发布的simeple测试程序,

https://github.com/adafruit/Adafruit_Python_MCP3008/tree/master/examples

当我运行simpletest.py程序时,我得到以下输出,

|    0 |    1 |    2 |    3 |    4 |    5 |    6 |    7 |
---------------------------------------------------------
|  813 |  307 |  276 |  277 |  254 |  228 |  231 |  609 |
|  813 |    0 |    0 |    0 |    0 |    0 |    0 |  503 |
|  813 |   50 |  185 |  289 |  292 |  289 |  306 |  514 |
|  813 |  274 |  183 |  153 |   47 |    0 |    0 |  511 |
|  813 |    0 |    0 |    9 |   28 |   44 |  154 |  513 |
|  813 |  302 |  278 |  258 |  226 |  209 |  217 |  507 |
|  813 |    0 |    0 |    0 |    0 |    0 |    0 |  521 |
|  813 |  144 |  292 |  290 |  286 |  285 |  304 |  476 |

有两个心跳传感器,一个位于Channel 0,一个位于Channel 7。 我正在使用以下心跳传感器, https://www.sparkfun.com/products/11574

当我将传感器放在我的手指上并轻轻挤压时,读数开始减少并减少到700至690.

然后我使用以下代码显示心跳,

http://antoniolopes.info/blog/2013/03/06/pulse_sensing_raspberry_pi/

以下是正确缩进后的代码,

#!/usr/bin/env python

# Written by Limor "Ladyada" Fried for Adafruit Industries, (c) 2015
# This code is released into the public domain

import time
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
DEBUG = 1

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
        if ((adcnum > 7) or (adcnum < 0)):
                return -1
        GPIO.output(cspin, True)

        GPIO.output(clockpin, False)  # start clock low
        GPIO.output(cspin, False)     # bring CS low

        commandout = adcnum
        commandout |= 0x18  # start bit + single-ended bit
        commandout <<= 3    # we only need to send 5 bits here
        for i in range(5):
                if (commandout & 0x80):
                        GPIO.output(mosipin, True)
                else:
                        GPIO.output(mosipin, False)
                commandout <<= 1
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)

        adcout = 0
        # read in one empty bit, one null bit and 10 ADC bits
        for i in range(12):
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)
                adcout <<= 1
                if (GPIO.input(misopin)):
                        adcout |= 0x1

        GPIO.output(cspin, True)

        adcout >>= 1       # first bit is 'null' so drop it
        return adcout

# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 18
SPIMISO = 23
SPIMOSI = 24
SPICS = 25

# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)

# 10k trim pot connected to adc #0
pulse_adc = 7

THRESH = 512
#pulse detection
pulse = False


while True:
    #read the analog pin
    analog_value = readadc(pulse_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
    #draw the equivalent number of points in an attempt to draw a vertical pulse sensing graph
    for i in range(analog_value / 100):
        print ".",
        print analog_value
        #detect beats
        if (analog_value > THRESH):
            if (pulse == False):
                pulse = True
                print "Beat"
            else:
                print ""
        else:
            pulse = False
            print ""
        time.sleep(0.5)

并运行代码应该给出以下输出,

. . .
. .
. . .
. . . . . . Beat
. . . . . . . . .
. . . . . . . . .
. .
. . .
. . .
. . .
. . . .
. . . . . . . . . Beat

但我只是得到了这个,

.
.
.
.
.
.

值得一提的是,我也尝试将传感器捆在我的手指上,但它也没有工作。

有人可以告诉我如何将模拟读数转换为BPM中的心跳值,如何计算它们?

0 个答案:

没有答案