トランジスタ技術2014年10月号第6章のソフトウェア

Dependencies:   USBDevice mbed

Information

tg_201410s6_Plethysmographs トランジスタ技術 2014年 10月号 第6章のソフトウェア

Program for Section 6 in October. 2014 issue of the Transistor Gijutsu
(Japanese electronics magazine)

概要

このプログラムは、

  • 脈波データ取得(A-Dサンプリング、ハイパスフィルタ)
  • 脈拍数の算出(パルス検出、LEDおよび同期音出力、移動平均処理)

を行うPulseRateクラスと、それらをUSBシリアル通信でホストへ送信するmain関数で構成されています。

PulseRate.h, PulseRate.cpp

  • A-Dサンプリング - 100 SPS
  • ハイパスフィルタ - 遮断周波数0.1Hz、1次バターワースフィルタ
  • パルス検出 - 脈波を微分処理、5point移動平均して速度脈波を求め、
    それを包絡線検波した波形を、速度脈波が再び超える点をパルス開始とする
  • LED、同期音出力 - パルス同期LED(10ms)、圧電サウンダ出力(1kHz、10ms)
  • 移動平均処理 - 直近5拍分の平均値を脈波として算出

main.cpp

  • PulseRateクラスのインスタンスを生成
  • 処理開始メソッドを実行
  • メインループ - ポーリングにより、脈波データ、脈拍数データの完了フラグが返されたら、
    USBシリアル通信経由で、ホストへ送信する

シリアル通信フォーマット

  • 4byte固定長パケット方式
  • 脈波データパケット、脈拍数データパケットの2種類
脈波データパケット脈拍数データパケット
0x00パケットヘッダ(固定値0xAA)パケットヘッダ(固定値0xAA)
0x01波形番号(0 - 99繰り返し)脈拍数ID(固定値0xBB)
0x02, 0x03脈波データ(singed, 2byte)脈拍数データ(20 - 300, 2byte)

Description

This contains PulseRate class and main function.

PulseRate class:

  • Acquiring pulse waveform (A-D sampling, high pass filter)
  • Calculate pulse rate (Detecting pulse, Sync. LED and buzzer, moving averaging)

Main function:

  • Send pulse waveform and rate to host via USB serial class.

PulseRate.h, PulseRate.cpp

  • A-D sampling - 100 SPS
  • High pass filter - Cut off frequency 0.1Hz, first order butterworth
  • Detecting pulse - Calculating velocity pulse waveform by derivation and moving averaging (5point).
    Moreover, calculating threshold waveform like envelope demodulator.
    Detecting point the velocity waveform pass over the threshold waveform as starting pulse.
  • Sync. LED, buzzer - Synchronous pulse LED(10ms), piezo sounder(1kHz, 10ms)
  • Moving averaging - Calculating pulse rate averaging the previous 5 pulse.

main.cpp

  • Generating an instance of PulseRate class
  • Executing start procedure method
  • Main loop - sending pulse waveform data and pulse rate data via USB serial interface when detecting ready in return value.

Packet format for USB serial interface

  • Packet size: 4 bytes(fixed)
  • Two types of packets, pulse waveform packet and pulse rate packet
Pulse waveform packetPulse rate packet
0x00Packet header (0xAA (fixed)))Packet header (0xAA (fixed))
0x01Sampling number (0 - 99)Pulse rate ID (0xBB (fixed))
0x02, 0x03Pulse waveform data (singed, 2byte)Pulse rate data (20 - 300, 2byte)
Revision:
0:f0c12790aadb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 11 15:02:45 2014 +0000
@@ -0,0 +1,56 @@
+/**
+ *  @file       Main.cpp
+ *  @brief      Send pulse waveform and pulse rate 
+ *  @date       2014.08.08
+ *  @version    1.0.0
+ */
+#include "mbed.h"
+#include "USBSerial.h"
+#include "PulseRate.h"
+
+#define PACKET_HEADER (0xAA)
+#define PULSE_RATE_ID (0xBB)
+#define BYTE_MASK (0xFF)
+
+USBSerial serial;
+PulseRate pr(p20, LED1, p26); /* AD, LED, Beep */
+
+/** Send data packet
+ *  @param      second_val  Byte data at packet address 0x01
+ *  @param      data_val    Short data at packet address 0x02, 0x03
+ */
+bool send_packet(int second_val, int data_val)
+{
+    if(serial.writeable()) {
+        serial.putc(PACKET_HEADER);
+        serial.putc(second_val & BYTE_MASK);
+        serial.putc((data_val >> 8 ) & BYTE_MASK);
+        serial.putc(data_val & BYTE_MASK);
+        return true;
+    }
+    else
+    {
+        return false;
+    }
+}
+
+/** Main function
+ */
+int main() {
+    uint32_t num;
+    int32_t wave;
+    uint32_t rate;
+    
+    pr.start_sampling();    /* start procedure */
+
+    while(1) {
+        /* Pulse waveform */
+        if(pr.get_wave(num, wave)) {
+            send_packet(num, wave);
+        }
+        /* Pulse rate */
+        if(pr.get_pr_val(rate)) {
+            send_packet(PULSE_RATE_ID, rate);
+        }
+    }
+}