CIS441 Controller

Dependencies:   TextLCD mbed-rtos mbed

Fork of PacemakerController by Chad Nachiappan

Committer:
mhowar
Date:
Wed Dec 02 05:19:41 2015 +0000
Revision:
41:d806d68f7969
Parent:
35:b5e8650a7669
Child:
42:2e4ed3a53c64
Added bpm calculation mode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chadnach1 0:d92e0b71c6b5 1 #include "mbed.h"
lucastai 2:5e9c4d83d038 2 #include "LPC17xx.h"
lucastai 2:5e9c4d83d038 3 #include "TextLCD.h"
lucastai 2:5e9c4d83d038 4 #include "rtos.h"
lucastai 2:5e9c4d83d038 5 #include "Thread.h"
lucastai 2:5e9c4d83d038 6 using namespace rtos;
chadnach1 0:d92e0b71c6b5 7
chadnach1 0:d92e0b71c6b5 8 // This is for the pacemaker
lucastai 2:5e9c4d83d038 9 volatile unsigned short timer_count;
lucastai 2:5e9c4d83d038 10 Serial pc(USBTX, USBRX);
lucastai 2:5e9c4d83d038 11 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7
lucastai 2:5e9c4d83d038 12
mhowar 41:d806d68f7969 13 Ticker rate_monitor;
mhowar 41:d806d68f7969 14
mhowar 5:9eee15818b0e 15 int keyboard_needs_numeric = 0; // boolean - is in middle of interval input?
chadnach1 0:d92e0b71c6b5 16
chadnach1 0:d92e0b71c6b5 17 int h_clock;
amiche 3:44d132582373 18 int pm_clock;
chadnach1 27:2c5aefcf3000 19 Timer avi_clk;
chadnach1 32:c58b6651336c 20 Timer t_count;
chadnach1 0:d92e0b71c6b5 21
chadnach1 4:f04eb7f96f4b 22 int LRI = 1000;
chadnach1 4:f04eb7f96f4b 23 int AVI = 150;
chadnach1 4:f04eb7f96f4b 24 int PVARP = 300;
chadnach1 4:f04eb7f96f4b 25 int VRP = 200;
lucastai 2:5e9c4d83d038 26
lucastai 2:5e9c4d83d038 27 // constants
lucastai 2:5e9c4d83d038 28 int MAX_PM_RT = 180;
lucastai 2:5e9c4d83d038 29 int MIN_PM_RT = 40;
lucastai 2:5e9c4d83d038 30 enum mode {NORMAL, SLEEP, EXERCISE, MANUAL};
lucastai 2:5e9c4d83d038 31
lucastai 2:5e9c4d83d038 32 // counters
mhowar 41:d806d68f7969 33 volatile int beats = 0;
lucastai 2:5e9c4d83d038 34
lucastai 2:5e9c4d83d038 35 // state variables
lucastai 2:5e9c4d83d038 36 int upper_bound = 100;
lucastai 2:5e9c4d83d038 37 int lower_bound = 40;
lucastai 2:5e9c4d83d038 38 int obs_int = 10;
lucastai 2:5e9c4d83d038 39 mode curr_mode = NORMAL;
lucastai 2:5e9c4d83d038 40
amiche 19:f3615321b5c4 41 // alarms
lucastai 14:a16b636c2bbc 42 DigitalOut led_apace(LED1);
lucastai 14:a16b636c2bbc 43 DigitalOut led_vpace(LED2);
chadnach1 33:b68c1648206e 44 DigitalOut led_asense(LED3);
chadnach1 33:b68c1648206e 45 DigitalOut led_vsense(LED4);
lucastai 2:5e9c4d83d038 46
chadnach1 27:2c5aefcf3000 47 DigitalIn agetSignal(p24);
chadnach1 27:2c5aefcf3000 48 DigitalIn vgetSignal(p23);
chadnach1 27:2c5aefcf3000 49 DigitalOut apaceSignal(p22);
chadnach1 27:2c5aefcf3000 50 DigitalOut vpaceSignal(p21);
chadnach1 27:2c5aefcf3000 51
chadnach1 27:2c5aefcf3000 52 bool v_sense;
chadnach1 27:2c5aefcf3000 53 bool a_sense;
chadnach1 27:2c5aefcf3000 54
chadnach1 27:2c5aefcf3000 55 void asense()
chadnach1 27:2c5aefcf3000 56 {
chadnach1 27:2c5aefcf3000 57 a_sense = 1;
chadnach1 33:b68c1648206e 58 led_asense = 1;
chadnach1 27:2c5aefcf3000 59 Thread::wait(10);
chadnach1 33:b68c1648206e 60 led_asense = 0;
chadnach1 27:2c5aefcf3000 61 a_sense = 0;
chadnach1 27:2c5aefcf3000 62 }
lucastai 18:d4cd9d12345e 63
chadnach1 27:2c5aefcf3000 64 void vsense()
chadnach1 27:2c5aefcf3000 65 {
chadnach1 27:2c5aefcf3000 66 v_sense = 1;
chadnach1 33:b68c1648206e 67 led_vsense = 1;
chadnach1 27:2c5aefcf3000 68 Thread::wait(10);
chadnach1 33:b68c1648206e 69 led_vsense = 0;
chadnach1 27:2c5aefcf3000 70 v_sense = 0;
mhowar 41:d806d68f7969 71 beats++;
chadnach1 27:2c5aefcf3000 72 }
chadnach1 27:2c5aefcf3000 73
chadnach1 27:2c5aefcf3000 74 void apace()
chadnach1 27:2c5aefcf3000 75 {
chadnach1 27:2c5aefcf3000 76 apaceSignal = 1;
chadnach1 27:2c5aefcf3000 77 led_apace = 1;
chadnach1 27:2c5aefcf3000 78 Thread::wait(10);
chadnach1 33:b68c1648206e 79 led_apace = 0;
chadnach1 27:2c5aefcf3000 80 apaceSignal = 0;
chadnach1 27:2c5aefcf3000 81
chadnach1 27:2c5aefcf3000 82 }
chadnach1 27:2c5aefcf3000 83
chadnach1 27:2c5aefcf3000 84 void vpace()
chadnach1 27:2c5aefcf3000 85 {
chadnach1 27:2c5aefcf3000 86 vpaceSignal = 1;
chadnach1 27:2c5aefcf3000 87 led_vpace = 1;
chadnach1 27:2c5aefcf3000 88 Thread::wait(10);
chadnach1 33:b68c1648206e 89 led_vpace = 0;
chadnach1 27:2c5aefcf3000 90 vpaceSignal = 0;
chadnach1 27:2c5aefcf3000 91 }
amiche 24:81cd9ef5c4f6 92
lucastai 2:5e9c4d83d038 93 // hardware interrupt handler, adapted from code in piazza post by Dagaen
chadnach1 33:b68c1648206e 94 /*extern "C" void TIMER0_IRQHandler (void)
lucastai 2:5e9c4d83d038 95 {
amiche 19:f3615321b5c4 96 if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
amiche 19:f3615321b5c4 97 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
amiche 19:f3615321b5c4 98 timer_count++; //increment timer_count
lucastai 2:5e9c4d83d038 99 }
lucastai 2:5e9c4d83d038 100 }
chadnach1 33:b68c1648206e 101 */
chadnach1 33:b68c1648206e 102 /*// init the hardware interrupt (timer0), adapted same as above
lucastai 2:5e9c4d83d038 103 void timer0_init(void)
lucastai 2:5e9c4d83d038 104 {
lucastai 2:5e9c4d83d038 105 LPC_SC->PCONP |=1<1; //timer0 power on
lucastai 2:5e9c4d83d038 106 LPC_SC-> PCLKSEL0 |= 1 << 2; // set timer clock to CCLCK nondivided (1 clock cycle = 1 increment)
lucastai 2:5e9c4d83d038 107 LPC_TIM0->MR0 = 1000000; //100mhz clock cycle, 1 cycle = 10ns, 10ms = 10 000 000 ns = 1M cycles
lucastai 2:5e9c4d83d038 108 LPC_TIM0->MCR = 3; //interrupt and reset control
amiche 19:f3615321b5c4 109 //3 = Interrupt & reset timer0 on match (111) sets all three bits
lucastai 2:5e9c4d83d038 110 NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt
chadnach1 33:b68c1648206e 111 }*/
chadnach1 0:d92e0b71c6b5 112
lucastai 2:5e9c4d83d038 113
chadnach1 27:2c5aefcf3000 114 void PM_ALARM(void const *args)
amiche 19:f3615321b5c4 115 {
chadnach1 29:5cec671cb80d 116 while (1) {
chadnach1 29:5cec671cb80d 117 // min hr alarm
chadnach1 29:5cec671cb80d 118 if( beats < MIN_PM_RT) {
chadnach1 30:97db5a684eaa 119 lcd.cls();
chadnach1 29:5cec671cb80d 120 lcd.locate(0,1);
chadnach1 30:97db5a684eaa 121 lcd.printf("!<\n");
chadnach1 29:5cec671cb80d 122 }
chadnach1 31:7f0e865e3d4b 123
chadnach1 29:5cec671cb80d 124 // max hr alarm
chadnach1 29:5cec671cb80d 125 if(beats > MAX_PM_RT) {
chadnach1 30:97db5a684eaa 126 lcd.cls();
chadnach1 29:5cec671cb80d 127 lcd.locate(0,1);
chadnach1 30:97db5a684eaa 128 lcd.printf("!>\n");
chadnach1 29:5cec671cb80d 129 }
amiche 19:f3615321b5c4 130 }
amiche 19:f3615321b5c4 131
chadnach1 0:d92e0b71c6b5 132 }
chadnach1 0:d92e0b71c6b5 133
lucastai 2:5e9c4d83d038 134 // hw interrupt callback, deal with the keyboard input from PC
lucastai 2:5e9c4d83d038 135
chadnach1 27:2c5aefcf3000 136 void pm_sense(void const *args)
amiche 19:f3615321b5c4 137 {
amiche 19:f3615321b5c4 138
chadnach1 4:f04eb7f96f4b 139 while(1) {
chadnach1 33:b68c1648206e 140 /*if (agetSignal == 0) {
chadnach1 33:b68c1648206e 141 pc.printf("Aget:LO\n");
chadnach1 33:b68c1648206e 142 }
chadnach1 33:b68c1648206e 143
chadnach1 33:b68c1648206e 144 if (vgetSignal == 0) {
chadnach1 33:b68c1648206e 145 pc.printf("Vget:LO\n");
chadnach1 33:b68c1648206e 146 }
chadnach1 33:b68c1648206e 147 if (agetSignal == 1) {
chadnach1 33:b68c1648206e 148 pc.printf("Aget:HI\n");
chadnach1 33:b68c1648206e 149 }
chadnach1 33:b68c1648206e 150
chadnach1 33:b68c1648206e 151 if (vgetSignal == 1) {
chadnach1 33:b68c1648206e 152 pc.printf("Vget:HI\n");
chadnach1 33:b68c1648206e 153 }*/
chadnach1 33:b68c1648206e 154
amiche 19:f3615321b5c4 155
chadnach1 33:b68c1648206e 156 if (t_count.read_ms() >= VRP && vgetSignal == 1) {
chadnach1 34:b4b50ca26e8c 157
amiche 24:81cd9ef5c4f6 158 // Valid_V state
chadnach1 32:c58b6651336c 159 t_count.reset();
amiche 24:81cd9ef5c4f6 160 vsense();
amiche 24:81cd9ef5c4f6 161
chadnach1 33:b68c1648206e 162 } else if (t_count.read_ms() < VRP && vgetSignal == 1) {
chadnach1 34:b4b50ca26e8c 163 //pc.printf("invalid v state\n");
chadnach1 32:c58b6651336c 164
chadnach1 20:dc272bfaa276 165 // Invalid_V state
chadnach1 4:f04eb7f96f4b 166 }
amiche 19:f3615321b5c4 167
chadnach1 33:b68c1648206e 168 if (t_count.read_ms() < PVARP && agetSignal == 1) {
chadnach1 20:dc272bfaa276 169 // Invalid_A state
chadnach1 34:b4b50ca26e8c 170
chadnach1 34:b4b50ca26e8c 171 //pc.printf("invalid a state\n");
chadnach1 33:b68c1648206e 172 } else if (t_count.read_ms() >= PVARP && agetSignal == 1) {
chadnach1 20:dc272bfaa276 173 // Valid_A state
amiche 24:81cd9ef5c4f6 174 asense();
chadnach1 4:f04eb7f96f4b 175 }
chadnach1 4:f04eb7f96f4b 176 }
amiche 3:44d132582373 177 }
amiche 3:44d132582373 178
chadnach1 27:2c5aefcf3000 179 void pm_response(void const *args)
amiche 19:f3615321b5c4 180 {
chadnach1 4:f04eb7f96f4b 181 while(1) {
chadnach1 34:b4b50ca26e8c 182
chadnach1 27:2c5aefcf3000 183 bool goInitalState = 1;
chadnach1 33:b68c1648206e 184 if (t_count.read_ms() >= LRI - AVI) {
chadnach1 33:b68c1648206e 185
chadnach1 27:2c5aefcf3000 186 goInitalState = 0;
chadnach1 34:b4b50ca26e8c 187
amiche 19:f3615321b5c4 188 // PM_A! sets the LED high
amiche 19:f3615321b5c4 189 led_apace = 1;
amiche 19:f3615321b5c4 190
amiche 19:f3615321b5c4 191 avi_clk.reset();
amiche 19:f3615321b5c4 192
amiche 19:f3615321b5c4 193 apace();
chadnach1 34:b4b50ca26e8c 194
amiche 19:f3615321b5c4 195 // At Atrial Event State
chadnach1 33:b68c1648206e 196 while (avi_clk.read_ms() < AVI) {
chadnach1 34:b4b50ca26e8c 197
chadnach1 27:2c5aefcf3000 198 if (v_sense == 1) {
chadnach1 27:2c5aefcf3000 199 goInitalState = 1;
amiche 24:81cd9ef5c4f6 200 break;
amiche 24:81cd9ef5c4f6 201 }
amiche 24:81cd9ef5c4f6 202 }
chadnach1 27:2c5aefcf3000 203 if (!goInitalState) {
amiche 24:81cd9ef5c4f6 204 // Ventricular Event
chadnach1 32:c58b6651336c 205 t_count.reset();
amiche 19:f3615321b5c4 206
amiche 24:81cd9ef5c4f6 207 // PM_V! sets the LED high
amiche 24:81cd9ef5c4f6 208 led_vpace = 1;
amiche 19:f3615321b5c4 209
chadnach1 32:c58b6651336c 210 t_count.reset();
chadnach1 33:b68c1648206e 211 pc.printf("in pmresponse 2");
amiche 24:81cd9ef5c4f6 212 vpace();
amiche 24:81cd9ef5c4f6 213 }
chadnach1 33:b68c1648206e 214 } else if (t_count.read_ms() < LRI - AVI) {
chadnach1 34:b4b50ca26e8c 215
amiche 24:81cd9ef5c4f6 216 // if Asense, move on to atrial event
chadnach1 27:2c5aefcf3000 217 if (a_sense == 1) {
chadnach1 27:2c5aefcf3000 218 goInitalState = 0;
chadnach1 34:b4b50ca26e8c 219
amiche 24:81cd9ef5c4f6 220 avi_clk.reset();
amiche 19:f3615321b5c4 221
amiche 24:81cd9ef5c4f6 222 // At Atrial Event State
amiche 24:81cd9ef5c4f6 223 while (avi_clk.read() < AVI) {
chadnach1 27:2c5aefcf3000 224 if (v_sense == 1) {
chadnach1 27:2c5aefcf3000 225 goInitalState = 1;
amiche 24:81cd9ef5c4f6 226 break;
amiche 24:81cd9ef5c4f6 227 }
amiche 24:81cd9ef5c4f6 228 }
amiche 19:f3615321b5c4 229
amiche 24:81cd9ef5c4f6 230 // Ventricular Event
chadnach1 32:c58b6651336c 231 t_count.reset();
amiche 24:81cd9ef5c4f6 232
amiche 24:81cd9ef5c4f6 233 // PM_V! sets the LED high
amiche 24:81cd9ef5c4f6 234 led_vpace = 1;
amiche 24:81cd9ef5c4f6 235
chadnach1 32:c58b6651336c 236 t_count.reset();
amiche 24:81cd9ef5c4f6 237 vpace();
amiche 24:81cd9ef5c4f6 238 }
amiche 19:f3615321b5c4 239 }
amiche 19:f3615321b5c4 240 }
amiche 3:44d132582373 241 }
lucastai 2:5e9c4d83d038 242
mhowar 41:d806d68f7969 243 /* Every observation interval, calculate beats per minute and display
mhowar 41:d806d68f7969 244 *
mhowar 41:d806d68f7969 245 */
mhowar 41:d806d68f7969 246 void update_display() {
mhowar 41:d806d68f7969 247 double rate = beats / (double) obs_int / 60;
mhowar 41:d806d68f7969 248 //reset count
mhowar 41:d806d68f7969 249 beats = 0;
mhowar 41:d806d68f7969 250 lcd.cls();
mhowar 41:d806d68f7969 251 lcd.locate(0,0);
mhowar 41:d806d68f7969 252 lcd.printf("BPM: %.1f ");
mhowar 41:d806d68f7969 253 }
mhowar 41:d806d68f7969 254
amiche 19:f3615321b5c4 255 int main()
amiche 19:f3615321b5c4 256 {
chadnach1 16:ae9df4194011 257 // https://developer.mbed.org/users/chadnach1/code/PacemakerController/
lucastai 2:5e9c4d83d038 258 // connect the serial device (PC keybd) to the interrupt
amiche 19:f3615321b5c4 259
chadnach1 16:ae9df4194011 260 // Start LED's Off
chadnach1 17:c89cddf30925 261 led_apace = 0;
chadnach1 17:c89cddf30925 262 led_vpace = 0;
amiche 19:f3615321b5c4 263
chadnach1 16:ae9df4194011 264 // Start the avi_clock
chadnach1 16:ae9df4194011 265 avi_clk.start();
chadnach1 33:b68c1648206e 266 avi_clk.reset();
amiche 19:f3615321b5c4 267
chadnach1 30:97db5a684eaa 268 Thread t1(pm_sense, (void *)"");
chadnach1 30:97db5a684eaa 269 Thread t2(pm_response, (void *)"");
chadnach1 31:7f0e865e3d4b 270 Thread t3(PM_ALARM, (void *)"");
mhowar 41:d806d68f7969 271
mhowar 41:d806d68f7969 272 //update_display
mhowar 41:d806d68f7969 273 rate_monitor.attach(&update_display, obs_int);
mhowar 41:d806d68f7969 274
chadnach1 33:b68c1648206e 275 t_count.start();
chadnach1 33:b68c1648206e 276 t_count.reset();
chadnach1 31:7f0e865e3d4b 277
chadnach1 31:7f0e865e3d4b 278 while(1) {
mhowar 35:b5e8650a7669 279 if (pc.readable()) {
mhowar 35:b5e8650a7669 280
mhowar 35:b5e8650a7669 281 char a = pc.getc();
mhowar 35:b5e8650a7669 282
mhowar 35:b5e8650a7669 283 // Handle different keyboard inputs
mhowar 35:b5e8650a7669 284 if (keyboard_needs_numeric) {
mhowar 35:b5e8650a7669 285 if (a >= '0' && a <= '9') {
mhowar 35:b5e8650a7669 286 // update observation interval
mhowar 35:b5e8650a7669 287 obs_int = (a - '0' + 1) * 5;
mhowar 35:b5e8650a7669 288 keyboard_needs_numeric = 0;
mhowar 41:d806d68f7969 289 rate_monitor.attach(&update_display, obs_int);
mhowar 41:d806d68f7969 290 pc.printf("Set observation interval to %d seconds\n", obs_int);
mhowar 35:b5e8650a7669 291 } else {
mhowar 35:b5e8650a7669 292 pc.printf("Expected numeric key\n");
mhowar 35:b5e8650a7669 293 }
mhowar 35:b5e8650a7669 294 } else if(a == 'N') {
mhowar 35:b5e8650a7669 295 // if the char is N, update bounds to normal mode
mhowar 35:b5e8650a7669 296 curr_mode = NORMAL;
mhowar 35:b5e8650a7669 297 upper_bound = 100;
mhowar 35:b5e8650a7669 298 lower_bound = 40;
mhowar 35:b5e8650a7669 299 pc.printf("MODE IS N\n");
mhowar 35:b5e8650a7669 300 // if the char is S, set bounds to sleep
mhowar 35:b5e8650a7669 301 } else if (a == 'S') {
mhowar 35:b5e8650a7669 302 curr_mode = SLEEP;
mhowar 35:b5e8650a7669 303 upper_bound = 60;
mhowar 35:b5e8650a7669 304 lower_bound = 30;
mhowar 35:b5e8650a7669 305 pc.printf("MODE IS S\n");
mhowar 35:b5e8650a7669 306 // if the char is E, set bounds to exercise
mhowar 35:b5e8650a7669 307 } else if (a == 'E') {
mhowar 35:b5e8650a7669 308 curr_mode = EXERCISE;
mhowar 35:b5e8650a7669 309 upper_bound = 175;
mhowar 35:b5e8650a7669 310 lower_bound = 100;
mhowar 35:b5e8650a7669 311 pc.printf("MODE IS E\n");
mhowar 35:b5e8650a7669 312 // if the char is M, set to manual
mhowar 35:b5e8650a7669 313 } else if (a == 'M') {
mhowar 35:b5e8650a7669 314 curr_mode = MANUAL;
mhowar 35:b5e8650a7669 315 upper_bound = 175;
mhowar 35:b5e8650a7669 316 lower_bound = 30;
mhowar 35:b5e8650a7669 317 pc.printf("MODE IS MANUAL\n");
mhowar 35:b5e8650a7669 318 // check for A if mode is manual
mhowar 35:b5e8650a7669 319 } else if (a == 'A') {
mhowar 35:b5e8650a7669 320 if(curr_mode == MANUAL) {
mhowar 35:b5e8650a7669 321 pc.printf("MODE IS MANUAL GOT APACE\n");
mhowar 35:b5e8650a7669 322 }
mhowar 35:b5e8650a7669 323 // check for V is mode is manual
mhowar 35:b5e8650a7669 324 } else if (a == 'V') {
mhowar 35:b5e8650a7669 325 if(curr_mode == MANUAL) {
mhowar 35:b5e8650a7669 326 pc.printf("MODE IS MANUAL GOT VPACE\n");
mhowar 35:b5e8650a7669 327 }
mhowar 35:b5e8650a7669 328 } else if (a == 'O') {
mhowar 35:b5e8650a7669 329 keyboard_needs_numeric = 1;
mhowar 35:b5e8650a7669 330 } else {
mhowar 35:b5e8650a7669 331 // do nothing for invalid char
mhowar 35:b5e8650a7669 332 }
chadnach1 31:7f0e865e3d4b 333 }
chadnach1 31:7f0e865e3d4b 334 }
chadnach1 31:7f0e865e3d4b 335 }