CIS441 Controller

Dependencies:   TextLCD mbed-rtos mbed

Fork of PacemakerController by Chad Nachiappan

Committer:
chadnach1
Date:
Wed Dec 02 02:23:26 2015 +0000
Revision:
32:c58b6651336c
Parent:
31:7f0e865e3d4b
Child:
33:b68c1648206e
only getting invalid states

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