Astrid Schut / Mbed 2 deprecated EMG_myo

Dependencies:   MovingAverage mbed HIDScope biquadFilter

Committer:
aschut
Date:
Thu Mar 28 14:09:33 2019 +0000
Revision:
23:f01a1bd3b3c4
Parent:
22:2405d2be193d
Child:
24:0db52d2e1d2a
poging tot filteren

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:32bb76391d89 1 #include "mbed.h"
vsluiter 11:ce72ec658a95 2 #include "HIDScope.h"
aschut 23:f01a1bd3b3c4 3 #include "BiQuad.h"
aschut 23:f01a1bd3b3c4 4 #include "BiQuadchains_zelfbeun.h"
aschut 23:f01a1bd3b3c4 5 DigitalOut led1(LED_GREEN);
aschut 23:f01a1bd3b3c4 6 DigitalOut led2(LED_RED);
aschut 23:f01a1bd3b3c4 7 DigitalOut led3(LED_BLUE);
vsluiter 0:32bb76391d89 8
aschut 23:f01a1bd3b3c4 9 //EMG tickers, these tickers are called in the mainscript with fsample 500Hz, also sends to HIDscope with same fsample
aschut 23:f01a1bd3b3c4 10 Ticker sample_ticker; //ticker for filtering pref. with 1000Hz, define in tick.attach
aschut 23:f01a1bd3b3c4 11 Ticker threshold_check_ticker;
aschut 23:f01a1bd3b3c4 12 Timer t; //timer try out for Astrid
aschut 23:f01a1bd3b3c4 13 Timer timer_calibration; //timer for EMG calibration
aschut 22:2405d2be193d 14
aschut 23:f01a1bd3b3c4 15 //Input
tomlankhorst 19:2bf824669684 16 AnalogIn emg1( A1 );
aschut 22:2405d2be193d 17 AnalogIn emg2( A2 );
aschut 22:2405d2be193d 18 AnalogIn emg3( A3 );
aschut 22:2405d2be193d 19 AnalogIn emg4( A4 );
tomlankhorst 19:2bf824669684 20
aschut 23:f01a1bd3b3c4 21 // GLOBALS EMG
aschut 23:f01a1bd3b3c4 22 //Filtered EMG signals from the end of the chains
aschut 23:f01a1bd3b3c4 23 volatile double emg1_filtered, emg2_filtered, emg3_filtered, emg4_filtered;
aschut 23:f01a1bd3b3c4 24 int i = 0;
aschut 23:f01a1bd3b3c4 25
aschut 23:f01a1bd3b3c4 26 //Define doubles for calibration and ticker
aschut 23:f01a1bd3b3c4 27 double ts = 0.001; //tijdsstap
aschut 23:f01a1bd3b3c4 28 double calibration_time = 55; //time EMG calibration should take
aschut 23:f01a1bd3b3c4 29
aschut 23:f01a1bd3b3c4 30 volatile double temp_highest_emg1 = 0; //highest detected value right biceps
aschut 23:f01a1bd3b3c4 31 volatile double temp_highest_emg2 = 0;
aschut 23:f01a1bd3b3c4 32 volatile double temp_highest_emg3 = 0;
aschut 23:f01a1bd3b3c4 33 volatile double temp_highest_emg4 = 0;
aschut 23:f01a1bd3b3c4 34
aschut 23:f01a1bd3b3c4 35 //Doubles for calculation threshold
aschut 23:f01a1bd3b3c4 36 double biceps_p_t = 0.4; //set threshold at percentage of highest value
aschut 23:f01a1bd3b3c4 37 double triceps_p_t = 0.5; //set threshold at percentage of highest value
aschut 23:f01a1bd3b3c4 38 volatile double threshold1;
aschut 23:f01a1bd3b3c4 39 volatile double threshold2;
aschut 23:f01a1bd3b3c4 40 volatile double threshold3;
aschut 23:f01a1bd3b3c4 41 volatile double threshold4;
aschut 23:f01a1bd3b3c4 42
aschut 23:f01a1bd3b3c4 43 // thresholdreads bools
aschut 23:f01a1bd3b3c4 44 int bicepsR;
aschut 23:f01a1bd3b3c4 45 int tricepsR;
aschut 23:f01a1bd3b3c4 46 int bicepsL;
aschut 23:f01a1bd3b3c4 47 int tricepsL;
aschut 23:f01a1bd3b3c4 48
aschut 23:f01a1bd3b3c4 49 // EMG OUTPUT
aschut 23:f01a1bd3b3c4 50 int EMGxplus;
aschut 23:f01a1bd3b3c4 51 int EMGxmin ;
aschut 23:f01a1bd3b3c4 52 int EMGyplus;
aschut 23:f01a1bd3b3c4 53 int EMGymin ;
aschut 23:f01a1bd3b3c4 54
aschut 23:f01a1bd3b3c4 55 // ~~~~~~~~~~~~~~~~~~~EMG FUNCTIONS~~~~~~~~~~~~~~~~~~
aschut 23:f01a1bd3b3c4 56 void emgsample()
aschut 23:f01a1bd3b3c4 57 {
aschut 23:f01a1bd3b3c4 58 //All EMG signal through Highpass
aschut 23:f01a1bd3b3c4 59 double emgread1 = emg1.read();
aschut 23:f01a1bd3b3c4 60 double emgread2 = emg2.read();
aschut 23:f01a1bd3b3c4 61 double emgread3 = emg3.read();
aschut 23:f01a1bd3b3c4 62 double emgread4 = emg4.read();
aschut 23:f01a1bd3b3c4 63
aschut 23:f01a1bd3b3c4 64 double emg1_highpassed = highp1.step(emgread1);
aschut 23:f01a1bd3b3c4 65 double emg2_highpassed = highp2.step(emgread2);
aschut 23:f01a1bd3b3c4 66 double emg3_highpassed = highp3.step(emgread3);
aschut 23:f01a1bd3b3c4 67 double emg4_highpassed = highp4.step(emgread4);
aschut 23:f01a1bd3b3c4 68
aschut 23:f01a1bd3b3c4 69 //All EMG highpassed through Notch
aschut 23:f01a1bd3b3c4 70 double emg1_notched = notch1.step(emg1_highpassed);
aschut 23:f01a1bd3b3c4 71 double emg2_notched = notch2.step(emg2_highpassed);
aschut 23:f01a1bd3b3c4 72 double emg3_notched = notch3.step(emg3_highpassed);
aschut 23:f01a1bd3b3c4 73 double emg4_notched = notch4.step(emg4_highpassed);
aschut 23:f01a1bd3b3c4 74
aschut 23:f01a1bd3b3c4 75 //All EMG notched rectify
aschut 23:f01a1bd3b3c4 76 double emg1_abs = abs(emg1_notched);
aschut 23:f01a1bd3b3c4 77 double emg2_abs = abs(emg2_notched);
aschut 23:f01a1bd3b3c4 78 double emg3_abs = abs(emg3_notched);
aschut 23:f01a1bd3b3c4 79 double emg4_abs = abs(emg4_notched);
aschut 23:f01a1bd3b3c4 80
aschut 23:f01a1bd3b3c4 81 //All EMG abs into lowpass
aschut 23:f01a1bd3b3c4 82 emg1_filtered = lowp1.step(emg1_abs);
aschut 23:f01a1bd3b3c4 83 emg2_filtered = lowp2.step(emg2_abs);
aschut 23:f01a1bd3b3c4 84 emg3_filtered = lowp3.step(emg3_abs);
aschut 23:f01a1bd3b3c4 85 emg4_filtered = lowp4.step(emg4_abs);
aschut 23:f01a1bd3b3c4 86
aschut 23:f01a1bd3b3c4 87 }
aschut 23:f01a1bd3b3c4 88
aschut 23:f01a1bd3b3c4 89 void CalibrationEMG()
aschut 23:f01a1bd3b3c4 90 {
aschut 23:f01a1bd3b3c4 91 //static float samples = calibration_time/ts;
aschut 23:f01a1bd3b3c4 92 while(timer_calibration<55) {
aschut 23:f01a1bd3b3c4 93 if(timer_calibration>0 && timer_calibration<10) {
aschut 23:f01a1bd3b3c4 94 led1=!led1;
aschut 23:f01a1bd3b3c4 95 if(emg1_filtered>temp_highest_emg1) {
aschut 23:f01a1bd3b3c4 96 temp_highest_emg1= emg1_filtered;
aschut 23:f01a1bd3b3c4 97 //pc.printf("Temp1 = %f \r\n",temp_highest_emg1);
aschut 23:f01a1bd3b3c4 98 }
aschut 23:f01a1bd3b3c4 99 }
aschut 23:f01a1bd3b3c4 100 if(timer_calibration>10 && timer_calibration<15) {
aschut 23:f01a1bd3b3c4 101 led1=0;
aschut 23:f01a1bd3b3c4 102 led2=0;
aschut 23:f01a1bd3b3c4 103 led3=0;
aschut 23:f01a1bd3b3c4 104 }
aschut 23:f01a1bd3b3c4 105 if(timer_calibration>15 && timer_calibration<25) {
aschut 23:f01a1bd3b3c4 106 led2=!led2;
aschut 23:f01a1bd3b3c4 107 if(emg2_filtered>temp_highest_emg2) {
aschut 23:f01a1bd3b3c4 108 temp_highest_emg2= emg2_filtered;
aschut 23:f01a1bd3b3c4 109 //pc.printf("Temp2 = %f \r\n",temp_highest_emg2);
aschut 23:f01a1bd3b3c4 110 }
aschut 23:f01a1bd3b3c4 111 }
aschut 23:f01a1bd3b3c4 112 if(timer_calibration>25 && timer_calibration<30) {
aschut 23:f01a1bd3b3c4 113 led1=0;
aschut 23:f01a1bd3b3c4 114 led2=0;
aschut 23:f01a1bd3b3c4 115 led3=0;
aschut 23:f01a1bd3b3c4 116 }
aschut 23:f01a1bd3b3c4 117 if(timer_calibration>30 && timer_calibration<40) {
aschut 23:f01a1bd3b3c4 118 led3=!led3;
aschut 23:f01a1bd3b3c4 119 if(emg3_filtered>temp_highest_emg3) {
aschut 23:f01a1bd3b3c4 120 temp_highest_emg3= emg3_filtered;
aschut 23:f01a1bd3b3c4 121 //pc.printf("Temp3 = %f \r\n",temp_highest_emg3);
aschut 23:f01a1bd3b3c4 122 }
aschut 23:f01a1bd3b3c4 123 }
aschut 23:f01a1bd3b3c4 124 if(timer_calibration>40 && timer_calibration<45) {
aschut 23:f01a1bd3b3c4 125 led1=0;
aschut 23:f01a1bd3b3c4 126 led2=0;
aschut 23:f01a1bd3b3c4 127 led3=0;
aschut 23:f01a1bd3b3c4 128 }
aschut 23:f01a1bd3b3c4 129 if(timer_calibration>45 && timer_calibration<55) {
aschut 23:f01a1bd3b3c4 130 led2=!led2;
aschut 23:f01a1bd3b3c4 131 led3=!led3;
aschut 23:f01a1bd3b3c4 132 if(emg4_filtered>temp_highest_emg4) {
aschut 23:f01a1bd3b3c4 133 temp_highest_emg4= emg4_filtered;
aschut 23:f01a1bd3b3c4 134 //pc.printf("Temp4 = %f \r\n",temp_highest_emg4);
aschut 23:f01a1bd3b3c4 135 }
aschut 23:f01a1bd3b3c4 136 }
aschut 23:f01a1bd3b3c4 137 led1=1;
aschut 23:f01a1bd3b3c4 138 led2=1;
aschut 23:f01a1bd3b3c4 139 led3=1;
aschut 23:f01a1bd3b3c4 140
aschut 23:f01a1bd3b3c4 141
aschut 23:f01a1bd3b3c4 142 }
aschut 23:f01a1bd3b3c4 143
aschut 23:f01a1bd3b3c4 144 //pc.printf("Highest value right biceps= %f \r\n", temp_highest_emg1);
aschut 23:f01a1bd3b3c4 145 //pc.printf("Highest value right triceps= %f \r\n", temp_highest_emg2);
aschut 23:f01a1bd3b3c4 146 //pc.printf("Highest value left biceps= %f \r\n", temp_highest_emg3);
aschut 23:f01a1bd3b3c4 147 //pc.printf("Highest value left triceps= %f \r\n", temp_highest_emg4);
aschut 23:f01a1bd3b3c4 148
aschut 23:f01a1bd3b3c4 149
aschut 23:f01a1bd3b3c4 150 threshold1 = temp_highest_emg1*biceps_p_t; //Right Biceps
aschut 23:f01a1bd3b3c4 151 threshold2 = temp_highest_emg2*triceps_p_t; //Right Triceps
aschut 23:f01a1bd3b3c4 152 threshold3 = temp_highest_emg3*biceps_p_t; //Left Biceps
aschut 23:f01a1bd3b3c4 153 threshold4 = temp_highest_emg4*triceps_p_t; //Left Triceps
aschut 23:f01a1bd3b3c4 154 }
aschut 23:f01a1bd3b3c4 155
aschut 23:f01a1bd3b3c4 156 //Check if emg_filtered has reached their threshold
aschut 23:f01a1bd3b3c4 157 void threshold_check()
aschut 23:f01a1bd3b3c4 158 {
aschut 23:f01a1bd3b3c4 159
aschut 23:f01a1bd3b3c4 160 //EMG1 threshold check
aschut 23:f01a1bd3b3c4 161 if(emg1_filtered>threshold1) {
aschut 23:f01a1bd3b3c4 162 bicepsR = 1;
aschut 23:f01a1bd3b3c4 163 } else {
aschut 23:f01a1bd3b3c4 164 bicepsR= 0;
aschut 23:f01a1bd3b3c4 165 }
aschut 23:f01a1bd3b3c4 166 //EMG2 threshold check
aschut 23:f01a1bd3b3c4 167 if(emg2_filtered>threshold2) {
aschut 23:f01a1bd3b3c4 168 tricepsR = 1;
aschut 23:f01a1bd3b3c4 169 } else {
aschut 23:f01a1bd3b3c4 170 tricepsR= 0;
aschut 23:f01a1bd3b3c4 171 }
aschut 23:f01a1bd3b3c4 172 //EMG3 threshold check
aschut 23:f01a1bd3b3c4 173 if(emg3_filtered>threshold3) {
aschut 23:f01a1bd3b3c4 174 bicepsL = 1;
aschut 23:f01a1bd3b3c4 175 } else {
aschut 23:f01a1bd3b3c4 176 bicepsL= 0;
aschut 23:f01a1bd3b3c4 177 }
aschut 23:f01a1bd3b3c4 178 //EMG4 threshold check
aschut 23:f01a1bd3b3c4 179 if(emg4_filtered>threshold4) {
aschut 23:f01a1bd3b3c4 180 tricepsL = 1;
aschut 23:f01a1bd3b3c4 181 } else {
aschut 23:f01a1bd3b3c4 182 tricepsL= 0;
aschut 23:f01a1bd3b3c4 183 }
aschut 23:f01a1bd3b3c4 184
aschut 23:f01a1bd3b3c4 185 /*
aschut 23:f01a1bd3b3c4 186 pc.printf("Biceps Right = %i", bicepsR);
aschut 23:f01a1bd3b3c4 187 pc.printf("Triceps Right = %i",tricepsR);
aschut 23:f01a1bd3b3c4 188 pc.printf("Biceps Left = %i", bicepsL);
aschut 23:f01a1bd3b3c4 189 pc.printf("Triceps Left = %i", tricepsL);
aschut 23:f01a1bd3b3c4 190 */
aschut 23:f01a1bd3b3c4 191
aschut 23:f01a1bd3b3c4 192 }
aschut 23:f01a1bd3b3c4 193
aschut 23:f01a1bd3b3c4 194
tomlankhorst 14:f83354387756 195 Ticker sample_timer;
aschut 22:2405d2be193d 196 HIDScope scope( 4 );
tomlankhorst 18:21d8e7a81cf5 197 DigitalOut led(LED1);
vsluiter 2:e314bb3b2d99 198
aschut 22:2405d2be193d 199
tomlankhorst 14:f83354387756 200 void sample()
vsluiter 2:e314bb3b2d99 201 {
aschut 22:2405d2be193d 202
aschut 22:2405d2be193d 203 scope.set(0, emg1.read() );
aschut 22:2405d2be193d 204 scope.set(1, emg2.read() );
aschut 23:f01a1bd3b3c4 205 scope.set(2, bicepsR );
aschut 23:f01a1bd3b3c4 206 scope.set(3, tricepsR);
aschut 22:2405d2be193d 207
vsluiter 11:ce72ec658a95 208 scope.send();
aschut 22:2405d2be193d 209
tomlankhorst 18:21d8e7a81cf5 210 led = !led;
vsluiter 2:e314bb3b2d99 211 }
vsluiter 0:32bb76391d89 212
vsluiter 0:32bb76391d89 213 int main()
tomlankhorst 19:2bf824669684 214 {
aschut 23:f01a1bd3b3c4 215 sample_timer.attach(&sample, 0.002);
aschut 22:2405d2be193d 216
aschut 23:f01a1bd3b3c4 217 temp_highest_emg1 = 0; //highest detected value right biceps
aschut 23:f01a1bd3b3c4 218 temp_highest_emg2 = 0;
aschut 23:f01a1bd3b3c4 219 temp_highest_emg3 = 0;
aschut 23:f01a1bd3b3c4 220 temp_highest_emg4 = 0;
aschut 23:f01a1bd3b3c4 221
aschut 23:f01a1bd3b3c4 222 timer_calibration.reset();
aschut 23:f01a1bd3b3c4 223 timer_calibration.start();
aschut 23:f01a1bd3b3c4 224
aschut 23:f01a1bd3b3c4 225 sample_ticker.attach(&emgsample, ts);
aschut 23:f01a1bd3b3c4 226 CalibrationEMG();
aschut 23:f01a1bd3b3c4 227 sample_ticker.detach();
aschut 23:f01a1bd3b3c4 228 timer_calibration.stop();
aschut 23:f01a1bd3b3c4 229 threshold_check_ticker.attach(threshold_check, ts)
aschut 23:f01a1bd3b3c4 230
aschut 23:f01a1bd3b3c4 231
tomlankhorst 15:0da764eea774 232
tomlankhorst 14:f83354387756 233 /*empty loop, sample() is executed periodically*/
tomlankhorst 15:0da764eea774 234 while(1) {}
vsluiter 0:32bb76391d89 235 }