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

Revision:
6:ec965bb75d40
Parent:
5:46e201518dd3
Child:
7:87d9904c1c19
--- a/main.cpp	Tue Oct 27 09:28:17 2015 +0000
+++ b/main.cpp	Tue Oct 27 10:35:00 2015 +0000
@@ -6,6 +6,7 @@
 AnalogIn    emg(A0); //Analog of EMG input
 Ticker      sample_timer;
 Ticker      motor_timer;
+Ticker      cal_timer;
 HIDScope    scope(2);        // Instantize a 2-channel HIDScope object
 DigitalIn button1(PTA4);//test button for starting motor 1
 DigitalOut led1(LED_RED);
@@ -24,9 +25,12 @@
 double signalfinal;
 double onoffsignal;
 double maxcal=0;        
+bool calyes=0;
+
 /* 
  */
 void filter(){
+        if(calyes==1){
         emg_value = emg.read();//read the emg value from the elektrodes
         signalpart1 = notch.step(emg_value);//Highpass filter for removing offset and artifacts
         signalpart2 = filterhigh1.step(signalpart1);//rectify the filtered signal
@@ -37,44 +41,53 @@
         scope.set(0,emg_value);//set emg signal to scope in channel 1
         scope.set(1,onoffsignal);//set filtered signal to scope in channel 2
     scope.send();//send the signals to the scope
-        pc.printf("emg signal %f, filtered signal %f /n",emg_value,onoffsignal);
+        //pc.printf("emg signal %f, filtered signal %f \n",emg_value,onoffsignal);
+}
+}
+void checkmotor(){
+    if(calyes==1){
+    if(onoffsignal<=0.25){
+             led1.write(1);
+             led2.write(0);
+             }
+    else if(onoffsignal >= 0.5){
+                 led1.write(0);
+                 led2.write(1);
+             }
+             }
 }
 
-void checkmotor(){
-    if(onoffsignal<=0.02){
-             led1.write(0);
-             led2.write(1);
-             }
-    else if(onoffsignal >= 0.05){
-                 led1.write(1);
-                 led2.write(0);
-             }
-             }
-
+void calibri(){
+    if(button1.read()==false){
+        for(int n =0; n<5000;n++){
+            emg_value = emg.read();//read the emg value from the elektrodes
+            signalpart1 = notch.step(emg_value);//Highpass filter for removing offset and artifacts
+            signalpart2 = filterhigh1.step(signalpart1);//rectify the filtered signal
+            signalpart3 = abs(signalpart2);//low pass filter to envelope the emg
+            signalpart4 = filterlow1.step(signalpart3);//notch filter to remove 50Hz signal
+            signalfinal = filterlow2.step(signalpart4);//2nd low pass filter to envelope the emg
+            double signalmeasure = signalfinal;
+            pc.printf("%f \n", signalmeasure);
+            if (signalmeasure > maxcal){
+                maxcal = signalmeasure;
+                }
+            }
+        calyes=1;
+        pc.printf("the max emg value is %f and calyes is %d", maxcal, calyes);
+    }  
+    }
 int main()
 {
     pc.baud(115200);
     led1.write(1);
     led2.write(1);
-    bool calyes=0;
-
-    if(calyes==1){
-        pc.printf("start the emg.read");
+   
         sample_timer.attach(&filter, 0.002);//continously execute the EMG reader and filter
-        motor_timer.attach(&filter, 0.002);//continously execute the motor controller
-        }
-while(1){                     if(button1.read()==false){
-                             for(int n =0; n<5000;n++){
-                                 double signalmeasure =emg.read();
-                                 pc.printf("%f \n", signalmeasure);
-                                 if (signalmeasure > maxcal){
-                                     maxcal = signalmeasure;
-                                     }
-                                 }
-                                 calyes=1;
-                                 pc.printf("the max emg value is %f and calyes is %d", maxcal, calyes);
-                  }  
-            
+        motor_timer.attach(&checkmotor, 0.002);//continously execute the motor controller
+        cal_timer.attach(&calibri, 0.002);//ticker to check if motor is being calibrated
+        
 
+while(1){
+                                                  
 }     
 }
\ No newline at end of file