
the complete model of the heart model
Dependencies: TextLCD mbed-rtos mbed
Fork of heart by
Diff: main.cpp
- Revision:
- 4:453a2f967f52
- Parent:
- 3:5941bb3c4fc2
--- a/main.cpp Wed Nov 30 04:16:42 2016 +0000 +++ b/main.cpp Wed Nov 30 05:39:08 2016 +0000 @@ -12,10 +12,13 @@ DigitalOut natVPace(LED2); DigitalOut aPace(LED3); DigitalOut vPace(LED4); -DigitalOut alarm(p5); //pin for alarm - DigitalOut a_signal(p6); //connected to pm DigitalOut v_signal(p7); //connected to pm +DigitalIn apace_signal(p8); //connected to pm +DigitalIn vpace_signal(p9); //connected to pm + +Timer atimer; //timer for atrial event +Timer vtimer; //timer for ventricular event unsigned int a_time = 0; unsigned int v_time = 0; @@ -23,12 +26,18 @@ int mode = 0; //0 = random, 1 = manual, 2 = test int modeInManul = -1; //0 = manualmode apace, 1 = manualmode vpace bool modeSwitch = false; -//constants -const int minwait_A = 100; -const int minwait_V = 200; +//the following flags indicate pacing signals either from the heart or the pacemaker +bool natural_apace = false; +bool natural_vpace = false; +bool apace = false; +bool vpace = false; +//constants +const int minwait_A = 100; //in miliseconds +const int minwait_V = 200; //in miliseconds /* +//so far no use of the following in the heart model const int LRI = 1000; const int VRP = 400; const int PVARP = 500; @@ -48,21 +57,6 @@ } } - -void received_apace() { - //TODO: DO - aPace = 1; - -} - -void received_vpace() { - //TODO: DO - vPace = 1; - -} - - - void send_signal(int type) { //type=0 a_signal, type=1 v_signal switch(type) { @@ -71,10 +65,6 @@ case 1: v_signal = 1; } - //TODO: Determine if time is right (is this supposed to be minWait?) - wait(1); - //a_sense = 0; - //v_sense = 0; } void modeSwitchDelay(){ @@ -125,30 +115,30 @@ // Blink functions gets called when there is an Atrial/V entricular event. void natApaceBlink(){ natAPace = 1; - Thread::wait(500); + Thread::wait(50); natAPace = 0; - Thread::wait(500); + Thread::wait(50); } void natVpaceBlink(){ natVPace = 1; - Thread::wait(500); + Thread::wait(50); natVPace = 0; - Thread::wait(500); + Thread::wait(50); } void ApaceBlink(){ aPace = 1; - Thread::wait(500); + Thread::wait(50); aPace = 0; - Thread::wait(500); + Thread::wait(50); } void VpaceBlink(){ vPace = 1; - Thread::wait(500); + Thread::wait(50); vPace = 0; - Thread::wait(500); + Thread::wait(50); } void performModeDelay(){ @@ -158,11 +148,81 @@ } } +//generate the time the next atrial event will happen +int Atrial_generate(){ + srand(time(NULL)); + return rand()%2000 + minwait_A; +} + +//generate the time the next ventricular event will happen. +int Ventricular_generate(){ + srand(time(NULL)); + return rand()%2000 + minwait_V; +} + //The following two modules random_sensing and random_pacing are two components of the random mode. void random_sensing(){ + int aTime = 0; //the time that the next atrial event will happen, + //if the time exceeds the expected limit, then it will be regarded as heart malfunction + //and the signal from the pacemaker will take effect. + int vTime = 0; //the time that the next ventricular event will happen, + //if the time exceeds the expected limit, then it will be regarded as heart malfunction + //and the signal from the pacemaker will take effect. while(1){ if(mode == 0){ performModeDelay(); + //use random functions to generate the sensing signals. + //asense time generate + //detect if there is an apace signal from the pacemaker, if there is, reset asense clock + //otherwise natural apace + //and go into vsense. + aTime = Atrial_generate(); + atimer.start(); + while(1){ + //the case when the hearth functions correctly + //this condition means the moment when vtimer passes aTime, the limit, and we still + // did not receive apace_signal, then, it means the heart is working correctly. + if(atimer.read_ms() >= aTime && !apace_signal){ + atimer.stop(); + atimer.reset(); + send_signal(0); + natural_apace = true; + break; + } + + //the case when the heart malfunctions in the Atrial + if((atimer.read_ms () <= aTime) && apace_signal){ + atimer.stop(); + atimer.reset(); + apace = true; + break; + } + } + + //detect if there is a vpace signal from the pacemaker, if there is, reset the vsense clock. + //otherwise natural vpace + vTime = Ventricular_generate(); + vtimer.start(); + while(1){ + //the case when the hearth functions correctly + //this condition means the moment when vtimer passes vTime, the limit, and we still + // did not receive vpace_signal, then, it means the heart is working correctly. + if(vtimer.read_ms() >= vTime && !vpace_signal){ + vtimer.stop(); + vtimer.reset(); + send_signal(1); + natural_vpace = true; + break; + } + + //the case when the heart malfunctions in the Atrial + if((vtimer.read_ms () <= vTime) && vpace_signal){ + vtimer.stop(); + vtimer.reset(); + vpace = true; + break; + } + } } } } @@ -171,6 +231,25 @@ while(1){ if(mode == 0){ performModeDelay(); + if(natural_apace){ + natApaceBlink(); + natural_apace = false; + } + + if(natural_vpace){ + natVpaceBlink(); + natural_vpace = false; + } + + if(apace){ + ApaceBlink(); + apace = false; + } + + if(vpace){ + VpaceBlink(); + vpace = false; + } } } }