• STM32+ MAX30102通过指尖测量心率+血氧饱和度


    一、前言

            重要的事情放在最前面:max30102只适用于指尖手指测量,不适用与手腕手指测量,如需做成可穿戴样式选择传感器的小伙伴请pass掉他,因为他只有红光和红外2种光,不够充足的数据源去运算。

            由于一些原因,本篇文章所有下载资源不收取任何积分,让你不花一分钱教你如何使用stm32驱动和使用max30102这个传感器,对大学生做毕设的很友好,如果帮助到你了,请在评论区说明有帮助,给与我能量继续为你们开源哦。

    二、环境

    stm32f1xx(我是用的是c8t6)

    keil5

    qt5.7

    三、正文

            简单的概括,从你有需求,到确认物料使用,到采购物料,到使用物料做成品,到调试使用中间会涉及很多环节,在文章开头就把最重要的说出来了,如果你想做穿戴在手腕上或贴在皮肤表面的,不适用这个传感器,他的原理是透过血管,采集图像,在经过算法分析,如果光无法透过血管,采集的数据特征不明显,那么准确率就没有了,实用性也就没有了,如果只是用在手指检测,那么可以继续往下看了。

            在网上搜索有很多很多的模块再买,资料也是有很多,在b站和各种论坛使用这种传感器成功的也有很多,都是打着开源的口号,然后背地里收钱,网上还有很多付费花积分下载的资源,下载下来也是不能直接用的,是不好使的。 

    1.电路设计

    首先附上我是用单片机的最小系统电路,这就很简单了,新手小白也能一下子就看懂

     这里我把图片缩小,单击就能方大看了,就是stm32最小系统

    然后是传感器电路图,我看网上很多模块都是使用到1.8V,在看了官方手册实际3.3V也可以为设备供电和使用了,原理图如下

     这里我把IIC信号和中断信号用0402的排阻上拉,大家伙可以用封装稍微大一些的电阻

    再有就是USB端子部分了,因为整体使用的是USB通信,通过USB连接电脑,软件端使用虚拟串口,与上位机通讯使用的是串口通讯方式,相当于省去了CH340芯片。

     最后在某创白嫖一下四层板,在焊接元器件,洗板水刷个板,硬件就干干净净的出炉了

          

     2.程序编写

            硬件完毕,就是软件的活了,软件我也是一开始在网上找现成的程序,毕竟能省去很多的研究时间,经过一两天晚上的寻找,发现网上很多程序都是骗积分的,根本不能用,再有就是带了一大堆乱七八糟的外设驱动,程序结构也是乱七八糟,这个也不是抱怨什么,都可以理解,因为编程习惯是看入门是使用的谁家教程,就习惯谁家的程序结构,比如我特别使用某原子的库函数程序结构,有些人用的是某火的结构,有些人是某xx的结构,各有各的习惯,这里不多说,继续。

            软件主要增加3个文件,分别是myiic.c  max30102.c  algorithm.c这三个程序分别是硬件iic引脚驱动程序,max30102时序逻辑驱动程序和对应采集数据算法程序,这三个文件基本把和自己设计的硬件引荐对应好,基本就没什么问题了,最主要的是main.c中程序,这里我在网上下载了10几个程序,没有一个在main函数里驱动部分写注释的,都是一模一样的,再有就是写的乱七八糟,没有任何说明的,我把main.c驱动部分写了中文注释,让你会清晰的静下心来了解程序是如何实现逻辑的(一开始我也是很懵圈,下载的里程连main调用部分都特别复杂,都不知道从哪里入手,也没有中文注释),以上三个程序在下面开源资源中。

             这里把main.c的程序发出来,因为有很多小伙伴看了这里,根本就不需要下载我的资源,就可以在自己的程序上一下子调通了。

    1. #include "sys.h"
    2. #include "delay.h"
    3. #include "string.h"
    4. #include "usart.h"
    5. #include "led.h"
    6. #include "usb_lib.h"
    7. #include "hw_config.h"
    8. #include "usb_pwr.h"
    9. #include "timer.h"
    10. #include "stdlib.h"
    11. #include "ct1711.h"
    12. #include "max30102.h"
    13. #include "algorithm.h"
    14. #include "myiic.h"
    15. //串口数据包
    16. char senddate[16]={0x5A,0xA5,0,0,0,0,0,0,0,0,0,0,0x53,0x54,0x4F,0x50};//缓存容量5A A5 温度2字节(1000倍,高前低后) 心率曲线2字节(高前低后) 心率2字节(高前低后) 心率检测状态 血氧2字节(高前低后) 血氧检测状态 53 54 4F 50
    17. //max30102参数
    18. uint32_t aun_ir_buffer[500]; //IR LED sensor data 红外数据,用于计算血氧
    19. int32_t n_ir_buffer_length=500; //data length
    20. uint32_t aun_red_buffer[500]; //Red LED sensor data 红光数据,用于计算心率曲线以及计算心率
    21. int32_t n_sp02; //SPO2 value
    22. int8_t ch_spo2_valid; //indicator to show if the SP02 calculation is valid
    23. int32_t n_heart_rate; //heart rate value
    24. int8_t ch_hr_valid; //indicator to show if the heart rate calculation is valid
    25. #define MAX_BRIGHTNESS 255
    26. int main(void)
    27. {
    28. //USB通信变量
    29. u32 len;
    30. u8 usbstatus=0;
    31. //max30102变量
    32. uint32_t un_min, un_max, un_prev_data;
    33. int i,j;
    34. int32_t n_brightness;
    35. float f_temp;
    36. u8 temp[6];
    37. //初始化系统
    38. delay_init(); //延时函数初始化
    39. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
    40. JTAG_Set(JTAG_SWD_DISABLE); //=====关闭JTAG接口
    41. JTAG_Set(SWD_ENABLE); //=====打开SWD接口 可以利用主板的SWD接口调试
    42. uart1_init(115200);
    43. LED_Init(); //初始化与LED连接的硬件接口
    44. TIM3_Int_Init(49,7199);//10Khz的计数频率,计数到50为5ms
    45. //max30102初始化
    46. max30102_init();
    47. //下面这些在主程序循环执行,在保证硬件没问题的情况下,无需在初始化中执行一次,反正也没有做什么保护和提示措施
    48. // un_min=0x3FFFF;
    49. // un_max=0;
    50. // n_ir_buffer_length=500; //缓冲区长度100存储5秒的运行在100sps的样本
    51. // //读取前500个样本,确定信号范围
    52. // for(i=0;i
    53. // while(MAX30102_INT==1); //等待直到中断引脚断言
    54. // max30102_FIFO_ReadBytes(REG_FIFO_DATA,temp);//读取传感器数据,赋值到temp中
    55. // aun_red_buffer[i] = (long)((long)((long)temp[0]&0x03)<<16) | (long)temp[1]<<8 | (long)temp[2]; // 将值合并得到实际数字
    56. // aun_ir_buffer[i] = (long)((long)((long)temp[3] & 0x03)<<16) |(long)temp[4]<<8 | (long)temp[5]; // 将值合并得到实际数字
    57. // if(un_min>aun_red_buffer[i])
    58. // un_min=aun_red_buffer[i];//更新计算最小值
    59. // if(un_max
    60. // un_max=aun_red_buffer[i];//更新计算最大值
    61. // }
    62. // //un_prev_data=aun_red_buffer[i];//获取数据最后一个数值,没有用,主程序中未马上使用被替换
    63. // //计算前500个样本(前5秒样本)后的心率和SpO2
    64. // maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);
    65. //USB虚拟串口初始化
    66. delay_ms(500);
    67. USB_Port_Set(0); //USB先断开
    68. delay_ms(200);
    69. USB_Port_Set(1); //USB再次连接
    70. Set_USBClock();
    71. USB_Interrupts_Config();
    72. USB_Init();
    73. while(1)
    74. {
    75. /**************************USB虚拟串口部分*************************/
    76. //检测usb热插拔状态
    77. if(usbstatus!=bDeviceState){//USB连接状态发生了改变.
    78. usbstatus=bDeviceState;//记录新的状态
    79. // if(usbstatus==CONFIGURED)printf("USB连接成功\r\n");//提示USB连接成功
    80. // else printf("USB连接断开\r\n");//提示USB断开
    81. }
    82. //终端接收usb虚拟串口数据
    83. if(USB_USART_RX_STA&0x8000){
    84. len=USB_USART_RX_STA&0x3FFF;//得到此次接收到的数据长度
    85. USB_USART_RX_STA=0;
    86. // printf("您发送的消息长度为:%d,内容为:%s\r\n",len,USB_USART_RX_BUF);//提示USB接收数据长度
    87. if(USB_USART_RX_BUF[0]==0x5a&&USB_USART_RX_BUF[1]==0xa5&&USB_USART_RX_BUF[2]==0xff&&USB_USART_RX_BUF[3]==0x50&&len==4){//判断接收数据
    88. //no deal with...
    89. }
    90. memset(USB_USART_RX_BUF,0,sizeof(USB_USART_RX_BUF));//缓存清零
    91. }
    92. if(usbstatus==CONFIGURED){//判断USB已经连接
    93. //定时500ms进入一次
    94. if(led_flag>100){//5ms×100=500ms
    95. led_flag=0;
    96. //闪烁led代表工作状态正常
    97. LED0=!LED0;
    98. }
    99. //读取和计算max30102数据,总体用缓存的500组数据分析,实际每读取100组新数据分析一次
    100. i=0;
    101. un_min=0x3FFFF;
    102. un_max=0;
    103. //将前100组样本转储到内存中(实际没有),并将后400组样本移到顶部,将100-500缓存数据移位到0-400
    104. for(i=100;i<500;i++){
    105. aun_red_buffer[i-100]=aun_red_buffer[i];//将100-500缓存数据移位到0-400
    106. aun_ir_buffer[i-100]=aun_ir_buffer[i];//将100-500缓存数据移位到0-400
    107. //更新信号的最小值和最大值
    108. if(un_min>aun_red_buffer[i])//寻找移位后0-400中的最小值
    109. un_min=aun_red_buffer[i];
    110. if(un_max//寻找移位后0-400中的最大值
    111. un_max=aun_red_buffer[i];
    112. }
    113. //在计算心率前取100组样本,取的数据放在400-500缓存数组中
    114. for(i=400;i<500;i++){
    115. un_prev_data=aun_red_buffer[i-1];//临时记录上一次读取数据
    116. while(MAX30102_INT==1);
    117. max30102_FIFO_ReadBytes(REG_FIFO_DATA,temp);//读取传感器数据,赋值到temp中
    118. aun_red_buffer[i] = (long)((long)((long)temp[0]&0x03)<<16) | (long)temp[1]<<8 | (long)temp[2]; //将值合并得到实际数字,数组400-500为新读取数据
    119. aun_ir_buffer[i] = (long)((long)((long)temp[3] & 0x03)<<16) |(long)temp[4]<<8 | (long)temp[5]; //将值合并得到实际数字,数组400-500为新读取数据
    120. if(aun_red_buffer[i]>un_prev_data){//用新获取的一个数值与上一个数值对比
    121. f_temp=aun_red_buffer[i]-un_prev_data;
    122. f_temp/=(un_max-un_min);
    123. f_temp*=MAX_BRIGHTNESS;//公式(心率曲线)=(新数值-旧数值)/(最大值-最小值)*255
    124. n_brightness-=(int)f_temp;
    125. if(n_brightness<0)
    126. n_brightness=0;
    127. }
    128. else{
    129. f_temp=un_prev_data-aun_red_buffer[i];
    130. f_temp/=(un_max-un_min);
    131. f_temp*=MAX_BRIGHTNESS;//公式(心率曲线)=(旧数值-新数值)/(最大值-最小值)*255
    132. n_brightness+=(int)f_temp;
    133. if(n_brightness>MAX_BRIGHTNESS)
    134. n_brightness=MAX_BRIGHTNESS;
    135. }
    136. //通过UART将样本和计算结果发送到终端程序
    137. //if(ch_hr_valid == 1 && ch_spo2_valid ==1 && n_heart_rate<200 && n_sp02<101){//使用上一次测量的结论作为检测正确性判断//ch_hr_valid == 1 && n_heart_rate<120
    138. // usb_printf("HR=%i, ", n_heart_rate);
    139. // usb_printf("HRvalid=%i, ", ch_hr_valid);
    140. // usb_printf("SpO2=%i, ", n_sp02);
    141. // usb_printf("SPO2Valid=%i\r\n", ch_spo2_valid);
    142. senddate[2]=CT1711_hex>>8;//温度搞8位
    143. senddate[3]=CT1711_hex;//温度低8位
    144. senddate[4]=n_brightness>>8;//心率曲线高8位
    145. senddate[5]=n_brightness;//心率曲线8位
    146. senddate[6]=n_heart_rate>>8;//心率高8位
    147. senddate[7]=n_heart_rate;//心率8位
    148. senddate[8]=ch_hr_valid;//心率监测状态
    149. senddate[9]=n_sp02>>8;//血氧搞8位
    150. senddate[10]=n_sp02;//血氧低8位
    151. senddate[11]=ch_spo2_valid;//血氧监测状态
    152. for(j=0;j<16;j++){//发送测试完毕标志,提示校验
    153. USB_USART_SendData(senddate[j]);//以字节方式,发送给USB
    154. }
    155. //}
    156. }
    157. maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);//传入500个心率和血氧数据计算传感器检测结论,反馈心率和血氧测试结果
    158. }
    159. else{//USB未连接
    160. //定时50ms进入一次
    161. if(led_flag>10){//5ms×10=50ms
    162. led_flag=0;
    163. LED0=!LED0;
    164. }
    165. }
    166. }
    167. }

            在程序中放了大量的注释,从函数定义,到初始化,到循环中,这里包含了USB初始化,大家有使用的保留即可,不使用的while中就看if(usbstatus==CONFIGURED)这个条件里的程序即可。

            主程序大概的流程就是每次采集100次数据,然后放在缓存数组的400-500地址位置,然后每次从500个全部缓存的数据中通过maxim_heart_rate_and_oxygen_saturation函数进行算法分析,计算出当前的心率、血氧饱和度,以及对应状态。

            在硬件中可以看见我把RD和IRD引脚也连接到了单片机上,正常情况下这两个引脚不配置,默认悬空也可以使用,当然也可以配置红光和红外更强一些,因为max30102芯片9和10引脚是红光和红外的电源正极,这两个对应就是电源负极,我将电源的正连接3.3,这里配置之后红光会直接被拉到0V,就会导致我在设置低电平时,红光特别刺眼,功耗也会大大增加

    //#define RD  PBout(3)    // 红光
    //#define IRD PBout(4)    // 红外

            在程序中也说明了对外的串口通讯协议,按照协议简单编写了一个上位机测试程序,只管的看一下曲线波形,在手指放上去之后他大概是这样的

             其中紫色的是心率曲线,红色的是心率数值,血氧计算也是比较平稳的,基本保持95-100左右

             但是放在手腕上,效果就完全不行, 虽然偶尔也能测出波形,但是心率和血氧就是乱了,就算使用算法磨平曲线二次分析计算,检测的波形也是无法保持的,只要稍微一动,测试曲线立马就没有了

             以上是在手腕上测试的几个数据,和在手指下测量完全不能对比

            软件也附在资源中了,以上硬件和软件就解决了,接下来还差什么呢,当然是结构设计了。

     3.结构设计

            这里本意是穿戴在手腕上,虽然比不上很多产品,又小、又精致、又实用、又准确,但是!我们这个自主可控,可以自己任意升级,自己想增加什么功能就增加什么功能,上位机或者手机APP想做什么样就做什么样,这个是买现成产品比不了的,毕竟,谁叫看这篇帖子到这的都是顶级动手小行家,都是未来的顶尖设计师工程师呢,我们的生命在于敢于折腾。

           结构上采用金属磁吸表链的方式,设计的外壳当然就要有手表链的孔啦,这里结构也是使用的ABS材料,在强度上也是有保障的。整体分为上壳可下盖两个部分,将电路板嵌入在上壳中,在下盖拧螺丝固定。

            上图即为上壳正面与反面,有4个3.5mm孔,嵌入铜花母

            上图即为下盖,中间位置为裸漏出传感器与手指连接部分

            整体结构设计相对简单,毕竟是准备使用3D打印的外壳,精度和材料没法像手机及只能手表那么复杂和高精尖。

     

                     最后出的结构有粉色和黑色两种,我还是比较喜欢粉色的一些,嘁哩喀喳开始组装

     

     

     

            最后在将USB线缆连接到电脑,打开qt编写的上位机软件。

    4.上位机设计

             上位机程序就相对简单很多了,把算法部分都放在了下位机软件中去处理,上位机制作串口数据解析以及 曲线刷新显示,

             头文件如下,可以看出内容很少,程序中还是使用了3个曲线独立,实际一个就够了,代码还能缩减1/3

    mainwindow.h

    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MainWindow(QWidget *parent = 0);
    6. ~MainWindow();
    7. private slots:
    8. void timeisout();
    9. void open_serialport(bool reset,QString comstr);
    10. void serialRead();//串口回调函数
    11. void comBatAnalyze(QByteArray &allData);//处理串口得到的数据
    12. //控制菜单槽函数
    13. void start_clicked();//开始采集
    14. void fw_data();//复位数据
    15. void setcan();
    16. void theback();//退出
    17. //曲线
    18. void QCustomPlot_Init(QCustomPlot *CustomPlot, char mode);
    19. void QCustomPlot_Updata1(QCustomPlot *CustomPlot);
    20. void QCustomPlot_Updata2(QCustomPlot *CustomPlot);
    21. void QCustomPlot_Updata3(QCustomPlot *CustomPlot);
    22. void updatedata(uint mode);
    23. double maxValue(QVector<double> list);//最大值
    24. double minValue(QVector<double> list);//最小值
    25. double averageValue(QVector<double> list);//平均值
    26. private:
    27. Ui::MainWindow *ui;
    28. QTimer *timeres;
    29. QSerialPort *serial;//串口句柄
    30. ulong m_allnum;//记录数据长度
    31. uint x_num1,x_num2,x_num3;//接收数据次数
    32. // QVector AIxx;
    33. QVector<double> y_history[4];//全部数据
    34. QVector<double> x_vec_p1,x_p1,x_vec_p1_2;
    35. QVector<double> x_vec_p2,x_p2;
    36. QVector<double> x_vec_p3,x_p3;
    37. double m_cursor_1[9];//曲线数值
    38. };

    主程序mainwindow.cpp

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. QString COMS;
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. // this->setWindowFlags(Qt::FramelessWindowHint);
    10. this->setWindowModality(Qt::ApplicationModal);
    11. //读取数据库历史串口配置
    12. COMS="COM13";
    13. open_serialport(0,COMS);//初始化串口
    14. ///初始化定时器
    15. QTimer *timebaty=new QTimer;
    16. timebaty->start(1000);//1s更新频率
    17. connect(timebaty,SIGNAL(timeout()),this,SLOT(timeisout()));//定时器1
    18. ///菜单栏绑定槽
    19. connect(ui->action_L,SIGNAL(triggered()),this,SLOT(start_clicked()));//开始采集
    20. connect(ui->action_R,SIGNAL(triggered()),this,SLOT(fw_data()));//复位数据
    21. connect(ui->action_set,SIGNAL(triggered()),this,SLOT(setcan()));
    22. connect(ui->action_back,SIGNAL(triggered()),this,SLOT(theback()));//退出程序
    23. connect(ui->pushButton,QPushButton::clicked,[=](){setcan();});
    24. ///初始化曲线
    25. // AIxx.clear();//清除x数据
    26. y_history[0].clear();//清除y数据
    27. y_history[1].clear();//清除y数据
    28. y_history[2].clear();//清除y数据
    29. y_history[3].clear();//清除y数据
    30. QCustomPlot_Init(ui->customplot1,1);
    31. QCustomPlot_Init(ui->customplot2,2);
    32. QCustomPlot_Init(ui->customplot3,3);
    33. ///复位数据
    34. fw_data();
    35. }
    36. MainWindow::~MainWindow()
    37. {
    38. delete ui;
    39. }
    40. void MainWindow::timeisout()
    41. {
    42. //刷新系统时间和系统运行时间
    43. ui->label_runtime->setText(QDateTime::currentDateTime().toString(QString::fromUtf8("yyyy年MM月dd日 HH:mm:ss")));
    44. //计算通讯速率
    45. static uint32_t number=0;
    46. if(m_allnum
    47. else number=m_allnum-number;
    48. ui->label_speed->setText(QString::number(number/1024)+"KByte/s RX:"+QString::number(m_allnum));
    49. number=m_allnum;
    50. }
    51. ///串口配置start/
    52. //初始化/复位串口
    53. void MainWindow::open_serialport(bool reset,QString comstr)
    54. {
    55. if(reset){
    56. serial->clear();//先关闭之前的串口
    57. serial->close();
    58. }
    59. serial = new QSerialPort(comstr);
    60. serial->open(QIODevice::ReadWrite);//读写打开
    61. serial->setBaudRate(921600); //波特率QSerialPort::Baud9600
    62. serial->setDataBits(QSerialPort::Data8); //数据位
    63. serial->setParity(QSerialPort::NoParity); //无奇偶校验
    64. serial->setStopBits(QSerialPort::OneStop); //无停止位
    65. serial->setFlowControl(QSerialPort::NoFlowControl); //无控制
    66. connect(serial,SIGNAL(readyRead()),this,SLOT(serialRead()));//连接串口读取函数
    67. }
    68. //串口回调函数
    69. void MainWindow::serialRead()
    70. {
    71. static QByteArray allData;
    72. while (!serial->atEnd()){
    73. allData += serial->readAll().toHex();
    74. comBatAnalyze(allData);//处理串口得到的数据
    75. }
    76. }
    77. //处理串口得到的数据5AA5 0000 0000 00 0000 00 53544F50
    78. void MainWindow::comBatAnalyze(QByteArray &allData)
    79. {
    80. QByteArray dataTemp;
    81. bool ok;
    82. int len=0;
    83. int start;
    84. int move;
    85. dataTemp=allData.toUpper();
    86. while((len=dataTemp.count())>=16*2){//最少长度为13+数据0
    87. move=2;
    88. start=dataTemp.indexOf("5AA5",0);//搜索字符,返回搜索到的起始位置
    89. if(start>=0){
    90. len=len-start;//将5AA5做为开头,长度随之变化
    91. dataTemp=dataTemp.right(len);//从右侧裁剪len长度数据保存
    92. if(len>=16*2){
    93. if(dataTemp.mid(0,4)=="5AA5"&&dataTemp.mid(24,8)=="53544F50"){
    94. // QByteArray aaa=QByteArray::fromHex(dataTemp);
    95. // uint16_t temp=((aaa[2]<<24)&0xFF000000)+((aaa[3]<<16)&0x00FF0000)+((aaa[4]<<8)&0x0000FF00)+(aaa[5]&0x000000FF);
    96. uint16_t temp=(unsigned int)dataTemp.mid(2*2,4).toInt(&ok,16);
    97. uint16_t HRplot=(unsigned int)dataTemp.mid(4*2,4).toInt(&ok,16);
    98. uint16_t HR=(unsigned int)dataTemp.mid(6*2,4).toInt(&ok,16);
    99. uint8_t HRvalid=(unsigned char)dataTemp.mid(8*2,2).toInt(&ok,16);
    100. uint16_t SpO2=(unsigned int)dataTemp.mid(9*2,4).toInt(&ok,16);
    101. uint8_t SPO2Valid=(unsigned char)dataTemp.mid(11*2,2).toInt(&ok,16);
    102. qDebug()<<(double)temp/1000<
    103. //if(HRvalid==1&&SPO2Valid==1){
    104. y_history[0].append(HRplot);
    105. y_history[1].append(HR);
    106. y_history[2].append(SpO2);
    107. y_history[3].append((double)temp/1000);
    108. //更新曲线
    109. QCustomPlot_Updata1(ui->customplot1);
    110. QCustomPlot_Updata2(ui->customplot2);
    111. QCustomPlot_Updata3(ui->customplot3);
    112. //更新数值
    113. updatedata(1);
    114. updatedata(2);
    115. updatedata(3);
    116. //}
    117. m_allnum+=len;
    118. move=16*2;//剪掉当前使用完毕的数据
    119. }
    120. dataTemp=dataTemp.right(len-move);//剪掉当前使用完毕的数据
    121. }
    122. else{
    123. break;
    124. }
    125. }
    126. else{
    127. dataTemp.clear();//若没有需求的数据,则清除整个数据内容,重新接收判断
    128. break;
    129. }
    130. }
    131. allData=dataTemp;
    132. }
    133. ///串口配置end/
    134. ///菜单栏回调函数/
    135. //开始采集y_history[0],y_history[1],y_history[2]
    136. void MainWindow::start_clicked()
    137. {
    138. open_serialport(1,COMS);//初始化串口
    139. qDebug()<<"11";
    140. }
    141. //复位数据
    142. void MainWindow::fw_data()
    143. {
    144. //开始采集前清空数据
    145. m_allnum=0;//清空长度
    146. y_history[0].clear();//清除y数据
    147. y_history[1].clear();//清除y数据
    148. y_history[2].clear();//清除y数据
    149. y_history[3].clear();//清除y数据
    150. x_num1=0;//接收刷新曲线数据次数
    151. x_num2=0;//接收刷新曲线数据次数
    152. x_num3=0;//接收刷新曲线数据次数
    153. //清空曲线显示
    154. x_vec_p1.clear();
    155. x_p1.clear();
    156. x_vec_p2.clear();
    157. x_p2.clear();
    158. x_vec_p3.clear();
    159. x_p3.clear();
    160. QCustomPlot_Updata1(ui->customplot1);
    161. QCustomPlot_Updata2(ui->customplot2);
    162. QCustomPlot_Updata3(ui->customplot3);
    163. updatedata(0);
    164. }
    165. //设置界面
    166. void MainWindow::setcan()
    167. {
    168. }
    169. //退出程序
    170. void MainWindow::theback()
    171. {
    172. this->close();
    173. }
    174. ///菜单栏回调函数end/
    175. 曲线配置
    176. void MainWindow::QCustomPlot_Init(QCustomPlot *CustomPlot,char mode)
    177. {
    178. //添加曲线
    179. CustomPlot->addGraph(CustomPlot->xAxis,CustomPlot->yAxis);//以左侧y轴为准
    180. if(mode==1)CustomPlot->addGraph(CustomPlot->xAxis,CustomPlot->yAxis2);//以右侧y轴为准
    181. QPen graphPen0;
    182. graphPen0.setColor(QColor(150,40,120));//设置曲线颜色
    183. QPen graphPen1;
    184. graphPen1.setColor(QColor(220,40,0));//设置曲线颜色
    185. // graphPen0.setWidthF(4);//曲线粗度
    186. if(mode==1)CustomPlot->yAxis2->setVisible(true);//显示y轴2
    187. CustomPlot->graph(0)->setPen(graphPen0);//使用画笔绘制曲线
    188. if(mode==1)CustomPlot->graph(1)->setPen(graphPen1);//使用画笔绘制曲线
    189. CustomPlot->graph(0)->setName(QString(""));
    190. if(mode==1)CustomPlot->graph(1)->setName(QString(""));
    191. //设置横坐标显示倍数
    192. QSharedPointer pITicker(new QCPAxisTickerPi());//QCPAxisTickerPi设置Pi刻度标签
    193. CustomPlot->xAxis->setTicker(pITicker);
    194. pITicker->setPiSymbol("");//设置数值后面π替换为*
    195. pITicker->setFractionStyle(QCPAxisTickerPi::fsFloatingPoint);//设置小数部分显示float型
    196. pITicker->setPiValue(1);//设置*表示的值 原代表π=3.14,先换为m_multiple//50ms传输一包,这里1秒就是20包,1/20=0.05
    197. //设置坐标标题
    198. // QString groupname[3]={"心率","血氧","体温"};
    199. // QString plotname=QString(groupname[mode-1]);
    200. // CustomPlot->xAxis->setLabel(QString::fromUtf8("时间(s)"));
    201. // CustomPlot->yAxis->setLabel(QString::fromUtf8("plotname"));
    202. //设置坐标轴范围
    203. CustomPlot->xAxis->setRange(0,1000);//设置x轴范围
    204. CustomPlot->yAxis->setRange(-5,5);//设置y轴范围
    205. if(mode==1)CustomPlot->yAxis2->setRange(0,200);//设置y轴范围
    206. //设置网格虚线
    207. // CustomPlot->xAxis->grid()->setVisible(false);
    208. // CustomPlot->yAxis->grid()->setVisible(false);
    209. //设置风格颜色
    210. QLinearGradient plotGradient;
    211. plotGradient.setStart(0, 0);
    212. plotGradient.setFinalStop(0, 350);
    213. plotGradient.setColorAt(0, QColor(220, 220, 220));//80, 80, 80
    214. plotGradient.setColorAt(1, QColor(120, 120, 120));//50, 50, 50
    215. CustomPlot->setBackground(plotGradient); // 设置背景颜色
    216. QLinearGradient axisRectGradient;
    217. axisRectGradient.setStart(0, 0);
    218. axisRectGradient.setFinalStop(0, 350);
    219. axisRectGradient.setColorAt(0, QColor(220, 220, 220));//80, 80, 80
    220. axisRectGradient.setColorAt(1, QColor(120, 120, 120));//30, 30, 30
    221. CustomPlot->axisRect()->setBackground(axisRectGradient); // 设置QCPAxisRect背景颜色
    222. CustomPlot->xAxis->setBasePen(QPen(Qt::black,2));//设置x轴坐标轴颜色
    223. CustomPlot->yAxis->setBasePen(QPen(Qt::black,2));//设置y轴坐标轴颜色//black
    224. if(mode==1)CustomPlot->yAxis2->setBasePen(QPen(Qt::black,2));//设置y轴坐标轴颜色//black
    225. CustomPlot->xAxis->setTickPen(QPen(Qt::black, 2)); // 轴刻度线的画笔
    226. CustomPlot->yAxis->setTickPen(QPen(Qt::black,2));//black
    227. if(mode==1)CustomPlot->yAxis2->setTickPen(QPen(Qt::black,2));//black
    228. CustomPlot->xAxis->setSubTickPen(QPen(Qt::black, 2)); // 轴子刻度线的画笔
    229. CustomPlot->yAxis->setSubTickPen(QPen(Qt::black,2));//black
    230. if(mode==1)CustomPlot->yAxis2->setSubTickPen(QPen(Qt::black,2));//black
    231. CustomPlot->xAxis->setTickLabelColor(Qt::black);//设置x轴坐标颜色
    232. CustomPlot->yAxis->setTickLabelColor(Qt::black);//设置y轴坐标颜色
    233. if(mode==1)CustomPlot->yAxis2->setTickLabelColor(Qt::black);//设置y轴坐标颜色
    234. CustomPlot->xAxis->setLabelColor(Qt::black);//设置x轴名称颜色
    235. CustomPlot->yAxis->setLabelColor(Qt::black);//设置y轴名称颜色
    236. if(mode==1)CustomPlot->yAxis2->setLabelColor(Qt::black);//设置y轴名称颜色
    237. //设置曲线名称显示
    238. CustomPlot->legend->setBrush(QColor(0, 0, 0, 0));//设置图例提示背景色(曲线名称背景色)
    239. CustomPlot->legend->setVisible(false);//设置曲线名称不可见
    240. CustomPlot->legend->setTextColor(Qt::black);//black
    241. CustomPlot->replot();//重绘制
    242. }
    243. //刷新曲线1
    244. void MainWindow::QCustomPlot_Updata1(QCustomPlot *CustomPlot)
    245. {
    246. // static QVector x_vec_p,x_p; //存放数据的容器 在h文件中声明,这里声明再次进入界面崩溃,不知道为什么
    247. uint x_number=x_num1;
    248. double y_value1=y_history[0].last();
    249. double y_value2=y_history[1].last();
    250. if(x_number<1000){//这时容器里面还没1000个点 所有一直向里面存
    251. x_vec_p1.append(y_value1);//新值数
    252. x_vec_p1_2.append(y_value2);//新值数
    253. x_p1.append(x_number);
    254. CustomPlot->xAxis->setRange(0,1000);//设置范围正好 能显示当前点
    255. }
    256. else{//容器数据现在是正好1000个 把第一个出栈 把第1001个入栈 正好还是1000个数据
    257. x_vec_p1.removeFirst();
    258. x_vec_p1_2.removeFirst();
    259. x_p1.removeFirst();
    260. //入栈
    261. x_vec_p1.append(y_value1);//新值数
    262. x_vec_p1_2.append(y_value2);//新值数
    263. x_p1.append(x_number);
    264. CustomPlot->xAxis->setRange(x_p1.at(0),x_p1.at(x_p1.size()-1));
    265. }
    266. //设置坐标轴范围
    267. CustomPlot->yAxis->setRange(0,255);//设置y轴范围
    268. CustomPlot->yAxis2->setRange(0,200);//设置y轴范围
    269. CustomPlot->graph(0)->setData(x_p1,x_vec_p1);//设置数据
    270. CustomPlot->graph(1)->setData(x_p1,x_vec_p1_2);//设置数据
    271. // CustomPlot->yAxis->rescale(true);//设置Y轴坐标系 自动缩放以正常显示所有的数据
    272. // CustomPlot->graph(0)->rescaleAxes(true);//根据图像最高点最低点自动缩放坐标轴
    273. // CustomPlot->yAxis2->rescale(true);//设置Y轴坐标系 自动缩放以正常显示所有的数据
    274. // CustomPlot->graph(1)->rescaleAxes(true);//根据图像最高点最低点自动缩放坐标轴
    275. CustomPlot->replot();//重绘制
    276. x_num1++;
    277. }
    278. //刷新曲线2
    279. void MainWindow::QCustomPlot_Updata2(QCustomPlot *CustomPlot)
    280. {
    281. // static QVector x_vec_p,x_p; //存放数据的容器 在h文件中声明,这里声明再次进入界面崩溃,不知道为什么
    282. uint x_number=x_num2;
    283. double y_value=y_history[2].last();
    284. if(x_number<1000){//这时容器里面还没1000个点 所有一直向里面存
    285. x_vec_p2.append(y_value);//新值数
    286. x_p2.append(x_number);
    287. CustomPlot->xAxis->setRange(0,1000);//设置范围正好 能显示当前点
    288. }
    289. else{//容器数据现在是正好1000个 把第一个出栈 把第1001个入栈 正好还是1000个数据
    290. x_vec_p2.removeFirst();
    291. x_p2.removeFirst();
    292. //入栈
    293. x_vec_p2.append(y_value);//新值数
    294. x_p2.append(x_number);
    295. CustomPlot->xAxis->setRange(x_p2.at(0),x_p2.at(x_p2.size()-1));
    296. }
    297. //设置坐标轴范围
    298. CustomPlot->yAxis->setRange(0,150);//设置y轴范围
    299. CustomPlot->graph(0)->setData(x_p2,x_vec_p2);//设置数据
    300. // CustomPlot->yAxis->rescale(true);//设置Y轴坐标系 自动缩放以正常显示所有的数据
    301. // CustomPlot->graph(0)->rescaleAxes(true);//根据图像最高点最低点自动缩放坐标轴
    302. CustomPlot->replot();//重绘制
    303. x_num2++;
    304. }
    305. //刷新曲线3
    306. void MainWindow::QCustomPlot_Updata3(QCustomPlot *CustomPlot)
    307. {
    308. // static QVector x_vec_p,x_p; //存放数据的容器 在h文件中声明,这里声明再次进入界面崩溃,不知道为什么
    309. uint x_number=x_num3;
    310. double y_value=y_history[3].last();
    311. if(x_number<1000){//这时容器里面还没1000个点 所有一直向里面存
    312. x_vec_p3.append(y_value);//新值数
    313. x_p3.append(x_number);
    314. CustomPlot->xAxis->setRange(0,1000);//设置范围正好 能显示当前点
    315. }
    316. else{//容器数据现在是正好1000个 把第一个出栈 把第1001个入栈 正好还是1000个数据
    317. x_vec_p3.removeFirst();
    318. x_p3.removeFirst();
    319. //入栈
    320. x_vec_p3.append(y_value);//新值数
    321. x_p3.append(x_number);
    322. CustomPlot->xAxis->setRange(x_p3.at(0),x_p3.at(x_p3.size()-1));
    323. }
    324. //设置坐标轴范围
    325. CustomPlot->yAxis->setRange(minValue(x_vec_p3)-2,maxValue(x_vec_p3)+2);//设置y轴范围,最低温度-2,最高温度+2
    326. CustomPlot->graph(0)->setData(x_p3,x_vec_p3);//设置数据
    327. // CustomPlot->yAxis->rescale(true);//设置Y轴坐标系 自动缩放以正常显示所有的数据
    328. // CustomPlot->graph(0)->rescaleAxes(true);//根据图像最高点最低点自动缩放坐标轴
    329. CustomPlot->replot();//重绘制
    330. x_num3++;
    331. }
    332. //初始化曲线数据1.2.3
    333. void MainWindow::updatedata(uint mode)
    334. {
    335. //计算曲线需求值
    336. QVector<double> shortdata1;
    337. shortdata1.clear();
    338. for(int i=0;i<(y_history[1].size());i++)
    339. shortdata1.append(y_history[1].at(i));
    340. QVector<double> shortdata2;
    341. shortdata2.clear();
    342. for(int i=0;i<(y_history[2].size());i++)
    343. shortdata2.append(y_history[2].at(i));
    344. QVector<double> shortdata3;
    345. shortdata3.clear();
    346. for(int i=0;i<(y_history[3].size());i++)
    347. shortdata3.append(y_history[3].at(i));
    348. m_cursor_1[0]=minValue(shortdata1);
    349. m_cursor_1[1]=maxValue(shortdata1);
    350. m_cursor_1[2]=averageValue(shortdata1);
    351. m_cursor_1[3]=minValue(shortdata2);
    352. m_cursor_1[4]=maxValue(shortdata2);
    353. m_cursor_1[5]=averageValue(shortdata2);
    354. m_cursor_1[6]=minValue(shortdata3);
    355. m_cursor_1[7]=maxValue(shortdata3);
    356. m_cursor_1[8]=averageValue(shortdata3);
    357. QString groupunit[9]={"次/分","次/分","次/分","%","%","%","℃","℃","℃"};
    358. QLabel *group1[9]={ui->label_show1,ui->label_show2,ui->label_show3,ui->label_show4,ui->label_show5,ui->label_show6,ui->label_show7,ui->label_show8,ui->label_show9};
    359. for(int i=0;i<9;i++)
    360. group1[i]->setText(QString::number(m_cursor_1[i])+groupunit[i]);
    361. ui->label_shuoming1->setText(QString::number(y_history[1].last()));
    362. ui->label_shuoming2->setText(QString::number(y_history[2].last()));
    363. ui->label_shuoming3->setText(QString::number(y_history[3].last()));
    364. }
    365. //求最大值
    366. double MainWindow::maxValue(QVector<double> list)
    367. {
    368. if(list.count() == 0) return 0;
    369. double max=list.at(0);
    370. foreach(double each,list){
    371. if(max>each){
    372. max = max;
    373. }
    374. else{
    375. max = each;
    376. }
    377. }
    378. return max;
    379. }
    380. //求最小值
    381. double MainWindow::minValue(QVector<double> list)
    382. {
    383. if(list.count() == 0) return 0;
    384. double min = list.at(0);
    385. foreach(double each,list){
    386. if(min
    387. min = min;
    388. }
    389. else{
    390. min = each;
    391. }
    392. }
    393. return min;
    394. }
    395. //求平均值
    396. double MainWindow::averageValue(QVector<double> list)
    397. {
    398. if(list.count() == 0) return 0;
    399. uint count=list.count();
    400. double num=0;
    401. for(int i=0;i
    402. num+=list.at(i);
    403. }
    404. return num/count;
    405. }

           实物演示视频https://live.csdn.net/v/255669

           所有资源下载链接https://download.csdn.net/download/qq_37603131/87090708

    四、结语

    本文中涉及注意事项:

    1.焊接传感器时不要把传感器表面搞坏,建议试用焊台,先焊接传感器,在焊接其他元器件

    2.硬件和下位机程序与上位机程序仅供参考,不需要的地方自行删除改动

    3.此传感器不适用于医疗,仅适用于监测和论文

    4.如有升级需求请看资源说明

  • 相关阅读:
    面试题库(一):数据结构和算法
    基于javaweb的在线图书商城系统(java+ssm+jsp+jquery+mysql)
    MySQL数据误删恢复操作
    springboot项目的配置和基本案例练习
    java计算机毕业设计基于ssm的基于android的家庭理财系统
    课设--学生成绩管理系统(一)
    后端程序员实现一个IP归属地的小程序
    RabbitMQ项目实战(一)
    亚马逊流量攻略:如何将流量转化为销售,测评实现销售飙升!
    vue手写提示组件弹窗
  • 原文地址:https://blog.csdn.net/qq_37603131/article/details/127943666