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@5:651843de21ce, 2018-02-14 (annotated)
- Committer:
- 2247469r
- Date:
- Wed Feb 14 15:26:02 2018 +0000
- Revision:
- 5:651843de21ce
- Parent:
- 4:878821708feb
Added flashing LED function (potential bugs).
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 2247469r | 5:651843de21ce | 1 | //Rango says, hey there Connnor! |
| 2236693B | 0:005922ecb765 | 2 | |
| 2236693B | 0:005922ecb765 | 3 | #include "mbed.h" |
| 2236693B | 0:005922ecb765 | 4 | |
| 2247469r | 5:651843de21ce | 5 | DigitalOut led(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; |
| 2247469r | 5:651843de21ce | 11 | float const DISP_MAX = 7; //size of display indexed from 0 |
| 2247469r | 5:651843de21ce | 12 | float const trigger_high = DISP_MAX/2 + 1; |
| 2247469r | 5:651843de21ce | 13 | float const trigger_low = DISP_MAX/2 - 1; |
| 2236693B | 2:29c3d03c9231 | 14 | |
| 2236693B | 2:29c3d03c9231 | 15 | int sample_buffer[2*MAX_B] = {}; |
| 2236693B | 2:29c3d03c9231 | 16 | int local_buffer[2*MAX_B] = {}; |
| 2236693B | 2:29c3d03c9231 | 17 | int avg_buffer[AVG_LEN] = {}; |
| 2236693B | 0:005922ecb765 | 18 | int read = 0; |
| 2236693B | 0:005922ecb765 | 19 | int write = 0; |
| 2236693B | 0:005922ecb765 | 20 | |
| 2236693B | 2:29c3d03c9231 | 21 | int avg_write = 0; |
| 2236693B | 2:29c3d03c9231 | 22 | |
| 2236693B | 2:29c3d03c9231 | 23 | int avg_sum; |
| 2236693B | 3:c9f4c9c84e36 | 24 | bool first; |
| 2236693B | 3:c9f4c9c84e36 | 25 | float avg; |
| 2247469r | 5:651843de21ce | 26 | float pre_point; |
| 2247469r | 5:651843de21ce | 27 | |
| 2247469r | 5:651843de21ce | 28 | bool rising = false; |
| 2236693B | 2:29c3d03c9231 | 29 | |
| 2236693B | 2:29c3d03c9231 | 30 | Ticker sampler; |
| 2236693B | 2:29c3d03c9231 | 31 | |
| 2236693B | 4:878821708feb | 32 | void sampling () { //Sample Signal |
| 2236693B | 2:29c3d03c9231 | 33 | unsigned int sample = Ain.read_u16(); |
| 2236693B | 2:29c3d03c9231 | 34 | sample_buffer[write++] = sample; |
| 2236693B | 2:29c3d03c9231 | 35 | write = write%(2*MAX_B); |
| 2236693B | 2:29c3d03c9231 | 36 | |
| 2236693B | 2:29c3d03c9231 | 37 | } |
| 2236693B | 2:29c3d03c9231 | 38 | |
| 2247469r | 5:651843de21ce | 39 | float min_of_avg(){ |
| 2247469r | 5:651843de21ce | 40 | int min = avg_buffer[0]; |
| 2247469r | 5:651843de21ce | 41 | for (int i = 0; i < AVG_LEN; i++){ |
| 2247469r | 5:651843de21ce | 42 | if (avg_buffer[i] < min){ |
| 2247469r | 5:651843de21ce | 43 | min = avg_buffer[i]; |
| 2247469r | 5:651843de21ce | 44 | } |
| 2247469r | 5:651843de21ce | 45 | } |
| 2247469r | 5:651843de21ce | 46 | return min; |
| 2247469r | 5:651843de21ce | 47 | } |
| 2247469r | 5:651843de21ce | 48 | |
| 2247469r | 5:651843de21ce | 49 | float max_of_avg(){ |
| 2247469r | 5:651843de21ce | 50 | int max = avg_buffer[0]; |
| 2247469r | 5:651843de21ce | 51 | for (int i = 0; i < AVG_LEN; i++){ |
| 2247469r | 5:651843de21ce | 52 | if (avg_buffer[i] > max){ |
| 2247469r | 5:651843de21ce | 53 | max = avg_buffer[i]; |
| 2247469r | 5:651843de21ce | 54 | } |
| 2247469r | 5:651843de21ce | 55 | } |
| 2247469r | 5:651843de21ce | 56 | return max; |
| 2247469r | 5:651843de21ce | 57 | } |
| 2247469r | 5:651843de21ce | 58 | |
| 2247469r | 5:651843de21ce | 59 | float normalise(float point) { //Normalise the value |
| 2247469r | 5:651843de21ce | 60 | float avg_min = min_of_avg(); |
| 2247469r | 5:651843de21ce | 61 | return (DISP_MAX * (point / (max_of_avg() - avg_min)) + avg_min); |
| 2236693B | 3:c9f4c9c84e36 | 62 | } |
| 2236693B | 3:c9f4c9c84e36 | 63 | |
| 2236693B | 4:878821708feb | 64 | float filter (float output) { //Filter signal digtally to make more precise |
| 2247469r | 5:651843de21ce | 65 | float point = alpha*output + (1- alpha)*pre_point; |
| 2247469r | 5:651843de21ce | 66 | point = normalise(point); |
| 2236693B | 3:c9f4c9c84e36 | 67 | return point; |
| 2236693B | 3:c9f4c9c84e36 | 68 | } |
| 2236693B | 3:c9f4c9c84e36 | 69 | |
| 2247469r | 5:651843de21ce | 70 | float average(){ |
| 2236693B | 3:c9f4c9c84e36 | 71 | float point; |
| 2236693B | 3:c9f4c9c84e36 | 72 | avg = avg_sum/AVG_LEN; |
| 2236693B | 4:878821708feb | 73 | int normalised = avg_buffer[avg_write-1] - avg; |
| 2236693B | 4:878821708feb | 74 | point = filter(normalised); |
| 2236693B | 3:c9f4c9c84e36 | 75 | |
| 2236693B | 3:c9f4c9c84e36 | 76 | return point; |
| 2236693B | 3:c9f4c9c84e36 | 77 | |
| 2236693B | 3:c9f4c9c84e36 | 78 | |
| 2236693B | 3:c9f4c9c84e36 | 79 | } |
| 2236693B | 3:c9f4c9c84e36 | 80 | |
| 2247469r | 5:651843de21ce | 81 | bool above_trig_low(float point){ |
| 2247469r | 5:651843de21ce | 82 | if(point <= trigger_low){ |
| 2247469r | 5:651843de21ce | 83 | rising = false; |
| 2247469r | 5:651843de21ce | 84 | return true; |
| 2247469r | 5:651843de21ce | 85 | } |
| 2247469r | 5:651843de21ce | 86 | return false; |
| 2247469r | 5:651843de21ce | 87 | } |
| 2247469r | 5:651843de21ce | 88 | |
| 2247469r | 5:651843de21ce | 89 | bool above_trig_high(float point){ |
| 2247469r | 5:651843de21ce | 90 | if(point >= trigger_high){ |
| 2247469r | 5:651843de21ce | 91 | rising = true; |
| 2247469r | 5:651843de21ce | 92 | return true; |
| 2247469r | 5:651843de21ce | 93 | } |
| 2247469r | 5:651843de21ce | 94 | return false; |
| 2247469r | 5:651843de21ce | 95 | } |
| 2247469r | 5:651843de21ce | 96 | |
| 2247469r | 5:651843de21ce | 97 | void identify_pulse(float point) { |
| 2247469r | 5:651843de21ce | 98 | // Implementation of hysteresis |
| 2247469r | 5:651843de21ce | 99 | if(rising){ |
| 2247469r | 5:651843de21ce | 100 | if (!above_trig_low(point)){ |
| 2247469r | 5:651843de21ce | 101 | led = 1; |
| 2247469r | 5:651843de21ce | 102 | } else { |
| 2247469r | 5:651843de21ce | 103 | led = 0; |
| 2247469r | 5:651843de21ce | 104 | } |
| 2247469r | 5:651843de21ce | 105 | } else if (!rising){ |
| 2247469r | 5:651843de21ce | 106 | if (!above_trig_high(point)){ |
| 2247469r | 5:651843de21ce | 107 | led = 0; |
| 2247469r | 5:651843de21ce | 108 | } else { |
| 2247469r | 5:651843de21ce | 109 | led = 1; |
| 2247469r | 5:651843de21ce | 110 | } |
| 2247469r | 5:651843de21ce | 111 | } |
| 2247469r | 5:651843de21ce | 112 | } |
| 2247469r | 5:651843de21ce | 113 | |
| 2247469r | 5:651843de21ce | 114 | float data_process() { //Setup for data process |
| 2236693B | 3:c9f4c9c84e36 | 115 | float point ; |
| 2236693B | 2:29c3d03c9231 | 116 | if (first) { |
| 2236693B | 2:29c3d03c9231 | 117 | for (int i =0; i <MAX_B; i++) { |
| 2247469r | 5:651843de21ce | 118 | int data = local_buffer[i]; |
| 2247469r | 5:651843de21ce | 119 | avg_buffer[avg_write] = data; |
| 2236693B | 2:29c3d03c9231 | 120 | avg_write = (++avg_write) % AVG_LEN; |
| 2247469r | 5:651843de21ce | 121 | avg_sum += data; |
| 2247469r | 5:651843de21ce | 122 | } |
| 2247469r | 5:651843de21ce | 123 | if (avg_write == 0) { |
| 2247469r | 5:651843de21ce | 124 | first = false; |
| 2247469r | 5:651843de21ce | 125 | pre_point = local_buffer[MAX_B - 1]; |
| 2236693B | 2:29c3d03c9231 | 126 | } |
| 2236693B | 2:29c3d03c9231 | 127 | } |
| 2236693B | 2:29c3d03c9231 | 128 | else { |
| 2236693B | 2:29c3d03c9231 | 129 | for (int i =0; i <MAX_B; i++) { |
| 2236693B | 2:29c3d03c9231 | 130 | int data = local_buffer[i]; |
| 2236693B | 2:29c3d03c9231 | 131 | avg_sum = avg_sum + data - avg_buffer[avg_write]; |
| 2236693B | 2:29c3d03c9231 | 132 | |
| 2236693B | 2:29c3d03c9231 | 133 | avg_buffer[avg_write] = data; |
| 2247469r | 5:651843de21ce | 134 | avg_write = (++avg_write) % AVG_LEN; |
| 2247469r | 5:651843de21ce | 135 | pre_point = point; |
| 2247469r | 5:651843de21ce | 136 | point = average(); |
| 2247469r | 5:651843de21ce | 137 | identify_pulse(point); |
| 2236693B | 2:29c3d03c9231 | 138 | } |
| 2236693B | 2:29c3d03c9231 | 139 | } |
| 2236693B | 4:878821708feb | 140 | return point; //The tenth point |
| 2236693B | 2:29c3d03c9231 | 141 | |
| 2236693B | 2:29c3d03c9231 | 142 | } |
| 2236693B | 2:29c3d03c9231 | 143 | |
| 2247469r | 5:651843de21ce | 144 | void write_to_display(float output) { |
| 2236693B | 0:005922ecb765 | 145 | } |
| 2236693B | 0:005922ecb765 | 146 | |
| 2236693B | 0:005922ecb765 | 147 | int main() { |
| 2236693B | 0:005922ecb765 | 148 | sampler.attach(&sampling, 0.0125); //Sample at 80Hz |
| 2236693B | 0:005922ecb765 | 149 | |
| 2236693B | 0:005922ecb765 | 150 | while(1) { |
| 2236693B | 2:29c3d03c9231 | 151 | if (write-read > MAX_B || read-write > MAX_B) { // |write-read| > MAX_B/2 |
| 2236693B | 0:005922ecb765 | 152 | for(int i = 0; i < MAX_B/2; i++) { |
| 2236693B | 0:005922ecb765 | 153 | local_buffer[i] = sample_buffer[read]; |
| 2236693B | 0:005922ecb765 | 154 | read = (++read) % MAX_B; |
| 2236693B | 0:005922ecb765 | 155 | } |
| 2236693B | 0:005922ecb765 | 156 | } |
| 2236693B | 4:878821708feb | 157 | float output = data_process(); |
| 2247469r | 5:651843de21ce | 158 | |
| 2236693B | 4:878821708feb | 159 | write_to_display(output); |
| 2236693B | 0:005922ecb765 | 160 | } |
| 2236693B | 0:005922ecb765 | 161 | } |
| 2236693B | 0:005922ecb765 | 162 | |
| 2236693B | 0:005922ecb765 | 163 | |
| 2236693B | 0:005922ecb765 | 164 |

