monitor thread, own random heart
Dependencies: TextLCD mbed-rtos mbed
Fork of pacemaker_v2 by
main.cpp@0:cd6f505d57da, 2014-12-02 (annotated)
- Committer:
- jfields
- Date:
- Tue Dec 02 22:01:20 2014 +0000
- Revision:
- 0:cd6f505d57da
- Child:
- 1:9d463bc2b7b9
updated pacemaker code (more accurate than v1)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jfields | 0:cd6f505d57da | 1 | #include "mbed.h" |
jfields | 0:cd6f505d57da | 2 | #include "rtos.h" |
jfields | 0:cd6f505d57da | 3 | #include "TextLCD.h" |
jfields | 0:cd6f505d57da | 4 | #include <stdio.h> |
jfields | 0:cd6f505d57da | 5 | #include <stdlib.h> |
jfields | 0:cd6f505d57da | 6 | |
jfields | 0:cd6f505d57da | 7 | #define RUN 0x1 |
jfields | 0:cd6f505d57da | 8 | |
jfields | 0:cd6f505d57da | 9 | TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); |
jfields | 0:cd6f505d57da | 10 | Serial pc (USBTX, USBRX); |
jfields | 0:cd6f505d57da | 11 | |
jfields | 0:cd6f505d57da | 12 | // LEDs |
jfields | 0:cd6f505d57da | 13 | DigitalOut leds[] = {LED1, LED2, LED3, LED4}; // 1 = VP, 2 = AP, 3 = AS, 4 = VS |
jfields | 0:cd6f505d57da | 14 | |
jfields | 0:cd6f505d57da | 15 | // create log file and debugging vars |
jfields | 0:cd6f505d57da | 16 | //LocalFileSystem local("local"); |
jfields | 0:cd6f505d57da | 17 | //FILE *fp = fopen("/local/out.txt", "w"); |
jfields | 0:cd6f505d57da | 18 | int between_a = 0; |
jfields | 0:cd6f505d57da | 19 | int between_v = 0; |
jfields | 0:cd6f505d57da | 20 | |
jfields | 0:cd6f505d57da | 21 | // Heart Signals |
jfields | 0:cd6f505d57da | 22 | int AG = 0; |
jfields | 0:cd6f505d57da | 23 | int VG = 0; |
jfields | 0:cd6f505d57da | 24 | |
jfields | 0:cd6f505d57da | 25 | // Normal Values |
jfields | 0:cd6f505d57da | 26 | const int N_PVARP = 325; // ms |
jfields | 0:cd6f505d57da | 27 | const int N_VRP = 300; // ms |
jfields | 0:cd6f505d57da | 28 | const int N_LRI = 857; // ms (= about 70ppm) |
jfields | 0:cd6f505d57da | 29 | const int N_AVI = 65; // ms |
jfields | 0:cd6f505d57da | 30 | const int N_UB = 100; // 100ppm |
jfields | 0:cd6f505d57da | 31 | const int N_LB = 40; // 40ppm |
jfields | 0:cd6f505d57da | 32 | |
jfields | 0:cd6f505d57da | 33 | // Heart Values - Normal Mode is default |
jfields | 0:cd6f505d57da | 34 | int PVARP = N_PVARP; |
jfields | 0:cd6f505d57da | 35 | int VRP = N_VRP; |
jfields | 0:cd6f505d57da | 36 | int LRI = N_LRI; |
jfields | 0:cd6f505d57da | 37 | int default_LRI = N_LRI; |
jfields | 0:cd6f505d57da | 38 | int AVI = N_AVI; |
jfields | 0:cd6f505d57da | 39 | int UB = N_UB; |
jfields | 0:cd6f505d57da | 40 | int LB = N_LB; |
jfields | 0:cd6f505d57da | 41 | |
jfields | 0:cd6f505d57da | 42 | // time vars |
jfields | 0:cd6f505d57da | 43 | Timer global_t; |
jfields | 0:cd6f505d57da | 44 | int isVRP = 0; |
jfields | 0:cd6f505d57da | 45 | int isPVARP = 0; |
jfields | 0:cd6f505d57da | 46 | int waitingForV = 1; |
jfields | 0:cd6f505d57da | 47 | |
jfields | 0:cd6f505d57da | 48 | // functions |
jfields | 0:cd6f505d57da | 49 | void VP_func(void const *args); |
jfields | 0:cd6f505d57da | 50 | void AP_func(void const *args); |
jfields | 0:cd6f505d57da | 51 | void VS_func(void const *args); |
jfields | 0:cd6f505d57da | 52 | void AS_func(void const *args); |
jfields | 0:cd6f505d57da | 53 | void PM_monitor_func(void const *args); |
jfields | 0:cd6f505d57da | 54 | void manage_signals(void const *i); |
jfields | 0:cd6f505d57da | 55 | void flashLED(int i); |
jfields | 0:cd6f505d57da | 56 | void event_out(char *s, int between_t); |
jfields | 0:cd6f505d57da | 57 | void blind(); |
jfields | 0:cd6f505d57da | 58 | //void rand_heart_func(void const *args); |
jfields | 0:cd6f505d57da | 59 | |
jfields | 0:cd6f505d57da | 60 | // threads |
jfields | 0:cd6f505d57da | 61 | Thread * VS_thread; |
jfields | 0:cd6f505d57da | 62 | Thread * AS_thread; |
jfields | 0:cd6f505d57da | 63 | //Thread * rand_heart_thread; // just for testing until mbed connection is made |
jfields | 0:cd6f505d57da | 64 | |
jfields | 0:cd6f505d57da | 65 | // rtos timers |
jfields | 0:cd6f505d57da | 66 | RtosTimer * VP_timer; |
jfields | 0:cd6f505d57da | 67 | RtosTimer * AP_timer; |
jfields | 0:cd6f505d57da | 68 | RtosTimer * VRP_timer; |
jfields | 0:cd6f505d57da | 69 | RtosTimer * PVARP_timer; |
jfields | 0:cd6f505d57da | 70 | //RtosTimer * exit_timer; // for log file |
jfields | 0:cd6f505d57da | 71 | |
jfields | 0:cd6f505d57da | 72 | int main() { |
jfields | 0:cd6f505d57da | 73 | |
jfields | 0:cd6f505d57da | 74 | // start global timer |
jfields | 0:cd6f505d57da | 75 | global_t.start(); |
jfields | 0:cd6f505d57da | 76 | |
jfields | 0:cd6f505d57da | 77 | // init threads |
jfields | 0:cd6f505d57da | 78 | VS_thread = new Thread(VS_func); |
jfields | 0:cd6f505d57da | 79 | AS_thread = new Thread(AS_func); |
jfields | 0:cd6f505d57da | 80 | //rand_heart_thread = new Thread(rand_heart_func); // just for testing until mbed connection is made |
jfields | 0:cd6f505d57da | 81 | |
jfields | 0:cd6f505d57da | 82 | // init timers |
jfields | 0:cd6f505d57da | 83 | VP_timer = new RtosTimer(VP_func, osTimerOnce, (void *)0); |
jfields | 0:cd6f505d57da | 84 | AP_timer = new RtosTimer(AP_func, osTimerOnce, (void *)0); |
jfields | 0:cd6f505d57da | 85 | VRP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)1); |
jfields | 0:cd6f505d57da | 86 | PVARP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)2); |
jfields | 0:cd6f505d57da | 87 | //exit_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)99); |
jfields | 0:cd6f505d57da | 88 | |
jfields | 0:cd6f505d57da | 89 | // init global time, VP thread, exit thread |
jfields | 0:cd6f505d57da | 90 | VP_timer->start(AVI); |
jfields | 0:cd6f505d57da | 91 | //exit_timer->start(60000); |
jfields | 0:cd6f505d57da | 92 | |
jfields | 0:cd6f505d57da | 93 | // main thread |
jfields | 0:cd6f505d57da | 94 | while (1) { |
jfields | 0:cd6f505d57da | 95 | |
jfields | 0:cd6f505d57da | 96 | } |
jfields | 0:cd6f505d57da | 97 | } |
jfields | 0:cd6f505d57da | 98 | |
jfields | 0:cd6f505d57da | 99 | void manage_signals(void const *i) { |
jfields | 0:cd6f505d57da | 100 | if ((int)i==1) isVRP = 0; |
jfields | 0:cd6f505d57da | 101 | if ((int)i==2) isPVARP = 0; |
jfields | 0:cd6f505d57da | 102 | |
jfields | 0:cd6f505d57da | 103 | // for debuggging |
jfields | 0:cd6f505d57da | 104 | //if ((int)i==99) { |
jfields | 0:cd6f505d57da | 105 | //fclose(fp); |
jfields | 0:cd6f505d57da | 106 | //exit(1); |
jfields | 0:cd6f505d57da | 107 | //} |
jfields | 0:cd6f505d57da | 108 | } |
jfields | 0:cd6f505d57da | 109 | |
jfields | 0:cd6f505d57da | 110 | void AP_func(void const *args) { |
jfields | 0:cd6f505d57da | 111 | |
jfields | 0:cd6f505d57da | 112 | // start VP timer |
jfields | 0:cd6f505d57da | 113 | VP_timer->start(AVI); |
jfields | 0:cd6f505d57da | 114 | |
jfields | 0:cd6f505d57da | 115 | // update state |
jfields | 0:cd6f505d57da | 116 | waitingForV = 1; |
jfields | 0:cd6f505d57da | 117 | |
jfields | 0:cd6f505d57da | 118 | // output |
jfields | 0:cd6f505d57da | 119 | event_out("AP",between_a); |
jfields | 0:cd6f505d57da | 120 | between_a = global_t.read_ms(); |
jfields | 0:cd6f505d57da | 121 | |
jfields | 0:cd6f505d57da | 122 | // flash LED |
jfields | 0:cd6f505d57da | 123 | flashLED(2); |
jfields | 0:cd6f505d57da | 124 | } |
jfields | 0:cd6f505d57da | 125 | |
jfields | 0:cd6f505d57da | 126 | void VP_func(void const *args) { |
jfields | 0:cd6f505d57da | 127 | |
jfields | 0:cd6f505d57da | 128 | // start AP timer |
jfields | 0:cd6f505d57da | 129 | AP_timer->start(LRI-AVI); |
jfields | 0:cd6f505d57da | 130 | |
jfields | 0:cd6f505d57da | 131 | // update state |
jfields | 0:cd6f505d57da | 132 | waitingForV = 0; |
jfields | 0:cd6f505d57da | 133 | |
jfields | 0:cd6f505d57da | 134 | // set VRP, PVARP |
jfields | 0:cd6f505d57da | 135 | blind(); |
jfields | 0:cd6f505d57da | 136 | |
jfields | 0:cd6f505d57da | 137 | // output |
jfields | 0:cd6f505d57da | 138 | event_out("VP", between_v); |
jfields | 0:cd6f505d57da | 139 | between_v = global_t.read_ms(); |
jfields | 0:cd6f505d57da | 140 | |
jfields | 0:cd6f505d57da | 141 | // flash LED |
jfields | 0:cd6f505d57da | 142 | flashLED(1); |
jfields | 0:cd6f505d57da | 143 | } |
jfields | 0:cd6f505d57da | 144 | |
jfields | 0:cd6f505d57da | 145 | void AS_func(void const *args) { |
jfields | 0:cd6f505d57da | 146 | while (1) { |
jfields | 0:cd6f505d57da | 147 | |
jfields | 0:cd6f505d57da | 148 | // wait for event |
jfields | 0:cd6f505d57da | 149 | Thread::signal_wait(RUN,osWaitForever); |
jfields | 0:cd6f505d57da | 150 | |
jfields | 0:cd6f505d57da | 151 | // update state |
jfields | 0:cd6f505d57da | 152 | waitingForV = 1; |
jfields | 0:cd6f505d57da | 153 | |
jfields | 0:cd6f505d57da | 154 | // stop AP timer and start VP timer |
jfields | 0:cd6f505d57da | 155 | AP_timer->stop(); |
jfields | 0:cd6f505d57da | 156 | VP_timer->start(AVI); |
jfields | 0:cd6f505d57da | 157 | |
jfields | 0:cd6f505d57da | 158 | // output |
jfields | 0:cd6f505d57da | 159 | event_out("AS", between_a); |
jfields | 0:cd6f505d57da | 160 | between_a = global_t.read_ms(); |
jfields | 0:cd6f505d57da | 161 | |
jfields | 0:cd6f505d57da | 162 | // flash LED |
jfields | 0:cd6f505d57da | 163 | flashLED(3); |
jfields | 0:cd6f505d57da | 164 | } |
jfields | 0:cd6f505d57da | 165 | } |
jfields | 0:cd6f505d57da | 166 | |
jfields | 0:cd6f505d57da | 167 | void VS_func(void const *args) { |
jfields | 0:cd6f505d57da | 168 | while (1) { |
jfields | 0:cd6f505d57da | 169 | |
jfields | 0:cd6f505d57da | 170 | // wait for event |
jfields | 0:cd6f505d57da | 171 | Thread::signal_wait(RUN,osWaitForever); |
jfields | 0:cd6f505d57da | 172 | |
jfields | 0:cd6f505d57da | 173 | // update state |
jfields | 0:cd6f505d57da | 174 | waitingForV = 0; |
jfields | 0:cd6f505d57da | 175 | |
jfields | 0:cd6f505d57da | 176 | // stop VP timer and start AP timer |
jfields | 0:cd6f505d57da | 177 | VP_timer->stop(); |
jfields | 0:cd6f505d57da | 178 | AP_timer->start(LRI-AVI); |
jfields | 0:cd6f505d57da | 179 | |
jfields | 0:cd6f505d57da | 180 | // set VRP, PVARP |
jfields | 0:cd6f505d57da | 181 | blind(); |
jfields | 0:cd6f505d57da | 182 | |
jfields | 0:cd6f505d57da | 183 | // output |
jfields | 0:cd6f505d57da | 184 | event_out("VS", between_v); |
jfields | 0:cd6f505d57da | 185 | between_v = global_t.read_ms(); |
jfields | 0:cd6f505d57da | 186 | |
jfields | 0:cd6f505d57da | 187 | // flash LED |
jfields | 0:cd6f505d57da | 188 | flashLED(4); |
jfields | 0:cd6f505d57da | 189 | } |
jfields | 0:cd6f505d57da | 190 | } |
jfields | 0:cd6f505d57da | 191 | |
jfields | 0:cd6f505d57da | 192 | /* |
jfields | 0:cd6f505d57da | 193 | void rand_heart_func(void const *args) { |
jfields | 0:cd6f505d57da | 194 | int interval; |
jfields | 0:cd6f505d57da | 195 | srand(time(NULL)); |
jfields | 0:cd6f505d57da | 196 | while (1) { |
jfields | 0:cd6f505d57da | 197 | interval = rand()%5000+10; |
jfields | 0:cd6f505d57da | 198 | fprintf(fp,"interval = %d\n",interval); |
jfields | 0:cd6f505d57da | 199 | Thread::wait(interval); |
jfields | 0:cd6f505d57da | 200 | if (interval%2) AG = 1; |
jfields | 0:cd6f505d57da | 201 | else |
jfields | 0:cd6f505d57da | 202 | VG = 1; |
jfields | 0:cd6f505d57da | 203 | } |
jfields | 0:cd6f505d57da | 204 | } |
jfields | 0:cd6f505d57da | 205 | */ |
jfields | 0:cd6f505d57da | 206 | |
jfields | 0:cd6f505d57da | 207 | void PM_monitor_func(void const *args) { |
jfields | 0:cd6f505d57da | 208 | while (1) { |
jfields | 0:cd6f505d57da | 209 | if (AG == 1) { |
jfields | 0:cd6f505d57da | 210 | //fprintf(fp,"%f\tAget\t%d\n",global_t.read_ms(),isPVARP); |
jfields | 0:cd6f505d57da | 211 | AG = 0; |
jfields | 0:cd6f505d57da | 212 | if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN); |
jfields | 0:cd6f505d57da | 213 | } |
jfields | 0:cd6f505d57da | 214 | if (VG == 1) { |
jfields | 0:cd6f505d57da | 215 | //fprintf(fp,"%f\tVget\t%d\n",global_t.read_ms(),isVRP); |
jfields | 0:cd6f505d57da | 216 | VG = 0; |
jfields | 0:cd6f505d57da | 217 | if (!isVRP && waitingForV) VS_thread->signal_set(RUN); |
jfields | 0:cd6f505d57da | 218 | } |
jfields | 0:cd6f505d57da | 219 | } |
jfields | 0:cd6f505d57da | 220 | } |
jfields | 0:cd6f505d57da | 221 | |
jfields | 0:cd6f505d57da | 222 | void flashLED(int i) { |
jfields | 0:cd6f505d57da | 223 | leds[i-1] = 1; |
jfields | 0:cd6f505d57da | 224 | wait(0.01); |
jfields | 0:cd6f505d57da | 225 | leds[i-1] = 0; |
jfields | 0:cd6f505d57da | 226 | } |
jfields | 0:cd6f505d57da | 227 | |
jfields | 0:cd6f505d57da | 228 | void event_out(char *s, int between_t) { |
jfields | 0:cd6f505d57da | 229 | lcd.printf("%d\t%s\t%d\n",global_t.read_ms(),s,global_t.read_ms()-between_t); |
jfields | 0:cd6f505d57da | 230 | //fprintf(fp, "%f\t%s\t%f\n",global_t.read_ms(),s,global_t.read_ms()-between_t); |
jfields | 0:cd6f505d57da | 231 | } |
jfields | 0:cd6f505d57da | 232 | |
jfields | 0:cd6f505d57da | 233 | void blind() { |
jfields | 0:cd6f505d57da | 234 | isVRP = 1; |
jfields | 0:cd6f505d57da | 235 | isPVARP = 1; |
jfields | 0:cd6f505d57da | 236 | VRP_timer->start(VRP); |
jfields | 0:cd6f505d57da | 237 | PVARP_timer->start(PVARP); |
jfields | 0:cd6f505d57da | 238 | } |