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.
Dependencies: mbed
main.cpp@4:878821708feb, 2018-02-14 (annotated)
- Committer:
- 2236693B
- Date:
- Wed Feb 14 11:56:50 2018 +0000
- Revision:
- 4:878821708feb
- Parent:
- 3:c9f4c9c84e36
- Child:
- 5:651843de21ce
Commented;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 2236693B | 1:d1e89afbe50c | 1 | //Rango says, hey there Conor! |
| 2236693B | 0:005922ecb765 | 2 | |
| 2236693B | 0:005922ecb765 | 3 | #include "mbed.h" |
| 2236693B | 0:005922ecb765 | 4 | |
| 2236693B | 0:005922ecb765 | 5 | DigitalOut myled(LED1); |
| 2236693B | 0:005922ecb765 | 6 | AnalogIn Ain(PTB1); |
| 2236693B | 0:005922ecb765 | 7 | |
| 2236693B | 2:29c3d03c9231 | 8 | int const MAX_B = 10; |
| 2236693B | 3:c9f4c9c84e36 | 9 | int const alpha = 0.5; |
| 2236693B | 2:29c3d03c9231 | 10 | int const AVG_LEN = 160; |
| 2236693B | 2:29c3d03c9231 | 11 | |
| 2236693B | 2:29c3d03c9231 | 12 | int sample_buffer[2*MAX_B] = {}; |
| 2236693B | 2:29c3d03c9231 | 13 | int local_buffer[2*MAX_B] = {}; |
| 2236693B | 2:29c3d03c9231 | 14 | int avg_buffer[AVG_LEN] = {}; |
| 2236693B | 0:005922ecb765 | 15 | int read = 0; |
| 2236693B | 0:005922ecb765 | 16 | int write = 0; |
| 2236693B | 0:005922ecb765 | 17 | |
| 2236693B | 2:29c3d03c9231 | 18 | int avg_write = 0; |
| 2236693B | 2:29c3d03c9231 | 19 | |
| 2236693B | 2:29c3d03c9231 | 20 | int avg_sum; |
| 2236693B | 3:c9f4c9c84e36 | 21 | bool first; |
| 2236693B | 3:c9f4c9c84e36 | 22 | float avg; |
| 2236693B | 3:c9f4c9c84e36 | 23 | float pre_avg; |
| 2236693B | 2:29c3d03c9231 | 24 | |
| 2236693B | 2:29c3d03c9231 | 25 | Ticker sampler; |
| 2236693B | 2:29c3d03c9231 | 26 | |
| 2236693B | 4:878821708feb | 27 | void sampling () { //Sample Signal |
| 2236693B | 2:29c3d03c9231 | 28 | unsigned int sample = Ain.read_u16(); |
| 2236693B | 2:29c3d03c9231 | 29 | sample_buffer[write++] = sample; |
| 2236693B | 2:29c3d03c9231 | 30 | write = write%(2*MAX_B); |
| 2236693B | 2:29c3d03c9231 | 31 | |
| 2236693B | 2:29c3d03c9231 | 32 | } |
| 2236693B | 2:29c3d03c9231 | 33 | |
| 2236693B | 4:878821708feb | 34 | float scale (float point) { //Multiply by scale factor |
| 2236693B | 3:c9f4c9c84e36 | 35 | return point*7 ; |
| 2236693B | 3:c9f4c9c84e36 | 36 | } |
| 2236693B | 3:c9f4c9c84e36 | 37 | |
| 2236693B | 4:878821708feb | 38 | float filter (float output) { //Filter signal digtally to make more precise |
| 2236693B | 3:c9f4c9c84e36 | 39 | float point = alpha*output + (1- alpha)*pre_avg; |
| 2236693B | 3:c9f4c9c84e36 | 40 | point = scale(point); |
| 2236693B | 3:c9f4c9c84e36 | 41 | return point; |
| 2236693B | 3:c9f4c9c84e36 | 42 | } |
| 2236693B | 3:c9f4c9c84e36 | 43 | |
| 2236693B | 4:878821708feb | 44 | float get_trend(){ //Normalise signal by removing trend |
| 2236693B | 3:c9f4c9c84e36 | 45 | float point; |
| 2236693B | 3:c9f4c9c84e36 | 46 | pre_avg = avg; |
| 2236693B | 3:c9f4c9c84e36 | 47 | avg = avg_sum/AVG_LEN; |
| 2236693B | 4:878821708feb | 48 | int normalised = avg_buffer[avg_write-1] - avg; |
| 2236693B | 4:878821708feb | 49 | point = filter(normalised); |
| 2236693B | 3:c9f4c9c84e36 | 50 | |
| 2236693B | 3:c9f4c9c84e36 | 51 | return point; |
| 2236693B | 3:c9f4c9c84e36 | 52 | |
| 2236693B | 3:c9f4c9c84e36 | 53 | |
| 2236693B | 3:c9f4c9c84e36 | 54 | } |
| 2236693B | 3:c9f4c9c84e36 | 55 | |
| 2236693B | 4:878821708feb | 56 | float normalise() { //Normalise values |
| 2236693B | 3:c9f4c9c84e36 | 57 | float point ; |
| 2236693B | 2:29c3d03c9231 | 58 | if (first) { |
| 2236693B | 2:29c3d03c9231 | 59 | for (int i =0; i <MAX_B; i++) { |
| 2236693B | 2:29c3d03c9231 | 60 | avg_buffer[avg_write] = local_buffer[i]; |
| 2236693B | 2:29c3d03c9231 | 61 | avg_write = (++avg_write) % AVG_LEN; |
| 2236693B | 2:29c3d03c9231 | 62 | } |
| 2236693B | 2:29c3d03c9231 | 63 | if (write == 0) { |
| 2236693B | 2:29c3d03c9231 | 64 | first = false; |
| 2236693B | 2:29c3d03c9231 | 65 | } |
| 2236693B | 2:29c3d03c9231 | 66 | } |
| 2236693B | 2:29c3d03c9231 | 67 | else { |
| 2236693B | 2:29c3d03c9231 | 68 | for (int i =0; i <MAX_B; i++) { |
| 2236693B | 2:29c3d03c9231 | 69 | int data = local_buffer[i]; |
| 2236693B | 2:29c3d03c9231 | 70 | avg_sum = avg_sum + data - avg_buffer[avg_write]; |
| 2236693B | 2:29c3d03c9231 | 71 | |
| 2236693B | 2:29c3d03c9231 | 72 | avg_buffer[avg_write] = data; |
| 2236693B | 2:29c3d03c9231 | 73 | avg_write = (++avg_write) % AVG_LEN; |
| 2236693B | 3:c9f4c9c84e36 | 74 | point = get_trend(); |
| 2236693B | 2:29c3d03c9231 | 75 | |
| 2236693B | 2:29c3d03c9231 | 76 | } |
| 2236693B | 2:29c3d03c9231 | 77 | } |
| 2236693B | 4:878821708feb | 78 | return point; //The tenth point |
| 2236693B | 2:29c3d03c9231 | 79 | |
| 2236693B | 2:29c3d03c9231 | 80 | } |
| 2236693B | 2:29c3d03c9231 | 81 | |
| 2236693B | 4:878821708feb | 82 | float data_process() { |
| 2236693B | 4:878821708feb | 83 | return normalise(); |
| 2236693B | 0:005922ecb765 | 84 | } |
| 2236693B | 0:005922ecb765 | 85 | |
| 2236693B | 4:878821708feb | 86 | void write_to_display(float output) { |
| 2236693B | 4:878821708feb | 87 | } |
| 2236693B | 4:878821708feb | 88 | |
| 2236693B | 4:878821708feb | 89 | void identify_pulse(float ouput) { |
| 2236693B | 4:878821708feb | 90 | |
| 2236693B | 4:878821708feb | 91 | |
| 2236693B | 4:878821708feb | 92 | } |
| 2236693B | 0:005922ecb765 | 93 | int main() { |
| 2236693B | 0:005922ecb765 | 94 | sampler.attach(&sampling, 0.0125); //Sample at 80Hz |
| 2236693B | 0:005922ecb765 | 95 | |
| 2236693B | 0:005922ecb765 | 96 | while(1) { |
| 2236693B | 2:29c3d03c9231 | 97 | if (write-read > MAX_B || read-write > MAX_B) { // |write-read| > MAX_B/2 |
| 2236693B | 0:005922ecb765 | 98 | for(int i = 0; i < MAX_B/2; i++) { |
| 2236693B | 0:005922ecb765 | 99 | local_buffer[i] = sample_buffer[read]; |
| 2236693B | 0:005922ecb765 | 100 | read = (++read) % MAX_B; |
| 2236693B | 0:005922ecb765 | 101 | } |
| 2236693B | 0:005922ecb765 | 102 | } |
| 2236693B | 4:878821708feb | 103 | float output = data_process(); |
| 2236693B | 4:878821708feb | 104 | identify_pulse(output); |
| 2236693B | 4:878821708feb | 105 | write_to_display(output); |
| 2236693B | 0:005922ecb765 | 106 | } |
| 2236693B | 0:005922ecb765 | 107 | } |
| 2236693B | 0:005922ecb765 | 108 | |
| 2236693B | 0:005922ecb765 | 109 | |
| 2236693B | 0:005922ecb765 | 110 |

