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