CIS441 Controller

Dependencies:   TextLCD mbed-rtos mbed

Fork of PacemakerController by Chad Nachiappan

Committer:
chadnach1
Date:
Wed Dec 02 07:50:33 2015 +0000
Revision:
46:2d9cf50b4bc3
Parent:
45:ec9e76ccec6c
Child:
47:bac7fc1a44fe
Pacemaker completely works! Huge thanks to Matt for figuring this out haha

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
chadnach1 46:2d9cf50b4bc3 13 Ticker rate_monitor;
chadnach1 46:2d9cf50b4bc3 14 // counters
chadnach1 46:2d9cf50b4bc3 15 volatile int beats = 0;
chadnach1 46:2d9cf50b4bc3 16 volatile double bpm = 0;
mhowar 5:9eee15818b0e 17 int keyboard_needs_numeric = 0; // boolean - is in middle of interval input?
chadnach1 0:d92e0b71c6b5 18
chadnach1 0:d92e0b71c6b5 19 int h_clock;
amiche 3:44d132582373 20 int pm_clock;
chadnach1 27:2c5aefcf3000 21 Timer avi_clk;
mhowar 44:dfa38e4b3146 22 Timer sense_clk;
mhowar 44:dfa38e4b3146 23 Timer pulse_clk;
chadnach1 0:d92e0b71c6b5 24
chadnach1 43:f28bc869321e 25 int LRI = 1000;
chadnach1 43:f28bc869321e 26 int AVI = 300;
amiche 38:ceabf8f1dfa0 27 int PVARP = 150;
amiche 38:ceabf8f1dfa0 28 int VRP = 100;
lucastai 2:5e9c4d83d038 29
lucastai 2:5e9c4d83d038 30 // constants
lucastai 2:5e9c4d83d038 31 int MAX_PM_RT = 180;
lucastai 2:5e9c4d83d038 32 int MIN_PM_RT = 40;
lucastai 2:5e9c4d83d038 33 enum mode {NORMAL, SLEEP, EXERCISE, MANUAL};
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;
chadnach1 27:2c5aefcf3000 71 }
chadnach1 27:2c5aefcf3000 72
chadnach1 27:2c5aefcf3000 73 void apace()
chadnach1 27:2c5aefcf3000 74 {
chadnach1 27:2c5aefcf3000 75 apaceSignal = 1;
chadnach1 27:2c5aefcf3000 76 led_apace = 1;
chadnach1 27:2c5aefcf3000 77 Thread::wait(10);
chadnach1 33:b68c1648206e 78 led_apace = 0;
chadnach1 27:2c5aefcf3000 79 apaceSignal = 0;
chadnach1 27:2c5aefcf3000 80
chadnach1 27:2c5aefcf3000 81 }
chadnach1 27:2c5aefcf3000 82
chadnach1 27:2c5aefcf3000 83 void vpace()
chadnach1 27:2c5aefcf3000 84 {
chadnach1 27:2c5aefcf3000 85 vpaceSignal = 1;
chadnach1 27:2c5aefcf3000 86 led_vpace = 1;
chadnach1 27:2c5aefcf3000 87 Thread::wait(10);
chadnach1 33:b68c1648206e 88 led_vpace = 0;
chadnach1 27:2c5aefcf3000 89 vpaceSignal = 0;
chadnach1 43:f28bc869321e 90
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 29:5cec671cb80d 119 lcd.locate(0,1);
chadnach1 46:2d9cf50b4bc3 120 lcd.printf("!<");
chadnach1 29:5cec671cb80d 121 }
chadnach1 31:7f0e865e3d4b 122
chadnach1 29:5cec671cb80d 123 // max hr alarm
chadnach1 46:2d9cf50b4bc3 124 else if(beats > MAX_PM_RT) {
chadnach1 46:2d9cf50b4bc3 125 lcd.locate(0,1);
chadnach1 46:2d9cf50b4bc3 126 lcd.printf("!>");
chadnach1 46:2d9cf50b4bc3 127 } else {
chadnach1 29:5cec671cb80d 128 lcd.locate(0,1);
chadnach1 46:2d9cf50b4bc3 129 lcd.printf(" ");
chadnach1 46:2d9cf50b4bc3 130
chadnach1 29:5cec671cb80d 131 }
chadnach1 46:2d9cf50b4bc3 132
chadnach1 46:2d9cf50b4bc3 133 lcd.locate(0,0);
chadnach1 46:2d9cf50b4bc3 134 lcd.printf("BPM: %.1f ", bpm);
amiche 19:f3615321b5c4 135 }
chadnach1 0:d92e0b71c6b5 136 }
chadnach1 0:d92e0b71c6b5 137
lucastai 2:5e9c4d83d038 138 // hw interrupt callback, deal with the keyboard input from PC
lucastai 2:5e9c4d83d038 139
chadnach1 27:2c5aefcf3000 140 void pm_sense(void const *args)
amiche 19:f3615321b5c4 141 {
amiche 19:f3615321b5c4 142
mhowar 44:dfa38e4b3146 143 while(1) {
amiche 19:f3615321b5c4 144
mhowar 44:dfa38e4b3146 145 if (sense_clk.read_ms() >= VRP && vgetSignal == 1) {
chadnach1 34:b4b50ca26e8c 146
amiche 24:81cd9ef5c4f6 147 // Valid_V state
mhowar 44:dfa38e4b3146 148 sense_clk.reset();
amiche 24:81cd9ef5c4f6 149 vsense();
amiche 24:81cd9ef5c4f6 150
mhowar 44:dfa38e4b3146 151 } else if (sense_clk.read_ms() < VRP && vgetSignal == 1) {
chadnach1 20:dc272bfaa276 152 // Invalid_V state
chadnach1 4:f04eb7f96f4b 153 }
amiche 19:f3615321b5c4 154
mhowar 44:dfa38e4b3146 155 if (sense_clk.read_ms() < PVARP && agetSignal == 1) {
chadnach1 20:dc272bfaa276 156 // Invalid_A state
chadnach1 34:b4b50ca26e8c 157
mhowar 44:dfa38e4b3146 158 } else if (sense_clk.read_ms() >= PVARP && agetSignal == 1) {
chadnach1 20:dc272bfaa276 159 // Valid_A state
amiche 24:81cd9ef5c4f6 160 asense();
chadnach1 4:f04eb7f96f4b 161 }
chadnach1 4:f04eb7f96f4b 162 }
amiche 3:44d132582373 163 }
amiche 3:44d132582373 164
chadnach1 27:2c5aefcf3000 165 void pm_response(void const *args)
amiche 19:f3615321b5c4 166 {
chadnach1 4:f04eb7f96f4b 167 while(1) {
chadnach1 34:b4b50ca26e8c 168
mhowar 44:dfa38e4b3146 169 bool goInitialState = 1;
mhowar 44:dfa38e4b3146 170 if (pulse_clk.read_ms() >= LRI - AVI) {
chadnach1 33:b68c1648206e 171
mhowar 44:dfa38e4b3146 172 goInitialState = 0;
chadnach1 34:b4b50ca26e8c 173
amiche 19:f3615321b5c4 174 avi_clk.reset();
amiche 19:f3615321b5c4 175
mhowar 44:dfa38e4b3146 176 // PM_A! sets the LED high
amiche 19:f3615321b5c4 177 apace();
chadnach1 34:b4b50ca26e8c 178
amiche 19:f3615321b5c4 179 // At Atrial Event State
chadnach1 33:b68c1648206e 180 while (avi_clk.read_ms() < AVI) {
chadnach1 34:b4b50ca26e8c 181
chadnach1 27:2c5aefcf3000 182 if (v_sense == 1) {
chadnach1 46:2d9cf50b4bc3 183 beats++;
mhowar 44:dfa38e4b3146 184 //sensed valid ventricular event
mhowar 44:dfa38e4b3146 185 goInitialState = 1;
mhowar 44:dfa38e4b3146 186 pulse_clk.reset();
amiche 24:81cd9ef5c4f6 187 break;
amiche 24:81cd9ef5c4f6 188 }
amiche 24:81cd9ef5c4f6 189 }
mhowar 44:dfa38e4b3146 190 if (!goInitialState) {
amiche 24:81cd9ef5c4f6 191 // Ventricular Event
mhowar 44:dfa38e4b3146 192 pulse_clk.reset();
mhowar 44:dfa38e4b3146 193 sense_clk.reset();
mhowar 44:dfa38e4b3146 194
amiche 24:81cd9ef5c4f6 195 // PM_V! sets the LED high
chadnach1 46:2d9cf50b4bc3 196 beats++;
amiche 24:81cd9ef5c4f6 197 vpace();
amiche 24:81cd9ef5c4f6 198 }
mhowar 44:dfa38e4b3146 199 } else if (pulse_clk.read_ms() < LRI - AVI) {
chadnach1 34:b4b50ca26e8c 200
amiche 24:81cd9ef5c4f6 201 // if Asense, move on to atrial event
chadnach1 27:2c5aefcf3000 202 if (a_sense == 1) {
mhowar 44:dfa38e4b3146 203 goInitialState = 0;
chadnach1 34:b4b50ca26e8c 204
amiche 24:81cd9ef5c4f6 205 avi_clk.reset();
amiche 19:f3615321b5c4 206
amiche 24:81cd9ef5c4f6 207 // At Atrial Event State
amiche 24:81cd9ef5c4f6 208 while (avi_clk.read() < AVI) {
chadnach1 27:2c5aefcf3000 209 if (v_sense == 1) {
chadnach1 46:2d9cf50b4bc3 210 beats++;
mhowar 44:dfa38e4b3146 211 pulse_clk.reset();
mhowar 44:dfa38e4b3146 212 goInitialState = 1;
amiche 24:81cd9ef5c4f6 213 break;
amiche 24:81cd9ef5c4f6 214 }
amiche 24:81cd9ef5c4f6 215 }
mhowar 44:dfa38e4b3146 216 if (!goInitialState) {
mhowar 44:dfa38e4b3146 217 // Ventricular Event
chadnach1 46:2d9cf50b4bc3 218 beats++;
mhowar 44:dfa38e4b3146 219 sense_clk.reset();
mhowar 44:dfa38e4b3146 220 pulse_clk.reset();
mhowar 44:dfa38e4b3146 221
mhowar 44:dfa38e4b3146 222 vpace();
mhowar 44:dfa38e4b3146 223 }
amiche 24:81cd9ef5c4f6 224 }
amiche 19:f3615321b5c4 225 }
amiche 19:f3615321b5c4 226 }
amiche 3:44d132582373 227 }
lucastai 2:5e9c4d83d038 228
chadnach1 46:2d9cf50b4bc3 229 /* Every observation interval, calculate beats per minute and display
chadnach1 46:2d9cf50b4bc3 230 *
chadnach1 46:2d9cf50b4bc3 231 */
chadnach1 46:2d9cf50b4bc3 232 void update_display() {
chadnach1 46:2d9cf50b4bc3 233 bpm = beats / (double) obs_int * 60;
chadnach1 46:2d9cf50b4bc3 234 //reset count
chadnach1 46:2d9cf50b4bc3 235 beats = 0;
chadnach1 46:2d9cf50b4bc3 236 }
chadnach1 46:2d9cf50b4bc3 237
amiche 19:f3615321b5c4 238 int main()
amiche 19:f3615321b5c4 239 {
chadnach1 16:ae9df4194011 240 // https://developer.mbed.org/users/chadnach1/code/PacemakerController/
lucastai 2:5e9c4d83d038 241 // connect the serial device (PC keybd) to the interrupt
amiche 19:f3615321b5c4 242
chadnach1 16:ae9df4194011 243 // Start LED's Off
chadnach1 17:c89cddf30925 244 led_apace = 0;
chadnach1 17:c89cddf30925 245 led_vpace = 0;
amiche 19:f3615321b5c4 246
chadnach1 16:ae9df4194011 247 // Start the avi_clock
chadnach1 16:ae9df4194011 248 avi_clk.start();
chadnach1 33:b68c1648206e 249 avi_clk.reset();
amiche 19:f3615321b5c4 250
chadnach1 30:97db5a684eaa 251 Thread t1(pm_sense, (void *)"");
chadnach1 30:97db5a684eaa 252 Thread t2(pm_response, (void *)"");
chadnach1 31:7f0e865e3d4b 253 Thread t3(PM_ALARM, (void *)"");
mhowar 44:dfa38e4b3146 254 sense_clk.start();
mhowar 44:dfa38e4b3146 255 pulse_clk.start();
chadnach1 46:2d9cf50b4bc3 256
chadnach1 46:2d9cf50b4bc3 257 //update_display
chadnach1 46:2d9cf50b4bc3 258 rate_monitor.attach(&update_display, obs_int);
chadnach1 46:2d9cf50b4bc3 259
chadnach1 31:7f0e865e3d4b 260 while(1) {
mhowar 35:b5e8650a7669 261 if (pc.readable()) {
mhowar 35:b5e8650a7669 262
mhowar 35:b5e8650a7669 263 char a = pc.getc();
mhowar 35:b5e8650a7669 264
mhowar 35:b5e8650a7669 265 // Handle different keyboard inputs
mhowar 35:b5e8650a7669 266 if (keyboard_needs_numeric) {
mhowar 35:b5e8650a7669 267 if (a >= '0' && a <= '9') {
mhowar 35:b5e8650a7669 268 // update observation interval
mhowar 35:b5e8650a7669 269 obs_int = (a - '0' + 1) * 5;
mhowar 35:b5e8650a7669 270 keyboard_needs_numeric = 0;
chadnach1 46:2d9cf50b4bc3 271 rate_monitor.attach(&update_display, obs_int);
chadnach1 46:2d9cf50b4bc3 272 pc.printf("Set observation interval to %d seconds\n", obs_int);
mhowar 35:b5e8650a7669 273 } else {
mhowar 35:b5e8650a7669 274 pc.printf("Expected numeric key\n");
mhowar 35:b5e8650a7669 275 }
chadnach1 46:2d9cf50b4bc3 276 } else if(a == 'N') {
mhowar 35:b5e8650a7669 277 // if the char is N, update bounds to normal mode
mhowar 35:b5e8650a7669 278 curr_mode = NORMAL;
mhowar 35:b5e8650a7669 279 upper_bound = 100;
chadnach1 45:ec9e76ccec6c 280 LRI = 600;
mhowar 35:b5e8650a7669 281 lower_bound = 40;
mhowar 35:b5e8650a7669 282 pc.printf("MODE IS N\n");
mhowar 35:b5e8650a7669 283 // if the char is S, set bounds to sleep
mhowar 35:b5e8650a7669 284 } else if (a == 'S') {
mhowar 35:b5e8650a7669 285 curr_mode = SLEEP;
mhowar 35:b5e8650a7669 286 upper_bound = 60;
chadnach1 45:ec9e76ccec6c 287 LRI = 1000;
mhowar 35:b5e8650a7669 288 lower_bound = 30;
mhowar 35:b5e8650a7669 289 pc.printf("MODE IS S\n");
mhowar 35:b5e8650a7669 290 // if the char is E, set bounds to exercise
mhowar 35:b5e8650a7669 291 } else if (a == 'E') {
mhowar 35:b5e8650a7669 292 curr_mode = EXERCISE;
mhowar 35:b5e8650a7669 293 upper_bound = 175;
chadnach1 45:ec9e76ccec6c 294 LRI = 343;
mhowar 35:b5e8650a7669 295 lower_bound = 100;
mhowar 35:b5e8650a7669 296 pc.printf("MODE IS E\n");
mhowar 35:b5e8650a7669 297 // if the char is M, set to manual
mhowar 35:b5e8650a7669 298 } else if (a == 'M') {
mhowar 35:b5e8650a7669 299 curr_mode = MANUAL;
mhowar 35:b5e8650a7669 300 upper_bound = 175;
chadnach1 43:f28bc869321e 301 LRI = 1750;
mhowar 35:b5e8650a7669 302 lower_bound = 30;
mhowar 35:b5e8650a7669 303 pc.printf("MODE IS MANUAL\n");
mhowar 35:b5e8650a7669 304 // check for A if mode is manual
mhowar 35:b5e8650a7669 305 } else if (a == 'A') {
mhowar 35:b5e8650a7669 306 if(curr_mode == MANUAL) {
chadnach1 43:f28bc869321e 307 pc.printf("MODE IS MANUAL SENT APACE\n");
chadnach1 39:51c4ec84f6fd 308 apace();
mhowar 35:b5e8650a7669 309 }
mhowar 35:b5e8650a7669 310 // check for V is mode is manual
mhowar 35:b5e8650a7669 311 } else if (a == 'V') {
mhowar 35:b5e8650a7669 312 if(curr_mode == MANUAL) {
chadnach1 43:f28bc869321e 313 pc.printf("MODE IS MANUAL SENT VPACE\n");
chadnach1 39:51c4ec84f6fd 314 vpace();
mhowar 35:b5e8650a7669 315 }
mhowar 35:b5e8650a7669 316 } else if (a == 'O') {
mhowar 35:b5e8650a7669 317 keyboard_needs_numeric = 1;
mhowar 35:b5e8650a7669 318 } else {
mhowar 35:b5e8650a7669 319 // do nothing for invalid char
mhowar 35:b5e8650a7669 320 }
chadnach1 31:7f0e865e3d4b 321 }
chadnach1 31:7f0e865e3d4b 322 }
chadnach1 31:7f0e865e3d4b 323 }