emg with text

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of emg_import by Daniqe Kottelenberg

Revision:
19:fb98ff1d06ed
Parent:
18:d7695ac04de3
Child:
20:a0495210915b
--- a/main.cpp	Mon Oct 24 12:09:25 2016 +0000
+++ b/main.cpp	Mon Oct 24 12:52:46 2016 +0000
@@ -2,6 +2,7 @@
 #include "mbed.h"
 #include "HIDScope.h"
 #include "BiQuad.h"  
+#include "MODSERIAL.h"
 
 //Define objects
 AnalogIn    emg_biceps_right_in( A0 );              //analog in to get EMG in to c++
@@ -9,10 +10,14 @@
 HIDScope    scope( 3);                              //open 3 channels in hidscope
 DigitalOut richting_motor1(D4);     //motor1 direction output    
 PwmOut pwm_motor1(D5);              //motor1 velocity  output
-DigitalOut  led(LED_GREEN);
-DigitalIn   button (D9);
+MODSERIAL pc(USBTX,USBRX);          //connection with computer
+DigitalOut  led(LED_GREEN);         //include led
+DigitalIn   button (D9);            //button 
 
+//define constant
+const int freq=1000;                //chosen sample frequency
 //define variables
+
 double emg_biceps_right;
 double emg_filtered_high_biceps_right;
 double emg_abs_biceps_right;
@@ -26,10 +31,10 @@
 
 //functions which are called in ticker
 void filter(){
-        emg_biceps_right=emg_biceps_right_in.read();                            //read the emg value from the elektrodes
-        emg_filtered_high_biceps_right= filterhigh.step(emg_biceps_right);
-        emg_abs_biceps_right=fabs(emg_filtered_high_biceps_right); //fabs because float
-        emg_filtered_biceps_right=filterlow.step(emg_abs_biceps_right);
+        emg_biceps_right                = emg_biceps_right_in.read();                   //read the emg value from the electrodes
+        emg_filtered_high_biceps_right  = filterhigh.step(emg_biceps_right);
+        emg_abs_biceps_right            =fabs(emg_filtered_high_biceps_right);          //fabs because float
+        emg_filtered_biceps_right       =filterlow.step(emg_abs_biceps_right);          //gefilterd met high, abs, low
         led=!led;
         
         if (emg_filtered_biceps_right>cut_off_value)
@@ -46,28 +51,35 @@
                 }
                 
 void calibration(){
-            for(int n =0; n<5000;n++){                                                  //read for 5000 samples as calibration
+            if(button==0)
+            {
+            for(int n =0; n<2000;n++)                                                  //read for 5000 samples as calibration
+                {
             emg_biceps_right=emg_biceps_right_in.read();                            //read the emg value from the elektrodes
             emg_filtered_high_biceps_right= filterhigh.step(emg_biceps_right);      //highpass
             emg_abs_biceps_right=fabs(emg_filtered_high_biceps_right);              //fabs because float
             emg_filtered_biceps_right=filterlow.step(emg_abs_biceps_right);         //lowpass to envelope
-            if (emg_filtered_biceps_right > max_right_biceps){                      //determine what the highest reachable emg signal is
-                max_right_biceps = emg_filtered_biceps_right;
+                        
+            if (emg_filtered_biceps_right > max_right_biceps)                    //determine what the highest reachable emg signal is
+                {
+            max_right_biceps = emg_filtered_biceps_right;
                 }
-            
-            cut_off_value=0.2*max_right_biceps;    
+                }
+            cut_off_value=0.2*max_right_biceps; 
+            pc.printf(" change of cv %f ",cut_off_value );   
             }
+                }
 //program
 
 int main()
 {
-sample_timer.attach(&filter, 0.001);        //continously execute the EMG reader and filter, it ensures that filter and sampling is executed every 1/frequency seconds
+pc.baud(115200); //start pc connection baudrate 115200
 
+sample_timer.attach (&filter, 1/freq);        //continously execute the EMG reader and filter, it ensures that filter and sampling is executed every 1/frequency seconds
+sample_timer.attach (&calibration,1/freq);    //continously execute callibration, only affects it when button is pressed for a while. 
 //endless loop
     while(1) {
-         if(button==0){
-         &calibri(); } //if button pressed calibration starts again!
-         
+                
         if (onoffsignal==1)
         {
         richting_motor1 = 0;    //motordirection (ccw)