CIS441 Controller

Dependencies:   TextLCD mbed-rtos mbed

Fork of PacemakerController by Chad Nachiappan

Committer:
chadnach1
Date:
Wed Dec 02 01:26:27 2015 +0000
Revision:
30:97db5a684eaa
Parent:
29:5cec671cb80d
Child:
31:7f0e865e3d4b
Changed a bunch in PacemakerController

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