Combination code of movement and emg code with small changes for 2 motors.

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of EMG_converter_code by Gerlinde van de Haar

Committer:
Technical_Muffin
Date:
Tue Oct 27 09:28:17 2015 +0000
Revision:
5:46e201518dd3
Parent:
4:fd29407c3115
Child:
6:ec965bb75d40
emg code with working calibrator but doesn't start reading emg. Possibly because of the while loop

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Technical_Muffin 0:1883d922ada8 1 #include "mbed.h"
Technical_Muffin 0:1883d922ada8 2 #include "HIDScope.h"
Technical_Muffin 0:1883d922ada8 3 #include "biquadFilter.h" // Require the HIDScope library
Technical_Muffin 3:a69f041108d4 4 #include "MODSERIAL.h"
Technical_Muffin 0:1883d922ada8 5 //Define objects
Technical_Muffin 0:1883d922ada8 6 AnalogIn emg(A0); //Analog of EMG input
Technical_Muffin 0:1883d922ada8 7 Ticker sample_timer;
Technical_Muffin 5:46e201518dd3 8 Ticker motor_timer;
Technical_Muffin 0:1883d922ada8 9 HIDScope scope(2); // Instantize a 2-channel HIDScope object
Technical_Muffin 2:83659da3e5fe 10 DigitalIn button1(PTA4);//test button for starting motor 1
Technical_Muffin 2:83659da3e5fe 11 DigitalOut led1(LED_RED);
Technical_Muffin 3:a69f041108d4 12 DigitalOut led2(LED_BLUE);
Technical_Muffin 3:a69f041108d4 13 MODSERIAL pc(USBTX,USBRX);
Technical_Muffin 0:1883d922ada8 14 /*The biquad filters required to transform the EMG signal into an usable signal*/
Technical_Muffin 0:1883d922ada8 15 biquadFilter filterhigh1(-1.1430, 0.4128, 0.6389, -1.2779, 0.6389);
Technical_Muffin 0:1883d922ada8 16 biquadFilter filterlow1(1.9556, 0.9565, 0.9780, 1.9561, 0.9780);
Technical_Muffin 0:1883d922ada8 17 biquadFilter notch(-1.1978e-16, 0.9561, 0.9780, -1.1978e-16, 0.9780);
Technical_Muffin 0:1883d922ada8 18 biquadFilter filterlow2(-1.9645, 0.9651, 1.5515e-4, 3.1030e-4, 1.5515e-4);
Technical_Muffin 1:9913e3886643 19 double emg_value;
Technical_Muffin 1:9913e3886643 20 double signalpart1;
Technical_Muffin 1:9913e3886643 21 double signalpart2;
Technical_Muffin 1:9913e3886643 22 double signalpart3;
Technical_Muffin 1:9913e3886643 23 double signalpart4;
Technical_Muffin 1:9913e3886643 24 double signalfinal;
Technical_Muffin 2:83659da3e5fe 25 double onoffsignal;
Technical_Muffin 4:fd29407c3115 26 double maxcal=0;
Technical_Muffin 0:1883d922ada8 27 /*
Technical_Muffin 0:1883d922ada8 28 */
Technical_Muffin 2:83659da3e5fe 29 void filter(){
Technical_Muffin 1:9913e3886643 30 emg_value = emg.read();//read the emg value from the elektrodes
Technical_Muffin 3:a69f041108d4 31 signalpart1 = notch.step(emg_value);//Highpass filter for removing offset and artifacts
Technical_Muffin 3:a69f041108d4 32 signalpart2 = filterhigh1.step(signalpart1);//rectify the filtered signal
Technical_Muffin 3:a69f041108d4 33 signalpart3 = abs(signalpart2);//low pass filter to envelope the emg
Technical_Muffin 3:a69f041108d4 34 signalpart4 = filterlow1.step(signalpart3);//notch filter to remove 50Hz signal
Technical_Muffin 1:9913e3886643 35 signalfinal = filterlow2.step(signalpart4);//2nd low pass filter to envelope the emg
Technical_Muffin 2:83659da3e5fe 36 onoffsignal=signalfinal/maxcal;//divide the emg signal by the max EMG to calibrate the signal per person
Technical_Muffin 1:9913e3886643 37 scope.set(0,emg_value);//set emg signal to scope in channel 1
Technical_Muffin 2:83659da3e5fe 38 scope.set(1,onoffsignal);//set filtered signal to scope in channel 2
Technical_Muffin 1:9913e3886643 39 scope.send();//send the signals to the scope
Technical_Muffin 5:46e201518dd3 40 pc.printf("emg signal %f, filtered signal %f /n",emg_value,onoffsignal);
Technical_Muffin 0:1883d922ada8 41 }
Technical_Muffin 5:46e201518dd3 42
Technical_Muffin 5:46e201518dd3 43 void checkmotor(){
Technical_Muffin 5:46e201518dd3 44 if(onoffsignal<=0.02){
Technical_Muffin 5:46e201518dd3 45 led1.write(0);
Technical_Muffin 5:46e201518dd3 46 led2.write(1);
Technical_Muffin 5:46e201518dd3 47 }
Technical_Muffin 5:46e201518dd3 48 else if(onoffsignal >= 0.05){
Technical_Muffin 5:46e201518dd3 49 led1.write(1);
Technical_Muffin 5:46e201518dd3 50 led2.write(0);
Technical_Muffin 5:46e201518dd3 51 }
Technical_Muffin 5:46e201518dd3 52 }
Technical_Muffin 3:a69f041108d4 53
Technical_Muffin 0:1883d922ada8 54 int main()
Technical_Muffin 0:1883d922ada8 55 {
Technical_Muffin 4:fd29407c3115 56 pc.baud(115200);
Technical_Muffin 3:a69f041108d4 57 led1.write(1);
Technical_Muffin 3:a69f041108d4 58 led2.write(1);
Technical_Muffin 4:fd29407c3115 59 bool calyes=0;
Technical_Muffin 4:fd29407c3115 60
Technical_Muffin 4:fd29407c3115 61 if(calyes==1){
Technical_Muffin 5:46e201518dd3 62 pc.printf("start the emg.read");
Technical_Muffin 4:fd29407c3115 63 sample_timer.attach(&filter, 0.002);//continously execute the EMG reader and filter
Technical_Muffin 5:46e201518dd3 64 motor_timer.attach(&filter, 0.002);//continously execute the motor controller
Technical_Muffin 4:fd29407c3115 65 }
Technical_Muffin 5:46e201518dd3 66 while(1){ if(button1.read()==false){
Technical_Muffin 5:46e201518dd3 67 for(int n =0; n<5000;n++){
Technical_Muffin 5:46e201518dd3 68 double signalmeasure =emg.read();
Technical_Muffin 5:46e201518dd3 69 pc.printf("%f \n", signalmeasure);
Technical_Muffin 5:46e201518dd3 70 if (signalmeasure > maxcal){
Technical_Muffin 5:46e201518dd3 71 maxcal = signalmeasure;
Technical_Muffin 5:46e201518dd3 72 }
Technical_Muffin 4:fd29407c3115 73 }
Technical_Muffin 4:fd29407c3115 74 calyes=1;
Technical_Muffin 5:46e201518dd3 75 pc.printf("the max emg value is %f and calyes is %d", maxcal, calyes);
Technical_Muffin 4:fd29407c3115 76 }
Technical_Muffin 5:46e201518dd3 77
Technical_Muffin 5:46e201518dd3 78
Technical_Muffin 5:46e201518dd3 79 }
Technical_Muffin 0:1883d922ada8 80 }