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.
Dependents: PID_VelocityExample TheProgram Passief_stuurprogramma Actief_stuurprogramma
EMG.h@0:b5f7b64b0fe4, 2015-10-12 (annotated)
- Committer:
- ewoud
- Date:
- Mon Oct 12 08:47:41 2015 +0000
- Revision:
- 0:b5f7b64b0fe4
- Child:
- 1:b73e3dc74d7c
first integrated emg library, calibration function needs work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ewoud | 0:b5f7b64b0fe4 | 1 | #include "biquadFilter.h" |
ewoud | 0:b5f7b64b0fe4 | 2 | #include <cmath> |
ewoud | 0:b5f7b64b0fe4 | 3 | |
ewoud | 0:b5f7b64b0fe4 | 4 | |
ewoud | 0:b5f7b64b0fe4 | 5 | int Fs = 512; // sampling frequency |
ewoud | 0:b5f7b64b0fe4 | 6 | const double low_b1 = 1.480219865318266e-04; //filter coefficients - second order butterworth filters at 2 hz low and 25 hz high, coefficents based on Fs of 512. |
ewoud | 0:b5f7b64b0fe4 | 7 | const double low_b2 = 2.960439730636533e-04; |
ewoud | 0:b5f7b64b0fe4 | 8 | const double low_b3 = 1.480219865318266e-04; |
ewoud | 0:b5f7b64b0fe4 | 9 | const double low_a2 = -1.965293372622690e+00; // a1 is normalized to 1 |
ewoud | 0:b5f7b64b0fe4 | 10 | const double low_a3 = 9.658854605688177e-01; |
ewoud | 0:b5f7b64b0fe4 | 11 | const double high_b1 = 8.047897937631126e-01; |
ewoud | 0:b5f7b64b0fe4 | 12 | const double high_b2 = -1.609579587526225e+00; |
ewoud | 0:b5f7b64b0fe4 | 13 | const double high_b3 = 8.047897937631126e-01; |
ewoud | 0:b5f7b64b0fe4 | 14 | const double high_a2 = -1.571102440190402e+00; // a1 is normalized to 1 |
ewoud | 0:b5f7b64b0fe4 | 15 | const double high_a3 = 6.480567348620491e-01; |
ewoud | 0:b5f7b64b0fe4 | 16 | biquadFilter highpass1(high_a2, high_a3, high_b1, high_b2, high_b3); // different objects for different inputs, otherwise the v1 and v2 variables get fucked up |
ewoud | 0:b5f7b64b0fe4 | 17 | biquadFilter highpass2(high_a2, high_a3, high_b1, high_b2, high_b3); |
ewoud | 0:b5f7b64b0fe4 | 18 | biquadFilter highpass3(high_a2, high_a3, high_b1, high_b2, high_b3); |
ewoud | 0:b5f7b64b0fe4 | 19 | biquadFilter highpass4(high_a2, high_a3, high_b1, high_b2, high_b3); |
ewoud | 0:b5f7b64b0fe4 | 20 | biquadFilter lowpass1(low_a2, low_a3, low_b1, low_b2, low_b3); |
ewoud | 0:b5f7b64b0fe4 | 21 | biquadFilter lowpass2(low_a2, low_a3, low_b1, low_b2, low_b3); |
ewoud | 0:b5f7b64b0fe4 | 22 | biquadFilter lowpass3(low_a2, low_a3, low_b1, low_b2, low_b3); |
ewoud | 0:b5f7b64b0fe4 | 23 | biquadFilter lowpass4(low_a2, low_a3, low_b1, low_b2, low_b3); |
ewoud | 0:b5f7b64b0fe4 | 24 | |
ewoud | 0:b5f7b64b0fe4 | 25 | AnalogIn input1(A0); // declaring the 4 inputs |
ewoud | 0:b5f7b64b0fe4 | 26 | AnalogIn input2(A1); |
ewoud | 0:b5f7b64b0fe4 | 27 | AnalogIn input3(A2); |
ewoud | 0:b5f7b64b0fe4 | 28 | AnalogIn input4(A3); |
ewoud | 0:b5f7b64b0fe4 | 29 | double u1; double y1; // declaring the input variables |
ewoud | 0:b5f7b64b0fe4 | 30 | double u2; double y2; |
ewoud | 0:b5f7b64b0fe4 | 31 | double u3; double y3; |
ewoud | 0:b5f7b64b0fe4 | 32 | double u4; double y4; |
ewoud | 0:b5f7b64b0fe4 | 33 | |
ewoud | 0:b5f7b64b0fe4 | 34 | Ticker T1; |
ewoud | 0:b5f7b64b0fe4 | 35 | volatile bool sample_go; |
ewoud | 0:b5f7b64b0fe4 | 36 | |
ewoud | 0:b5f7b64b0fe4 | 37 | InterruptIn cali_button(PTA4); // initialize interrupt button for calibration stuff |
ewoud | 0:b5f7b64b0fe4 | 38 | double cali_fact = 8; // calibration factor to normalize filter output to a scale of 0 - 1 |
ewoud | 0:b5f7b64b0fe4 | 39 | double cali_array[2560] = {}; // array to store values in |
ewoud | 0:b5f7b64b0fe4 | 40 | |
ewoud | 0:b5f7b64b0fe4 | 41 | |
ewoud | 0:b5f7b64b0fe4 | 42 | void sample_filter() |
ewoud | 0:b5f7b64b0fe4 | 43 | { |
ewoud | 0:b5f7b64b0fe4 | 44 | u1 = input1; u2 = input2; u3 = input3; u4 = input4; // sample |
ewoud | 0:b5f7b64b0fe4 | 45 | y1 = highpass1.step(u1); y2 = highpass2.step(u2); y3 = highpass3.step(u3); y4 = highpass4.step(u4); // filter order is: high-pass --> rectify --> low-pass |
ewoud | 0:b5f7b64b0fe4 | 46 | y1 = fabs(y1); y2 = fabs(y2); y3 = fabs(y3); y4 = fabs(y4); |
ewoud | 0:b5f7b64b0fe4 | 47 | y1 = lowpass1.step(y1)*cali_fact; y2 = lowpass2.step(y2)*cali_fact; y3 = lowpass3.step(y3)*cali_fact; y4 = lowpass4.step(y4)*cali_fact; // roughly normalize to a scale of 0 - 1, where 0 is minimum and 1 is roughly the maximum output of dennis. |
ewoud | 0:b5f7b64b0fe4 | 48 | } |
ewoud | 0:b5f7b64b0fe4 | 49 | |
ewoud | 0:b5f7b64b0fe4 | 50 | void samplego() // ticker function, set flag to true every sample interval |
ewoud | 0:b5f7b64b0fe4 | 51 | { |
ewoud | 0:b5f7b64b0fe4 | 52 | sample_go = 1; |
ewoud | 0:b5f7b64b0fe4 | 53 | } |
ewoud | 0:b5f7b64b0fe4 | 54 | |
ewoud | 0:b5f7b64b0fe4 | 55 | void calibrate() // function to calibrate the emg signals from the user. It takes 5 seconds of measurements of maximum output, then takes the max and normalizes to that. |
ewoud | 0:b5f7b64b0fe4 | 56 | { |
ewoud | 0:b5f7b64b0fe4 | 57 | double cali_max = 0; // declare max |
ewoud | 0:b5f7b64b0fe4 | 58 | cali_fact = 1; |
ewoud | 0:b5f7b64b0fe4 | 59 | for(int cali_index = 0; cali_index < 2560; cali_index++) |
ewoud | 0:b5f7b64b0fe4 | 60 | { |
ewoud | 0:b5f7b64b0fe4 | 61 | sample_filter(); |
ewoud | 0:b5f7b64b0fe4 | 62 | cali_array[cali_index] = y1; |
ewoud | 0:b5f7b64b0fe4 | 63 | wait((float)1/Fs); |
ewoud | 0:b5f7b64b0fe4 | 64 | } |
ewoud | 0:b5f7b64b0fe4 | 65 | for(int cali_index2 = 0; cali_index2<2560; cali_index2++) |
ewoud | 0:b5f7b64b0fe4 | 66 | { |
ewoud | 0:b5f7b64b0fe4 | 67 | if(cali_array[cali_index2] > cali_max) |
ewoud | 0:b5f7b64b0fe4 | 68 | cali_max = cali_array[cali_index2]; |
ewoud | 0:b5f7b64b0fe4 | 69 | } |
ewoud | 0:b5f7b64b0fe4 | 70 | cali_fact = (double)1/cali_max; |
ewoud | 0:b5f7b64b0fe4 | 71 | } |
ewoud | 0:b5f7b64b0fe4 | 72 | |
ewoud | 0:b5f7b64b0fe4 | 73 | /* |
ewoud | 0:b5f7b64b0fe4 | 74 | int main() |
ewoud | 0:b5f7b64b0fe4 | 75 | { |
ewoud | 0:b5f7b64b0fe4 | 76 | T1.attach(&samplego, (float)1/Fs); |
ewoud | 0:b5f7b64b0fe4 | 77 | cali_button.rise(&calibrate); |
ewoud | 0:b5f7b64b0fe4 | 78 | while(1) |
ewoud | 0:b5f7b64b0fe4 | 79 | { |
ewoud | 0:b5f7b64b0fe4 | 80 | if(sample_go) |
ewoud | 0:b5f7b64b0fe4 | 81 | { |
ewoud | 0:b5f7b64b0fe4 | 82 | sample_filter(); |
ewoud | 0:b5f7b64b0fe4 | 83 | sample_go = 0; |
ewoud | 0:b5f7b64b0fe4 | 84 | } |
ewoud | 0:b5f7b64b0fe4 | 85 | } // while end |
ewoud | 0:b5f7b64b0fe4 | 86 | } // main end |
ewoud | 0:b5f7b64b0fe4 | 87 | */ |