Part of the Pacemaker Project; this models the Pacemaker.

Dependencies:   mbed TextLCD mbed-rtos

Committer:
lucastai
Date:
Mon Nov 30 20:53:55 2015 +0000
Revision:
13:2a25dd47478e
fix conflicts

Who changed what in which revision?

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