EMG signals all around zero, not showing differences anymore in hidscope

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of a_check_emg_filtered_without_cal by Daniqe Kottelenberg

Committer:
daniQQue
Date:
Mon Oct 24 14:20:51 2016 +0000
Revision:
20:a0495210915b
Parent:
19:fb98ff1d06ed
also error in hidscope, emg around zero...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daniQQue 0:34c739fcc3e0 1 //libraries
daniQQue 0:34c739fcc3e0 2 #include "mbed.h"
daniQQue 0:34c739fcc3e0 3 #include "HIDScope.h"
daniQQue 14:5b17697cf775 4 #include "BiQuad.h"
daniQQue 19:fb98ff1d06ed 5 #include "MODSERIAL.h"
daniQQue 0:34c739fcc3e0 6
daniQQue 0:34c739fcc3e0 7 //Define objects
daniQQue 18:d7695ac04de3 8 AnalogIn emg_biceps_right_in( A0 ); //analog in to get EMG in to c++
daniQQue 16:fd4521a4f0b3 9 Ticker sample_timer; //ticker
daniQQue 18:d7695ac04de3 10 HIDScope scope( 3); //open 3 channels in hidscope
daniQQue 8:cd0cb71b69f2 11 DigitalOut richting_motor1(D4); //motor1 direction output
daniQQue 8:cd0cb71b69f2 12 PwmOut pwm_motor1(D5); //motor1 velocity output
daniQQue 19:fb98ff1d06ed 13 MODSERIAL pc(USBTX,USBRX); //connection with computer
daniQQue 19:fb98ff1d06ed 14 DigitalOut led(LED_GREEN); //include led
daniQQue 19:fb98ff1d06ed 15 DigitalIn button (D9); //button
daniQQue 0:34c739fcc3e0 16
daniQQue 0:34c739fcc3e0 17 //define variables
daniQQue 19:fb98ff1d06ed 18
daniQQue 18:d7695ac04de3 19 double emg_biceps_right;
daniQQue 18:d7695ac04de3 20 double emg_filtered_high_biceps_right;
daniQQue 18:d7695ac04de3 21 double emg_abs_biceps_right;
daniQQue 18:d7695ac04de3 22 double emg_filtered_biceps_right;
daniQQue 7:42d0e38196f1 23 int onoffsignal=0;
daniQQue 18:d7695ac04de3 24 double cut_off_value=0.20;
daniQQue 18:d7695ac04de3 25 double max_right_biceps;
daniQQue 18:d7695ac04de3 26
daniQQue 15:bb4a6c7836d8 27 BiQuad filterhigh(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 15:bb4a6c7836d8 28 BiQuad filterlow (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 10:7255b59224cc 29
daniQQue 15:bb4a6c7836d8 30 //functions which are called in ticker
daniQQue 0:34c739fcc3e0 31 void filter(){
daniQQue 19:fb98ff1d06ed 32 emg_biceps_right = emg_biceps_right_in.read(); //read the emg value from the electrodes
daniQQue 19:fb98ff1d06ed 33 emg_filtered_high_biceps_right = filterhigh.step(emg_biceps_right);
daniQQue 19:fb98ff1d06ed 34 emg_abs_biceps_right =fabs(emg_filtered_high_biceps_right); //fabs because float
daniQQue 19:fb98ff1d06ed 35 emg_filtered_biceps_right =filterlow.step(emg_abs_biceps_right); //gefilterd met high, abs, low
daniQQue 18:d7695ac04de3 36 led=!led;
daniQQue 7:42d0e38196f1 37
daniQQue 18:d7695ac04de3 38 if (emg_filtered_biceps_right>cut_off_value)
daniQQue 7:42d0e38196f1 39 {onoffsignal=1;}
daniQQue 7:42d0e38196f1 40
daniQQue 7:42d0e38196f1 41 else
daniQQue 7:42d0e38196f1 42 {onoffsignal=0;}
daniQQue 7:42d0e38196f1 43
daniQQue 0:34c739fcc3e0 44 //send signals to scope
daniQQue 18:d7695ac04de3 45 scope.set(0, emg_biceps_right ); //set emg signal to scope in channel 1
daniQQue 18:d7695ac04de3 46 scope.set(1, emg_filtered_biceps_right);
daniQQue 18:d7695ac04de3 47 scope.set(2, onoffsignal);
daniQQue 0:34c739fcc3e0 48 scope.send(); //send all the signals to the scope
daniQQue 0:34c739fcc3e0 49 }
daniQQue 18:d7695ac04de3 50
daniQQue 18:d7695ac04de3 51 void calibration(){
daniQQue 19:fb98ff1d06ed 52 if(button==0)
daniQQue 19:fb98ff1d06ed 53 {
daniQQue 19:fb98ff1d06ed 54 for(int n =0; n<2000;n++) //read for 5000 samples as calibration
daniQQue 19:fb98ff1d06ed 55 {
daniQQue 18:d7695ac04de3 56 emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 18:d7695ac04de3 57 emg_filtered_high_biceps_right= filterhigh.step(emg_biceps_right); //highpass
daniQQue 18:d7695ac04de3 58 emg_abs_biceps_right=fabs(emg_filtered_high_biceps_right); //fabs because float
daniQQue 18:d7695ac04de3 59 emg_filtered_biceps_right=filterlow.step(emg_abs_biceps_right); //lowpass to envelope
daniQQue 19:fb98ff1d06ed 60
daniQQue 19:fb98ff1d06ed 61 if (emg_filtered_biceps_right > max_right_biceps) //determine what the highest reachable emg signal is
daniQQue 19:fb98ff1d06ed 62 {
daniQQue 19:fb98ff1d06ed 63 max_right_biceps = emg_filtered_biceps_right;
daniQQue 18:d7695ac04de3 64 }
daniQQue 19:fb98ff1d06ed 65 }
daniQQue 19:fb98ff1d06ed 66 cut_off_value=0.2*max_right_biceps;
daniQQue 19:fb98ff1d06ed 67 pc.printf(" change of cv %f ",cut_off_value );
daniQQue 18:d7695ac04de3 68 }
daniQQue 19:fb98ff1d06ed 69 }
daniQQue 0:34c739fcc3e0 70 //program
daniQQue 0:34c739fcc3e0 71
daniQQue 0:34c739fcc3e0 72 int main()
daniQQue 18:d7695ac04de3 73 {
daniQQue 19:fb98ff1d06ed 74 pc.baud(115200); //start pc connection baudrate 115200
daniQQue 4:7d9ca9c1dcce 75
daniQQue 20:a0495210915b 76 sample_timer.attach (&filter, 0.001) //continously execute the EMG reader and filter, it ensures that filter and sampling is executed every 1/frequency seconds
daniQQue 20:a0495210915b 77 sample_timer.attach (&calibration,0.001); //continously execute callibration, only affects it when button is pressed for a while.
daniQQue 0:34c739fcc3e0 78 //endless loop
daniQQue 18:d7695ac04de3 79 while(1) {
daniQQue 19:fb98ff1d06ed 80
daniQQue 8:cd0cb71b69f2 81 if (onoffsignal==1)
daniQQue 8:cd0cb71b69f2 82 {
daniQQue 8:cd0cb71b69f2 83 richting_motor1 = 0; //motordirection (ccw)
daniQQue 10:7255b59224cc 84 pwm_motor1 = 1; //motorspeed 1
daniQQue 8:cd0cb71b69f2 85
daniQQue 8:cd0cb71b69f2 86 }
daniQQue 8:cd0cb71b69f2 87 else if(onoffsignal==0)
daniQQue 8:cd0cb71b69f2 88 {
daniQQue 8:cd0cb71b69f2 89 richting_motor1 = 0; //motordirection (ccw)
daniQQue 8:cd0cb71b69f2 90 pwm_motor1 = 0; //motorspeed 0
daniQQue 18:d7695ac04de3 91 }
daniQQue 8:cd0cb71b69f2 92
daniQQue 18:d7695ac04de3 93 }
daniQQue 0:34c739fcc3e0 94 }