Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
pacemaker.cpp@1:c6b87b3cc7af, 2013-11-25 (annotated)
- Committer:
- rohitdureja
- Date:
- Mon Nov 25 03:02:33 2013 +0000
- Revision:
- 1:c6b87b3cc7af
- Parent:
- main.cpp@0:2196b652345b
- Child:
- 2:f6066ec3c8bc
Working DDD Model
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rohitdureja | 0:2196b652345b | 1 | #include "mbed.h" |
rohitdureja | 0:2196b652345b | 2 | |
rohitdureja | 1:c6b87b3cc7af | 3 | //Enable serial communication |
rohitdureja | 1:c6b87b3cc7af | 4 | Serial pc(USBTX, USBRX); // tx, rx |
rohitdureja | 1:c6b87b3cc7af | 5 | |
rohitdureja | 1:c6b87b3cc7af | 6 | //Timers used to model the pacemaker |
rohitdureja | 1:c6b87b3cc7af | 7 | Timeout AVI; |
rohitdureja | 1:c6b87b3cc7af | 8 | Timeout PVARP; |
rohitdureja | 1:c6b87b3cc7af | 9 | Timeout LRI; |
rohitdureja | 1:c6b87b3cc7af | 10 | |
rohitdureja | 1:c6b87b3cc7af | 11 | //Pins used to communicate with the heart, P9 is AP, P10 is VP, P11 is AS and P12 is VS |
rohitdureja | 1:c6b87b3cc7af | 12 | DigitalOut pinAP(p9); |
rohitdureja | 1:c6b87b3cc7af | 13 | DigitalOut pinVP(p10); |
rohitdureja | 1:c6b87b3cc7af | 14 | InterruptIn pinAS(p11); |
rohitdureja | 1:c6b87b3cc7af | 15 | InterruptIn pinVS(p12); |
rohitdureja | 1:c6b87b3cc7af | 16 | |
rohitdureja | 1:c6b87b3cc7af | 17 | //Reference LEDs |
rohitdureja | 1:c6b87b3cc7af | 18 | DigitalOut vs(LED1); |
rohitdureja | 1:c6b87b3cc7af | 19 | DigitalOut vp(LED2); |
rohitdureja | 1:c6b87b3cc7af | 20 | DigitalOut as(LED3); |
rohitdureja | 1:c6b87b3cc7af | 21 | DigitalOut ap(LED4); |
rohitdureja | 1:c6b87b3cc7af | 22 | |
rohitdureja | 1:c6b87b3cc7af | 23 | //Pacemaker values in seconds; |
rohitdureja | 1:c6b87b3cc7af | 24 | float lri = 1.0; |
rohitdureja | 1:c6b87b3cc7af | 25 | float pvarp = 0.3; |
rohitdureja | 1:c6b87b3cc7af | 26 | float vrp = 0.2; |
rohitdureja | 1:c6b87b3cc7af | 27 | float pvab = 0.05; |
rohitdureja | 1:c6b87b3cc7af | 28 | float pavb = 0.05; |
rohitdureja | 1:c6b87b3cc7af | 29 | float avi = 0.15; |
rohitdureja | 1:c6b87b3cc7af | 30 | |
rohitdureja | 1:c6b87b3cc7af | 31 | //Initial states |
rohitdureja | 1:c6b87b3cc7af | 32 | int vsValid = 1; |
rohitdureja | 1:c6b87b3cc7af | 33 | int asValid = 1; |
rohitdureja | 1:c6b87b3cc7af | 34 | |
rohitdureja | 1:c6b87b3cc7af | 35 | //Interrupt function prototypes |
rohitdureja | 1:c6b87b3cc7af | 36 | void ASReceivedOrAPSent(); |
rohitdureja | 1:c6b87b3cc7af | 37 | void VSReceviedOrVPSent(); |
rohitdureja | 1:c6b87b3cc7af | 38 | void noVentricleEvent(); |
rohitdureja | 1:c6b87b3cc7af | 39 | void noAtrialEvent(); |
rohitdureja | 1:c6b87b3cc7af | 40 | void postPVARP(); |
rohitdureja | 1:c6b87b3cc7af | 41 | void blinkled(); |
rohitdureja | 1:c6b87b3cc7af | 42 | |
rohitdureja | 1:c6b87b3cc7af | 43 | //Interrupt routines for all pacemaker states |
rohitdureja | 1:c6b87b3cc7af | 44 | |
rohitdureja | 1:c6b87b3cc7af | 45 | //Interrupt routine to handle atrial event |
rohitdureja | 1:c6b87b3cc7af | 46 | void ASReceivedOrAPSent() { |
rohitdureja | 1:c6b87b3cc7af | 47 | if(asValid==1) { |
rohitdureja | 1:c6b87b3cc7af | 48 | /*as = 1; |
rohitdureja | 1:c6b87b3cc7af | 49 | wait(0.2); |
rohitdureja | 1:c6b87b3cc7af | 50 | as = 0;*/ |
rohitdureja | 1:c6b87b3cc7af | 51 | //Disable the LRI timer as a AS has been received |
rohitdureja | 1:c6b87b3cc7af | 52 | LRI.detach(); |
rohitdureja | 1:c6b87b3cc7af | 53 | //Disable the PVARP timer as a AS has been recieved |
rohitdureja | 1:c6b87b3cc7af | 54 | PVARP.detach(); |
rohitdureja | 1:c6b87b3cc7af | 55 | //Make asValid=0 and vsValid=0 |
rohitdureja | 1:c6b87b3cc7af | 56 | asValid = 0; |
rohitdureja | 1:c6b87b3cc7af | 57 | vsValid = 0; |
rohitdureja | 1:c6b87b3cc7af | 58 | //Set up timer to interrupt at AVI and call noVentricleEvent if no ventricular event comes |
rohitdureja | 1:c6b87b3cc7af | 59 | AVI.attach(&noVentricleEvent, avi); |
rohitdureja | 1:c6b87b3cc7af | 60 | //Wait for PAVB and disable all interrupts |
rohitdureja | 1:c6b87b3cc7af | 61 | __disable_irq(); |
rohitdureja | 1:c6b87b3cc7af | 62 | wait(pavb); |
rohitdureja | 1:c6b87b3cc7af | 63 | //After waiting for PAVB enable interrupts and make asValid=0 and vsValid=1 |
rohitdureja | 1:c6b87b3cc7af | 64 | __enable_irq(); |
rohitdureja | 1:c6b87b3cc7af | 65 | asValid = 0; |
rohitdureja | 1:c6b87b3cc7af | 66 | vsValid = 1; |
rohitdureja | 1:c6b87b3cc7af | 67 | } |
rohitdureja | 1:c6b87b3cc7af | 68 | } |
rohitdureja | 1:c6b87b3cc7af | 69 | |
rohitdureja | 1:c6b87b3cc7af | 70 | //Interrupt routine to handle atrial event |
rohitdureja | 1:c6b87b3cc7af | 71 | void VSReceivedOrVPSent() { |
rohitdureja | 1:c6b87b3cc7af | 72 | if(vsValid==1) { |
rohitdureja | 1:c6b87b3cc7af | 73 | /*vs = 1; |
rohitdureja | 1:c6b87b3cc7af | 74 | wait(0.2); |
rohitdureja | 1:c6b87b3cc7af | 75 | vs = 0;*/ |
rohitdureja | 1:c6b87b3cc7af | 76 | //Disable the AVI timer as VS has been recieved. |
rohitdureja | 1:c6b87b3cc7af | 77 | AVI.detach(); |
rohitdureja | 1:c6b87b3cc7af | 78 | asValid = 0; |
rohitdureja | 1:c6b87b3cc7af | 79 | vsValid = 0; |
rohitdureja | 1:c6b87b3cc7af | 80 | //Set up timer to interrupt at PVARP |
rohitdureja | 1:c6b87b3cc7af | 81 | PVARP.attach(&postPVARP, pvarp); |
rohitdureja | 1:c6b87b3cc7af | 82 | //Set up timer to interrupt at LRI-AVI |
rohitdureja | 1:c6b87b3cc7af | 83 | LRI.attach(&noAtrialEvent, lri-avi); |
rohitdureja | 1:c6b87b3cc7af | 84 | //Wait for VRP and disable all interrupts |
rohitdureja | 1:c6b87b3cc7af | 85 | __disable_irq(); |
rohitdureja | 1:c6b87b3cc7af | 86 | wait(vrp); |
rohitdureja | 1:c6b87b3cc7af | 87 | //After waiting for VRP enable interrupts make asValid = 0 and vsValid = 1 |
rohitdureja | 1:c6b87b3cc7af | 88 | __enable_irq(); |
rohitdureja | 1:c6b87b3cc7af | 89 | asValid = 0; |
rohitdureja | 1:c6b87b3cc7af | 90 | vsValid = 1; |
rohitdureja | 1:c6b87b3cc7af | 91 | } |
rohitdureja | 1:c6b87b3cc7af | 92 | } |
rohitdureja | 1:c6b87b3cc7af | 93 | |
rohitdureja | 1:c6b87b3cc7af | 94 | //Interrupt routine to handle post AVI scenario when no VS is receieved |
rohitdureja | 1:c6b87b3cc7af | 95 | void noVentricleEvent() { |
rohitdureja | 1:c6b87b3cc7af | 96 | //Pace heart now, ventricle! |
rohitdureja | 1:c6b87b3cc7af | 97 | pinVP = 1; |
rohitdureja | 1:c6b87b3cc7af | 98 | wait(0.01); |
rohitdureja | 1:c6b87b3cc7af | 99 | pinVP = 0; |
rohitdureja | 1:c6b87b3cc7af | 100 | //Call VSReceiedOrVPSent() |
rohitdureja | 1:c6b87b3cc7af | 101 | VSReceivedOrVPSent(); |
rohitdureja | 1:c6b87b3cc7af | 102 | } |
rohitdureja | 1:c6b87b3cc7af | 103 | |
rohitdureja | 1:c6b87b3cc7af | 104 | //Interrupt routine to hande scenario when no AS or VS is recevied till PVARP |
rohitdureja | 1:c6b87b3cc7af | 105 | void postPVARP() { |
rohitdureja | 1:c6b87b3cc7af | 106 | vsValid = 1; |
rohitdureja | 1:c6b87b3cc7af | 107 | asValid = 1; |
rohitdureja | 1:c6b87b3cc7af | 108 | } |
rohitdureja | 1:c6b87b3cc7af | 109 | |
rohitdureja | 1:c6b87b3cc7af | 110 | |
rohitdureja | 1:c6b87b3cc7af | 111 | //Interruot routine to handle scenarios when there is no Atrial event |
rohitdureja | 1:c6b87b3cc7af | 112 | void noAtrialEvent() { |
rohitdureja | 1:c6b87b3cc7af | 113 | //Pace heart now, atrial! |
rohitdureja | 1:c6b87b3cc7af | 114 | pinAP = 1; |
rohitdureja | 1:c6b87b3cc7af | 115 | wait(0.01); |
rohitdureja | 1:c6b87b3cc7af | 116 | pinAP = 0; |
rohitdureja | 1:c6b87b3cc7af | 117 | ASReceivedOrAPSent(); |
rohitdureja | 1:c6b87b3cc7af | 118 | } |
rohitdureja | 1:c6b87b3cc7af | 119 | |
rohitdureja | 1:c6b87b3cc7af | 120 | /*void blinkled() { |
rohitdureja | 1:c6b87b3cc7af | 121 | vs=1; |
rohitdureja | 1:c6b87b3cc7af | 122 | wait(0.2); |
rohitdureja | 1:c6b87b3cc7af | 123 | vs=0; |
rohitdureja | 1:c6b87b3cc7af | 124 | }*/ |
rohitdureja | 0:2196b652345b | 125 | |
rohitdureja | 0:2196b652345b | 126 | int main() { |
rohitdureja | 1:c6b87b3cc7af | 127 | //Pins used to communicate with the heart |
rohitdureja | 1:c6b87b3cc7af | 128 | //P11 is AS and P12 is VS |
rohitdureja | 1:c6b87b3cc7af | 129 | pinAS.rise(&ASReceivedOrAPSent); |
rohitdureja | 1:c6b87b3cc7af | 130 | pinVS.rise(&VSReceivedOrVPSent); |
rohitdureja | 1:c6b87b3cc7af | 131 | __enable_irq(); |
rohitdureja | 0:2196b652345b | 132 | while(1) { |
rohitdureja | 0:2196b652345b | 133 | } |
rohitdureja | 0:2196b652345b | 134 | } |