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