Example code to create a heartbeat sensor using the Si1146 on Silicon Lab's Biometrics expansion board for the Wonder Gecko
Dependencies: EFM32_SegmentLCD Si114x mbed
main.cpp@1:b2b2924c3c48, 2015-08-25 (annotated)
- Committer:
- Sissors
- Date:
- Tue Aug 25 19:30:13 2015 +0000
- Revision:
- 1:b2b2924c3c48
- Parent:
- 0:f3705ec14cf0
Improved heartbeat algorithmes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:f3705ec14cf0 | 1 | /* |
Sissors | 0:f3705ec14cf0 | 2 | Heart rate example for the SiLabs biometrics board using the Si1146 |
Sissors | 0:f3705ec14cf0 | 3 | |
Sissors | 0:f3705ec14cf0 | 4 | Since their own example code is all hidden behind precompiled libraries, here an |
Sissors | 0:f3705ec14cf0 | 5 | example which is actually useful :D. Take into account this is an example, I just screwed |
Sissors | 0:f3705ec14cf0 | 6 | around a bit and observed some output waveforms to make it work reasonable, there are |
Sissors | 0:f3705ec14cf0 | 7 | for sure better ways to implement this. |
Sissors | 0:f3705ec14cf0 | 8 | |
Sissors | 0:f3705ec14cf0 | 9 | User manual: |
Sissors | 0:f3705ec14cf0 | 10 | If the Gecko symbol turns on on the LCD it is connected to the sensor |
Sissors | 0:f3705ec14cf0 | 11 | Place your finger on the sensor. In the biometrics board manual it is explained how, |
Sissors | 0:f3705ec14cf0 | 12 | but you can also try until it works properly. You don't want to use the extreme tip of your |
Sissors | 0:f3705ec14cf0 | 13 | finger, but a bit further back. |
Sissors | 0:f3705ec14cf0 | 14 | The battery indicator will flash briefly when a beat is detected. |
Sissors | 0:f3705ec14cf0 | 15 | |
Sissors | 0:f3705ec14cf0 | 16 | Every beat it re-calculates your heartbeat using the time of the previous beat, since no |
Sissors | 0:f3705ec14cf0 | 17 | filtering happens here, it can jump around a bit, but this should be limitted. |
Sissors | 0:f3705ec14cf0 | 18 | |
Sissors | 0:f3705ec14cf0 | 19 | And just to be sure since we can never overestimate human stupidity: This is not a medical tool... |
Sissors | 0:f3705ec14cf0 | 20 | |
Sissors | 0:f3705ec14cf0 | 21 | |
Sissors | 0:f3705ec14cf0 | 22 | Todo: |
Sissors | 0:f3705ec14cf0 | 23 | 1. Get rid of my hardcoded value used as threshold |
Sissors | 0:f3705ec14cf0 | 24 | 2. Measure PO2 |
Sissors | 0:f3705ec14cf0 | 25 | */ |
Sissors | 0:f3705ec14cf0 | 26 | |
Sissors | 0:f3705ec14cf0 | 27 | |
Sissors | 0:f3705ec14cf0 | 28 | #include "mbed.h" |
Sissors | 0:f3705ec14cf0 | 29 | #include "Si114x.h" |
Sissors | 0:f3705ec14cf0 | 30 | #include "EFM32_SegmentLCD.h" |
Sissors | 0:f3705ec14cf0 | 31 | |
Sissors | 0:f3705ec14cf0 | 32 | |
Sissors | 0:f3705ec14cf0 | 33 | DigitalOut myled(LED1); |
Sissors | 0:f3705ec14cf0 | 34 | silabs::EFM32_SegmentLCD segmentDisplay; |
Sissors | 0:f3705ec14cf0 | 35 | |
Sissors | 0:f3705ec14cf0 | 36 | |
Sissors | 0:f3705ec14cf0 | 37 | int main() { |
Sissors | 0:f3705ec14cf0 | 38 | segmentDisplay.AllOff(); |
Sissors | 0:f3705ec14cf0 | 39 | Si114x sensor(PD6, PD7); |
Sissors | 0:f3705ec14cf0 | 40 | while(sensor.verifyConnection() != 1); |
Sissors | 0:f3705ec14cf0 | 41 | segmentDisplay.Symbol(LCD_SYMBOL_GECKO, 1); |
Sissors | 0:f3705ec14cf0 | 42 | |
Sissors | 0:f3705ec14cf0 | 43 | int curval; |
Sissors | 0:f3705ec14cf0 | 44 | int prevval = 0; |
Sissors | 0:f3705ec14cf0 | 45 | int mvingavg[3] = {0}; |
Sissors | 0:f3705ec14cf0 | 46 | int sum; |
Sissors | 0:f3705ec14cf0 | 47 | int prevtime = 0; |
Sissors | 0:f3705ec14cf0 | 48 | int timesincelast; |
Sissors | 1:b2b2924c3c48 | 49 | float abssum; |
Sissors | 1:b2b2924c3c48 | 50 | float abssum_iir = 0.0f; |
Sissors | 0:f3705ec14cf0 | 51 | |
Sissors | 0:f3705ec14cf0 | 52 | Timer timey; |
Sissors | 0:f3705ec14cf0 | 53 | timey.start(); |
Sissors | 0:f3705ec14cf0 | 54 | while(1) { |
Sissors | 0:f3705ec14cf0 | 55 | curval=sensor.getProximity(2); //We use visible light, seems to work alot better than IR |
Sissors | 0:f3705ec14cf0 | 56 | |
Sissors | 0:f3705ec14cf0 | 57 | //Three times moving average of the diff of the output |
Sissors | 0:f3705ec14cf0 | 58 | mvingavg[2] = mvingavg[1]; |
Sissors | 0:f3705ec14cf0 | 59 | mvingavg[1] = mvingavg[0]; |
Sissors | 0:f3705ec14cf0 | 60 | mvingavg[0] = curval - prevval; |
Sissors | 0:f3705ec14cf0 | 61 | sum = mvingavg[2] + mvingavg[1] + mvingavg[0]; |
Sissors | 0:f3705ec14cf0 | 62 | |
Sissors | 1:b2b2924c3c48 | 63 | //Float val for abs(sum) to calculate threshold dynamically |
Sissors | 1:b2b2924c3c48 | 64 | abssum = (float)abs(sum); |
Sissors | 1:b2b2924c3c48 | 65 | abssum_iir = abssum_iir * 0.99f + abssum * 0.01f; |
Sissors | 1:b2b2924c3c48 | 66 | |
Sissors | 1:b2b2924c3c48 | 67 | //Now we know the 'average' absolute 'noise' ( plus some part of the heartbeat) |
Sissors | 1:b2b2924c3c48 | 68 | //We multiply this by four (yes randomly chosen) and use it as threshold for heartbeat |
Sissors | 1:b2b2924c3c48 | 69 | //Possibly this is too much if you are really excited, but haven't been excited enough to check this |
Sissors | 1:b2b2924c3c48 | 70 | if (sum < (int)(-4.0f * abssum_iir)) { |
Sissors | 0:f3705ec14cf0 | 71 | segmentDisplay.Battery(4); |
Sissors | 0:f3705ec14cf0 | 72 | timesincelast = timey.read_ms() - prevtime; |
Sissors | 0:f3705ec14cf0 | 73 | if (timesincelast > 250) { |
Sissors | 0:f3705ec14cf0 | 74 | uint32_t hearthrate = 60000 / timesincelast; |
Sissors | 0:f3705ec14cf0 | 75 | segmentDisplay.Number(hearthrate); |
Sissors | 0:f3705ec14cf0 | 76 | prevtime = timey.read_ms(); |
Sissors | 0:f3705ec14cf0 | 77 | } |
Sissors | 0:f3705ec14cf0 | 78 | } |
Sissors | 0:f3705ec14cf0 | 79 | else { |
Sissors | 0:f3705ec14cf0 | 80 | segmentDisplay.Battery(0); |
Sissors | 0:f3705ec14cf0 | 81 | } |
Sissors | 0:f3705ec14cf0 | 82 | |
Sissors | 0:f3705ec14cf0 | 83 | prevval = curval; |
Sissors | 0:f3705ec14cf0 | 84 | |
Sissors | 0:f3705ec14cf0 | 85 | } |
Sissors | 0:f3705ec14cf0 | 86 | |
Sissors | 0:f3705ec14cf0 | 87 | } |