首先介紹一下Pulse Sensor
PulseSensor 脈搏傳感器介紹
基本參數
供電電壓: | 3.3~5V |
---|---|
檢測信號類型: | 光反射信號(PPG) |
輸出信號類型: | 模擬信號 |
輸出信號大小: | 0~VCC |
電流大小: | ~4ma(5v 下) |
功能原理
PulseSensor 是一款用於脈搏心率測量的光電反射式模擬傳感器。將其佩戴於手指、耳垂等處,利用人體組織在血管搏動時造成透光率不同來進行脈搏測量。傳感器對光電信號進行濾波、放大,最終輸出模擬電壓值。單片機通過將採集到的模擬信號值轉換爲數字信號,再通過簡單計算就可以得到心率數值。
PulseSensor 是一款開源硬件,目前國外官網上已有其對應的開源 arduino 程序和上位機 Processing 程序,其適用於心率方面的科學研究和教學演示,也非常適合用於二次開發。 網上關於傳感器的 arduino 資料已經十分豐富(畢竟同爲開源硬件),本文采用 STM32F407系列芯片 的 ADC 模塊讀取並處理傳感器數據,實現心率測量。
引腳定義
傳感器只有三個引腳,分別爲信號輸出 S 腳 、電源正極 VCC 以及電源負極 GND,供電電壓爲 3.3V - 5V,可通過杜邦線與開發板連接。上電後, 傳感器會不斷從 S 腳輸出採集到的電壓模擬值。需要注意的是,印有心形的一面纔是與手指接觸面,在測量時要避免接觸佈滿元件的另一面,否則會影響信號準確性。
Cube配置
生成代碼
完善代碼
main.C裏邊完成
1、/* USER CODE BEGIN Includes */和/* USER CODE END Includes */中間添加
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
2、/* USER CODE BEGIN PV */和/* USER CODE END PV */中間添加
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
//==============心率============================
// these variables are volatile because they are used during the interrupt service routine!
#define true 1
#define false 0
int BPM; // used to hold the pulse rate
int Signal; // holds the incoming raw data
int IBI = 600; // holds the time between beats, must be seeded!
unsigned char Pulse = false; // true when pulse wave is high, false when it's low
unsigned char QS = false; // becomes true when Arduoino finds a beat.
int rate[10]; // array to hold last ten IBI values
unsigned long sampleCounter = 0; // used to determine pulse timing
unsigned long lastBeatTime = 0; // used to find IBI
int P =512; // used to find peak in pulse wave, seeded
int T = 512; // used to find trough in pulse wave, seeded
int thresh = 512; // used to find instant moment of heart beat, seeded
int amp = 100; // used to hold amplitude of pulse waveform, seeded
int Num;
unsigned char firstBeat = true; // used to seed rate array so we startup with reasonable BPM
unsigned char secondBeat = false; // used to seed rate array so we startup with reasonable BPM
//===============心率完成=========================
/* USER CODE END PV */
3、/* USER CODE BEGIN PFP */和/* USER CODE END PFP */中間添加
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
void sendDataToProcessing(char symbol, int dat );
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* USER CODE END PFP */
4、 /* USER CODE BEGIN 2 */和 /* USER CODE END 2 */中間添加
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim3);
/* USER CODE END 2 */
5、
/* USER CODE BEGIN WHILE */和 /* USER CODE END WHILE */中間添加
/* USER CODE BEGIN WHILE */
while (1)
{
sendDataToProcessing('S', Signal); // send Processing the raw Pulse Sensor data
if (QS == true)
{
sendDataToProcessing('B',BPM); // send heart rate with a 'B' prefix
sendDataToProcessing('Q',IBI); // send time between beats with a 'Q' prefix
QS = false; // reset the Quantified Self flag for next time
}
HAL_Delay(20); //delay for 20ms
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
6、
/* USER CODE BEGIN 4 */和/* USER CODE END 4 */中間添加/*
/* USER CODE BEGIN 4 */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
unsigned int runningTotal;
if(htim->Instance==htim3.Instance)
{
Signal=HAL_ADC_GetValue(&hadc1)>>2; // read the Pulse Senso
sampleCounter += 2; // keep track of the time in mS with this variable
Num = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise
HAL_ADC_Start(&hadc1); //restart ADC conversion
// find the peak and trough of the pulse wave
if(Signal < thresh && Num > (IBI/5)*3){ // 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 && 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 (Num > 250){ // avoid high frequency noise
if ( (Signal > thresh) && (Pulse == false) && (Num > (IBI/5)*3) ){
Pulse = true; // set the Pulse flag when we think there is a pulse
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET); // turn on pin 13 LED
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(int i=0; i<=9; i++){ // 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
// sei(); // enable interrupts again
return; // IBI value is unreliable so discard it
}
// keep a running total of the last 10 IBI values
runningTotal = 0; // clear the runningTotal variable
for(int i=0; i<=8; i++){ // 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!
QS = true; // set Quantified Self flag
// QS FLAG IS NOT CLEARED INSIDE THIS ISR
}
}
if (Signal < thresh && Pulse == true){ // when the values are going down, the beat is over
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET); // turn off pin 13 LED
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 (Num > 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
}
}
}
void sendDataToProcessing(char symbol, int dat )
{
putchar(symbol); // symbol prefix tells Processing what type of data is coming
printf("%d\r\n",dat); // the data to send culminating in a carriage return
}
/* USER CODE END 4 */
//順利完成 Pulse Senso傳感器移植 在此記錄供以後查看
最後附粘貼一個別人的心率算法
讀取傳感器電壓值 —— STM32 ADC 功能配置
硬件配置
開發板使用的是公司的 M4 板子,傳感器 3.3V 供電,信號採集選用 ADC1 的 通道 2,硬件連接如下:
開發板 | 傳感器 |
---|---|
PA2 | S |
3V3 | + |
GND | - |
把 PA2 用作模擬功能,配置 ADC 爲 12 位分辨率,單次轉換,並設置轉換序列長度爲 1,首次轉換通道 2。爲確保數據準確性,選擇APB2 時鐘 6 分頻作爲 ADC 時鐘(即 84M / 6 = 14M),採樣時間 480 個週期(使得采樣時間更加充分),最後使能 ADC。初始化函數如下:
ADC。初始化函數如下:
/******************** ADC通道2初始化函數 ************************/void ADC_AN2_Init(void){
/* 設置ADC功能對應的GPIO端口 */
RCC->AHB1ENR |= 1 << 0;
GPIOA->MODER &= ~(3 << (2 * 2));
GPIOA->MODER |= 3 << (2 * 2);
/* 配置ADC採樣模式 */
RCC->APB2ENR |= 1 << 8; //使能ADC模塊時鐘 ADC1->CR1 &= ~(3 << 24); //12位分辨率 ADC1->CR1 &= ~(1 << 8); //非掃描模式 ADC1->CR2 &= ~(3 << 28); //禁止外部觸發 ADC1->CR2 &= ~(1 << 11); //右對齊 ADC1->CR2 &= ~(1 << 1); //單次轉換 ADC->CCR &= ~(3 << 16);
ADC->CCR |= 2 << 16; //6分頻 ADC1->SMPR2 &= ~(0x07 << 6);
ADC1->SMPR2 |= 0x07 << 6; //480採樣週期 ADC1->SQR1 &= ~(0x0f << 20); //1次轉換 ADC1->SQR3 &= ~(0x1f << 0);
ADC1->SQR3 |= 0x02 << 0; //轉換的通道爲通道2
/* 使能ADC */
ADC1->CR2 |= 1 << 0; //開啓ADC}
編寫好初始化函數後還需要寫一個進行 AD 轉換的函數,這也是我們功能的核心。思路是通過將十次 AD 轉換值進行冒泡排序,然後掐頭去尾求平均值作爲最後的轉換輸出值,程序如下:
/******************** ADC通道2轉換函數 ************************/u16 Get_ADC_1_CH2(void){
u8 i,j;
u16 buff[10] = {0};
u16 temp;
for(i = 0; i < 10; i ++)
{
/* 開始轉換 */
ADC1->CR2 |= 1 << 30;
/* 等待轉換結束 */
while( !(ADC1->SR & (1 << 1)) )
{
/* 等待轉換接收 */
}
buff[i] = ADC1->DR; //讀取轉換結果 }
/* 把讀取值的數據按從小到大的排列 */
for(i = 0; i < 9; i++)
{
for(j = 0 ; j < 9-i; j++)
{
if(buff[j] > buff[j+1])
{
temp = buff[j];
buff[j] = buff[j+1];
buff[j+1] = temp;
}
}
}
/* 求平均值 */
temp = 0;
for(i = 1; i < 9; i++)
{
temp += buff[i];
}
return temp/8;}
串口打印,驗證數據讀取
是驢是馬得拉出來溜溜,配好的 ADC 能不能用也要經過檢驗。方法是把從傳感器讀到的轉換值在串口打印,以此測試 ADC 轉換是否工作正常。爲了模擬波形的效果,編寫如下波形打印函數 —— 將讀出來的數據縮小適當倍數後,用同一行的星號數量來表示。
void Print_Wave(void){
int temp, i;
temp = Get_ADC_1_CH2() / 20; // 縮小一個倍數
for (i = 0; i < temp; i++)
printf("*");
printf ("\r\n");}
在主函數的 while (1)
循環中不斷調用 Print_Wave()
函數在串口打印輸出,每次打印延時一段時間,代碼如下:
int main(void){
Usrat_1_Init(84,115200,0);
Timer_6_Init();
ADC_AN2_Init();
while(1)
{
Print_Wave();
Timer_6_Delay_ms(5); // 延時 5 ms }}
把開發板連接電腦,下載程序後打開串口工具接收數據,通過對傳感器測量面綠光的遮擋,可在串口看到用字符打印的波形,波峯波谷清晰可見,並不懂波動,證明 ADC 讀取到了傳感器輸出的模擬電壓信號。效果如下圖:
計算心率值 —— 採樣數據處理算法
心率指的是一分鐘內的心跳次數,得到心率最笨的方法就是計時一分鐘後數有多少次脈搏。但這樣的話每次測心率都要等上個一分鐘纔有一次結果,效率極低。另外一種方法是,測量相鄰兩次脈搏的時間間隔,再用一分鐘除以這個間隔得出心率。這樣的好處是可以實時計算脈搏,效率高。由此引出了 IBI
和 BPM
兩個值的概念:
IBI: 相鄰兩次脈搏的時間間隔(單位:ms) BPM(beats per minute):心率,一分鐘內的心跳次數
且:BPM = 60 / IBI
從網上找來的 arduino 開源算法複雜的一匹,看了一遍感覺一頭霧水(反正我暫時沒看懂)。由上面的分析可以得出,我們的最終目的就是要求出 IBI 的值,並通過 IBI 計算出實時心率。既然知道原理了那就自己來把算法實現吧。
核心操作 —— 識別一個脈搏信號
無論是採用計數法還是計時法,只有能識別出一個脈搏,才能數出一分鐘內脈搏數或者計算兩個相鄰脈搏之間的時間間隔。那怎麼從採集的電壓波形數據判斷是不是一個有效的脈搏呢?
顯然,可以通過檢測波峯來識別脈搏。最簡單粗暴的方法是設定一個閾值,當讀取到的信號值大於此閾值時便認爲檢測一個脈搏。似乎用一個 if
語句就輕輕鬆鬆解決。但,事情真的有那麼簡單麼?
其實這裏存在兩個問題。
問題一:閾值的選取
作爲判斷的參考標尺,閾值該選多大?10?100?還是1000?我們不得而知,因爲波形的電壓範圍是不確定的,振幅有大有小並且會改變,根本不能用一個寫死的值去判斷。就像下面這張圖一樣:
可以看出,兩個形狀相同波形的檢測結果截然不同 —— 同樣是波峯,在不同振幅的波形中與閾值比較的結果存在差異。實際情況正是如此:傳感器輸出波形的振幅是在不斷隨機變化的,想用一個固定的值去判定波峯是不現實的。
既然固定閾值的方法不可取,那自然想到改變閾值 —— 根據信號振幅調整閾值,以適應不同信號的波峯檢測。通過對一個週期內的信號多次採樣,得出信號的最高與最低電壓值,由此算出閾值,再用這個閾值對採集的電壓值進行判定,考慮是否爲波峯。也就是說電壓信號的處理分兩步,首先動態計算出參考閾值,然後用用閾值對信號判定、識別一個波峯。
問題二:特徵點識別
上面得出的是一段有效波形,而計算 IBI 只需要一個點。需要從一段有效信號上選取一個點,這裏暫且把它稱爲特徵點,這個特徵點代表了一個有效脈搏,只要能識別到這個特徵點,就能在一個脈搏到來時觸發任何動作。
通過記錄相鄰兩個特徵點的時間並求差值,計算 IBI 便水到渠成。那這個特徵點應該取在哪個位置呢,從官網算法說明可以看出,官方開源 arduino 代碼的 v1.1 版本是選取信號上升到振幅的一半作爲特徵點,我們可以捕獲這個特徵點作爲一個有效脈搏的標誌,然後計算 IBI。
算法整體框架與代碼實現
分析得出算法的整體框架如下:
-
緩存一個波形週期內的多次採樣值,求出最大最小值,計算出振幅中間值作爲信號判定閾值
-
通過把當前採樣值和上一採樣值與閾值作比較,尋找到「信號上升到振幅中間位置」的特徵點,記錄當前時間
-
尋找下一個特徵點並記錄時間,算出兩個點的時間差值,即相鄰兩次脈搏的時間間隔 IBI
-
由 IBI 計算心率值 BPM
代碼如下,程序中使用一個 50 長度的數組進行採樣數據緩存,在主函數 while (1)
中以 20ms 的週期不斷執行採樣、數據處理,其中的條件語句 if (PRE_PULSE == FALSE && PULSE == TRUE)
就表示找到了特徵點、識別出一次有效脈搏,串口輸出心率計算結果。
int main(void){
Usrat_1_Init(84,115200,0);
Timer_6_Init();
ADC_AN2_Init();
while(1)
{
//Print_Wave();
preReadData = readData; // 保存前一次值 readData = Get_ADC_1_CH2(); // 讀取AD轉換值
if ((readData - preReadData) < filter) // 濾除突變噪聲信號干擾 data[index++] = readData; // 填充緩存數組
if (index >= BUFF_SIZE)
{
index = 0; // 數組填滿,從頭再填
// 通過緩存數組獲取脈衝信號的波峯、波谷值,並計算中間值作爲判定參考閾值 max = Get_Array_Max(data, BUFF_SIZE);
min = Get_Array_Min(data, BUFF_SIZE);
mid = (max + min)/2;
filter = (max - min) / 2;
}
PRE_PULSE = PULSE; // 保存當前脈衝狀態 PULSE = (readData > mid) ? TRUE : FALSE; // 採樣值大於中間值爲有效脈衝
if (PRE_PULSE == FALSE && PULSE == TRUE) // 尋找到「信號上升到振幅中間位置」的特徵點,檢測到一次有效脈搏 {
pulseCount++;
pulseCount %= 2;
if(pulseCount == 1) // 兩次脈搏的第一次 {
firstTimeCount = timeCount; // 記錄第一次脈搏時間 }
if(pulseCount == 0) // 兩次脈搏的第二次 {
secondTimeCount = timeCount; // 記錄第二次脈搏時間 timeCount = 0;
if ( (secondTimeCount > firstTimeCount))
{
IBI = (secondTimeCount - firstTimeCount) * SAMPLE_PERIOD; // 計算相鄰兩次脈搏的時間,得到 IBI BPM = 60000 / IBI; // 通過 IBI 得到心率值 BPM
if (BPM > 200) //限制BPM最高顯示值 BPM = 200;
if (BPM < 30) //限制BPM最低顯示值 BPM=30;
}
}
printf("SIG = %d IBI = %d, BMP = %d\r\n\r\n", readData, IBI, BPM); // 串口打印調試
// printf("B%d\r\n", BPM); // 上位機B數據發送
// printf("Q%d\r\n", IBI); // 上位機Q數據發送 }
SIG = readData - 1500; // 脈象圖數值向下偏移,調整上位機圖像
// printf("S%d\r\n", SIG); // 上位機S數據發送
timeCount++; // 時間計數累加 Timer_6_Delay_ms(SAMPLE_PERIOD); // 延時再進行下一週期採樣 }}