CIS441 Controller

Dependencies:   TextLCD mbed-rtos mbed

Fork of PacemakerController by Chad Nachiappan

Committer:
amiche
Date:
Mon Nov 30 03:10:45 2015 +0000
Revision:
3:44d132582373
Parent:
2:5e9c4d83d038
Child:
4:f04eb7f96f4b
Child:
5:9eee15818b0e
Test commit to group, start pacemaker timing logic

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
lucastai 2:5e9c4d83d038 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
chadnach1 0:d92e0b71c6b5 14
chadnach1 0:d92e0b71c6b5 15 int h_clock;
amiche 3:44d132582373 16 int pm_clock;
amiche 3:44d132582373 17 int avi_clock;
chadnach1 0:d92e0b71c6b5 18
amiche 3:44d132582373 19 int LRI = 1000
amiche 3:44d132582373 20 int AVI = 150
amiche 3:44d132582373 21 int PVARP = 300
amiche 3:44d132582373 22 int VRP = 200
lucastai 2:5e9c4d83d038 23
lucastai 2:5e9c4d83d038 24 // constants
lucastai 2:5e9c4d83d038 25 int MAX_PM_RT = 180;
lucastai 2:5e9c4d83d038 26 int MIN_PM_RT = 40;
lucastai 2:5e9c4d83d038 27 enum mode {NORMAL, SLEEP, EXERCISE, MANUAL};
lucastai 2:5e9c4d83d038 28
lucastai 2:5e9c4d83d038 29 // counters
lucastai 2:5e9c4d83d038 30 int beats = 0;
lucastai 2:5e9c4d83d038 31
lucastai 2:5e9c4d83d038 32 // state variables
lucastai 2:5e9c4d83d038 33 int upper_bound = 100;
lucastai 2:5e9c4d83d038 34 int lower_bound = 40;
lucastai 2:5e9c4d83d038 35 int obs_int = 10;
lucastai 2:5e9c4d83d038 36 mode curr_mode = NORMAL;
lucastai 2:5e9c4d83d038 37
lucastai 2:5e9c4d83d038 38
lucastai 2:5e9c4d83d038 39
lucastai 2:5e9c4d83d038 40 // alarms
lucastai 2:5e9c4d83d038 41 DigitalOut Apace(LED1);
lucastai 2:5e9c4d83d038 42 DigitalOut Vpace(LED2);
lucastai 2:5e9c4d83d038 43 DigitalOut Asense(LED3);
lucastai 2:5e9c4d83d038 44 DigitalOut Vsense(LED4);
lucastai 2:5e9c4d83d038 45
lucastai 2:5e9c4d83d038 46 // hardware interrupt handler, adapted from code in piazza post by Dagaen
lucastai 2:5e9c4d83d038 47 extern "C" void TIMER0_IRQHandler (void)
lucastai 2:5e9c4d83d038 48 {
lucastai 2:5e9c4d83d038 49 if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
lucastai 2:5e9c4d83d038 50 {
lucastai 2:5e9c4d83d038 51 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
lucastai 2:5e9c4d83d038 52 timer_count++; //increment timer_count
lucastai 2:5e9c4d83d038 53 }
lucastai 2:5e9c4d83d038 54 }
lucastai 2:5e9c4d83d038 55
lucastai 2:5e9c4d83d038 56 // init the hardware interrupt (timer0), adapted same as above
lucastai 2:5e9c4d83d038 57 void timer0_init(void)
lucastai 2:5e9c4d83d038 58 {
lucastai 2:5e9c4d83d038 59 LPC_SC->PCONP |=1<1; //timer0 power on
lucastai 2:5e9c4d83d038 60 LPC_SC-> PCLKSEL0 |= 1 << 2; // set timer clock to CCLCK nondivided (1 clock cycle = 1 increment)
lucastai 2:5e9c4d83d038 61 LPC_TIM0->MR0 = 1000000; //100mhz clock cycle, 1 cycle = 10ns, 10ms = 10 000 000 ns = 1M cycles
lucastai 2:5e9c4d83d038 62 LPC_TIM0->MCR = 3; //interrupt and reset control
lucastai 2:5e9c4d83d038 63 //3 = Interrupt & reset timer0 on match (111) sets all three bits
lucastai 2:5e9c4d83d038 64 NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt
chadnach1 0:d92e0b71c6b5 65 }
chadnach1 0:d92e0b71c6b5 66
lucastai 2:5e9c4d83d038 67
lucastai 2:5e9c4d83d038 68 void PM_ALARM(void const *argument){
lucastai 2:5e9c4d83d038 69
lucastai 2:5e9c4d83d038 70 // min hr alarm
lucastai 2:5e9c4d83d038 71 if( beats < MIN_PM_RT){
lucastai 2:5e9c4d83d038 72 lcd.locate(0,1);
lucastai 2:5e9c4d83d038 73 lcd.printf("!<");
lucastai 2:5e9c4d83d038 74 }
lucastai 2:5e9c4d83d038 75
lucastai 2:5e9c4d83d038 76 // max hr alarm
lucastai 2:5e9c4d83d038 77 if(beats > MAX_PM_RT){
lucastai 2:5e9c4d83d038 78 lcd.locate(0,1);
lucastai 2:5e9c4d83d038 79 lcd.printf("!>");
lucastai 2:5e9c4d83d038 80 }
chadnach1 0:d92e0b71c6b5 81
chadnach1 0:d92e0b71c6b5 82 }
chadnach1 0:d92e0b71c6b5 83
lucastai 2:5e9c4d83d038 84 // hw interrupt callback, deal with the keyboard input from PC
lucastai 2:5e9c4d83d038 85 void MODE_SWITCH() {
lucastai 2:5e9c4d83d038 86
lucastai 2:5e9c4d83d038 87 // get the char, put it on the PC command line
lucastai 2:5e9c4d83d038 88 char a = pc.getc();
lucastai 2:5e9c4d83d038 89
lucastai 2:5e9c4d83d038 90 // if the char is N, update bounds to normal mode
lucastai 2:5e9c4d83d038 91 if(a == 'N'){
lucastai 2:5e9c4d83d038 92 curr_mode = NORMAL;
lucastai 2:5e9c4d83d038 93 upper_bound = 100;
lucastai 2:5e9c4d83d038 94 lower_bound = 40;
lucastai 2:5e9c4d83d038 95 pc.printf("MODE IS N\n");
lucastai 2:5e9c4d83d038 96 // if the char is S, set bounds to sleep
lucastai 2:5e9c4d83d038 97 }else if (a == 'S'){
lucastai 2:5e9c4d83d038 98 curr_mode = SLEEP;
lucastai 2:5e9c4d83d038 99 upper_bound = 60;
lucastai 2:5e9c4d83d038 100 lower_bound = 30;
lucastai 2:5e9c4d83d038 101 pc.printf("MODE IS S\n");
lucastai 2:5e9c4d83d038 102 // if the char is E, set bounds to exercise
lucastai 2:5e9c4d83d038 103 }else if (a == 'E'){
lucastai 2:5e9c4d83d038 104 curr_mode = EXERCISE;
lucastai 2:5e9c4d83d038 105 upper_bound = 175;
lucastai 2:5e9c4d83d038 106 lower_bound = 100;
lucastai 2:5e9c4d83d038 107 pc.printf("MODE IS E\n");
lucastai 2:5e9c4d83d038 108 beats = 2;
lucastai 2:5e9c4d83d038 109 // if the char is M, set to manual
lucastai 2:5e9c4d83d038 110 }else if (a == 'M'){
lucastai 2:5e9c4d83d038 111 curr_mode = MANUAL;
lucastai 2:5e9c4d83d038 112 upper_bound = 175;
lucastai 2:5e9c4d83d038 113 lower_bound = 30;
lucastai 2:5e9c4d83d038 114 beats = 300;
lucastai 2:5e9c4d83d038 115 pc.printf("MODE IS MANUAL\n");
lucastai 2:5e9c4d83d038 116 // check for A if mode is manual
lucastai 2:5e9c4d83d038 117 }else if (a == 'A'){
lucastai 2:5e9c4d83d038 118 if(curr_mode == MANUAL){
lucastai 2:5e9c4d83d038 119 pc.printf("MODE IS MANUAL GOT APACE\n");
lucastai 2:5e9c4d83d038 120 }
lucastai 2:5e9c4d83d038 121 // check for V is mode is manual
lucastai 2:5e9c4d83d038 122 }else if (a == 'V'){
lucastai 2:5e9c4d83d038 123 if(curr_mode == MANUAL){
lucastai 2:5e9c4d83d038 124 pc.printf("MODE IS MANUAL GOT VPACE\n");
lucastai 2:5e9c4d83d038 125 }
lucastai 2:5e9c4d83d038 126 }else{
lucastai 2:5e9c4d83d038 127 // do nothing for invalid char
lucastai 2:5e9c4d83d038 128 }
lucastai 2:5e9c4d83d038 129
lucastai 2:5e9c4d83d038 130
lucastai 2:5e9c4d83d038 131 }
lucastai 2:5e9c4d83d038 132
amiche 3:44d132582373 133 void pm_sense() {
amiche 3:44d132582373 134 if (timer_count >= VRP) {
amiche 3:44d132582373 135 //wait for Vget vsense();
amiche 3:44d132582373 136 timer_count = 0;
amiche 3:44d132582373 137 } else
amiche 3:44d132582373 138 }
amiche 3:44d132582373 139
amiche 3:44d132582373 140 void pm_response() {
amiche 3:44d132582373 141 if (timer_count >= LRI-AVI) {
amiche 3:44d132582373 142 apace();
amiche 3:44d132582373 143 } else if (timer_count >= AVI) {
amiche 3:44d132582373 144 vsense();
amiche 3:44d132582373 145 }
amiche 3:44d132582373 146 }
lucastai 2:5e9c4d83d038 147
lucastai 2:5e9c4d83d038 148
lucastai 2:5e9c4d83d038 149 void apace() {
amiche 3:44d132582373 150 make pin 1
lucastai 2:5e9c4d83d038 151 }
lucastai 2:5e9c4d83d038 152
lucastai 2:5e9c4d83d038 153 void vpace() {
amiche 3:44d132582373 154 make pin 1
chadnach1 0:d92e0b71c6b5 155 }
chadnach1 0:d92e0b71c6b5 156
amiche 3:44d132582373 157
chadnach1 0:d92e0b71c6b5 158 int main() {
lucastai 2:5e9c4d83d038 159 // connect the serial device (PC keybd) to the interrupt
lucastai 2:5e9c4d83d038 160 pc.attach(&MODE_SWITCH);
lucastai 2:5e9c4d83d038 161
amiche 3:44d132582373 162 Thread t3(pm_sense);
amiche 3:44d132582373 163 Thread t4(pm_response);
amiche 3:44d132582373 164
amiche 3:44d132582373 165 Thread t2(PM_ALARM);
amiche 3:44d132582373 166 Thread t5();
amiche 3:44d132582373 167
chadnach1 0:d92e0b71c6b5 168 }