Biorobotics / Robot-Software

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed Servo

Committer:
MaikOvermars
Date:
Mon Oct 29 14:45:01 2018 +0000
Revision:
25:734a26538711
Parent:
17:1f93c83e211f
Child:
41:e9d6fdf02074
fixed processing chain to include individual filters for the emg signals

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MaikOvermars 17:1f93c83e211f 1 #include "mbed.h"
MaikOvermars 17:1f93c83e211f 2 #include "BiQuad.h"
MaikOvermars 17:1f93c83e211f 3
MaikOvermars 17:1f93c83e211f 4 //Define objects
MaikOvermars 17:1f93c83e211f 5
MaikOvermars 17:1f93c83e211f 6 // filter chains for high pass, low pass and notch filters
MaikOvermars 25:734a26538711 7 BiQuadChain bqc0;
MaikOvermars 25:734a26538711 8 BiQuadChain bqc1;
MaikOvermars 17:1f93c83e211f 9
MaikOvermars 17:1f93c83e211f 10 // these are for sampling frequency 1000 Hz
MaikOvermars 25:734a26538711 11 BiQuad bq0low( 0.1311, 0.2622, 0.1311, -0.7478, 0.2722);
MaikOvermars 25:734a26538711 12 BiQuad bq0high( 0.9150, -1.8299, 0.9150, -1.8227, 0.8372);
MaikOvermars 25:734a26538711 13 BiQuad bq0notch( 0.9695, -1.8442, 0.9695, -1.8442, 0.9391);
MaikOvermars 25:734a26538711 14
MaikOvermars 25:734a26538711 15 BiQuad bq1low( 0.1311, 0.2622, 0.1311, -0.7478, 0.2722);
MaikOvermars 25:734a26538711 16 BiQuad bq1high( 0.9150, -1.8299, 0.9150, -1.8227, 0.8372);
MaikOvermars 25:734a26538711 17 BiQuad bq1notch( 0.9695, -1.8442, 0.9695, -1.8442, 0.9391);
MaikOvermars 17:1f93c83e211f 18
MaikOvermars 17:1f93c83e211f 19 // old filters
MaikOvermars 17:1f93c83e211f 20 //BiQuad bqlow( 0.3767, 0.7533, 0.3767, 0.3172, 0.1894);
MaikOvermars 17:1f93c83e211f 21 //BiQuad bqhigh( 0.6458, -1.2917, 0.6458, -1.1620, 0.4213);
MaikOvermars 17:1f93c83e211f 22 //BiQuad bqnotch( 0.5, 0, 0.5, 0, 0);
MaikOvermars 17:1f93c83e211f 23
MaikOvermars 17:1f93c83e211f 24 const int k = 300; // number of moving average samples
MaikOvermars 17:1f93c83e211f 25 double movavg0[k]= { }; // array with samples emg0
MaikOvermars 17:1f93c83e211f 26 double movavg1[k]= { }; // array with samples emg1
MaikOvermars 17:1f93c83e211f 27 int n = 0; // counter
MaikOvermars 17:1f93c83e211f 28 bool full = 0; // boolean to check if we have already had 300 samples
MaikOvermars 17:1f93c83e211f 29
MaikOvermars 17:1f93c83e211f 30 void processing_chain_emg(double &raw_emg_0, double &raw_emg_1, double &process_emg_0, double &process_emg_1) {
MaikOvermars 17:1f93c83e211f 31
MaikOvermars 17:1f93c83e211f 32 // we first filter high pass and notch, then rectifier and after that low pass
MaikOvermars 25:734a26538711 33 movavg0[n] = bq0low.step(fabs(bqc0.step( raw_emg_0 )));
MaikOvermars 25:734a26538711 34 movavg1[n] = bq1low.step(fabs(bqc1.step( raw_emg_1 )));
MaikOvermars 17:1f93c83e211f 35
MaikOvermars 17:1f93c83e211f 36 // moving average
MaikOvermars 17:1f93c83e211f 37 double emgavg0 = 0, emgavg1 = 0;
MaikOvermars 17:1f93c83e211f 38 if(full == 0){
MaikOvermars 17:1f93c83e211f 39 for(int i=0;i<k;i++){
MaikOvermars 17:1f93c83e211f 40 emgavg0 = emgavg0 + movavg0[i];
MaikOvermars 17:1f93c83e211f 41 emgavg1 = emgavg1 + movavg1[i];
MaikOvermars 17:1f93c83e211f 42 }
MaikOvermars 17:1f93c83e211f 43 emgavg0 = emgavg0/(n+1);
MaikOvermars 17:1f93c83e211f 44 emgavg1 = emgavg1/(n+1);
MaikOvermars 17:1f93c83e211f 45 }
MaikOvermars 17:1f93c83e211f 46 else{
MaikOvermars 17:1f93c83e211f 47 for(int i=0;i<k;i++){
MaikOvermars 17:1f93c83e211f 48 emgavg0 = emgavg0 + movavg0[i];
MaikOvermars 17:1f93c83e211f 49 emgavg1 = emgavg1 + movavg1[i];
MaikOvermars 17:1f93c83e211f 50 }
MaikOvermars 17:1f93c83e211f 51 emgavg0 = emgavg0/k;
MaikOvermars 17:1f93c83e211f 52 emgavg1 = emgavg1/k;
MaikOvermars 17:1f93c83e211f 53 }
MaikOvermars 17:1f93c83e211f 54 n++;
MaikOvermars 17:1f93c83e211f 55 if(n == k){
MaikOvermars 17:1f93c83e211f 56 n = 0; // reset counter when end array reached
MaikOvermars 17:1f93c83e211f 57 full = 1; // assures we are past 300 samples
MaikOvermars 17:1f93c83e211f 58 }
MaikOvermars 17:1f93c83e211f 59
MaikOvermars 17:1f93c83e211f 60 // outputs
MaikOvermars 17:1f93c83e211f 61 process_emg_0 = emgavg0;
MaikOvermars 17:1f93c83e211f 62 process_emg_1 = emgavg1;
MaikOvermars 17:1f93c83e211f 63 }
MaikOvermars 17:1f93c83e211f 64