Skip to main content

This is heartrate driver for HaaS Python. HaaS Python is forked from micropython.

Project description

HaaS Python介绍

HaaS Python是阿里云IoT团队最新研发的一套低代码编程框架,兼容MicroPython编程规范,依托HaaS平台软硬件积木提供AI、支付、蓝牙配网、云连接、UI等物联网场景常用的能力,从而解决了物联网应用开发难的问题。有了Python轻应用框架,物联网编程不再局限于专业软件开发人员,一般的技术员也可以快速实现复杂的物联网需求。 更多HaaS Python介绍和开发资料见HaaS Python官网,创意案例, 硬件积木

HaaS Python is a set of low-code programming frameworks newly developed by Alibaba Cloud IoT team. It is compatible with MicroPython programming specifications. It relies on the hardware and software building blocks of the HaaS platform to provide capabilities commonly used in IoT scenarios such as AI, payment, Bluetooth network configuration, cloud connection, and UI. Solve the difficult problem of IoT application development. With the Python light application framework, IoT programming is no longer limited to professional software developers, and general technicians can quickly implement complex IoT requirements. For more HaaS Python introduction and development materials, see HaaS Python official website, Creative Case, Hardware Building Blocks

安装方法

Download the firmware corresponding to the development board at HaaS Python website and complete the firmware burning

  • 在开发板上运行以下命令完成安装和使用,如果您在使用过程中遇到问题,欢迎在github上向我们提交issue,我们的工程师会及时解答

Run the following commands on the development board to complete the installation and use. If you encounter problems during use, please submit an issue to us on github, Our engineers will answer in time

import upip
upip.install("haas-python-heartrate")
import haas-python-heartrate

心率传感器 - MDL0025

一、产品简介

PulseSensor(MDL0025) 是一款用于脉搏心率测量的光电反射式模拟传感器。将其佩戴于手指或耳垂等处,通过导线连接可将采集到的模拟信号传输给HaaS开发版用来转换为数字信号,再通过开发板简单计算后就可以得到心率数值,此外还可将脉搏波形上传到电脑上显示波形。PulseSensor 是一款开源硬件,其适用于心率方面的科学研究和教学演示,也非常适合用于二次开发。

引脚定义:

  • +:接 3.3V/5V
  • -:接 GND
  • S:接 ADC

二、技术参数

  • 工作电压:3.3 V 或 5V DC
  • 电路板直径:16mm
  • 电路板厚度:1.6mm(普通PCB 板厚度)
  • LED 峰值波长:515nm
  • 输出信号类型:模拟信号
  • 输出信号大小:0~3.3V(3.3V电源) 0~5V(5V电源)

三、软件接口

MDL0025心率传感器 HaaS Python驱动:下载地址

class HEARTBEAT(adcdev[,rled=None,gled=None,bled=None,highBPM=150,lowBPM=100]) - 创建heartbeart驱动对象

  • 函数原型:

heartbeatdev = HEARTBEAT(adcdev=adcobj,rled=gpioobj01,gled=gpioobj02,bled=gpioobj03,highBPM=150,lowBPM=100)

  • 参数说明:
参数 类型 必选参数? 说明
adcdev ADC 调用此函数前需确保 adcdev 对象已经处于 open 状态
rled GPIO 调用此函数前需确保 rled 对象已经处于 open 状态
gled GPIO 调用此函数前需确保 gled 对象已经处于 open 状态
bled GPIO 调用此函数前需确保 bled 对象已经处于 open 状态
highBPM int 心率报警上限
lowBPM int 心率报警下限
  • 返回值: heartbeatdev 对象创建成功,返回 heartbeatdev 对象;heartbeatdev 对象创建失败,抛出 Exception

  • 示例代码:

from driver import ADC
from driver import GPIO
import heartbeat

BPM_high=150
BPM_low=100

#初始化ADC类型的设备
adcObj = ADC()
adcObj.open('heartbeat')      # 按照 board.json 中名为 "heartbeat" 的设备节点的配置参数

