emg values for left and right

Dependencies:   HIDScope biquadFilter mbed

Committer:
Roytsg
Date:
Thu Nov 02 13:24:09 2017 +0000
Revision:
0:4fd672acf3f6
final emg values;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Roytsg 0:4fd672acf3f6 1 #include "mbed.h"
Roytsg 0:4fd672acf3f6 2 #include "HIDScope.h"
Roytsg 0:4fd672acf3f6 3 #include "BiQuad.h"
Roytsg 0:4fd672acf3f6 4 #include "math.h"
Roytsg 0:4fd672acf3f6 5
Roytsg 0:4fd672acf3f6 6 //Define objects
Roytsg 0:4fd672acf3f6 7 AnalogIn emg( A0 );
Roytsg 0:4fd672acf3f6 8 AnalogIn emg1( A1 );
Roytsg 0:4fd672acf3f6 9 //AnalogIn emg2( A2 );
Roytsg 0:4fd672acf3f6 10 //AnalogIn emg3( A3 );
Roytsg 0:4fd672acf3f6 11
Roytsg 0:4fd672acf3f6 12 Ticker sample_timer;
Roytsg 0:4fd672acf3f6 13 HIDScope scope( 2 );
Roytsg 0:4fd672acf3f6 14 DigitalOut led(LED1);
Roytsg 0:4fd672acf3f6 15
Roytsg 0:4fd672acf3f6 16 int P= 200;
Roytsg 0:4fd672acf3f6 17 int Q = 200;
Roytsg 0:4fd672acf3f6 18 double A[200];
Roytsg 0:4fd672acf3f6 19 double B[200];
Roytsg 0:4fd672acf3f6 20
Roytsg 0:4fd672acf3f6 21 BiQuadChain bqcR;
Roytsg 0:4fd672acf3f6 22 BiQuad bq1R( 0.6844323315947305,1.368864663189461, 0.6844323315947305,1.2243497755555954,0.5133795508233265);
Roytsg 0:4fd672acf3f6 23 BiQuad bq2R( 0.6844323315947306, -1.3688646631894612, 0.6844323315947306, -1.2243497755555959, 0.5133795508233266);
Roytsg 0:4fd672acf3f6 24 BiQuad bq3R( 0.7566897754116633, -1.2243497755555959, 0.7566897754116633, -1.2243497755555959, 0.5133795508233266);
Roytsg 0:4fd672acf3f6 25 BiQuadChain bqcL;
Roytsg 0:4fd672acf3f6 26 BiQuad bq1L( 0.6844323315947305,1.368864663189461, 0.6844323315947305,1.2243497755555954,0.5133795508233265);
Roytsg 0:4fd672acf3f6 27 BiQuad bq2L( 0.6844323315947306, -1.3688646631894612, 0.6844323315947306, -1.2243497755555959, 0.5133795508233266);
Roytsg 0:4fd672acf3f6 28 BiQuad bq3L( 0.7566897754116633, -1.2243497755555959, 0.7566897754116633, -1.2243497755555959, 0.5133795508233266);
Roytsg 0:4fd672acf3f6 29
Roytsg 0:4fd672acf3f6 30 void emgSample() {
Roytsg 0:4fd672acf3f6 31
Roytsg 0:4fd672acf3f6 32 double emgFilteredR = bqcR.step( emg.read() );
Roytsg 0:4fd672acf3f6 33 double emgabsR = abs(emgFilteredR);
Roytsg 0:4fd672acf3f6 34 //scope.set(0, emgFiltered );
Roytsg 0:4fd672acf3f6 35 //scope.set(1, emgabs );
Roytsg 0:4fd672acf3f6 36
Roytsg 0:4fd672acf3f6 37 for(int i = P-1; i >= 0; i--){
Roytsg 0:4fd672acf3f6 38 if (i == 0) {
Roytsg 0:4fd672acf3f6 39 A[i] = emgabsR;
Roytsg 0:4fd672acf3f6 40 }
Roytsg 0:4fd672acf3f6 41 else {
Roytsg 0:4fd672acf3f6 42 A[i] = A[i-1];
Roytsg 0:4fd672acf3f6 43 }
Roytsg 0:4fd672acf3f6 44 }
Roytsg 0:4fd672acf3f6 45
Roytsg 0:4fd672acf3f6 46 double sumR = 0;
Roytsg 0:4fd672acf3f6 47 for (int n = 0; n < P-1; n++) {
Roytsg 0:4fd672acf3f6 48 sumR = sumR + A[n];
Roytsg 0:4fd672acf3f6 49 }
Roytsg 0:4fd672acf3f6 50
Roytsg 0:4fd672acf3f6 51 double movmeanR = sumR/P;
Roytsg 0:4fd672acf3f6 52
Roytsg 0:4fd672acf3f6 53 scope.set(0, movmeanR);
Roytsg 0:4fd672acf3f6 54
Roytsg 0:4fd672acf3f6 55 double emgFilteredL = bqcL.step( emg1.read() );
Roytsg 0:4fd672acf3f6 56 double emgabsL = abs(emgFilteredL);
Roytsg 0:4fd672acf3f6 57
Roytsg 0:4fd672acf3f6 58 for(int i = Q-1; i >= 0; i--){
Roytsg 0:4fd672acf3f6 59 if (i == 0) {
Roytsg 0:4fd672acf3f6 60 B[i] = emgabsL;
Roytsg 0:4fd672acf3f6 61 }
Roytsg 0:4fd672acf3f6 62 else {
Roytsg 0:4fd672acf3f6 63 B[i] = B[i-1];
Roytsg 0:4fd672acf3f6 64 }
Roytsg 0:4fd672acf3f6 65 }
Roytsg 0:4fd672acf3f6 66
Roytsg 0:4fd672acf3f6 67 double sumL = 0;
Roytsg 0:4fd672acf3f6 68 for (int n = 0; n < Q-1; n++) {
Roytsg 0:4fd672acf3f6 69 sumL = sumL + B[n];
Roytsg 0:4fd672acf3f6 70 }
Roytsg 0:4fd672acf3f6 71
Roytsg 0:4fd672acf3f6 72 double movmeanL = sumL/Q;
Roytsg 0:4fd672acf3f6 73
Roytsg 0:4fd672acf3f6 74 scope.set(1, movmeanL);
Roytsg 0:4fd672acf3f6 75
Roytsg 0:4fd672acf3f6 76 scope.send();
Roytsg 0:4fd672acf3f6 77
Roytsg 0:4fd672acf3f6 78 }
Roytsg 0:4fd672acf3f6 79 int main()
Roytsg 0:4fd672acf3f6 80 {
Roytsg 0:4fd672acf3f6 81 bqcR.add( &bq1R ).add( &bq2R ).add( &bq3R );
Roytsg 0:4fd672acf3f6 82 bqcL.add( &bq1L ).add( &bq2L ).add( &bq3L );
Roytsg 0:4fd672acf3f6 83 sample_timer.attach( &emgSample, 0.002 );
Roytsg 0:4fd672acf3f6 84 while(1) {}
Roytsg 0:4fd672acf3f6 85 }