Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:a0204dfc42fd, 2010-09-09 (annotated)
- Committer:
- dejan_volk
- Date:
- Thu Sep 09 15:43:12 2010 +0000
- Revision:
- 0:a0204dfc42fd
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| dejan_volk | 0:a0204dfc42fd | 1 | /* |
| dejan_volk | 0:a0204dfc42fd | 2 | Heart Beat Monitor v1.0 w/ BlueBeat(Java-based program for mobile phones) & BlueBeat for PC |
| dejan_volk | 0:a0204dfc42fd | 3 | Authors: Dejan Volk & Blaz Magdic |
| dejan_volk | 0:a0204dfc42fd | 4 | 3rd grade of Computer Science, Faculty of Electrical Engineering and Computer Science |
| dejan_volk | 0:a0204dfc42fd | 5 | Maribor, Slovenia |
| dejan_volk | 0:a0204dfc42fd | 6 | |
| dejan_volk | 0:a0204dfc42fd | 7 | Used for sending information about heart beats per minute to the mobile phone via |
| dejan_volk | 0:a0204dfc42fd | 8 | Bluetooth module WT11-A-AI made by Bluegiga. |
| dejan_volk | 0:a0204dfc42fd | 9 | |
| dejan_volk | 0:a0204dfc42fd | 10 | Description of pins used: |
| dejan_volk | 0:a0204dfc42fd | 11 | |
| dejan_volk | 0:a0204dfc42fd | 12 | p5 - RESET switch for Polar's RMCM01 OEM module |
| dejan_volk | 0:a0204dfc42fd | 13 | p8 - RESET switch on WT11 |
| dejan_volk | 0:a0204dfc42fd | 14 | p9 - TX for communicating with WT11 |
| dejan_volk | 0:a0204dfc42fd | 15 | p10 - RX for communicating with WT11 |
| dejan_volk | 0:a0204dfc42fd | 16 | p11 - DigitalIn for Polar's RMCM01 OEM module (pin HR) |
| dejan_volk | 0:a0204dfc42fd | 17 | p12 - DigitalIn for Polar's RMCM01 OEM module (pin FPLS) |
| dejan_volk | 0:a0204dfc42fd | 18 | |
| dejan_volk | 0:a0204dfc42fd | 19 | Possible improvements: |
| dejan_volk | 0:a0204dfc42fd | 20 | - Integrating a LCD monitor that will use the serial port to display information about the current heart beat |
| dejan_volk | 0:a0204dfc42fd | 21 | - Implementing a more efficient algorithm to calculate the heart beat |
| dejan_volk | 0:a0204dfc42fd | 22 | - Better code comments (doh) |
| dejan_volk | 0:a0204dfc42fd | 23 | |
| dejan_volk | 0:a0204dfc42fd | 24 | */ |
| dejan_volk | 0:a0204dfc42fd | 25 | |
| dejan_volk | 0:a0204dfc42fd | 26 | #include "mbed.h" |
| dejan_volk | 0:a0204dfc42fd | 27 | |
| dejan_volk | 0:a0204dfc42fd | 28 | //************global declarations***************** |
| dejan_volk | 0:a0204dfc42fd | 29 | Serial s_port(p9,p10); |
| dejan_volk | 0:a0204dfc42fd | 30 | DigitalOut led_yes(LED1); //visual indicator for sending data to WT11 |
| dejan_volk | 0:a0204dfc42fd | 31 | DigitalOut reset(p8); //reset trigger on WT11 |
| dejan_volk | 0:a0204dfc42fd | 32 | DigitalOut reset_RMCM(p5); //reset trigger on RMCM01 |
| dejan_volk | 0:a0204dfc42fd | 33 | InterruptIn heartBeat(p12); |
| dejan_volk | 0:a0204dfc42fd | 34 | Timer timer; |
| dejan_volk | 0:a0204dfc42fd | 35 | int interval; //interval between heartbeats |
| dejan_volk | 0:a0204dfc42fd | 36 | int intrHB; //heartbeats per minute |
| dejan_volk | 0:a0204dfc42fd | 37 | int baud_rate = 115200; |
| dejan_volk | 0:a0204dfc42fd | 38 | int bits = 8; |
| dejan_volk | 0:a0204dfc42fd | 39 | int stop_bits = 1; |
| dejan_volk | 0:a0204dfc42fd | 40 | int counter_HB = 0; |
| dejan_volk | 0:a0204dfc42fd | 41 | int array[3]; //small array for storing heart beats |
| dejan_volk | 0:a0204dfc42fd | 42 | bool intrPin = false; |
| dejan_volk | 0:a0204dfc42fd | 43 | int previous = 11; //variable that stores the previously calculated heartbeat |
| dejan_volk | 0:a0204dfc42fd | 44 | //***********end of global declarations************* |
| dejan_volk | 0:a0204dfc42fd | 45 | |
| dejan_volk | 0:a0204dfc42fd | 46 | void initWT11() { //init function for initial configuration of WT11 module |
| dejan_volk | 0:a0204dfc42fd | 47 | int zero = 0; |
| dejan_volk | 0:a0204dfc42fd | 48 | int high = 1; |
| dejan_volk | 0:a0204dfc42fd | 49 | reset.write(zero); //Initializing WT11 for inputting commands |
| dejan_volk | 0:a0204dfc42fd | 50 | wait(0.2); |
| dejan_volk | 0:a0204dfc42fd | 51 | reset.write(high); |
| dejan_volk | 0:a0204dfc42fd | 52 | wait(0.01); |
| dejan_volk | 0:a0204dfc42fd | 53 | reset.write(zero); |
| dejan_volk | 0:a0204dfc42fd | 54 | wait(2); |
| dejan_volk | 0:a0204dfc42fd | 55 | //Configuration for WT11 - setting it to slave mode |
| dejan_volk | 0:a0204dfc42fd | 56 | s_port.printf("SET BT PAGEMODE 3 2000 1\n"); |
| dejan_volk | 0:a0204dfc42fd | 57 | s_port.printf("SET BT NAME HBMWT11\n"); |
| dejan_volk | 0:a0204dfc42fd | 58 | s_port.printf("SET BT ROLE 0 f 7d00\n"); |
| dejan_volk | 0:a0204dfc42fd | 59 | s_port.printf("SET CONTROL ECHO 0\n"); |
| dejan_volk | 0:a0204dfc42fd | 60 | s_port.printf("SET BT AUTH * 1234\n"); |
| dejan_volk | 0:a0204dfc42fd | 61 | s_port.printf("SET CONTROL ESCAPE 43 00 0\n"); |
| dejan_volk | 0:a0204dfc42fd | 62 | s_port.printf("SET CONTROL CD 00 0\n"); |
| dejan_volk | 0:a0204dfc42fd | 63 | s_port.printf("SET CONTROL BAUD 115200,8n1\n"); |
| dejan_volk | 0:a0204dfc42fd | 64 | } |
| dejan_volk | 0:a0204dfc42fd | 65 | |
| dejan_volk | 0:a0204dfc42fd | 66 | void initRMCM() { //init function for initial configuration of RMCM01 |
| dejan_volk | 0:a0204dfc42fd | 67 | int zero = 0; |
| dejan_volk | 0:a0204dfc42fd | 68 | int high = 1; |
| dejan_volk | 0:a0204dfc42fd | 69 | reset_RMCM.write(high); |
| dejan_volk | 0:a0204dfc42fd | 70 | wait(0.1); |
| dejan_volk | 0:a0204dfc42fd | 71 | reset_RMCM.write(zero); |
| dejan_volk | 0:a0204dfc42fd | 72 | wait(0.1); |
| dejan_volk | 0:a0204dfc42fd | 73 | reset_RMCM.write(high); |
| dejan_volk | 0:a0204dfc42fd | 74 | } |
| dejan_volk | 0:a0204dfc42fd | 75 | |
| dejan_volk | 0:a0204dfc42fd | 76 | void intrTrigRise() { |
| dejan_volk | 0:a0204dfc42fd | 77 | led_yes=1; //triggering the indicator led |
| dejan_volk | 0:a0204dfc42fd | 78 | interval=timer.read_ms(); //reads the interval |
| dejan_volk | 0:a0204dfc42fd | 79 | timer.stop(); |
| dejan_volk | 0:a0204dfc42fd | 80 | timer.reset(); //resets the timer |
| dejan_volk | 0:a0204dfc42fd | 81 | timer.start(); //starts the timer |
| dejan_volk | 0:a0204dfc42fd | 82 | intrPin = true; //sets the interrupt to true |
| dejan_volk | 0:a0204dfc42fd | 83 | heartBeat.mode(PullNone); |
| dejan_volk | 0:a0204dfc42fd | 84 | wait(0.29); |
| dejan_volk | 0:a0204dfc42fd | 85 | heartBeat.mode(PullUp); |
| dejan_volk | 0:a0204dfc42fd | 86 | } |
| dejan_volk | 0:a0204dfc42fd | 87 | |
| dejan_volk | 0:a0204dfc42fd | 88 | void interpolation() { |
| dejan_volk | 0:a0204dfc42fd | 89 | if (interval != 0) { //avoiding divide-by-zero error |
| dejan_volk | 0:a0204dfc42fd | 90 | array[counter_HB] = interval; |
| dejan_volk | 0:a0204dfc42fd | 91 | counter_HB++; |
| dejan_volk | 0:a0204dfc42fd | 92 | led_yes=0; |
| dejan_volk | 0:a0204dfc42fd | 93 | |
| dejan_volk | 0:a0204dfc42fd | 94 | if (counter_HB == 3) { //when the array is full, start calculating the average heartbeat |
| dejan_volk | 0:a0204dfc42fd | 95 | counter_HB = 0; |
| dejan_volk | 0:a0204dfc42fd | 96 | int minute = 60000; |
| dejan_volk | 0:a0204dfc42fd | 97 | int avrg = 0; |
| dejan_volk | 0:a0204dfc42fd | 98 | int temp = 0; |
| dejan_volk | 0:a0204dfc42fd | 99 | int flag = 1; |
| dejan_volk | 0:a0204dfc42fd | 100 | int avg = 0; |
| dejan_volk | 0:a0204dfc42fd | 101 | |
| dejan_volk | 0:a0204dfc42fd | 102 | for (int i = 1; i <= 3 && flag; i++) { //bubble sort in descending order |
| dejan_volk | 0:a0204dfc42fd | 103 | flag = 0; |
| dejan_volk | 0:a0204dfc42fd | 104 | |
| dejan_volk | 0:a0204dfc42fd | 105 | for (int j = 0; j < 2; j++) { |
| dejan_volk | 0:a0204dfc42fd | 106 | if (array[j+1] > array[j]) { |
| dejan_volk | 0:a0204dfc42fd | 107 | temp = array[j]; |
| dejan_volk | 0:a0204dfc42fd | 108 | array[j] = array[j+1]; |
| dejan_volk | 0:a0204dfc42fd | 109 | array[j+1] = temp; |
| dejan_volk | 0:a0204dfc42fd | 110 | flag = 1; |
| dejan_volk | 0:a0204dfc42fd | 111 | } |
| dejan_volk | 0:a0204dfc42fd | 112 | } |
| dejan_volk | 0:a0204dfc42fd | 113 | } |
| dejan_volk | 0:a0204dfc42fd | 114 | |
| dejan_volk | 0:a0204dfc42fd | 115 | int sum1 = (60000/array[0]) - (60000/array[1]); //taking the middle value and calculate the difference |
| dejan_volk | 0:a0204dfc42fd | 116 | int sum2 = (60000/array[2]) - (60000/array[1]); //between both remaining values |
| dejan_volk | 0:a0204dfc42fd | 117 | |
| dejan_volk | 0:a0204dfc42fd | 118 | if ((sum1 >= -10 || sum1 <= 10) || (sum2 >= -10 || sum2 <= 10)) { //if all values do not deviate by +/- 10 heart beats |
| dejan_volk | 0:a0204dfc42fd | 119 | for (int i = 0; i < 3; i++) { //then take all 3 and calculate the average |
| dejan_volk | 0:a0204dfc42fd | 120 | avrg+=array[i]; |
| dejan_volk | 0:a0204dfc42fd | 121 | } |
| dejan_volk | 0:a0204dfc42fd | 122 | avg = avrg/3; |
| dejan_volk | 0:a0204dfc42fd | 123 | } else if ((sum1 >= -10 || sum1 <= 10) || (sum2 > 10 || sum2 < -10)) { //if 2nd sum deviates for more than +/- 10 heartbeats |
| dejan_volk | 0:a0204dfc42fd | 124 | avrg = array[0] + array[1]; //take the elements of 1st sum |
| dejan_volk | 0:a0204dfc42fd | 125 | avg = avrg/2; |
| dejan_volk | 0:a0204dfc42fd | 126 | } else if ((sum1 < -10 || sum1 > 10) || (sum2 <= 10 || sum2 >= -10)) { //if 1st sum deviates for more than +/- 10 heartbeats |
| dejan_volk | 0:a0204dfc42fd | 127 | avrg = array[2] + array[1]; //take the elements of 2nd sum |
| dejan_volk | 0:a0204dfc42fd | 128 | avg = avrg/2; |
| dejan_volk | 0:a0204dfc42fd | 129 | } else { |
| dejan_volk | 0:a0204dfc42fd | 130 | avg = previous; //if all 3 values deviate for more than -/+ 10 heart beats, discard all and show the previous |
| dejan_volk | 0:a0204dfc42fd | 131 | } //"good" heartbeat |
| dejan_volk | 0:a0204dfc42fd | 132 | |
| dejan_volk | 0:a0204dfc42fd | 133 | |
| dejan_volk | 0:a0204dfc42fd | 134 | intrHB = minute/avg; //interpolating the interval to 1 min |
| dejan_volk | 0:a0204dfc42fd | 135 | intrPin = false; //setting the interrupt to false |
| dejan_volk | 0:a0204dfc42fd | 136 | s_port.printf("#%i*",intrHB); |
| dejan_volk | 0:a0204dfc42fd | 137 | previous = avg; |
| dejan_volk | 0:a0204dfc42fd | 138 | } |
| dejan_volk | 0:a0204dfc42fd | 139 | intrPin = false; |
| dejan_volk | 0:a0204dfc42fd | 140 | } else |
| dejan_volk | 0:a0204dfc42fd | 141 | intrPin=false; |
| dejan_volk | 0:a0204dfc42fd | 142 | } |
| dejan_volk | 0:a0204dfc42fd | 143 | |
| dejan_volk | 0:a0204dfc42fd | 144 | int main() { |
| dejan_volk | 0:a0204dfc42fd | 145 | initRMCM(); //calling init function for RMCM01 |
| dejan_volk | 0:a0204dfc42fd | 146 | s_port.baud(baud_rate); |
| dejan_volk | 0:a0204dfc42fd | 147 | s_port.format(bits,Serial::None,stop_bits); |
| dejan_volk | 0:a0204dfc42fd | 148 | initWT11(); //calling the init function for setting WT11 |
| dejan_volk | 0:a0204dfc42fd | 149 | s_port.printf("SELECT 0\n"); //switching from COMMAND mode to DATA mode |
| dejan_volk | 0:a0204dfc42fd | 150 | |
| dejan_volk | 0:a0204dfc42fd | 151 | while (1) { |
| dejan_volk | 0:a0204dfc42fd | 152 | heartBeat.rise(&intrTrigRise); //trigger for interrupt |
| dejan_volk | 0:a0204dfc42fd | 153 | if (intrPin == true && interval != 0) { //if interrupt is true and interval is not 0, send data |
| dejan_volk | 0:a0204dfc42fd | 154 | interpolation(); |
| dejan_volk | 0:a0204dfc42fd | 155 | } |
| dejan_volk | 0:a0204dfc42fd | 156 | } |
| dejan_volk | 0:a0204dfc42fd | 157 | } |