#初始化GPIO类型的设备
redled = GPIO()
blueled = GPIO()
greenled = GPIO()

redled.open('led_r')          # board.json中led_r节点定义的GPIO,对应esp32外接的的红灯
blueled.open('led_b')         # board.json中led_b节点定义的GPIO,对应esp32外接的上的蓝灯
greenled.open('led_g')        # board.json中led_g节点定义的GPIO,对应esp32外接的上的绿灯

heartbeatdev = HEARTBEAT(adcdev = adcobj, rled=redled, bled=blueled, gled=greenled, highBPM=BPM_high, lowBPM=BPM_low)
print("heartbeat inited!")
  • 输出:
heartbeat inited!

start - 开始测量心率

  • 函数功能: 启动心率传感器开始测量心率

  • 函数原型:

heartbeatdev.start()

  • 参数说明: 无

  • 返回值 无

  • 示例:

from driver import ADC
from driver import GPIO
import heartbeat

BPM_high=150
BPM_low=100

#初始化ADC类型的设备
adcObj = ADC()
adcObj.open('heartbeat')      # 按照 board.json 中名为 "heartbeat" 的设备节点的配置参数

#初始化GPIO类型的设备
redled = GPIO()
blueled = GPIO()
greenled = GPIO()

redled.open('led_r')          # board.json中led_r节点定义的GPIO,对应esp32外接的的红灯
blueled.open('led_b')         # board.json中led_b节点定义的GPIO,对应esp32外接的上的蓝灯
greenled.open('led_g')        # board.json中led_g节点定义的GPIO,对应esp32外接的上的绿灯

heartbeatdev = HEARTBEAT(adcdev = adcobj, rled=redled, bled=blueled, gled=greenled, highBPM=BPM_high, lowBPM=BPM_low)

heartbeatdev.start()
  • 输出:

stop - 停止测量心率

  • 函数功能: 停止心率传感器测量心率

  • 函数原型:

heartbeatdev.stop()

  • 参数说明: 无

  • 返回值 无

  • 示例:

from driver import ADC
from driver import GPIO
import heartbeat

BPM_high=150
BPM_low=100

#初始化ADC类型的设备
adcObj = ADC()
adcObj.open('heartbeat')      # 按照 board.json 中名为 "heartbeat" 的设备节点的配置参数

#初始化GPIO类型的设备
redled = GPIO()
blueled = GPIO()
greenled = GPIO()

redled.open('led_r')          # board.json中led_r节点定义的GPIO,对应esp32外接的的红灯
blueled.open('led_b')         # board.json中led_b节点定义的GPIO,对应esp32外接的上的蓝灯
greenled.open('led_g')        # board.json中led_g节点定义的GPIO,对应esp32外接的上的绿灯

heartbeatdev = HEARTBEAT(adcdev = adcobj, rled=redled, bled=blueled, gled=greenled, highBPM=BPM_high, lowBPM=BPM_low)

heartbeatdev.stop()
  • 输出:

getBPM() - 测量心率

  • 函数功能: 返回测量的心率值

  • 函数原型:

heartbeatdev.getBPM()

  • 参数说明: 无

  • 返回值: 返回测量的心率值,单位:次数每分钟(BPM)

  • 示例:

from driver import ADC
from driver import GPIO
import heartbeat

BPM_high=150
BPM_low=100

#初始化ADC类型的设备
adcObj = ADC()
adcObj.open('heartbeat')      # 按照 board.json 中名为 "heartbeat" 的设备节点的配置参数

#初始化GPIO类型的设备
redled = GPIO()
blueled = GPIO()
greenled = GPIO()

redled.open('led_r')          # board.json中led_r节点定义的GPIO,对应esp32外接的的红灯
blueled.open('led_b')         # board.json中led_b节点定义的GPIO,对应esp32外接的上的蓝灯
greenled.open('led_g')        # board.json中led_g节点定义的GPIO,对应esp32外接的上的绿灯

