Dejan Volk / Mbed 2 deprecated HeartBeatMonitor

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Heart Beat Monitor v1.0 w/ BlueBeat(Java-based program for mobile phones) & BlueBeat for PC
00003 Authors: Dejan Volk & Blaz Magdic
00004 3rd grade of Computer Science, Faculty of Electrical Engineering and Computer Science
00005 Maribor, Slovenia
00006 
00007 Used for sending information about heart beats per minute to the mobile phone via
00008 Bluetooth module WT11-A-AI made by Bluegiga.
00009 
00010 Description of pins used:
00011 
00012 p5 - RESET switch for Polar's RMCM01 OEM module
00013 p8 - RESET switch on WT11
00014 p9 - TX for communicating with WT11
00015 p10 - RX for communicating with WT11
00016 p11 - DigitalIn for Polar's RMCM01 OEM module (pin HR)
00017 p12 - DigitalIn for Polar's RMCM01 OEM module (pin FPLS)
00018 
00019 Possible improvements:
00020 - Integrating a LCD monitor that will use the serial port to display information about the current heart beat
00021 - Implementing a more efficient algorithm to calculate the heart beat
00022 - Better code comments (doh)
00023 
00024 */
00025 
00026 #include "mbed.h"
00027 
00028 //************global declarations*****************
00029 Serial s_port(p9,p10);
00030 DigitalOut led_yes(LED1); //visual indicator for sending data to WT11
00031 DigitalOut reset(p8);  //reset trigger on WT11
00032 DigitalOut reset_RMCM(p5);    //reset trigger on RMCM01
00033 InterruptIn heartBeat(p12);
00034 Timer timer;
00035 int interval;    //interval between heartbeats
00036 int intrHB;      //heartbeats per minute
00037 int baud_rate = 115200;
00038 int bits = 8;
00039 int stop_bits = 1;
00040 int counter_HB = 0;
00041 int array[3];        //small array for storing heart beats
00042 bool intrPin = false;
00043 int previous = 11;    //variable that stores the previously calculated heartbeat
00044 //***********end of global declarations*************
00045 
00046 void initWT11() {       //init function for initial configuration of WT11 module
00047     int zero = 0;
00048     int high = 1;
00049     reset.write(zero); //Initializing WT11 for inputting commands
00050     wait(0.2);
00051     reset.write(high);
00052     wait(0.01);
00053     reset.write(zero);
00054     wait(2);
00055     //Configuration for WT11 - setting it to slave mode
00056     s_port.printf("SET BT PAGEMODE 3 2000 1\n");
00057     s_port.printf("SET BT NAME HBMWT11\n");
00058     s_port.printf("SET BT ROLE 0 f 7d00\n");
00059     s_port.printf("SET CONTROL ECHO 0\n");
00060     s_port.printf("SET BT AUTH * 1234\n");
00061     s_port.printf("SET CONTROL ESCAPE 43 00 0\n");
00062     s_port.printf("SET CONTROL CD 00 0\n");
00063     s_port.printf("SET CONTROL BAUD 115200,8n1\n");
00064 }
00065 
00066 void initRMCM() { //init function for initial configuration of RMCM01
00067     int zero = 0;
00068     int high = 1;
00069     reset_RMCM.write(high);
00070     wait(0.1);
00071     reset_RMCM.write(zero);
00072     wait(0.1);
00073     reset_RMCM.write(high);
00074 }
00075 
00076 void intrTrigRise() {
00077     led_yes=1;    //triggering the indicator led
00078     interval=timer.read_ms();    //reads the interval
00079     timer.stop();
00080     timer.reset();          //resets the timer
00081     timer.start();          //starts the timer
00082     intrPin = true;        //sets the interrupt to true
00083     heartBeat.mode(PullNone);
00084     wait(0.29);
00085     heartBeat.mode(PullUp);
00086 }
00087 
00088 void interpolation() {
00089     if (interval != 0) {  //avoiding divide-by-zero error
00090         array[counter_HB] = interval;
00091         counter_HB++;
00092         led_yes=0;
00093 
00094         if (counter_HB == 3) {     //when the array is full, start calculating the average heartbeat
00095             counter_HB = 0;
00096             int minute = 60000;
00097             int avrg = 0;
00098             int temp = 0;
00099             int flag = 1;
00100             int avg = 0;
00101 
00102             for (int i = 1; i <= 3 && flag; i++) {    //bubble sort in descending order
00103                 flag = 0;
00104 
00105                 for (int j = 0; j < 2; j++) {
00106                     if (array[j+1] > array[j]) {
00107                         temp = array[j];
00108                         array[j] = array[j+1];
00109                         array[j+1] = temp;
00110                         flag = 1;
00111                     }
00112                 }
00113             }
00114 
00115             int sum1 = (60000/array[0]) - (60000/array[1]);        //taking the middle value and calculate the difference
00116             int sum2 = (60000/array[2]) - (60000/array[1]);        //between both remaining values
00117 
00118             if ((sum1 >= -10 || sum1 <= 10) || (sum2 >= -10 || sum2 <= 10)) {        //if all values do not deviate by +/- 10 heart beats
00119                 for (int i = 0; i < 3; i++) {                                        //then take all 3 and calculate the average
00120                     avrg+=array[i];
00121                 }
00122                 avg = avrg/3;
00123             } else if ((sum1 >= -10 || sum1 <= 10) || (sum2 > 10 || sum2 < -10)) { //if 2nd sum deviates for more than +/- 10 heartbeats
00124                 avrg = array[0] + array[1];                                        //take the elements of 1st sum
00125                 avg = avrg/2;
00126             } else if ((sum1 < -10 || sum1 > 10) || (sum2 <= 10 || sum2 >= -10)) { //if 1st sum deviates for more than +/- 10 heartbeats
00127                 avrg = array[2] + array[1];                                        //take the elements of 2nd sum
00128                 avg = avrg/2;
00129             } else {
00130                 avg = previous;        //if all 3 values deviate for more than -/+ 10 heart beats, discard all and show the previous 
00131             }                          //"good" heartbeat
00132 
00133 
00134             intrHB = minute/avg;    //interpolating the interval to 1 min
00135             intrPin = false;            //setting the interrupt to false
00136             s_port.printf("#%i*",intrHB);
00137             previous = avg;
00138         }
00139         intrPin = false;
00140     } else
00141         intrPin=false;
00142 }
00143 
00144 int main() {
00145     initRMCM();                            //calling init function for RMCM01
00146     s_port.baud(baud_rate);
00147     s_port.format(bits,Serial::None,stop_bits);
00148     initWT11();                         //calling the init function for setting WT11
00149     s_port.printf("SELECT 0\n");        //switching from COMMAND mode to DATA mode
00150 
00151     while (1) {
00152         heartBeat.rise(&intrTrigRise);    //trigger for interrupt
00153         if (intrPin == true && interval != 0) {  //if interrupt is true and interval is not 0, send data
00154             interpolation();
00155         }
00156     }
00157 }