CIS441 Controller

Dependencies:   TextLCD mbed-rtos mbed

Fork of PacemakerController by Chad Nachiappan

Committer:
amiche
Date:
Mon Nov 30 21:14:51 2015 +0000
Revision:
19:f3615321b5c4
Parent:
18:d4cd9d12345e
Child:
20:dc272bfaa276
Fix AVI logic in pm_response

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
amiche 19:f3615321b5c4 8
chadnach1 0:d92e0b71c6b5 9 // This is for the pacemaker
lucastai 2:5e9c4d83d038 10 volatile unsigned short timer_count;
lucastai 2:5e9c4d83d038 11 Serial pc(USBTX, USBRX);
lucastai 2:5e9c4d83d038 12 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7
lucastai 2:5e9c4d83d038 13
mhowar 5:9eee15818b0e 14 int keyboard_needs_numeric = 0; // boolean - is in middle of interval input?
chadnach1 0:d92e0b71c6b5 15
chadnach1 0:d92e0b71c6b5 16 int h_clock;
amiche 3:44d132582373 17 int pm_clock;
amiche 19:f3615321b5c4 18 Timer avi_clock;
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
lucastai 18:d4cd9d12345e 45 DigitalOut apace(p22):
amiche 19:f3615321b5c4 46 DigitalOut vpace(p21):
lucastai 18:d4cd9d12345e 47
lucastai 2:5e9c4d83d038 48 // hardware interrupt handler, adapted from code in piazza post by Dagaen
amiche 19:f3615321b5c4 49 extern "C" void TIMER0_IRQHandler (void)
lucastai 2:5e9c4d83d038 50 {
amiche 19:f3615321b5c4 51 if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
amiche 19:f3615321b5c4 52 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
amiche 19:f3615321b5c4 53 timer_count++; //increment timer_count
lucastai 2:5e9c4d83d038 54 }
lucastai 2:5e9c4d83d038 55 }
amiche 19:f3615321b5c4 56
lucastai 2:5e9c4d83d038 57 // init the hardware interrupt (timer0), adapted same as above
lucastai 2:5e9c4d83d038 58 void timer0_init(void)
lucastai 2:5e9c4d83d038 59 {
lucastai 2:5e9c4d83d038 60 LPC_SC->PCONP |=1<1; //timer0 power on
lucastai 2:5e9c4d83d038 61 LPC_SC-> PCLKSEL0 |= 1 << 2; // set timer clock to CCLCK nondivided (1 clock cycle = 1 increment)
lucastai 2:5e9c4d83d038 62 LPC_TIM0->MR0 = 1000000; //100mhz clock cycle, 1 cycle = 10ns, 10ms = 10 000 000 ns = 1M cycles
lucastai 2:5e9c4d83d038 63 LPC_TIM0->MCR = 3; //interrupt and reset control
amiche 19:f3615321b5c4 64 //3 = Interrupt & reset timer0 on match (111) sets all three bits
lucastai 2:5e9c4d83d038 65 NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt
chadnach1 0:d92e0b71c6b5 66 }
chadnach1 0:d92e0b71c6b5 67
lucastai 2:5e9c4d83d038 68
amiche 19:f3615321b5c4 69 void PM_ALARM(void const *argument)
amiche 19:f3615321b5c4 70 {
amiche 19:f3615321b5c4 71
lucastai 2:5e9c4d83d038 72 // min hr alarm
amiche 19:f3615321b5c4 73 if( beats < MIN_PM_RT) {
lucastai 2:5e9c4d83d038 74 lcd.locate(0,1);
amiche 19:f3615321b5c4 75 lcd.printf("!<");
lucastai 2:5e9c4d83d038 76 }
amiche 19:f3615321b5c4 77
lucastai 2:5e9c4d83d038 78 // max hr alarm
amiche 19:f3615321b5c4 79 if(beats > MAX_PM_RT) {
lucastai 2:5e9c4d83d038 80 lcd.locate(0,1);
amiche 19:f3615321b5c4 81 lcd.printf("!>");
amiche 19:f3615321b5c4 82 }
amiche 19:f3615321b5c4 83
chadnach1 0:d92e0b71c6b5 84 }
chadnach1 0:d92e0b71c6b5 85
lucastai 2:5e9c4d83d038 86 // hw interrupt callback, deal with the keyboard input from PC
amiche 19:f3615321b5c4 87 void keyboard_handler()
amiche 19:f3615321b5c4 88 {
amiche 19:f3615321b5c4 89
lucastai 2:5e9c4d83d038 90 // get the char, put it on the PC command line
lucastai 2:5e9c4d83d038 91 char a = pc.getc();
lucastai 2:5e9c4d83d038 92
mhowar 5:9eee15818b0e 93 if (keyboard_needs_numeric) {
mhowar 5:9eee15818b0e 94 if (a >= '0' && a <= '9') {
mhowar 5:9eee15818b0e 95 // update observation interval
mhowar 9:fd8477284185 96 obs_int = (a - '0' + 1) * 5;
mhowar 5:9eee15818b0e 97 } else {
amiche 19:f3615321b5c4 98 pc.printf("Expected numeric key\n");
mhowar 5:9eee15818b0e 99 }
amiche 19:f3615321b5c4 100 } else if(a == 'N') {
mhowar 5:9eee15818b0e 101 // if the char is N, update bounds to normal mode
lucastai 2:5e9c4d83d038 102 curr_mode = NORMAL;
lucastai 2:5e9c4d83d038 103 upper_bound = 100;
lucastai 2:5e9c4d83d038 104 lower_bound = 40;
amiche 19:f3615321b5c4 105 pc.printf("MODE IS N\n");
amiche 19:f3615321b5c4 106 // if the char is S, set bounds to sleep
amiche 19:f3615321b5c4 107 } else if (a == 'S') {
lucastai 2:5e9c4d83d038 108 curr_mode = SLEEP;
lucastai 2:5e9c4d83d038 109 upper_bound = 60;
lucastai 2:5e9c4d83d038 110 lower_bound = 30;
amiche 19:f3615321b5c4 111 pc.printf("MODE IS S\n");
amiche 19:f3615321b5c4 112 // if the char is E, set bounds to exercise
amiche 19:f3615321b5c4 113 } else if (a == 'E') {
lucastai 2:5e9c4d83d038 114 curr_mode = EXERCISE;
lucastai 2:5e9c4d83d038 115 upper_bound = 175;
lucastai 2:5e9c4d83d038 116 lower_bound = 100;
amiche 19:f3615321b5c4 117 pc.printf("MODE IS E\n");
lucastai 2:5e9c4d83d038 118 beats = 2;
amiche 19:f3615321b5c4 119 // if the char is M, set to manual
amiche 19:f3615321b5c4 120 } else if (a == 'M') {
lucastai 2:5e9c4d83d038 121 curr_mode = MANUAL;
lucastai 2:5e9c4d83d038 122 upper_bound = 175;
lucastai 2:5e9c4d83d038 123 lower_bound = 30;
lucastai 2:5e9c4d83d038 124 beats = 300;
amiche 19:f3615321b5c4 125 pc.printf("MODE IS MANUAL\n");
amiche 19:f3615321b5c4 126 // check for A if mode is manual
amiche 19:f3615321b5c4 127 } else if (a == 'A') {
amiche 19:f3615321b5c4 128 if(curr_mode == MANUAL) {
amiche 19:f3615321b5c4 129 pc.printf("MODE IS MANUAL GOT APACE\n");
lucastai 2:5e9c4d83d038 130 }
amiche 19:f3615321b5c4 131 // check for V is mode is manual
amiche 19:f3615321b5c4 132 } else if (a == 'V') {
amiche 19:f3615321b5c4 133 if(curr_mode == MANUAL) {
amiche 19:f3615321b5c4 134 pc.printf("MODE IS MANUAL GOT VPACE\n");
amiche 19:f3615321b5c4 135 }
amiche 19:f3615321b5c4 136 } else if (a == 'O') {
mhowar 5:9eee15818b0e 137 keyboard_needs_numeric = 1;
amiche 19:f3615321b5c4 138 } else {
lucastai 2:5e9c4d83d038 139 // do nothing for invalid char
lucastai 2:5e9c4d83d038 140 }
amiche 19:f3615321b5c4 141
amiche 19:f3615321b5c4 142
lucastai 2:5e9c4d83d038 143 }
lucastai 2:5e9c4d83d038 144
amiche 19:f3615321b5c4 145 void pm_sense()
amiche 19:f3615321b5c4 146 {
amiche 19:f3615321b5c4 147
chadnach1 4:f04eb7f96f4b 148 while(1) {
amiche 19:f3615321b5c4 149
chadnach1 4:f04eb7f96f4b 150 if (timer_count >= VRP) {
amiche 19:f3615321b5c4 151 //wait for Vget;
amiche 19:f3615321b5c4 152 timer_count = 0;
amiche 19:f3615321b5c4 153 // do something with Vsense!
chadnach1 4:f04eb7f96f4b 154 } else if (timer_count < VRP) {
chadnach1 4:f04eb7f96f4b 155 // wait for Vget
chadnach1 4:f04eb7f96f4b 156 }
amiche 19:f3615321b5c4 157
chadnach1 4:f04eb7f96f4b 158 if (timer_count < PVARP) {
chadnach1 4:f04eb7f96f4b 159 // wait for Aget?
chadnach1 4:f04eb7f96f4b 160 } else if (timer_count >= PVARP) {
chadnach1 4:f04eb7f96f4b 161 // wait for Aget?
chadnach1 4:f04eb7f96f4b 162 // do something with Asense!
chadnach1 4:f04eb7f96f4b 163 }
amiche 19:f3615321b5c4 164
chadnach1 4:f04eb7f96f4b 165 }
amiche 3:44d132582373 166 }
amiche 3:44d132582373 167
amiche 19:f3615321b5c4 168 void pm_response()
amiche 19:f3615321b5c4 169 {
amiche 19:f3615321b5c4 170
chadnach1 4:f04eb7f96f4b 171 while(1) {
amiche 19:f3615321b5c4 172 if (timer_count >= LRI-AVI) {
amiche 19:f3615321b5c4 173
amiche 19:f3615321b5c4 174 // PM_A! sets the LED high
amiche 19:f3615321b5c4 175 led_apace = 1;
amiche 19:f3615321b5c4 176
amiche 19:f3615321b5c4 177 // avi_clk = 0
amiche 19:f3615321b5c4 178 avi_clk.reset();
amiche 19:f3615321b5c4 179
amiche 19:f3615321b5c4 180 apace();
amiche 19:f3615321b5c4 181 // At Atrial Event State
amiche 19:f3615321b5c4 182 while (avi_clk.read() < AVI);
amiche 19:f3615321b5c4 183
amiche 19:f3615321b5c4 184 // Ventricular Event
amiche 19:f3615321b5c4 185 timer_count = 0;
amiche 19:f3615321b5c4 186
amiche 19:f3615321b5c4 187 // PM_V! sets the LED high
amiche 19:f3615321b5c4 188 led_vpace = 1;
amiche 19:f3615321b5c4 189
amiche 19:f3615321b5c4 190 timer_count = 0;
amiche 19:f3615321b5c4 191 vpace();
amiche 19:f3615321b5c4 192 } else if (timer_count < LRI - AVI) {
amiche 19:f3615321b5c4 193
amiche 19:f3615321b5c4 194 // avi_clk = 0
amiche 19:f3615321b5c4 195 avi_clk.reset();
amiche 19:f3615321b5c4 196
amiche 19:f3615321b5c4 197 // wait for Asense? naaah
amiche 19:f3615321b5c4 198 }
amiche 19:f3615321b5c4 199 }
amiche 3:44d132582373 200 }
lucastai 2:5e9c4d83d038 201
amiche 19:f3615321b5c4 202 void apace()
amiche 19:f3615321b5c4 203 {
lucastai 2:5e9c4d83d038 204
lucastai 11:14b15f4604ad 205 apace = 1;
lucastai 14:a16b636c2bbc 206 led_apace = 1;
lucastai 11:14b15f4604ad 207 Thread::wait(10);
lucastai 11:14b15f4604ad 208 apace = 0;
lucastai 13:2a25dd47478e 209
lucastai 2:5e9c4d83d038 210 }
lucastai 2:5e9c4d83d038 211
amiche 19:f3615321b5c4 212 void vpace()
amiche 19:f3615321b5c4 213 {
amiche 19:f3615321b5c4 214
lucastai 11:14b15f4604ad 215 vpace = 1;
lucastai 14:a16b636c2bbc 216 led_vpace = 1;
lucastai 11:14b15f4604ad 217 Thread::wait(10);
lucastai 11:14b15f4604ad 218 vpace = 0;
amiche 19:f3615321b5c4 219
chadnach1 0:d92e0b71c6b5 220 }
chadnach1 0:d92e0b71c6b5 221
amiche 3:44d132582373 222
amiche 19:f3615321b5c4 223 int main()
amiche 19:f3615321b5c4 224 {
chadnach1 16:ae9df4194011 225 // https://developer.mbed.org/users/chadnach1/code/PacemakerController/
lucastai 2:5e9c4d83d038 226 // connect the serial device (PC keybd) to the interrupt
mhowar 5:9eee15818b0e 227 pc.attach(&keyboard_handler);
amiche 19:f3615321b5c4 228
chadnach1 16:ae9df4194011 229 // Start LED's Off
chadnach1 17:c89cddf30925 230 led_apace = 0;
chadnach1 17:c89cddf30925 231 led_vpace = 0;
amiche 19:f3615321b5c4 232
chadnach1 16:ae9df4194011 233 // Start the avi_clock
chadnach1 16:ae9df4194011 234 avi_clk.start();
amiche 19:f3615321b5c4 235
amiche 3:44d132582373 236 Thread t3(pm_sense);
amiche 3:44d132582373 237 Thread t4(pm_response);
amiche 3:44d132582373 238
amiche 3:44d132582373 239 Thread t2(PM_ALARM);
amiche 3:44d132582373 240 Thread t5();
amiche 3:44d132582373 241
chadnach1 17:c89cddf30925 242 }