EMG Filter, normalisatie zonder ticker, met for/while loops. Werkt niet, HIDScope is niet te openen
Dependencies: mbed HIDScope biquadFilter
main.cpp@29:1483fd8674da, 2019-10-21 (annotated)
- Committer:
- hidde1104
- Date:
- Mon Oct 21 09:29:28 2019 +0000
- Revision:
- 29:1483fd8674da
- Parent:
- 28:4eaf5990a7b3
Pre-running calibration
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
vsluiter | 11:ce72ec658a95 | 2 | #include "HIDScope.h" |
hidde1104 | 21:48b40b82d195 | 3 | #include "BiQuad.h" |
hidde1104 | 27:c53dd84c38aa | 4 | Serial pc(USBTX, USBRX); |
hidde1104 | 27:c53dd84c38aa | 5 | InterruptIn but1(PTC6); |
hidde1104 | 21:48b40b82d195 | 6 | |
hidde1104 | 21:48b40b82d195 | 7 | BiQuad bq1 (0.881889334678067, -1.76377866935613, 0.8818893346780671, -1.77069673005903, 0.797707797506027); |
hidde1104 | 21:48b40b82d195 | 8 | |
hidde1104 | 21:48b40b82d195 | 9 | BiQuad bq2 (0.000198358203463849, 0.000396716406927699, 0.000198358203463849, -1.96262073248799, 0.963423352820821); |
hidde1104 | 21:48b40b82d195 | 10 | |
hidde1104 | 21:48b40b82d195 | 11 | |
hidde1104 | 21:48b40b82d195 | 12 | BiQuadChain bqc; |
hidde1104 | 21:48b40b82d195 | 13 | |
vsluiter | 0:32bb76391d89 | 14 | |
vsluiter | 4:8b298dfada81 | 15 | //Define objects |
tomlankhorst | 19:2bf824669684 | 16 | AnalogIn emg0( A0 ); |
tomlankhorst | 19:2bf824669684 | 17 | AnalogIn emg1( A1 ); |
tomlankhorst | 19:2bf824669684 | 18 | |
tomlankhorst | 14:f83354387756 | 19 | Ticker sample_timer; |
hidde1104 | 27:c53dd84c38aa | 20 | Ticker calibration_timer; |
hidde1104 | 21:48b40b82d195 | 21 | HIDScope scope( 3 ); |
hidde1104 | 27:c53dd84c38aa | 22 | DigitalOut led(LED2); |
vsluiter | 2:e314bb3b2d99 | 23 | |
tomlankhorst | 14:f83354387756 | 24 | /** Sample function |
tomlankhorst | 14:f83354387756 | 25 | * this function samples the emg and sends it to HIDScope |
tomlankhorst | 14:f83354387756 | 26 | **/ |
hidde1104 | 27:c53dd84c38aa | 27 | float filter_value; |
hidde1104 | 28:4eaf5990a7b3 | 28 | float emg0_value; |
hidde1104 | 28:4eaf5990a7b3 | 29 | float emg1_value; |
hidde1104 | 29:1483fd8674da | 30 | float filter_max; |
hidde1104 | 29:1483fd8674da | 31 | float filter_min; |
hidde1104 | 27:c53dd84c38aa | 32 | |
tomlankhorst | 14:f83354387756 | 33 | void sample() |
vsluiter | 2:e314bb3b2d99 | 34 | { |
tomlankhorst | 19:2bf824669684 | 35 | /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */ |
hidde1104 | 28:4eaf5990a7b3 | 36 | emg0_value = emg0.read(); |
hidde1104 | 28:4eaf5990a7b3 | 37 | emg1_value = emg1.read(); |
hidde1104 | 27:c53dd84c38aa | 38 | |
hidde1104 | 27:c53dd84c38aa | 39 | |
hidde1104 | 28:4eaf5990a7b3 | 40 | filter_value = fabs(bq2.step(fabs(bq1.step(emg0_value-emg1_value)))); |
hidde1104 | 27:c53dd84c38aa | 41 | |
hidde1104 | 28:4eaf5990a7b3 | 42 | scope.set(0, emg0.read() ); |
hidde1104 | 28:4eaf5990a7b3 | 43 | scope.set(1, emg1.read() ); |
hidde1104 | 28:4eaf5990a7b3 | 44 | scope.set(2, filter_value); |
hidde1104 | 29:1483fd8674da | 45 | |
hidde1104 | 27:c53dd84c38aa | 46 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
tomlankhorst | 19:2bf824669684 | 47 | * Ensure that enough channels are available (HIDScope scope( 2 )) |
tomlankhorst | 20:97059009a491 | 48 | * Finally, send all channels to the PC at once */ |
hidde1104 | 29:1483fd8674da | 49 | |
hidde1104 | 28:4eaf5990a7b3 | 50 | scope.send(); |
hidde1104 | 29:1483fd8674da | 51 | |
tomlankhorst | 18:21d8e7a81cf5 | 52 | /* To indicate that the function is working, the LED is toggled */ |
tomlankhorst | 18:21d8e7a81cf5 | 53 | led = !led; |
vsluiter | 2:e314bb3b2d99 | 54 | } |
vsluiter | 0:32bb76391d89 | 55 | |
hidde1104 | 29:1483fd8674da | 56 | void sample_2() |
hidde1104 | 29:1483fd8674da | 57 | { |
hidde1104 | 29:1483fd8674da | 58 | /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */ |
hidde1104 | 29:1483fd8674da | 59 | emg0_value = emg0.read(); |
hidde1104 | 29:1483fd8674da | 60 | emg1_value = emg1.read(); |
hidde1104 | 29:1483fd8674da | 61 | |
hidde1104 | 29:1483fd8674da | 62 | |
hidde1104 | 29:1483fd8674da | 63 | filter_value = fabs(bq2.step(fabs(bq1.step(emg0_value-emg1_value)))); |
hidde1104 | 29:1483fd8674da | 64 | filter_value = (filter_value-filter_min) / (0.5*filter_max-filter_min); |
hidde1104 | 29:1483fd8674da | 65 | |
hidde1104 | 29:1483fd8674da | 66 | scope.set(0, emg0.read() ); |
hidde1104 | 29:1483fd8674da | 67 | scope.set(1, emg1.read() ); |
hidde1104 | 29:1483fd8674da | 68 | scope.set(2, filter_value); |
hidde1104 | 29:1483fd8674da | 69 | |
hidde1104 | 29:1483fd8674da | 70 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
hidde1104 | 29:1483fd8674da | 71 | * Ensure that enough channels are available (HIDScope scope( 2 )) |
hidde1104 | 29:1483fd8674da | 72 | * Finally, send all channels to the PC at once */ |
hidde1104 | 29:1483fd8674da | 73 | |
hidde1104 | 29:1483fd8674da | 74 | scope.send(); |
hidde1104 | 29:1483fd8674da | 75 | |
hidde1104 | 29:1483fd8674da | 76 | /* To indicate that the function is working, the LED is toggled */ |
hidde1104 | 29:1483fd8674da | 77 | led = !led; |
hidde1104 | 29:1483fd8674da | 78 | } |
hidde1104 | 29:1483fd8674da | 79 | |
hidde1104 | 29:1483fd8674da | 80 | |
hidde1104 | 27:c53dd84c38aa | 81 | const int length = 1000; |
hidde1104 | 27:c53dd84c38aa | 82 | int i; |
hidde1104 | 27:c53dd84c38aa | 83 | float sample_array[length] = {}; |
hidde1104 | 27:c53dd84c38aa | 84 | |
hidde1104 | 27:c53dd84c38aa | 85 | void calibration() |
hidde1104 | 27:c53dd84c38aa | 86 | { |
hidde1104 | 27:c53dd84c38aa | 87 | i = 0; |
hidde1104 | 29:1483fd8674da | 88 | while (i < (length)) { |
hidde1104 | 27:c53dd84c38aa | 89 | sample(); |
hidde1104 | 27:c53dd84c38aa | 90 | pc.printf("I can reach this point\r\n"); |
hidde1104 | 27:c53dd84c38aa | 91 | sample_array[i] = filter_value; |
hidde1104 | 29:1483fd8674da | 92 | pc.printf("The filter_value is %f\r\n",filter_value); |
hidde1104 | 27:c53dd84c38aa | 93 | i++; |
hidde1104 | 27:c53dd84c38aa | 94 | wait(0.001); |
hidde1104 | 27:c53dd84c38aa | 95 | } |
hidde1104 | 29:1483fd8674da | 96 | |
hidde1104 | 29:1483fd8674da | 97 | filter_max = -100.0; |
hidde1104 | 29:1483fd8674da | 98 | filter_min = 100.0; |
hidde1104 | 29:1483fd8674da | 99 | int m; |
hidde1104 | 29:1483fd8674da | 100 | for (m = 200 ; m < length ; m++) { |
hidde1104 | 29:1483fd8674da | 101 | pc.printf("the value of the m-st instance of sample_array is %f\r\n",sample_array[m]); |
hidde1104 | 29:1483fd8674da | 102 | if (sample_array[m] > filter_max) { |
hidde1104 | 29:1483fd8674da | 103 | filter_max = sample_array[m]; |
hidde1104 | 29:1483fd8674da | 104 | } |
hidde1104 | 29:1483fd8674da | 105 | if (sample_array[m] < filter_min) { |
hidde1104 | 29:1483fd8674da | 106 | filter_min = sample_array[m]; |
hidde1104 | 29:1483fd8674da | 107 | } |
hidde1104 | 29:1483fd8674da | 108 | } |
hidde1104 | 29:1483fd8674da | 109 | pc.printf("The value of filter_max is %f\r\nThe value of filter_min is %f\r\n",filter_max, filter_min); |
hidde1104 | 27:c53dd84c38aa | 110 | } |
hidde1104 | 27:c53dd84c38aa | 111 | |
vsluiter | 0:32bb76391d89 | 112 | int main() |
hidde1104 | 27:c53dd84c38aa | 113 | { |
hidde1104 | 27:c53dd84c38aa | 114 | pc.baud(115200); |
hidde1104 | 27:c53dd84c38aa | 115 | pc.printf("Running main"); |
hidde1104 | 21:48b40b82d195 | 116 | bqc.add( &bq1); |
tomlankhorst | 14:f83354387756 | 117 | /**Attach the 'sample' function to the timer 'sample_timer'. |
tomlankhorst | 19:2bf824669684 | 118 | * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz |
vsluiter | 4:8b298dfada81 | 119 | */ |
hidde1104 | 28:4eaf5990a7b3 | 120 | wait(2); |
hidde1104 | 27:c53dd84c38aa | 121 | calibration(); |
hidde1104 | 27:c53dd84c38aa | 122 | |
hidde1104 | 29:1483fd8674da | 123 | sample_timer.attach(&sample_2, 0.001); |
tomlankhorst | 15:0da764eea774 | 124 | |
tomlankhorst | 14:f83354387756 | 125 | /*empty loop, sample() is executed periodically*/ |
tomlankhorst | 15:0da764eea774 | 126 | while(1) {} |
hidde1104 | 21:48b40b82d195 | 127 | } |