monitor thread, own random heart

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v2 by Pacemaker

Committer:
jfields
Date:
Tue Dec 02 22:20:46 2014 +0000
Revision:
1:9d463bc2b7b9
Parent:
0:cd6f505d57da
random heart, monitor thread included

Who changed what in which revision?

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