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.
Dependencies: MovingAverage mbed HIDScope biquadFilter
Diff: main.cpp
- Revision:
- 23:f01a1bd3b3c4
- Parent:
- 22:2405d2be193d
- Child:
- 24:0db52d2e1d2a
--- a/main.cpp Thu Mar 28 13:26:30 2019 +0000 +++ b/main.cpp Thu Mar 28 14:09:33 2019 +0000 @@ -1,13 +1,197 @@ #include "mbed.h" #include "HIDScope.h" +#include "BiQuad.h" +#include "BiQuadchains_zelfbeun.h" +DigitalOut led1(LED_GREEN); +DigitalOut led2(LED_RED); +DigitalOut led3(LED_BLUE); -//Define objects +//EMG tickers, these tickers are called in the mainscript with fsample 500Hz, also sends to HIDscope with same fsample +Ticker sample_ticker; //ticker for filtering pref. with 1000Hz, define in tick.attach +Ticker threshold_check_ticker; +Timer t; //timer try out for Astrid +Timer timer_calibration; //timer for EMG calibration +//Input AnalogIn emg1( A1 ); AnalogIn emg2( A2 ); AnalogIn emg3( A3 ); AnalogIn emg4( A4 ); +// GLOBALS EMG +//Filtered EMG signals from the end of the chains +volatile double emg1_filtered, emg2_filtered, emg3_filtered, emg4_filtered; +int i = 0; + +//Define doubles for calibration and ticker +double ts = 0.001; //tijdsstap +double calibration_time = 55; //time EMG calibration should take + +volatile double temp_highest_emg1 = 0; //highest detected value right biceps +volatile double temp_highest_emg2 = 0; +volatile double temp_highest_emg3 = 0; +volatile double temp_highest_emg4 = 0; + +//Doubles for calculation threshold +double biceps_p_t = 0.4; //set threshold at percentage of highest value +double triceps_p_t = 0.5; //set threshold at percentage of highest value +volatile double threshold1; +volatile double threshold2; +volatile double threshold3; +volatile double threshold4; + +// thresholdreads bools +int bicepsR; +int tricepsR; +int bicepsL; +int tricepsL; + +// EMG OUTPUT +int EMGxplus; +int EMGxmin ; +int EMGyplus; +int EMGymin ; + +// ~~~~~~~~~~~~~~~~~~~EMG FUNCTIONS~~~~~~~~~~~~~~~~~~ +void emgsample() +{ + //All EMG signal through Highpass + double emgread1 = emg1.read(); + double emgread2 = emg2.read(); + double emgread3 = emg3.read(); + double emgread4 = emg4.read(); + + double emg1_highpassed = highp1.step(emgread1); + double emg2_highpassed = highp2.step(emgread2); + double emg3_highpassed = highp3.step(emgread3); + double emg4_highpassed = highp4.step(emgread4); + + //All EMG highpassed through Notch + double emg1_notched = notch1.step(emg1_highpassed); + double emg2_notched = notch2.step(emg2_highpassed); + double emg3_notched = notch3.step(emg3_highpassed); + double emg4_notched = notch4.step(emg4_highpassed); + + //All EMG notched rectify + double emg1_abs = abs(emg1_notched); + double emg2_abs = abs(emg2_notched); + double emg3_abs = abs(emg3_notched); + double emg4_abs = abs(emg4_notched); + + //All EMG abs into lowpass + emg1_filtered = lowp1.step(emg1_abs); + emg2_filtered = lowp2.step(emg2_abs); + emg3_filtered = lowp3.step(emg3_abs); + emg4_filtered = lowp4.step(emg4_abs); + +} + +void CalibrationEMG() +{ + //static float samples = calibration_time/ts; + while(timer_calibration<55) { + if(timer_calibration>0 && timer_calibration<10) { + led1=!led1; + if(emg1_filtered>temp_highest_emg1) { + temp_highest_emg1= emg1_filtered; + //pc.printf("Temp1 = %f \r\n",temp_highest_emg1); + } + } + if(timer_calibration>10 && timer_calibration<15) { + led1=0; + led2=0; + led3=0; + } + if(timer_calibration>15 && timer_calibration<25) { + led2=!led2; + if(emg2_filtered>temp_highest_emg2) { + temp_highest_emg2= emg2_filtered; + //pc.printf("Temp2 = %f \r\n",temp_highest_emg2); + } + } + if(timer_calibration>25 && timer_calibration<30) { + led1=0; + led2=0; + led3=0; + } + if(timer_calibration>30 && timer_calibration<40) { + led3=!led3; + if(emg3_filtered>temp_highest_emg3) { + temp_highest_emg3= emg3_filtered; + //pc.printf("Temp3 = %f \r\n",temp_highest_emg3); + } + } + if(timer_calibration>40 && timer_calibration<45) { + led1=0; + led2=0; + led3=0; + } + if(timer_calibration>45 && timer_calibration<55) { + led2=!led2; + led3=!led3; + if(emg4_filtered>temp_highest_emg4) { + temp_highest_emg4= emg4_filtered; + //pc.printf("Temp4 = %f \r\n",temp_highest_emg4); + } + } + led1=1; + led2=1; + led3=1; + + + } + + //pc.printf("Highest value right biceps= %f \r\n", temp_highest_emg1); + //pc.printf("Highest value right triceps= %f \r\n", temp_highest_emg2); + //pc.printf("Highest value left biceps= %f \r\n", temp_highest_emg3); + //pc.printf("Highest value left triceps= %f \r\n", temp_highest_emg4); + + + threshold1 = temp_highest_emg1*biceps_p_t; //Right Biceps + threshold2 = temp_highest_emg2*triceps_p_t; //Right Triceps + threshold3 = temp_highest_emg3*biceps_p_t; //Left Biceps + threshold4 = temp_highest_emg4*triceps_p_t; //Left Triceps +} + +//Check if emg_filtered has reached their threshold +void threshold_check() +{ + + //EMG1 threshold check + if(emg1_filtered>threshold1) { + bicepsR = 1; + } else { + bicepsR= 0; + } + //EMG2 threshold check + if(emg2_filtered>threshold2) { + tricepsR = 1; + } else { + tricepsR= 0; + } + //EMG3 threshold check + if(emg3_filtered>threshold3) { + bicepsL = 1; + } else { + bicepsL= 0; + } + //EMG4 threshold check + if(emg4_filtered>threshold4) { + tricepsL = 1; + } else { + tricepsL= 0; + } + + /* + pc.printf("Biceps Right = %i", bicepsR); + pc.printf("Triceps Right = %i",tricepsR); + pc.printf("Biceps Left = %i", bicepsL); + pc.printf("Triceps Left = %i", tricepsL); + */ + +} + + Ticker sample_timer; HIDScope scope( 4 ); DigitalOut led(LED1); @@ -18,8 +202,8 @@ scope.set(0, emg1.read() ); scope.set(1, emg2.read() ); - scope.set(2, emg3.read() ); - scope.set(3, emg4.read() ); + scope.set(2, bicepsR ); + scope.set(3, tricepsR); scope.send(); @@ -28,8 +212,23 @@ int main() { + sample_timer.attach(&sample, 0.002); - sample_timer.attach(&sample, 0.002); + temp_highest_emg1 = 0; //highest detected value right biceps + temp_highest_emg2 = 0; + temp_highest_emg3 = 0; + temp_highest_emg4 = 0; + + timer_calibration.reset(); + timer_calibration.start(); + + sample_ticker.attach(&emgsample, ts); + CalibrationEMG(); + sample_ticker.detach(); + timer_calibration.stop(); + threshold_check_ticker.attach(threshold_check, ts) + + /*empty loop, sample() is executed periodically*/ while(1) {}