heartbeatdev = HEARTBEAT(adcdev = adcobj, rled=redled, bled=blueled, gled=greenled, highBPM=BPM_high, lowBPM=BPM_low)
heartbeatdev.start()

heartrate=heartbeatdev.getBPM()
print("heartrate is ", heartrate, "BPM")
  • 输出:
heartrate is 80 BPM

四、接口案例

此使用实例在 board.json 中定义了名为 heartbeat 的 ADC 类型的对象。名为led_b, led_g, led_r的GPIO对象。 在 Python 脚本中周期性的检查当前噪音值结果并打印在日志中。

注: leb_b, led_g, led_r 分别对应蓝色,绿色,红色三色LED灯,如果这三个LED灯设备存在并且在心率传感器驱动初始化时作为参数传入,则心率传感器驱动会根据心率值让不同颜色的LED灯闪烁:心率值大于上限值时,红灯按实测心率为频率闪烁; 心率在上限和下限之间时,绿灯按照实测心率为频率闪烁;心率低于下限值时,蓝灯按实测心率为频率闪烁。

  • 代码:
# board.json配置:{
    "name": "haasedu",
    "version": "1.0.0",
    "io": {

      "led_b": {
        "type": "GPIO",
        "port": 19,
        "dir": "output",
        "pull": "pullup"
      },

      "led_g": {
        "type": "GPIO",
        "port": 18,
        "dir": "output",
        "pull": "pullup"
      },

      "led_r": {
        "type": "GPIO",
        "port": 17,
        "dir": "output",
        "pull": "pullup"
      },

      "heartbeat": {
        "type": "ADC",
        "port": 0,
        "sampling": 12000000,
        "atten": 3,
        "width": 3
      }
    },
    "debugLevel": "ERROR",
    "repl": "disable"
  }
# Python代码
import utime
from driver import ADC                           # ADC类,通过微处理器的ADC模块读取ADC通道输入电压
from driver import GPIO

BPM_high=150
BPM_low=100
redled = GPIO()
blueled = GPIO()
greenled = GPIO()

redled.open('led_r')          # board.json中led_r节点定义的GPIO,对应esp32外接的的红灯
blueled.open('led_b')         # board.json中led_b节点定义的GPIO,对应esp32外接的上的蓝灯
greenled.open('led_g')        # board.json中led_g节点定义的GPIO,对应esp32外接的上的绿灯

adcobj = ADC()
adcobj.open("heartbeat")

heartbeatdev = HEARTBEAT(adcdev = adcobj, rled=redled, bled=blueled, gled=greenled, highBPM=BPM_high, lowBPM=BPM_low)
heartbeatdev.start()
for i in range(100):
    print ("heart rate is: ", heartbeatdev.BPM)
    utime.sleep(0.5)
heartbeatdev.stop()
  • 输出:
...
heart rate is:  80
heart rate is:  88
heart rate is:  99
heart rate is:  100
...

五、通信协议

主控芯片和心率测量传感器之间通信为 ADC 通讯,获取电压值 mV

六、工作原理

如下图所示,传感器利用人体组织在血管搏动时造成透光率不同,将透光率转化为电信号来进行脉搏测量的。

光的透光率发生改变,此时光感受器接收经人体组织反射光线,转变为电信号并将其放大输出。

传感器电路原理图如下:

参考文献及购买链接

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

haas-python-heartrate-0.0.4.tar.gz (10.2 kB view details)

Uploaded Source

File details

Details for the file haas-python-heartrate-0.0.4.tar.gz.

File metadata

  • Download URL: haas-python-heartrate-0.0.4.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.5

File hashes

Hashes for haas-python-heartrate-0.0.4.tar.gz
Algorithm Hash digest
SHA256 7f04eb51e4acb716c670adc733dc3e77b3f736073a5dd579a697a3a146c692f3
MD5 bd8edb3fc46c700bc28942303896ad17
BLAKE2b-256 621066f001b2ef6580c7795e4963d011d09d49d6eef5d40a9f8e0042da6e5c41

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page